My code reads (tiffIn) and write (tiffOut) a tiff image line by line, I open a Tiff file with #include "tiffio.h" and read each tag and copy it exactly the same in the new tiff. The only tag that I change is:
int rows_per_strip = 65536 * 8 /(nc * nx * bps);
if (rows_per_strip == 0) rows_per_strip = 1;
where nc, nx, ny and bps are:
TIFFGetField (tiffIn, TIFFTAG_IMAGELENGTH, &ny)
TIFFGetField (tiffOut, TIFFTAG_IMAGEWIDTH, &nx);
TIFFGetField (tiffOut, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField (tiffOut, TIFFTAG_SAMPLESPERPIXEL, &nc);
and rows_per_strip is:
TIFFSetField (tiffOut, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
the for loop that reads and write can be summarized as:
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;
}
where sll is:
sll = (unsigned int) TIFFScanlineSize (tiffIn);
When I open the written TIFF file with gimp I get an error:
Wrong "StripByteCounts" field, ignoring and calculating from imagelength
and the image is displayed in this way:
but I read and write the tag with:
unsigned short int stmp;
unsigned short int striptBytesCount;
TIFFGetField(tiffIn, TIFFTAG_STRIPBYTECOUNTS,&stmp);
striptBytesCount=stmp;
TIFFSetField (tiffOut, TIFFTAG_STRIPBYTECOUNTS, striptBytesCount);
So it should be right.
moreover, if I change the rows_per_strip tag, and assign 1, the image is displayed correctly.
