Determine image type (RGBA or alpha)

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
ismfismf

Determine image type (RGBA or alpha)

Post by ismfismf »

Hi!

I have posted this question under another forum, but wihtou any answer so far. Since it is a programmign issue, this might be the right forum. I have troubles to tell an RGBA image apart from a black/white image.

If a PNG, TGA, or TIF image is stored as RGBA, but the whole image is transparent, then ImageMagick always returns type BilevelType and color space RGBColorspace when reading the file. How can I tell that apart from a real bilevel image (indexed black/white with transparency)?

Image *image = new Image();
image->adjoin(true);
image->read(filename);
ImageType image_type = image->type(); // is BilevelType
ColorspaceType colorType = image->colorSpace(); // is RGBColorspace

BTW, the same also happens when the image is RGBA with only black and white pixels. Then it also reports BiLevelType as image type.

If ImageMagick does some automatic quantization/color reduction, how can I turn that off for reading an image (for the sample code above).

I know that it works somehow with convert.exe (%info), but I have to get this info programmatically.

Thanks.

Image
Image
Last edited by ismfismf on 2010-08-05T00:36:12-07:00, edited 1 time in total.
ismfismf

Re: Determine image type (RGBA or alpha)

Post by ismfismf »

I am using ImageMagick 6.6.0, but also tried it with 6.6.3.
ismfismf

Re: Determine image type (RGBA or alpha)

Post by ismfismf »

The image type is supposed to be GrayscaleMatteType, but is BiLevelType, if only black and white pixels are there. If I add one single gray pixel, the image type is GrayscaleMatteType as expected.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Determine image type (RGBA or alpha)

Post by fmw42 »

I am not sure I am following all of what you are asking. Could you post some links to example images and explain what you expect for each and what you are seeing?

Also I don't think IM supports 8-bit total (indexed/colortable/pseudocolor) PNG with transparency.

I don't see the bi-level behavior with the following in IM 6.6.3.2 Q16 Mac OSX Tiger:


convert rose: -alpha transparent roset.png

identify -verbose roset.png
Image: roset.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: TrueColorMatte
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit

Channel statistics:
Red:
min: 35 (0.137255)
max: 255 (1)
mean: 145.712 (0.57142)
standard deviation: 69.2953 (0.271746)
kurtosis: -1.385
skewness: 0.14637
Green:
min: 22 (0.0862745)
max: 255 (1)
mean: 89.2602 (0.35004)
standard deviation: 52.4698 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
Blue:
min: 24 (0.0941176)
max: 255 (1)
mean: 80.4683 (0.315562)
standard deviation: 55.1114 (0.216123)
kurtosis: 3.01818
skewness: 1.9795
Alpha:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: -0 (-0)
kurtosis: 0
skewness: 0
ismfismf

Re: Determine image type (RGBA or alpha)

Post by ismfismf »

Thanks for the answer. Here the missing details.

I have attached two png images to the original post.

One is RGB+alpha and one is grayscale+alpha. That is what every other image software says.

When I use convert.exe, I do not get the original file format. It always says rgba.
convert cef2630f96.png -format "%[channnels]" info:

When I use the piece of code as described, then I always get BiLevelType and matte is enabled.

I need a way to find out the original image type programmatically (ior prevent quantization, or whatever is necessary to be able to ask for the original image type).

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Determine image type (RGBA or alpha)

Post by fmw42 »

IM sees your first image as bilevel with alpha.


identify -verbose ce82e79268.png
Image: ce82e79268.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 246x92+0+0
Resolution: 28.35x28.35
Print size: 8.67725x3.24515
Units: PixelsPerCentimeter
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
gray: 1-bit
alpha: 8-bit

Channel statistics:
Gray:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: -0 (-0)
kurtosis: 0
skewness: 0
Alpha:
min: 0 (0)
max: 255 (1)
mean: 33.2852 (0.13053)
standard deviation: 85.3677 (0.334775)
kurtosis: 2.85234
skewness: -2.19845
Alpha: none #00000000

