1

Looking at the XPM (X PixMap) image format, I don't see anywhere that the number of bits per channel (or, equivalently, the number of possible colors) is specified. Poking around on my Ubuntu system, I see a number of examples of XPM files, and most of them have 8 bits per channel (24 bits for R, G, B), but some have 16 bits per color (48 bits for R, G, B).

For example, here's the first part of teapot.xpm which is packaged with the XFig application:

/* XPM */
static char * teapot_xpm[] = {
"234 149 34 1",
"   c #FFFFFFFFFFFF",
".  c #FFFF9191AAAA",
"X  c #DADA9191AAAA",
"o  c #FFFFB6B6AAAA",
"O  c #FFFFB6B6FFFF",

etc.

Is there a way to determine the number of bits per channel, aside from inspecting the hexadecimal numbers and counting how many characters they occupy?

2 Answers 2

1

You can use for example ImageMagick's identify utility like this:

identify -verbose teapot.xpm

This outputs, for example:

  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 2-bit

which you can then process with utilities like grep to e.g. extract only the numbers.

1
  • Thanks, that helps a lot. identify -verbose is very informative. I'll take a look at how they're determining the color depth. Commented Dec 5, 2023 at 16:22
0

It appears that the answer to the specific question, how to determine the color depth in an XPM image, is that it can only be done by inspecting the entries in the color table.

See this response to a question I asked in the issues for the ImageMagick project: https://github.com/ImageMagick/ImageMagick/discussions/6921#discussioncomment-7768171

Quoting user snibgo:

The depth is implicit in the colours.

xpm.c function ReadXPMImage() calls QueryColorCompliance() in color.c to set each entry in the palette. When a colour starts with #, that function counts the number n of hex digits, and sets depth=4*(n/4) EDIT or depth=4*(n/3). The depth is returned in the PixelInfo *color parameter, and ReadXPMImage() sets the maximum of these to image->depth. Other colours have depth=8.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .