0
votes

I have data for every pixel red one byte, green one byte, blue one byte. I need to pack this to 8 bits bitmap, so I have only one byte for pixel. How to transform rgb (three bytes) to one byte for bitmap format ? ( I am using C++ and cannot use any external libraries)

3

3 Answers

0
votes

I think you misunderstood how to form a bitmap structure. You do not need to pack (somehow) 3 bytes into one. That is not possible after all, unless you throw away information (like using special image formats GL_R3_G3_B2).

The BMP file format wiki page shows detailed BMP format : it is a header, followed by data. Now depending on what you set in your header, it is possible to form a BMP image containing RBG data component, where each component is one byte.

0
votes

First you need to decide how many bits you want to allocate for each color.

  1. 3bit per color will overflow a byte (9bits)

  2. 2bits per color will underflow;

In three byte RGB bitmap you have one byte to represent each color's intensity. Where 0 is minimum and 255 is max intensity. When you convert it to 1 byte bitmap (assuming you will choose 2bits per color ) transform should be:

1-byte red color/64

i.e you will get only 4 shades out of a spectrum of 265 shades per color.

0
votes

First you have to produce 256 colors palette that best fits your source image.

Then you need to dither the image using the palette you've generated.

Both problems have many well-known solutions. However, it's impossible to produce high-quality result completely automatic: for different source images, different approaches work best. For example, here's the Photoshop UI that tunes the parameters of the process:

enter image description here