c++ - 在 C++ 中读取 BMP 奇怪的行为

标签 c++ colors bitmap pixel bmp

提前致谢!

我正在使用自定义的 BITMAPFILEHEADER 和 BITMAPINFOHEADER 在 C++ 中读取 BMP。然后我使用 SetPixel() 将其打印出来。但是像素的打印顺序有一些问题,我没能解决。为了说明它们,请参阅提供的图像。我没有足够的声誉在这里上传图片所以请检查 url。抱歉给您带来不便:(

http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html

上面两个是256*256 lena.bmp。第一个是原来的,第二个是我程序的结果。看到最左边的乱序栏了吗?它应该出现在原始图像的最右侧。

我还尝试了一些随机的 bmp 文件。但是有了这个,我得到了一种奇怪的颜色。

(还是看上面的链接)它的大小是146*146。第三张是原图,第四张是打印出来的。我使用屏幕捕获工具从互联网上截取它,所以我不知道问题是在我的程序中(但它不存在于 lena.bmp 中!)还是在 bmp 本身中。

我的代码如下:

/*****BMP.h*****/
#include <fstream>
#include <Windows.h>
#define N 384
using namespace std;

typedef struct {
   WORD bfType;                 /* Magic identifier            */
   DWORD bfSize;                       /* File size in bytes          */
   WORD bfReserved1;
   WORD bfReserved2;
   DWORD bfOffBits;                     /* Offset to image data, bytes */
} HEADER;

typedef struct {
   DWORD biSize;               /* Header size in bytes      */
   LONG biWidth;                /* Width and height of image */
   LONG biHeight;
   WORD biPlanes;       /* Number of colour planes   */
   WORD biBitCount;         /* Bits per pixel            */
   DWORD biCompression;        /* Compression type          */
   DWORD biSizeImage;          /* Image size in bytes       */
   LONG biXPelsPerMeter;     /* Pixels per meter          */
   LONG biYPelsPerMeter;
   DWORD biClrUsed;           /* Number of colours         */
   DWORD biClrImportant;   /* Important colours         */
} INFOHEADER;



/*****main.cpp*****/
#include "BMP.h"

HEADER bmfh;        //bitmap file header
INFOHEADER bmih;    //bitmap info header
BYTE R[N][N], G[N][N], B[N][N];                     //RGB value of every pixel
COLORREF color[N][N];   //color
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();  //initialize the screen
void ReadBmp(char *BmpFileName);
void GetColor(int i, int j);

void main()
{
    HWND hwnd;  //handlers HWND and HDC
    HDC hdc;
    char BmpFileName[10];
    int i, j;

    hwnd = GetConsoleWindow();  //initialize the screen
    hdc = GetDC(hwnd);  
    scanf("%s", BmpFileName);
    ReadBmp(BmpFileName);   //read the BMP file
    for(i = 0; i < bmih.biHeight; i++)
        for(j = 0; j < bmih.biWidth; j++)
            SetPixel(hdc, i, j, color[bmih.biWidth - j][i]);    //draw the BMP image
    ReleaseDC(hwnd,hdc);
}

void ReadBmp(char *BmpFileName)     //read the BMP file
{
    int patch;  //number of 0s for complement in every row
    ifstream in(BmpFileName, ios_base::binary);     //open the BMP file
    in.read((char *)(&bmfh), sizeof(bmfh) - 2);     //read in BITMAPFILEHEADER. Here sizeof returns a value larger than struct size, so "-2"
    if(bmfh.bfType != 0x4d42)                       //if not BMP, exit
    {
        printf("File type is not BMP!\n");
        exit(1);
    }
    in.read((char *)(&bmih),sizeof(bmih));      //read in BITMAPINFOHEADER
    in.seekg(bmfh.bfOffBits, ios_base::beg);    //seek bitmap data
    patch = (4 - (bmih.biWidth * 3) % 4) % 4;   //calculate number of 0s for complement in every row
    for(int i = 0; i < abs(bmih.biHeight); i++) 
    {
        for(int j = 0; j < abs(bmih.biWidth); j++)
        {
            in.read((char *)(&R[i][j]), 1);     //read in data pixel by pixel
            in.read((char *)(&G[i][j]), 1);
            in.read((char *)(&B[i][j]), 1);
            GetColor(i, j);     //obtain the original and greyscaled color
        }
        in.seekg(patch, ios_base::cur); //skip 0s for complement in every row
    }
}

void GetColor(int i, int j)     //obtain the original and greyscaled color
{
    int iB, iG, iR;
    iB = (int)(B[i][j]);
    iG = (int)(G[i][j]);
    iR = (int)(R[i][j]);
    color[i][j] = RGB(iB, iG, iR);  //original color
}

最佳答案

问题出在 HEADER 的结构包装上(请参阅 http://en.wikipedia.org/wiki/Data_structure_alignment 了解其含义)。

in.read((char *)(&bmfh), sizeof(bmfh) - 2); 中的 - 2 试图解决这个问题,但实际上不会吨。 bfType 之后的所有字段 - 包括 bfOffBits - 都将具有错误的偏移量,因此它们的值将不正确。

使用 MSVC,您可以像这样禁用 HEADERINFOHEADER 的结构打包:

#pragma pack(push)
#pragma pack(1)

typedef struct {
...
} HEADER;

typedef struct {
...
} INFOHEADER;

#pragma pack(pop)

然后删除 - 2 就可以了。

关于c++ - 在 C++ 中读取 BMP 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388536/

相关文章:

android - API 级别 < 26 的 Color.valueOf 替代方案?

c++ - 如何将顶点添加到 OpenGL C++ triangle_fan

C++ 如何在初始化列表中设置指向 null 的指针数组?

C++ - 检查 TCHAR 数组是否为空

c++ - 嵌套类作为C++中成员函数的参数

Java - 多色文本

javascript - 创建两种颜色之间的色阶

android - 在运行时以编程方式更改复选框 colorAccent

android - 如何将drawBitmap设置为非全屏

java - PorterduffXfermode : Clear a section of a bitmap