How to save a 1 bit per pixel png using stbi?

389 views Asked by At

I struggled for a long time devising bitmap problems for students in C++ that were easy. The problem is the libraries which are not easy to use. stbi, an include-only library completely solved this problem and now students can easily load and save images, allowing me to focus on giving them problems in creating various images in a bitmap class, and then writing it out with a simple call.

https://github.com/nothings/stb

However, I want to do the same thing for a microprocessor class involving ARM assembler and i want a 1 bit per pixel bitmap, ideally a png. I cannot find an example writing out a png of this type. Does anyone know how to do it, preferably using stbi or simply and portably between windows, mac and linux using some other library?

The following would do it for a png with 4 bytes per pixel. I want 1 bit per pixel.

stbi_write_png("testpng_4.png", width, height, CHANNEL_NUM, pixels, width*CHANNEL_NUM);

Alternatively, I found that in libpng++ there is a cleaner API than libpng.

constexpr int W = 128, H = 128;
png::image< png::index_pixel > image(W,H);
png::palette pal(2);
pal[0] = png::color(0,0,0);
pal[1] = png::color(255,0,0);
image.set_palette(pal);

Can anyone tell me how to get the address of the image to work with the raw bits? The C++ API provides square brackets and I dug down a bit but can't find anything I can take an address of.

0

There are 0 answers