Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/obj/jdinput.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdinput.c
3
 *
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
5
 * Modified 2002-2020 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains input control logic for the JPEG decompressor.
10
 * These routines are concerned with controlling the decompressor's input
11
 * processing (marker reading and coefficient decoding).  The actual input
12
 * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
13
 */
14
15
#define JPEG_INTERNALS
16
#include "jinclude.h"
17
#include "jpeglib.h"
18
19
20
/* Private state */
21
22
typedef struct {
23
  struct jpeg_input_controller pub; /* public fields */
24
25
  int inheaders;    /* Nonzero until first SOS is reached */
26
} my_input_controller;
27
28
typedef my_input_controller * my_inputctl_ptr;
29
30
31
/* Forward declarations */
32
METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
33
34
35
/*
36
 * Routines to calculate various quantities related to the size of the image.
37
 */
38
39
40
/*
41
 * Compute output image dimensions and related values.
42
 * NOTE: this is exported for possible use by application.
43
 * Hence it mustn't do anything that can't be done twice.
44
 */
45
46
GLOBAL(void)
47
jpeg_core_output_dimensions (j_decompress_ptr cinfo)
48
/* Do computations that are needed before master selection phase.
49
 * This function is used for transcoding and full decompression.
50
 */
51
10.0k
{
52
#ifdef IDCT_SCALING_SUPPORTED
53
  int ci;
54
  jpeg_component_info *compptr;
55
56
  /* Compute actual output image dimensions and DCT scaling choices. */
57
  if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
58
    /* Provide 1/block_size scaling */
59
    cinfo->output_width = (JDIMENSION)
60
      jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
61
    cinfo->output_height = (JDIMENSION)
62
      jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
63
    cinfo->min_DCT_h_scaled_size = 1;
64
    cinfo->min_DCT_v_scaled_size = 1;
65
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
66
    /* Provide 2/block_size scaling */
67
    cinfo->output_width = (JDIMENSION)
68
      jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
69
    cinfo->output_height = (JDIMENSION)
70
      jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
71
    cinfo->min_DCT_h_scaled_size = 2;
72
    cinfo->min_DCT_v_scaled_size = 2;
73
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) {
74
    /* Provide 3/block_size scaling */
75
    cinfo->output_width = (JDIMENSION)
76
      jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);
77
    cinfo->output_height = (JDIMENSION)
78
      jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);
79
    cinfo->min_DCT_h_scaled_size = 3;
80
    cinfo->min_DCT_v_scaled_size = 3;
81
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
82
    /* Provide 4/block_size scaling */
83
    cinfo->output_width = (JDIMENSION)
84
      jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
85
    cinfo->output_height = (JDIMENSION)
86
      jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
87
    cinfo->min_DCT_h_scaled_size = 4;
88
    cinfo->min_DCT_v_scaled_size = 4;
89
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) {
90
    /* Provide 5/block_size scaling */
91
    cinfo->output_width = (JDIMENSION)
92
      jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);
93
    cinfo->output_height = (JDIMENSION)
94
      jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);
95
    cinfo->min_DCT_h_scaled_size = 5;
96
    cinfo->min_DCT_v_scaled_size = 5;
97
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) {
98
    /* Provide 6/block_size scaling */
99
    cinfo->output_width = (JDIMENSION)
100
      jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);
101
    cinfo->output_height = (JDIMENSION)
102
      jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);
103
    cinfo->min_DCT_h_scaled_size = 6;
104
    cinfo->min_DCT_v_scaled_size = 6;
105
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) {
106
    /* Provide 7/block_size scaling */
107
    cinfo->output_width = (JDIMENSION)
108
      jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);
109
    cinfo->output_height = (JDIMENSION)
110
      jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);
111
    cinfo->min_DCT_h_scaled_size = 7;
112
    cinfo->min_DCT_v_scaled_size = 7;
113
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
114
    /* Provide 8/block_size scaling */
115
    cinfo->output_width = (JDIMENSION)
116
      jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
117
    cinfo->output_height = (JDIMENSION)
118
      jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
119
    cinfo->min_DCT_h_scaled_size = 8;
120
    cinfo->min_DCT_v_scaled_size = 8;
121
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) {
122
    /* Provide 9/block_size scaling */
123
    cinfo->output_width = (JDIMENSION)
124
      jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);
125
    cinfo->output_height = (JDIMENSION)
126
      jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);
127
    cinfo->min_DCT_h_scaled_size = 9;
128
    cinfo->min_DCT_v_scaled_size = 9;
129
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) {
130
    /* Provide 10/block_size scaling */
131
    cinfo->output_width = (JDIMENSION)
132
      jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);
133
    cinfo->output_height = (JDIMENSION)
134
      jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);
135
    cinfo->min_DCT_h_scaled_size = 10;
136
    cinfo->min_DCT_v_scaled_size = 10;
137
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) {
138
    /* Provide 11/block_size scaling */
139
    cinfo->output_width = (JDIMENSION)
140
      jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);
141
    cinfo->output_height = (JDIMENSION)
142
      jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);
143
    cinfo->min_DCT_h_scaled_size = 11;
144
    cinfo->min_DCT_v_scaled_size = 11;
145
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) {
146
    /* Provide 12/block_size scaling */
147
    cinfo->output_width = (JDIMENSION)
148
      jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);
149
    cinfo->output_height = (JDIMENSION)
150
      jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);
151
    cinfo->min_DCT_h_scaled_size = 12;
152
    cinfo->min_DCT_v_scaled_size = 12;
153
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) {
154
    /* Provide 13/block_size scaling */
155
    cinfo->output_width = (JDIMENSION)
156
      jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);
157
    cinfo->output_height = (JDIMENSION)
158
      jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);
159
    cinfo->min_DCT_h_scaled_size = 13;
160
    cinfo->min_DCT_v_scaled_size = 13;
161
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) {
162
    /* Provide 14/block_size scaling */
163
    cinfo->output_width = (JDIMENSION)
164
      jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);
165
    cinfo->output_height = (JDIMENSION)
166
      jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);
167
    cinfo->min_DCT_h_scaled_size = 14;
168
    cinfo->min_DCT_v_scaled_size = 14;
169
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) {
170
    /* Provide 15/block_size scaling */
171
    cinfo->output_width = (JDIMENSION)
172
      jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);
173
    cinfo->output_height = (JDIMENSION)
174
      jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);
175
    cinfo->min_DCT_h_scaled_size = 15;
176
    cinfo->min_DCT_v_scaled_size = 15;
177
  } else {
178
    /* Provide 16/block_size scaling */
179
    cinfo->output_width = (JDIMENSION)
180
      jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);
181
    cinfo->output_height = (JDIMENSION)
182
      jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);
183
    cinfo->min_DCT_h_scaled_size = 16;
184
    cinfo->min_DCT_v_scaled_size = 16;
185
  }
186
187
  /* Recompute dimensions of components */
188
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
189
       ci++, compptr++) {
190
    compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
191
    compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
192
  }
193
194
#else /* !IDCT_SCALING_SUPPORTED */
195
196
  /* Hardwire it to "no scaling" */
197
10.0k
  cinfo->output_width = cinfo->image_width;
198
10.0k
  cinfo->output_height = cinfo->image_height;
199
  /* initial_setup has already initialized DCT_scaled_size,
200
   * and has computed unscaled downsampled_width and downsampled_height.
201
   */
202
203
10.0k
#endif /* IDCT_SCALING_SUPPORTED */
204
10.0k
}
205
206
207
LOCAL(void)
208
initial_setup (j_decompress_ptr cinfo)
209
/* Called once, when first SOS marker is reached */
210
10.0k
{
211
10.0k
  int ci;
212
10.0k
  jpeg_component_info *compptr;
213
214
  /* Make sure image isn't bigger than I can handle */
215
10.0k
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
216
10.0k
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
217
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
218
219
  /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
220
10.0k
  if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
221
7
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
222
223
  /* Check that number of components won't exceed internal array sizes */
224
10.0k
  if (cinfo->num_components > MAX_COMPONENTS)
225
0
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
226
10.0k
       MAX_COMPONENTS);
227
228
  /* Compute maximum sampling factors; check factor validity */
229
10.0k
  cinfo->max_h_samp_factor = 1;
230
10.0k
  cinfo->max_v_samp_factor = 1;
231
38.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
232
28.5k
       ci++, compptr++) {
233
28.5k
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
234
28.5k
  compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
235
19
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
236
28.5k
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
237
28.5k
           compptr->h_samp_factor);
238
28.5k
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
239
28.5k
           compptr->v_samp_factor);
240
28.5k
  }
241
242
  /* Derive block_size, natural_order, and lim_Se */
243
10.0k
  if (cinfo->is_baseline || (cinfo->progressive_mode &&
244
10.0k
      cinfo->comps_in_scan)) { /* no pseudo SOS marker */
245
10.0k
    cinfo->block_size = DCTSIZE;
246
10.0k
    cinfo->natural_order = jpeg_natural_order;
247
10.0k
    cinfo->lim_Se = DCTSIZE2-1;
248
10.0k
  } else
249
26
    switch (cinfo->Se) {
250
0
    case (1*1-1):
251
0
      cinfo->block_size = 1;
252
0
      cinfo->natural_order = jpeg_natural_order; /* not needed */
253
0
      cinfo->lim_Se = cinfo->Se;
254
0
      break;
255
0
    case (2*2-1):
256
0
      cinfo->block_size = 2;
257
0
      cinfo->natural_order = jpeg_natural_order2;
258
0
      cinfo->lim_Se = cinfo->Se;
259
0
      break;
260
0
    case (3*3-1):
261
0
      cinfo->block_size = 3;
262
0
      cinfo->natural_order = jpeg_natural_order3;
263
0
      cinfo->lim_Se = cinfo->Se;
264
0
      break;
265
0
    case (4*4-1):
266
0
      cinfo->block_size = 4;
267
0
      cinfo->natural_order = jpeg_natural_order4;
268
0
      cinfo->lim_Se = cinfo->Se;
269
0
      break;
270
0
    case (5*5-1):
271
0
      cinfo->block_size = 5;
272
0
      cinfo->natural_order = jpeg_natural_order5;
273
0
      cinfo->lim_Se = cinfo->Se;
274
0
      break;
275
0
    case (6*6-1):
276
0
      cinfo->block_size = 6;
277
0
      cinfo->natural_order = jpeg_natural_order6;
278
0
      cinfo->lim_Se = cinfo->Se;
279
0
      break;
280
0
    case (7*7-1):
281
0
      cinfo->block_size = 7;
282
0
      cinfo->natural_order = jpeg_natural_order7;
283
0
      cinfo->lim_Se = cinfo->Se;
284
0
      break;
285
0
    case (8*8-1):
286
0
      cinfo->block_size = 8;
287
0
      cinfo->natural_order = jpeg_natural_order;
288
0
      cinfo->lim_Se = DCTSIZE2-1;
289
0
      break;
290
0
    case (9*9-1):
291
0
      cinfo->block_size = 9;
292
0
      cinfo->natural_order = jpeg_natural_order;
293
0
      cinfo->lim_Se = DCTSIZE2-1;
294
0
      break;
295
0
    case (10*10-1):
296
0
      cinfo->block_size = 10;
297
0
      cinfo->natural_order = jpeg_natural_order;
298
0
      cinfo->lim_Se = DCTSIZE2-1;
299
0
      break;
300
0
    case (11*11-1):
301
0
      cinfo->block_size = 11;
302
0
      cinfo->natural_order = jpeg_natural_order;
303
0
      cinfo->lim_Se = DCTSIZE2-1;
304
0
      break;
305
0
    case (12*12-1):
306
0
      cinfo->block_size = 12;
307
0
      cinfo->natural_order = jpeg_natural_order;
308
0
      cinfo->lim_Se = DCTSIZE2-1;
309
0
      break;
310
0
    case (13*13-1):
311
0
      cinfo->block_size = 13;
312
0
      cinfo->natural_order = jpeg_natural_order;
313
0
      cinfo->lim_Se = DCTSIZE2-1;
314
0
      break;
315
0
    case (14*14-1):
316
0
      cinfo->block_size = 14;
317
0
      cinfo->natural_order = jpeg_natural_order;
318
0
      cinfo->lim_Se = DCTSIZE2-1;
319
0
      break;
320
0
    case (15*15-1):
321
0
      cinfo->block_size = 15;
322
0
      cinfo->natural_order = jpeg_natural_order;
323
0
      cinfo->lim_Se = DCTSIZE2-1;
324
0
      break;
325
0
    case (16*16-1):
326
0
      cinfo->block_size = 16;
327
0
      cinfo->natural_order = jpeg_natural_order;
328
0
      cinfo->lim_Se = DCTSIZE2-1;
329
0
      break;
330
0
    default:
331
0
      ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
332
26
         cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
333
26
    }
334
335
  /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
336
   * In the full decompressor,
337
   * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
338
   * but in the transcoder,
339
   * jpeg_calc_output_dimensions is not used, so we must do it here.
340
   */
341
10.0k
  cinfo->min_DCT_h_scaled_size = cinfo->block_size;
342
10.0k
  cinfo->min_DCT_v_scaled_size = cinfo->block_size;
343
344
  /* Compute dimensions of components */
345
38.5k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
346
28.5k
       ci++, compptr++) {
347
28.5k
    compptr->DCT_h_scaled_size = cinfo->block_size;
348
28.5k
    compptr->DCT_v_scaled_size = cinfo->block_size;
349
    /* Size in DCT blocks */
350
28.5k
    compptr->width_in_blocks = (JDIMENSION)
351
28.5k
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
352
28.5k
        (long) (cinfo->max_h_samp_factor * cinfo->block_size));
353
28.5k
    compptr->height_in_blocks = (JDIMENSION)
354
28.5k
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
355
28.5k
        (long) (cinfo->max_v_samp_factor * cinfo->block_size));
356
    /* downsampled_width and downsampled_height will also be overridden by
357
     * jdmaster.c if we are doing full decompression.  The transcoder library
358
     * doesn't use these values, but the calling application might.
359
     */
360
    /* Size in samples */
361
28.5k
    compptr->downsampled_width = (JDIMENSION)
362
28.5k
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
363
28.5k
        (long) cinfo->max_h_samp_factor);
364
28.5k
    compptr->downsampled_height = (JDIMENSION)
365
28.5k
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
366
28.5k
        (long) cinfo->max_v_samp_factor);
367
    /* Mark component needed, until color conversion says otherwise */
368
28.5k
    compptr->component_needed = TRUE;
369
    /* Mark no quantization table yet saved for component */
370
28.5k
    compptr->quant_table = NULL;
371
28.5k
  }
372
373
  /* Compute number of fully interleaved MCU rows. */
374
10.0k
  cinfo->total_iMCU_rows = (JDIMENSION)
375
10.0k
    jdiv_round_up((long) cinfo->image_height,
376
10.0k
            (long) (cinfo->max_v_samp_factor * cinfo->block_size));
377
378
  /* Decide whether file contains multiple scans */
379
10.0k
  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
380
1.36k
    cinfo->inputctl->has_multiple_scans = TRUE;
381
8.63k
  else
382
8.63k
    cinfo->inputctl->has_multiple_scans = FALSE;
383
10.0k
}
384
385
386
LOCAL(void)
387
per_scan_setup (j_decompress_ptr cinfo)
388
/* Do computations that are needed before processing a JPEG scan */
389
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
390
19.1k
{
391
19.1k
  int ci, mcublks, tmp;
392
19.1k
  jpeg_component_info *compptr;
393
394
19.1k
  if (cinfo->comps_in_scan == 1) {
395
396
    /* Noninterleaved (single-component) scan */
397
9.71k
    compptr = cinfo->cur_comp_info[0];
398
399
    /* Overall image size in MCUs */
400
9.71k
    cinfo->MCUs_per_row = compptr->width_in_blocks;
401
9.71k
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
402
403
    /* For noninterleaved scan, always one block per MCU */
404
9.71k
    compptr->MCU_width = 1;
405
9.71k
    compptr->MCU_height = 1;
406
9.71k
    compptr->MCU_blocks = 1;
407
9.71k
    compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
408
9.71k
    compptr->last_col_width = 1;
409
    /* For noninterleaved scans, it is convenient to define last_row_height
410
     * as the number of block rows present in the last iMCU row.
411
     */
412
9.71k
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
413
9.71k
    if (tmp == 0) tmp = compptr->v_samp_factor;
414
9.71k
    compptr->last_row_height = tmp;
415
416
    /* Prepare array describing MCU composition */
417
9.71k
    cinfo->blocks_in_MCU = 1;
418
9.71k
    cinfo->MCU_membership[0] = 0;
419
420
9.71k
  } else {
421
422
    /* Interleaved (multi-component) scan */
423
9.45k
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
424
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
425
9.45k
         MAX_COMPS_IN_SCAN);
426
427
    /* Overall image size in MCUs */
428
9.45k
    cinfo->MCUs_per_row = (JDIMENSION)
429
9.45k
      jdiv_round_up((long) cinfo->image_width,
430
9.45k
        (long) (cinfo->max_h_samp_factor * cinfo->block_size));
431
9.45k
    cinfo->MCU_rows_in_scan = cinfo->total_iMCU_rows;
432
433
9.45k
    cinfo->blocks_in_MCU = 0;
434
435
38.2k
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
436
28.7k
      compptr = cinfo->cur_comp_info[ci];
437
      /* Sampling factors give # of blocks of component in each MCU */
438
28.7k
      compptr->MCU_width = compptr->h_samp_factor;
439
28.7k
      compptr->MCU_height = compptr->v_samp_factor;
440
28.7k
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
441
28.7k
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
442
      /* Figure number of non-dummy blocks in last MCU column & row */
443
28.7k
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
444
28.7k
      if (tmp == 0) tmp = compptr->MCU_width;
445
28.7k
      compptr->last_col_width = tmp;
446
28.7k
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
447
28.7k
      if (tmp == 0) tmp = compptr->MCU_height;
448
28.7k
      compptr->last_row_height = tmp;
449
      /* Prepare array describing MCU composition */
450
28.7k
      mcublks = compptr->MCU_blocks;
451
28.7k
      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
452
0
  ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
453
75.8k
      while (mcublks-- > 0) {
454
47.0k
  cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
455
47.0k
      }
456
28.7k
    }
457
458
9.45k
  }
459
19.1k
}
460
461
462
/*
463
 * Save away a copy of the Q-table referenced by each component present
464
 * in the current scan, unless already saved during a prior scan.
465
 *
466
 * In a multiple-scan JPEG file, the encoder could assign different components
467
 * the same Q-table slot number, but change table definitions between scans
468
 * so that each component uses a different Q-table.  (The IJG encoder is not
469
 * currently capable of doing this, but other encoders might.)  Since we want
470
 * to be able to dequantize all the components at the end of the file, this
471
 * means that we have to save away the table actually used for each component.
472
 * We do this by copying the table at the start of the first scan containing
473
 * the component.
474
 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
475
 * slot between scans of a component using that slot.  If the encoder does so
476
 * anyway, this decoder will simply use the Q-table values that were current
477
 * at the start of the first scan for the component.
478
 *
479
 * The decompressor output side looks only at the saved quant tables,
480
 * not at the current Q-table slots.
481
 */
482
483
LOCAL(void)
484
latch_quant_tables (j_decompress_ptr cinfo)
485
19.1k
{
486
19.1k
  int ci, qtblno;
487
19.1k
  jpeg_component_info *compptr;
488
19.1k
  JQUANT_TBL * qtbl;
489
490
57.6k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
491
38.4k
    compptr = cinfo->cur_comp_info[ci];
492
    /* No work if we already saved Q-table for this component */
493
38.4k
    if (compptr->quant_table != NULL)
494
10.0k
      continue;
495
    /* Make sure specified quantization table is present */
496
28.4k
    qtblno = compptr->quant_tbl_no;
497
28.4k
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
498
28.4k
  cinfo->quant_tbl_ptrs[qtblno] == NULL)
499
11
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
500
    /* OK, save away the quantization table */
501
28.4k
    qtbl = (JQUANT_TBL *) (*cinfo->mem->alloc_small)
502
28.4k
      ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(JQUANT_TBL));
503
28.4k
    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
504
28.4k
    compptr->quant_table = qtbl;
505
28.4k
  }
506
19.1k
}
507
508
509
/*
510
 * Initialize the input modules to read a scan of compressed data.
511
 * The first call to this is done by jdmaster.c after initializing
512
 * the entire decompressor (during jpeg_start_decompress).
513
 * Subsequent calls come from consume_markers, below.
514
 */
515
516
METHODDEF(void)
517
start_input_pass (j_decompress_ptr cinfo)
518
19.1k
{
519
19.1k
  per_scan_setup(cinfo);
520
19.1k
  latch_quant_tables(cinfo);
521
19.1k
  (*cinfo->entropy->start_pass) (cinfo);
522
19.1k
  (*cinfo->coef->start_input_pass) (cinfo);
523
19.1k
  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
524
19.1k
}
525
526
527
/*
528
 * Finish up after inputting a compressed-data scan.
529
 * This is called by the coefficient controller after it's read all
530
 * the expected data of the scan.
531
 */
532
533
METHODDEF(void)
534
finish_input_pass (j_decompress_ptr cinfo)
535
19.0k
{
536
19.0k
  (*cinfo->entropy->finish_pass) (cinfo);
537
19.0k
  cinfo->inputctl->consume_input = consume_markers;
538
19.0k
}
539
540
541
/*
542
 * Read JPEG markers before, between, or after compressed-data scans.
543
 * Change state as necessary when a new scan is reached.
544
 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
545
 *
546
 * The consume_input method pointer points either here or to the
547
 * coefficient controller's consume_data routine, depending on whether
548
 * we are reading a compressed data segment or inter-segment markers.
549
 *
550
 * Note: This function should NOT return a pseudo SOS marker (with zero
551
 * component number) to the caller.  A pseudo marker received by
552
 * read_markers is processed and then skipped for other markers.
553
 */
