如何将单色xbm位图数据转换为1通道8bpp图像?

时间:2017-03-08 16:53:23

标签: c image bitmap

我试图将单色xbm文件的数据(每8像素1个字节,即每像素1位)转换为C中每像素1个字节的内存位图表示。我该怎么做?

[UPDATE]
到目前为止,我一直在尝试(没有任何成功)的内容:

#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,
  0x01, 0xFF, 0xFF, 0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xFF, 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,
};

1 个答案:

答案 0 :(得分: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;
        }
    }
}