I've read the Image class documentation and then searched and searched through the forum postings to find out a way to check an image file to see if it has an alpha channel, but I just can't find anything. I'm sure someone else has asked the same thing, but I must be blind or stupid!
Does anyone know if there is an Image class method called HasAlpha(), or something like that? I'd really appreciate any ideas on this topic.
Thanks!
sanderton
How can I check a file for alpha channel?
CMYKA image
Thanks for the answer.
I have another question on the same topic:
I have a CMYKA image (transparent background), and I need to extract the alpha channel and save it to a separate file as a 1-bpp depth (I've read several other posts with similar queries but I'm still stumped.)
This is the question: Is the alpha channel data stored in the PixelPacket.opacity member, or is it in the indexes?
(I'm puzzled because for the non-transparent CMYK images I had to get the K values from the indexes and it appeared that the opacity member wasn't used. For CMYKA images the IM documentation says that the alpha values are stored in the indexes; does this mean that the K values are in the opacity member?)
The 2nd part of my task concerns saving the alpha data to a 1-bpp file. Which method should I use?
image.monochrome(true), or
image.type(BilevelType)
I'd really appreciate any ideas/answers on these topics.
Thanks again,
sanderton
I have another question on the same topic:
I have a CMYKA image (transparent background), and I need to extract the alpha channel and save it to a separate file as a 1-bpp depth (I've read several other posts with similar queries but I'm still stumped.)
This is the question: Is the alpha channel data stored in the PixelPacket.opacity member, or is it in the indexes?
(I'm puzzled because for the non-transparent CMYK images I had to get the K values from the indexes and it appeared that the opacity member wasn't used. For CMYKA images the IM documentation says that the alpha values are stored in the indexes; does this mean that the K values are in the opacity member?)
The 2nd part of my task concerns saving the alpha data to a 1-bpp file. Which method should I use?
image.monochrome(true), or
image.type(BilevelType)
I'd really appreciate any ideas/answers on these topics.
Thanks again,
sanderton
The opacity value is always retrieved with PixelPacket.opacity for RGBA and CMYKA images. The K value for CMYK and CMYKA is stored in the indexes. See http://magick.imagemagick.org/script/architecture.php for details.
For 1-bit alpha there are many solutions. Common ones are anything not opaque is transparent or you could threshold at the QuantumRange/2 (50%) level.
For 1-bit alpha there are many solutions. Common ones are anything not opaque is transparent or you could threshold at the QuantumRange/2 (50%) level.
extract alpha channel to 1-bpp file (mask file)
Thanks for clearing it up for me!
I guess I wasn't clear enough about the 2nd part, so here is the code I'm working on (maybe someone can see where I'm going wrong). I'm trying to extract the alpha channel and create a mask file that is later used when writing to a ImageType 3 (masked image) PostScript file. Could the creation of pAlphaImage be wrong?
Thanks for any comments/ideas!
sanderton
I guess I wasn't clear enough about the 2nd part, so here is the code I'm working on (maybe someone can see where I'm going wrong). I'm trying to extract the alpha channel and create a mask file that is later used when writing to a ImageType 3 (masked image) PostScript file. Could the creation of pAlphaImage be wrong?
Code: Select all
UINT nCols = m_pIMImage->columns();
UINT nRows = m_pIMImage->rows();
MagickLib::PixelPacket* pImageData = m_pIMImage->setPixels( 0, 0, nCols, nRows );
if ( pImageData == ( MagickLib::PixelPacket* ) NULL )
{
ErrCode = DATA_ERR_FAIL;
}
else
{
MagickLib::Quantum* pAlphaData = new MagickLib::Quantum[ nCols * nRows ];
if ( pAlphaData )
{
MagickLib::PixelPacket* pPacket = pImageData;
for ( int i = 1; i <= nRows; i++ )
{
for ( int j = 1; j <= nCols; j++ )
{
pAlphaData[ (i*j)-1 ] = pPacket->opacity;
pPacket++;
}
}
Magick::Image* pAlphaImage = new Magick::Image( nCols, nRows, "R", MagickLib::CharPixel, pAlphaData );
if ( pAlphaImage )
{
pAlphaImage->magick( "BMP" ); // not a valid magick identifier?
pAlphaImage->monochrome( true );
// pAlphaImage->type( MagickLib::BilevelType );
pAlphaImage->write( (LPCTSTR) m_csAlphaFileName );
ErrCode = ( _access(m_csAlphaFileName, 0) == 0 ) ? DATA_ERR_OK : DATA_ERR_FAIL;
delete pAlphaImage;
}
delete [] pAlphaData;
}
}
sanderton