如何使用C++正确计算BMP图像中每像素的字节数



我正在尝试使用我从结构 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;
}

我正在尝试处理名为索引的位图图像.bmp

尺寸 : 275x184
宽度 : 275 像素
高度 : 184 像素
位深 : 24
名称 : 索引.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

此外,包装问题图例2k还提到:

  • 你没有考虑字节序(谷歌它,然后在你的代码中使用ntohs等),

  • 您应该创建一个本地BITMAPFILEHEADER bmpheader;对象然后brgfile.read((char*)&bmpheader, sizeof bmpheader); - 这样您就知道嵌入的多字节整数将正确对齐以进行访问 - 否则您可能会在某些系统上获得SIGBUS或类似内容; 对BITMAPINFOHEADER执行类似操作

  • 也不要只打印bfType - 作为一个数组,它会衰减到<<const char*,并且它不是NUL终止的,所以你会得到垃圾额外的字符。

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 个。在这个实时示例中,它显示了16 .

最新更新