How can I check a file for alpha channel?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
sanderton

How can I check a file for alpha channel?

Post by sanderton » 2006-07-04T09:47:13-07:00

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

User avatar
magick
Site Admin
Posts: 11100
Joined: 2003-05-31T11:32:55-07:00

Post by magick » 2006-07-04T11:51:53-07:00

Use image.matte() to determine if an image has a valid alpha channel or not.

sanderton

CMYKA image

Post by sanderton » 2006-07-10T12:18:02-07:00

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

User avatar
magick
Site Admin
Posts: 11100
Joined: 2003-05-31T11:32:55-07:00

Post by magick » 2006-07-10T12:48:03-07:00

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.

sanderton

extract alpha channel to 1-bpp file (mask file)

Post by sanderton » 2006-07-10T13:29:54-07:00

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?

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;
        }
      }
Thanks for any comments/ideas!
sanderton

User avatar
magick
Site Admin
Posts: 11100
Joined: 2003-05-31T11:32:55-07:00

Post by magick » 2006-07-10T14:07:52-07:00

ImageMagick creates a mask image for Postscript already. Use the PS3 image format, for example,
  • convert someimage.png ps3:image.ps

Post Reply