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

Using libjpeg with a 8-bit grayscale source image, can it be done?

我正在使用 libjpeg 创建 jpeg

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。

谢谢!

没关系,我找到问题所在了。行跨度需要匹配颜色 space.

原始来源有:

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

感谢您阅读所有案例。