我最奇怪的問題在這裏...我使用相同的代碼(複製粘貼)在Linux在Windows READ和WRITE和BMP圖片。並從某些原因,Linux的每一件事情的作品完全正常,但是當我來的Windows 10一些我無法打開圖片,我已經收到錯誤消息是怎麼說的是這樣的:C++:寫BMP圖像格式錯誤的WINDOWS
「看起來我們不支持這種文件格式。」
你知道該怎麼辦?我將把代碼放在下面。
編輯:
我已經解決了這個問題,填充現在是寫圖像,但它們是完全白色,任何想法,爲什麼?我也更新了代碼。
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;
}
在我看來,這個代碼應該是跨平臺的......但它不是......爲什麼?
感謝您的幫助
您可以在窗口中使用比較工具,比如使用Visual Studio自帶的windiff來比較兩個文件。或者使用調試器確保寫入相同的圖像標題數據。 –
比較什麼?這是完全一樣的代碼... – Mircea
我已經檢查了這段代碼幾次,它是完全一樣的... – Mircea