损坏/不支持的.bmp文件
我需要为学校项目实现盒子模糊算法,然后在线发现了该算法。我的问题是,完成算法后,我无法打开“ result.bmp”文件。 它说该文件要么损坏或损坏,而且我似乎找不到此问题的原因。
代码:
FILE* fIn = fopen("test.bmp", "r"); // Input File name
FILE* fOut = fopen("result.bmp", "w+"); // Output File name
int i, j, y, x;
unsigned char byte[54];
for (i = 0; i < 54; i++) // read the 54 byte header from fIn
{
byte[i] = fgetc(fIn);
}
fwrite(byte, sizeof(unsigned char), 54, fOut); // write the header back
// extract image height, width and bitDepth from imageHeader
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];
printf("width: %d\n", width);
printf("height: %d\n", height);
int size = height * width;
unsigned char** buffer = (unsigned char**)malloc(size * sizeof(unsigned char*));
for (int i = 0; i < size; i++)
buffer[i] = (unsigned char*)malloc(3 * sizeof(unsigned char));
unsigned char** out = (unsigned char**)malloc(size * sizeof(unsigned char*));
for (int i = 0; i < size; i++)
out[i] = (unsigned char*)malloc(3 * sizeof(unsigned char));
for (i = 0; i < size; i++)
{
buffer[i][2] = getc(fIn); // blue
buffer[i][1] = getc(fIn); // green
buffer[i][0] = getc(fIn); // red
}
float v = 1.0 / 9.0; // initialize the blurring kernel
float kernel[3][3] = { {v,v,v},
{v,v,v},
{v,v,v} };
for (x = 1; x < height - 1; x++)
{
for (y = 1; y < width - 1; y++)
{
float sum0 = 0.0;
float sum1 = 0.0;
float sum2 = 0.0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
// matrix multiplication with kernel with every color plane
sum0 = sum0 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][0];
sum1 = sum1 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][1];
sum2 = sum2 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][2];
}
}
out[(x)*width + (y)][0] = (unsigned char)sum0;
out[(x)*width + (y)][1] = (unsigned char)sum1;
out[(x)*width + (y)][2] = (unsigned char)sum2;
}
}
for (i = 0; i < size; i++) //write image data back to the file
{
fputc(out[i][2], fOut);
fputc(out[i][1], fOut);
fputc(out[i][0], fOut);
}
该算法成功读取输入图像的宽度和高度,它创建输出文件,但无法打开文件。
I need to implement a Box Blur algorithm for a school project and I found this algorithm online. My issue is that i can't open the "result.bmp" file after the algorithm is done.
It says the file is either corrupted or damaged and I can't seem to find the cause of this issue.
Code:
FILE* fIn = fopen("test.bmp", "r"); // Input File name
FILE* fOut = fopen("result.bmp", "w+"); // Output File name
int i, j, y, x;
unsigned char byte[54];
for (i = 0; i < 54; i++) // read the 54 byte header from fIn
{
byte[i] = fgetc(fIn);
}
fwrite(byte, sizeof(unsigned char), 54, fOut); // write the header back
// extract image height, width and bitDepth from imageHeader
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];
printf("width: %d\n", width);
printf("height: %d\n", height);
int size = height * width;
unsigned char** buffer = (unsigned char**)malloc(size * sizeof(unsigned char*));
for (int i = 0; i < size; i++)
buffer[i] = (unsigned char*)malloc(3 * sizeof(unsigned char));
unsigned char** out = (unsigned char**)malloc(size * sizeof(unsigned char*));
for (int i = 0; i < size; i++)
out[i] = (unsigned char*)malloc(3 * sizeof(unsigned char));
for (i = 0; i < size; i++)
{
buffer[i][2] = getc(fIn); // blue
buffer[i][1] = getc(fIn); // green
buffer[i][0] = getc(fIn); // red
}
float v = 1.0 / 9.0; // initialize the blurring kernel
float kernel[3][3] = { {v,v,v},
{v,v,v},
{v,v,v} };
for (x = 1; x < height - 1; x++)
{
for (y = 1; y < width - 1; y++)
{
float sum0 = 0.0;
float sum1 = 0.0;
float sum2 = 0.0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
// matrix multiplication with kernel with every color plane
sum0 = sum0 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][0];
sum1 = sum1 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][1];
sum2 = sum2 + (float)kernel[i + 1][j + 1] * buffer[(x + i) * width + (y + j)][2];
}
}
out[(x)*width + (y)][0] = (unsigned char)sum0;
out[(x)*width + (y)][1] = (unsigned char)sum1;
out[(x)*width + (y)][2] = (unsigned char)sum2;
}
}
for (i = 0; i < size; i++) //write image data back to the file
{
fputc(out[i][2], fOut);
fputc(out[i][1], fOut);
fputc(out[i][0], fOut);
}
The algorithm successfully reads the width and the height of the input image, it creates the output file but the file cannot be opened.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码非常接近,但存在一些问题:
使用
((width * 3 + 3) / 4) * 4 < / code>。
width
和高度
的附接反之亦然。malloc()
的返回值。BMP文件标头
(14个字节固定)和
dib头
(可变大小)。由于存储DIB标头的大小在DIB标头的启动4个字节中,让我们读取18个字节(= 14 + 4)
首先,确定剩余的标头尺寸,然后读取其余的DIB标头。
然后,修改版本看起来像:
Your code is very close but has some issues:
with
((width * 3 + 3) / 4) * 4
.width
andheight
are vice versa.malloc()
.BMP File header
(14 bytes fixed)and
DIB header
(variable size). As the size of DIB header is storedin the starting 4 bytes of the DIB header, lets read the 18 bytes (= 14 + 4)
at first, determine the remaining header size, then read the rest of DIB header.
Then the modified version will look like: