From 32d81d98fdb9dd3f9b512c656c29d01d803fc1ce Mon Sep 17 00:00:00 2001 From: DRC Date: Tue, 21 Jul 2015 17:36:18 -0500 Subject: [PATCH] Further improvements to partial image decoding When using context-based upsampling, use a dummy color conversion routine instead of a dummy row buffer. This improves performance (since the actual color conversion routine no longer has to be called), and it also fixes valgrind errors when decompressing to RGB565. Valgrind previously complained, because using the RGB565 color converter with the dummy row buffer was causing a table lookup with undefined indices. --- jdapistd.c | 54 +++++++++++++++++++++++++++++++----------------------- jdmaster.c | 4 +--- jpegint.h | 10 ++-------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/jdapistd.c b/jdapistd.c index 578d5cc4d..18f9f6ceb 100644 --- a/jdapistd.c +++ b/jdapistd.c @@ -179,21 +179,37 @@ jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, } -/* Prepare temporary row buffer */ +/* Dummy color convert function used by jpeg_skip_scanlines() */ +LOCAL(void) +noop_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, + JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) +{ +} + + +/* + * In some cases, it is best to call jpeg_read_scanlines() and discard the + * output, rather than skipping the scanlines, because this allows us to + * maintain the internal state of the context-based upsampler. In these cases, + * we set up and tear down a dummy color converter in order to avoid valgrind + * errors and to achieve the best possible performance. + */ LOCAL(void) -dummy_buffer_setup (j_decompress_ptr cinfo) +read_and_discard_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines) { - int nc; + JDIMENSION n; + void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, + JDIMENSION input_row, JSAMPARRAY output_buf, + int num_rows); + + color_convert = cinfo->cconvert->color_convert; + cinfo->cconvert->color_convert = noop_convert; - if (!cinfo->master || cinfo->master->dummy_row_buffer) - return; + for (n = 0; n < num_lines; n++) + jpeg_read_scanlines(cinfo, NULL, 1); - nc = (cinfo->out_color_space == JCS_RGB565) ? - 2 : cinfo->out_color_components; - cinfo->master->dummy_row_buffer = - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - cinfo->output_width * nc * sizeof(JSAMPLE)); + cinfo->cconvert->color_convert = color_convert; } @@ -205,7 +221,7 @@ dummy_buffer_setup (j_decompress_ptr cinfo) LOCAL(void) increment_simple_rowgroup_ctr (j_decompress_ptr cinfo, JDIMENSION rows) { - JDIMENSION i, rows_left; + JDIMENSION rows_left; my_main_ptr main_ptr = (my_main_ptr) cinfo->main; /* Increment the counter to the next row group after the skipped rows. */ @@ -217,9 +233,7 @@ increment_simple_rowgroup_ctr (j_decompress_ptr cinfo, JDIMENSION rows) rows_left = rows % cinfo->max_v_samp_factor; cinfo->output_scanline += rows - rows_left; - dummy_buffer_setup(cinfo); - for (i = 0; i < rows_left; i++) - jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); + read_and_discard_scanlines(cinfo, rows_left); } /* @@ -278,9 +292,7 @@ jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines) if ((num_lines < lines_left_in_iMCU_row + 1) || (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full && lines_after_iMCU_row < lines_per_iMCU_row + 1)) { - dummy_buffer_setup(cinfo); - for (i = 0; i < num_lines; i++) - jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); + read_and_discard_scanlines(cinfo, num_lines); return num_lines; } @@ -344,9 +356,7 @@ jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines) /* It is complex to properly move to the middle of a context block, so * read the remaining lines instead of skipping them. */ - dummy_buffer_setup(cinfo); - for (i = 0; i < lines_to_read; i++) - jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); + read_and_discard_scanlines(cinfo, lines_to_read); } else { cinfo->output_scanline += lines_to_skip; cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row; @@ -383,9 +393,7 @@ jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines) /* It is complex to properly move to the middle of a context block, so * read the remaining lines instead of skipping them. */ - dummy_buffer_setup(cinfo); - for (i = 0; i < lines_to_read; i++) - jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); + read_and_discard_scanlines(cinfo, lines_to_read); } else { increment_simple_rowgroup_ctr(cinfo, lines_to_read); } diff --git a/jdmaster.c b/jdmaster.c index 2ef43feaf..604e29161 100644 --- a/jdmaster.c +++ b/jdmaster.c @@ -5,7 +5,7 @@ * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 2002-2009 by Guido Vollbeding. * libjpeg-turbo Modifications: - * Copyright (C) 2009-2011, 2015 D. R. Commander. + * Copyright (C) 2009-2011, D. R. Commander. * Copyright (C) 2013, Linaro Limited. * For conditions of distribution and use, see the accompanying README file. * @@ -733,7 +733,5 @@ jinit_master_decompress (j_decompress_ptr cinfo) master->pub.is_dummy_pass = FALSE; - master->pub.dummy_row_buffer = NULL; - master_selection(cinfo); } diff --git a/jpegint.h b/jpegint.h index 0e4dafe00..025accd91 100644 --- a/jpegint.h +++ b/jpegint.h @@ -4,8 +4,8 @@ * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 1997-2009 by Guido Vollbeding. - * libjpeg-turbo Modifications: - * Copyright (C) 2015, Google, Inc. + * It was modified by The libjpeg-turbo Project to include only code relevant + * to libjpeg-turbo. * For conditions of distribution and use, see the accompanying README file. * * This file provides common declarations for the various JPEG modules. @@ -137,12 +137,6 @@ struct jpeg_decomp_master { /* State variables made visible to other modules */ boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */ - - /* Buffer large enough to store an output row. This is used when - * jpeg_skip_scanlines() chooses to "skip" a row by reading it into this - * dummy buffer. - */ - JSAMPROW dummy_row_buffer; }; /* Input control module */