gpt4 book ai didi

c++ - BMP Exporter 不工作 C++

转载 作者:行者123 更新时间:2023-11-28 06:20:12 26 4
gpt4 key购买 nike

因此,正如标题所述,我在使用 C++ 导出 .bmp(24 位 bmp)时遇到了问题。我正在做一个学校项目类型的事情,我需要一些帮助。为了了解 .BMP 的工作原理,我查看了维基百科页面,并从 here 获得了一些帮助。 ,但我还是想不通。这是我所拥有的:

//Export the map as a .bmp
void PixelMap::exportMap(const char* fileName)
{
//Size of the file in bytes
int fileSize = 54 + (3 * width * height);

//The sections of the file
unsigned char generalHeader[14] = {'B','M',0,0, 0,0,0,0, 0,0,54,0, 0,0};
unsigned char DIBHeader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,24,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
unsigned char pixelArray[] = "";

//Set the binary portion of the generalHeader, mainly just file size
generalHeader[2] = (unsigned char)(fileSize);
generalHeader[3] = (unsigned char)(fileSize << 8);
generalHeader[4] = (unsigned char)(fileSize << 16);
generalHeader[5] = (unsigned char)(fileSize << 24);

//The binary variable portion of the DIB header
DIBHeader[4] = (unsigned char)(width);
DIBHeader[5] = (unsigned char)(width << 8);
DIBHeader[6] = (unsigned char)(width << 16);
DIBHeader[7] = (unsigned char)(width << 24);
DIBHeader[8] = (unsigned char)(height);
DIBHeader[9] = (unsigned char)(height << 8);
DIBHeader[10] = (unsigned char)(height << 16);
DIBHeader[11] = (unsigned char)(height << 24);
int picSize = 3 * width * height;
DIBHeader[20] = (unsigned char)(picSize);
DIBHeader[21] = (unsigned char)(picSize << 8);
DIBHeader[22] = (unsigned char)(picSize << 16);
DIBHeader[23] = (unsigned char)(picSize << 24);

//Loop through all width and height places to add all pixels
int counter = 0;
for(short j = height; j >= 0; j--)
{
for(short i = 0; i < width; i++)
{
//Add all 3 RGB values
pixelArray[counter] = pixelColour[i, j].red;
counter++;
pixelArray[counter] = pixelColour[i, j].green;
counter++;
pixelArray[counter] = pixelColour[i, j].blue;
counter++;
}
}

//Open it
ofstream fileWorking(fileName);

//Write the sections
fileWorking << generalHeader;
fileWorking << DIBHeader;
fileWorking << pixelArray;

//NO MEMORY LEAKS 4 ME
fileWorking.close();
}

这是名为“PixelMap”的类的一部分,基本上是帧缓冲区或表面。 PixelMap 具有变量“宽度”、“高度”和结构数组“pixelColour”。 (包含 3 个字符的结构称为“红色”、“绿色”和“蓝色”)如果您想查看该类,请看这里。 (这只是一个骨架,试图先把.bmp弄下来)

//This is a pixel map, mainly for exporting BMPs
class PixelMap
{
public:

//The standard pixel variables
int width;
int height;
Colour pixelColour[];

//The constructor will set said variables
PixelMap(int Width, int Height);

//Manipulate pixels
void setPixel(int X, int Y, char r, char g, char b);

//Export the map
void exportMap(const char* fileName);
};

(颜色是结构体)

所以我的问题是,当我尝试运行它时,我得到了这个: Error所以 pixelArray,要导出的颜色数组被破坏了。我认为这与未正确指定大小有关,但我尝试为其分配正确的值(3 * 宽度 * 高度(3 是 RGB)),但它说它需要是一个常量值。

非常感谢对此问题的任何帮助!

最佳答案

代替

unsigned char pixelArray[]      = "";

你可以使用:

std::vector<unsigned char> pixelArray(3*width*height,0);

这声明了一个包含 3*width*height 元素的 vector ,初始化为 0。您可以使用与数组版本相同的语法访问元素(除了,如注释中指出的,您必须注意将二进制值正确写入输出文件)。

关于c++ - BMP Exporter 不工作 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29436718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com