| Index: jdmaster.c
 | 
| diff --git a/jdmaster.c b/jdmaster.c
 | 
| index f0dd15f6c1e9d4a43fa8a0fda207b135bf9b3582..7908849bf9faee5d52ce03e94c633873381967c2 100644
 | 
| --- a/jdmaster.c
 | 
| +++ b/jdmaster.c
 | 
| @@ -5,10 +5,11 @@
 | 
|   * Copyright (C) 1991-1997, Thomas G. Lane.
 | 
|   * Modified 2002-2009 by Guido Vollbeding.
 | 
|   * libjpeg-turbo Modifications:
 | 
| - * Copyright (C) 2009-2011, D. R. Commander.
 | 
| + * Copyright (C) 2009-2011, 2016, D. R. Commander.
 | 
|   * Copyright (C) 2013, Linaro Limited.
 | 
| -
 | 
| - * For conditions of distribution and use, see the accompanying README file.
 | 
| + * Copyright (C) 2015, Google, Inc.
 | 
| + * For conditions of distribution and use, see the accompanying README.ijg
 | 
| + * file.
 | 
|   *
 | 
|   * This file contains master control logic for the JPEG decompressor.
 | 
|   * These routines are concerned with selecting the modules to be executed
 | 
| @@ -20,25 +21,7 @@
 | 
|  #include "jinclude.h"
 | 
|  #include "jpeglib.h"
 | 
|  #include "jpegcomp.h"
 | 
| -
 | 
| -
 | 
| -/* Private state */
 | 
| -
 | 
| -typedef struct {
 | 
| -  struct jpeg_decomp_master pub; /* public fields */
 | 
| -
 | 
| -  int pass_number;		/* # of passes completed */
 | 
| -
 | 
| -  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
 | 
| -
 | 
| -  /* Saved references to initialized quantizer modules,
 | 
| -   * in case we need to switch modes.
 | 
| -   */
 | 
| -  struct jpeg_color_quantizer * quantizer_1pass;
 | 
| -  struct jpeg_color_quantizer * quantizer_2pass;
 | 
| -} my_decomp_master;
 | 
| -
 | 
| -typedef my_decomp_master * my_master_ptr;
 | 
| +#include "jdmaster.h"
 | 
|  
 | 
|  
 | 
|  /*
 | 
| @@ -87,7 +70,7 @@ use_merged_upsample (j_decompress_ptr cinfo)
 | 
|        cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
 | 
|      return FALSE;
 | 
|    /* ??? also need to test for upsample-time rescaling, when & if supported */
 | 
| -  return TRUE;			/* by golly, it'll work... */
 | 
| +  return TRUE;                  /* by golly, it'll work... */
 | 
|  #else
 | 
|    return FALSE;
 | 
|  #endif
 | 
| @@ -299,10 +282,10 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
 | 
|         ci++, compptr++) {
 | 
|      int ssize = cinfo->_min_DCT_scaled_size;
 | 
|      while (ssize < DCTSIZE &&
 | 
| -	   ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
 | 
| -	    (compptr->h_samp_factor * ssize * 2) == 0) &&
 | 
| -	   ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
 | 
| -	    (compptr->v_samp_factor * ssize * 2) == 0)) {
 | 
| +           ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
 | 
| +            (compptr->h_samp_factor * ssize * 2) == 0) &&
 | 
| +           ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
 | 
| +            (compptr->v_samp_factor * ssize * 2) == 0)) {
 | 
|        ssize = ssize * 2;
 | 
|      }
 | 
|  #if JPEG_LIB_VERSION >= 70
 | 
| @@ -320,12 +303,12 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
 | 
|      /* Size in samples, after IDCT scaling */
 | 
|      compptr->downsampled_width = (JDIMENSION)
 | 
