【发布时间】:2016-11-17 18:33:21
【问题描述】:
我在这里遇到了最奇怪的问题...我在 Windows 中使用相同的代码(复制粘贴)从 Linux 到 READ 和 WRITE 和 BMP 图像。并且由于某种原因,在 Linux 中,每件事都可以完全正常,但是当我从某些地方进入 Windows 10 时,我无法打开它图片,我收到一条错误消息,怎么说是这样的:
“我们好像不支持这种文件格式。”
你知道我该怎么做吗?我会把代码放在下面。
编辑:
我已经解决了填充问题,现在可以写入图像,但它们完全是白色,知道为什么吗?我也更新了代码。
struct BMP {
int width;
int height;
unsigned char header[54];
unsigned char *pixels;
int size;
int row_padded;
};
void writeBMP(string filename, BMP image) {
string fileName = "Output Files\\" + filename;
FILE *out = fopen(fileName.c_str(), "wb");
fwrite(image.header, sizeof(unsigned char), 54, out);
unsigned char tmp;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width * 3; j += 3) {
// Convert (B, G, R) to (R, G, B)
tmp = image.pixels[j];
image.pixels[j] = image.pixels[j + 2];
image.pixels[j + 2] = tmp;
}
fwrite(image.pixels, sizeof(unsigned char), image.row_padded, out);
}
fclose(out);
}
BMP readBMP(string filename) {
BMP image;
string fileName = "Input Files\\" + filename;
FILE *f = fopen(fileName.c_str(), "rb");
if (f == NULL)
throw "Argument Exception";
fread(image.header, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
image.width = *(int *) &image.header[18];
image.height = *(int *) &image.header[22];
image.row_padded = (image.width * 3 + 3) & (~3);
image.pixels = new unsigned char[image.row_padded];
unsigned char tmp;
for (int i = 0; i < image.height; i++) {
fread(image.pixels, sizeof(unsigned char), image.row_padded, f);
for (int j = 0; j < image.width * 3; j += 3) {
// Convert (B, G, R) to (R, G, B)
tmp = image.pixels[j];
image.pixels[j] = image.pixels[j + 2];
image.pixels[j + 2] = tmp;
}
}
fclose(f);
return image;
}
在我看来,这段代码应该是跨平台的……但不是……为什么?
感谢您的帮助
【问题讨论】:
-
您可以在windows中使用Visual Studio自带的比较工具比较两个文件。或者使用调试器确保写入相同的图像头数据。
-
比较什么?完全一样的代码...
-
我检查了这段代码几次,它完全一样......
-
@Mircea 比较 BMP 文件,而不是代码。
-
@Mircea 我认为他的意思是文件中的标题数据。
标签: c++ windows image bitmap bitmapimage