2009-10-09 100 views

回答

3

声明:我不知道C#,但我已经做了太多的C/C++图像处理,所以我不能通过回答 - 我会用C来回答,因为我认为C#有一个类似的语法。

两个1位(两种颜色)和8位(256色)图像具有的调色板。但是将1bit转换为8bit转换很容易 - 因为不涉及量化,只是上采样。

首先,你需要选择(或进口)的1位图像的调色板的两种颜色。如果你没有,我建议使用黑色(0x000000FF)和白色(0xFFFFFFFF)为清晰(注意:两种颜色都是RGBA,我认为windows使用ABGR)。这将是你的'调色板'。

然后,每个颜色映射到调色板 - 输入图像将不得不width * height/8字节。每个字节代表八个像素。因为我不知道你在bittwiddling的专业知识做(即我不想迷惑你,我不希望你盲目复制和粘贴代码,你已经被授予的互联网络),我会保持这个答案简单。

// 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); 
    } 
} 

希望有所帮助!