代码之家  ›  专栏  ›  技术社区  ›  gmmo

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

  •  0
  • gmmo  · 技术社区  · 6 年前

    我正在使用libjpeg创建jpeg

    http://www.ijg.org/

    #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,
    };
    

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

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

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  0
  •   gmmo    6 年前

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

    原始来源:

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

    我刚把行步长改为1,因为它是一个灰度级,每像素使用1字节

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

    推荐文章