Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何写入原始像素数据并保存为位图_C++_Bmp - Fatal编程技术网

C++ 如何写入原始像素数据并保存为位图

C++ 如何写入原始像素数据并保存为位图,c++,bmp,C++,Bmp,我的问题是:我想尝试制作一个白色24位位图,但每次我写入bmp文件时,它都会给我黑白条纹。我不明白为什么?也许我跳过了一些字节? 如果你想了解更多关于代码的信息,请询问 设置设置: void setup_settings( ) { // information om billedet pic.infoHeader.biSize = sizeof(BMP_InfoHeader); pic.infoHeader.biBitCount = 24; pic.infoHeader.biWidth

我的问题是:我想尝试制作一个白色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 Writing\n");
    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;
}
我的源代码主要源代码:

// 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: %d\n", 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;
}
//pic是一种BMP\U数据类型。对不起,如果我觉得这太乱了。
int main(int argc,char*argv[]){
设置_设置();
pic.data.Heigh=pic.infoHeader.biHeight;
pic.data.Width=pic.infoHeader.biWidth;
int bytespergb=(pic.infoHeader.bibibitcount/8);
//填充字节?
int paddedBytes=(pic.data.Width*bytespergb)%4;
printf(“PaddedBytes:%d\n”,PaddedBytes);
pic.data.RGB=新的位24[pic.data.Heigh*pic.data.Width*bytesPerRGB];
uint8_t r,g,b;
r=0xFF;
g=0xFF;
b=0xFF;
/*
for(无符号整数y=0;ypic.data.RGB[x+(y*pic.data.Width)].value=((b必须填充所有BitMapInfo头字段)。
-假设您需要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字节的倍数!!!
(从nx4宽度的图像开始——它们没有填充问题)

好的,我发现了问题所在

在我将bit24更改为:

typedef struct{ 
uint8_t blue;
uint8_t green;
uint8_t red;
}RGB_Data;
发件人:

还有一点主要的变化:

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;
}
for(无符号整数i=0;i

它工作起来很有魅力。谢谢你的帮助:)

什么是
pic
SaveBitmapFile
SaveBitmapFile
似乎是Windows的东西-我现在添加了一个Windows标签。@PaulR不,没有Windows API函数。你可以在Linux和Windows上执行此操作。但这是一个完整的示例(使用其他一些Windows API函数)如下:您不会觉得
bit24
结构的大小实际上是24位,是吗?使用3个字符(它们的数组或三个单独的成员)。它们被填充为4字节的倍数。我甚至检查并打印出来:)您能在帖子中打印出BitmapFileHeader和BitmapInfoHeader字段(值)吗?或者只打印文件的二进制转储。
#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;
}