在C++中读取BMP的奇怪行为
Reading BMP in C++ strange behavior
提前感谢!
我正在使用自定义的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
}
问题出在HEADER
的结构封装上(请参阅http://en.wikipedia.org/wiki/Data_structure_alignment这意味着什么)。
in.read((char *)(&bmfh), sizeof(bmfh) - 2);
中的- 2
试图解决这个问题,但实际上不会。bfType
之后的所有字段(包括bfOffBits
)都将具有错误的偏移量,因此它们的值将不正确。
使用MSVC,您可以禁用HEADER
和INFOHEADER
的结构打包,如下所示:
#pragma pack(push)
#pragma pack(1)
typedef struct {
...
} HEADER;
typedef struct {
...
} INFOHEADER;
#pragma pack(pop)
然后取下- 2
,你应该没事了。
相关文章:
- 为什么 BMP 图像上的 imwrite 会卡住/不返回?
- 从 bmp 文件数据创建 MFC CBitmap
- 将 bmp 文件加载到 TSpeedButton 中
- 在C++中的SDL2窗口上显示.bmp时出现问题
- 将位图 (bmp) 转换为具有透明度的 png (Windows c++)
- 将 BMP 图像转换为灰度
- GUID variable for JPEG,BMP,PNG
- 在 BMP 文件中查找隐藏的消息
- C++,我正在尝试创建一个有效的.bmp输出文件
- 将 24 位".bmp"图像转换为黑白/单色图像的可能算法是什么?
- 如何在包含数字的.bmp(二进制图像)中执行组件标记9 4 3 6
- BMP的完整屏幕截图.与块状和保存有关的问题
- 如何在C++的四叉树中存储 bmp 文件?
- 在控制台上显示 BMP 映像时堆损坏
- 如何读取从UChar缓冲区到CV :: MAT的1位BMP
- 使用 std::vector 保存 BMP 像素图
- 如何确定 BMP 图像是否包含来自标题的 BGR 或 BGRA 像素
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- C++:读取.BMP文件时出现问题;文件结束时间早于预期
- 无法加载.bmp文件win32