Search code examples
linuxlibjpegv4l2

How to get LibJPEG to adjust Quality of resulting image


I'm using libjpeg to save a V4L2 buffer to file. The resulting image looks great. Now I am trying to set the quality on the JPEG ( range 1-100 ). I consulted the docs: https://www4.cs.fau.de/Services/Doc/graphics/doc/jpeg/libjpeg.html

I see that there is a jpeg_set_quality function. I added it to me code by it always looks like highest quality, even when the quality parameter is 1.

How can I have the quality parameter take effect? Thanks.

My save jpeg function is shown here:

static void save_jpeg(CamInfo *cam, struct v4l2_buffer *dqBuf)
{
    static int inited = 0;
    static int fileno = 0;
    int row_stride;
    JSAMPROW row_pointer[1];
    char *image_buf;
    FILE * outfile;
    char filename[32];

    if(!inited)
    {
        memset(&cam->cinfo, 0, sizeof(cam->cinfo));
        memset(&cam->jerr, 0, sizeof(cam->jerr));

        cam->cinfo.err = jpeg_std_error(&cam->jerr);
        jpeg_create_compress(&cam->cinfo);

        cam->cinfo.image_width = WIDTH; //image width and height, in pixels
        cam->cinfo.image_height = HEIGHT;
        cam->cinfo.input_components = 3;    // must be 3
        cam->cinfo.in_color_space = JCS_YCbCr;
        printf("Setting JPEG quality: %d\n",quality);
        jpeg_set_quality(&cam->cinfo, quality, (boolean)0);
        jpeg_set_defaults(&cam->cinfo);

        inited = 1;
    }

    if( continuous_stream )
        fileno = 0; /* same filename each time */

    printf("About to write JPEG image\n");

    sprintf(filename, "/tmp/nginx/vl.jpeg");

    if ((outfile = fopen(filename, "wb")) == NULL)
    {
        fprintf(stderr, "can't open %s\n", filename);
        exit(1);
    }
    jpeg_stdio_dest(&cam->cinfo, outfile);

    jpeg_start_compress(&cam->cinfo, TRUE);

    row_stride = WIDTH * 3;

    image_buf = planar_to_interleave(mCam.frames[dqBuf->index]);

    while (cam->cinfo.next_scanline < cam->cinfo.image_height)
    {
        row_pointer[0] = (JSAMPROW)(&image_buf[cam->cinfo.next_scanline * row_stride]);
        (void) jpeg_write_scanlines(&cam->cinfo, row_pointer, 1);
    }

    jpeg_finish_compress(&cam->cinfo);
    fclose(outfile);
}

Solution

  • Calling jpeg_set_defaults() after jpeg_set_quality() resets the quality to 75. Swap the order of the function calls to fix this.

    From the source:

    GLOBAL(void)
    jpeg_set_defaults (j_compress_ptr cinfo)
    {
      …
      /* Set up two quantization tables using default quality of 75 */
      jpeg_set_quality(cinfo, 75, TRUE);