skip to Main Content

I have a problem with saving an image using the LibTIFF library.

In my code I have an array of floats representing the pixel values of the image. When I save these values to a RAW file, it looks fine. However, when I try to save it as a TIFF file using the LibTIFF library, the image looks perfectly fine when I open it in ImageJ, however when I open it in Photoshop or Windows, it looks strange (look below), and Gimp shows a completely transparent image (like there was no data in the image at all).

I use ImageJ to open the RAW file, image type is set to 32bit – Real and Little-endian byte order is ticked.

RAW on the left, TIFF on the right

Here’s the code I use to save the TIFF image:

TIFF *tif= TIFFOpen(name, "w");

TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField (tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField (tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField (tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);

tsize_t strip_size = TIFFStripSize (tif);
tstrip_t strips_num = TIFFNumberOfStrips (tif); 

float* strip_buf=(float*)_TIFFmalloc(strip_size);
for (unsigned int s=0; s<strips_num; s++) {
    for (unsigned int col=0; col<width; col++) {
        strip_buf[col]=image[s][col];
    }
    TIFFWriteEncodedStrip (tif, s, strip_buf, strip_size);

}
_TIFFfree(strip_buf);
TIFFClose(tif);

I’m pretty sure something’s wrong with the way I’m saving the file – but I have no idea what. Thanks a lot for your help!

2

Answers


  1. I doubt there is something wrong in the way you’re saving it – it is just that the Windows Image Viewer can’t properly handle TIFFs with 32 bit. Not knowing anything about LibTIFF, I’d assume you could change the bitdepth of the created TIFF by adjusting the corresponding line in your code to 8 instead of 32, so it reads like this:

    TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, 8);
    

    However, please be aware that this will lead to a loss of information if your original data had more intensity values than what can be represented with 8 bits. And I have no idea how LibTIFF does the scaling there.

    But just because Windows / Gimp / Photoshop don’t display them correctly doesn’t mean the TIFF files are broken.

    Login or Signup to reply.
  2. You may need to add following line to indicate the image is of floating 32 bit.

        TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
    

    And then you should not have any problem with a program that can read a float image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search