BMP文件行填充问题
BMP File line padding issue
所以我试图用C++代码导出一个.bmp文件,除了一件主要的事情:换行之外,我已经让它正常工作了。我不能100%确定行填充是如何工作的,但我知道我需要它。除了填充之外,我的算法也能工作,我在十六进制编辑器中手动将填充添加到我导出的图像中,它也能工作。但是我该如何添加填充?这是我所拥有的:
//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[1000000];
unsigned char bmpPad[3] = {0, 0, 0};
//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);
//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;
pixelArray[counter] = pixelColour[i][j].green;
pixelArray[counter] = pixelColour[i][j].blue;
counter++;
}
}
//Open it
ofstream fileWorking(fileName);
//Write the sections
fileWorking.write((const char*)generalHeader, 14);
fileWorking.write((const char*)DIBHeader, 40);
fileWorking.write((const char*)pixelArray, 3 * width * height);
//NO MEMORY LEAKS 4 ME
fileWorking.close();
pixelColour是具有3种颜色的结构数据类型,所有类型都是无符号字符。非常感谢您的帮助!
在您的情况下,每行必须是4个字节(32位)的倍数。
int pad = 0; // Set pad byte count per row to zero by default.
// Each row needs to be a multiple of 4 bytes.
if ((width * 3) % 4 != 0) pad = 4 - ((width * 3) % 4); // 4 - remainder(width * 3 / 4).
填充值几乎可以包含任何内容,但最好将它们设置为0。当写入每一行时,只需在写入下一行之前再写入pad
个零(字节)。
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; // Need to advance counter.
pixelArray[counter++] = pixelColour[i][j].green;
pixelArray[counter++] = pixelColour[i][j].blue;
}
for (int padVal = 0; padVal < pad; padVal++) pixelArray[counter++] = 0; // Pad.
}
最后,您需要编写一个更大的文件大小:
fileWorking.write((const char*) pixelArray, (3 * width + pad) * height);
相关文章:
- C++ 填充数组时遇到问题
- 将十六进制值设置为用于填充的字符变量时出现问题
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 使用队列和列表 STL 进行C++分配。在将数据填充到列表或队列中然后打印该数据时遇到问题
- 在使用结构体和用函数填充其变量(使用指针)时遇到问题
- 用零填充矩阵行的问题
- BMP文件行填充问题
- 按行、列或随机从数组填充矩阵时出现错误索引问题
- 清除并重新填充 std::vector<std::vector<T>>导致分配问题
- 填充 c++ 数组时遇到问题,每个索引处只有最后一项
- 尝试递归填充和打印出两组的所有归纳函数的问题
- 这个简单的木炭填充器有什么问题
- 在检查现有元素时,从文件填充并行数组时出现问题
- 填充动态类模板数组时遇到问题
- 使用__declspec的内存填充问题
- 使用 C++11 的 std::regex 填充子匹配时遇到问题
- c++填充数组问题
- 长哈希填充问题
- 内存填充问题