Using libTIFF to determine the number of bits present in an image, using the following code:
unsigned short bitsPerChannel;
unsigned short channelPerPixel;
TIFFGetField(mem_TIFF, TIFFTAG_BITSPERSAMPLE, &bitsPerChannel);
TIFFGetField(mem_TIFF, TIFFTAG_SAMPLESPERPIXEL, &channelPerPixel);
int imageBits = bitsPerChannel * channelPerPixel;
This usually results in imageBits
being 8
or 24
. Although I have seen that when alpha is present I can get 16
or 32
. Is there any way to determine if one of the channels is an Alpha, or the number of visible image bits, as I'm not interested in alpha bits.
The alpha channel is stored in TIFFTAG_EXTRASAMPLES
. Query all extra samples to check if one of them is an alpha channel...
bool hasAlphaChannel(TIFF *mem_TIFF)
{
unsigned short extraSamples = 0;
unsigned short* sampleInfo = NULL;
if (TIFFGetField(mem_TIFF, TIFFTAG_EXTRASAMPLES, &extraSamples, &sampleInfo))
{
for (unsigned short i = 0; i < extraSamples; i++)
{
if (sampleInfo[i] == EXTRASAMPLE_ASSOCALPHA || sampleInfo[i] == EXTRASAMPLE_UNASSALPHA)
{
return true; // alpha channel found
}
}
}
return false; // no alpha channel
}
See also: Getting error when trying to apply "ExtraSamples" tag to a TIFF file to be written