Coverage Report

Created: 2025-07-01 06:27

/src/libjpeg-turbo.3.0.x/jdapistd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdapistd.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2010, 2015-2020, 2022-2024, D. R. Commander.
8
 * Copyright (C) 2015, Google, Inc.
9
 * For conditions of distribution and use, see the accompanying README.ijg
10
 * file.
11
 *
12
 * This file contains application interface code for the decompression half
13
 * of the JPEG library.  These are the "standard" API routines that are
14
 * used in the normal full-decompression case.  They are not used by a
15
 * transcoding-only application.  Note that if an application links in
16
 * jpeg_start_decompress, it will end up linking in the entire decompressor.
17
 * We thus must separate this file from jdapimin.c to avoid linking the
18
 * whole decompression library into a transcoder.
19
 */
20
21
#include "jinclude.h"
22
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
23
#include "jdmainct.h"
24
#include "jdcoefct.h"
25
#else
26
#define JPEG_INTERNALS
27
#include "jpeglib.h"
28
#endif
29
#include "jdmaster.h"
30
#include "jdmerge.h"
31
#include "jdsample.h"
32
#include "jmemsys.h"
33
34
#if BITS_IN_JSAMPLE == 8
35
36
/* Forward declarations */
37
LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
38
39
40
/*
41
 * Decompression initialization.
42
 * jpeg_read_header must be completed before calling this.
43
 *
44
 * If a multipass operating mode was selected, this will do all but the
45
 * last pass, and thus may take a great deal of time.
46
 *
47
 * Returns FALSE if suspended.  The return value need be inspected only if
48
 * a suspending data source is used.
49
 */
50
51
GLOBAL(boolean)
52
jpeg_start_decompress(j_decompress_ptr cinfo)
53
26.6k
{
54
26.6k
  if (cinfo->global_state == DSTATE_READY) {
55
    /* First call: initialize master control, select active modules */
56
26.6k
    jinit_master_decompress(cinfo);
57
26.6k
    if (cinfo->buffered_image) {
58
      /* No more work here; expecting jpeg_start_output next */
59
0
      cinfo->global_state = DSTATE_BUFIMAGE;
60
0
      return TRUE;
61
0
    }
62
26.6k
    cinfo->global_state = DSTATE_PRELOAD;
63
26.6k
  }
64
26.6k
  if (cinfo->global_state == DSTATE_PRELOAD) {
65
    /* If file has multiple scans, absorb them all into the coef buffer */
66
21.6k
    if (cinfo->inputctl->has_multiple_scans) {
67
17.9k
#ifdef D_MULTISCAN_FILES_SUPPORTED
68
164M
      for (;;) {
69
164M
        int retcode;
70
        /* Call progress monitor hook if present */
71
164M
        if (cinfo->progress != NULL)
72
164M
          (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
73
        /* Absorb some more input */
74
164M
        retcode = (*cinfo->inputctl->consume_input) (cinfo);
75
164M
        if (retcode == JPEG_SUSPENDED)
76
0
          return FALSE;
77
164M
        if (retcode == JPEG_REACHED_EOI)
78
16.3k
          break;
79
        /* Advance progress counter if appropriate */
80
164M
        if (cinfo->progress != NULL &&
81
164M
            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
82
164M
          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
83
            /* jdmaster underestimated number of scans; ratchet up one scan */
84
82.9k
            cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
85
82.9k
          }
86
164M
        }
87
164M
      }
88
#else
89
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90
#endif /* D_MULTISCAN_FILES_SUPPORTED */
91
17.9k
    }
92
21.6k
    cinfo->output_scan_number = cinfo->input_scan_number;
93
21.6k
  } else if (cinfo->global_state != DSTATE_PRESCAN)
94
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
95
  /* Perform any dummy output passes, and set up for the final pass */
96
26.6k
  return output_pass_setup(cinfo);
97
26.6k
}
98
99
100
/*
101
 * Set up for an output pass, and perform any dummy pass(es) needed.
102
 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
103
 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
104
 * Exit: If done, returns TRUE and sets global_state for proper output mode.
105
 *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
106
 */
107
108
LOCAL(boolean)
109
output_pass_setup(j_decompress_ptr cinfo)
110
10.1k
{
111
10.1k
  if (cinfo->global_state != DSTATE_PRESCAN) {
112
    /* First call: do pass setup */
113
10.1k
    (*cinfo->master->prepare_for_output_pass) (cinfo);
114
10.1k
    cinfo->output_scanline = 0;
115
10.1k
    cinfo->global_state = DSTATE_PRESCAN;
116
10.1k
  }
117
  /* Loop over any required dummy passes */
118
10.1k
  while (cinfo->master->is_dummy_pass) {
119
0
#ifdef QUANT_2PASS_SUPPORTED
120
    /* Crank through the dummy pass */
121
0
    while (cinfo->output_scanline < cinfo->output_height) {
122
0
      JDIMENSION last_scanline;
123
      /* Call progress monitor hook if present */
124
0
      if (cinfo->progress != NULL) {
125
0
        cinfo->progress->pass_counter = (long)cinfo->output_scanline;
126
0
        cinfo->progress->pass_limit = (long)cinfo->output_height;
127
0
        (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
128
0
      }
129
      /* Process some data */
130
0
      last_scanline = cinfo->output_scanline;
131
0
      if (cinfo->data_precision == 16) {
132
0
#ifdef D_LOSSLESS_SUPPORTED
133
0
        if (cinfo->main->process_data_16 == NULL)
134
0
          ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
135
0
        (*cinfo->main->process_data_16) (cinfo, (J16SAMPARRAY)NULL,
136
0
                                         &cinfo->output_scanline,
137
0
                                         (JDIMENSION)0);
138
#else
139
        ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
140
#endif
141
0
      } else if (cinfo->data_precision == 12) {
142
0
        if (cinfo->main->process_data_12 == NULL)
143
0
          ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
144
0
        (*cinfo->main->process_data_12) (cinfo, (J12SAMPARRAY)NULL,
145
0
                                         &cinfo->output_scanline,
146
0
                                         (JDIMENSION)0);
147
0
      } else {
148
0
        if (cinfo->main->process_data == NULL)
149
0
          ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
150
0
        (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
151
0
                                      &cinfo->output_scanline, (JDIMENSION)0);
152
0
      }
153
0
      if (cinfo->output_scanline == last_scanline)
154
0
        return FALSE;           /* No progress made, must suspend */
155
0
    }
156
    /* Finish up dummy pass, and set up for another one */
157
0
    (*cinfo->master->finish_output_pass) (cinfo);
158
0
    (*cinfo->master->prepare_for_output_pass) (cinfo);
159
0
    cinfo->output_scanline = 0;
160
#else
161
    ERREXIT(cinfo, JERR_NOT_COMPILED);
162
#endif /* QUANT_2PASS_SUPPORTED */
163
0
  }
164
  /* Ready for application to drive output pass through
165
   * _jpeg_read_scanlines or _jpeg_read_raw_data.
166
   */
167
10.1k
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
168
10.1k
  return TRUE;
169
10.1k
}
170
171
#endif /* BITS_IN_JSAMPLE == 8 */
172
173
174
#if BITS_IN_JSAMPLE != 16
175
176
/*
177
 * Enable partial scanline decompression
178
 *
179
 * Must be called after jpeg_start_decompress() and before any calls to
180
 * _jpeg_read_scanlines() or _jpeg_skip_scanlines().
181
 *
182
 * Refer to libjpeg.txt for more information.
183
 */
184
185
GLOBAL(void)
186
_jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
187
                    JDIMENSION *width)
