大家好!

我得到了这个image.bmp
当我阅读包含所有填充的内容时,这样我会得到this结果。

除了颠倒读取图像外,我在这里还做错什么?我在Wikipedia上或通过谷歌搜索找不到任何亲戚。看起来在24像素宽度后,图像被镜像为8像素。为什么!?我不明白!我怎样才能解决这个问题!?

我正在Windows上使用一些C++代码读取文件,从而读取了原始BMP文件。
图像文件是单色的。每个像素1位。

显示位图数据的代码:

unsigned int count = 0; // Bit counting variable
unsigned char *bitmap_data = new char[size]; // Array containing the raw data of the image

for(unsigned int i=0; i<size; i++){ // This for-loop goes through every byte of the bitmap_data

    for(int j=1; j<256; j*=2){ // This gives j 1, 2, 4, 8, 16, 32, 64 and 128. Used to go through every bit in the bitmap_data byte

        if(count >= width){ // Checking if the row is ended
            cout << "\n"; // Line feed

            while(count > 32) count -=32; // For padding.
            if(count < 24) i++;
            if(count < 16) i++;
            if(count < 8) i++;

            count = 0; // resetting bit count and break out to next row
            break;
        }

        if(i>=size) break; // Just in case

        count++; // Increment the bitcounter. Need to be after end of row check

        if(bitmap_data[i] & j){ // Compare bits
            cout << (char)0xDB; // Block
        }else{
            cout << (char)' ';  // Space
        }
    }
}

提前致谢!

最佳答案

几乎可以肯定,您在每个字节中以错误的顺序解释/输出了这些位。这导致8像素的每一列从左向右翻转。

BMP格式指出最左边的像素是最高有效位,而最右边的像素是最低位。在您的代码中,您通过位迭代了错误的方式。

关于c++ - BMP文件格式无法正确读取?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15204265/

10-14 10:16