[英]Reading BMP in C++ strange behavior
Thank you in advance! 先感谢您!
I'm reading a BMP in C++ using self-defined BITMAPFILEHEADER and BITMAPINFOHEADER. 我正在使用自定义的BITMAPFILEHEADER和BITMAPINFOHEADER读取C ++中的BMP。 Then I print it out using SetPixel(). 然后我使用SetPixel()将其打印出来。 But there're some problems with the printing order of pixels and I failed to fix it. 但是像素的打印顺序存在一些问题,我无法解决它。 To illustrate them, please see the images provided. 为了说明它们,请参阅提供的图像。 I don't have enough reputations to upload images here so please check the url. 我的信誉不足,无法在此处上传图片,因此请检查网址。 Sorry for the inconvenience:( 抱歉给你带来不便:(
http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html http://liuyuel.blogspot.com/2012/02/reading-bmp-in-c.html
Above two are 256*256 lena.bmp. 上面两个是256 * 256 lena.bmp。 The first one is the original one, and the second one is the result of my program. 第一个是原始的,第二个是我的程序的结果。 See the left-most out-of-order bar? 看到最左边的乱序条? It should appear in the right-most as in the original image. 它应该出现在原始图像的最右边。
I also tried some random bmp files. 我还尝试了一些随机的bmp文件。 But with this one I got a strange color. 但是有了这个,我得到了一种奇怪的颜色。
(Still see the above link) Its size is 146*146. (仍请参见上面的链接)其尺寸为146 * 146。 The third image is the original and fourth is the printed result. 第三张图像是原始图像,第四张是打印结果。 I used a screen capturing tool to snip it from the internet, so I don't know if the problem is in my program (but it didn't exist in lena.bmp!) or is in the bmp itself. 我使用了一个屏幕捕获工具从互联网上截取它,所以我不知道问题是否出在我的程序中(但lena.bmp中不存在此问题)或bmp本身。
My code is as below: 我的代码如下:
/*****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
}
The problem is with the structure packing of HEADER
(see http://en.wikipedia.org/wiki/Data_structure_alignment for what that means). 问题与HEADER
的结构打包有关(请参阅http://en.wikipedia.org/wiki/Data_structure_alignment的含义)。
The - 2
in in.read((char *)(&bmfh), sizeof(bmfh) - 2);
的- 2
在in.read((char *)(&bmfh), sizeof(bmfh) - 2);
tries to fix that, but actually won't. 尝试解决此问题,但实际上不会。 All fields after bfType
- including bfOffBits
- will have the wrong offset, so their value will not be correct. bfType
之后的所有字段(包括bfOffBits
)将具有错误的偏移量,因此其值将不正确。
With MSVC you can disable structure packing for HEADER
and INFOHEADER
like this: 使用MSVC,您可以像这样禁用HEADER
和INFOHEADER
结构打包:
#pragma pack(push)
#pragma pack(1)
typedef struct {
...
} HEADER;
typedef struct {
...
} INFOHEADER;
#pragma pack(pop)
Then remove the - 2
and you should be fine. 然后删除- 2
,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.