Tho*_*son 10 c++ jpeg image image-processing libjpeg
我正在使用libjpeg将jpeg映像从磁盘解码到堆上分配的内存缓冲区.我用来jpeg_read_scanlines从文件中读取和解码每个扫描线.这完美地工作,将每个像素解码为24位RGB值.
问题是我正在使用一个额外的第三方库,它需要一个BGR格式的缓冲区(而不是RGB).当使用这个库时,我得到奇怪的结果,因为通道的顺序错误.
因此,我想找到一种方法使libjpeg解码为BGR格式而不是RGB.我已经在网上搜索过,无法找到如何配置libjpeg来做到这一点?我知道我可以通过内存缓冲区进行额外的传递并手动重新排序颜色通道,但是我正在处理的应用程序非常关键,必须尽可能快速有效.
几种解决方案:
JCS_EXT_BGR和JCS_EXT_BGRX颜色空间.另外,你说你在追赶速度但你操纵BGR数据(而不是BGRX).这对我来说没有多大意义,因为在32位边界上对齐像素可能要快得多.
正如 Antun Tun 所说,配置位于jmorecfg.h. 在我的 libjpeg (v7) 版本中,它位于第 320 行:
#define RGB_RED 0 /* Offset of Red in an RGB scanline element */
#define RGB_GREEN 1 /* Offset of Green */
#define RGB_BLUE 2 /* Offset of Blue */
Run Code Online (Sandbox Code Playgroud)
所以你只需将它们更改为:
#define RGB_RED 2 /* Offset of Red in an RGB scanline element */
#define RGB_GREEN 1 /* Offset of Green */
#define RGB_BLUE 0 /* Offset of Blue */
Run Code Online (Sandbox Code Playgroud)
你就完成了。评论进一步指出:
/*
* RESTRICTIONS:
* 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
* 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
* useful if you are using JPEG color spaces other than YCbCr or grayscale.
* 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
* is not 3 (they don't understand about dummy color components!). So you
* can't use color quantization if you change that value.
*/
Run Code Online (Sandbox Code Playgroud)