Skip to content

Commit

Permalink
Further improvements to partial image decoding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dcommander committed Jul 27, 2015
1 parent 317129b commit 32d81d9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
54 changes: 31 additions & 23 deletions jdapistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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. */
Expand All @@ -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);
}

/*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions jdmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
10 changes: 2 additions & 8 deletions jpegint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 32d81d9

Please sign in to comment.