2009-10-09 103 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); 
    } 
} 

希望有所幫助!