Coverage Report

Created: 2024-05-15 07:07

/src/libjpeg-turbo.main/jdmaster.c
Line
Count
Source (jump to first uncovered line)
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
 * libjpeg-turbo Modifications:
8
 * Copyright (C) 2009-2011, 2016, 2019, 2022, D. R. Commander.
9
 * Copyright (C) 2013, Linaro Limited.
10
 * Copyright (C) 2015, Google, Inc.
11
 * For conditions of distribution and use, see the accompanying README.ijg
12
 * file.
13
 *
14
 * This file contains master control logic for the JPEG decompressor.
15
 * These routines are concerned with selecting the modules to be executed
16
 * and with determining the number of passes and the work to be done in each
17
 * pass.
18
 */
19
20
#define JPEG_INTERNALS
21
#include "jinclude.h"
22
#include "jpeglib.h"
23
#include "jpegcomp.h"
24
#include "jdmaster.h"
25
26
27
/*
28
 * Determine whether merged upsample/color conversion should be used.
29
 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
30
 */
31
32
LOCAL(boolean)
33
use_merged_upsample(j_decompress_ptr cinfo)
34
708k
{
35
708k
#ifdef UPSAMPLE_MERGING_SUPPORTED
36
  /* Merging is the equivalent of plain box-filter upsampling */
37
708k
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
38
531k
    return FALSE;
39
  /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
40
177k
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
41
177k
      (cinfo->out_color_space != JCS_RGB &&
42
153k
       cinfo->out_color_space != JCS_RGB565 &&
43
153k
       cinfo->out_color_space != JCS_EXT_RGB &&
44
153k
       cinfo->out_color_space != JCS_EXT_RGBX &&
45
153k
       cinfo->out_color_space != JCS_EXT_BGR &&
46
153k
       cinfo->out_color_space != JCS_EXT_BGRX &&
47
153k
       cinfo->out_color_space != JCS_EXT_XBGR &&
48
153k
       cinfo->out_color_space != JCS_EXT_XRGB &&
49
153k
       cinfo->out_color_space != JCS_EXT_RGBA &&
50
153k
       cinfo->out_color_space != JCS_EXT_BGRA &&
51
153k
       cinfo->out_color_space != JCS_EXT_ABGR &&
52
153k
       cinfo->out_color_space != JCS_EXT_ARGB))
53
24.0k
    return FALSE;
54
153k
  if ((cinfo->out_color_space == JCS_RGB565 &&
55
153k
       cinfo->out_color_components != 3) ||
56
153k
      (cinfo->out_color_space != JCS_RGB565 &&
57
153k
       cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
58
0
    return FALSE;
59
  /* and it only handles 2h1v or 2h2v sampling ratios */
60
153k
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
61
153k
      cinfo->comp_info[1].h_samp_factor != 1 ||
62
153k
      cinfo->comp_info[2].h_samp_factor != 1 ||
63
153k
      cinfo->comp_info[0].v_samp_factor >  2 ||
64
153k
      cinfo->comp_info[1].v_samp_factor != 1 ||
65
153k
      cinfo->comp_info[2].v_samp_factor != 1)
66
122k
    return FALSE;
67
  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
68
30.8k
  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
69
30.8k
      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
70
30.8k
      cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
71
0
    return FALSE;
72
  /* ??? also need to test for upsample-time rescaling, when & if supported */
73
30.8k
  return TRUE;                  /* by golly, it'll work... */
74
#else
75
  return FALSE;
76
#endif
77
30.8k
}
78
79
80
/*
81
 * Compute output image dimensions and related values.
82
 * NOTE: this is exported for possible use by application.
83
 * Hence it mustn't do anything that can't be done twice.
84
 */
85
86
#if JPEG_LIB_VERSION >= 80
87
GLOBAL(void)
88
#else
89
LOCAL(void)
90
#endif
91
jpeg_core_output_dimensions(j_decompress_ptr cinfo)
92
/* Do computations that are needed before master selection phase.
93
 * This function is used for transcoding and full decompression.
94
 */
95
354k
{
96
354k
#ifdef IDCT_SCALING_SUPPORTED
97
354k
  int ci;
98
354k
  jpeg_component_info *compptr;
99
100
  /* Compute actual output image dimensions and DCT scaling choices. */
101
354k
  if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
102
    /* Provide 1/block_size scaling */
103
0
    cinfo->output_width = (JDIMENSION)
104
0
      jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
105
0
    cinfo->output_height = (JDIMENSION)
106
0
      jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
107
0
    cinfo->_min_DCT_h_scaled_size = 1;
108
0
    cinfo->_min_DCT_v_scaled_size = 1;
109
354k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
110
    /* Provide 2/block_size scaling */
111
0
    cinfo->output_width = (JDIMENSION)
112
0
      jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
113
0
    cinfo->output_height = (JDIMENSION)
114
0
      jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
115
0
    cinfo->_min_DCT_h_scaled_size = 2;
116
0
    cinfo->_min_DCT_v_scaled_size = 2;
117
354k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
118
    /* Provide 3/block_size scaling */
119
0
    cinfo->output_width = (JDIMENSION)
120
0
      jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
121
0
    cinfo->output_height = (JDIMENSION)
122
0
      jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
123
0
    cinfo->_min_DCT_h_scaled_size = 3;
124
0
    cinfo->_min_DCT_v_scaled_size = 3;
125
354k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
126
    /* Provide 4/block_size scaling */
127
87.9k
    cinfo->output_width = (JDIMENSION)
128
87.9k
      jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
129
87.9k
    cinfo->output_height = (JDIMENSION)
130
87.9k
      jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
131
87.9k
    cinfo->_min_DCT_h_scaled_size = 4;
132
87.9k
    cinfo->_min_DCT_v_scaled_size = 4;
133
266k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
134
    /* Provide 5/block_size scaling */
135
371
    cinfo->output_width = (JDIMENSION)
136
371
      jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
137
371
    cinfo->output_height = (JDIMENSION)
138
371
      jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
139
371
    cinfo->_min_DCT_h_scaled_size = 5;
140
371
    cinfo->_min_DCT_v_scaled_size = 5;
141
265k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
142
    /* Provide 6/block_size scaling */
143
0
    cinfo->output_width = (JDIMENSION)
144
0
      jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
145
0
    cinfo->output_height = (JDIMENSION)
146
0
      jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
147
0
    cinfo->_min_DCT_h_scaled_size = 6;
148
0
    cinfo->_min_DCT_v_scaled_size = 6;
149
265k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
150
    /* Provide 7/block_size scaling */
151
0
    cinfo->output_width = (JDIMENSION)
152
0
      jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
153
0
    cinfo->output_height = (JDIMENSION)
154
0
      jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
155
0
    cinfo->_min_DCT_h_scaled_size = 7;
156
0
    cinfo->_min_DCT_v_scaled_size = 7;
157
265k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
158
    /* Provide 8/block_size scaling */
159
265k
    cinfo->output_width = (JDIMENSION)
160
265k
      jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
161
265k
    cinfo->output_height = (JDIMENSION)
162
265k
      jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
163
265k
    cinfo->_min_DCT_h_scaled_size = 8;
164
265k
    cinfo->_min_DCT_v_scaled_size = 8;
165
265k
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
166
    /* Provide 9/block_size scaling */
167
0
    cinfo->output_width = (JDIMENSION)
168
0
      jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
169
0
    cinfo->output_height = (JDIMENSION)
170
0
      jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
171
0
    cinfo->_min_DCT_h_scaled_size = 9;
172
0
    cinfo->_min_DCT_v_scaled_size = 9;
173
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
174
    /* Provide 10/block_size scaling */
175
0
    cinfo->output_width = (JDIMENSION)
176
0
      jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
177
0
    cinfo->output_height = (JDIMENSION)
178
0
      jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
179
0
    cinfo->_min_DCT_h_scaled_size = 10;
180
0
    cinfo->_min_DCT_v_scaled_size = 10;
181
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
182
    /* Provide 11/block_size scaling */
183
0
    cinfo->output_width = (JDIMENSION)
184
0
      jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
185
0
    cinfo->output_height = (JDIMENSION)
186
0
      jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
187
0
    cinfo->_min_DCT_h_scaled_size = 11;
188
0
    cinfo->_min_DCT_v_scaled_size = 11;
189
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
190
    /* Provide 12/block_size scaling */
191
0
    cinfo->output_width = (JDIMENSION)
192
0
      jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
193
0
    cinfo->output_height = (JDIMENSION)
194
0
      jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
195
0
    cinfo->_min_DCT_h_scaled_size = 12;
196
0
    cinfo->_min_DCT_v_scaled_size = 12;
197
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
198
    /* Provide 13/block_size scaling */
199
0
    cinfo->output_width = (JDIMENSION)
200
0
      jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
201
0
    cinfo->output_height = (JDIMENSION)
202
0
      jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
203
0
    cinfo->_min_DCT_h_scaled_size = 13;
204
0
    cinfo->_min_DCT_v_scaled_size = 13;
205
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
206
    /* Provide 14/block_size scaling */
207
0
    cinfo->output_width = (JDIMENSION)
208
0
      jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
209
0
    cinfo->output_height = (JDIMENSION)
210
0
      jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
211
0
    cinfo->_min_DCT_h_scaled_size = 14;
212
0
    cinfo->_min_DCT_v_scaled_size = 14;
213
0
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
214
    /* Provide 15/block_size scaling */
215
0
    cinfo->output_width = (JDIMENSION)
216
0
      jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
217
0
    cinfo->output_height = (JDIMENSION)
218
0
      jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
219
0
    cinfo->_min_DCT_h_scaled_size = 15;
220
0
    cinfo->_min_DCT_v_scaled_size = 15;
221
0
  } else {
222
    /* Provide 16/block_size scaling */
223
0
    cinfo->output_width = (JDIMENSION)
224
0
      jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
225
0
    cinfo->output_height = (JDIMENSION)
226
0
      jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
227
0
    cinfo->_min_DCT_h_scaled_size = 16;
228
0
    cinfo->_min_DCT_v_scaled_size = 16;
229
0
  }
230
231
  /* Recompute dimensions of components */
232
1.45M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
233
1.09M
       ci++, compptr++) {
234
1.09M
    compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
235
1.09M
    compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
236
1.09M
  }
237
238
#else /* !IDCT_SCALING_SUPPORTED */
239
240
  /* Hardwire it to "no scaling" */
241
  cinfo->output_width = cinfo->image_width;
242
  cinfo->output_height = cinfo->image_height;
243
  /* jdinput.c has already initialized DCT_scaled_size,
244
   * and has computed unscaled downsampled_width and downsampled_height.
245
   */
246
247
#endif /* IDCT_SCALING_SUPPORTED */
248
354k
}
249
250
251
/*
252
 * Compute output image dimensions and related values.
253
 * NOTE: this is exported for possible use by application.
254
 * Hence it mustn't do anything that can't be done twice.
255
 * Also note that it may be called before the master module is initialized!
256
 */
257
258
GLOBAL(void)
259
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
260
/* Do computations that are needed before master selection phase */
261
354k
{
262
354k
#ifdef IDCT_SCALING_SUPPORTED
263
354k
  int ci;
264
354k
  jpeg_component_info *compptr;
265
354k
#endif
266
267
  /* Prevent application from calling me at wrong times */
268
354k
  if (cinfo->global_state != DSTATE_READY)
269
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
270
271
  /* Compute core output image dimensions and DCT scaling choices. */
272
354k
  jpeg_core_output_dimensions(cinfo);
273
274
354k
#ifdef IDCT_SCALING_SUPPORTED
275
276
  /* In selecting the actual DCT scaling for each component, we try to
277
   * scale up the chroma components via IDCT scaling rather than upsampling.
278
   * This saves time if the upsampler gets to use 1:1 scaling.
279
   * Note this code adapts subsampling ratios which are powers of 2.
280
   */
281
1.45M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
282
1.09M
       ci++, compptr++) {
283
1.09M
    int ssize = cinfo->_min_DCT_scaled_size;
284
1.17M
    while (ssize < DCTSIZE &&
285
1.17M
           ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
286
273k
            (compptr->h_samp_factor * ssize * 2) == 0) &&
287
1.17M
           ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
288
135k
            (compptr->v_samp_factor * ssize * 2) == 0)) {
289
78.0k
      ssize = ssize * 2;
290
78.0k
    }
291
#if JPEG_LIB_VERSION >= 70
292
    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
293
#else
294
1.09M
    compptr->DCT_scaled_size = ssize;
295
1.09M
#endif
296
1.09M
  }
297
298
  /* Recompute downsampled dimensions of components;
299
   * application needs to know these if using raw downsampled data.
300
   */
301
1.45M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
302
1.09M
       ci++, compptr++) {
303
    /* Size in samples, after IDCT scaling */
304
1.09M
    compptr->downsampled_width = (JDIMENSION)
305
1.09M
      jdiv_round_up((long)cinfo->image_width *
306
1.09M
                    (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size),
307
1.09M
                    (long)(cinfo->max_h_samp_factor * DCTSIZE));
308
1.09M
    compptr->downsampled_height = (JDIMENSION)
309
1.09M
      jdiv_round_up((long)cinfo->image_height *
310
1.09M
                    (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size),
311
1.09M
                    (long)(cinfo->max_v_samp_factor * DCTSIZE));
312
1.09M
  }
313
314
#else /* !IDCT_SCALING_SUPPORTED */
315
316
  /* Hardwire it to "no scaling" */
317
  cinfo->output_width = cinfo->image_width;
318
  cinfo->output_height = cinfo->image_height;
319
  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
320
   * and has computed unscaled downsampled_width and downsampled_height.
321
   */
322
323
#endif /* IDCT_SCALING_SUPPORTED */
324
325
  /* Report number of components in selected colorspace. */
326
  /* Probably this should be in the color conversion module... */
327
354k
  switch (cinfo->out_color_space) {
328
88.5k
  case JCS_GRAYSCALE:
329
88.5k
    cinfo->out_color_components = 1;
330
88.5k
    break;
331
0
  case JCS_RGB:
332
88.5k
  case JCS_EXT_RGB:
333
88.5k
  case JCS_EXT_RGBX:
334
88.5k
  case JCS_EXT_BGR:
335
177k
  case JCS_EXT_BGRX:
336
177k
  case JCS_EXT_XBGR:
337
177k
  case JCS_EXT_XRGB:
338
177k
  case JCS_EXT_RGBA:
339
177k
  case JCS_EXT_BGRA:
340
177k
  case JCS_EXT_ABGR:
341
177k
  case JCS_EXT_ARGB:
342
177k
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
343
177k
    break;
344
0
  case JCS_YCbCr:
345
0
  case JCS_RGB565:
346
0
    cinfo->out_color_components = 3;
347
0
    break;
348
88.5k
  case JCS_CMYK:
349
88.5k
  case JCS_YCCK:
350
88.5k
    cinfo->out_color_components = 4;
351
88.5k
    break;
352
0
  default:                      /* else must be same colorspace as in file */
353
0
    cinfo->out_color_components = cinfo->num_components;
354
0
    break;
355
354k
  }
356
354k
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
357
354k
                              cinfo->out_color_components);
358
359
  /* See if upsampler will want to emit more than one row at a time */
360
354k
  if (use_merged_upsample(cinfo))
361
15.4k
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
362
338k
  else
363
338k
    cinfo->rec_outbuf_height = 1;
364
354k
}
365
366
367
/*
368
 * Several decompression processes need to range-limit values to the range
369
 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
370
 * due to noise introduced by quantization, roundoff error, etc.  These
371
 * processes are inner loops and need to be as fast as possible.  On most
372
 * machines, particularly CPUs with pipelines or instruction prefetch,
373
 * a (subscript-check-less) C table lookup
374
 *              x = sample_range_limit[x];
375
 * is faster than explicit tests
376
 *              if (x < 0)  x = 0;
377
 *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
378
 * These processes all use a common table prepared by the routine below.
379
 *
380
 * For most steps we can mathematically guarantee that the initial value
381
 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
382
 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
383
 * limiting step (just after the IDCT), a wildly out-of-range value is
384
 * possible if the input data is corrupt.  To avoid any chance of indexing
385
 * off the end of memory and getting a bad-pointer trap, we perform the
386
 * post-IDCT limiting thus:
387
 *              x = range_limit[x & MASK];
388
 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
389
 * samples.  Under normal circumstances this is more than enough range and
390
 * a correct output will be generated; with bogus input data the mask will
391
 * cause wraparound, and we will safely generate a bogus-but-in-range output.
392
 * For the post-IDCT step, we want to convert the data from signed to unsigned
393
 * representation by adding CENTERJSAMPLE at the same time that we limit it.
394
 * So the post-IDCT limiting table ends up looking like this:
395
 *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
396
 *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
397
 *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
398
 *   0,1,...,CENTERJSAMPLE-1
399
 * Negative inputs select values from the upper half of the table after
400
 * masking.
401
 *
402
 * We can save some space by overlapping the start of the post-IDCT table
403
 * with the simpler range limiting table.  The post-IDCT table begins at
404
 * sample_range_limit + CENTERJSAMPLE.
405
 */
406
407
LOCAL(void)
408
prepare_range_limit_table(j_decompress_ptr cinfo)
409
/* Allocate and fill in the sample_range_limit table */
410
354k
{
411
354k
  JSAMPLE *table;
412
354k
  int i;
413
414
354k
  table = (JSAMPLE *)
415
354k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
416
354k
                (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
417
354k
  table += (MAXJSAMPLE + 1);    /* allow negative subscripts of simple table */
418
354k
  cinfo->sample_range_limit = table;
419
  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
420
354k
  memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
421
  /* Main part of "simple" table: limit[x] = x */
422
91.0M
  for (i = 0; i <= MAXJSAMPLE; i++)
423
90.6M
    table[i] = (JSAMPLE)i;
424
354k
  table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
425
  /* End of simple table, rest of first half of post-IDCT table */
426
136M
  for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
427
136M
    table[i] = MAXJSAMPLE;
428
  /* Second half of post-IDCT table */
429
354k
  memset(table + (2 * (MAXJSAMPLE + 1)), 0,
430
354k
         (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
431
354k
  memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
432
354k
         cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
433
354k
}
434
435
436
/*
437
 * Master selection of decompression modules.
438
 * This is done once at jpeg_start_decompress time.  We determine
439
 * which modules will be used and give them appropriate initialization calls.
440
 * We also initialize the decompressor input side to begin consuming data.
441
 *
442
 * Since jpeg_read_header has finished, we know what is in the SOF
443
 * and (first) SOS markers.  We also have all the application parameter
444
 * settings.
445
 */
446
447
LOCAL(void)
448
master_selection(j_decompress_ptr cinfo)
449
354k
{
450
354k
  my_master_ptr master = (my_master_ptr)cinfo->master;
451
354k
  boolean use_c_buffer;
452
354k
  long samplesperrow;
453
354k
  JDIMENSION jd_samplesperrow;
454
455
  /* Initialize dimensions and other stuff */
456
354k
  jpeg_calc_output_dimensions(cinfo);
457
354k
  prepare_range_limit_table(cinfo);
458
459
  /* Width of an output scanline must be representable as JDIMENSION. */
460
354k
  samplesperrow = (long)cinfo->output_width *
461
354k
                  (long)cinfo->out_color_components;
462
354k
  jd_samplesperrow = (JDIMENSION)samplesperrow;
463
354k
  if ((long)jd_samplesperrow != samplesperrow)
464
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
465
466
  /* Initialize my private state */
467
354k
  master->pass_number = 0;
468
354k
  master->using_merged_upsample = use_merged_upsample(cinfo);
469
470
  /* Color quantizer selection */
471
354k
  master->quantizer_1pass = NULL;
472
354k
  master->quantizer_2pass = NULL;
473
  /* No mode changes if not using buffered-image mode. */
474
354k
  if (!cinfo->quantize_colors || !cinfo->buffered_image) {
475
354k
    cinfo->enable_1pass_quant = FALSE;
476
354k
    cinfo->enable_external_quant = FALSE;
477
354k
    cinfo->enable_2pass_quant = FALSE;
478
354k
  }
479
354k
  if (cinfo->quantize_colors) {
480
0
    if (cinfo->raw_data_out)
481
0
      ERREXIT(cinfo, JERR_NOTIMPL);
482
    /* 2-pass quantizer only works in 3-component color space. */
483
0
    if (cinfo->out_color_components != 3) {
484
0
      cinfo->enable_1pass_quant = TRUE;
485
0
      cinfo->enable_external_quant = FALSE;
486
0
      cinfo->enable_2pass_quant = FALSE;
487
0
      cinfo->colormap = NULL;
488
0
    } else if (cinfo->colormap != NULL) {
489
0
      cinfo->enable_external_quant = TRUE;
490
0
    } else if (cinfo->two_pass_quantize) {
491
0
      cinfo->enable_2pass_quant = TRUE;
492
0
    } else {
493
0
      cinfo->enable_1pass_quant = TRUE;
494
0
    }
495
496
0
    if (cinfo->enable_1pass_quant) {
497
0
#ifdef QUANT_1PASS_SUPPORTED
498
0
      jinit_1pass_quantizer(cinfo);
499
0
      master->quantizer_1pass = cinfo->cquantize;
500
#else
501
      ERREXIT(cinfo, JERR_NOT_COMPILED);
502
#endif
503
0
    }
504
505
    /* We use the 2-pass code to map to external colormaps. */
506
0
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
507
0
#ifdef QUANT_2PASS_SUPPORTED
508
0
      jinit_2pass_quantizer(cinfo);
509
0
      master->quantizer_2pass = cinfo->cquantize;
510
#else
511
      ERREXIT(cinfo, JERR_NOT_COMPILED);
512
#endif
513
0
    }
514
    /* If both quantizers are initialized, the 2-pass one is left active;
515
     * this is necessary for starting with quantization to an external map.
516
     */
517
0
  }
518
519
  /* Post-processing: in particular, color conversion first */
520
354k
  if (!cinfo->raw_data_out) {
521
354k
    if (master->using_merged_upsample) {
522
15.4k
#ifdef UPSAMPLE_MERGING_SUPPORTED
523
15.4k
      jinit_merged_upsampler(cinfo); /* does color conversion too */
524
#else
525
      ERREXIT(cinfo, JERR_NOT_COMPILED);
526
#endif
527
338k
    } else {
528
338k
      jinit_color_deconverter(cinfo);
529
338k
      jinit_upsampler(cinfo);
530
338k
    }
531
354k
    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
532
354k
  }
533
  /* Inverse DCT */
534
354k
  jinit_inverse_dct(cinfo);
535
  /* Entropy decoding: either Huffman or arithmetic coding. */
536
354k
  if (cinfo->arith_code) {
537
77.6k
#ifdef D_ARITH_CODING_SUPPORTED
538
77.6k
    jinit_arith_decoder(cinfo);
539
#else
540
    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
541
#endif
542
276k
  } else {
543
276k
    if (cinfo->progressive_mode) {
544
63.6k
#ifdef D_PROGRESSIVE_SUPPORTED
545
63.6k
      jinit_phuff_decoder(cinfo);
546
#else
547
      ERREXIT(cinfo, JERR_NOT_COMPILED);
548
#endif
549
63.6k
    } else
550
212k
      jinit_huff_decoder(cinfo);
551
276k
  }
552
553
  /* Initialize principal buffer controllers. */
554
354k
  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
555
354k
  jinit_d_coef_controller(cinfo, use_c_buffer);
556
557
354k
  if (!cinfo->raw_data_out)
558
238k
    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
559
560
  /* We can now tell the memory manager to allocate virtual arrays. */
561
354k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
562
563
  /* Initialize input side of decompressor to consume first scan. */
564
354k
  (*cinfo->inputctl->start_input_pass) (cinfo);
565
566
  /* Set the first and last iMCU columns to decompress from single-scan images.
567
   * By default, decompress all of the iMCU columns.
568
   */
569
354k
  cinfo->master->first_iMCU_col = 0;
570
354k
  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
571
354k
  cinfo->master->last_good_iMCU_row = 0;
572
573
354k
#ifdef D_MULTISCAN_FILES_SUPPORTED
574
  /* If jpeg_start_decompress will read the whole file, initialize
575
   * progress monitoring appropriately.  The input step is counted
576
   * as one pass.
577
   */
578
354k
  if (cinfo->progress != NULL && !cinfo->buffered_image &&
579
354k
      cinfo->inputctl->has_multiple_scans) {
580
182k
    int nscans;
581
    /* Estimate number of scans to set pass_limit. */
582
182k
    if (cinfo->progressive_mode) {
583
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
584
110k
      nscans = 2 + 3 * cinfo->num_components;
585
110k
    } else {
586
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
587
72.8k
      nscans = cinfo->num_components;
588
72.8k
    }
589
182k
    cinfo->progress->pass_counter = 0L;
590
182k
    cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
591
182k
    cinfo->progress->completed_passes = 0;
592
182k
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
593
    /* Count the input pass as done */
594
182k
    master->pass_number++;
595
182k
  }
596
354k
#endif /* D_MULTISCAN_FILES_SUPPORTED */
597
354k
}
598
599
600
/*
601
 * Per-pass setup.
602
 * This is called at the beginning of each output pass.  We determine which
603
 * modules will be active during this pass and give them appropriate
604
 * start_pass calls.  We also set is_dummy_pass to indicate whether this
605
 * is a "real" output pass or a dummy pass for color quantization.
606
 * (In the latter case, jdapistd.c will crank the pass to completion.)
607
 */
608
609
METHODDEF(void)
610
prepare_for_output_pass(j_decompress_ptr cinfo)
611
110k
{
612
110k
  my_master_ptr master = (my_master_ptr)cinfo->master;
613
614
110k
  if (master->pub.is_dummy_pass) {
615
0
#ifdef QUANT_2PASS_SUPPORTED
616
    /* Final pass of 2-pass quantization */
617
0
    master->pub.is_dummy_pass = FALSE;
618
0
    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
619
0
    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
620
0
    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
621
#else
622
    ERREXIT(cinfo, JERR_NOT_COMPILED);
623
#endif /* QUANT_2PASS_SUPPORTED */
624
110k
  } else {
625
110k
    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
626
      /* Select new quantization method */
627
0
      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
628
0
        cinfo->cquantize = master->quantizer_2pass;
629
0
        master->pub.is_dummy_pass = TRUE;
630
0
      } else if (cinfo->enable_1pass_quant) {
631
0
        cinfo->cquantize = master->quantizer_1pass;
632
0
      } else {
633
0
        ERREXIT(cinfo, JERR_MODE_CHANGE);
634
0
      }
635
0
    }
636
110k
    (*cinfo->idct->start_pass) (cinfo);
637
110k
    (*cinfo->coef->start_output_pass) (cinfo);
638
110k
    if (!cinfo->raw_data_out) {
639
110k
      if (!master->using_merged_upsample)
640
99.9k
        (*cinfo->cconvert->start_pass) (cinfo);
641
110k
      (*cinfo->upsample->start_pass) (cinfo);
642
110k
      if (cinfo->quantize_colors)
643
0
        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
644
110k
      (*cinfo->post->start_pass) (cinfo,
645
110k
            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
646
110k
      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
647
110k
    }
648
110k
  }
649
650
  /* Set up progress monitor's pass info if present */
651
110k
  if (cinfo->progress != NULL) {
652
110k
    cinfo->progress->completed_passes = master->pass_number;
653
110k
    cinfo->progress->total_passes = master->pass_number +
654
110k
                                    (master->pub.is_dummy_pass ? 2 : 1);
655
    /* In buffered-image mode, we assume one more output pass if EOI not
656
     * yet reached, but no more passes if EOI has been reached.
657
     */
658
110k
    if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
659
0
      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
660
0
    }
661
110k
  }
662
110k
}
663
664
665
/*
666
 * Finish up at end of an output pass.
667
 */
668
669
METHODDEF(void)
670
finish_output_pass(j_decompress_ptr cinfo)
671
110k
{
672
110k
  my_master_ptr master = (my_master_ptr)cinfo->master;
673
674
110k
  if (cinfo->quantize_colors)
675
0
    (*cinfo->cquantize->finish_pass) (cinfo);
676
110k
  master->pass_number++;
677
110k
}
678
679
680
#ifdef D_MULTISCAN_FILES_SUPPORTED
681
682
/*
683
 * Switch to a new external colormap between output passes.
684
 */
685
686
GLOBAL(void)
687
jpeg_new_colormap(j_decompress_ptr cinfo)
688
0
{
689
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
690
691
  /* Prevent application from calling me at wrong times */
692
0
  if (cinfo->global_state != DSTATE_BUFIMAGE)
693
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
694
695
0
  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
696
0
      cinfo->colormap != NULL) {
697
    /* Select 2-pass quantizer for external colormap use */
698
0
    cinfo->cquantize = master->quantizer_2pass;
699
    /* Notify quantizer of colormap change */
700
0
    (*cinfo->cquantize->new_color_map) (cinfo);
701
0
    master->pub.is_dummy_pass = FALSE; /* just in case */
702
0
  } else
703
0
    ERREXIT(cinfo, JERR_MODE_CHANGE);
704
0
}
705
706
#endif /* D_MULTISCAN_FILES_SUPPORTED */
707
708
709
/*
710
 * Initialize master decompression control and select active modules.
711
 * This is performed at the start of jpeg_start_decompress.
712
 */
713
714
GLOBAL(void)
715
jinit_master_decompress(j_decompress_ptr cinfo)
716
354k
{
717
354k
  my_master_ptr master = (my_master_ptr)cinfo->master;
718
719
354k
  master->pub.prepare_for_output_pass = prepare_for_output_pass;
720
354k
  master->pub.finish_output_pass = finish_output_pass;
721
722
354k
  master->pub.is_dummy_pass = FALSE;
723
354k
  master->pub.jinit_upsampler_no_alloc = FALSE;
724
725
354k
  master_selection(cinfo);
726
354k
}