188
0
{
189
0
  int ci, align, orig_downsampled_width;
190
0
  JDIMENSION input_xoffset;
191
0
  boolean reinit_upsampler = FALSE;
192
0
  jpeg_component_info *compptr;
193
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
194
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
195
0
#endif
196
197
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
198
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
199
200
0
  if (cinfo->master->lossless)
201
0
    ERREXIT(cinfo, JERR_NOTIMPL);
202
203
0
  if ((cinfo->global_state != DSTATE_SCANNING &&
204
0
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
205
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
206
207
0
  if (!xoffset || !width)
208
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
209
210
  /* xoffset and width must fall within the output image dimensions. */
211
0
  if (*width == 0 ||
212
0
      (unsigned long long)(*xoffset) + *width > cinfo->output_width)
213
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
214
215
  /* No need to do anything if the caller wants the entire width. */
216
0
  if (*width == cinfo->output_width)
217
0
    return;
218
219
  /* Ensuring the proper alignment of xoffset is tricky.  At minimum, it
220
   * must align with an MCU boundary, because:
221
   *
222
   *   (1) The IDCT is performed in blocks, and it is not feasible to modify
223
   *       the algorithm so that it can transform partial blocks.
224
   *   (2) Because of the SIMD extensions, any input buffer passed to the
225
   *       upsampling and color conversion routines must be aligned to the
226
   *       SIMD word size (for instance, 128-bit in the case of SSE2.)  The
227
   *       easiest way to accomplish this without copying data is to ensure
228
   *       that upsampling and color conversion begin at the start of the
229
   *       first MCU column that will be inverse transformed.
230
   *
231
   * In practice, we actually impose a stricter alignment requirement.  We
232
   * require that xoffset be a multiple of the maximum MCU column width of all
233
   * of the components (the "iMCU column width.")  This is to simplify the
234
   * single-pass decompression case, allowing us to use the same MCU column
235
   * width for all of the components.
236
   */
237
0
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
238
0
    align = cinfo->_min_DCT_scaled_size;
239
0
  else
240
0
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
241
242
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
243
0
  input_xoffset = *xoffset;
244
0
  *xoffset = (input_xoffset / align) * align;
245
246
  /* Adjust the width so that the right edge of the output image is as
247
   * requested (only the left edge is altered.)  It is important that calling
248
   * programs check this value after this function returns, so that they can
249
   * allocate an output buffer with the appropriate size.
250
   */
251
0
  *width = *width + input_xoffset - *xoffset;
252
0
  cinfo->output_width = *width;
253
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
254
0
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
255
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
256
0
    upsample->out_row_width =
257
0
      cinfo->output_width * cinfo->out_color_components;
258
0
  }
259
0
#endif
260
261
  /* Set the first and last iMCU columns that we must decompress.  These values
262
   * will be used in single-scan decompressions.
263
   */
264
0
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
265
0
  cinfo->master->last_iMCU_col =
266
0
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
267
0
                              (long)align) - 1;
268
269
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
270
0
       ci++, compptr++) {
271
0
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
272
0
              1 : compptr->h_samp_factor;
273
274
    /* Set downsampled_width to the new output width. */
275
0
    orig_downsampled_width = compptr->downsampled_width;
276
0
    compptr->downsampled_width =
277
0
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
278
0
                                (long)(compptr->h_samp_factor *
279
0
                                       compptr->_DCT_scaled_size),
280
0
                                (long)(cinfo->max_h_samp_factor *
281
0
                                       cinfo->_min_DCT_scaled_size));
282
0
    if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
283
0
      reinit_upsampler = TRUE;
284
285
    /* Set the first and last iMCU columns that we must decompress.  These
286
     * values will be used in multi-scan decompressions.
287
     */
288
0
    cinfo->master->first_MCU_col[ci] =
289
0
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
290
0
    cinfo->master->last_MCU_col[ci] =
291
0
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
292
0
                                (long)align) - 1;
293
0
  }
294
295
0
  if (reinit_upsampler) {
296
0
    cinfo->master->jinit_upsampler_no_alloc = TRUE;
297
0
    _jinit_upsampler(cinfo);
298
0
    cinfo->master->jinit_upsampler_no_alloc = FALSE;
299
0
  }
300
0
}
Unexecuted instantiation: jpeg12_crop_scanline
Unexecuted instantiation: jpeg_crop_scanline
301
302
#endif /* BITS_IN_JSAMPLE != 16 */
303
304
305
/*
306
 * Read some scanlines of data from the JPEG decompressor.
307
 *
308
 * The return value will be the number of lines actually read.
309
 * This may be less than the number requested in several cases,
310
 * including bottom of image, data source suspension, and operating
311
 * modes that emit multiple scanlines at a time.
312
 *
313
 * Note: we warn about excess calls to _jpeg_read_scanlines() since
314
 * this likely signals an application programmer error.  However,
315
 * an oversize buffer (max_lines > scanlines remaining) is not an error.
316
 */
317
318
GLOBAL(JDIMENSION)
319
_jpeg_read_scanlines(j_decompress_ptr cinfo, _JSAMPARRAY scanlines,
320
                     JDIMENSION max_lines)
321
12.1M
{
322
12.1M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
323
12.1M
  JDIMENSION row_ctr;
324
325
12.1M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
326
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
327
328
12.1M
  if (cinfo->global_state != DSTATE_SCANNING)
329
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
330
12.1M
  if (cinfo->output_scanline >= cinfo->output_height) {
331
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
332
0
    return 0;
333
0
  }
334
335
  /* Call progress monitor hook if present */
336
12.1M
  if (cinfo->progress != NULL) {
337
12.1M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
338
12.1M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
339
12.1M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
340
12.1M
  }
341
342
  /* Process some data */
343
12.1M
  row_ctr = 0;
344
12.1M
  if (cinfo->main->_process_data == NULL)
345
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
346
12.1M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
347
12.1M
  cinfo->output_scanline += row_ctr;
348
12.1M
  return row_ctr;
349
#else
350
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
351
  return 0;
352
#endif
353
12.1M
}
jpeg12_read_scanlines
Line
Count
Source
321
4.44M
{
322
4.44M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
323
4.44M
  JDIMENSION row_ctr;
324
325
4.44M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
326
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
327
328
4.44M
  if (cinfo->global_state != DSTATE_SCANNING)
329
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
330
4.44M
  if (cinfo->output_scanline >= cinfo->output_height) {
331
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
332
0
    return 0;
333
0
  }
334
335
  /* Call progress monitor hook if present */
336
4.44M
  if (cinfo->progress != NULL) {
337
4.44M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
338
4.44M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
339
4.44M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
340
4.44M
  }
341
342
  /* Process some data */
343
4.44M
  row_ctr = 0;
344
4.44M
  if (cinfo->main->_process_data == NULL)
345
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
346
4.44M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
347
4.44M
  cinfo->output_scanline += row_ctr;
348
4.44M
  return row_ctr;
349
#else
350
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
351
  return 0;
352
#endif
353
4.44M
}
jpeg16_read_scanlines
Line
Count
Source
321
240k
{
322
240k
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
323
240k
  JDIMENSION row_ctr;
324
325
240k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
326
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
327
328
240k
  if (cinfo->global_state != DSTATE_SCANNING)
329
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
330
240k
  if (cinfo->output_scanline >= cinfo->output_height) {
331
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
332
0
    return 0;
333
0
  }
334
335
  /* Call progress monitor hook if present */
336
240k
  if (cinfo->progress != NULL) {
337
240k
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
338
240k
    cinfo->progress->pass_limit = (long)cinfo->output_height;
339
240k
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
340
240k
  }
341
342
  /* Process some data */
343
240k
  row_ctr = 0;
344
240k
  if (cinfo->main->_process_data == NULL)
345
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
346
240k
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
347
240k
  cinfo->output_scanline += row_ctr;
348
240k
  return row_ctr;
349
#else
350
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
351
  return 0;
352
#endif
353
240k
}
jpeg_read_scanlines
Line
Count
Source
321
7.49M
{
322
7.49M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
323
7.49M
  JDIMENSION row_ctr;
324
325
7.49M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
326
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
327
328
7.49M
  if (cinfo->global_state != DSTATE_SCANNING)
329
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
330
7.49M
  if (cinfo->output_scanline >= cinfo->output_height) {
331
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
332
0
    return 0;
333
0
  }
334
335
  /* Call progress monitor hook if present */
336
7.49M
  if (cinfo->progress != NULL) {
337
7.49M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
338
7.49M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
339
7.49M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
340
7.49M
  }
341
342
  /* Process some data */
343
7.49M
  row_ctr = 0;
344
7.49M
  if (cinfo->main->_process_data == NULL)
345
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
346
7.49M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
347
7.49M
  cinfo->output_scanline += row_ctr;
348
7.49M
  return row_ctr;
349
#else
350
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
351
  return 0;
352
#endif
353
7.49M
}
354
355
356
#if BITS_IN_JSAMPLE != 16
357
358
/* Dummy color convert function used by _jpeg_skip_scanlines() */
359
LOCAL(void)
360
noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
361
             JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)
362
0
{
363
0
}
364
365
366
/* Dummy quantize function used by _jpeg_skip_scanlines() */
367
LOCAL(void)
368
noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
369
              _JSAMPARRAY output_buf, int num_rows)
370
0
{
371
0
}
372
373
374
/*
375
 * In some cases, it is best to call _jpeg_read_scanlines() and discard the
376
 * output, rather than skipping the scanlines, because this allows us to
377
 * maintain the internal state of the context-based upsampler.  In these cases,
378
 * we set up and tear down a dummy color converter in order to avoid valgrind
379
 * errors and to achieve the best possible performance.
380
 */
381
382
LOCAL(void)
383
read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
384
0
{
385
0
  JDIMENSION n;
386
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
387
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
388
0
#endif
389
0
  _JSAMPLE dummy_sample[1] = { 0 };
390
0
  _JSAMPROW dummy_row = dummy_sample;
391
0
  _JSAMPARRAY scanlines = NULL;
392
0
  void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
393
0
                         JDIMENSION input_row, _JSAMPARRAY output_buf,
394
0
                         int num_rows) = NULL;
395
0
  void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
396
0
                          _JSAMPARRAY output_buf, int num_rows) = NULL;
397
398
0
  if (cinfo->cconvert && cinfo->cconvert->_color_convert) {
399
0
    color_convert = cinfo->cconvert->_color_convert;
400
0
    cinfo->cconvert->_color_convert = noop_convert;
401
    /* This just prevents UBSan from complaining about adding 0 to a NULL
402
     * pointer.  The pointer isn't actually used.
403
     */
404
0
    scanlines = &dummy_row;
405
0
  }
406
407
0
  if (cinfo->cquantize && cinfo->cquantize->_color_quantize) {
408
0
    color_quantize = cinfo->cquantize->_color_quantize;
409
0
    cinfo->cquantize->_color_quantize = noop_quantize;
410
0
  }
411
412
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
413
0
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
414
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
415
0
    scanlines = &upsample->spare_row;
416
0
  }
417
0
#endif
418
419
0
  for (n = 0; n < num_lines; n++)
420
0
    _jpeg_read_scanlines(cinfo, scanlines, 1);
421
422
0
  if (color_convert)
423
0
    cinfo->cconvert->_color_convert = color_convert;
424
425
0
  if (color_quantize)
426
0
    cinfo->cquantize->_color_quantize = color_quantize;
427
0
}
428
429
430
/*
431
 * Called by _jpeg_skip_scanlines().  This partially skips a decompress block
432
 * by incrementing the rowgroup counter.
433
 */
434
435
LOCAL(void)
436
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
437
0
{
438
0
  JDIMENSION rows_left;
439
0
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
440
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
441
442
0
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
443
0
    read_and_discard_scanlines(cinfo, rows);
444
0
    return;
445
0
  }
446
447
  /* Increment the counter to the next row group after the skipped rows. */
448
0
  main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
449
450
  /* Partially skipping a row group would involve modifying the internal state
451
   * of the upsampler, so read the remaining rows into a dummy buffer instead.
452
   */
453
0
  rows_left = rows % cinfo->max_v_samp_factor;
454
0
  cinfo->output_scanline += rows - rows_left;
455
456
0
  read_and_discard_scanlines(cinfo, rows_left);
457
0
}
458
459
/*
460
 * Skips some scanlines of data from the JPEG decompressor.
461
 *
462
 * The return value will be the number of lines actually skipped.  If skipping
463
 * num_lines would move beyond the end of the image, then the actual number of
464
 * lines remaining in the image is returned.  Otherwise, the return value will
465
 * be equal to num_lines.
466
 *
467
 * Refer to libjpeg.txt for more information.
468
 */
469
470
GLOBAL(JDIMENSION)
471
_jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
472
0
{
473
0
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
474
0
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
475
0
  my_master_ptr master = (my_master_ptr)cinfo->master;
476
0
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
477
0
  JDIMENSION i, x;
478
0
  int y;
479
0
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
480
0
  JDIMENSION lines_to_skip, lines_to_read;
481
482
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
483
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
484
485
0
  if (cinfo->master->lossless)
486
0
    ERREXIT(cinfo, JERR_NOTIMPL);
487
488
  /* Two-pass color quantization is not supported. */
489
0
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
490
0
    ERREXIT(cinfo, JERR_NOTIMPL);
491
492
0
  if (cinfo->global_state != DSTATE_SCANNING)
493
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
494
495
  /* Do not skip past the bottom of the image. */
496
0
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
497
0
      cinfo->output_height) {
498
0
    num_lines = cinfo->output_height - cinfo->output_scanline;
499
0
    cinfo->output_scanline = cinfo->output_height;
500
0
    (*cinfo->inputctl->finish_input_pass) (cinfo);
501
0
    cinfo->inputctl->eoi_reached = TRUE;
502
0
    return num_lines;
503
0
  }
504
505
0
  if (num_lines == 0)
506
0
    return 0;
507
508
0
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
509
0
  lines_left_in_iMCU_row =
510
0
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
511
0
    lines_per_iMCU_row;
512
0
  lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
513
514
  /* Skip the lines remaining in the current iMCU row.  When upsampling
515
   * requires context rows, we need the previous and next rows in order to read
516
   * the current row.  This adds some complexity.
517
   */
518
0
  if (cinfo->upsample->need_context_rows) {
519
    /* If the skipped lines would not move us past the current iMCU row, we
520
     * read the lines and ignore them.  There might be a faster way of doing
521
     * this, but we are facing increasing complexity for diminishing returns.
522
     * The increasing complexity would be a by-product of meddling with the
523
     * state machine used to skip context rows.  Near the end of an iMCU row,
524
     * the next iMCU row may have already been entropy-decoded.  In this unique
525
     * case, we will read the next iMCU row if we cannot skip past it as well.
526
     */
527
0
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
528
0
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
529
0
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
530
0
      read_and_discard_scanlines(cinfo, num_lines);
531
0
      return num_lines;
532
0
    }
533
534
    /* If the next iMCU row has already been entropy-decoded, make sure that
535
     * we do not skip too far.
536
     */
537
0
    if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
538
0
      cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
539
0
      lines_after_iMCU_row -= lines_per_iMCU_row;
540
0
    } else {
541
0
      cinfo->output_scanline += lines_left_in_iMCU_row;
542
0
    }
543
544
    /* If we have just completed the first block, adjust the buffer pointers */
545
0
    if (main_ptr->iMCU_row_ctr == 0 ||
546
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
547
0
      set_wraparound_pointers(cinfo);
548
0
    main_ptr->buffer_full = FALSE;
549
0
    main_ptr->rowgroup_ctr = 0;
550
0
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
551
0
    if (!master->using_merged_upsample) {
552
0
      upsample->next_row_out = cinfo->max_v_samp_factor;
553
0
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
554
0
    }
555
0
  }
556
557
  /* Skipping is much simpler when context rows are not required. */
558
0
  else {
559
0
    if (num_lines < lines_left_in_iMCU_row) {
560
0
      increment_simple_rowgroup_ctr(cinfo, num_lines);
561
0
      return num_lines;
562
0
    } else {
563
0
      cinfo->output_scanline += lines_left_in_iMCU_row;
564
0
      main_ptr->buffer_full = FALSE;
565
0
      main_ptr->rowgroup_ctr = 0;
566
0
      if (!master->using_merged_upsample) {
567
0
        upsample->next_row_out = cinfo->max_v_samp_factor;
568
0
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
569
0
      }
570
0
    }
571
0
  }
572
573
  /* Calculate how many full iMCU rows we can skip. */
574
0
  if (cinfo->upsample->need_context_rows)
575
0
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
576
0
                    lines_per_iMCU_row;
577
0
  else
578
0
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
579
0
                    lines_per_iMCU_row;
580
  /* Calculate the number of lines that remain to be skipped after skipping all
581
   * of the full iMCU rows that we can.  We will not read these lines unless we
582
   * have to.
583
   */
584
0
  lines_to_read = lines_after_iMCU_row - lines_to_skip;
585
586
  /* For images requiring multiple scans (progressive, non-interleaved, etc.),
587
   * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
588
   * that the input data source is non-suspending.  This makes skipping easy.
589
   */
590
0
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
591
0
    if (cinfo->upsample->need_context_rows) {
592
0
      cinfo->output_scanline += lines_to_skip;
593
0
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
594
0
      main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
595
      /* It is complex to properly move to the middle of a context block, so
596
       * read the remaining lines instead of skipping them.
597
       */
598
0
      read_and_discard_scanlines(cinfo, lines_to_read);
599
0
    } else {
600
0
      cinfo->output_scanline += lines_to_skip;
601
0
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
602
0
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
603
0
    }
604
0
    if (!master->using_merged_upsample)
605
0
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
606
0
    return num_lines;
607
0
  }
608
609
  /* Skip the iMCU rows that we can safely skip. */
610
0
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
611
0
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
612
0
      for (x = 0; x < cinfo->MCUs_per_row; x++) {
613
        /* Calling decode_mcu() with a NULL pointer causes it to discard the
614
         * decoded coefficients.  This is ~5% faster for large subsets, but
615
         * it's tough to tell a difference for smaller images.
616
         */
617
0
        if (!cinfo->entropy->insufficient_data)
618
0
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
619
0
        (*cinfo->entropy->decode_mcu) (cinfo, NULL);
620
0
      }
621
0
    }
622
0
    cinfo->input_iMCU_row++;
623
0
    cinfo->output_iMCU_row++;
624
0
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
625
0
      start_iMCU_row(cinfo);
626
0
    else
627
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
628
0
  }
629
0
  cinfo->output_scanline += lines_to_skip;
630
631
0
  if (cinfo->upsample->need_context_rows) {
632
    /* Context-based upsampling keeps track of iMCU rows. */
633
0
    main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
634
635
    /* It is complex to properly move to the middle of a context block, so
636
     * read the remaining lines instead of skipping them.
637
     */
638
0
    read_and_discard_scanlines(cinfo, lines_to_read);
639
0
  } else {
640
0
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
641
0
  }
642
643
  /* Since skipping lines involves skipping the upsampling step, the value of
644
   * "rows_to_go" will become invalid unless we set it here.  NOTE: This is a
645
   * bit odd, since "rows_to_go" seems to be redundantly keeping track of
646
   * output_scanline.
647
   */
648
0
  if (!master->using_merged_upsample)
649
0
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
650
651
  /* Always skip the requested number of lines. */
652
0
  return num_lines;
653
0
}
Unexecuted instantiation: jpeg12_skip_scanlines
Unexecuted instantiation: jpeg_skip_scanlines
654
655
/*
656
 * Alternate entry point to read raw data.
657
 * Processes exactly one iMCU row per call, unless suspended.
658
 */
659
660
GLOBAL(JDIMENSION)
661
_jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,
662
                    JDIMENSION max_lines)
