使用带有8位灰度源图像的libjpeg,可以做到吗?

问题描述 投票:0回答:1

我正在使用libjpeg来创建jpegs

http://www.ijg.org/

但是,只有当我将3分量的rba图像传递给它时,它才有效。如果我尝试传递1分量灰度图像,则不起作用。具体来说,这有效:

#define COLOR_COMPONENTS    (3)
#define COLOR_SPACE         (JCS_RGB)
JSAMPLE image_buffer[WIDTH*HEIGHT *3] = 
{
0x80, 0x80, 0x80,    0x80, 0x80, 0x80,    0x80, 0x80, 0x80,    0x80, 0x80, 0x80,
0x80, 0x80, 0x80,    0x00, 0x00, 0x00,    0x00, 0x00, 0x00,    0x80, 0x80, 0x80,
0x80, 0x80, 0x80,    0x00, 0x00, 0x00,    0x00, 0x00, 0x00,    0x80, 0x80, 0x80,
0x80, 0x80, 0x80,    0x80, 0x80, 0x80,    0x80, 0x80, 0x80,    0x80, 0x80, 0x80,
};

...
// inside libjpeg I set the values
cinfo.image_width = image_width;    /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = COLOR_COMPONENTS;      /* # of color components per pixel */
cinfo.in_color_space = COLOR_SPACE;     /* colorspace of input image */
/* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo);

但是这不起作用:

#define COLOR_COMPONENTS    (1)
#define COLOR_SPACE         (JCS_GRAYSCALE)
JSAMPLE image_buffer[WIDTH*HEIGHT * 1] =
{
    0x80, 0x80, 0x80, 0x80,
    0x80, 0x00, 0x00, 0x80,
    0x80, 0x00, 0x00, 0x80,
    0x80, 0x80, 0x80, 0x80,
};

我可以使用libjpeg编码8位图像吗?以下代码的正确设置是什么?

cinfo.input_components = ???
cinfo.in_color_space = ???

代码需要在低时钟CPU上运行。因此,我没有足够的周期来从灰度图像转换为RGB。

谢谢!

libjpeg
1个回答
0
投票

没关系,我发现了问题所在。行步幅需要匹配颜色空间。

原始来源有:

row_stride = image_width * 3;   /* JSAMPLEs per row in image_buffer */

我刚刚将row_stride更改为1,因为它是一个灰度,每个像素使用1个字节

row_stride = image_width * 1;   /* JSAMPLEs per row in image_buffer */

感谢所有案例的阅读。

© www.soinside.com 2019 - 2024. All rights reserved.