Coverage Report

Created: 2026-07-30 06:40

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