how can I find out number of color components per pixel

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
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

how can I find out number of color components per pixel

Post by jstph »

Is there anyway I can find out how many color components per pixel after I load image data as Image?
I try to use image->colorspace to identify, it doesn't work. I also try to use image->type, however for ColorSeparationType and UndefinedType, it will not work. I must miss something here.

I am also not sure if I can use image->matter to identify the transparent or not.

Can anyone give me a hint. Thanks in advance.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: how can I find out number of color components per pixel

Post by magick »

A colorspace of RGB suggests at least 3 color components per pixel. Add 1 if image->matte != MagickFalse (for alpha) channel. A colorspace of CMYK suggests at least 4 color components per pixel. Add 1 if image->matte != MagickFalse (for alpha) channel.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how can I find out number of color components per pixel

Post by anthony »

Only if combined with a 'type' that isn't greyscale! Remember IM fakes grayscale by using RGB

But generally that is correct 4 for CMYK otherwise it is 3, then add 1 if transparency is present.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I find out number of color components per pixel

Post by fmw42 »

try

convert image -format "%[channels]" info:

that should report

rgb
rgba
gray
graya
cmyk
cmyka
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: how can I find out number of color components per pixel

Post by jstph »

Thanks for all comments.
At the end, I did as anthony suggested. If the color space is RGB, then check image type. If image type is grayscale or Bilevel, the number of component should be one. otherwise, it is either 3 or 4 depending on color space.
However, I does have something unsure of PaletteBilevelMatteType. Is it one color index plus one alpha or just one color index which include alpha in its color palette? Right now, I transformed it to RGB.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how can I find out number of color components per pixel

Post by anthony »

PaletteBilevelMatteType I would assume this is an image with only three posible valid colors.
Black White and Transparent. Channel wise it is two channels, greyscale (bitmap) plus alpha.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply