goimage-processingpngimage-compressionbit-depth

Visualize an image pixel buffer with a bit depth of 3 i.e. each pixel having 3 bits of data


Question

What are the tools/standards/file formats by which I can visualize my image pixel buffer on which each pixel has 3 bits?

My Language is Go.

PNG header

Bit depth: 3 vs 4

An image pixel buffer is created on which each pixel has only 3 bits of data. Having saved it as a PNG file with header info of bit depth set to 3, I cannot visualize it:

Screenshot: 3-bit header bit depth of PNG

But when the PNG file header info for bit depth is set to 4, I can be previewed, although I'm not sure if the preview is actually correct or wrong:

Screenshot: 4-bit header bit depth of PNG File

The change that I'm making is just the header info for bit depth, that's all. My code is in Golang:

    // Set bit depth and color type.

    e.tmp[8] = 3 // Preview is not possible.
    e.tmp[8] = 4 // Preview is possible.

    e.tmp[9] = ctGrayscale

PNG standard

According to Wikipedia we have:

The standard allows indexed color PNGs to have 1, 2, 4 or 8 bits per pixel; grayscale images with no alpha channel may have 1, 2, 4, 8 or 16 bits per pixel. Everything else uses a bit depth per channel of either 8 or 16.


Solution

  • Having saved it as a PNG file with header info of bit depth set to 3

    No, you did not. As you note yourself at the end of your question, there is no such thing. PNG supports 1, 2, 4, 8, and 16 bits per pixel. Whatever you saved is not a valid PNG file, and whatever you were using to view it rightfully rejected it.

    You will need to use at least four bits per pixel with PNG. I am not aware of an image format that represents the uncompressed data as three bits per pixel. The uncompressed representation should not matter, as it is transient. Either you store it as compressed, where PNG compression will use approximately three bits per pixel in the compressed data anyway, if only eight values out of the 16 are used. Or you store it in memory as one byte, eight bits, per pixel, in order to deliver it to whatever will display it on the screen.

    I just tried PNG compression with random 3-bit values in 4-bit pixels. The compressed data portion is 3.03 bits per pixel.