Coverage Report

Created: 2026-04-12 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/jdmaster.c
Line
Count
Source
1
/*
2
 * jdmaster.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2002-2009 by Guido Vollbeding.
7
 * Lossless JPEG Modifications:
8
 * Copyright (C) 1999, Ken Murchison.
9
 * libjpeg-turbo Modifications:
10
 * Copyright (C) 2009-2011, 2016, 2019, 2022-2024, 2026, D. R. Commander.
11
 * Copyright (C) 2013, Linaro Limited.
12
 * Copyright (C) 2015, Google, Inc.
13
 * For conditions of distribution and use, see the accompanying README.ijg
14
 * file.
15
 *
16
 * This file contains master control logic for the JPEG decompressor.
17
 * These routines are concerned with selecting the modules to be executed
18
 * and with determining the number of passes and the work to be done in each
19
 * pass.
20
 */
21
22
#define JPEG_INTERNALS
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
#include "jpegapicomp.h"
26
#include "jdmaster.h"
27
#ifdef WITH_PROFILE
28
#include "tjutil.h"
29
#endif
30
31
32
/*
33
 * Determine whether merged upsample/color conversion should be used.
34
 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
35
 */
36
37
LOCAL(boolean)
38
use_merged_upsample(j_decompress_ptr cinfo)
39
60.6k
{
40
60.6k
#ifdef UPSAMPLE_MERGING_SUPPORTED
41
  /* Colorspace conversion is not supported with lossless JPEG images */
42
60.6k
  if (cinfo->master->lossless)
43
15.5k
    return FALSE;
44
  /* Merging is the equivalent of plain box-filter upsampling */
45
45.0k
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
46
36.0k
    return FALSE;
47
  /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
48
9.06k
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
49
2.95k
      (cinfo->out_color_space != JCS_RGB &&
50
2.95k
       cinfo->out_color_space != JCS_RGB565 &&
51
2.95k
       cinfo->out_color_space != JCS_EXT_RGB &&
52
0
       cinfo->out_color_space != JCS_EXT_RGBX &&
53
0
       cinfo->out_color_space != JCS_EXT_BGR &&
54
0
       cinfo->out_color_space != JCS_EXT_BGRX &&
55
0
       cinfo->out_color_space != JCS_EXT_XBGR &&
56
0
       cinfo->out_color_space != JCS_EXT_XRGB &&
57
0
       cinfo->out_color_space != JCS_EXT_RGBA &&
58
0
       cinfo->out_color_space != JCS_EXT_BGRA &&
59
0
       cinfo->out_color_space != JCS_EXT_ABGR &&
60
0
       cinfo->out_color_space != JCS_EXT_ARGB))
61
6.10k
    return FALSE;
62
2.95k
  if ((cinfo->out_color_space == JCS_RGB565 &&
63
0
       cinfo->out_color_components != 3) ||
64
2.95k
      (cinfo->out_color_space != JCS_RGB565 &&
65
2.95k
       cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
66
0
    return FALSE;
67
  /* and it only handles 2h1v or 2h2v sampling ratios */
68
2.95k
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
69
1.31k
      cinfo->comp_info[1].h_samp_factor != 1 ||
70
1.12k
      cinfo->comp_info[2].h_samp_factor != 1 ||
71
1.02k
      cinfo->comp_info[0].v_samp_factor >  2 ||
72
918
      cinfo->comp_info[1].v_samp_factor != 1 ||
73
702
      cinfo->comp_info[2].v_samp_factor != 1)
74
2.32k
    return FALSE;
75
  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
76
634
  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
77
634
      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
78
634
      cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
79
0
    return FALSE;
80
  /* ??? also need to test for upsample-time rescaling, when & if supported */
81
634
  return TRUE;                  /* by golly, it'll work... */
82
#else
83
  return FALSE;
84
#endif
85
634
}
86
87
88
/*
89
 * Compute output image dimensions and related values.
90
 * NOTE: this is exported for possible use by application.
91
 * Hence it mustn't do anything that can't be done twice.
92
 */
93
94
#if JPEG_LIB_VERSION >= 80
95
GLOBAL(void)
96
#else
97
LOCAL(void)
98
#endif
99
jpeg_core_output_dimensions(j_decompress_ptr cinfo)
100
/* Do computations that are needed before master selection phase.
101
 * This function is used for transcoding and full decompression.
102
 */
103
30.3k
{
104
30.3k
#ifdef IDCT_SCALING_SUPPORTED
105
30.3k
  int ci;
106
30.3k
  jpeg_component_info *compptr;
107
108
30.3k
  if (!cinfo->master->lossless) {
109
    /* Compute actual output image dimensions and DCT scaling choices. */
110
22.5k
    if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
111
      /* Provide 1/block_size scaling */
112
4.50k
      cinfo->output_width = (JDIMENSION)
113
4.50k
        jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
114
4.50k
      cinfo->output_height = (JDIMENSION)
115
4.50k
        jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
116
4.50k
      cinfo->_min_DCT_h_scaled_size = 1;
117
4.50k
      cinfo->_min_DCT_v_scaled_size = 1;
118
18.0k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
119
      /* Provide 2/block_size scaling */
120
0
      cinfo->output_width = (JDIMENSION)
121
0
        jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
122
0
      cinfo->output_height = (JDIMENSION)
123
0
        jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
124
0
      cinfo->_min_DCT_h_scaled_size = 2;
125
0
      cinfo->_min_DCT_v_scaled_size = 2;
126
18.0k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
127
      /* Provide 3/block_size scaling */
128
0
      cinfo->output_width = (JDIMENSION)
129
0
        jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
130
0
      cinfo->output_height = (JDIMENSION)
131
0
        jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
132
0
      cinfo->_min_DCT_h_scaled_size = 3;
133
0
      cinfo->_min_DCT_v_scaled_size = 3;
134
18.0k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
135
      /* Provide 4/block_size scaling */
136
4.50k
      cinfo->output_width = (JDIMENSION)
137
4.50k
        jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
138
4.50k
      cinfo->output_height = (JDIMENSION)
139
4.50k
        jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
140
4.50k
      cinfo->_min_DCT_h_scaled_size = 4;
141
4.50k
      cinfo->_min_DCT_v_scaled_size = 4;
142
13.5k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
143
      /* Provide 5/block_size scaling */
144
0
      cinfo->output_width = (JDIMENSION)
145
0
        jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
146
0
      cinfo->output_height = (JDIMENSION)
147
0
        jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
148
0
      cinfo->_min_DCT_h_scaled_size = 5;
149
0
      cinfo->_min_DCT_v_scaled_size = 5;
150
13.5k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
151
      /* Provide 6/block_size scaling */
152
0
      cinfo->output_width = (JDIMENSION)
153
0
        jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
154
0
      cinfo->output_height = (JDIMENSION)
155
0
        jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
156
0
      cinfo->_min_DCT_h_scaled_size = 6;
157
0
      cinfo->_min_DCT_v_scaled_size = 6;
158
13.5k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
159
      /* Provide 7/block_size scaling */
160
0
      cinfo->output_width = (JDIMENSION)
161
0
        jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
162
0
      cinfo->output_height = (JDIMENSION)
163
0
        jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
164
0
      cinfo->_min_DCT_h_scaled_size = 7;
165
0
      cinfo->_min_DCT_v_scaled_size = 7;
166
13.5k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
167
      /* Provide 8/block_size scaling */
168
13.5k
      cinfo->output_width = (JDIMENSION)
169
13.5k
        jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
170
13.5k
      cinfo->output_height = (JDIMENSION)
171
13.5k
        jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
172
13.5k
      cinfo->_min_DCT_h_scaled_size = 8;
173
13.5k
      cinfo->_min_DCT_v_scaled_size = 8;
174
13.5k
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
175
      /* Provide 9/block_size scaling */
176
0
      cinfo->output_width = (JDIMENSION)
177
0
        jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
178
0
      cinfo->output_height = (JDIMENSION)
179
0
        jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
180
0
      cinfo->_min_DCT_h_scaled_size = 9;
181
0
      cinfo->_min_DCT_v_scaled_size = 9;
182
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
183
      /* Provide 10/block_size scaling */
184
0
      cinfo->output_width = (JDIMENSION)
185
0
        jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
186
0
      cinfo->output_height = (JDIMENSION)
187
0
        jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
188
0
      cinfo->_min_DCT_h_scaled_size = 10;
189
0
      cinfo->_min_DCT_v_scaled_size = 10;
190
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
191
      /* Provide 11/block_size scaling */
192
0
      cinfo->output_width = (JDIMENSION)
193
0
        jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
194
0
      cinfo->output_height = (JDIMENSION)
195
0
        jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
196
0
      cinfo->_min_DCT_h_scaled_size = 11;
197
0
      cinfo->_min_DCT_v_scaled_size = 11;
198
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
199
      /* Provide 12/block_size scaling */
200
0
      cinfo->output_width = (JDIMENSION)
201
0
        jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
202
0
      cinfo->output_height = (JDIMENSION)
203
0
        jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
204
0
      cinfo->_min_DCT_h_scaled_size = 12;
205
0
      cinfo->_min_DCT_v_scaled_size = 12;
206
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
207
      /* Provide 13/block_size scaling */
208
0
      cinfo->output_width = (JDIMENSION)
209
0
        jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
210
0
      cinfo->output_height = (JDIMENSION)
211
0
        jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
212
0
      cinfo->_min_DCT_h_scaled_size = 13;
213
0
      cinfo->_min_DCT_v_scaled_size = 13;
214
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
215
      /* Provide 14/block_size scaling */
216
0
      cinfo->output_width = (JDIMENSION)
217
0
        jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
218
0
      cinfo->output_height = (JDIMENSION)
219
0
        jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
220
0
      cinfo->_min_DCT_h_scaled_size = 14;
221
0
      cinfo->_min_DCT_v_scaled_size = 14;
222
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
223
      /* Provide 15/block_size scaling */
224
0
      cinfo->output_width = (JDIMENSION)
225
0
        jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
226
0
      cinfo->output_height = (JDIMENSION)
227
0
        jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
228
0
      cinfo->_min_DCT_h_scaled_size = 15;
229
0
      cinfo->_min_DCT_v_scaled_size = 15;
230
0
    } else {
231
      /* Provide 16/block_size scaling */
232
0
      cinfo->output_width = (JDIMENSION)
233
0
        jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
234
0
      cinfo->output_height = (JDIMENSION)
235
0
        jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
236
0
      cinfo->_min_DCT_h_scaled_size = 16;
237
0
      cinfo->_min_DCT_v_scaled_size = 16;
238
0
    }
239
240
    /* Recompute dimensions of components */
241
63.8k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
242
41.3k
         ci++, compptr++) {
243
41.3k
      compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
244
41.3k
      compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
245
41.3k
    }
246
22.5k
  } else
247
7.76k
#endif /* !IDCT_SCALING_SUPPORTED */
248
7.76k
  {
249
    /* Hardwire it to "no scaling" */
250
7.76k
    cinfo->output_width = cinfo->image_width;
251
7.76k
    cinfo->output_height = cinfo->image_height;
252
    /* jdinput.c has already initialized DCT_scaled_size,
253
     * and has computed unscaled downsampled_width and downsampled_height.
254
     */
255
7.76k
  }
256
30.3k
}
257
258
259
/*
260
 * Compute output image dimensions and related values.
261
 * NOTE: this is exported for possible use by application.
262
 * Hence it mustn't do anything that can't be done twice.
263
 * Also note that it may be called before the master module is initialized!
264
 */
265
266
GLOBAL(void)
267
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
268
/* Do computations that are needed before master selection phase */
269
30.3k
{
270
30.3k
#ifdef IDCT_SCALING_SUPPORTED
271
30.3k
  int ci;
272
30.3k
  jpeg_component_info *compptr;
273
30.3k
#endif
274
275
  /* Prevent application from calling me at wrong times */
276
30.3k
  if (cinfo->global_state != DSTATE_READY)
277
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
278
279
  /* Compute core output image dimensions and DCT scaling choices. */
280
30.3k
  jpeg_core_output_dimensions(cinfo);
281
282
30.3k
#ifdef IDCT_SCALING_SUPPORTED
283
284
30.3k
  if (!cinfo->master->lossless) {
285
    /* In selecting the actual DCT scaling for each component, we try to
286
     * scale up the chroma components via IDCT scaling rather than upsampling.
287
     * This saves time if the upsampler gets to use 1:1 scaling.
288
     * Note this code adapts subsampling ratios which are powers of 2.
289
     */
290
63.8k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
291
41.3k
         ci++, compptr++) {
292
41.3k
      int ssize = cinfo->_min_DCT_scaled_size;
293
43.8k
      while (ssize < DCTSIZE &&
294
17.8k
             ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
295
17.8k
              (compptr->h_samp_factor * ssize * 2) == 0) &&
296
4.76k
             ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
297
4.76k
              (compptr->v_samp_factor * ssize * 2) == 0)) {
298
2.52k
        ssize = ssize * 2;
299
2.52k
      }
300
#if JPEG_LIB_VERSION >= 70
301
      compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
302
#else
303
41.3k
      compptr->DCT_scaled_size = ssize;
304
41.3k
#endif
305
41.3k
    }
306
307
    /* Recompute downsampled dimensions of components;
308
     * application needs to know these if using raw downsampled data.
309
     */
310
63.8k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
311
41.3k
         ci++, compptr++) {
312
      /* Size in samples, after IDCT scaling */
313
41.3k
      compptr->downsampled_width = (JDIMENSION)
314
41.3k
        jdiv_round_up((long)cinfo->image_width *
315
41.3k
                      (long)(compptr->h_samp_factor *
316
41.3k
                             compptr->_DCT_scaled_size),
317
41.3k
                      (long)(cinfo->max_h_samp_factor * DCTSIZE));
318
41.3k
      compptr->downsampled_height = (JDIMENSION)
319
41.3k
        jdiv_round_up((long)cinfo->image_height *
320
41.3k
                      (long)(compptr->v_samp_factor *
321
41.3k
                             compptr->_DCT_scaled_size),
322
41.3k
                      (long)(cinfo->max_v_samp_factor * DCTSIZE));
323
41.3k
    }
324
22.5k
  } else
325
7.76k
#endif /* IDCT_SCALING_SUPPORTED */
326
7.76k
  {
327
    /* Hardwire it to "no scaling" */
328
7.76k
    cinfo->output_width = cinfo->image_width;
329
7.76k
    cinfo->output_height = cinfo->image_height;
330
    /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
331
     * and has computed unscaled downsampled_width and downsampled_height.
332
     */
333
7.76k
  }
334
335
  /* Report number of components in selected colorspace. */
336
  /* Probably this should be in the color conversion module... */
337
30.3k
  switch (cinfo->out_color_space) {
338
6.05k
  case JCS_GRAYSCALE:
339
6.05k
    cinfo->out_color_components = 1;
340
6.05k
    break;
341
0
  case JCS_RGB:
342
6.08k
  case JCS_EXT_RGB:
343
6.08k
  case JCS_EXT_RGBX:
344
6.08k
  case JCS_EXT_BGR:
345
12.1k
  case JCS_EXT_BGRX:
346
12.1k
  case JCS_EXT_XBGR:
347
12.1k
  case JCS_EXT_XRGB:
348
12.1k
  case JCS_EXT_RGBA:
349
12.1k
  case JCS_EXT_BGRA:
350
18.1k
  case JCS_EXT_ABGR:
351
18.1k
  case JCS_EXT_ARGB:
352
18.1k
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
353
18.1k
    break;
354
0
  case JCS_YCbCr:
355
0
  case JCS_RGB565:
356
0
    cinfo->out_color_components = 3;
357
0
    break;
358
6.05k
  case JCS_CMYK:
359
6.05k
  case JCS_YCCK:
360
6.05k
    cinfo->out_color_components = 4;
361
6.05k
    break;
362
0
  default:                      /* else must be same colorspace as in file */
363
0
    cinfo->out_color_components = cinfo->num_components;
364
0
    break;
365
30.3k
  }
366
30.3k
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
367
30.3k
                              cinfo->out_color_components);
368
369
  /* See if upsampler will want to emit more than one row at a time */
370
30.3k
  if (use_merged_upsample(cinfo))
371
317
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
372
29.9k
  else
373
29.9k
    cinfo->rec_outbuf_height = 1;
374
30.3k
}
375
376
377
/*
378
 * Several decompression processes need to range-limit values to the range
379
 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
380
 * due to noise introduced by quantization, roundoff error, etc.  These
381
 * processes are inner loops and need to be as fast as possible.  On most
382
 * machines, particularly CPUs with pipelines or instruction prefetch,
383
 * a (subscript-check-less) C table lookup
384
 *              x = sample_range_limit[x];
385
 * is faster than explicit tests
386
 *              if (x < 0)  x = 0;
387
 *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
388
 * These processes all use a common table prepared by the routine below.
389
 *
390
 * For most steps we can mathematically guarantee that the initial value
391
 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
392
 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
393
 * limiting step (just after the IDCT), a wildly out-of-range value is
394
 * possible if the input data is corrupt.  To avoid any chance of indexing
395
 * off the end of memory and getting a bad-pointer trap, we perform the
396
 * post-IDCT limiting thus:
397
 *              x = range_limit[x & MASK];
398
 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
399
 * samples.  Under normal circumstances this is more than enough range and
400
 * a correct output will be generated; with bogus input data the mask will
401
 * cause wraparound, and we will safely generate a bogus-but-in-range output.
402
 * For the post-IDCT step, we want to convert the data from signed to unsigned
403
 * representation by adding CENTERJSAMPLE at the same time that we limit it.
404
 * So the post-IDCT limiting table ends up looking like this:
405
 *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
406
 *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
407
 *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
408
 *   0,1,...,CENTERJSAMPLE-1
409
 * Negative inputs select values from the upper half of the table after
410
 * masking.
411
 *
412
 * We can save some space by overlapping the start of the post-IDCT table
413
 * with the simpler range limiting table.  The post-IDCT table begins at
414
 * sample_range_limit + CENTERJSAMPLE.
415
 */
416
417
LOCAL(void)
418
prepare_range_limit_table(j_decompress_ptr cinfo)
419
/* Allocate and fill in the sample_range_limit table */
420
30.3k
{
421
30.3k
  JSAMPLE *table;
422
30.3k
  J12SAMPLE *table12;
423
30.3k
#ifdef D_LOSSLESS_SUPPORTED
424
30.3k
  J16SAMPLE *table16;
425
30.3k
#endif
426
30.3k
  int i;
427
428
30.3k
  if (cinfo->data_precision <= 8) {
429
15.8k
    table = (JSAMPLE *)
430
15.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
431
15.8k
                  (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
432
15.8k
    table += (MAXJSAMPLE + 1);  /* allow negative subscripts of simple table */
433
15.8k
    cinfo->sample_range_limit = table;
434
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
435
15.8k
    memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
436
    /* Main part of "simple" table: limit[x] = x */
437
4.06M
    for (i = 0; i <= MAXJSAMPLE; i++)
438
4.04M
      table[i] = (JSAMPLE)i;
439
15.8k
    table += CENTERJSAMPLE;     /* Point to where post-IDCT table starts */
440
    /* End of simple table, rest of first half of post-IDCT table */
441
6.08M
    for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
442
6.06M
      table[i] = MAXJSAMPLE;
443
    /* Second half of post-IDCT table */
444
15.8k
    memset(table + (2 * (MAXJSAMPLE + 1)), 0,
445
15.8k
           (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
446
15.8k
    memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
447
15.8k
           cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
448
15.8k
  } else if (cinfo->data_precision <= 12) {
449
11.5k
    table12 = (J12SAMPLE *)
450
11.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
451
11.5k
                  (5 * (MAXJ12SAMPLE + 1) + CENTERJ12SAMPLE) *
452
11.5k
                  sizeof(J12SAMPLE));
453
11.5k
    table12 += (MAXJ12SAMPLE + 1);  /* allow negative subscripts of simple
454
                                       table */
455
11.5k
    cinfo->sample_range_limit = (JSAMPLE *)table12;
456
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
457
11.5k
    memset(table12 - (MAXJ12SAMPLE + 1), 0,
458
11.5k
           (MAXJ12SAMPLE + 1) * sizeof(J12SAMPLE));
459
    /* Main part of "simple" table: limit[x] = x */
460
47.3M
    for (i = 0; i <= MAXJ12SAMPLE; i++)
461
47.3M
      table12[i] = (J12SAMPLE)i;
462
11.5k
    table12 += CENTERJ12SAMPLE; /* Point to where post-IDCT table starts */
463
    /* End of simple table, rest of first half of post-IDCT table */
464
71.0M
    for (i = CENTERJ12SAMPLE; i < 2 * (MAXJ12SAMPLE + 1); i++)
465
71.0M
      table12[i] = MAXJ12SAMPLE;
466
    /* Second half of post-IDCT table */
467
11.5k
    memset(table12 + (2 * (MAXJ12SAMPLE + 1)), 0,
468
11.5k
           (2 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE) * sizeof(J12SAMPLE));
469
11.5k
    memcpy(table12 + (4 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE),
470
11.5k
           cinfo->sample_range_limit, CENTERJ12SAMPLE * sizeof(J12SAMPLE));
471
11.5k
  } else {
472
2.93k
#ifdef D_LOSSLESS_SUPPORTED
473
2.93k
    table16 = (J16SAMPLE *)
474
2.93k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
475
2.93k
                  (5 * (MAXJ16SAMPLE + 1) + CENTERJ16SAMPLE) *
476
2.93k
                  sizeof(J16SAMPLE));
477
2.93k
    table16 += (MAXJ16SAMPLE + 1);  /* allow negative subscripts of simple
478
                                       table */
479
2.93k
    cinfo->sample_range_limit = (JSAMPLE *)table16;
480
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
481
2.93k
    memset(table16 - (MAXJ16SAMPLE + 1), 0,
482
2.93k
           (MAXJ16SAMPLE + 1) * sizeof(J16SAMPLE));
483
    /* Main part of "simple" table: limit[x] = x */
484
192M
    for (i = 0; i <= MAXJ16SAMPLE; i++)
485
192M
      table16[i] = (J16SAMPLE)i;
486
2.93k
    table16 += CENTERJ16SAMPLE; /* Point to where post-IDCT table starts */
487
    /* End of simple table, rest of first half of post-IDCT table */
488
288M
    for (i = CENTERJ16SAMPLE; i < 2 * (MAXJ16SAMPLE + 1); i++)
489
288M
      table16[i] = MAXJ16SAMPLE;
490
    /* Second half of post-IDCT table */
491
2.93k
    memset(table16 + (2 * (MAXJ16SAMPLE + 1)), 0,
492
2.93k
           (2 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE) * sizeof(J16SAMPLE));
493
2.93k
    memcpy(table16 + (4 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE),
494
2.93k
           cinfo->sample_range_limit, CENTERJ16SAMPLE * sizeof(J16SAMPLE));
495
#else
496
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
497
#endif
498
2.93k
  }
499
30.3k
}
500
501
502
/*
503
 * Master selection of decompression modules.
504
 * This is done once at jpeg_start_decompress time.  We determine
505
 * which modules will be used and give them appropriate initialization calls.
506
 * We also initialize the decompressor input side to begin consuming data.
507
 *
508
 * Since jpeg_read_header has finished, we know what is in the SOF
509
 * and (first) SOS markers.  We also have all the application parameter
510
 * settings.
511
 */
512
513
LOCAL(void)
514
master_selection(j_decompress_ptr cinfo)
515
30.3k
{
516
30.3k
  my_master_ptr master = (my_master_ptr)cinfo->master;
517
30.3k
  boolean use_c_buffer;
518
30.3k
  long samplesperrow;
519
30.3k
  JDIMENSION jd_samplesperrow;
520
521
  /* When decompressing an 8-bit-per-sample lossy JPEG image, we allow the
522
   * caller to request 12-bit-per-sample output in order to facilitate shadow
523
   * recovery in underexposed images.  However, in all other cases, setting the
524
   * output data precision to a different value than the JPEG data precision
525
   * will produce unexpected results, such as a bogus output image.
526
   */
527
30.3k
  if (cinfo->master->jpeg_data_precision &&
528
30.3k
      cinfo->data_precision != cinfo->master->jpeg_data_precision &&
529
0
      (cinfo->master->lossless || cinfo->data_precision != 12))
530
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
531
532
  /* Disable IDCT scaling and raw (downsampled) data output in lossless mode.
533
   * IDCT scaling is not useful in lossless mode, and it must be disabled in
534
   * order to properly calculate the output dimensions.  Raw data output isn't
535
   * particularly useful without subsampling and has not been tested in
536
   * lossless mode.
537
   */
538
30.3k
#ifdef D_LOSSLESS_SUPPORTED
539
30.3k
  if (cinfo->master->lossless) {
540
7.76k
    cinfo->raw_data_out = FALSE;
541
7.76k
    cinfo->scale_num = cinfo->scale_denom = 1;
542
7.76k
  }
543
30.3k
#endif
544
545
  /* Initialize dimensions and other stuff */
546
30.3k
  jpeg_calc_output_dimensions(cinfo);
547
30.3k
  prepare_range_limit_table(cinfo);
548
549
  /* Width of an output scanline must be representable as JDIMENSION. */
550
30.3k
  samplesperrow = (long)cinfo->output_width *
551
30.3k
                  (long)cinfo->out_color_components;
552
30.3k
  jd_samplesperrow = (JDIMENSION)samplesperrow;
553
30.3k
  if ((long)jd_samplesperrow != samplesperrow)
554
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
555
556
  /* Initialize my private state */
557
30.3k
  master->pass_number = 0;
558
30.3k
  master->using_merged_upsample = use_merged_upsample(cinfo);
559
560
  /* Color quantizer selection */
561
30.3k
  master->quantizer_1pass = NULL;
562
30.3k
  master->quantizer_2pass = NULL;
563
  /* No mode changes if not using buffered-image mode. */
564
30.3k
  if (!cinfo->quantize_colors || !cinfo->buffered_image) {
565
30.3k
    cinfo->enable_1pass_quant = FALSE;
566
30.3k
    cinfo->enable_external_quant = FALSE;
567
30.3k
    cinfo->enable_2pass_quant = FALSE;
568
30.3k
  }
569
30.3k
  if (cinfo->quantize_colors) {
570
0
    if (cinfo->raw_data_out)
571
0
      ERREXIT(cinfo, JERR_NOTIMPL);
572
    /* 2-pass quantizer only works in 3-component color space. */
573
0
    if (cinfo->out_color_components != 3 ||
574
0
        cinfo->out_color_space == JCS_RGB565) {
575
0
      cinfo->enable_1pass_quant = TRUE;
576
0
      cinfo->enable_external_quant = FALSE;
577
0
      cinfo->enable_2pass_quant = FALSE;
578
0
      cinfo->colormap = NULL;
579
0
    } else if (cinfo->colormap != NULL) {
580
0
      cinfo->enable_external_quant = TRUE;
581
0
    } else if (cinfo->two_pass_quantize) {
582
0
      cinfo->enable_2pass_quant = TRUE;
583
0
    } else {
584
0
      cinfo->enable_1pass_quant = TRUE;
585
0
    }
586
587
0
    if (cinfo->enable_1pass_quant) {
588
0
#ifdef QUANT_1PASS_SUPPORTED
589
0
      if (cinfo->data_precision == 8)
590
0
        jinit_1pass_quantizer(cinfo);
591
0
      else if (cinfo->data_precision == 12)
592
0
        j12init_1pass_quantizer(cinfo);
593
0
      else
594
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
595
0
      master->quantizer_1pass = cinfo->cquantize;
596
#else
597
      ERREXIT(cinfo, JERR_NOT_COMPILED);
598
#endif
599
0
    }
600
601
    /* We use the 2-pass code to map to external colormaps. */
602
0
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
603
0
#ifdef QUANT_2PASS_SUPPORTED
604
0
      if (cinfo->data_precision == 8)
605
0
        jinit_2pass_quantizer(cinfo);
606
0
      else if (cinfo->data_precision == 12)
607
0
        j12init_2pass_quantizer(cinfo);
608
0
      else
609
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
610
0
      master->quantizer_2pass = cinfo->cquantize;
611
#else
612
      ERREXIT(cinfo, JERR_NOT_COMPILED);
613
#endif
614
0
    }
615
    /* If both quantizers are initialized, the 2-pass one is left active;
616
     * this is necessary for starting with quantization to an external map.
617
     */
618
0
  }
619
620
  /* Post-processing: in particular, color conversion first */
621
30.3k
  if (!cinfo->raw_data_out) {
622
30.3k
    if (master->using_merged_upsample) {
623
317
#ifdef UPSAMPLE_MERGING_SUPPORTED
624
317
      if (cinfo->data_precision == 8)
625
171
        jinit_merged_upsampler(cinfo); /* does color conversion too */
626
146
      else if (cinfo->data_precision == 12)
627
146
        j12init_merged_upsampler(cinfo); /* does color conversion too */
628
0
      else
629
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
630
#else
631
      ERREXIT(cinfo, JERR_NOT_COMPILED);
632
#endif
633
29.9k
    } else {
634
29.9k
      if (cinfo->data_precision <= 8) {
635
15.6k
        jinit_color_deconverter(cinfo);
636
15.6k
        jinit_upsampler(cinfo);
637
15.6k
      } else if (cinfo->data_precision <= 12) {
638
11.4k
        j12init_color_deconverter(cinfo);
639
11.4k
        j12init_upsampler(cinfo);
640
11.4k
      } else {
641
2.93k
#ifdef D_LOSSLESS_SUPPORTED
642
2.93k
        j16init_color_deconverter(cinfo);
643
2.93k
        j16init_upsampler(cinfo);
644
#else
645
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
646
#endif
647
2.93k
      }
648
29.9k
    }
649
30.3k
    if (cinfo->data_precision <= 8)
650
10.4k
      jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
651
19.8k
    else if (cinfo->data_precision <= 12)
652
7.82k
      j12init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
653
11.9k
    else
654
11.9k
#ifdef D_LOSSLESS_SUPPORTED
655
11.9k
      j16init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
656
#else
657
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
658
#endif
659
30.3k
  }
660
661
30.3k
  if (cinfo->master->lossless) {
662
3.85k
#ifdef D_LOSSLESS_SUPPORTED
663
    /* Prediction, sample undifferencing, point transform, and sample size
664
     * scaling
665
     */
666
3.85k
    if (cinfo->data_precision <= 8)
667
1.19k
      jinit_lossless_decompressor(cinfo);
668
2.66k
    else if (cinfo->data_precision <= 12)
669
1.20k
      j12init_lossless_decompressor(cinfo);
670
1.45k
    else
671
1.45k
      j16init_lossless_decompressor(cinfo);
672
    /* Entropy decoding: either Huffman or arithmetic coding. */
673
3.85k
    if (cinfo->arith_code) {
674
48
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
675
3.80k
    } else {
676
3.80k
      jinit_lhuff_decoder(cinfo);
677
3.80k
    }
678
679
    /* Initialize principal buffer controllers. */
680
3.85k
    use_c_buffer = cinfo->inputctl->has_multiple_scans ||
681
795
                   cinfo->buffered_image;
682
3.85k
    if (cinfo->data_precision <= 8)
683
1.17k
      jinit_d_diff_controller(cinfo, use_c_buffer);
684
2.68k
    else if (cinfo->data_precision <= 12)
685
1.19k
      j12init_d_diff_controller(cinfo, use_c_buffer);
686
1.48k
    else
687
1.48k
      j16init_d_diff_controller(cinfo, use_c_buffer);
688
#else
689
    ERREXIT(cinfo, JERR_NOT_COMPILED);
690
#endif
691
26.4k
  } else {
692
26.4k
#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED) || \
693
26.4k
    defined(DCT_FLOAT_SUPPORTED)
694
    /* Inverse DCT */
695
26.4k
    if (cinfo->data_precision == 8)
696
9.29k
      jinit_inverse_dct(cinfo);
697
17.1k
    else if (cinfo->data_precision == 12)
698
6.61k
      j12init_inverse_dct(cinfo);
699
10.5k
    else
700
10.5k
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
701
    /* Entropy decoding: either Huffman or arithmetic coding. */
702
26.4k
    if (cinfo->arith_code) {
703
9.30k
#ifdef D_ARITH_CODING_SUPPORTED
704
9.30k
      jinit_arith_decoder(cinfo);
705
#else
706
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
707
#endif
708
17.1k
    } else {
709
17.1k
      if (cinfo->progressive_mode) {
710
2.61k
#ifdef D_PROGRESSIVE_SUPPORTED
711
2.61k
        jinit_phuff_decoder(cinfo);
712
#else
713
        ERREXIT(cinfo, JERR_NOT_COMPILED);
714
#endif
715
2.61k
      } else
716
14.5k
        jinit_huff_decoder(cinfo);
717
17.1k
    }
718
719
    /* Initialize principal buffer controllers. */
720
26.4k
    use_c_buffer = cinfo->inputctl->has_multiple_scans ||
721
3.75k
                   cinfo->buffered_image;
722
26.4k
    if (cinfo->data_precision == 12)
723
6.61k
      j12init_d_coef_controller(cinfo, use_c_buffer);
724
19.8k
    else
725
19.8k
      jinit_d_coef_controller(cinfo, use_c_buffer);
726
#else
727
    ERREXIT(cinfo, JERR_NOT_COMPILED);
728
#endif
729
26.4k
  }
730
731
30.3k
  if (!cinfo->raw_data_out) {
732
19.7k
    if (cinfo->data_precision <= 8)
733
10.4k
      jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
734
9.25k
    else if (cinfo->data_precision <= 12)
735
7.81k
      j12init_d_main_controller(cinfo,
736
7.81k
                                FALSE /* never need full buffer here */);
737
1.44k
    else
738
1.44k
#ifdef D_LOSSLESS_SUPPORTED
739
1.44k
      j16init_d_main_controller(cinfo,
740
1.44k
                                FALSE /* never need full buffer here */);
741
#else
742
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
743
#endif
744
19.7k
  }
745
746
  /* We can now tell the memory manager to allocate virtual arrays. */
747
30.3k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
748
749
  /* Initialize input side of decompressor to consume first scan. */
750
30.3k
  (*cinfo->inputctl->start_input_pass) (cinfo);
751
752
  /* Set the first and last iMCU columns to decompress from single-scan images.
753
   * By default, decompress all of the iMCU columns.
754
   */
755
30.3k
  cinfo->master->first_iMCU_col = 0;
756
30.3k
  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
757
30.3k
  cinfo->master->last_good_iMCU_row = 0;
758
759
30.3k
#ifdef D_MULTISCAN_FILES_SUPPORTED
760
  /* If jpeg_start_decompress will read the whole file, initialize
761
   * progress monitoring appropriately.  The input step is counted
762
   * as one pass.
763
   */
764
30.3k
  if (cinfo->progress != NULL && !cinfo->buffered_image &&
765
16.0k
      cinfo->inputctl->has_multiple_scans) {
766
12.0k
    int nscans;
767
    /* Estimate number of scans to set pass_limit. */
768
12.0k
    if (cinfo->progressive_mode) {
769
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
770
6.91k
      nscans = 2 + 3 * cinfo->num_components;
771
6.91k
    } else {
772
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
773
5.11k
      nscans = cinfo->num_components;
774
5.11k
    }
775
12.0k
    cinfo->progress->pass_counter = 0L;
776
12.0k
    cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
777
12.0k
    cinfo->progress->completed_passes = 0;
778
12.0k
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
779
    /* Count the input pass as done */
780
12.0k
    master->pass_number++;
781
12.0k
  }
782
30.3k
#endif /* D_MULTISCAN_FILES_SUPPORTED */
783
30.3k
}
784
785
786
/*
787
 * Per-pass setup.
788
 * This is called at the beginning of each output pass.  We determine which
789
 * modules will be active during this pass and give them appropriate
790
 * start_pass calls.  We also set is_dummy_pass to indicate whether this
791
 * is a "real" output pass or a dummy pass for color quantization.
792
 * (In the latter case, jdapistd.c will crank the pass to completion.)
793
 */
