2017-03-08 78 views
0

我試圖將單色xbm文件(每8像素1個字節,意思是每像素1位)的數據轉換爲每像素1個字節的內存位圖表示。我該怎麼做?如何將單色xbm位圖數據轉換爲1通道8bpp圖像?

[更新]
到目前爲止,我一直想(沒有成功)的線的東西:

#define IMAGE_WIDTH 769 
#define IMAGE_HEIGHT 14 

static void uncompress_bm(unsigned char** buf, int* w, int* h) 
{ 
    *w = IMAGE_WIDTH; *h = IMAGE_HEIGHT; 
    unsigned int num_pixels = (*w) * (*h); 
    *buf = malloc(num_pixels * sizeof(char)); 
    memset(*buf, 0, num_pixels * sizeof(char)); 
    for (unsigned int i = 0; i < num_pixels; ++i) { 
     unsigned short data_bit = image_data_bits2[i/8]; 
     (*buf)[i] = (data_bit & (1 << (i % 8))) ? 255 : 0; 
    } 
} 

unsigned char image_data_bits[] = { 
    0xFF, 0xFF, 0xFF, 0xF3, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 
    0x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3C, 0x81, 0x40, 0x86, 0x00, 0xF9, 0x13, 0x3E, 
    0x9F, 0x99, 0xCC, 0xE4, 0x09, 0x9C, 0x27, 0xF0, 0xE1, 0x73, 0x9E, 0x33, 
    0x67, 0xE6, 0xF3, 0x9F, 0x3F, 0xE7, 0xF3, 0xF9, 0xFF, 0xFF, 0x01, 0x02, 
    0xE4, 0xCF, 0x13, 0x08, 0x64, 0x08, 0x90, 0x3F, 0xE1, 0xF3, 0x99, 0xC9, 
    0x4C, 0x9E, 0xC0, 0x79, 0x02, 0x1F, 0x3E, 0xE7, 0x39, 0x73, 0x66, 0x3E, 
    0xFF, 0xF9, 0x73, 0x3E, 0x9F, 0xFF, 0x01, 0xF9, 0x9F, 0xF9, 0xF0, 0xE7, 
    0x0F, 0x3F, 0x9F, 0x03, 0x07, 0xE0, 0x07, 0x7E, 0xCE, 0x73, 0x3E, 0x7C, 
    0x38, 0x40, 0xE0, 0xC0, 0x9F, 0x0F, 0x1F, 0xF8, 0xCF, 0xFF, 0x3F, 0x87, 
    0x33, 0x1C, 0x20, 0x40, 0xFE, 0x3C, 0x81, 0x40, 0x86, 0x00, 0xF9, 0x13, 
    0x3E, 0x9F, 0x99, 0xCC, 0xE4, 0x09, 0x9C, 0x27, 0xF0, 0xE1, 0x73, 0x9E, 
    0x33, 0x67, 0xE6, 0xF3, 0x9F, 0x3F, 0xE7, 0xF3, 0xF9, 0xFF, 0xFF, 0x01, 
    0x02, 0xE4, 0xCF, 0x13, 0x08, 0x64, 0x08, 0x90, 0x3F, 0xE1, 0xF3, 0x99, 
    0xC9, 0x4C, 0x9E, 0xC0, 0x79, 0x02, 0x1F, 0x3E, 0xE7, 0x39, 0x73, 0x66, 
    0x3E, 0xFF, 0xF9, 0x73, 0x3E, 0x9F, 0xFF, 0x01, 0xFF, 0x07, 0xE0, 0xCF, 
    0x19, 0xF2, 0x3F, 0x9F, 0xCC, 0x7C, 0xFE, 0xFF, 0x9F, 0xCF, 0x73, 0xCE, 
    0xFF,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0xFF, 
    0xFF, 0xE7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0x07, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 
    0xFF, 0x01, 
}; 
+0

通過編寫相應的代碼... OK更嚴重的是,告訴我們你到目前爲止做了什麼,然後問你在哪裏有困難的部分具體問題。 –

+0

我認爲它應該是'unsigned char data_bit = image_data_bits [i/8]' –

+0

順便說一句,它是以標點符號,數字0-9和字母表(兩次)的形式出現的。像這樣...... http://thesetchells.com/a.jpg –

回答

0

當它看來,XBM格式包含8位對齊行。 奏效示例實現如下:

static void uncompress_bm(unsigned char** buf, int* w, int* h) 
{ 
    *w = IMAGE_WIDTH; *h = IMAGE_HEIGHT; 
    /* Align width to 8 byte boundary */ 
    *w = (*w) + (8 - (*w) % 8); 
    unsigned int num_pixels = (*w) * (*h); 
    *buf = malloc(num_pixels * sizeof(char)); 
    memset(*buf, 0, num_pixels * sizeof(char)); 
    for (int i = 0; i < *h; ++i) { 
     for (int j = 0; j < *w; ++j) { 
      unsigned int cur = i * (*w) + j; 
      (*buf)[cur] = (image_data_bits[cur/8] & (1 << (cur % 8))) ? 255 : 0; 
     } 
    } 
} 
相關問題