【问题标题】:BMP file format not read properly?BMP 文件格式无法正确读取?
【发布时间】:2013-02-18 16:49:19
【问题描述】:

大家好!

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

除了颠倒阅读图像之外,我在这里做错了什么?我在维基百科或谷歌搜索上找不到任何相关的东西。似乎在 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
        }
    }
}

提前致谢!

【问题讨论】:

  • 你能告诉我们你正在使用的代码吗?除非您这样做,否则我们不会很有帮助。
  • 还有EasyBMP,它只用于 BMP 文件,比 FreeImage 具有更基本的功能。

标签: c++ bitmap bmp


【解决方案1】:

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

BMP 格式规定最左边的像素是最高 位,最右边的像素是最低。在您的代码中,您通过位迭代错误的方式。

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 2018-07-24
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多