2016.10.25 20:02 "[Tiff] problem adding YRESOLUTION and RESOLUTIONUNIT to 16bit grayscale TIFF", by Paul Hemmer

2016.10.25 20:02 "[Tiff] problem adding YRESOLUTION and RESOLUTIONUNIT to 16bit grayscale TIFF", by Paul Hemmer

Hello!

The following code saves a TIF file which I can open and see correctly in a variety of image viewers. The problem is described following.

Note that in the code below, the "l_thumb" structure holds the filename, width, height, pixels_per_cm and buffer data for a 16bit grayscale image.

I have compiled libTiff4.0 on a 64bit Win10 machine using Visual Studio 2012.

TIFF* tif = TIFFOpen(l_thumb->filename, "w");

TIFFSetField(tif, TIFFTAG_IMAGEWIDTH     , l_thumb->width        );
TIFFSetField(tif, TIFFTAG_IMAGELENGTH    , l_thumb->height       );
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE  , 16                    );
TIFFSetField(tif, TIFFTAG_COMPRESSION    , COMPRESSION_LZW       );
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC    , PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_FILLORDER      , FILLORDER_MSB2LSB     );
TIFFSetField(tif, TIFFTAG_MAKE           , "My Company Name"     );
TIFFSetField(tif, TIFFTAG_MODEL          , "My Product"         );
TIFFSetField(tif, TIFFTAG_ORIENTATION    , ORIENTATION_TOPLEFT   );
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP , l_thumb->height   );
//TIFFSetField(tif, TIFFTAG_XRESOLUTION    , l_thumb->pixels_per_cm);
//TIFFSetField(tif, TIFFTAG_YRESOLUTION    , l_thumb->pixels_per_cm);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG   , PLANARCONFIG_CONTIG   );

TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER );

unsigned long l_buffer_size = l_thumb->width*2;
tdata_t l_buffer;

l_buffer = _TIFFmalloc(l_buffer_size);

for (int row = 0; row < l_thumb->height; row++){ memcpy_s((byte*)l_buffer, l_buffer_size, &l_thumb->data[row*l_thumb->width], l_buffer_size);

int ret=TIFFWriteScanline(tif, l_buffer, row, 0);
if (ret==-1){
TIFFClose(tif);
return PACKET_TYPE_ERROR;
}
}

TIFFClose(tif);
free(l_thumb->data);

I can open the image and see what I expect to see. When I view the header tags, I can see all values set correctly including the final "Centimeter" resolution unit tag.

However, notice I've commented out the X/Y resolution tags. If I put those lines back, suddenly I continue to see XResolution, but Y Resolution and ResolutionUnit are missing. Instead I see this and I am at a loss to explain why:

XResolution     (1 Rational): 115
0               (0 NoType):
1               (2051838 NoType):

I have tried hard-coding values like "115" and "115.00" and tried making l_thumb->pixels_per_cm double, float, int, and the results are always the same.

I have also tried compiling and running against libTiff4.1 (BigTIFF) because ultimately I will need files > 4GB in size. But with either version of the library, I simply cannot seem to insert the resolution tags. Also, TIFFSetField on those tags is always returning 1, success.

Any idea where I am going wrong?

Thanks!
-Paul