Coverage Report

Created: 2025-06-13 06:18

/src/gdal/build/frmts/jpeg/libjpeg12/jdinput12.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdinput.c
3
 *
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
5
 * This file is part of the Independent JPEG Group's software.
6
 * For conditions of distribution and use, see the accompanying README file.
7
 *
8
 * This file contains input control logic for the JPEG decompressor.
9
 * These routines are concerned with controlling the decompressor's input
10
 * processing (marker reading and coefficient decoding).  The actual input
11
 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
12
 */
13
14
#define JPEG_INTERNALS
15
#include "jinclude.h"
16
#include "jpeglib.h"
17
18
19
/* Private state */
20
21
typedef struct {
22
  struct jpeg_input_controller pub; /* public fields */
23
24
  boolean inheaders;    /* TRUE until first SOS is reached */
25
} my_input_controller;
26
27
typedef my_input_controller * my_inputctl_ptr;
28
29
30
/* Forward declarations */
31
METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
32
33
34
/*
35
 * Routines to calculate various quantities related to the size of the image.
36
 */
37
38
LOCAL(void)
39
initial_setup (j_decompress_ptr cinfo)
40
/* Called once, when first SOS marker is reached */
41
0
{
42
0
  int ci;
43
0
  jpeg_component_info *compptr;
44
45
  /* Make sure image isn't bigger than I can handle */
46
0
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
47
0
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
48
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
49
50
  /* For now, precision must match compiled-in value... */
51
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
52
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
53
54
  /* Check that number of components won't exceed internal array sizes */
55
0
  if (cinfo->num_components > MAX_COMPONENTS)
56
0
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
57
0
       MAX_COMPONENTS);
58
59
  /* Compute maximum sampling factors; check factor validity */
60
0
  cinfo->max_h_samp_factor = 1;
61
0
  cinfo->max_v_samp_factor = 1;
62
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
63
0
       ci++, compptr++) {
64
0
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
65
0
  compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
66
0
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
67
0
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
68
0
           compptr->h_samp_factor);
69
0
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
70
0
           compptr->v_samp_factor);
71
0
  }
72
73
  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
74
   * In the full decompressor, this will be overridden by jdmaster.c;
75
   * but in the transcoder, jdmaster.c is not used, so we must do it here.
76
   */
77
0
  cinfo->min_DCT_scaled_size = DCTSIZE;
78
79
  /* Compute dimensions of components */
80
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
81
0
       ci++, compptr++) {
82
0
    compptr->DCT_scaled_size = DCTSIZE;
83
    /* Size in DCT blocks */
84
0
    compptr->width_in_blocks = (JDIMENSION)
85
0
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
86
0
        (long) (cinfo->max_h_samp_factor * DCTSIZE));
87
0
    compptr->height_in_blocks = (JDIMENSION)
88
0
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
89
0
        (long) (cinfo->max_v_samp_factor * DCTSIZE));
90
    /* downsampled_width and downsampled_height will also be overridden by
91
     * jdmaster.c if we are doing full decompression.  The transcoder library
92
     * doesn't use these values, but the calling application might.
93
     */
94
    /* Size in samples */
95
0
    compptr->downsampled_width = (JDIMENSION)
96
0
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
97
0
        (long) cinfo->max_h_samp_factor);
98
0
    compptr->downsampled_height = (JDIMENSION)
99
0
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
100
0
        (long) cinfo->max_v_samp_factor);
101
    /* Mark component needed, until color conversion says otherwise */
102
0
    compptr->component_needed = TRUE;
103
    /* Mark no quantization table yet saved for component */
104
0
    compptr->quant_table = NULL;
105
0
  }
106
107
  /* Compute number of fully interleaved MCU rows. */
108
0
  cinfo->total_iMCU_rows = (JDIMENSION)
109
0
    jdiv_round_up((long) cinfo->image_height,
110
0
      (long) (cinfo->max_v_samp_factor*DCTSIZE));
111
112
  /* Decide whether file contains multiple scans */
113
0
  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
114
0
    cinfo->inputctl->has_multiple_scans = TRUE;
115
0
  else
116
0
    cinfo->inputctl->has_multiple_scans = FALSE;
117
0
}
118
119
120
LOCAL(void)
121
per_scan_setup (j_decompress_ptr cinfo)
122
/* Do computations that are needed before processing a JPEG scan */
123
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
124
0
{
125
0
  int ci, mcublks, tmp;
126
0
  jpeg_component_info *compptr;
127
  
128
0
  if (cinfo->comps_in_scan == 1) {
129
    
130
    /* Noninterleaved (single-component) scan */
131
0
    compptr = cinfo->cur_comp_info[0];
132
    
133
    /* Overall image size in MCUs */
134
0
    cinfo->MCUs_per_row = compptr->width_in_blocks;
135
0
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
136
    
137
    /* For noninterleaved scan, always one block per MCU */
138
0
    compptr->MCU_width = 1;
139
0
    compptr->MCU_height = 1;
140
0
    compptr->MCU_blocks = 1;
141
0
    compptr->MCU_sample_width = compptr->DCT_scaled_size;
142
0
    compptr->last_col_width = 1;
143
    /* For noninterleaved scans, it is convenient to define last_row_height
144
     * as the number of block rows present in the last iMCU row.
145
     */
146
0
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
147
0
    if (tmp == 0) tmp = compptr->v_samp_factor;
148
0
    compptr->last_row_height = tmp;
149
    
150
    /* Prepare array describing MCU composition */
151
0
    cinfo->blocks_in_MCU = 1;
152
0
    cinfo->MCU_membership[0] = 0;
153
    
154
0
  } else {
155
    
156
    /* Interleaved (multi-component) scan */
157
0
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
158
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
159
0
         MAX_COMPS_IN_SCAN);
160
    
161
    /* Overall image size in MCUs */
162
0
    cinfo->MCUs_per_row = (JDIMENSION)
163
0
      jdiv_round_up((long) cinfo->image_width,
164
0
        (long) (cinfo->max_h_samp_factor*DCTSIZE));
165
0
    cinfo->MCU_rows_in_scan = (JDIMENSION)
166
0
      jdiv_round_up((long) cinfo->image_height,
167
0
        (long) (cinfo->max_v_samp_factor*DCTSIZE));
168
    
169
0
    cinfo->blocks_in_MCU = 0;
170
    
171
0
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
0
      compptr = cinfo->cur_comp_info[ci];
173
      /* Sampling factors give # of blocks of component in each MCU */
174
0
      compptr->MCU_width = compptr->h_samp_factor;
175
0
      compptr->MCU_height = compptr->v_samp_factor;
176
0
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
177
0
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
178
      /* Figure number of non-dummy blocks in last MCU column & row */
179
0
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
180
0
      if (tmp == 0) tmp = compptr->MCU_width;
181
0
      compptr->last_col_width = tmp;
182
0
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
183
0
      if (tmp == 0) tmp = compptr->MCU_height;
184
0
      compptr->last_row_height = tmp;
185
      /* Prepare array describing MCU composition */
186
0
      mcublks = compptr->MCU_blocks;
187
0
      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
188
0
  ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
189
0
      while (mcublks-- > 0) {
190
0
  cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
191
0
      }
192
0
    }
193
    
194
0
  }
195
0
}
196
197
198
/*
199
 * Save away a copy of the Q-table referenced by each component present
200
 * in the current scan, unless already saved during a prior scan.
201
 *
202
 * In a multiple-scan JPEG file, the encoder could assign different components
203
 * the same Q-table slot number, but change table definitions between scans
204
 * so that each component uses a different Q-table.  (The IJG encoder is not
205
 * currently capable of doing this, but other encoders might.)  Since we want
206
 * to be able to dequantize all the components at the end of the file, this
207
 * means that we have to save away the table actually used for each component.
208
 * We do this by copying the table at the start of the first scan containing
209
 * the component.
210
 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
211
 * slot between scans of a component using that slot.  If the encoder does so
212
 * anyway, this decoder will simply use the Q-table values that were current
213
 * at the start of the first scan for the component.
214
 *
215
 * The decompressor output side looks only at the saved quant tables,
216
 * not at the current Q-table slots.
217
 */
218
219
LOCAL(void)
220
latch_quant_tables (j_decompress_ptr cinfo)
221
0
{
222
0
  int ci, qtblno;
223
0
  jpeg_component_info *compptr;
224
0
  JQUANT_TBL * qtbl;
225
226
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
227
0
    compptr = cinfo->cur_comp_info[ci];
228
    /* No work if we already saved Q-table for this component */
229
0
    if (compptr->quant_table != NULL)
230
0
      continue;
231
    /* Make sure specified quantization table is present */
232
0
    qtblno = compptr->quant_tbl_no;
233
0
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
234
0
  cinfo->quant_tbl_ptrs[qtblno] == NULL)
235
0
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
236
    /* OK, save away the quantization table */
237
0
    qtbl = (JQUANT_TBL *)
238
0
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
239
0
          SIZEOF(JQUANT_TBL));
240
0
    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
241
0
    compptr->quant_table = qtbl;
242
0
  }
243
0
}
244
245
246
/*
247
 * Initialize the input modules to read a scan of compressed data.
248
 * The first call to this is done by jdmaster.c after initializing
249
 * the entire decompressor (during jpeg_start_decompress).
250
 * Subsequent calls come from consume_markers, below.
251
 */
252
253
METHODDEF(void)
254
start_input_pass (j_decompress_ptr cinfo)
255
0
{
256
0
  per_scan_setup(cinfo);
257
0
  latch_quant_tables(cinfo);
258
0
  (*cinfo->entropy->start_pass) (cinfo);
259
0
  (*cinfo->coef->start_input_pass) (cinfo);
260
0
  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
261
0
}
262
263
264
/*
265
 * Finish up after inputting a compressed-data scan.
266
 * This is called by the coefficient controller after it's read all
267
 * the expected data of the scan.
268
 */
269
270
METHODDEF(void)
271
finish_input_pass (j_decompress_ptr cinfo)
272
0
{
273
0
  cinfo->inputctl->consume_input = consume_markers;
274
0
}
275
276
277
/*
278
 * Read JPEG markers before, between, or after compressed-data scans.
279
 * Change state as necessary when a new scan is reached.
280
 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
281
 *
282
 * The consume_input method pointer points either here or to the
283
 * coefficient controller's consume_data routine, depending on whether
284
 * we are reading a compressed data segment or inter-segment markers.
285
 */
286
287
METHODDEF(int)
288
consume_markers (j_decompress_ptr cinfo)
289
0
{
290
0
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
291
0
  int val;
292
293
0
  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
294
0
    return JPEG_REACHED_EOI;
295
296
0
  val = (*cinfo->marker->read_markers) (cinfo);
297
298
0
  switch (val) {
299
0
  case JPEG_REACHED_SOS: /* Found SOS */
300
0
    if (inputctl->inheaders) { /* 1st SOS */
301
0
      initial_setup(cinfo);
302
0
      inputctl->inheaders = FALSE;
303
      /* Note: start_input_pass must be called by jdmaster.c
304
       * before any more input can be consumed.  jdapimin.c is
305
       * responsible for enforcing this sequencing.
306
       */
307
0
    } else {     /* 2nd or later SOS marker */
308
0
      if (! inputctl->pub.has_multiple_scans)
309
0
  ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I was not expecting this! */
310
0
      start_input_pass(cinfo);
311
0
    }
312
0
    break;
313
0
  case JPEG_REACHED_EOI: /* Found EOI */
314
0
    inputctl->pub.eoi_reached = TRUE;
315
0
    if (inputctl->inheaders) { /* Tables-only datastream, apparently */
316
0
      if (cinfo->marker->saw_SOF)
317
0
  ERREXIT(cinfo, JERR_SOF_NO_SOS);
318
0
    } else {
319
      /* Prevent infinite loop in coef ctlr's decompress_data routine
320
       * if user set output_scan_number larger than number of scans.
321
       */
322
0
      if (cinfo->output_scan_number > cinfo->input_scan_number)
323
0
  cinfo->output_scan_number = cinfo->input_scan_number;
324
0
    }
325
0
    break;
326
0
  case JPEG_SUSPENDED:
327
0
    break;
328
0
  }
329
330
0
  return val;
331
0
}
332
333
334
/*
335
 * Reset state to begin a fresh datastream.
336
 */
337
338
METHODDEF(void)
339
reset_input_controller (j_decompress_ptr cinfo)
340
0
{
341
0
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
342
343
0
  inputctl->pub.consume_input = consume_markers;
344
0
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
345
0
  inputctl->pub.eoi_reached = FALSE;
346
0
  inputctl->inheaders = TRUE;
347
  /* Reset other modules */
348
0
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
349
0
  (*cinfo->marker->reset_marker_reader) (cinfo);
350
  /* Reset progression state -- would be cleaner if entropy decoder did this */
351
0
  cinfo->coef_bits = NULL;
352
0
}
353
354
355
/*
356
 * Initialize the input controller module.
357
 * This is called only once, when the decompression object is created.
358
 */
359
360
GLOBAL(void)
361
jinit_input_controller (j_decompress_ptr cinfo)
362
0
{
363
0
  my_inputctl_ptr inputctl;
364
365
  /* Create subobject in permanent pool */
366
0
  inputctl = (my_inputctl_ptr)
367
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
368
0
        SIZEOF(my_input_controller));
369
0
  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
370
  /* Initialize method pointers */
371
0
  inputctl->pub.consume_input = consume_markers;
372
0
  inputctl->pub.reset_input_controller = reset_input_controller;
373
0
  inputctl->pub.start_input_pass = start_input_pass;
374
0
  inputctl->pub.finish_input_pass = finish_input_pass;
375
  /* Initialize state: can't use reset_input_controller since we don't
376
   * want to try to reset other modules yet.
377
   */
378
0
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
379
0
  inputctl->pub.eoi_reached = FALSE;
380
0
  inputctl->inheaders = TRUE;
381
0
}