794
795
METHODDEF(void)
796
prepare_for_output_pass(j_decompress_ptr cinfo)
797
14.4k
{
798
14.4k
  my_master_ptr master = (my_master_ptr)cinfo->master;
799
800
14.4k
  if (master->pub.is_dummy_pass) {
801
0
#ifdef QUANT_2PASS_SUPPORTED
802
    /* Final pass of 2-pass quantization */
803
0
    master->pub.is_dummy_pass = FALSE;
804
0
    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
805
0
    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
806
0
    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
807
#else
808
    ERREXIT(cinfo, JERR_NOT_COMPILED);
809
#endif /* QUANT_2PASS_SUPPORTED */
810
14.4k
  } else {
811
14.4k
    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
812
      /* Select new quantization method */
813
0
      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
814
0
        cinfo->cquantize = master->quantizer_2pass;
815
0
        master->pub.is_dummy_pass = TRUE;
816
0
      } else if (cinfo->enable_1pass_quant) {
817
0
        cinfo->cquantize = master->quantizer_1pass;
818
0
      } else {
819
0
        ERREXIT(cinfo, JERR_MODE_CHANGE);
820
0
      }
821
0
    }
822
14.4k
    (*cinfo->idct->start_pass) (cinfo);
823
14.4k
    (*cinfo->coef->start_output_pass) (cinfo);
