【发布时间】:2013-01-16 18:26:28
【问题描述】:
我试图将一个 bmp 图像克隆到另一个 bmp 图像中,但最终图像无法打开。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <process.h>
void readBMP(char* filename) {
int i;
FILE* f = fopen(filename, "rb");
FILE* f1= fopen("save.bmp", "wb");
if (!f) {
printf("Could not read file!\n");
exit(0);
}
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);
int width = *(int*)&info[18];
int height = *(int*)&info[22];
printf("%d %d\n", width, height);
fwrite(info, sizeof(unsigned char), 54, f1);
int length = width * height;
unsigned int image[10000][3];
for(i = 0; i < length; i++) {
image[i][2] = getc(f);
image[i][1] = getc(f);
image[i][0] = getc(f);
putc(image[i][2], f1);
putc(image[i][1], f1);
putc(image[i][0], f1);
printf("pixel %d : [%d,%d,%d]\n", i+1, image[i][0], image[i][1], image[i][2]);
}
fclose(f);
fclose(f1);
}
void main() {
char* fileName = "bitgray.bmp";
readBMP(fileName);
getch();
}
我作为输入的图像是 114X81,大小为 27918 字节。 最终图像大小相同,但大小为 27756 字节。
可能是什么错误??
【问题讨论】:
-
你怎么知道你遇到了错误?
fopen()失败了吗?程序崩溃了吗?save.bmp文件是否出现在您的目录中但无法读取? -
长度不是宽度*高度那么简单。
-
您应该检查文件 f1 的 fopen 结果。
标签: c image-processing bmp