3
我們使用IJG libjpeg庫的8d版本來從24位Windows位圖(.bmp)文件創建JPEG圖像。使用libjpeg/C++從文件中JPEG編碼位圖(BMP)圖像
write_JPEG_file()正在使用沒有任何修改從IJG的example.c功能,如出現此:http://code.google.com/p/sumatrapdf/source/browse/trunk/ext/libjpeg-turbo/example.c?r=2397
的執行步驟的順序如下:
BITMAPFILEHEADER bfh;
BITMAPINFO bi;
BITMAPINFOHEADER *bih;
FILE *input;
int image_height;
int image_width;
fopen_s(&input, "image.bmp", "rb"); // Open existing file
// Read bitmap file header
fread_s(&bfh, sizeof(BITMAPFILEHEADER), 1, sizeof(BITMAPFILEHEADER), input);
// Read bitmap info header
fread_s(&bi, sizeof(BITMAPINFO), 1, sizeof(BITMAPINFO), input);
bih = &bi.bmiHeader;
image_height = bih->biHeight;
image_width = bih->biWidth;
int data_size = image_width * image_height * 3; // Compute image data size
// Allocate image buffer; this is the buffer write_JPEG_file() will use
JSAMPLE * image_buffer = (JSAMPLE *)malloc(data_size);
// Read image pixel data from file
fread_s(image_buffer, data_size, 1, data_size, input);
fclose(input);
write_JPEG_file("image.jpg", 100 /* quality */);
雖然一切正常,沒有任何錯誤,產生的JPEG圖像不具有與原始位圖圖像相同的顏色,例如,紅色和藍色交換,黃色和青色相同...
我們嘗試使用fseek()來設置輸入文件光標到bfh.bfOffBits,但顏色仍然關閉。
是否有任何其他步驟可能需要確保JPEG編碼正確完成?
謝謝,馬克。以下是BGR到RGB轉換的示例:http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html – alexg