663
2.26M
{
664
2.26M
  JDIMENSION lines_per_iMCU_row;
665
666
2.26M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
667
1.76k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
668
669
2.26M
  if (cinfo->master->lossless)
670
537
    ERREXIT(cinfo, JERR_NOTIMPL);
671
672
2.26M
  if (cinfo->global_state != DSTATE_RAW_OK)
673
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
674
2.26M
  if (cinfo->output_scanline >= cinfo->output_height) {
675
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
676
0
    return 0;
677
0
  }
678
679
  /* Call progress monitor hook if present */
680
2.26M
  if (cinfo->progress != NULL) {
681
2.26M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
682
2.26M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
683
2.26M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
684
2.26M
  }
685
686
  /* Verify that at least one iMCU row can be returned. */
687
2.26M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
688
2.26M
  if (max_lines < lines_per_iMCU_row)
689
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
690
691
  /* Decompress directly into user's buffer. */
692
2.26M
  if (cinfo->coef->_decompress_data == NULL)
693
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
694
2.26M
  if (!(*cinfo->coef->_decompress_data) (cinfo, data))
695
0
    return 0;                   /* suspension forced, can do nothing more */
696
697
  /* OK, we processed one iMCU row. */
698
2.26M
  cinfo->output_scanline += lines_per_iMCU_row;
699
2.26M
  return lines_per_iMCU_row;
700
2.26M
}
Unexecuted instantiation: jpeg12_read_raw_data
jpeg_read_raw_data
Line
Count
Source
663
2.26M
{
664
2.26M
  JDIMENSION lines_per_iMCU_row;
665
666
2.26M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
667
1.76k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
668
669
2.26M
  if (cinfo->master->lossless)
670
537
    ERREXIT(cinfo, JERR_NOTIMPL);
671
672
2.26M
  if (cinfo->global_state != DSTATE_RAW_OK)
673
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
674
2.26M
  if (cinfo->output_scanline >= cinfo->output_height) {
675
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
676
0
    return 0;
677
0
  }
678
679
  /* Call progress monitor hook if present */
680
2.26M
  if (cinfo->progress != NULL) {
681
2.26M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
682
2.26M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
683
2.26M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
684
2.26M
  }
685
686
  /* Verify that at least one iMCU row can be returned. */
687
2.26M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
688
2.26M
  if (max_lines < lines_per_iMCU_row)
689
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
690
691
  /* Decompress directly into user's buffer. */
692
2.26M
  if (cinfo->coef->_decompress_data == NULL)
693
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
694
2.26M
  if (!(*cinfo->coef->_decompress_data) (cinfo, data))
695
0
    return 0;                   /* suspension forced, can do nothing more */
696
697
  /* OK, we processed one iMCU row. */
698
2.26M
  cinfo->output_scanline += lines_per_iMCU_row;
699
2.26M
  return lines_per_iMCU_row;
700
2.26M
}
701
702
#endif /* BITS_IN_JSAMPLE != 16 */
703
704
705
#if BITS_IN_JSAMPLE == 8
706
707
/* Additional entry points for buffered-image mode. */
708
709
#ifdef D_MULTISCAN_FILES_SUPPORTED
710
711
/*
712
 * Initialize for an output pass in buffered-image mode.
713
 */
714
715
GLOBAL(boolean)
716
jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
717
0
{
718
0
  if (cinfo->global_state != DSTATE_BUFIMAGE &&
719
0
      cinfo->global_state != DSTATE_PRESCAN)
720
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
721
  /* Limit scan number to valid range */
722
0
  if (scan_number <= 0)
723
0
    scan_number = 1;
724
0
  if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
725
0
    scan_number = cinfo->input_scan_number;
726
0
  cinfo->output_scan_number = scan_number;
727
  /* Perform any dummy output passes, and set up for the real pass */
728
0
  return output_pass_setup(cinfo);
729
0
}
730
731
732
/*
733
 * Finish up after an output pass in buffered-image mode.
734
 *
735
 * Returns FALSE if suspended.  The return value need be inspected only if
736
 * a suspending data source is used.
737
 */
738
739
GLOBAL(boolean)
740
jpeg_finish_output(j_decompress_ptr cinfo)
741
0
{
742
0
  if ((cinfo->global_state == DSTATE_SCANNING ||
743
0
       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
744
    /* Terminate this pass. */
745
    /* We do not require the whole pass to have been completed. */
746
0
    (*cinfo->master->finish_output_pass) (cinfo);
747
0
    cinfo->global_state = DSTATE_BUFPOST;
748
0
  } else if (cinfo->global_state != DSTATE_BUFPOST) {
749
    /* BUFPOST = repeat call after a suspension, anything else is error */
750
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
751
0
  }
752
  /* Read markers looking for SOS or EOI */
753
0
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
754
0
         !cinfo->inputctl->eoi_reached) {
755
0
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
756
0
      return FALSE;             /* Suspend, come back later */
757
0
  }
758
0
  cinfo->global_state = DSTATE_BUFIMAGE;
759
0
  return TRUE;
760
0
}
761
762
#endif /* D_MULTISCAN_FILES_SUPPORTED */
763
764
#endif /* BITS_IN_JSAMPLE == 8 */