|        jdiv_round_up((long) cinfo->image_width *
 | 
| -		    (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
 | 
| -		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
 | 
| +                    (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
 | 
| +                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
 | 
|      compptr->downsampled_height = (JDIMENSION)
 | 
|        jdiv_round_up((long) cinfo->image_height *
 | 
| -		    (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
 | 
| -		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
 | 
| +                    (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
 | 
| +                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
 | 
|    }
 | 
|  
 | 
|  #else /* !IDCT_SCALING_SUPPORTED */
 | 
| @@ -366,12 +349,12 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
 | 
|    case JCS_YCCK:
 | 
|      cinfo->out_color_components = 4;
 | 
|      break;
 | 
| -  default:			/* else must be same colorspace as in file */
 | 
| +  default:                      /* else must be same colorspace as in file */
 | 
|      cinfo->out_color_components = cinfo->num_components;
 | 
|      break;
 | 
|    }
 | 
|    cinfo->output_components = (cinfo->quantize_colors ? 1 :
 | 
| -			      cinfo->out_color_components);
 | 
| +                              cinfo->out_color_components);
 | 
|  
 | 
|    /* See if upsampler will want to emit more than one row at a time */
 | 
|    if (use_merged_upsample(cinfo))
 | 
| @@ -388,20 +371,20 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
 | 
|   * processes are inner loops and need to be as fast as possible.  On most
 | 
|   * machines, particularly CPUs with pipelines or instruction prefetch,
 | 
|   * a (subscript-check-less) C table lookup
 | 
| - *		x = sample_range_limit[x];
 | 
| + *              x = sample_range_limit[x];
 | 
|   * is faster than explicit tests
 | 
| - *		if (x < 0)  x = 0;
 | 
| - *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
 | 
| + *              if (x < 0)  x = 0;
 | 
| + *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
 | 
|   * These processes all use a common table prepared by the routine below.
 | 
|   *
 | 
|   * For most steps we can mathematically guarantee that the initial value
 | 
|   * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
 | 
|   * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
 | 
| - * limiting step (just after the IDCT), a wildly out-of-range value is 
 | 
| + * limiting step (just after the IDCT), a wildly out-of-range value is
 | 
|   * possible if the input data is corrupt.  To avoid any chance of indexing
 | 
|   * off the end of memory and getting a bad-pointer trap, we perform the
 | 
|   * post-IDCT limiting thus:
 | 
| - *		x = range_limit[x & MASK];
 | 
| + *              x = range_limit[x & MASK];
 | 
|   * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
 | 
|   * samples.  Under normal circumstances this is more than enough range and
 | 
|   * a correct output will be generated; with bogus input data the mask will
 | 
| @@ -419,37 +402,34 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
 | 
|   * We can save some space by overlapping the start of the post-IDCT table
 | 
|   * with the simpler range limiting table.  The post-IDCT table begins at
 | 
|   * sample_range_limit + CENTERJSAMPLE.
 | 
| - *
 | 
| - * Note that the table is allocated in near data space on PCs; it's small
 | 
| - * enough and used often enough to justify this.
 | 
|   */
 | 
|  
 | 
|  LOCAL(void)
 | 
|  prepare_range_limit_table (j_decompress_ptr cinfo)
 | 
|  /* Allocate and fill in the sample_range_limit table */
 | 
|  {
 | 
| -  JSAMPLE * table;
 | 
| +  JSAMPLE *table;
 | 
|    int i;
 | 
|  
 | 
|    table = (JSAMPLE *)
 | 
|      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 | 
| -		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
 | 
| -  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
 | 
| +                (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
 | 
| +  table += (MAXJSAMPLE+1);      /* allow negative subscripts of simple table */
 | 
|    cinfo->sample_range_limit = table;
 | 
|    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
 | 
| -  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
 | 
| +  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * sizeof(JSAMPLE));
 | 
|    /* Main part of "simple" table: limit[x] = x */
 | 
|    for (i = 0; i <= MAXJSAMPLE; i++)
 | 
|      table[i] = (JSAMPLE) i;
 | 
| -  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
 | 
| +  table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
 | 
|    /* End of simple table, rest of first half of post-IDCT table */
 | 
|    for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
 | 
|      table[i] = MAXJSAMPLE;
 | 
|    /* Second half of post-IDCT table */
 | 
|    MEMZERO(table + (2 * (MAXJSAMPLE+1)),
 | 
| -	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
 | 
| +          (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
 | 
|    MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
 | 
| -	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
 | 
| +          cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
 | 
|  }
 | 
|  
 | 
|  
 | 
| @@ -582,6 +562,12 @@ master_selection (j_decompress_ptr cinfo)
 | 
|    /* Initialize input side of decompressor to consume first scan. */
 | 
|    (*cinfo->inputctl->start_input_pass) (cinfo);
 | 
|  
 | 
| +  /* Set the first and last iMCU columns to decompress from single-scan images.
 | 
| +   * By default, decompress all of the iMCU columns.
 | 
| +   */
 | 
| +  cinfo->master->first_iMCU_col = 0;
 | 
| +  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
 | 
| +
 | 
|  #ifdef D_MULTISCAN_FILES_SUPPORTED
 | 
|    /* If jpeg_start_decompress will read the whole file, initialize
 | 
|     * progress monitoring appropriately.  The input step is counted
 | 
| @@ -637,24 +623,24 @@ prepare_for_output_pass (j_decompress_ptr cinfo)
 | 
|      if (cinfo->quantize_colors && cinfo->colormap == NULL) {
 | 
|        /* Select new quantization method */
 | 
|        if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
 | 
| -	cinfo->cquantize = master->quantizer_2pass;
 | 
| -	master->pub.is_dummy_pass = TRUE;
 | 
| +        cinfo->cquantize = master->quantizer_2pass;
 | 
| +        master->pub.is_dummy_pass = TRUE;
 | 
|        } else if (cinfo->enable_1pass_quant) {
 | 
| -	cinfo->cquantize = master->quantizer_1pass;
 | 
| +        cinfo->cquantize = master->quantizer_1pass;
 | 
|        } else {
 | 
| -	ERREXIT(cinfo, JERR_MODE_CHANGE);
 | 
| +        ERREXIT(cinfo, JERR_MODE_CHANGE);
 | 
|        }
 | 
|      }
 | 
|      (*cinfo->idct->start_pass) (cinfo);
 | 
|      (*cinfo->coef->start_output_pass) (cinfo);
 | 
|      if (! cinfo->raw_data_out) {
 | 
|        if (! master->using_merged_upsample)
 | 
| -	(*cinfo->cconvert->start_pass) (cinfo);
 | 
| +        (*cinfo->cconvert->start_pass) (cinfo);
 | 
|        (*cinfo->upsample->start_pass) (cinfo);
 | 
|        if (cinfo->quantize_colors)
 | 
| -	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
 | 
| +        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
 | 
|        (*cinfo->post->start_pass) (cinfo,
 | 
| -	    (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
 | 
| +            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
 | 
|        (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
 | 
|      }
 | 
|    }
 | 
| @@ -663,7 +649,7 @@ prepare_for_output_pass (j_decompress_ptr cinfo)
 | 
|    if (cinfo->progress != NULL) {
 | 
|      cinfo->progress->completed_passes = master->pass_number;
 | 
|      cinfo->progress->total_passes = master->pass_number +
 | 
| -				    (master->pub.is_dummy_pass ? 2 : 1);
 | 
| +                                    (master->pub.is_dummy_pass ? 2 : 1);
 | 
|      /* In buffered-image mode, we assume one more output pass if EOI not
 | 
|       * yet reached, but no more passes if EOI has been reached.
 | 
|       */
 | 
| @@ -726,16 +712,13 @@ jpeg_new_colormap (j_decompress_ptr cinfo)
 | 
|  GLOBAL(void)
 | 
|  jinit_master_decompress (j_decompress_ptr cinfo)
 | 
|  {
 | 
| -  my_master_ptr master;
 | 
| +  my_master_ptr master = (my_master_ptr) cinfo->master;
 | 
|  
 | 
| -  master = (my_master_ptr)
 | 
| -      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 | 
| -				  SIZEOF(my_decomp_master));
 | 
| -  cinfo->master = (struct jpeg_decomp_master *) master;
 | 
|    master->pub.prepare_for_output_pass = prepare_for_output_pass;
 | 
|    master->pub.finish_output_pass = finish_output_pass;
 | 
|  
 | 
|    master->pub.is_dummy_pass = FALSE;
 | 
| +  master->pub.jinit_upsampler_no_alloc = FALSE;
 | 
|  
 | 
|    master_selection(cinfo);
 | 
|  }
 | 
| 
 |