1 Star 0 Fork 1

ctguhzy/GeoTiffTools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
GeoTiffWriter.cxx 3.22 KB
一键复制 编辑 原始数据 按行查看 历史
ctguhzy 提交于 2022-05-02 00:03 +08:00 . Added AreaIntegrator.
#include <xtiffio.h>
#include "GeoTiffWriter.hxx"
#include "GeoTiffImage.hxx"
GeoTiffWriter::GeoTiffWriter()
{
_fileName[0] = '\0';
_image = 0;
}
GeoTiffWriter::~GeoTiffWriter()
{
}
void GeoTiffWriter::write()
{
if( _image == 0 )
{
fprintf(stderr, "Need a GeoTiffImage to write.\n");
return;
}
TIFF *tif = 0;
tif = XTIFFOpen(_fileName, "w");
if (!tif) {
fprintf(stderr, "Can't create test TIFF file %s.\n", _fileName);
return;
}
setUpTIFFDirectory(tif);
double scale[3] = { 0.0, 0.0, 0.0 };
double pos[6] = { 0.0, 0.0,0.0,0.0, 0.0,0.0};
_image->getGeoScale(scale[0], scale[1], scale[2]);
_image->getGeoModelCoord(pos[3], pos[4], pos[5]);
TIFFSetField(tif, TIFFTAG_GEOPIXELSCALE, 3, scale);
TIFFSetField(tif, TIFFTAG_GEOTIEPOINTS, 6, pos);
// TIFFSetField(tif, TIFFTAG_GEOKEYDIRECTORY, 56, tifImage->getGeoKeyDirectory());
// TIFFSetField(tif, TIFFTAG_GEODOUBLEPARAMS, 4, tifImage->getGeoDoubleParams());
// TIFFSetField(tif, TIFFTAG_GEOASCIIPARAMS, tifImage->getGeoAsciiParams());
int lineSize = _image->getWidth() * _image->getChannels() * _image->getDepth() / 8;
for (int i = 0; i < _image->getHeight(); i++)
{
unsigned char* buf = _image->getImageData() + i * lineSize;
if (TIFFWriteScanline(tif, buf, i, 0) == -1)
{
fprintf(stderr, "Can't write image data.\n");
XTIFFClose(tif);
}
}
XTIFFClose(tif);
}
void GeoTiffWriter::setUpTIFFDirectory(TIFF* tif)
{
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, _image->getWidth());
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, _image->getHeight());
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, _image->getWidth() * _image->getDepth() / 8 );
}
void GeoTiffWriter::write(IplImage* image, GeoTiffImage* tifImage)
{
TIFF *tif = 0;
tif = XTIFFOpen(_fileName, "w");
if (!tif) {
fprintf(stderr, "Can't create test TIFF file %s.\n", _fileName);
return;
}
setUpTIFFDirectory(tif);
double scale[3] = { 0.0, 0.0, 0.0 };
double pos[6] = { 0.0, 0.0,0.0,0.0, 0.0,0.0};
tifImage->getGeoScale(scale[0], scale[1], scale[2]);
tifImage->getGeoModelCoord(pos[3], pos[4], pos[5]);
TIFFSetField(tif, TIFFTAG_GEOPIXELSCALE, 3, scale);
TIFFSetField(tif, TIFFTAG_GEOTIEPOINTS, 6, pos);
// TIFFSetField(tif, TIFFTAG_GEOKEYDIRECTORY, 56, tifImage->getGeoKeyDirectory());
// TIFFSetField(tif, TIFFTAG_GEODOUBLEPARAMS, 4, tifImage->getGeoDoubleParams());
// TIFFSetField(tif, TIFFTAG_GEOASCIIPARAMS, tifImage->getGeoAsciiParams());
int bufSize = image->width * image->nChannels * image->depth / 8;
char* buf = (char*)malloc(bufSize);
for (int i = 0; i < image->height; i++)
{
memcpy(buf, image->imageData + bufSize * i, bufSize);
if (TIFFWriteScanline(tif, buf, i, 0) == -1)
{
fprintf(stderr, "Can't write image data.\n");
XTIFFClose(tif);
}
}
XTIFFClose(tif);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ctguhzy/GeoTiffTools.git
git@gitee.com:ctguhzy/GeoTiffTools.git
ctguhzy
GeoTiffTools
GeoTiffTools
master

搜索帮助