简体   繁体   中英

How can I tell if a Magick::Image has an alpha channel?

If I load an image into ImageMagick with the read function like so:

Magick::Image image;
image.read(filename);  

how can I tell if the loaded image has an alpha channel? I want to direct my program to a different algorithm when I manipulate the pixels of a PNG with transparency vs when I load an opaque JPG.

Is there a simple yes/no test I can do?

The reason I am asking is because a code snippet like the following seems to assign random opacities if the loaded image does not have them, rather than assuming the pixel is completely opaque:

// transform the pixels to something GL can use
Magick::Pixels view(image);
GLubyte *pixels = (GLubyte*)malloc( sizeof(GLubyte)*width*height*4 );
for ( ssize_t row=0; row<height; row++ ) {
  const Magick::PixelPacket *im_pixels = view.getConst(0,row,width,1);
  for ( ssize_t col=0; col<width; col++ ) {
    *(pixels+(row*width+col)*4+0) =     (GLubyte)im_pixels[col].red;
    *(pixels+(row*width+col)*4+1) =     (GLubyte)im_pixels[col].green;
    *(pixels+(row*width+col)*4+2) =     (GLubyte)im_pixels[col].blue;
    *(pixels+(row*width+col)*4+3) = 255-(GLubyte)im_pixels[col].opacity;
  }
}
*pTex = pContext->LoadTexture( pixels, width, height );
free(pixels);

You can use the matte() property to determine if your image supports transparency.

Magick::Image image;
image.read(filename);
if (image.matte())
  executeMethod(image);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM