src/java.desktop/share/native/libjavajpeg/jdmaster.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.desktop/share/native/libjavajpeg/jdmaster.c@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * jdmaster.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1991-1997, Thomas G. Lane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This file is part of the Independent JPEG Group's software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * For conditions of distribution and use, see the accompanying README file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This file contains master control logic for the JPEG decompressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * These routines are concerned with selecting the modules to be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * and with determining the number of passes and the work to be done in each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/* Private state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  struct jpeg_decomp_master pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  int pass_number;              /* # of passes completed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  /* Saved references to initialized quantizer modules,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   * in case we need to switch modes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  struct jpeg_color_quantizer * quantizer_1pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  struct jpeg_color_quantizer * quantizer_2pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
} my_decomp_master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
typedef my_decomp_master * my_master_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Determine whether merged upsample/color conversion should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
use_merged_upsample (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#ifdef UPSAMPLE_MERGING_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  /* Merging is the equivalent of plain box-filter upsampling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  /* jdmerge.c only supports YCC=>RGB color conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
      cinfo->out_color_space != JCS_RGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      cinfo->out_color_components != RGB_PIXELSIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  /* and it only handles 2h1v or 2h2v sampling ratios */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      cinfo->comp_info[1].h_samp_factor != 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      cinfo->comp_info[2].h_samp_factor != 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      cinfo->comp_info[0].v_samp_factor >  2 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      cinfo->comp_info[1].v_samp_factor != 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      cinfo->comp_info[2].v_samp_factor != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  /* ??? also need to test for upsample-time rescaling, when & if supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  return TRUE;                  /* by golly, it'll work... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Compute output image dimensions and related values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * NOTE: this is exported for possible use by application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Hence it mustn't do anything that can't be done twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Also note that it may be called before the master module is initialized!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/* Do computations that are needed before master selection phase */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#ifdef IDCT_SCALING_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  /* Prevent application from calling me at wrong times */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  if (cinfo->global_state != DSTATE_READY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#ifdef IDCT_SCALING_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  /* Compute actual output image dimensions and DCT scaling choices. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* Provide 1/8 scaling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    cinfo->output_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      jdiv_round_up((long) cinfo->image_width, 8L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    cinfo->output_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      jdiv_round_up((long) cinfo->image_height, 8L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    cinfo->min_DCT_scaled_size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Provide 1/4 scaling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    cinfo->output_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      jdiv_round_up((long) cinfo->image_width, 4L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    cinfo->output_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      jdiv_round_up((long) cinfo->image_height, 4L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    cinfo->min_DCT_scaled_size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* Provide 1/2 scaling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    cinfo->output_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      jdiv_round_up((long) cinfo->image_width, 2L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    cinfo->output_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      jdiv_round_up((long) cinfo->image_height, 2L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    cinfo->min_DCT_scaled_size = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* Provide 1/1 scaling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    cinfo->output_width = cinfo->image_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    cinfo->output_height = cinfo->image_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    cinfo->min_DCT_scaled_size = DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  /* In selecting the actual DCT scaling for each component, we try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
   * scale up the chroma components via IDCT scaling rather than upsampling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
   * This saves time if the upsampler gets to use 1:1 scaling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
   * Note this code assumes that the supported DCT scalings are powers of 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    int ssize = cinfo->min_DCT_scaled_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    while (ssize < DCTSIZE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
           (compptr->h_samp_factor * ssize * 2 <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
           (compptr->v_samp_factor * ssize * 2 <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      ssize = ssize * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    compptr->DCT_scaled_size = ssize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  /* Recompute downsampled dimensions of components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
   * application needs to know these if using raw downsampled data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /* Size in samples, after IDCT scaling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    compptr->downsampled_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      jdiv_round_up((long) cinfo->image_width *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    compptr->downsampled_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      jdiv_round_up((long) cinfo->image_height *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
#else /* !IDCT_SCALING_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  /* Hardwire it to "no scaling" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
  cinfo->output_width = cinfo->image_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  cinfo->output_height = cinfo->image_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   * and has computed unscaled downsampled_width and downsampled_height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
#endif /* IDCT_SCALING_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  /* Report number of components in selected colorspace. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  /* Probably this should be in the color conversion module... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  switch (cinfo->out_color_space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
  case JCS_GRAYSCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    cinfo->out_color_components = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
  case JCS_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
#if RGB_PIXELSIZE != 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    cinfo->out_color_components = RGB_PIXELSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
#endif /* else share code with YCbCr */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  case JCS_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    cinfo->out_color_components = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  case JCS_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
  case JCS_YCCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    cinfo->out_color_components = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  default:                      /* else must be same colorspace as in file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    cinfo->out_color_components = cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                              cinfo->out_color_components);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  /* See if upsampler will want to emit more than one row at a time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
  if (use_merged_upsample(cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    cinfo->rec_outbuf_height = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * Several decompression processes need to range-limit values to the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * due to noise introduced by quantization, roundoff error, etc.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * processes are inner loops and need to be as fast as possible.  On most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * machines, particularly CPUs with pipelines or instruction prefetch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * a (subscript-check-less) C table lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *              x = sample_range_limit[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * is faster than explicit tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *              if (x < 0)  x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * These processes all use a common table prepared by the routine below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * For most steps we can mathematically guarantee that the initial value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * limiting step (just after the IDCT), a wildly out-of-range value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * possible if the input data is corrupt.  To avoid any chance of indexing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * off the end of memory and getting a bad-pointer trap, we perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * post-IDCT limiting thus:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *              x = range_limit[x & MASK];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * samples.  Under normal circumstances this is more than enough range and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * a correct output will be generated; with bogus input data the mask will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * cause wraparound, and we will safely generate a bogus-but-in-range output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * For the post-IDCT step, we want to convert the data from signed to unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * representation by adding CENTERJSAMPLE at the same time that we limit it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * So the post-IDCT limiting table ends up looking like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *   0,1,...,CENTERJSAMPLE-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * Negative inputs select values from the upper half of the table after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * masking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * We can save some space by overlapping the start of the post-IDCT table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * with the simpler range limiting table.  The post-IDCT table begins at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * sample_range_limit + CENTERJSAMPLE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * Note that the table is allocated in near data space on PCs; it's small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * enough and used often enough to justify this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
prepare_range_limit_table (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
/* Allocate and fill in the sample_range_limit table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
  JSAMPLE * table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  table = (JSAMPLE *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  table += (MAXJSAMPLE+1);      /* allow negative subscripts of simple table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
  cinfo->sample_range_limit = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
  /* Main part of "simple" table: limit[x] = x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  for (i = 0; i <= MAXJSAMPLE; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    table[i] = (JSAMPLE) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
  table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
  /* End of simple table, rest of first half of post-IDCT table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    table[i] = MAXJSAMPLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
  /* Second half of post-IDCT table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
          (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
          cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * Master selection of decompression modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * This is done once at jpeg_start_decompress time.  We determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * which modules will be used and give them appropriate initialization calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * We also initialize the decompressor input side to begin consuming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * Since jpeg_read_header has finished, we know what is in the SOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * and (first) SOS markers.  We also have all the application parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
master_selection (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
  boolean use_c_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
  long samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
  JDIMENSION jd_samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
  /* Initialize dimensions and other stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
  jpeg_calc_output_dimensions(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
  prepare_range_limit_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
  /* Width of an output scanline must be representable as JDIMENSION. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
  jd_samplesperrow = (JDIMENSION) samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
  if ((long) jd_samplesperrow != samplesperrow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
  /* Initialize my private state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  master->pass_number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
  master->using_merged_upsample = use_merged_upsample(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
  /* Color quantizer selection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
  master->quantizer_1pass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  master->quantizer_2pass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  /* No mode changes if not using buffered-image mode. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    cinfo->enable_1pass_quant = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    cinfo->enable_external_quant = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    cinfo->enable_2pass_quant = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  if (cinfo->quantize_colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if (cinfo->raw_data_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      ERREXIT(cinfo, JERR_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /* 2-pass quantizer only works in 3-component color space. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    if (cinfo->out_color_components != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
      cinfo->enable_1pass_quant = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
      cinfo->enable_external_quant = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
      cinfo->enable_2pass_quant = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
      cinfo->colormap = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    } else if (cinfo->colormap != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
      cinfo->enable_external_quant = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    } else if (cinfo->two_pass_quantize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
      cinfo->enable_2pass_quant = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
      cinfo->enable_1pass_quant = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if (cinfo->enable_1pass_quant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
#ifdef QUANT_1PASS_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      jinit_1pass_quantizer(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
      master->quantizer_1pass = cinfo->cquantize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /* We use the 2-pass code to map to external colormaps. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
#ifdef QUANT_2PASS_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      jinit_2pass_quantizer(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
      master->quantizer_2pass = cinfo->cquantize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /* If both quantizers are initialized, the 2-pass one is left active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * this is necessary for starting with quantization to an external map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
  /* Post-processing: in particular, color conversion first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
  if (! cinfo->raw_data_out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    if (master->using_merged_upsample) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
#ifdef UPSAMPLE_MERGING_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      jinit_merged_upsampler(cinfo); /* does color conversion too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      jinit_color_deconverter(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      jinit_upsampler(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
  /* Inverse DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  jinit_inverse_dct(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
  /* Entropy decoding: either Huffman or arithmetic coding. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
  if (cinfo->arith_code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    if (cinfo->progressive_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
#ifdef D_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
      jinit_phuff_decoder(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
      jinit_huff_decoder(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  /* Initialize principal buffer controllers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
  jinit_d_coef_controller(cinfo, use_c_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
  if (! cinfo->raw_data_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
  /* We can now tell the memory manager to allocate virtual arrays. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
  /* Initialize input side of decompressor to consume first scan. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
  (*cinfo->inputctl->start_input_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
#ifdef D_MULTISCAN_FILES_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
  /* If jpeg_start_decompress will read the whole file, initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
   * progress monitoring appropriately.  The input step is counted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
   * as one pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
      cinfo->inputctl->has_multiple_scans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    int nscans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /* Estimate number of scans to set pass_limit. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    if (cinfo->progressive_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
      nscans = 2 + 3 * cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      nscans = cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    cinfo->progress->pass_counter = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    cinfo->progress->completed_passes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /* Count the input pass as done */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    master->pass_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
#endif /* D_MULTISCAN_FILES_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * Per-pass setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * This is called at the beginning of each output pass.  We determine which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 * modules will be active during this pass and give them appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 * start_pass calls.  We also set is_dummy_pass to indicate whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 * is a "real" output pass or a dummy pass for color quantization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * (In the latter case, jdapistd.c will crank the pass to completion.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
prepare_for_output_pass (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
  if (master->pub.is_dummy_pass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
#ifdef QUANT_2PASS_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /* Final pass of 2-pass quantization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    master->pub.is_dummy_pass = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
#endif /* QUANT_2PASS_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      /* Select new quantization method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        cinfo->cquantize = master->quantizer_2pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        master->pub.is_dummy_pass = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
      } else if (cinfo->enable_1pass_quant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        cinfo->cquantize = master->quantizer_1pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        ERREXIT(cinfo, JERR_MODE_CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    (*cinfo->idct->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    (*cinfo->coef->start_output_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    if (! cinfo->raw_data_out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
      if (! master->using_merged_upsample)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        (*cinfo->cconvert->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
      (*cinfo->upsample->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      if (cinfo->quantize_colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
      (*cinfo->post->start_pass) (cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
  /* Set up progress monitor's pass info if present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
  if (cinfo->progress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    cinfo->progress->completed_passes = master->pass_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    cinfo->progress->total_passes = master->pass_number +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                    (master->pub.is_dummy_pass ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /* In buffered-image mode, we assume one more output pass if EOI not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * yet reached, but no more passes if EOI has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 * Finish up at end of an output pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
finish_output_pass (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  if (cinfo->quantize_colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    (*cinfo->cquantize->finish_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
  master->pass_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
#ifdef D_MULTISCAN_FILES_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 * Switch to a new external colormap between output passes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
jpeg_new_colormap (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
  /* Prevent application from calling me at wrong times */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
  if (cinfo->global_state != DSTATE_BUFIMAGE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
      cinfo->colormap != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /* Select 2-pass quantizer for external colormap use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    cinfo->cquantize = master->quantizer_2pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /* Notify quantizer of colormap change */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    (*cinfo->cquantize->new_color_map) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    master->pub.is_dummy_pass = FALSE; /* just in case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    ERREXIT(cinfo, JERR_MODE_CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
#endif /* D_MULTISCAN_FILES_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
 * Initialize master decompression control and select active modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 * This is performed at the start of jpeg_start_decompress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
jinit_master_decompress (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  my_master_ptr master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  master = (my_master_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                  SIZEOF(my_decomp_master));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  cinfo->master = (struct jpeg_decomp_master *) master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  master->pub.prepare_for_output_pass = prepare_for_output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  master->pub.finish_output_pass = finish_output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
  master->pub.is_dummy_pass = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
  master_selection(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
}