tags:

views:

269

answers:

2

How can I convert a 1 bit image to an 8 bit image using C#? What color matrix should be used? Can you please provide samples or links?

+1  A: 

Does this help:

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

But of course it needs some clean up. It could use some try..finally's for all the delete's and releasing of DC's

Bryan
+2  A: 

Disclaimer: I don't know C#, but I've done too much image processing on C/C++ so I can't pass on answering - I'll answer in C since I think C# shares a similar syntax.

Both 1bit (two colors) and 8bit (256 colors) images have a palette. but transitioning a 1bit to 8bit transition is easy - since there's no quantization involved, just up-sampling.

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

Then map each color to the palette - The input image is going to have width * height / 8 bytes. Each byte represents eight pixels. Since I don't know about your expertise in bittwiddling (i.e I don't want to confuse you and I don't want you to mindlessly copy and paste code you've been granted on the internets), I'll keep this answer simple.

 // Insert your image's 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 that helps!

LiraNuna