0

I would like to generate PNG images with 1bits (2 colors) or 2bit (4 colors) depth with the library Libpng.

Does any one know how to do it ? I have tested examples, and they all seem to work with 8bit color depth ?

I know png_set_IHDR but in the example I test, when I change the depth parameter in png_set_IHDR from 8 to 2 or 1, my program draw one pixels of 2 or 4. I think, it's due to the memory allocation created with the png_malloc function.

In the example I try to modify (http://www.lemoda.net/c/write-png/), the png_malloc function allocates all pixels of the image with sizeof uint8_t.

png_malloc (png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);

Can you tell to me how to allocate 1bit or 2bits pixels ?

Thank's Jo2s

jo2s
  • 1
  • 2
  • It is directly done by using `png_set_IHDR` with a bit depth of either 1 or 2. What else do you need ? – mmgp Feb 14 '13 at 22:48

1 Answers1

1

In png_set_IHDR, the bit_depth parameter sets the bit depth of all color components. Together with PNG_COLOR_TYPE_RGB, you end up with one bit for the red component, one for the green and one for the blue.

You should call png_set_IHDR with PNG_COLOR_TYPE_PALETTE, and then if bit_depth is one, you must have a palette with two colors (0 and 1), and if bit_depth is 2, one with four colors (0 to 3.)