Coverage Report

Created: 2026-08-31 06:22

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