我尝试用 C++ 编写一个 bmp 文件,但是 bmp 文件的宽度和高度似乎会影响结果,有时我会得到一个错误的 bmp 文件
I try to write a bmp file in c++, but the bmp file's width and height seems to affect the result,sometimes I got a bad bmp file
如标题所述。代码如下
#include <Windows.h>
#include <string>
using namespace std;
#define WIDTH 90
#define HEIGHT 180
class Test {
public:
static bool ShowWithBMP(string filename) {
BITMAPFILEHEADER bmfh; // bitmap file header
BITMAPINFOHEADER bmih; // bitmap info header (windows)
const int OffBits = 54;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfType = 0x4d42; //"BM"
bmfh.bfOffBits = OffBits;
bmfh.bfSize = WIDTH * HEIGHT * 3 + OffBits;
memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
bmih.biSize = 40;
bmih.biPlanes = 1;
bmih.biSizeImage = 0;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biWidth = WIDTH;
bmih.biHeight = HEIGHT;
FILE* file = fopen(filename.c_str(), "w");
fwrite((const void*)&bmfh, sizeof(BITMAPFILEHEADER), 1, file);
fwrite((const void*)&bmih, sizeof(BITMAPINFOHEADER), 1, file);
for (int row = 0; row < HEIGHT; ++row)
{
// RGB 24 BITS
for (int col = 0; col < WIDTH; ++col)
{
int index = row * WIDTH + col;
RGBTRIPLE pix;
pix.rgbtRed = 0;
pix.rgbtGreen = 0;
pix.rgbtBlue = 0;
fwrite((const void*)&pix, sizeof(RGBTRIPLE), 1, file);
}
}
fclose(file);
return true;
}
};
int main() {
Test::ShowWithBMP("test.bmp");
system("pause");
return 0;
}
当我设置 WIDTH 为 90 和 HEIGHT 为 180 时,输出文件 "test.bmp" 似乎是一个错误的文件,我无法打开它,但是当设置 WIDTH 为 100 和 HEIGHT 为 180 时,它变得正确。它出什么问题了?用C++写bmp文件有什么限制吗?
根据记忆(大约 20 年前),BMP format 本质上包含像素数据的 DIB 格式。每个像素行需要是 4 字节的倍数(即整数个 DWORD);因此,您需要确保在每一行的末尾都有适当的填充。
这是你的函数,修复后应该可以正常工作
static bool ShowWithBMP(string filename) {
BITMAPFILEHEADER bmfh; // bitmap file header
BITMAPINFOHEADER bmih; // bitmap info header (windows)
const int OffBits = 54;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfType = 0x4d42; //"BM"
bmfh.bfOffBits = OffBits;
DWORD bitmapLineSize = ((WIDTH * 3 + 3) / 4) * 4; //size of a row in bytes (ceil(WIDTH * 3 / 4) * 4)
bmfh.bfSize = bitmapLineSize * HEIGHT + OffBits;
memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
bmih.biSize = 40;
bmih.biPlanes = 1;
bmih.biSizeImage = 0;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biWidth = WIDTH;
bmih.biHeight = HEIGHT;
DWORD paddingSize = bitmapLineSize - 3 * WIDTH;//extra 0 to 3 bytes per line to write
FILE* file = fopen(filename.c_str(), "w");
fwrite((const void*)&bmfh, sizeof(BITMAPFILEHEADER), 1, file);
fwrite((const void*)&bmih, sizeof(BITMAPINFOHEADER), 1, file);
for (int row = 0; row < HEIGHT; ++row)
{
// RGB 24 BITS
for (int col = 0; col < WIDTH; ++col)
{
int index = row * WIDTH + col;
RGBTRIPLE pix;
pix.rgbtRed = 0;
pix.rgbtGreen = 0;
pix.rgbtBlue = 0;
fwrite((const void*)&pix, sizeof(RGBTRIPLE), 1, file);
}
if (paddingSize > 0)
{
//write out the rest of the bitmap line filled with zeros
DWORD padding = 0;
fwrite((const void*)&padding, paddingSize, 1, file);
}
}
fclose(file);
return true;
}
如标题所述。代码如下
#include <Windows.h>
#include <string>
using namespace std;
#define WIDTH 90
#define HEIGHT 180
class Test {
public:
static bool ShowWithBMP(string filename) {
BITMAPFILEHEADER bmfh; // bitmap file header
BITMAPINFOHEADER bmih; // bitmap info header (windows)
const int OffBits = 54;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfType = 0x4d42; //"BM"
bmfh.bfOffBits = OffBits;
bmfh.bfSize = WIDTH * HEIGHT * 3 + OffBits;
memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
bmih.biSize = 40;
bmih.biPlanes = 1;
bmih.biSizeImage = 0;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biWidth = WIDTH;
bmih.biHeight = HEIGHT;
FILE* file = fopen(filename.c_str(), "w");
fwrite((const void*)&bmfh, sizeof(BITMAPFILEHEADER), 1, file);
fwrite((const void*)&bmih, sizeof(BITMAPINFOHEADER), 1, file);
for (int row = 0; row < HEIGHT; ++row)
{
// RGB 24 BITS
for (int col = 0; col < WIDTH; ++col)
{
int index = row * WIDTH + col;
RGBTRIPLE pix;
pix.rgbtRed = 0;
pix.rgbtGreen = 0;
pix.rgbtBlue = 0;
fwrite((const void*)&pix, sizeof(RGBTRIPLE), 1, file);
}
}
fclose(file);
return true;
}
};
int main() {
Test::ShowWithBMP("test.bmp");
system("pause");
return 0;
}
当我设置 WIDTH 为 90 和 HEIGHT 为 180 时,输出文件 "test.bmp" 似乎是一个错误的文件,我无法打开它,但是当设置 WIDTH 为 100 和 HEIGHT 为 180 时,它变得正确。它出什么问题了?用C++写bmp文件有什么限制吗?
根据记忆(大约 20 年前),BMP format 本质上包含像素数据的 DIB 格式。每个像素行需要是 4 字节的倍数(即整数个 DWORD);因此,您需要确保在每一行的末尾都有适当的填充。
这是你的函数,修复后应该可以正常工作
static bool ShowWithBMP(string filename) {
BITMAPFILEHEADER bmfh; // bitmap file header
BITMAPINFOHEADER bmih; // bitmap info header (windows)
const int OffBits = 54;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfType = 0x4d42; //"BM"
bmfh.bfOffBits = OffBits;
DWORD bitmapLineSize = ((WIDTH * 3 + 3) / 4) * 4; //size of a row in bytes (ceil(WIDTH * 3 / 4) * 4)
bmfh.bfSize = bitmapLineSize * HEIGHT + OffBits;
memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
bmih.biSize = 40;
bmih.biPlanes = 1;
bmih.biSizeImage = 0;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biWidth = WIDTH;
bmih.biHeight = HEIGHT;
DWORD paddingSize = bitmapLineSize - 3 * WIDTH;//extra 0 to 3 bytes per line to write
FILE* file = fopen(filename.c_str(), "w");
fwrite((const void*)&bmfh, sizeof(BITMAPFILEHEADER), 1, file);
fwrite((const void*)&bmih, sizeof(BITMAPINFOHEADER), 1, file);
for (int row = 0; row < HEIGHT; ++row)
{
// RGB 24 BITS
for (int col = 0; col < WIDTH; ++col)
{
int index = row * WIDTH + col;
RGBTRIPLE pix;
pix.rgbtRed = 0;
pix.rgbtGreen = 0;
pix.rgbtBlue = 0;
fwrite((const void*)&pix, sizeof(RGBTRIPLE), 1, file);
}
if (paddingSize > 0)
{
//write out the rest of the bitmap line filled with zeros
DWORD padding = 0;
fwrite((const void*)&padding, paddingSize, 1, file);
}
}
fclose(file);
return true;
}