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

时间:2012-02-22 03:04:53

标签: c++ colors bitmap pixel bmp

提前谢谢!

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

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
}

1 个答案:

答案 0 :(得分:3)

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

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

使用MSVC,您可以禁用HEADERINFOHEADER的结构打包,如下所示:

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

typedef struct {
...
} HEADER;

typedef struct {
...
} INFOHEADER;

#pragma pack(pop)

然后删除- 2,你应该没问题。

相关问题