824
14.4k
    if (!cinfo->raw_data_out) {
825
14.4k
      if (!master->using_merged_upsample)
826
14.2k
        (*cinfo->cconvert->start_pass) (cinfo);
827
14.4k
      (*cinfo->upsample->start_pass) (cinfo);
828
14.4k
      if (cinfo->quantize_colors)
829
0
        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
830
14.4k
      (*cinfo->post->start_pass) (cinfo,
831
14.4k
            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
832
14.4k
      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
833
14.4k
    }
834
14.4k
  }
835
836
  /* Set up progress monitor's pass info if present */
837
14.4k
  if (cinfo->progress != NULL) {
838
14.4k
    cinfo->progress->completed_passes = master->pass_number;
839
14.4k
    cinfo->progress->total_passes = master->pass_number +
840
14.4k
                                    (master->pub.is_dummy_pass ? 2 : 1);
841
    /* In buffered-image mode, we assume one more output pass if EOI not
842
     * yet reached, but no more passes if EOI has been reached.
843
     */
844
14.4k
    if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
845
0
      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
846
0
    }
847
14.4k
  }
848
14.4k
}
849
850
851
/*
852
 * Finish up at end of an output pass.
853
 */
854
855
METHODDEF(void)
856
finish_output_pass(j_decompress_ptr cinfo)
857
12.5k
{
858
12.5k
  my_master_ptr master = (my_master_ptr)cinfo->master;
859
860
12.5k
  if (cinfo->quantize_colors)
861
0
    (*cinfo->cquantize->finish_pass) (cinfo);
862
12.5k
  master->pass_number++;
863
12.5k
}
864
865
866
#ifdef D_MULTISCAN_FILES_SUPPORTED
867
868
/*
869
 * Switch to a new external colormap between output passes.
870
 */
871
872
GLOBAL(void)
873
jpeg_new_colormap(j_decompress_ptr cinfo)
874
0
{
875
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
876
877
  /* Prevent application from calling me at wrong times */
878
0
  if (cinfo->global_state != DSTATE_BUFIMAGE)
879
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
880
881
0
  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
882
0
      cinfo->colormap != NULL) {
883
    /* Select 2-pass quantizer for external colormap use */
884
0
    cinfo->cquantize = master->quantizer_2pass;
885
    /* Notify quantizer of colormap change */
886
0
    (*cinfo->cquantize->new_color_map) (cinfo);
887
0
    master->pub.is_dummy_pass = FALSE; /* just in case */
888
0
  } else
889
0
    ERREXIT(cinfo, JERR_MODE_CHANGE);
890
0
}
891
892
#endif /* D_MULTISCAN_FILES_SUPPORTED */
893
894
895
/*
896
 * Initialize master decompression control and select active modules.
897
 * This is performed at the start of jpeg_start_decompress.
898
 */
899
900
GLOBAL(void)
901
jinit_master_decompress(j_decompress_ptr cinfo)
902
30.3k
{
903
30.3k
  my_master_ptr master = (my_master_ptr)cinfo->master;
904
905
#ifdef WITH_PROFILE
906
  master->pub.total_start = getTime();
907
#endif
908
909
30.3k
  master->pub.prepare_for_output_pass = prepare_for_output_pass;
910
30.3k
  master->pub.finish_output_pass = finish_output_pass;
911
912
30.3k
  master->pub.is_dummy_pass = FALSE;
913
30.3k
  master->pub.jinit_upsampler_no_alloc = FALSE;
914
915
30.3k
  master_selection(cinfo);
916
30.3k
}