Histogram:
19549: ( 0, 0, 0, 0) #00000000 none
2819: ( 0, 0, 0,255) #000000 black
55: ( 0, 0, 0, 65) #00000041 rgba(0,0,0,0.254902)
48: ( 0, 0, 0,247) #000000F7 rgba(0,0,0,0.968627)
47: ( 0, 0, 0, 73) #00000049 rgba(0,0,0,0.286275)
27: ( 0, 0, 0,244) #000000F4 rgba(0,0,0,0.956863)
26: ( 0, 0, 0, 29) #0000001D rgba(0,0,0,0.113725)
3: ( 0, 0, 0,237) #000000ED rgba(0,0,0,0.929412)
3: ( 0, 0, 0,243) #000000F3 rgba(0,0,0,0.952941)
3: ( 0, 0, 0,254) #000000FE rgba(0,0,0,0.996078)
2: ( 0, 0, 0, 1) #00000001 rgba(0,0,0,0.00392157)
2: ( 0, 0, 0, 45) #0000002D rgba(0,0,0,0.176471)
2: ( 0, 0, 0,249) #000000F9 rgba(0,0,0,0.976471)
2: ( 0, 0, 0, 20) #00000014 rgba(0,0,0,0.0784314)
2: ( 0, 0, 0, 16) #00000010 rgba(0,0,0,0.0627451)
2: ( 0, 0, 0, 4) #00000004 rgba(0,0,0,0.0156863)
2: ( 0, 0, 0,252) #000000FC rgba(0,0,0,0.988235)
1: ( 0, 0, 0,150) #00000096 rgba(0,0,0,0.588235)
1: ( 0, 0, 0,146) #00000092 rgba(0,0,0,0.572549)
1: ( 0, 0, 0,137) #00000089 rgba(0,0,0,0.537255)
1: ( 0, 0, 0,123) #0000007B rgba(0,0,0,0.482353)
1: ( 0, 0, 0,116) #00000074 rgba(0,0,0,0.454902)
1: ( 0, 0, 0,115) #00000073 rgba(0,0,0,0.45098)
1: ( 0, 0, 0,113) #00000071 rgba(0,0,0,0.443137)
1: ( 0, 0, 0,111) #0000006F rgba(0,0,0,0.435294)
1: ( 0, 0, 0, 95) #0000005F rgba(0,0,0,0.372549)
1: ( 0, 0, 0, 87) #00000057 rgba(0,0,0,0.341176)
1: ( 0, 0, 0, 63) #0000003F rgba(0,0,0,0.247059)
1: ( 0, 0, 0, 58) #0000003A rgba(0,0,0,0.227451)
1: ( 0, 0, 0, 52) #00000034 rgba(0,0,0,0.203922)
1: ( 0, 0, 0, 53) #00000035 rgba(0,0,0,0.207843)
1: ( 0, 0, 0, 37) #00000025 rgba(0,0,0,0.145098)
1: ( 0, 0, 0, 31) #0000001F rgba(0,0,0,0.121569)
1: ( 0, 0, 0, 28) #0000001C rgba(0,0,0,0.109804)
1: ( 0, 0, 0, 21) #00000015 rgba(0,0,0,0.0823529)
1: ( 0, 0, 0, 8) #00000008 rgba(0,0,0,0.0313725)
1: ( 0, 0, 0, 6) #00000006 rgba(0,0,0,0.0235294)
1: ( 0, 0, 0, 3) #00000003 rgba(0,0,0,0.0117647)
1: ( 0, 0, 0, 10) #0000000A rgba(0,0,0,0.0392157)
1: ( 0, 0, 0,250) #000000FA rgba(0,0,0,0.980392)
1: ( 0, 0, 0,242) #000000F2 rgba(0,0,0,0.94902)
1: ( 0, 0, 0,233) #000000E9 rgba(0,0,0,0.913725)
1: ( 0, 0, 0,229) #000000E5 rgba(0,0,0,0.898039)
1: ( 0, 0, 0,227) #000000E3 rgba(0,0,0,0.890196)
1: ( 0, 0, 0,224) #000000E0 rgba(0,0,0,0.878431)
1: ( 0, 0, 0,221) #000000DD rgba(0,0,0,0.866667)
1: ( 0, 0, 0,218) #000000DA rgba(0,0,0,0.854902)
1: ( 0, 0, 0,214) #000000D6 rgba(0,0,0,0.839216)
1: ( 0, 0, 0,209) #000000D1 rgba(0,0,0,0.819608)
1: ( 0, 0, 0,208) #000000D0 rgba(0,0,0,0.815686)
1: ( 0, 0, 0,203) #000000CB rgba(0,0,0,0.796078)
1: ( 0, 0, 0,169) #000000A9 rgba(0,0,0,0.662745)
1: ( 0, 0, 0,164) #000000A4 rgba(0,0,0,0.643137)
1: ( 0, 0, 0,157) #0000009D rgba(0,0,0,0.615686)
1: ( 0, 0, 0,154) #0000009A rgba(0,0,0,0.603922)

