TIFF读写TIFFTAG_ROWSPERSTRIP依赖

时间:2016-03-18 17:09:56

标签: c++ io tiff libtiff

我的代码逐行读取(tiffIn)并写入(tiffOut)tiff图像,我用#include "tiffio.h"打开一个Tiff文件并读取每个标记并将其完全相同在新的tiff。我改变的唯一标签是:

int rows_per_strip = 65536 * 8 /(nc * nx * bps);
if (rows_per_strip == 0) rows_per_strip = 1;

其中nc,nx,ny和bps为:

TIFFGetField (tiffIn, TIFFTAG_IMAGELENGTH, &ny)
TIFFGetField (tiffOut, TIFFTAG_IMAGEWIDTH, &nx);
TIFFGetField (tiffOut, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField (tiffOut, TIFFTAG_SAMPLESPERPIXEL, &nc);

rows_per_strip是:

TIFFSetField (tiffOut, TIFFTAG_ROWSPERSTRIP,    rows_per_strip);

读取和写入的for循环可以概括为:

for(int i = 0; i<ny; i++){
unsigned char * dataptr=new unsigned char[sll];
TIFFReadScanline(tiffIn, dataptr, line, 0);
TIFFWriteScanline (tiffOut, dataptr, line);
delete[] dataptr;
}

其中sll是:

sll = (unsigned int) TIFFScanlineSize (tiffIn);

当我用gimp打开写入的TIFF文件时出现错误:

 Wrong "StripByteCounts" field, ignoring and calculating from imagelength

并以这种方式显示图像:

wrongly written tiff

但是我用以下内容读取并写下了标签:

 unsigned short int stmp;
 unsigned short int striptBytesCount;

 TIFFGetField(tiffIn, TIFFTAG_STRIPBYTECOUNTS,&stmp);
 striptBytesCount=stmp;

 TIFFSetField (tiffOut, TIFFTAG_STRIPBYTECOUNTS, striptBytesCount);

所以应该是对的。 此外,如果我更改rows_per_strip代码并指定1,则图片会正确显示。

0 个答案:

没有答案