Converting 1 bit image to 8 bit

How to convert 1 bit image to 8 bit image using C #? What color matrix should I use? Can you provide samples or links?

+2


source to share


2 answers


Disclaimer: I don't know C #, but I have handled too much image processing in C / C ++, so I can't seem to convey an answer - I'll answer in C, since I think C # has a similar syntax.

Both 1 bit (two colors) and 8 bit (256 colors) images have a palette. but going to jump from 1 to 8 bits is easy - since there is no quantization, just up-fetch.

First you need to select (or import) two colors of the 1 bit image palette. If you don't have them, I suggest using black (0x000000FF) and white (0xFFFFFFFF) for clarity (note: both are RGBA colors, I think windows use ABGR). This will be your "palette".



Then display each color in a palette. The input image will have a width * height / 8

byte. Each byte represents eight pixels. Since I donโ€™t know about your experience with bittwiddling (i.e. I donโ€™t want to confuse you, and I donโ€™t want you to mindlessly copy and paste the code that is provided to you on the internet), I will keep this answer simple.

    // Insert your image attributes here
int w = image.width;
int h = image.height;
    // Data of the image
u8* data = image.data;

/* 
 * Here, you should allocate (w * h) bytes of data.
 * I'm sure C# has ByteArray or something similar...
 * I'll call it output in my code.
 */

u8* output = new u8[w * h];
u8* walker = output;

    // Loop across each byte (8 pixels)
for(int i=0; i<w*h/8; ++i) {
        // Loop across each pixel
    for(int b=(1<<7); b>0; b>>=1) {
            // Expand pixel data to output
        *walker++ = !!(data[i] & b);
    }
}

      

Hope it helps!

+3


source


Does this help:

http://www.wischik.com/lu/programmer/1bpp.html



But of course this requires some cleaning. He can use some attempts. Finally, for all DC deletions and release

+1


source







All Articles