Coverage Report

Created: 2026-09-13 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/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
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
170k
{
54
170k
  if (cinfo->global_state == DSTATE_READY) {
55
    /* First call: initialize master control, select active modules */
56
170k
    jinit_master_decompress(cinfo);
57
170k
    if (cinfo->buffered_image) {
58
      /* No more work here; expecting jpeg_start_output next */
59
24.4k
      cinfo->global_state = DSTATE_BUFIMAGE;
60
24.4k
      return TRUE;
61
24.4k
    }
62
146k
    cinfo->global_state = DSTATE_PRELOAD;
63
146k
  }
64
146k
  if (cinfo->global_state == DSTATE_PRELOAD) {
65
    /* If file has multiple scans, absorb them all into the coef buffer */
66
88.4k
    if (cinfo->inputctl->has_multiple_scans) {
67
59.2k
#ifdef D_MULTISCAN_FILES_SUPPORTED
68
347M
      for (;;) {
69
347M
        int retcode;
70
        /* Call progress monitor hook if present */
71
347M
        if (cinfo->progress != NULL)
72
347M
          (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
73
        /* Absorb some more input */
74
347M
        retcode = (*cinfo->inputctl->consume_input) (cinfo);
75
347M
        if (retcode == JPEG_SUSPENDED)
76
0
          return FALSE;
77
347M
        if (retcode == JPEG_REACHED_EOI)
78
48.1k
          break;
79
        /* Advance progress counter if appropriate */
80
347M
        if (cinfo->progress != NULL &&
81
347M
            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
82
347M
          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
83
            /* jdmaster underestimated number of scans; ratchet up one scan */
84
186k
            cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
85
186k
          }
86
347M
        }
87
347M
      }
88
#else
89
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90
#endif /* D_MULTISCAN_FILES_SUPPORTED */
91
59.2k
    }
92
88.4k
    cinfo->output_scan_number = cinfo->input_scan_number;
93
88.4k
  } 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
146k
  return output_pass_setup(cinfo);
97
146k
}
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
20.4k
{
111
20.4k
  if (cinfo->global_state != DSTATE_PRESCAN) {
112
    /* First call: do pass setup */
113
20.4k
    (*cinfo->master->prepare_for_output_pass) (cinfo);
114
20.4k
    cinfo->output_scanline = 0;
115
20.4k
    cinfo->global_state = DSTATE_PRESCAN;
116
20.4k
  }
117
  /* Loop over any required dummy passes */
118
20.4k
  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
20.4k
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
168
20.4k
  return TRUE;
169
20.4k
}
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
4.33k
{
189
4.33k
  int ci, align, orig_downsampled_width;
190
4.33k
  JDIMENSION input_xoffset;
191
4.33k
  boolean reinit_upsampler = FALSE;
192
4.33k
  jpeg_component_info *compptr;
193
4.33k
#ifdef UPSAMPLE_MERGING_SUPPORTED
194
4.33k
  my_master_ptr master = (my_master_ptr)cinfo->master;
195
4.33k
#endif
196
197
4.33k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
198
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
199
200
4.33k
  if (cinfo->master->lossless || cinfo->raw_data_out)
201
0
    ERREXIT(cinfo, JERR_NOTIMPL);
202
203
4.33k
  if ((cinfo->global_state != DSTATE_SCANNING &&
204
4.33k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
205
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
206
207
4.33k
  if (!xoffset || !width)
208
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
209
210
  /* xoffset and width must fall within the output image dimensions. */
211
4.33k
  if (*width == 0 ||
212
4.33k
      (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
4.33k
  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
4.33k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
238
3.96k
    align = cinfo->_min_DCT_scaled_size;
239
372
  else
240
372
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
241
242
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
243
4.33k
  input_xoffset = *xoffset;
244
4.33k
  *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
4.33k
  *width = *width + input_xoffset - *xoffset;
252
4.33k
  cinfo->output_width = *width;
253
4.33k
#ifdef UPSAMPLE_MERGING_SUPPORTED
254
4.33k
  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
4.33k
#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
4.33k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
265
4.33k
  cinfo->master->last_iMCU_col =
266
4.33k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
267
4.33k
                              (long)align) - 1;
268
269
9.47k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
270
5.14k
       ci++, compptr++) {
271
5.14k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
272
3.96k
              1 : compptr->h_samp_factor;
273
274
    /* Set downsampled_width to the new output width. */
275
5.14k
    orig_downsampled_width = compptr->downsampled_width;
276
5.14k
    compptr->downsampled_width =
277
5.14k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
278
5.14k
                                (long)(compptr->h_samp_factor *
279
5.14k
                                       compptr->_DCT_scaled_size),
280
5.14k
                                (long)(cinfo->max_h_samp_factor *
281
5.14k
                                       cinfo->_min_DCT_scaled_size));
282
5.14k
    if (compptr->downsampled_width < 2 &&
283
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
284
0
        !master->using_merged_upsample &&
285
0
#endif
286
0
        orig_downsampled_width >= 2)
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
5.14k
    cinfo->master->first_MCU_col[ci] =
293
5.14k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
294
5.14k
    cinfo->master->last_MCU_col[ci] =
295
5.14k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
296
5.14k
                                (long)align) - 1;
297
5.14k
  }
298
299
4.33k
  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
4.33k
}
jpeg12_crop_scanline
Line
Count
Source
188
2.08k
{
189
2.08k
  int ci, align, orig_downsampled_width;
190
2.08k
  JDIMENSION input_xoffset;
191
2.08k
  boolean reinit_upsampler = FALSE;
192
2.08k
  jpeg_component_info *compptr;
193
2.08k
#ifdef UPSAMPLE_MERGING_SUPPORTED
194
2.08k
  my_master_ptr master = (my_master_ptr)cinfo->master;
195
2.08k
#endif
196
197
2.08k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
198
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
199
200
2.08k
  if (cinfo->master->lossless || cinfo->raw_data_out)
201
0
    ERREXIT(cinfo, JERR_NOTIMPL);
202
203
2.08k
  if ((cinfo->global_state != DSTATE_SCANNING &&
204
2.08k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
205
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
206
207
2.08k
  if (!xoffset || !width)
208
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
209
210
  /* xoffset and width must fall within the output image dimensions. */
211
2.08k
  if (*width == 0 ||
212
2.08k
      (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
2.08k
  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
2.08k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
238
1.88k
    align = cinfo->_min_DCT_scaled_size;
239
204
  else
240
204
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
241
242
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
243
2.08k
  input_xoffset = *xoffset;
244
2.08k
  *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
2.08k
  *width = *width + input_xoffset - *xoffset;
252
2.08k
  cinfo->output_width = *width;
253
2.08k
#ifdef UPSAMPLE_MERGING_SUPPORTED
254
2.08k
  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
2.08k
#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
2.08k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
265
2.08k
  cinfo->master->last_iMCU_col =
266
2.08k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
267
2.08k
                              (long)align) - 1;
268
269
4.61k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
270
2.53k
       ci++, compptr++) {
271
2.53k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
272
1.88k
              1 : compptr->h_samp_factor;
273
274
    /* Set downsampled_width to the new output width. */
275
2.53k
    orig_downsampled_width = compptr->downsampled_width;
276
2.53k
    compptr->downsampled_width =
277
2.53k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
278
2.53k
                                (long)(compptr->h_samp_factor *
279
2.53k
                                       compptr->_DCT_scaled_size),
280
2.53k
                                (long)(cinfo->max_h_samp_factor *
281
2.53k
                                       cinfo->_min_DCT_scaled_size));
282
2.53k
    if (compptr->downsampled_width < 2 &&
283
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
284
0
        !master->using_merged_upsample &&
285
0
#endif
286
0
        orig_downsampled_width >= 2)
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
2.53k
    cinfo->master->first_MCU_col[ci] =
293
2.53k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
294
2.53k
    cinfo->master->last_MCU_col[ci] =
295
2.53k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
296
2.53k
                                (long)align) - 1;
297
2.53k
  }
298
299
2.08k
  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
2.08k
}
jpeg_crop_scanline
Line
Count
Source
188
2.24k
{
189
2.24k
  int ci, align, orig_downsampled_width;
190
2.24k
  JDIMENSION input_xoffset;
191
2.24k
  boolean reinit_upsampler = FALSE;
192
2.24k
  jpeg_component_info *compptr;
193
2.24k
#ifdef UPSAMPLE_MERGING_SUPPORTED
194
2.24k
  my_master_ptr master = (my_master_ptr)cinfo->master;
195
2.24k
#endif
196
197
2.24k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
198
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
199
200
2.24k
  if (cinfo->master->lossless || cinfo->raw_data_out)
201
0
    ERREXIT(cinfo, JERR_NOTIMPL);
202
203
2.24k
  if ((cinfo->global_state != DSTATE_SCANNING &&
204
2.24k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
205
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
206
207
2.24k
  if (!xoffset || !width)
208
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
209
210
  /* xoffset and width must fall within the output image dimensions. */
211
2.24k
  if (*width == 0 ||
212
2.24k
      (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
2.24k
  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
2.24k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
238
2.07k
    align = cinfo->_min_DCT_scaled_size;
239
168
  else
240
168
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
241
242
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
243
2.24k
  input_xoffset = *xoffset;
244
2.24k
  *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
2.24k
  *width = *width + input_xoffset - *xoffset;
252
2.24k
  cinfo->output_width = *width;
253
2.24k
#ifdef UPSAMPLE_MERGING_SUPPORTED
254
2.24k
  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
2.24k
#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
2.24k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
265
2.24k
  cinfo->master->last_iMCU_col =
266
2.24k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
267
2.24k
                              (long)align) - 1;
268
269
4.85k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
270
2.61k
       ci++, compptr++) {
271
2.61k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
272
2.07k
              1 : compptr->h_samp_factor;
273
274
    /* Set downsampled_width to the new output width. */
275
2.61k
    orig_downsampled_width = compptr->downsampled_width;
276
2.61k
    compptr->downsampled_width =
277
2.61k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
278
2.61k
                                (long)(compptr->h_samp_factor *
279
2.61k
                                       compptr->_DCT_scaled_size),
280
2.61k
                                (long)(cinfo->max_h_samp_factor *
281
2.61k
                                       cinfo->_min_DCT_scaled_size));
282
2.61k
    if (compptr->downsampled_width < 2 &&
283
0
#ifdef UPSAMPLE_MERGING_SUPPORTED
284
0
        !master->using_merged_upsample &&
285
0
#endif
286
0
        orig_downsampled_width >= 2)
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
2.61k
    cinfo->master->first_MCU_col[ci] =
293
2.61k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
294
2.61k
    cinfo->master->last_MCU_col[ci] =
295
2.61k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
296
2.61k
                                (long)align) - 1;
297
2.61k
  }
298
299
2.24k
  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
2.24k
}
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
25.2M
{
326
25.2M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
327
25.2M
  JDIMENSION row_ctr;
328
329
25.2M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
330
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
331
332
25.2M
  if (cinfo->global_state != DSTATE_SCANNING)
333
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
334
25.2M
  if (cinfo->output_scanline >= cinfo->output_height) {
335
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
336
0
    return 0;
337
0
  }
338
339
  /* Call progress monitor hook if present */
340
25.2M
  if (cinfo->progress != NULL) {
341
25.2M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
342
25.2M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
343
25.2M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
344
25.2M
  }
345
346
  /* Process some data */
347
25.2M
  row_ctr = 0;
348
25.2M
  if (cinfo->main->_process_data == NULL)
349
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
350
25.2M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
351
25.2M
  cinfo->output_scanline += row_ctr;
352
25.2M
  return row_ctr;
353
#else
354
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
355
  return 0;
356
#endif
357
25.2M
}
jpeg12_read_scanlines
Line
Count
Source
325
8.99M
{
326
8.99M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
327
8.99M
  JDIMENSION row_ctr;
328
329
8.99M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
330
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
331
332
8.99M
  if (cinfo->global_state != DSTATE_SCANNING)
333
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
334
8.99M
  if (cinfo->output_scanline >= cinfo->output_height) {
335
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
336
0
    return 0;
337
0
  }
338
339
  /* Call progress monitor hook if present */
340
8.99M
  if (cinfo->progress != NULL) {
341
8.99M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
342
8.99M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
343
8.99M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
344
8.99M
  }
345
346
  /* Process some data */
347
8.99M
  row_ctr = 0;
348
8.99M
  if (cinfo->main->_process_data == NULL)
349
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
350
8.99M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
351
8.99M
  cinfo->output_scanline += row_ctr;
352
8.99M
  return row_ctr;
353
#else
354
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
355
  return 0;
356
#endif
357
8.99M
}
jpeg16_read_scanlines
Line
Count
Source
325
2.29M
{
326
2.29M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
327
2.29M
  JDIMENSION row_ctr;
328
329
2.29M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
330
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
331
332
2.29M
  if (cinfo->global_state != DSTATE_SCANNING)
333
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
334
2.29M
  if (cinfo->output_scanline >= cinfo->output_height) {
335
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
336
0
    return 0;
337
0
  }
338
339
  /* Call progress monitor hook if present */
340
2.29M
  if (cinfo->progress != NULL) {
341
2.29M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
342
2.29M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
343
2.29M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
344
2.29M
  }
345
346
  /* Process some data */
347
2.29M
  row_ctr = 0;
348
2.29M
  if (cinfo->main->_process_data == NULL)
349
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
350
2.29M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
351
2.29M
  cinfo->output_scanline += row_ctr;
352
2.29M
  return row_ctr;
353
#else
354
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
355
  return 0;
356
#endif
357
2.29M
}
jpeg_read_scanlines
Line
Count
Source
325
13.9M
{
326
13.9M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
327
13.9M
  JDIMENSION row_ctr;
328
329
13.9M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
330
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
331
332
13.9M
  if (cinfo->global_state != DSTATE_SCANNING)
333
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
334
13.9M
  if (cinfo->output_scanline >= cinfo->output_height) {
335
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
336
0
    return 0;
337
0
  }
338
339
  /* Call progress monitor hook if present */
340
13.9M
  if (cinfo->progress != NULL) {
341
13.9M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
342
13.9M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
343
13.9M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
344
13.9M
  }
345
346
  /* Process some data */
347
13.9M
  row_ctr = 0;
348
13.9M
  if (cinfo->main->_process_data == NULL)
349
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
350
13.9M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
351
13.9M
  cinfo->output_scanline += row_ctr;
352
13.9M
  return row_ctr;
353
#else
354
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
355
  return 0;
356
#endif
357
13.9M
}
358
359
360
#if BITS_IN_JSAMPLE != 16
361
362
/* Dummy color convert function used by _jpeg_skip_scanlines() */
363
LOCAL(void)
364
noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
365
             JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)
366
80
{
367
80
}
368
369
370
/* Dummy quantize function used by _jpeg_skip_scanlines() */
371
LOCAL(void)
372
noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
373
              _JSAMPARRAY output_buf, int num_rows)
374
0
{
375
0
}
376
377
378
/*
379
 * In some cases, it is best to call _jpeg_read_scanlines() and discard the
380
 * output, rather than skipping the scanlines, because this allows us to
381
 * maintain the internal state of the context-based upsampler.  In these cases,
382
 * we set up and tear down a dummy color converter in order to avoid valgrind
383
 * errors and to achieve the best possible performance.
384
 */
385
386
LOCAL(void)
387
read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
388
68
{
389
68
  JDIMENSION n;
390
68
#ifdef UPSAMPLE_MERGING_SUPPORTED
391
68
  my_master_ptr master = (my_master_ptr)cinfo->master;
392
68
#endif
393
68
  _JSAMPLE dummy_sample[1] = { 0 };
394
68
  _JSAMPROW dummy_row = dummy_sample;
395
68
  _JSAMPARRAY scanlines = NULL;
396
68
  void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
397
68
                         JDIMENSION input_row, _JSAMPARRAY output_buf,
398
68
                         int num_rows) = NULL;
399
68
  void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
400
68
                          _JSAMPARRAY output_buf, int num_rows) = NULL;
401
402
68
  if (cinfo->cconvert &&
403
68
#ifdef UPSAMPLE_MERGING_SUPPORTED
404
68
      !master->using_merged_upsample &&
405
68
#endif
406
68
      cinfo->cconvert->_color_convert) {
407
68
    color_convert = cinfo->cconvert->_color_convert;
408
68
    cinfo->cconvert->_color_convert = noop_convert;
409
    /* This just prevents UBSan from complaining about adding 0 to a NULL
410
     * pointer.  The pointer isn't actually used.
411
     */
412
68
    scanlines = &dummy_row;
413
68
  }
414
415
68
  if (cinfo->quantize_colors && cinfo->cquantize &&
416
0
      cinfo->cquantize->_color_quantize) {
417
0
    color_quantize = cinfo->cquantize->_color_quantize;
418
0
    cinfo->cquantize->_color_quantize = noop_quantize;
419
0
  }
420
421
68
#ifdef UPSAMPLE_MERGING_SUPPORTED
422
68
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
423
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
424
0
    scanlines = &upsample->spare_row;
425
0
  }
426
68
#endif
427
428
148
  for (n = 0; n < num_lines; n++)
429
80
    _jpeg_read_scanlines(cinfo, scanlines, 1);
430
431
68
  if (color_convert)
432
68
    cinfo->cconvert->_color_convert = color_convert;
433
434
68
  if (color_quantize)
435
0
    cinfo->cquantize->_color_quantize = color_quantize;
436
68
}
437
438
439
/*
440
 * Called by _jpeg_skip_scanlines().  This partially skips a decompress block
441
 * by incrementing the rowgroup counter.
442
 */
443
444
LOCAL(void)
445
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
446
63
{
447
63
  JDIMENSION rows_left;
448
63
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
449
450
63
  if (!cinfo->upsample->need_context_rows && cinfo->max_v_samp_factor != 1) {
451
0
    read_and_discard_scanlines(cinfo, rows);
452
0
    return;
453
0
  }
454
455
  /* Increment the counter to the next row group after the skipped rows. */
456
63
  main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
457
458
  /* Partially skipping a row group would involve modifying the internal state
459
   * of the upsampler, so read the remaining rows into a dummy buffer instead.
460
   */
461
63
  rows_left = rows % cinfo->max_v_samp_factor;
462
63
  cinfo->output_scanline += rows - rows_left;
463
464
63
  read_and_discard_scanlines(cinfo, rows_left);
465
63
}
466
467
/*
468
 * Skips some scanlines of data from the JPEG decompressor.
469
 *
470
 * The return value will be the number of lines actually skipped.  If skipping
471
 * num_lines would move beyond the end of the image, then the actual number of
472
 * lines remaining in the image is returned.  Otherwise, the return value will
473
 * be equal to num_lines.
474
 *
475
 * Refer to libjpeg.txt for more information.
476
 */
477
478
GLOBAL(JDIMENSION)
479
_jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
480
94.0k
{
481
94.0k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
482
94.0k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
483
94.0k
  my_master_ptr master = (my_master_ptr)cinfo->master;
484
94.0k
  JDIMENSION i, x;
485
94.0k
  int y;
486
94.0k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
487
94.0k
  JDIMENSION lines_to_skip, lines_to_read;
488
489
94.0k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
490
177
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
491
492
94.0k
  if (cinfo->master->lossless)
493
0
    ERREXIT(cinfo, JERR_NOTIMPL);
494
495
  /* Two-pass color quantization is not supported. */
496
94.0k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
497
0
    ERREXIT(cinfo, JERR_NOTIMPL);
498
499
94.0k
  if (cinfo->global_state != DSTATE_SCANNING)
500
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
501
502
  /* Do not skip past the bottom of the image. */
503
94.0k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
504
94.0k
      cinfo->output_height) {
505
6.67k
    num_lines = cinfo->output_height - cinfo->output_scanline;
506
6.67k
    cinfo->output_scanline = cinfo->output_height;
507
6.67k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
508
6.67k
    cinfo->inputctl->eoi_reached = TRUE;
509
6.67k
    return num_lines;
510
6.67k
  }
511
512
87.4k
  if (num_lines == 0)
513
0
    return 0;
514
515
87.4k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
516
87.4k
  lines_left_in_iMCU_row =
517
87.4k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
518
87.4k
    lines_per_iMCU_row;
519
87.4k
  lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
520
521
  /* Skip the lines remaining in the current iMCU row.  When upsampling
522
   * requires context rows, we need the previous and next rows in order to read
523
   * the current row.  This adds some complexity.
524
   */
525
87.4k
  if (cinfo->upsample->need_context_rows) {
526
    /* If the skipped lines would not move us past the current iMCU row, we
527
     * read the lines and ignore them.  There might be a faster way of doing
528
     * this, but we are facing increasing complexity for diminishing returns.
529
     * The increasing complexity would be a by-product of meddling with the
530
     * state machine used to skip context rows.  Near the end of an iMCU row,
531
     * the next iMCU row may have already been entropy-decoded.  In this unique
532
     * case, we will read the next iMCU row if we cannot skip past it as well.
533
     */
534
39.1k
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
535
34.1k
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
536
16.9k
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
537
16.9k
      read_and_discard_scanlines(cinfo, num_lines);
538
16.9k
      return num_lines;
539
16.9k
    }
540
541
    /* If the next iMCU row has already been entropy-decoded, make sure that
542
     * we do not skip too far.
543
     */
544
22.2k
    if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
545
0
      cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
546
0
      lines_after_iMCU_row -= lines_per_iMCU_row;
547
22.2k
    } else {
548
22.2k
      cinfo->output_scanline += lines_left_in_iMCU_row;
549
22.2k
    }
550
551
    /* If we have just completed the first block, adjust the buffer pointers */
552
22.2k
    if (main_ptr->iMCU_row_ctr == 0 ||
553
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
554
22.2k
      set_wraparound_pointers(cinfo);
555
22.2k
    main_ptr->buffer_full = FALSE;
556
22.2k
    main_ptr->rowgroup_ctr = 0;
557
22.2k
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
558
22.2k
    if (!master->using_merged_upsample) {
559
22.2k
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
560
22.2k
      upsample->next_row_out = cinfo->max_v_samp_factor;
561
22.2k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
562
22.2k
    }
563
22.2k
  }
564
565
  /* Skipping is much simpler when context rows are not required. */
566
48.2k
  else {
567
48.2k
    if (num_lines < lines_left_in_iMCU_row) {
568
4.76k
      increment_simple_rowgroup_ctr(cinfo, num_lines);
569
4.76k
      return num_lines;
570
43.4k
    } else {
571
43.4k
      cinfo->output_scanline += lines_left_in_iMCU_row;
572
43.4k
      main_ptr->buffer_full = FALSE;
573
43.4k
      main_ptr->rowgroup_ctr = 0;
574
43.4k
#ifdef UPSAMPLE_MERGING_SUPPORTED
575
43.4k
      if (master->using_merged_upsample) {
576
0
        my_merged_upsample_ptr upsample =
577
0
          (my_merged_upsample_ptr)cinfo->upsample;
578
0
        upsample->spare_full = FALSE;
579
0
      } else
580
43.4k
#endif
581
43.4k
      {
582
43.4k
        my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
583
43.4k
        upsample->next_row_out = cinfo->max_v_samp_factor;
584
43.4k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
585
43.4k
      }
586
43.4k
    }
587
48.2k
  }
588
589
  /* Calculate how many full iMCU rows we can skip. */
590
65.7k
  if (cinfo->upsample->need_context_rows)
591
22.2k
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
592
22.2k
                    lines_per_iMCU_row;
593
43.4k
  else
594
43.4k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
595
43.4k
                    lines_per_iMCU_row;
596
  /* Calculate the number of lines that remain to be skipped after skipping all
597
   * of the full iMCU rows that we can.  We will not read these lines unless we
598
   * have to.
599
   */
600
65.7k
  lines_to_read = lines_after_iMCU_row - lines_to_skip;
601
602
  /* For images requiring multiple scans (progressive, non-interleaved, etc.),
603
   * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
604
   * that the input data source is non-suspending.  This makes skipping easy.
605
   */
606
65.7k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
607
51.7k
    if (cinfo->upsample->need_context_rows) {
608
21.1k
      cinfo->output_scanline += lines_to_skip;
609
21.1k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
610
21.1k
      main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
611
      /* It is complex to properly move to the middle of a context block, so
612
       * read the remaining lines instead of skipping them.
613
       */
614
21.1k
      read_and_discard_scanlines(cinfo, lines_to_read);
615
30.5k
    } else {
616
30.5k
      cinfo->output_scanline += lines_to_skip;
617
30.5k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
618
30.5k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
619
30.5k
    }
620
51.7k
#ifdef UPSAMPLE_MERGING_SUPPORTED
621
51.7k
    if (master->using_merged_upsample) {
622
0
      my_merged_upsample_ptr upsample =
623
0
        (my_merged_upsample_ptr)cinfo->upsample;
624
0
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
625
0
    } else
626
51.7k
#endif
627
51.7k
    {
628
51.7k
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
629
51.7k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
630
51.7k
    }
631
51.7k
    return num_lines;
632
51.7k
  }
633
634
  /* Skip the iMCU rows that we can safely skip. */
635
24.7k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
636
22.8k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
637
4.48M
      for (x = 0; x < cinfo->MCUs_per_row; x++) {
638
        /* Calling decode_mcu() with a NULL pointer causes it to discard the
639
         * decoded coefficients.  This is ~5% faster for large subsets, but
640
         * it's tough to tell a difference for smaller images.
641
         */
642
4.47M
        if (!cinfo->entropy->insufficient_data)
643
2.01M
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
644
4.47M
        (*cinfo->entropy->decode_mcu) (cinfo, NULL);
645
4.47M
      }
646
12.1k
    }
647
10.7k
    cinfo->input_iMCU_row++;
648
10.7k
    cinfo->output_iMCU_row++;
649
10.7k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
650
10.7k
      start_iMCU_row(cinfo);
651
0
    else
652
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
653
10.7k
  }
654
13.9k
  cinfo->output_scanline += lines_to_skip;
655
656
13.9k
  if (cinfo->upsample->need_context_rows) {
657
    /* Context-based upsampling keeps track of iMCU rows. */
658
1.08k
    main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
659
660
    /* It is complex to properly move to the middle of a context block, so
661
     * read the remaining lines instead of skipping them.
662
     */
663
1.08k
    read_and_discard_scanlines(cinfo, lines_to_read);
664
12.9k
  } else {
665
12.9k
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
666
12.9k
  }
667
668
  /* Since skipping lines involves skipping the upsampling step, the value of
669
   * "rows_to_go" will become invalid unless we set it here.  NOTE: This is a
670
   * bit odd, since "rows_to_go" seems to be redundantly keeping track of
671
   * output_scanline.
672
   */
673
13.9k
#ifdef UPSAMPLE_MERGING_SUPPORTED
674
13.9k
  if (master->using_merged_upsample) {
675
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
676
0
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
677
0
  } else
678
13.9k
#endif
679
13.9k
  {
680
13.9k
    my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
681
13.9k
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
682
13.9k
  }
683
684
  /* Always skip the requested number of lines. */
685
13.9k
  return num_lines;
686
65.7k
}
jpeg12_skip_scanlines
Line
Count
Source
480
4.14k
{
481
4.14k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
482
4.14k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
483
4.14k
  my_master_ptr master = (my_master_ptr)cinfo->master;
484
4.14k
  JDIMENSION i, x;
485
4.14k
  int y;
486
4.14k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
487
4.14k
  JDIMENSION lines_to_skip, lines_to_read;
488
489
4.14k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
490
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
491
492
4.14k
  if (cinfo->master->lossless)
493
0
    ERREXIT(cinfo, JERR_NOTIMPL);
494
495
  /* Two-pass color quantization is not supported. */
496
4.14k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
497
0
    ERREXIT(cinfo, JERR_NOTIMPL);
498
499
4.14k
  if (cinfo->global_state != DSTATE_SCANNING)
500
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
501
502
  /* Do not skip past the bottom of the image. */
503
4.14k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
504
4.14k
      cinfo->output_height) {
505
2.06k
    num_lines = cinfo->output_height - cinfo->output_scanline;
506
2.06k
    cinfo->output_scanline = cinfo->output_height;
507
2.06k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
508
2.06k
    cinfo->inputctl->eoi_reached = TRUE;
509
2.06k
    return num_lines;
510
2.06k
  }
511
512
2.08k
  if (num_lines == 0)
513
0
    return 0;
514
515
2.08k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
516
2.08k
  lines_left_in_iMCU_row =
517
2.08k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
518
2.08k
    lines_per_iMCU_row;
519
2.08k
  lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
520
521
  /* Skip the lines remaining in the current iMCU row.  When upsampling
522
   * requires context rows, we need the previous and next rows in order to read
523
   * the current row.  This adds some complexity.
524
   */
525
2.08k
  if (cinfo->upsample->need_context_rows) {
526
    /* If the skipped lines would not move us past the current iMCU row, we
527
     * read the lines and ignore them.  There might be a faster way of doing
528
     * this, but we are facing increasing complexity for diminishing returns.
529
     * The increasing complexity would be a by-product of meddling with the
530
     * state machine used to skip context rows.  Near the end of an iMCU row,
531
     * the next iMCU row may have already been entropy-decoded.  In this unique
532
     * case, we will read the next iMCU row if we cannot skip past it as well.
533
     */
534
52
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
535
52
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
536
0
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
537
0
      read_and_discard_scanlines(cinfo, num_lines);
538
0
      return num_lines;
539
0
    }
540
541
    /* If the next iMCU row has already been entropy-decoded, make sure that
542
     * we do not skip too far.
543
     */
544
52
    if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
545
0
      cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
546
0
      lines_after_iMCU_row -= lines_per_iMCU_row;
547
52
    } else {
548
52
      cinfo->output_scanline += lines_left_in_iMCU_row;
549
52
    }
550
551
    /* If we have just completed the first block, adjust the buffer pointers */
552
52
    if (main_ptr->iMCU_row_ctr == 0 ||
553
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
554
52
      set_wraparound_pointers(cinfo);
555
52
    main_ptr->buffer_full = FALSE;
556
52
    main_ptr->rowgroup_ctr = 0;
557
52
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
558
52
    if (!master->using_merged_upsample) {
559
52
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
560
52
      upsample->next_row_out = cinfo->max_v_samp_factor;
561
52
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
562
52
    }
563
52
  }
564
565
  /* Skipping is much simpler when context rows are not required. */
566
2.02k
  else {
567
2.02k
    if (num_lines < lines_left_in_iMCU_row) {
568
0
      increment_simple_rowgroup_ctr(cinfo, num_lines);
569
0
      return num_lines;
570
2.02k
    } else {
571
2.02k
      cinfo->output_scanline += lines_left_in_iMCU_row;
572
2.02k
      main_ptr->buffer_full = FALSE;
573
2.02k
      main_ptr->rowgroup_ctr = 0;
574
2.02k
#ifdef UPSAMPLE_MERGING_SUPPORTED
575
2.02k
      if (master->using_merged_upsample) {
576
0
        my_merged_upsample_ptr upsample =
577
0
          (my_merged_upsample_ptr)cinfo->upsample;
578
0
        upsample->spare_full = FALSE;
579
0
      } else
580
2.02k
#endif
581
2.02k
      {
582
2.02k
        my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
583
2.02k
        upsample->next_row_out = cinfo->max_v_samp_factor;
584
2.02k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
585
2.02k
      }
586
2.02k
    }
587
2.02k
  }
588
589
  /* Calculate how many full iMCU rows we can skip. */
590
2.08k
  if (cinfo->upsample->need_context_rows)
591
52
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
592
52
                    lines_per_iMCU_row;
593
2.02k
  else
594
2.02k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
595
2.02k
                    lines_per_iMCU_row;
596
  /* Calculate the number of lines that remain to be skipped after skipping all
597
   * of the full iMCU rows that we can.  We will not read these lines unless we
598
   * have to.
599
   */
600
2.08k
  lines_to_read = lines_after_iMCU_row - lines_to_skip;
601
602
  /* For images requiring multiple scans (progressive, non-interleaved, etc.),
603
   * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
604
   * that the input data source is non-suspending.  This makes skipping easy.
605
   */
606
2.08k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
607
1.16k
    if (cinfo->upsample->need_context_rows) {
608
10
      cinfo->output_scanline += lines_to_skip;
609
10
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
610
10
      main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
611
      /* It is complex to properly move to the middle of a context block, so
612
       * read the remaining lines instead of skipping them.
613
       */
614
10
      read_and_discard_scanlines(cinfo, lines_to_read);
615
1.15k
    } else {
616
1.15k
      cinfo->output_scanline += lines_to_skip;
617
1.15k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
618
1.15k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
619
1.15k
    }
620
1.16k
#ifdef UPSAMPLE_MERGING_SUPPORTED
621
1.16k
    if (master->using_merged_upsample) {
622
0
      my_merged_upsample_ptr upsample =
623
0
        (my_merged_upsample_ptr)cinfo->upsample;
624
0
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
625
0
    } else
626
1.16k
#endif
627
1.16k
    {
628
1.16k
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
629
1.16k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
630
1.16k
    }
631
1.16k
    return num_lines;
632
1.16k
  }
633
634
  /* Skip the iMCU rows that we can safely skip. */
635
2.28k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
636
3.53k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
637
305k
      for (x = 0; x < cinfo->MCUs_per_row; x++) {
638
        /* Calling decode_mcu() with a NULL pointer causes it to discard the
639
         * decoded coefficients.  This is ~5% faster for large subsets, but
640
         * it's tough to tell a difference for smaller images.
641
         */
642
302k
        if (!cinfo->entropy->insufficient_data)
643
119k
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
644
302k
        (*cinfo->entropy->decode_mcu) (cinfo, NULL);
645
302k
      }
646
2.15k
    }
647
1.37k
    cinfo->input_iMCU_row++;
648
1.37k
    cinfo->output_iMCU_row++;
649
1.37k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
650
1.37k
      start_iMCU_row(cinfo);
651
0
    else
652
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
653
1.37k
  }
654
914
  cinfo->output_scanline += lines_to_skip;
655
656
914
  if (cinfo->upsample->need_context_rows) {
657
    /* Context-based upsampling keeps track of iMCU rows. */
658
42
    main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
659
660
    /* It is complex to properly move to the middle of a context block, so
661
     * read the remaining lines instead of skipping them.
662
     */
663
42
    read_and_discard_scanlines(cinfo, lines_to_read);
664
872
  } else {
665
872
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
666
872
  }
667
668
  /* Since skipping lines involves skipping the upsampling step, the value of
669
   * "rows_to_go" will become invalid unless we set it here.  NOTE: This is a
670
   * bit odd, since "rows_to_go" seems to be redundantly keeping track of
671
   * output_scanline.
672
   */
673
914
#ifdef UPSAMPLE_MERGING_SUPPORTED
674
914
  if (master->using_merged_upsample) {
675
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
676
0
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
677
0
  } else
678
914
#endif
679
914
  {
680
914
    my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
681
914
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
682
914
  }
683
684
  /* Always skip the requested number of lines. */
685
914
  return num_lines;
686
2.08k
}
jpeg_skip_scanlines
Line
Count
Source
480
89.9k
{
481
89.9k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
482
89.9k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
483
89.9k
  my_master_ptr master = (my_master_ptr)cinfo->master;
484
89.9k
  JDIMENSION i, x;
485
89.9k
  int y;
486
89.9k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
487
89.9k
  JDIMENSION lines_to_skip, lines_to_read;
488
489
89.9k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
490
177
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
491
492
89.9k
  if (cinfo->master->lossless)
493
0
    ERREXIT(cinfo, JERR_NOTIMPL);
494
495
  /* Two-pass color quantization is not supported. */
496
89.9k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
497
0
    ERREXIT(cinfo, JERR_NOTIMPL);
498
499
89.9k
  if (cinfo->global_state != DSTATE_SCANNING)
500
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
501
502
  /* Do not skip past the bottom of the image. */
503
89.9k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
504
89.9k
      cinfo->output_height) {
505
4.60k
    num_lines = cinfo->output_height - cinfo->output_scanline;
506
4.60k
    cinfo->output_scanline = cinfo->output_height;
507
4.60k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
508
4.60k
    cinfo->inputctl->eoi_reached = TRUE;
509
4.60k
    return num_lines;
510
4.60k
  }
511
512
85.3k
  if (num_lines == 0)
513
0
    return 0;
514
515
85.3k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
516
85.3k
  lines_left_in_iMCU_row =
517
85.3k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
518
85.3k
    lines_per_iMCU_row;
519
85.3k
  lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
520
521
  /* Skip the lines remaining in the current iMCU row.  When upsampling
522
   * requires context rows, we need the previous and next rows in order to read
523
   * the current row.  This adds some complexity.
524
   */
525
85.3k
  if (cinfo->upsample->need_context_rows) {
526
    /* If the skipped lines would not move us past the current iMCU row, we
527
     * read the lines and ignore them.  There might be a faster way of doing
528
     * this, but we are facing increasing complexity for diminishing returns.
529
     * The increasing complexity would be a by-product of meddling with the
530
     * state machine used to skip context rows.  Near the end of an iMCU row,
531
     * the next iMCU row may have already been entropy-decoded.  In this unique
532
     * case, we will read the next iMCU row if we cannot skip past it as well.
533
     */
534
39.1k
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
535
34.1k
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
536
16.9k
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
537
16.9k
      read_and_discard_scanlines(cinfo, num_lines);
538
16.9k
      return num_lines;
539
16.9k
    }
540
541
    /* If the next iMCU row has already been entropy-decoded, make sure that
542
     * we do not skip too far.
543
     */
544
22.1k
    if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
545
0
      cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
546
0
      lines_after_iMCU_row -= lines_per_iMCU_row;
547
22.1k
    } else {
548
22.1k
      cinfo->output_scanline += lines_left_in_iMCU_row;
549
22.1k
    }
550
551
    /* If we have just completed the first block, adjust the buffer pointers */
552
22.1k
    if (main_ptr->iMCU_row_ctr == 0 ||
553
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
554
22.1k
      set_wraparound_pointers(cinfo);
555
22.1k
    main_ptr->buffer_full = FALSE;
556
22.1k
    main_ptr->rowgroup_ctr = 0;
557
22.1k
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
558
22.1k
    if (!master->using_merged_upsample) {
559
22.1k
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
560
22.1k
      upsample->next_row_out = cinfo->max_v_samp_factor;
561
22.1k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
562
22.1k
    }
563
22.1k
  }
564
565
  /* Skipping is much simpler when context rows are not required. */
566
46.2k
  else {
567
46.2k
    if (num_lines < lines_left_in_iMCU_row) {
568
4.76k
      increment_simple_rowgroup_ctr(cinfo, num_lines);
569
4.76k
      return num_lines;
570
41.4k
    } else {
571
41.4k
      cinfo->output_scanline += lines_left_in_iMCU_row;
572
41.4k
      main_ptr->buffer_full = FALSE;
573
41.4k
      main_ptr->rowgroup_ctr = 0;
574
41.4k
#ifdef UPSAMPLE_MERGING_SUPPORTED
575
41.4k
      if (master->using_merged_upsample) {
576
0
        my_merged_upsample_ptr upsample =
577
0
          (my_merged_upsample_ptr)cinfo->upsample;
578
0
        upsample->spare_full = FALSE;
579
0
      } else
580
41.4k
#endif
581
41.4k
      {
582
41.4k
        my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
583
41.4k
        upsample->next_row_out = cinfo->max_v_samp_factor;
584
41.4k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
585
41.4k
      }
586
41.4k
    }
587
46.2k
  }
588
589
  /* Calculate how many full iMCU rows we can skip. */
590
63.6k
  if (cinfo->upsample->need_context_rows)
591
22.1k
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
592
22.1k
                    lines_per_iMCU_row;
593
41.4k
  else
594
41.4k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
595
41.4k
                    lines_per_iMCU_row;
596
  /* Calculate the number of lines that remain to be skipped after skipping all
597
   * of the full iMCU rows that we can.  We will not read these lines unless we
598
   * have to.
599
   */
600
63.6k
  lines_to_read = lines_after_iMCU_row - lines_to_skip;
601
602
  /* For images requiring multiple scans (progressive, non-interleaved, etc.),
603
   * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
604
   * that the input data source is non-suspending.  This makes skipping easy.
605
   */
606
63.6k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
607
50.5k
    if (cinfo->upsample->need_context_rows) {
608
21.1k
      cinfo->output_scanline += lines_to_skip;
609
21.1k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
610
21.1k
      main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
611
      /* It is complex to properly move to the middle of a context block, so
612
       * read the remaining lines instead of skipping them.
613
       */
614
21.1k
      read_and_discard_scanlines(cinfo, lines_to_read);
615
29.4k
    } else {
616
29.4k
      cinfo->output_scanline += lines_to_skip;
617
29.4k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
618
29.4k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
619
29.4k
    }
620
50.5k
#ifdef UPSAMPLE_MERGING_SUPPORTED
621
50.5k
    if (master->using_merged_upsample) {
622
0
      my_merged_upsample_ptr upsample =
623
0
        (my_merged_upsample_ptr)cinfo->upsample;
624
0
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
625
0
    } else
626
50.5k
#endif
627
50.5k
    {
628
50.5k
      my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
629
50.5k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
630
50.5k
    }
631
50.5k
    return num_lines;
632
50.5k
  }
633
634
  /* Skip the iMCU rows that we can safely skip. */
635
22.4k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
636
19.3k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
637
4.18M
      for (x = 0; x < cinfo->MCUs_per_row; x++) {
638
        /* Calling decode_mcu() with a NULL pointer causes it to discard the
639
         * decoded coefficients.  This is ~5% faster for large subsets, but
640
         * it's tough to tell a difference for smaller images.
641
         */
642
4.17M
        if (!cinfo->entropy->insufficient_data)
643
1.89M
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
644
4.17M
        (*cinfo->entropy->decode_mcu) (cinfo, NULL);
645
4.17M
      }
646
9.94k
    }
647
9.37k
    cinfo->input_iMCU_row++;
648
9.37k
    cinfo->output_iMCU_row++;
649
9.37k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
650
9.37k
      start_iMCU_row(cinfo);
651
0
    else
652
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
653
9.37k
  }
654
13.0k
  cinfo->output_scanline += lines_to_skip;
655
656
13.0k
  if (cinfo->upsample->need_context_rows) {
657
    /* Context-based upsampling keeps track of iMCU rows. */
658
1.04k
    main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
659
660
    /* It is complex to properly move to the middle of a context block, so
661
     * read the remaining lines instead of skipping them.
662
     */
663
1.04k
    read_and_discard_scanlines(cinfo, lines_to_read);
664
12.0k
  } else {
665
12.0k
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
666
12.0k
  }
