Coverage Report

Created: 2026-03-12 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/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, 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
0
{
40
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
41
  /* Colorspace conversion is not supported with lossless JPEG images */
42
0
  if (cinfo->master->lossless)
43
0
    return FALSE;
44
  /* Merging is the equivalent of plain box-filter upsampling */
45
0
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
46
0
    return FALSE;
47
  /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
48
0
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
49
0
      (cinfo->out_color_space != JCS_RGB &&
50
0
       cinfo->out_color_space != JCS_RGB565 &&
51
0
       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
0
    return FALSE;
62
0
  if ((cinfo->out_color_space == JCS_RGB565 &&
63
0
       cinfo->out_color_components != 3) ||
64
0
      (cinfo->out_color_space != JCS_RGB565 &&
65
0
       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
0
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
69
0
      cinfo->comp_info[1].h_samp_factor != 1 ||
70
0
      cinfo->comp_info[2].h_samp_factor != 1 ||
71
0
      cinfo->comp_info[0].v_samp_factor >  2 ||
72
0
      cinfo->comp_info[1].v_samp_factor != 1 ||
73
0
      cinfo->comp_info[2].v_samp_factor != 1)
74
0
    return FALSE;
75
  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
76
0
  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
77
0
      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
78
0
      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
0
  return TRUE;                  /* by golly, it'll work... */
82
#else
83
  return FALSE;
84
#endif
85
0
}
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
0
{
104
0
#ifdef IDCT_SCALING_SUPPORTED
105
0
  int ci;
106
0
  jpeg_component_info *compptr;
107
108
0
  if (!cinfo->master->lossless) {
109
    /* Compute actual output image dimensions and DCT scaling choices. */
110
0
    if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
111
      /* Provide 1/block_size scaling */
112
0
      cinfo->output_width = (JDIMENSION)
113
0
        jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
114
0
      cinfo->output_height = (JDIMENSION)
115
0
        jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
116
0
      cinfo->_min_DCT_h_scaled_size = 1;
117
0
      cinfo->_min_DCT_v_scaled_size = 1;
118
0
    } 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
0
    } 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
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
135
      /* Provide 4/block_size scaling */
136
0
      cinfo->output_width = (JDIMENSION)
137
0
        jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
138
0
      cinfo->output_height = (JDIMENSION)
139
0
        jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
140
0
      cinfo->_min_DCT_h_scaled_size = 4;
141
0
      cinfo->_min_DCT_v_scaled_size = 4;
142
0
    } 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
0
    } 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
0
    } 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
0
    } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
167
      /* Provide 8/block_size scaling */
168
0
      cinfo->output_width = (JDIMENSION)
169
0
        jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
170
0
      cinfo->output_height = (JDIMENSION)
171
0
        jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
172
0
      cinfo->_min_DCT_h_scaled_size = 8;
173
0
      cinfo->_min_DCT_v_scaled_size = 8;
174
0
    } 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
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
242
0
         ci++, compptr++) {
243
0
      compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
244
0
      compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
245
0
    }
246
0
  } else
247
0
#endif /* !IDCT_SCALING_SUPPORTED */
248
0
  {
249
    /* Hardwire it to "no scaling" */
250
0
    cinfo->output_width = cinfo->image_width;
251
0
    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
0
  }
256
0
}
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
0
{
270
0
#ifdef IDCT_SCALING_SUPPORTED
271
0
  int ci;
272
0
  jpeg_component_info *compptr;
273
0
#endif
274
275
  /* Prevent application from calling me at wrong times */
276
0
  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
0
  jpeg_core_output_dimensions(cinfo);
281
282
0
#ifdef IDCT_SCALING_SUPPORTED
283
284
0
  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
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
291
0
         ci++, compptr++) {
292
0
      int ssize = cinfo->_min_DCT_scaled_size;
293
0
      while (ssize < DCTSIZE &&
294
0
             ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
295
0
              (compptr->h_samp_factor * ssize * 2) == 0) &&
296
0
             ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
297
0
              (compptr->v_samp_factor * ssize * 2) == 0)) {
298
0
        ssize = ssize * 2;
299
0
      }
300
#if JPEG_LIB_VERSION >= 70
301
      compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
302
#else
303
0
      compptr->DCT_scaled_size = ssize;
304
0
#endif
305
0
    }
306
307
    /* Recompute downsampled dimensions of components;
308
     * application needs to know these if using raw downsampled data.
309
     */
310
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
311
0
         ci++, compptr++) {
312
      /* Size in samples, after IDCT scaling */
313
0
      compptr->downsampled_width = (JDIMENSION)
314
0
        jdiv_round_up((long)cinfo->image_width *
315
0
                      (long)(compptr->h_samp_factor *
316
0
                             compptr->_DCT_scaled_size),
317
0
                      (long)(cinfo->max_h_samp_factor * DCTSIZE));
318
0
      compptr->downsampled_height = (JDIMENSION)
319
0
        jdiv_round_up((long)cinfo->image_height *
320
0
                      (long)(compptr->v_samp_factor *
321
0
                             compptr->_DCT_scaled_size),
322
0
                      (long)(cinfo->max_v_samp_factor * DCTSIZE));
323
0
    }
324
0
  } else
325
0
#endif /* IDCT_SCALING_SUPPORTED */
326
0
  {
327
    /* Hardwire it to "no scaling" */
328
0
    cinfo->output_width = cinfo->image_width;
329
0
    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
0
  }
334
335
  /* Report number of components in selected colorspace. */
336
  /* Probably this should be in the color conversion module... */
337
0
  switch (cinfo->out_color_space) {
338
0
  case JCS_GRAYSCALE:
339
0
    cinfo->out_color_components = 1;
340
0
    break;
341
0
  case JCS_RGB:
342
0
  case JCS_EXT_RGB:
343
0
  case JCS_EXT_RGBX:
344
0
  case JCS_EXT_BGR:
345
0
  case JCS_EXT_BGRX:
346
0
  case JCS_EXT_XBGR:
347
0
  case JCS_EXT_XRGB:
348
0
  case JCS_EXT_RGBA:
349
0
  case JCS_EXT_BGRA:
350
0
  case JCS_EXT_ABGR:
351
0
  case JCS_EXT_ARGB:
352
0
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
353
0
    break;
354
0
  case JCS_YCbCr:
355
0
  case JCS_RGB565:
356
0
    cinfo->out_color_components = 3;
357
0
    break;
358
0
  case JCS_CMYK:
359
0
  case JCS_YCCK:
360
0
    cinfo->out_color_components = 4;
361
0
    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
0
  }
366
0
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
367
0
                              cinfo->out_color_components);
368
369
  /* See if upsampler will want to emit more than one row at a time */
370
0
  if (use_merged_upsample(cinfo))
371
0
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
372
0
  else
373
0
    cinfo->rec_outbuf_height = 1;
374
0
}
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
0
{
421
0
  JSAMPLE *table;
422
0
  J12SAMPLE *table12;
423
0
#ifdef D_LOSSLESS_SUPPORTED
424
0
  J16SAMPLE *table16;
425
0
#endif
426
0
  int i;
427
428
0
  if (cinfo->data_precision <= 8) {
429
0
    table = (JSAMPLE *)
430
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
431
0
                  (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
432
0
    table += (MAXJSAMPLE + 1);  /* allow negative subscripts of simple table */
433
0
    cinfo->sample_range_limit = table;
434
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
435
0
    memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
436
    /* Main part of "simple" table: limit[x] = x */
437
0
    for (i = 0; i <= MAXJSAMPLE; i++)
438
0
      table[i] = (JSAMPLE)i;
439
0
    table += CENTERJSAMPLE;     /* Point to where post-IDCT table starts */
440
    /* End of simple table, rest of first half of post-IDCT table */
441
0
    for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
442
0
      table[i] = MAXJSAMPLE;
443
    /* Second half of post-IDCT table */
444
0
    memset(table + (2 * (MAXJSAMPLE + 1)), 0,
445
0
           (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
446
0
    memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
447
0
           cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
448
0
  } else if (cinfo->data_precision <= 12) {
449
0
    table12 = (J12SAMPLE *)
450
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
451
0
                  (5 * (MAXJ12SAMPLE + 1) + CENTERJ12SAMPLE) *
452
0
                  sizeof(J12SAMPLE));
453
0
    table12 += (MAXJ12SAMPLE + 1);  /* allow negative subscripts of simple
454
                                       table */
455
0
    cinfo->sample_range_limit = (JSAMPLE *)table12;
456
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
457
0
    memset(table12 - (MAXJ12SAMPLE + 1), 0,
458
0
           (MAXJ12SAMPLE + 1) * sizeof(J12SAMPLE));
459
    /* Main part of "simple" table: limit[x] = x */
460
0
    for (i = 0; i <= MAXJ12SAMPLE; i++)
461
0
      table12[i] = (J12SAMPLE)i;
462
0
    table12 += CENTERJ12SAMPLE; /* Point to where post-IDCT table starts */
463
    /* End of simple table, rest of first half of post-IDCT table */
464
0
    for (i = CENTERJ12SAMPLE; i < 2 * (MAXJ12SAMPLE + 1); i++)
465
0
      table12[i] = MAXJ12SAMPLE;
466
    /* Second half of post-IDCT table */
467
0
    memset(table12 + (2 * (MAXJ12SAMPLE + 1)), 0,
468
0
           (2 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE) * sizeof(J12SAMPLE));
469
0
    memcpy(table12 + (4 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE),
470
0
           cinfo->sample_range_limit, CENTERJ12SAMPLE * sizeof(J12SAMPLE));
471
0
  } else {
472
0
#ifdef D_LOSSLESS_SUPPORTED
473
0
    table16 = (J16SAMPLE *)
474
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
475
0
                  (5 * (MAXJ16SAMPLE + 1) + CENTERJ16SAMPLE) *
476
0
                  sizeof(J16SAMPLE));
477
0
    table16 += (MAXJ16SAMPLE + 1);  /* allow negative subscripts of simple
478
                                       table */
479
0
    cinfo->sample_range_limit = (JSAMPLE *)table16;
480
    /* First segment of "simple" table: limit[x] = 0 for x < 0 */
481
0
    memset(table16 - (MAXJ16SAMPLE + 1), 0,
482
0
           (MAXJ16SAMPLE + 1) * sizeof(J16SAMPLE));
483
    /* Main part of "simple" table: limit[x] = x */
484
0
    for (i = 0; i <= MAXJ16SAMPLE; i++)
485
0
      table16[i] = (J16SAMPLE)i;
486
0
    table16 += CENTERJ16SAMPLE; /* Point to where post-IDCT table starts */
487
    /* End of simple table, rest of first half of post-IDCT table */
488
0
    for (i = CENTERJ16SAMPLE; i < 2 * (MAXJ16SAMPLE + 1); i++)
489
0
      table16[i] = MAXJ16SAMPLE;
490
    /* Second half of post-IDCT table */
491
0
    memset(table16 + (2 * (MAXJ16SAMPLE + 1)), 0,
492
0
           (2 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE) * sizeof(J16SAMPLE));
493
0
    memcpy(table16 + (4 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE),
494
0
           cinfo->sample_range_limit, CENTERJ16SAMPLE * sizeof(J16SAMPLE));
495
#else
496
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
497
#endif
498
0
  }
499
0
}
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
0
{
516
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
517
0
  boolean use_c_buffer;
518
0
  long samplesperrow;
519
0
  JDIMENSION jd_samplesperrow;
520
521
  /* Disable IDCT scaling and raw (downsampled) data output in lossless mode.
522
   * IDCT scaling is not useful in lossless mode, and it must be disabled in
523
   * order to properly calculate the output dimensions.  Raw data output isn't
524
   * particularly useful without subsampling and has not been tested in
525
   * lossless mode.
526
   */
527
0
#ifdef D_LOSSLESS_SUPPORTED
528
0
  if (cinfo->master->lossless) {
529
0
    cinfo->raw_data_out = FALSE;
530
0
    cinfo->scale_num = cinfo->scale_denom = 1;
531
0
  }
532
0
#endif
533
534
  /* Initialize dimensions and other stuff */
535
0
  jpeg_calc_output_dimensions(cinfo);
536
0
  prepare_range_limit_table(cinfo);
537
538
  /* Width of an output scanline must be representable as JDIMENSION. */
539
0
  samplesperrow = (long)cinfo->output_width *
540
0
                  (long)cinfo->out_color_components;
541
0
  jd_samplesperrow = (JDIMENSION)samplesperrow;
542
0
  if ((long)jd_samplesperrow != samplesperrow)
543
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
544
545
  /* Initialize my private state */
546
0
  master->pass_number = 0;
547
0
  master->using_merged_upsample = use_merged_upsample(cinfo);
548
549
  /* Color quantizer selection */
550
0
  master->quantizer_1pass = NULL;
551
0
  master->quantizer_2pass = NULL;
552
  /* No mode changes if not using buffered-image mode. */
553
0
  if (!cinfo->quantize_colors || !cinfo->buffered_image) {
554
0
    cinfo->enable_1pass_quant = FALSE;
555
0
    cinfo->enable_external_quant = FALSE;
556
0
    cinfo->enable_2pass_quant = FALSE;
557
0
  }
558
0
  if (cinfo->quantize_colors) {
559
0
    if (cinfo->raw_data_out)
560
0
      ERREXIT(cinfo, JERR_NOTIMPL);
561
    /* 2-pass quantizer only works in 3-component color space. */
562
0
    if (cinfo->out_color_components != 3 ||
563
0
        cinfo->out_color_space == JCS_RGB565) {
564
0
      cinfo->enable_1pass_quant = TRUE;
565
0
      cinfo->enable_external_quant = FALSE;
566
0
      cinfo->enable_2pass_quant = FALSE;
567
0
      cinfo->colormap = NULL;
568
0
    } else if (cinfo->colormap != NULL) {
569
0
      cinfo->enable_external_quant = TRUE;
570
0
    } else if (cinfo->two_pass_quantize) {
571
0
      cinfo->enable_2pass_quant = TRUE;
572
0
    } else {
573
0
      cinfo->enable_1pass_quant = TRUE;
574
0
    }
575
576
0
    if (cinfo->enable_1pass_quant) {
577
0
#ifdef QUANT_1PASS_SUPPORTED
578
0
      if (cinfo->data_precision == 8)
579
0
        jinit_1pass_quantizer(cinfo);
580
0
      else if (cinfo->data_precision == 12)
581
0
        j12init_1pass_quantizer(cinfo);
582
0
      else
583
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
584
0
      master->quantizer_1pass = cinfo->cquantize;
585
#else
586
      ERREXIT(cinfo, JERR_NOT_COMPILED);
587
#endif
588
0
    }
589
590
    /* We use the 2-pass code to map to external colormaps. */
591
0
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
592
0
#ifdef QUANT_2PASS_SUPPORTED
593
0
      if (cinfo->data_precision == 8)
594
0
        jinit_2pass_quantizer(cinfo);
595
0
      else if (cinfo->data_precision == 12)
596
0
        j12init_2pass_quantizer(cinfo);
597
0
      else
598
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
599
0
      master->quantizer_2pass = cinfo->cquantize;
600
#else
601
      ERREXIT(cinfo, JERR_NOT_COMPILED);
602
#endif
603
0
    }
604
    /* If both quantizers are initialized, the 2-pass one is left active;
605
     * this is necessary for starting with quantization to an external map.
606
     */
607
0
  }
608
609
  /* Post-processing: in particular, color conversion first */
610
0
  if (!cinfo->raw_data_out) {
611
0
    if (master->using_merged_upsample) {
612
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
613
0
      if (cinfo->data_precision == 8)
614
0
        jinit_merged_upsampler(cinfo); /* does color conversion too */
615
0
      else if (cinfo->data_precision == 12)
616
0
        j12init_merged_upsampler(cinfo); /* does color conversion too */
617
0
      else
618
0
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
619
#else
620
      ERREXIT(cinfo, JERR_NOT_COMPILED);
621
#endif
622
0
    } else {
623
0
      if (cinfo->data_precision <= 8) {
624
0
        jinit_color_deconverter(cinfo);
625
0
        jinit_upsampler(cinfo);
626
0
      } else if (cinfo->data_precision <= 12) {
627
0
        j12init_color_deconverter(cinfo);
628
0
        j12init_upsampler(cinfo);
629
0
      } else {
630
0
#ifdef D_LOSSLESS_SUPPORTED
631
0
        j16init_color_deconverter(cinfo);
632
0
        j16init_upsampler(cinfo);
633
#else
634
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
635
#endif
636
0
      }
637
0
    }
638
0
    if (cinfo->data_precision <= 8)
639
0
      jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
640
0
    else if (cinfo->data_precision <= 12)
641
0
      j12init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
642
0
    else
643
0
#ifdef D_LOSSLESS_SUPPORTED
644
0
      j16init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
645
#else
646
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
647
#endif
648
0
  }
649
650
0
  if (cinfo->master->lossless) {
651
0
#ifdef D_LOSSLESS_SUPPORTED
652
    /* Prediction, sample undifferencing, point transform, and sample size
653
     * scaling
654
     */
655
0
    if (cinfo->data_precision <= 8)
656
0
      jinit_lossless_decompressor(cinfo);
657
0
    else if (cinfo->data_precision <= 12)
658
0
      j12init_lossless_decompressor(cinfo);
659
0
    else
660
0
      j16init_lossless_decompressor(cinfo);
661
    /* Entropy decoding: either Huffman or arithmetic coding. */
662
0
    if (cinfo->arith_code) {
663
0
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
664
0
    } else {
665
0
      jinit_lhuff_decoder(cinfo);
666
0
    }
667
668
    /* Initialize principal buffer controllers. */
669
0
    use_c_buffer = cinfo->inputctl->has_multiple_scans ||
670
0
                   cinfo->buffered_image;
671
0
    if (cinfo->data_precision <= 8)
672
0
      jinit_d_diff_controller(cinfo, use_c_buffer);
673
0
    else if (cinfo->data_precision <= 12)
674
0
      j12init_d_diff_controller(cinfo, use_c_buffer);
675
0
    else
676
0
      j16init_d_diff_controller(cinfo, use_c_buffer);
677
#else
678
    ERREXIT(cinfo, JERR_NOT_COMPILED);
679
#endif
680
0
  } else {
681
    /* Inverse DCT */
682
0
    if (cinfo->data_precision == 8)
683
0
      jinit_inverse_dct(cinfo);
684
0
    else if (cinfo->data_precision == 12)
685
0
      j12init_inverse_dct(cinfo);
686
0
    else
687
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
688
    /* Entropy decoding: either Huffman or arithmetic coding. */
689
0
    if (cinfo->arith_code) {
690
0
#ifdef D_ARITH_CODING_SUPPORTED
691
0
      jinit_arith_decoder(cinfo);
692
#else
693
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
694
#endif
695
0
    } else {
696
0
      if (cinfo->progressive_mode) {
697
0
#ifdef D_PROGRESSIVE_SUPPORTED
698
0
        jinit_phuff_decoder(cinfo);
699
#else
700
        ERREXIT(cinfo, JERR_NOT_COMPILED);
701
#endif
702
0
      } else
703
0
        jinit_huff_decoder(cinfo);
704
0
    }
705
706
    /* Initialize principal buffer controllers. */
707
0
    use_c_buffer = cinfo->inputctl->has_multiple_scans ||
708
0
                   cinfo->buffered_image;
709
0
    if (cinfo->data_precision == 12)
710
0
      j12init_d_coef_controller(cinfo, use_c_buffer);
711
0
    else
712
0
      jinit_d_coef_controller(cinfo, use_c_buffer);
713
0
  }
714
715
0
  if (!cinfo->raw_data_out) {
716
0
    if (cinfo->data_precision <= 8)
717
0
      jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
718
0
    else if (cinfo->data_precision <= 12)
719
0
      j12init_d_main_controller(cinfo,
720
0
                                FALSE /* never need full buffer here */);
721
0
    else
722
0
#ifdef D_LOSSLESS_SUPPORTED
723
0
      j16init_d_main_controller(cinfo,
724
0
                                FALSE /* never need full buffer here */);
725
#else
726
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
727
#endif
728
0
  }
729
730
  /* We can now tell the memory manager to allocate virtual arrays. */
731
0
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
732
733
  /* Initialize input side of decompressor to consume first scan. */
734
0
  (*cinfo->inputctl->start_input_pass) (cinfo);
735
736
  /* Set the first and last iMCU columns to decompress from single-scan images.
737
   * By default, decompress all of the iMCU columns.
738
   */
739
0
  cinfo->master->first_iMCU_col = 0;
740
0
  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
741
0
  cinfo->master->last_good_iMCU_row = 0;
742
743
0
#ifdef D_MULTISCAN_FILES_SUPPORTED
744
  /* If jpeg_start_decompress will read the whole file, initialize
745
   * progress monitoring appropriately.  The input step is counted
746
   * as one pass.
747
   */
748
0
  if (cinfo->progress != NULL && !cinfo->buffered_image &&
749
0
      cinfo->inputctl->has_multiple_scans) {
750
0
    int nscans;
751
    /* Estimate number of scans to set pass_limit. */
752
0
    if (cinfo->progressive_mode) {
753
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
754
0
      nscans = 2 + 3 * cinfo->num_components;
755
0
    } else {
756
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
757
0
      nscans = cinfo->num_components;
758
0
    }
759
0
    cinfo->progress->pass_counter = 0L;
760
0
    cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
761
0
    cinfo->progress->completed_passes = 0;
762
0
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
763
    /* Count the input pass as done */
764
0
    master->pass_number++;
765
0
  }
766
0
#endif /* D_MULTISCAN_FILES_SUPPORTED */
767
0
}
768
769
770
/*
771
 * Per-pass setup.
772
 * This is called at the beginning of each output pass.  We determine which
773
 * modules will be active during this pass and give them appropriate
774
 * start_pass calls.  We also set is_dummy_pass to indicate whether this
775
 * is a "real" output pass or a dummy pass for color quantization.
776
 * (In the latter case, jdapistd.c will crank the pass to completion.)
777
 */
778
779
METHODDEF(void)
780
prepare_for_output_pass(j_decompress_ptr cinfo)
781
0
{
782
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
783
784
0
  if (master->pub.is_dummy_pass) {
785
0
#ifdef QUANT_2PASS_SUPPORTED
786
    /* Final pass of 2-pass quantization */
787
0
    master->pub.is_dummy_pass = FALSE;
788
0
    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
789
0
    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
790
0
    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
791
#else
792
    ERREXIT(cinfo, JERR_NOT_COMPILED);
793
#endif /* QUANT_2PASS_SUPPORTED */
794
0
  } else {
795
0
    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
796
      /* Select new quantization method */
797
0
      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
798
0
        cinfo->cquantize = master->quantizer_2pass;
799
0
        master->pub.is_dummy_pass = TRUE;
800
0
      } else if (cinfo->enable_1pass_quant) {
801
0
        cinfo->cquantize = master->quantizer_1pass;
802
0
      } else {
803
0
        ERREXIT(cinfo, JERR_MODE_CHANGE);
804
0
      }
805
0
    }
806
0
    (*cinfo->idct->start_pass) (cinfo);
807
0
    (*cinfo->coef->start_output_pass) (cinfo);
808
0
    if (!cinfo->raw_data_out) {
809
0
      if (!master->using_merged_upsample)
810
0
        (*cinfo->cconvert->start_pass) (cinfo);
811
0
      (*cinfo->upsample->start_pass) (cinfo);
812
0
      if (cinfo->quantize_colors)
813
0
        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
814
0
      (*cinfo->post->start_pass) (cinfo,
815
0
            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
816
0
      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
817
0
    }
818
0
  }
