JPEG encoding bitmap (BMP) image from file using libjpeg / C++

时间:2022-09-02 00:27:21

We're using version 8d of IJG's libjpeg library to create JPEG images from 24-bit Windows bitmap (.bmp) files.

我们正在使用IJG的libjpeg库的8d版本来创建来自24位Windows位图(.bmp)文件的JPEG图像。

write_JPEG_file() function from IJG's example.c is being used without any modifications, as appears here: http://code.google.com/p/sumatrapdf/source/browse/trunk/ext/libjpeg-turbo/example.c?r=2397

来自IJG的example.c的write_JPEG_file()函数正在使用而没有任何修改,如下所示:http://code.google.com/p/sumatrapdf/source/browse/trunk/ext/libjpeg-turbo/example.c? R = 2397

The sequence of steps performed is as following:

执行的步骤顺序如下:

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 */ );

Although everything works without any errors, the resulting JPEG image does not have the same colors as the original bitmap image, e.g., red and blue are swapped, same for yellow and cyan...

虽然一切都没有任何错误,但生成的JPEG图像与原始位图图像的颜色不同,例如红色和蓝色交换,黄色和青色相同......

We tried using fseek() to set the input file cursor to bfh.bfOffBits, but the colors are still off.

我们尝试使用fseek()将输入文件光标设置为bfh.bfOffBits,但颜色仍处于关闭状态。

Is there any additional step that may be required to ensure that JPEG encoding is done properly?

是否还需要执行其他步骤以确保正确完成JPEG编码?

1 个解决方案

#1


3  

BMP files are encoded with the pixel colors in BGR order, and the JPEG library expects RGB order. You'll have to reverse the red and blue bytes out of each group of 3.

BMP文件使用BGR顺序的像素颜色进行编码,JPEG库需要RGB顺序。你将不得不反转每组3的红色和蓝色字节。

BMP files are also organized with the bottom line at the top of the file, you'll want to reverse that too.

BMP文件也按文件顶部的底行进行组织,您也想要反转它。

#1


3  

BMP files are encoded with the pixel colors in BGR order, and the JPEG library expects RGB order. You'll have to reverse the red and blue bytes out of each group of 3.

BMP文件使用BGR顺序的像素颜色进行编码,JPEG库需要RGB顺序。你将不得不反转每组3的红色和蓝色字节。

BMP files are also organized with the bottom line at the top of the file, you'll want to reverse that too.

BMP文件也按文件顶部的底行进行组织,您也想要反转它。