如何写入原始像素数据并另存为位图

How do I write raw pixel data and save as a bitmap

本文关键字:另存为 位图 数据 像素数 何写入 原始 像素      更新时间:2023-10-16

我的问题是:我想尝试制作白色 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;
}

它就像一个魅力。 谢谢你的帮助:)