Reputation: 8325
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);
Upvotes: 0
Views: 615
Reputation: 8163
You can use the matte() property to determine if your image supports transparency.
Magick::Image image;
image.read(filename);
if (image.matte())
executeMethod(image);
Upvotes: 1