我的代码有一个问题,它是。bmp文件的生成器,没有任何库来生成这些文件。
程序生成一个空白图像,即使有像素数据。我试图改变字节,但我没有工作。
有什么想法吗?谢了。用C++编写的代码。
代码:
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
#define BYTES_PER_PIXEL 3
#define FILE_HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
struct BITMAPFILEHEADER {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
};
struct BITMAPINFOHEADER {
unsigned int biSize;
unsigned int biWidth;
unsigned int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
};
struct RGB_data {
unsigned char b;
unsigned char g;
unsigned char r;
};
int main() {
size_t height = 100;
size_t width = 50;
size_t size = width * height * 3;
//------------------------------------------------------------------------------
FILE *file = fopen("./sample.bmp", "wb");
//------------------------------------------------------------------------------
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
//------------------------------------------------------------------------------
RGB_data buffer[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
RGB_data data;
data.b = 0;
data.r = 0;
data.b = 0;
buffer[y][x] = data;
}
}
//------------------------------------------------------------------------------
bmfh.bfType = 0x4D42;
bmfh.bfSize = size + FILE_HEADER_SIZE + INFO_HEADER_SIZE;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = bmfh.bfSize - size;
//------------------------------------------------------------------------------
bmih.biSize = 40;
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = 0;
bmih.biSizeImage = size;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
//------------------------------------------------------------------------------
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
buffer[y][x].b = 10;
buffer[y][x].r = 20;
buffer[y][x].g = 30;
}
}
//------------------------------------------------------------------------------
fwrite(&bmfh, 1, FILE_HEADER_SIZE, file);
fwrite(&bmih, 1, INFO_HEADER_SIZE, file);
fwrite(&buffer, 3, size, file);
fclose(file);
//------------------------------------------------------------------------------
cout << "Image generated successfully!" << endl;
return 0;
}
附注。我检查了正确的数据类型。我也试图改变一些变量的名字,但我没有工作。
您没有在此处初始化G
值
data.b = 0;
data.r = 0;
data.b = 0;
还包括第一个结构开头的#pragma pack(push,1)
和最后一个结构结尾的#pragma pack(pop)
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
#define BYTES_PER_PIXEL 3
#define FILE_HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
#pragma pack(push, 1)
struct BITMAPFILEHEADER {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
};
struct BITMAPINFOHEADER {
unsigned int biSize;
unsigned int biWidth;
unsigned int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
};
struct RGB_data {
unsigned char b;
unsigned char g;
unsigned char r;
};
#pragma pack(pop)
int main() {
size_t height = 100;
size_t width = 50;
size_t size = width * height * 3;
//------------------------------------------------------------------------------
FILE *file = fopen("./sample.bmp", "wb");
//------------------------------------------------------------------------------
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
//------------------------------------------------------------------------------
RGB_data buffer[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
RGB_data data;
data.b = 0;
data.r = 0;
data.b = 0;
buffer[y][x] = data;
}
}
//------------------------------------------------------------------------------
bmfh.bfType = 0x4D42;
bmfh.bfSize = size + FILE_HEADER_SIZE + INFO_HEADER_SIZE;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = bmfh.bfSize - size;
//------------------------------------------------------------------------------
bmih.biSize = 40;
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = 0;
bmih.biSizeImage = size;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
//------------------------------------------------------------------------------
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
buffer[y][x].b = 10;
buffer[y][x].r = 20;
buffer[y][x].g = 30;
}
}
//------------------------------------------------------------------------------
fwrite(&bmfh, 1, FILE_HEADER_SIZE, file);
fwrite(&bmih, 1, INFO_HEADER_SIZE, file);
fwrite(&buffer, 3, size, file);
fclose(file);
//------------------------------------------------------------------------------
cout << "Image generated successfully!" << endl;
return 0;
}