But


convert ce82e79268.png -format "%[channels]" info:
rgba

Channels probably will only be rgb, rgba, cmyk and cmyka. Not sure if it will ever say gray or graya.

I think in this case you really want colorspace:

convert ce82e79268.png -format "%[colorspace]" info:
Gray

in combination with

convert ce82e79268.png -format "%A" info:
True

or just

convert ce82e79268.png -format "%r" info:
DirectClassGrayMatte

But these are not distinguishing gray from bilevel. They see it as gray, but ignore the 1-bit part that makes it bilevel.

Your other image also shows bilevel with alpha.


identify -verbose cef2630f96.png
Image: cef2630f96.png
Format: PNG (Portable Network Graphics)
Class: PseudoClass
Geometry: 246x92+0+0
Resolution: 28.35x28.35
Print size: 8.67725x3.24515
Units: PixelsPerCentimeter
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
gray: 1-bit
alpha: 8-bit
Channel statistics:
Gray:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: -0 (-0)
kurtosis: 0
skewness: 0
Alpha:
min: 0 (0)
max: 255 (1)
mean: 33.2852 (0.13053)
standard deviation: 85.3677 (0.334775)
kurtosis: 2.85234
skewness: -2.19845


It says the channel is gray, but it is only 1-bit, so bilevel.

In IM, it has colorspace RGB, because there are always 3 colors, but in this case they are all the same, so IM knows it is gray (or bilevel b/w if only 1-bit). Your other software is probably seeing all three colors (channels), but not determining that they are all the same. This may be because PNG perhaps does not support a true grayscale type. But I am not an expert on PNG.

I would trust the IM type from the verbose info, plus the alpha.


type=`identify -verbose ce82e79268.png | sed -n 's/^.*Type: \(.*\).*$/\1/p'`
echo "type=$type"
type=Bilevel

alpha=`convert ce82e79268.png -format "%A" info:`
echo "withAlpha=$alpha"
withAlpha=True

NOTE: your histogram says the image is totally black, but has an 8-bit alpha.

NOTE2: Perhaps your other software is only looking at the image in gross terms or only looking at the colorspace, so only distinguishing between RGB and CMYK. Without working with your other software, it is hard to tell what it is doing!

NOTE3: Both images are totally black with 8-bit transparency. The only difference is that the first is truecolor and the second is pseudocolor. That may be why your other software thinks one is RGB (for truecolor) and thinks the other is grayscale (for pseudocolor). Truecolor is 24-bit total RGB (8-bits per each r,g,b channel) and pseudocolor is 8-bit total RGB. But in fact, your image has only one color - black (so bilevel, but no white) with 8-bit alpha.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Determine image type (RGBA or alpha)

Post by fmw42 »

see Anthony's reply to my post at viewtopic.php?f=3&t=16794
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine image type (RGBA or alpha)

Post by Drarakel »

Yes, the first image is stored as RGB+alpha PNG, and the second one as grayscale+alpha PNG. Do you really have to know about the details of the files itself? If yes, then ImageMagick is not the right tool to do that.
Post Reply