Question

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

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

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...

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

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

Was it helpful?

Solution

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 files are also organized with the bottom line at the top of the file, you'll want to reverse that too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top