简体   繁体   English

使用 libjpeg/C++ 的文件中的 JPEG 编码位图 (BMP) 图像

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

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, eg, 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 编码正确完成?

BMP files are encoded with the pixel colors in BGR order, and the JPEG library expects RGB order. BMP 文件使用 BGR 顺序的像素颜色进行编码,JPEG 库需要 RGB 顺序。 You'll have to reverse the red and blue bytes out of each group of 3.您必须从每组 3 个字节中反转红色和蓝色字节。

BMP files are also organized with the bottom line at the top of the file, you'll want to reverse that too. BMP 文件也用文件顶部的底线组织,您也需要反转它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM