0

I'm trying to create paletted PNG image (8-bit per pixel) that uses RGBA palette (32-bit per palette entry) using Cocoa framework*.

I've tried few combinations for [NSBitmapImageRep initWithBitmapDataPlanes:…] method. It seems to create appropriate bitmap for bitsPerSample:2 bitsPerPixel:8.

However, when I try to write such bitmap with [NSBitmapImageRep representationUsingType:NSPNGFileType…] I get:

libpng error: Invalid bit depth for RGBA image

If I try other bit depths, then I get 32-bit per pixel (non-paletted) image.


*) I know I could just use libpng, but that's not an answer I'm looking for.

Kornel
  • 97,764
  • 37
  • 219
  • 309

1 Answers1

2

2 bits per sample, 8 per pixel will not get you an indexed PNG--it will, in theory, create an RGBA PNG file with 2 bits per sample, just as it suggests. Now, such an image has 256 possible colour values per pixel (including alpha channel) but it's not indexed in the sense of having a colour lookup table.

To my knowledge, there is no way to specify a colour palette when using NSBitmapImageRep. You will probably have to use libpng directly to get the effect you want. (By the way, it doesn't matter if you aren't looking for this answer. It's still the correct answer to this particular problem and saying "no!" isn't going to change the universe around you.)

However, before you do that, if you tell us why you think/know you need an indexed PNG, we may be able to point you toward a better or simpler solution.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • Yes, but that's for a *GIF* file, not a *PNG* file. They're different animals. – Jonathan Grynspan Feb 07 '11 at 20:40
  • There is a way to specify palette — it takes `NSImageRGBColorTable` property. I found the 2-bps trick *somewhere* explaining how to get GIF output to work (although maybe it was wrong — what would be correct setting for GIF then?). – Kornel Feb 07 '11 at 20:42
  • I know PNG != GIF. My point was that this class supports palettes — GIF supports proves it. – Kornel Feb 07 '11 at 20:46
  • It supports them *for GIF files only*. It has no support for writing indexed PNG files. – Jonathan Grynspan Feb 07 '11 at 20:49