819
820
  /* Set up progress monitor's pass info if present */
821
0
  if (cinfo->progress != NULL) {
822
0
    cinfo->progress->completed_passes = master->pass_number;
823
0
    cinfo->progress->total_passes = master->pass_number +
824
0
                                    (master->pub.is_dummy_pass ? 2 : 1);
825
    /* In buffered-image mode, we assume one more output pass if EOI not
826
     * yet reached, but no more passes if EOI has been reached.
827
     */
828
0
    if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
829
0
      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
830
0
    }
831
0
  }
832
0
}
833
834
835
/*
836
 * Finish up at end of an output pass.
837
 */
838
839
METHODDEF(void)
840
finish_output_pass(j_decompress_ptr cinfo)
841
0
{
842
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
843
844
0
  if (cinfo->quantize_colors)
845
0
    (*cinfo->cquantize->finish_pass) (cinfo);
846
0
  master->pass_number++;
847
0
}
848
849
850
#ifdef D_MULTISCAN_FILES_SUPPORTED
851
852
/*
853
 * Switch to a new external colormap between output passes.
854
 */
855
856
GLOBAL(void)
857
jpeg_new_colormap(j_decompress_ptr cinfo)
858
0
{
859
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
860
861
  /* Prevent application from calling me at wrong times */
862
0
  if (cinfo->global_state != DSTATE_BUFIMAGE)
863
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
864
865
0
  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
866
0
      cinfo->colormap != NULL) {
867
    /* Select 2-pass quantizer for external colormap use */
868
0
    cinfo->cquantize = master->quantizer_2pass;
869
    /* Notify quantizer of colormap change */
870
0
    (*cinfo->cquantize->new_color_map) (cinfo);
871
0
    master->pub.is_dummy_pass = FALSE; /* just in case */
872
0
  } else
873
0
    ERREXIT(cinfo, JERR_MODE_CHANGE);
874
0
}
875
876
#endif /* D_MULTISCAN_FILES_SUPPORTED */
877
878
879
/*
880
 * Initialize master decompression control and select active modules.
881
 * This is performed at the start of jpeg_start_decompress.
882
 */
883
884
GLOBAL(void)
885
jinit_master_decompress(j_decompress_ptr cinfo)
886
0
{
887
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
888
889
#ifdef WITH_PROFILE
890
  master->pub.total_start = getTime();
891
#endif
892
893
0
  master->pub.prepare_for_output_pass = prepare_for_output_pass;
894
0
  master->pub.finish_output_pass = finish_output_pass;
895
896
0
  master->pub.is_dummy_pass = FALSE;
897
0
  master->pub.jinit_upsampler_no_alloc = FALSE;
898
899
0
  master_selection(cinfo);
900
0
}