我试图通过使用从struct BITMAPINFOHEADER收集的信息来计算BMP图像中的像素数,从而计算每像素的字节数。但每当我运行我的代码时,我得到每个像素的字节数= 0
struct BITMAPFILEHEADER // File header
{
char bfType[2]; // File type: should be BM ( 0x42 0x4D )
int bfSize; // File size in bytes
short bfReserved1; // Reserved - for what i have no idea :P
short bfReserved2; // -||-
int bfOffBits; // Offset, adress of the beginning of the information about image (pixels )
};
struct BITMAPINFOHEADER // Bitmap header
{
unsigned int biSize; // Size of this header
unsigned int biWidth; // Width of image ( in pixels)
unsigned int biHeight; // Height of this image ( in pixels )
unsigned short biPlanes; // Numer of color planes, always 1
unsigned short biBitCount; // Number of bytes for pixel. Possibility values :1,4,8,16, 24 and 32
unsigned int biCompression; // Used compression (0 -none)
unsigned int biSizeImage; // Size of image
signed int biXPelsPerMeter; // Horizontal resolution of the image (pixel per meter)
signed int biYPelsPerMeter; // Vertical resolution of the image (pixel per meter)
unsigned int biClrUsed; // Number of colors in the color palette, or 0 to default to 2^n ( 0- no palette)
unsigned int biClrImportant; // Number of important colors used
};
struct Pixel{
unsigned int blue; // or double?
unsigned int green;
unsigned int red;
//unsigned char reserved;
};
void Image::conversiontoBRG(const char* filename)
{
ifstream brgfile;
brgfile.open(filename, ios::in | ios::binary);
char *bmpheadinfo = new char[sizeof(BITMAPFILEHEADER)];
brgfile.read(bmpheadinfo, sizeof(BITMAPFILEHEADER));
BITMAPFILEHEADER* bmpheader = (BITMAPFILEHEADER*)bmpheadinfo;
cout << "File type : " << bmpheader->bfType << endl;
cout << "File size : " << bmpheader->bfSize << endl;
cout << "File Offset for the beginning of image info : " << bmpheader->bfOffBits << endl << endl;
bmpheadinfo = new char[sizeof(BITMAPINFOHEADER)];
brgfile.read(bmpheadinfo, sizeof(BITMAPINFOHEADER));
BITMAPINFOHEADER* bmpinfo = (BITMAPINFOHEADER*)bmpheadinfo;
cout << "File Header Size : " << bmpinfo->biSize << endl;
cout << "Width : " << bmpinfo->biWidth << endl;
cout << "Height : " << bmpinfo->biHeight << endl;
cout << "No of bytes per pixel : " << bmpinfo->biBitCount << endl;
cout << "Used compression: " << bmpinfo->biCompression << endl;
cout << "Image size: " << bmpinfo->biSizeImage << endl;
cout << "Horizontal resolution: " << bmpinfo->biXPelsPerMeter << endl;
cout << "Vertical resolution: " << bmpinfo->biYPelsPerMeter << endl;
cout << "Number of colors in the color palette: " << bmpinfo->biClrUsed << endl;
cout << "Number of important colors used: " << bmpinfo->biClrImportant << endl;
}
我正在尝试处理名为index.bmp
的位图图像尺寸:275x184
宽度:275像素
高度:184像素
钻头深度:24
名称:index.bmp
项目类型:BMP文件
大小:148 KB
但每当我运行上面的代码时,我得到以下输出。我不确定我哪里出错了。请帮帮我。
File type : BMVS
File size : 2
File Offset for the beginning of image info : 2621440
File Header Size : 18022400
Width : 12058624
Height : 65536
No of bytes per pixel : 0
Used compression: 1394606080
Image size: 2
Horizontal resolution: 0
Vertical resolution: 0
Number of colors in the color palette: 0
Number of important colors used: 973078528
答案 0 :(得分:2)
包装问题legends2k还提到:
你没有考虑字节顺序(谷歌它,然后在你的代码中使用ntohs
等),
你应该创建一个本地BITMAPFILEHEADER bmpheader;
对象然后brgfile.read((char*)&bmpheader, sizeof bmpheader);
- 这样你知道嵌入的多字节整数将被正确对齐以便访问 - 否则你可能会得到一些SIGBUS或类似的东西系统;为BITMAPINFOHEADER
不要只打印bfType
- 作为一个数组,它会衰减为const char*
<<
,并且它不会终止,因此你会获得额外的垃圾字符。
答案 1 :(得分:1)
BMP文件在标题数据中没有填充位,而您的struct
已定义为没有紧密的打包宏;这将导致您的文件数据成员和struct
数据成员未正确对齐。解决这个问题,您就可以正确地看到这些字段了
#pragma pack(push, 1) // macro to avoid padding bytes within a struct
struct BITMAPFILEHEADER // File header
{
char bfType[2]; // File type: should be BM ( 0x42 0x4D )
int bfSize; // File size in bytes
short bfReserved1; // Reserved - for what i have no idea :P
short bfReserved2; // -||-
int bfOffBits; // Offset, adress of the beginning of the information about image (pixels )
};
struct BITMAPINFOHEADER // Bitmap header
{
unsigned int biSize; // Size of this header
unsigned int biWidth; // Width of image ( in pixels)
unsigned int biHeight; // Height of this image ( in pixels )
unsigned short biPlanes; // Numer of color planes, always 1
unsigned short biBitCount; // Number of bytes for pixel. Possibility values :1,4,8,16, 24 and 32
unsigned int biCompression; // Used compression (0 -none)
unsigned int biSizeImage; // Size of image
signed int biXPelsPerMeter; // Horizontal resolution of the image (pixel per meter)
signed int biYPelsPerMeter; // Vertical resolution of the image (pixel per meter)
unsigned int biClrUsed; // Number of colors in the color palette, or 0 to default to 2^n ( 0- no palette)
unsigned int biClrImportant; // Number of important colors used
};
#pragma pack(pop) // stop doing the tight packing
可以验证现在没有填充位
std::cout << sizeof BITMAPFILEHEADER << '\n';
现在打印14
;通过禁用宏来尝试它,你会看到超过14的内容。在this live example上显示16
。