667
668
  /* Since skipping lines involves skipping the upsampling step, the value of
669
   * "rows_to_go" will become invalid unless we set it here.  NOTE: This is a
670
   * bit odd, since "rows_to_go" seems to be redundantly keeping track of
671
   * output_scanline.
672
   */
673
13.0k
#ifdef UPSAMPLE_MERGING_SUPPORTED
674
13.0k
  if (master->using_merged_upsample) {
675
0
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
676
0
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
677
0
  } else
678
13.0k
#endif
679
13.0k
  {
680
13.0k
    my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
681
13.0k
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
682
13.0k
  }
683
684
  /* Always skip the requested number of lines. */
685
13.0k
  return num_lines;
686
63.6k
}
687
688
/*
689
 * Alternate entry point to read raw data.
690
 * Processes exactly one iMCU row per call, unless suspended.
691
 */
692
693
GLOBAL(JDIMENSION)
694
_jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,
695
                    JDIMENSION max_lines)
696
7.04M
{
697
7.04M
  JDIMENSION lines_per_iMCU_row;
698
699
7.04M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
700
8.76k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
701
702
7.04M
  if (cinfo->master->lossless)
703
1.79k
    ERREXIT(cinfo, JERR_NOTIMPL);
704
705
7.04M
  if (cinfo->global_state != DSTATE_RAW_OK)
706
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
707
7.04M
  if (cinfo->output_scanline >= cinfo->output_height) {
708
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
709
0
    return 0;
710
0
  }
711
712
  /* Call progress monitor hook if present */
713
7.04M
  if (cinfo->progress != NULL) {
714
7.03M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
715
7.03M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
716
7.03M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
717
7.03M
  }
718
719
  /* Verify that at least one iMCU row can be returned. */
720
7.04M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
721
7.04M
  if (max_lines < lines_per_iMCU_row)
722
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
723
724
  /* Decompress directly into user's buffer. */
725
7.04M
  if (cinfo->coef->_decompress_data == NULL)
726
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
727
7.04M
  if (!(*cinfo->coef->_decompress_data) (cinfo, data))
728
0
    return 0;                   /* suspension forced, can do nothing more */
729
730
  /* OK, we processed one iMCU row. */
731
7.04M
  cinfo->output_scanline += lines_per_iMCU_row;
732
7.04M
  return lines_per_iMCU_row;
733
7.04M
}
Unexecuted instantiation: jpeg12_read_raw_data
jpeg_read_raw_data
Line
Count
Source
696
7.04M
{
697
7.04M
  JDIMENSION lines_per_iMCU_row;
698
699
7.04M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
700
8.76k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
701
702
7.04M
  if (cinfo->master->lossless)
703
1.79k
    ERREXIT(cinfo, JERR_NOTIMPL);
704
705
7.04M
  if (cinfo->global_state != DSTATE_RAW_OK)
706
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
707
7.04M
  if (cinfo->output_scanline >= cinfo->output_height) {
708
0
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
709
0
    return 0;
710
0
  }
711
712
  /* Call progress monitor hook if present */
713
7.04M
  if (cinfo->progress != NULL) {
714
7.03M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
715
7.03M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
716
7.03M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
717
7.03M
  }
718
719
  /* Verify that at least one iMCU row can be returned. */
720
7.04M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
721
7.04M
  if (max_lines < lines_per_iMCU_row)
722
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
723
724
  /* Decompress directly into user's buffer. */
725
7.04M
  if (cinfo->coef->_decompress_data == NULL)
726
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
727
7.04M
  if (!(*cinfo->coef->_decompress_data) (cinfo, data))
728
0
    return 0;                   /* suspension forced, can do nothing more */
729
730
  /* OK, we processed one iMCU row. */
731
7.04M
  cinfo->output_scanline += lines_per_iMCU_row;
732
7.04M
  return lines_per_iMCU_row;
733
7.04M
}
734
735
#endif /* BITS_IN_JSAMPLE != 16 */
736
737
738
#if BITS_IN_JSAMPLE == 8
739
740
/* Additional entry points for buffered-image mode. */
741
742
#ifdef D_MULTISCAN_FILES_SUPPORTED
743
744
/*
745
 * Initialize for an output pass in buffered-image mode.
746
 */
747
748
GLOBAL(boolean)
749
jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
750
51.0k
{
751
51.0k
  if (cinfo->global_state != DSTATE_BUFIMAGE &&
752
0
      cinfo->global_state != DSTATE_PRESCAN)
753
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
754
  /* Limit scan number to valid range */
755
51.0k
  if (scan_number <= 0)
756
0
    scan_number = 1;
757
51.0k
  if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
758
0
    scan_number = cinfo->input_scan_number;
759
51.0k
  cinfo->output_scan_number = scan_number;
760
  /* Perform any dummy output passes, and set up for the real pass */
761
51.0k
  return output_pass_setup(cinfo);
762
51.0k
}
763
764
765
/*
766
 * Finish up after an output pass in buffered-image mode.
767
 *
768
 * Returns FALSE if suspended.  The return value need be inspected only if
769
 * a suspending data source is used.
770
 */
771
772
GLOBAL(boolean)
773
jpeg_finish_output(j_decompress_ptr cinfo)
774
50.5k
{
775
50.5k
  if ((cinfo->global_state == DSTATE_SCANNING ||
776
50.5k
       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
777
    /* Terminate this pass. */
778
    /* We do not require the whole pass to have been completed. */
779
50.5k
    (*cinfo->master->finish_output_pass) (cinfo);
780
50.5k
    cinfo->global_state = DSTATE_BUFPOST;
781
50.5k
  } else if (cinfo->global_state != DSTATE_BUFPOST) {
782
    /* BUFPOST = repeat call after a suspension, anything else is error */
783
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
784
0
  }
785
  /* Read markers looking for SOS or EOI */
786
87.9k
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
787
46.5k
         !cinfo->inputctl->eoi_reached) {
788
37.7k
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
789
352
      return FALSE;             /* Suspend, come back later */
790
37.7k
  }
791
50.2k
  cinfo->global_state = DSTATE_BUFIMAGE;
792
50.2k
  return TRUE;
793
50.5k
}
794
795
#endif /* D_MULTISCAN_FILES_SUPPORTED */
796
797
#endif /* BITS_IN_JSAMPLE == 8 */