FreeImage 3.18.0 loads the attached tiff as a 32 bit rgba FIT_BITMAP. It is really a 32 bit greyscale + alpha (16 bit per channel, 2 channels) image. It should therefore either be loaded as a FIT_UINT16 or converted to a FIT_RGBA16.
I've tested the subject TIFF file; the resultant image I got is a colored one (instead of a grayscale one), e.g. the RGBA values in the inner area acound the center are 176, 92, 255, 255 (instead of 92, 92, 92, 255). This point is what I feel puzzled about. Whether FreeImage processes the file on a 16-bit basis or takes the low bytes only, the resultant pixels should be of grayscale in any case, not colored, since PhotometricInterpretation in file is "1".
On the other hand, there are two discrepancies in the source file: (i) the tag count is coded 19, but the actual tag listing I got has 18 tags only. (ii) ExtraSamples is coded as "1", Associated Alpha Data. This is confusing to me. Should it be "2" (Unassociated Alpha Data)?
The "StripsPerImage" calculation I got is 1350, exactly the same as ImageHeight. A bit odd to me; is this an intended one?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The reason it displays as colored is that I think it's interpretting two 16 bit values as four 8 bit values. The second value is the alpha which at the centre is 100%, or 0xFFFF which is then interpreted as blue = 0xFF and alpha = 0xFF (255). The grey scale amount at the centre, which will be the first 16 bit value, will be something between 0 and 100%, in the case you sampled it would appear to be 0xB05C (45148, about 70%), which is then interpreted as red = 0xB0 (176) and green = 0x5C (92).
Last edit: Samuel Morris 2018-09-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I see your point, and I thank you for the explanation on the "colored display".
Each TIFF tag has "Tag ID", "Type", "Units" and "Value/offset" fields (totalling 12 bytes); the last field is supposedly to be for an UnsignedLong value. However, Photoshop's practice is, in this case, having two UnsignedShort values "jammed" into the last 4-byte field (per the attached screenshot, the Value/offset field of BitsPerSample is entered as "10 00 10 00", instead of "10 00 00 00", Little Endian) . Unless somewhere there is an official documentation allowing this kind of entry, I consider it being a deviation from the base TIFF specification.
I might be wrong, as I am very much an amateur in this kind of stuff. I just try to understand the rationale behind FreeImage/LibTIFF's handling of the subject TIFF file.
Unless somewhere there is an official documentation allowing this kind of entry, I consider it being a deviation from the base TIFF specification.
If that is the case then I understand FreeImage not handling this. If it helps this image was generated in Affinity Photo.
I just try to understand the rationale behind FreeImage/LibTIFF's handling of the subject TIFF file
I might be wrong, but I think that LibTIFF handles it fine, it's just how FreeImage interpretes the data as a FIT_BITMAP (due to that being the default and the missing branch above). I've not looked very far into the code yet though.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Below I upload a 16-bit grayscale with alpha TIFF file which is coded in a similar manner as your TIFF file, i.e. "BitsPerSample 16 and SamplesPerPixel 2", with ExtraSamples 0 (i.e. Unspecified).
The thumbnail image in the appended TIFF Tag Listing is by GDIplus, not FreeImage. We can tell that GDIplus can honour the ICC profile in the file. (However, in order to honour its ICC, I had to explicitly resave the file to an 8 bit one first during the process, also using GDIplus. If I am just to display the image without ICC effect, no such conversion is needed, but the alpha bytes I got are all of 255 for this file).
As shown in the screenshot, this file is by "Adobe Photoshop CS Macintosh", specifically for ICC-conversion testing purposes. For ready reference, screenshots of both before and after ICC-conversion are appended.
ICC-conversion is not an issue of this thread, I mention it to show that GDIplus is somehow capable of processing a "BitsPerSample 16 and SamplesPerPixel 2" TIFF file, albeit alpha bytes are all of a 255 value for this type of file.
I've tested the subject TIFF file; the resultant image I got is a colored one (instead of a grayscale one), e.g. the RGBA values in the inner area acound the center are 176, 92, 255, 255 (instead of 92, 92, 92, 255). This point is what I feel puzzled about. Whether FreeImage processes the file on a 16-bit basis or takes the low bytes only, the resultant pixels should be of grayscale in any case, not colored, since PhotometricInterpretation in file is "1".
On the other hand, there are two discrepancies in the source file: (i) the tag count is coded 19, but the actual tag listing I got has 18 tags only. (ii) ExtraSamples is coded as "1", Associated Alpha Data. This is confusing to me. Should it be "2" (Unassociated Alpha Data)?
The "StripsPerImage" calculation I got is 1350, exactly the same as ImageHeight. A bit odd to me; is this an intended one?
Perhaps I should attach the tag listing and the StripsPerImage calculation. Remarks: the displayed thumbnail on TIFF Tag Listing is not by FreeImage.
Just to clarify, it turned out that the missing tag on my TIFF Tag Listing is "51168", a private tag.
Per my earlier comment, the puzzle to me is that why a grayscale image (RGB equal) is displayed as a colored one (RGB unequal).
I think part of the problem is a missing case in the else block at https://sourceforge.net/p/freeimage/svn/HEAD/tree/FreeImage/trunk/Source/FreeImage/PluginTIFF.cpp#l588 for samplesperpixel == 2.
The reason it displays as colored is that I think it's interpretting two 16 bit values as four 8 bit values. The second value is the alpha which at the centre is 100%, or 0xFFFF which is then interpreted as blue = 0xFF and alpha = 0xFF (255). The grey scale amount at the centre, which will be the first 16 bit value, will be something between 0 and 100%, in the case you sampled it would appear to be 0xB05C (45148, about 70%), which is then interpreted as red = 0xB0 (176) and green = 0x5C (92).
Last edit: Samuel Morris 2018-09-21
I see your point, and I thank you for the explanation on the "colored display".
Each TIFF tag has "Tag ID", "Type", "Units" and "Value/offset" fields (totalling 12 bytes); the last field is supposedly to be for an UnsignedLong value. However, Photoshop's practice is, in this case, having two UnsignedShort values "jammed" into the last 4-byte field (per the attached screenshot, the Value/offset field of BitsPerSample is entered as "10 00 10 00", instead of "10 00 00 00", Little Endian) . Unless somewhere there is an official documentation allowing this kind of entry, I consider it being a deviation from the base TIFF specification.
I might be wrong, as I am very much an amateur in this kind of stuff. I just try to understand the rationale behind FreeImage/LibTIFF's handling of the subject TIFF file.
From the attached TIFF file which is 8-bit grayscale with alpha, we can see that the following tags are coded as:
BitsPerSample 8
-- Remarks: Entered at four offsets, 8, 8, 8, 8
SamplesPerPixel 4
ExtraSamples 2 (i.e. Unassociated Alpha Data)
The above is from an 8-bit file, now if it is a 16-bit file, I guess FreeImage would expect it to have the same pattern of tags, i.e.
BitsPerSample 16
SamplesPerPixel 4
ExtraSample 2
For convenience, below I also append two screenshots, TIFF Tag Listing and Tag Interpretation
Last edit: Johnny Petersen 2018-09-22
If that is the case then I understand FreeImage not handling this. If it helps this image was generated in Affinity Photo.
I might be wrong, but I think that LibTIFF handles it fine, it's just how FreeImage interpretes the data as a FIT_BITMAP (due to that being the default and the missing branch above). I've not looked very far into the code yet though.
Below I upload a 16-bit grayscale with alpha TIFF file which is coded in a similar manner as your TIFF file, i.e. "BitsPerSample 16 and SamplesPerPixel 2", with ExtraSamples 0 (i.e. Unspecified).
The thumbnail image in the appended TIFF Tag Listing is by GDIplus, not FreeImage. We can tell that GDIplus can honour the ICC profile in the file. (However, in order to honour its ICC, I had to explicitly resave the file to an 8 bit one first during the process, also using GDIplus. If I am just to display the image without ICC effect, no such conversion is needed, but the alpha bytes I got are all of 255 for this file).
As shown in the screenshot, this file is by "Adobe Photoshop CS Macintosh", specifically for ICC-conversion testing purposes. For ready reference, screenshots of both before and after ICC-conversion are appended.
ICC-conversion is not an issue of this thread, I mention it to show that GDIplus is somehow capable of processing a "BitsPerSample 16 and SamplesPerPixel 2" TIFF file, albeit alpha bytes are all of a 255 value for this type of file.
Last edit: Johnny Petersen 2018-09-24