554
555
METHODDEF(int)
556
consume_markers (j_decompress_ptr cinfo)
557
49.2k
{
558
49.2k
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
559
49.2k
  int val;
560
561
49.2k
  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
562
0
    return JPEG_REACHED_EOI;
563
564
49.2k
  for (;;) {     /* Loop to pass pseudo SOS marker */
565
49.2k
    val = (*cinfo->marker->read_markers) (cinfo);
566
567
49.2k
    switch (val) {
568
19.2k
    case JPEG_REACHED_SOS: /* Found SOS */
569
19.2k
      if (inputctl->inheaders) { /* 1st SOS */
570
10.0k
  if (inputctl->inheaders == 1)
571
10.0k
    initial_setup(cinfo);
572
10.0k
  if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
573
0
    inputctl->inheaders = 2;
574
0
    break;
575
0
  }
576
10.0k
  inputctl->inheaders = 0;
577
  /* Note: start_input_pass must be called by jdmaster.c
578
   * before any more input can be consumed.  jdapimin.c is
579
   * responsible for enforcing this sequencing.
580
   */
581
10.0k
      } else {     /* 2nd or later SOS marker */
582
9.17k
  if (! inputctl->pub.has_multiple_scans)
583
9
    ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
584
9.17k
  if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
585
0
    break;
586
9.17k
  start_input_pass(cinfo);
587
9.17k
      }
588
19.2k
      return val;
589
10.8k
    case JPEG_REACHED_EOI: /* Found EOI */
590
10.8k
      inputctl->pub.eoi_reached = TRUE;
591
10.8k
      if (inputctl->inheaders) { /* Tables-only datastream, apparently */
592
1.77k
  if (cinfo->marker->saw_SOF)
593
103
    ERREXIT(cinfo, JERR_SOF_NO_SOS);
594
9.03k
      } else {
595
  /* Prevent infinite loop in coef ctlr's decompress_data routine
596
   * if user set output_scan_number larger than number of scans.
597
   */
598
9.03k
  if (cinfo->output_scan_number > cinfo->input_scan_number)
599
0
    cinfo->output_scan_number = cinfo->input_scan_number;
600
9.03k
      }
601
10.8k
      return val;
602
16.9k
    case JPEG_SUSPENDED:
603
16.9k
      return val;
604
0
    default:
605
0
      return val;
606
49.2k
    }
607
49.2k
  }
608
49.2k
}
609
610
611
/*
612
 * Reset state to begin a fresh datastream.
613
 */
614
615
METHODDEF(void)
616
reset_input_controller (j_decompress_ptr cinfo)
617
13.2k
{
618
13.2k
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
619
620
13.2k
  inputctl->pub.consume_input = consume_markers;
621
13.2k
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
622
13.2k
  inputctl->pub.eoi_reached = FALSE;
623
13.2k
  inputctl->inheaders = 1;
624
  /* Reset other modules */
625
13.2k
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
626
13.2k
  (*cinfo->marker->reset_marker_reader) (cinfo);
627
  /* Reset progression state -- would be cleaner if entropy decoder did this */
628
13.2k
  cinfo->coef_bits = NULL;
629
13.2k
}
630
631
632
/*
633
 * Initialize the input controller module.
634
 * This is called only once, when the decompression object is created.
635
 */
636
637
GLOBAL(void)
638
jinit_input_controller (j_decompress_ptr cinfo)
639
13.3k
{
640
13.3k
  my_inputctl_ptr inputctl;
641
642
  /* Create subobject in permanent pool */
643
13.3k
  inputctl = (my_inputctl_ptr) (*cinfo->mem->alloc_small)
644
13.3k
    ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_input_controller));
645
13.3k
  cinfo->inputctl = &inputctl->pub;
646
  /* Initialize method pointers */
647
13.3k
  inputctl->pub.consume_input = consume_markers;
648
13.3k
  inputctl->pub.reset_input_controller = reset_input_controller;
649
13.3k
  inputctl->pub.start_input_pass = start_input_pass;
650
13.3k
  inputctl->pub.finish_input_pass = finish_input_pass;
651
  /* Initialize state: can't use reset_input_controller since we don't
652
   * want to try to reset other modules yet.
653
   */
654
13.3k
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
655
13.3k
  inputctl->pub.eoi_reached = FALSE;
656
13.3k
  inputctl->inheaders = 1;
657
13.3k
}