如何写入原始像素数据并另存为位图
How do I write raw pixel data and save as a bitmap
我的问题是:我想尝试制作白色 24 位位图,但每次我写入 bmp 文件时,它都会给我白色黑/白条纹。 我不明白为什么?也许我跳过一些字节?如果您想了解有关代码的更多信息,请询问。
设置设置:
void setup_settings( ) {
// information om billedet
pic.infoHeader.biSize = sizeof(BMP_InfoHeader);
pic.infoHeader.biBitCount = 24;
pic.infoHeader.biWidth = WIDTH; // Hoejte i pixels
pic.infoHeader.biHeight = HEIGH; // bredte i pixels
pic.infoHeader.biPlanes = 1;
pic.infoHeader.biCompression = 0;
pic.infoHeader.biSizeImage = WIDTH * HEIGH * (pic.infoHeader.biBitCount/8);
pic.infoHeader.biXPelsPerMeter = 0;
pic.infoHeader.biYPelsPerMeter = 0;
pic.infoHeader.biClrUsed = 0;
pic.infoHeader.biClrInportant = 0;
pic.fileHeader.bfType[0] = 'B';
pic.fileHeader.bfType[1] = 'M';
pic.fileHeader.bfReservered1 = pic.fileHeader.bfReservered2 = 0;
pic.fileHeader.bfOffBits = sizeof(BMP_FileHeader) + pic.infoHeader.biSize;
}
我的SaveBitmapFile的这个功能定义是:
int SaveBitmapFile(const std::string filename, bit24* image){
// gem filen
std::ofstream writer(FileName.c_str(), std::ofstream::binary);
if(!writer.is_open()){
printf("Error: While Writingn");
return -1;
}
writer.write(reinterpret_cast<char *>(&pic.fileHeader), sizeof(BMP_FileHeader) );
writer.write(reinterpret_cast<char *>(&pic.infoHeader), sizeof(BMP_InfoHeader) );
writer.write(reinterpret_cast<char*>(&image[0]), pic.infoHeader.biSizeImage);
writer.close();
return 0;
}
我的结构:
#pragma pack(1)
typedef struct{
uint32_t value : 24;
}bit24;
#pragma pack(0)
// Billedet
#pragma pack(1)
typedef struct{
unsigned int Width;
unsigned int Heigh;
bit24* RGB;
}Image;
#pragma pack(0)
typedef struct {
BMP_FileHeader fileHeader;
BMP_InfoHeader infoHeader;
Image data;
}BMP_Data;
我的源码主要源代码:
// the pic is a type of BMP_Data. sorry if i this is really messy.
int main( int argc, char *argv[] ){
setup_settings();
pic.data.Heigh = pic.infoHeader.biHeight;
pic.data.Width = pic.infoHeader.biWidth;
int bytesPerRGB = (pic.infoHeader.biBitCount/8);
//padded bytes?
int paddedBytes = ( pic.data.Width * bytesPerRGB) % 4;
printf("PaddedBytes: %dn", paddedBytes);
pic.data.RGB = new bit24[ pic.data.Heigh * pic.data.Width * bytesPerRGB];
uint8_t r,g,b;
r = 0xFF;
g = 0xFF;
b = 0xFF;
/*
for( unsigned int y = 0; y < pic.data.Heigh; y++)
for( unsigned int x = 0; x < pic.data.Width; x++)
{
pic.data.RGB[x + (y*pic.data.Width )].value = ( (b << 0) | (g << 8) | (r << 16) );
}
*/
for( unsigned int i = 0; i < pic.data.Heigh * pic.data.Width * bytesPerRGB; i+=3){
pic.data.RGB[i ].value = ( (b << 0) | (g << 8) | (r << 16) );
}
SaveBitmapFile(FileName, pic.data.RGB);
delete [] pic.data.RGB;
return 0;
}
您必须填写所有BitmapInfoHeader字段。- 假设你想要24位位图:
BITMAPINFOHEADER bi =
{
sizeof(BITMAPINFOHEADER), // DWORD, = 40
WIDTH, // DWORD, image width in pix
HEIGHT, // DWORD, image width in pix
1, // WORD, planes MUST be 1
24,// WORD, num of bits per pixel
BI_RGB, // DWORD, (no compression = BI_RGB = 0)
0, // DWORD, sizeof image data, can be 0 if BI_RGB = 0
0, // DWORD, x-resolution
0, // DWORD, y-resolution
0, // DWORD, colors used (palette images only)
0, // DWORD, importand colors (palette images only)
};
还要确保像素的每一行(水平线(都填充为 4 个字节的倍数!!
(因此,请从N x 4宽度的图像开始 - 它们没有填充问题(
好的,我发现了问题。
在我将 bit24 更改为:
typedef struct{
uint8_t blue;
uint8_t green;
uint8_t red;
}RGB_Data;
从:
#pragma pack(1)
typedef struct{
uint32_t value : 24;
}bit24;
#pragma pack(0)
主要方面略有变化:
for( unsigned int i = 0; i < pic.data.Heigh * pic.data.Width * bytesPerRGB; i++){
pic.data.RGB[i].blue = b;
pic.data.RGB[i].green = g;
pic.data.RGB[i].red = r;
}
它就像一个魅力。 谢谢你的帮助:)
相关文章:
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- C++将 lambda 函数另存为成员变量,而不使用函数指针进行优化
- 将Qt qml文件转换为位图图像
- 如何将反转的字符串另存为新字符串?
- 将 OpenCV 帧另存为对象
- Lambda 捕获'this'另存为 std::function
- 提升输入流错误.如何将标准:矢量的动态矢量另存为 XML 文件
- 将字符串另存为浮点数组C++
- WinAPI/GDI:如何使用 GetDIBits() 为位图合成颜色表
- 将整数转换为位图颜色 RGB 值 C++
- 将字符数组另存为 JPG C++用于 Windows 应用商店应用
- 将端口另存为静态常量成员
- 将文件名的文件另存为当前时间日期
- 将 txt 文件另存为变量的名称
- 如何将对象名称另存为字符串
- 如何在C++(没有 .NET)将 PDF 转换为位图(栅格化)
- 将用于绘制为位图的像素格式更改
- 有没有办法在不复制所有像素的情况下将现有的像素数组转换为位图
- QTableView 输出另存为 .csv 或 .txt
- 如何写入原始像素数据并另存为位图