Coverage Report

Created: 2026-05-30 07:16

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
172k
{
57
172k
  if (cinfo->global_state == DSTATE_READY) {
58
    /* First call: initialize master control, select active modules */
59
172k
    jinit_master_decompress(cinfo);
60
172k
    if (cinfo->buffered_image) {
61
      /* No more work here; expecting jpeg_start_output next */
62
24.8k
      cinfo->global_state = DSTATE_BUFIMAGE;
63
24.8k
      return TRUE;
64
24.8k
    }
65
147k
    cinfo->global_state = DSTATE_PRELOAD;
66
147k
  }
67
147k
  if (cinfo->global_state == DSTATE_PRELOAD) {
68
    /* If file has multiple scans, absorb them all into the coef buffer */
69
91.7k
    if (cinfo->inputctl->has_multiple_scans) {
70
64.3k
#ifdef D_MULTISCAN_FILES_SUPPORTED
71
325M
      for (;;) {
72
325M
        int retcode;
73
        /* Call progress monitor hook if present */
74
325M
        if (cinfo->progress != NULL)
75
325M
          (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
76
        /* Absorb some more input */
77
325M
        retcode = (*cinfo->inputctl->consume_input) (cinfo);
78
325M
        if (retcode == JPEG_SUSPENDED)
79
0
          return FALSE;
80
325M
        if (retcode == JPEG_REACHED_EOI)
81
51.3k
          break;
82
        /* Advance progress counter if appropriate */
83
325M
        if (cinfo->progress != NULL &&
84
325M
            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
85
325M
          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
86
            /* jdmaster underestimated number of scans; ratchet up one scan */
87
213k
            cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
88
213k
          }
89
325M
        }
90
325M
      }
91
#else
92
      ERREXIT(cinfo, JERR_NOT_COMPILED);
93
#endif /* D_MULTISCAN_FILES_SUPPORTED */
94
64.3k
    }
95
91.7k
    cinfo->output_scan_number = cinfo->input_scan_number;
96
91.7k
  } 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
147k
  return output_pass_setup(cinfo);
100
147k
}
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
115k
{
114
115k
  if (cinfo->global_state != DSTATE_PRESCAN) {
115
    /* First call: do pass setup */
116
115k
    (*cinfo->master->prepare_for_output_pass) (cinfo);
117
115k
    cinfo->output_scanline = 0;
118
115k
    cinfo->global_state = DSTATE_PRESCAN;
119
115k
  }
120
  /* Loop over any required dummy passes */
121
119k
  while (cinfo->master->is_dummy_pass) {
122
4.08k
#ifdef QUANT_2PASS_SUPPORTED
123
    /* Crank through the dummy pass */
124
26.4M
    while (cinfo->output_scanline < cinfo->output_height) {
125
26.4M
      JDIMENSION last_scanline;
126
      /* Call progress monitor hook if present */
127
26.4M
      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
26.4M
      last_scanline = cinfo->output_scanline;
134
26.4M
      if (cinfo->data_precision <= 8) {
135
26.4M
        if (cinfo->main->process_data == NULL)
136
0
          ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
137
26.4M
        (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
138
26.4M
                                      &cinfo->output_scanline, (JDIMENSION)0);
139
26.4M
      } 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
26.4M
      if (cinfo->output_scanline == last_scanline)
157
39
        return FALSE;           /* No progress made, must suspend */
158
26.4M
    }
159
    /* Finish up dummy pass, and set up for another one */
160
4.04k
    (*cinfo->master->finish_output_pass) (cinfo);
161
4.04k
    (*cinfo->master->prepare_for_output_pass) (cinfo);
162
4.04k
    cinfo->output_scanline = 0;
163
#else
164
    ERREXIT(cinfo, JERR_NOT_COMPILED);
165
#endif /* QUANT_2PASS_SUPPORTED */
166
4.04k
  }
167
  /* Ready for application to drive output pass through
168
   * _jpeg_read_scanlines or _jpeg_read_raw_data.
169
   */
170
115k
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
171
115k
  return TRUE;
172
115k
}
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
4.20k
{
192
4.20k
  int ci, align, orig_downsampled_width;
193
4.20k
  JDIMENSION input_xoffset;
194
4.20k
  boolean reinit_upsampler = FALSE;
195
4.20k
  jpeg_component_info *compptr;
196
4.20k
#ifdef UPSAMPLE_MERGING_SUPPORTED
197
4.20k
  my_master_ptr master = (my_master_ptr)cinfo->master;
198
4.20k
#endif
199
200
4.20k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
201
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
202
203
4.20k
  if (cinfo->master->lossless || cinfo->raw_data_out)
204
0
    ERREXIT(cinfo, JERR_NOTIMPL);
205
206
4.20k
  if ((cinfo->global_state != DSTATE_SCANNING &&
207
4.20k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
208
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
209
210
4.20k
  if (!xoffset || !width)
211
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
212
213
  /* xoffset and width must fall within the output image dimensions. */
214
4.20k
  if (*width == 0 ||
215
4.20k
      (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
4.20k
  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
4.20k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
241
3.64k
    align = cinfo->_min_DCT_scaled_size;
242
567
  else
243
567
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
244
245
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
246
4.20k
  input_xoffset = *xoffset;
247
4.20k
  *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
4.20k
  *width = *width + input_xoffset - *xoffset;
255
4.20k
  cinfo->output_width = *width;
256
4.20k
#ifdef UPSAMPLE_MERGING_SUPPORTED
257
4.20k
  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
4.20k
#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
4.20k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
268
4.20k
  cinfo->master->last_iMCU_col =
269
4.20k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
270
4.20k
                              (long)align) - 1;
271
272
9.58k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
273
5.37k
       ci++, compptr++) {
274
5.37k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
275
3.64k
              1 : compptr->h_samp_factor;
276
277
    /* Set downsampled_width to the new output width. */
278
5.37k
    orig_downsampled_width = compptr->downsampled_width;
279
5.37k
    compptr->downsampled_width =
280
5.37k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
281
5.37k
                                (long)(compptr->h_samp_factor *
282
5.37k
                                       compptr->_DCT_scaled_size),
283
5.37k
                                (long)(cinfo->max_h_samp_factor *
284
5.37k
                                       cinfo->_min_DCT_scaled_size));
285
5.37k
    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
5.37k
    cinfo->master->first_MCU_col[ci] =
292
5.37k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
293
5.37k
    cinfo->master->last_MCU_col[ci] =
294
5.37k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
295
5.37k
                                (long)align) - 1;
296
5.37k
  }
297
298
4.20k
  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
4.20k
}
jpeg_crop_scanline
Line
Count
Source
191
2.07k
{
192
2.07k
  int ci, align, orig_downsampled_width;
193
2.07k
  JDIMENSION input_xoffset;
194
2.07k
  boolean reinit_upsampler = FALSE;
195
2.07k
  jpeg_component_info *compptr;
196
2.07k
#ifdef UPSAMPLE_MERGING_SUPPORTED
197
2.07k
  my_master_ptr master = (my_master_ptr)cinfo->master;
198
2.07k
#endif
199
200
2.07k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
201
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
202
203
2.07k
  if (cinfo->master->lossless || cinfo->raw_data_out)
204
0
    ERREXIT(cinfo, JERR_NOTIMPL);
205
206
2.07k
  if ((cinfo->global_state != DSTATE_SCANNING &&
207
2.07k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
208
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
209
210
2.07k
  if (!xoffset || !width)
211
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
212
213
  /* xoffset and width must fall within the output image dimensions. */
214
2.07k
  if (*width == 0 ||
215
2.07k
      (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
2.07k
  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
2.07k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
241
1.85k
    align = cinfo->_min_DCT_scaled_size;
242
218
  else
243
218
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
244
245
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
246
2.07k
  input_xoffset = *xoffset;
247
2.07k
  *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
2.07k
  *width = *width + input_xoffset - *xoffset;
255
2.07k
  cinfo->output_width = *width;
256
2.07k
#ifdef UPSAMPLE_MERGING_SUPPORTED
257
2.07k
  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
2.07k
#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
2.07k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
268
2.07k
  cinfo->master->last_iMCU_col =
269
2.07k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
270
2.07k
                              (long)align) - 1;
271
272
4.60k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
273
2.52k
       ci++, compptr++) {
274
2.52k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
275
1.85k
              1 : compptr->h_samp_factor;
276
277
    /* Set downsampled_width to the new output width. */
278
2.52k
    orig_downsampled_width = compptr->downsampled_width;
279
2.52k
    compptr->downsampled_width =
280
2.52k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
281
2.52k
                                (long)(compptr->h_samp_factor *
282
2.52k
                                       compptr->_DCT_scaled_size),
283
2.52k
                                (long)(cinfo->max_h_samp_factor *
284
2.52k
                                       cinfo->_min_DCT_scaled_size));
285
2.52k
    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.52k
    cinfo->master->first_MCU_col[ci] =
292
2.52k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
293
2.52k
    cinfo->master->last_MCU_col[ci] =
294
2.52k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
295
2.52k
                                (long)align) - 1;
296
2.52k
  }
297
298
2.07k
  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
2.07k
}
jpeg12_crop_scanline
Line
Count
Source
191
2.13k
{
192
2.13k
  int ci, align, orig_downsampled_width;
193
2.13k
  JDIMENSION input_xoffset;
194
2.13k
  boolean reinit_upsampler = FALSE;
195
2.13k
  jpeg_component_info *compptr;
196
2.13k
#ifdef UPSAMPLE_MERGING_SUPPORTED
197
2.13k
  my_master_ptr master = (my_master_ptr)cinfo->master;
198
2.13k
#endif
199
200
2.13k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
201
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
202
203
2.13k
  if (cinfo->master->lossless || cinfo->raw_data_out)
204
0
    ERREXIT(cinfo, JERR_NOTIMPL);
205
206
2.13k
  if ((cinfo->global_state != DSTATE_SCANNING &&
207
2.13k
       cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
208
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
209
210
2.13k
  if (!xoffset || !width)
211
0
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
212
213
  /* xoffset and width must fall within the output image dimensions. */
214
2.13k
  if (*width == 0 ||
215
2.13k
      (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
2.13k
  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
2.13k
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
241
1.78k
    align = cinfo->_min_DCT_scaled_size;
242
349
  else
243
349
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
244
245
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
246
2.13k
  input_xoffset = *xoffset;
247
2.13k
  *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
2.13k
  *width = *width + input_xoffset - *xoffset;
255
2.13k
  cinfo->output_width = *width;
256
2.13k
#ifdef UPSAMPLE_MERGING_SUPPORTED
257
2.13k
  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
2.13k
#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
2.13k
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
268
2.13k
  cinfo->master->last_iMCU_col =
269
2.13k
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
270
2.13k
                              (long)align) - 1;
271
272
4.97k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
273
2.84k
       ci++, compptr++) {
274
2.84k
    int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
275
1.78k
              1 : compptr->h_samp_factor;
276
277
    /* Set downsampled_width to the new output width. */
278
2.84k
    orig_downsampled_width = compptr->downsampled_width;
279
2.84k
    compptr->downsampled_width =
280
2.84k
      (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
281
2.84k
                                (long)(compptr->h_samp_factor *
282
2.84k
                                       compptr->_DCT_scaled_size),
283
2.84k
                                (long)(cinfo->max_h_samp_factor *
284
2.84k
                                       cinfo->_min_DCT_scaled_size));
285
2.84k
    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.84k
    cinfo->master->first_MCU_col[ci] =
292
2.84k
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
293
2.84k
    cinfo->master->last_MCU_col[ci] =
294
2.84k
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
295
2.84k
                                (long)align) - 1;
296
2.84k
  }
297
298
2.13k
  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
2.13k
}
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
273M
{
325
273M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
326
273M
  JDIMENSION row_ctr;
327
328
273M
#ifdef D_LOSSLESS_SUPPORTED
329
273M
  if (cinfo->master->lossless) {
330
#if BITS_IN_JSAMPLE == 8
331
9.60M
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
332
#else
333
5.00M
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
334
5.00M
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
335
511
#endif
336
539
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
337
14.6M
  } else
338
258M
#endif
339
258M
  {
340
258M
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
341
18
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
342
258M
  }
343
344
273M
  if (cinfo->global_state != DSTATE_SCANNING)
345
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
346
273M
  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
273M
  if (cinfo->progress != NULL) {
353
34.6M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
354
34.6M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
355
34.6M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
356
34.6M
  }
357
358
  /* Process some data */
359
273M
  row_ctr = 0;
360
273M
  if (cinfo->main->_process_data == NULL)
361
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
362
273M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
363
273M
  cinfo->output_scanline += row_ctr;
364
273M
  return row_ctr;
365
#else
366
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
367
  return 0;
368
#endif
369
273M
}
jpeg_read_scanlines
Line
Count
Source
324
254M
{
325
254M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
326
254M
  JDIMENSION row_ctr;
327
328
254M
#ifdef D_LOSSLESS_SUPPORTED
329
254M
  if (cinfo->master->lossless) {
330
9.60M
#if BITS_IN_JSAMPLE == 8
331
9.60M
    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
28
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
337
9.60M
  } else
338
245M
#endif
339
245M
  {
340
245M
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
341
18
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
342
245M
  }
343
344
254M
  if (cinfo->global_state != DSTATE_SCANNING)
345
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
346
254M
  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
254M
  if (cinfo->progress != NULL) {
353
16.3M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
354
16.3M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
355
16.3M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
356
16.3M
  }
357
358
  /* Process some data */
359
254M
  row_ctr = 0;
360
254M
  if (cinfo->main->_process_data == NULL)
361
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
362
254M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
363
254M
  cinfo->output_scanline += row_ctr;
364
254M
  return row_ctr;
365
#else
366
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
367
  return 0;
368
#endif
369
254M
}
jpeg12_read_scanlines
Line
Count
Source
324
15.8M
{
325
15.8M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
326
15.8M
  JDIMENSION row_ctr;
327
328
15.8M
#ifdef D_LOSSLESS_SUPPORTED
329
15.8M
  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
2.50M
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
334
2.50M
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
335
0
#endif
336
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
337
2.50M
  } else
338
13.3M
#endif
339
13.3M
  {
340
13.3M
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
341
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
342
13.3M
  }
343
344
15.8M
  if (cinfo->global_state != DSTATE_SCANNING)
345
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
346
15.8M
  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
15.8M
  if (cinfo->progress != NULL) {
353
15.8M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
354
15.8M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
355
15.8M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
356
15.8M
  }
357
358
  /* Process some data */
359
15.8M
  row_ctr = 0;
360
15.8M
  if (cinfo->main->_process_data == NULL)
361
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
362
15.8M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
363
15.8M
  cinfo->output_scanline += row_ctr;
364
15.8M
  return row_ctr;
365
#else
366
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
367
  return 0;
368
#endif
369
15.8M
}
jpeg16_read_scanlines
Line
Count
Source
324
2.49M
{
325
2.49M
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
326
2.49M
  JDIMENSION row_ctr;
327
328
2.49M
#ifdef D_LOSSLESS_SUPPORTED
329
2.49M
  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
2.49M
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
334
2.49M
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
335
511
#endif
336
511
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
337
2.49M
  } 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
2.49M
  if (cinfo->global_state != DSTATE_SCANNING)
345
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
346
2.49M
  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
2.49M
  if (cinfo->progress != NULL) {
353
2.49M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
354
2.49M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
355
2.49M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
356
2.49M
  }
357
358
  /* Process some data */
359
2.49M
  row_ctr = 0;
360
2.49M
  if (cinfo->main->_process_data == NULL)
361
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
362
2.49M
  (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
363
2.49M
  cinfo->output_scanline += row_ctr;
364
2.49M
  return row_ctr;
365
#else
366
  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
367
  return 0;
368
#endif
369
2.49M
}
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
291k
{
379
291k
}
jdapistd-8.c:noop_convert
Line
Count
Source
378
289k
{
379
289k
}
jdapistd-12.c:noop_convert
Line
Count
Source
378
1.48k
{
379
1.48k
}
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
149k
{
387
149k
}
jdapistd-8.c:noop_quantize
Line
Count
Source
386
149k
{
387
149k
}
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
94.9k
{
401
94.9k
  JDIMENSION n;
402
94.9k
#ifdef UPSAMPLE_MERGING_SUPPORTED
403
94.9k
  my_master_ptr master = (my_master_ptr)cinfo->master;
404
94.9k
#endif
405
94.9k
  _JSAMPLE dummy_sample[1] = { 0 };
406
94.9k
  _JSAMPROW dummy_row = dummy_sample;
407
94.9k
  _JSAMPARRAY scanlines = NULL;
408
94.9k
  void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
409
94.9k
                         JDIMENSION input_row, _JSAMPARRAY output_buf,
410
94.9k
                         int num_rows) = NULL;
411
94.9k
  void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
412
94.9k
                          _JSAMPARRAY output_buf, int num_rows) = NULL;
413
414
94.9k
  if (cinfo->cconvert &&
415
94.9k
#ifdef UPSAMPLE_MERGING_SUPPORTED
416
94.9k
      !master->using_merged_upsample &&
417
94.9k
#endif
418
94.9k
      cinfo->cconvert->_color_convert) {
419
94.9k
    color_convert = cinfo->cconvert->_color_convert;
420
94.9k
    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
94.9k
    scanlines = &dummy_row;
425
94.9k
  }
426
427
94.9k
  if (cinfo->quantize_colors && cinfo->cquantize &&
428
48.0k
      cinfo->cquantize->_color_quantize) {
429
48.0k
    color_quantize = cinfo->cquantize->_color_quantize;
430
48.0k
    cinfo->cquantize->_color_quantize = noop_quantize;
431
48.0k
  }
432
433
94.9k
#ifdef UPSAMPLE_MERGING_SUPPORTED
434
94.9k
  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
94.9k
#endif
439
440
400k
  for (n = 0; n < num_lines; n++)
441
305k
    _jpeg_read_scanlines(cinfo, scanlines, 1);
442
443
94.9k
  if (color_convert)
444
94.9k
    cinfo->cconvert->_color_convert = color_convert;
445
446
94.9k
  if (color_quantize)
447
48.0k
    cinfo->cquantize->_color_quantize = color_quantize;
448
94.9k
}
jdapistd-8.c:read_and_discard_scanlines
Line
Count
Source
400
92.8k
{
401
92.8k
  JDIMENSION n;
402
92.8k
#ifdef UPSAMPLE_MERGING_SUPPORTED
403
92.8k
  my_master_ptr master = (my_master_ptr)cinfo->master;
404
92.8k
#endif
405
92.8k
  _JSAMPLE dummy_sample[1] = { 0 };
406
92.8k
  _JSAMPROW dummy_row = dummy_sample;
407
92.8k
  _JSAMPARRAY scanlines = NULL;
408
92.8k
  void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
409
92.8k
                         JDIMENSION input_row, _JSAMPARRAY output_buf,
410
92.8k
                         int num_rows) = NULL;
411
92.8k
  void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
412
92.8k
                          _JSAMPARRAY output_buf, int num_rows) = NULL;
413
414
92.8k
  if (cinfo->cconvert &&
415
92.8k
#ifdef UPSAMPLE_MERGING_SUPPORTED
416
92.8k
      !master->using_merged_upsample &&
417
92.8k
#endif
418
92.8k
      cinfo->cconvert->_color_convert) {
419
92.8k
    color_convert = cinfo->cconvert->_color_convert;
420
92.8k
    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
92.8k
    scanlines = &dummy_row;
425
92.8k
  }
426
427
92.8k
  if (cinfo->quantize_colors && cinfo->cquantize &&
428
48.0k
      cinfo->cquantize->_color_quantize) {
429
48.0k
    color_quantize = cinfo->cquantize->_color_quantize;
430
48.0k
    cinfo->cquantize->_color_quantize = noop_quantize;
431
48.0k
  }
432
433
92.8k
#ifdef UPSAMPLE_MERGING_SUPPORTED
434
92.8k
  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
92.8k
#endif
439
440
396k
  for (n = 0; n < num_lines; n++)
441
304k
    _jpeg_read_scanlines(cinfo, scanlines, 1);
442
443
92.8k
  if (color_convert)
444
92.8k
    cinfo->cconvert->_color_convert = color_convert;
445
446
92.8k
  if (color_quantize)
447
48.0k
    cinfo->cquantize->_color_quantize = color_quantize;
448
92.8k
}
jdapistd-12.c:read_and_discard_scanlines
Line
Count
Source
400
2.11k
{
401
2.11k
  JDIMENSION n;
402
2.11k
#ifdef UPSAMPLE_MERGING_SUPPORTED
403
2.11k
  my_master_ptr master = (my_master_ptr)cinfo->master;
404
2.11k
#endif
405
2.11k
  _JSAMPLE dummy_sample[1] = { 0 };
406
2.11k
  _JSAMPROW dummy_row = dummy_sample;
407
2.11k
  _JSAMPARRAY scanlines = NULL;
408
2.11k
  void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
409
2.11k
                         JDIMENSION input_row, _JSAMPARRAY output_buf,
410
2.11k
                         int num_rows) = NULL;
411
2.11k
  void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
412
2.11k
                          _JSAMPARRAY output_buf, int num_rows) = NULL;
413
414
2.11k
  if (cinfo->cconvert &&
415
2.11k
#ifdef UPSAMPLE_MERGING_SUPPORTED
416
2.11k
      !master->using_merged_upsample &&
417
2.11k
#endif
418
2.11k
      cinfo->cconvert->_color_convert) {
419
2.11k
    color_convert = cinfo->cconvert->_color_convert;
420
2.11k
    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
2.11k
    scanlines = &dummy_row;
425
2.11k
  }
426
427
2.11k
  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
2.11k
#ifdef UPSAMPLE_MERGING_SUPPORTED
434
2.11k
  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
2.11k
#endif
439
440
3.59k
  for (n = 0; n < num_lines; n++)
441
1.48k
    _jpeg_read_scanlines(cinfo, scanlines, 1);
442
443
2.11k
  if (color_convert)
444
2.11k
    cinfo->cconvert->_color_convert = color_convert;
445
446
2.11k
  if (color_quantize)
447
0
    cinfo->cquantize->_color_quantize = color_quantize;
448
2.11k
}
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
58.5k
{
459
58.5k
  JDIMENSION rows_left;
460
58.5k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
461
58.5k
  my_master_ptr master = (my_master_ptr)cinfo->master;
462
463
58.5k
  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
58.5k
  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
58.5k
  rows_left = rows % cinfo->max_v_samp_factor;
475
58.5k
  cinfo->output_scanline += rows - rows_left;
476
477
58.5k
  read_and_discard_scanlines(cinfo, rows_left);
478
58.5k
}
jdapistd-8.c:increment_simple_rowgroup_ctr
Line
Count
Source
458
56.4k
{
459
56.4k
  JDIMENSION rows_left;
460
56.4k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
461
56.4k
  my_master_ptr master = (my_master_ptr)cinfo->master;
462
463
56.4k
  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
56.4k
  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
56.4k
  rows_left = rows % cinfo->max_v_samp_factor;
475
56.4k
  cinfo->output_scanline += rows - rows_left;
476
477
56.4k
  read_and_discard_scanlines(cinfo, rows_left);
478
56.4k
}
jdapistd-12.c:increment_simple_rowgroup_ctr
Line
Count
Source
458
2.04k
{
459
2.04k
  JDIMENSION rows_left;
460
2.04k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
461
2.04k
  my_master_ptr master = (my_master_ptr)cinfo->master;
462
463
2.04k
  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
2.04k
  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
2.04k
  rows_left = rows % cinfo->max_v_samp_factor;
475
2.04k
  cinfo->output_scanline += rows - rows_left;
476
477
2.04k
  read_and_discard_scanlines(cinfo, rows_left);
478
2.04k
}
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
101k
{
494
101k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
495
101k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
496
101k
  my_master_ptr master = (my_master_ptr)cinfo->master;
497
101k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
498
101k
  JDIMENSION i, x;
499
101k
  int y;
500
101k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
501
101k
  JDIMENSION lines_to_skip, lines_to_read;
502
503
101k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
504
148
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
505
506
101k
  if (cinfo->master->lossless)
507
0
    ERREXIT(cinfo, JERR_NOTIMPL);
508
509
  /* Two-pass color quantization is not supported. */
510
101k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
511
0
    ERREXIT(cinfo, JERR_NOTIMPL);
512
513
101k
  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
101k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
518
101k
      cinfo->output_height) {
519
6.64k
    num_lines = cinfo->output_height - cinfo->output_scanline;
520
6.64k
    cinfo->output_scanline = cinfo->output_height;
521
6.64k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
522
6.64k
    cinfo->inputctl->eoi_reached = TRUE;
523
6.64k
    return num_lines;
524
6.64k
  }
525
526
95.1k
  if (num_lines == 0)
527
0
    return 0;
528
529
95.1k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
530
95.1k
  lines_left_in_iMCU_row =
531
95.1k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
532
95.1k
    lines_per_iMCU_row;
533
95.1k
  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
95.1k
  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
36.4k
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
549
31.9k
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
550
16.1k
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
551
16.1k
      read_and_discard_scanlines(cinfo, num_lines);
552
16.1k
      return num_lines;
553
16.1k
    }
554
555
    /* If the next iMCU row has already been entropy-decoded, make sure that
556
     * we do not skip too far.
557
     */
558
20.2k
    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
20.2k
    } else {
562
20.2k
      cinfo->output_scanline += lines_left_in_iMCU_row;
563
20.2k
    }
564
565
    /* If we have just completed the first block, adjust the buffer pointers */
566
20.2k
    if (main_ptr->iMCU_row_ctr == 0 ||
567
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
568
20.2k
      set_wraparound_pointers(cinfo);
569
20.2k
    main_ptr->buffer_full = FALSE;
570
20.2k
    main_ptr->rowgroup_ctr = 0;
571
20.2k
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
572
20.2k
    if (!master->using_merged_upsample) {
573
20.2k
      upsample->next_row_out = cinfo->max_v_samp_factor;
574
20.2k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
575
20.2k
    }
576
20.2k
  }
577
578
  /* Skipping is much simpler when context rows are not required. */
579
58.7k
  else {
580
58.7k
    if (num_lines < lines_left_in_iMCU_row) {
581
9.06k
      increment_simple_rowgroup_ctr(cinfo, num_lines);
582
9.06k
      return num_lines;
583
49.6k
    } else {
584
49.6k
      cinfo->output_scanline += lines_left_in_iMCU_row;
585
49.6k
      main_ptr->buffer_full = FALSE;
586
49.6k
      main_ptr->rowgroup_ctr = 0;
587
49.6k
      if (!master->using_merged_upsample) {
588
49.5k
        upsample->next_row_out = cinfo->max_v_samp_factor;
589
49.5k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
590
49.5k
      }
591
49.6k
    }
592
58.7k
  }
593
594
  /* Calculate how many full iMCU rows we can skip. */
595
69.9k
  if (cinfo->upsample->need_context_rows)
596
20.2k
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
597
20.2k
                    lines_per_iMCU_row;
598
49.6k
  else
599
49.6k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
600
49.6k
                    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
69.9k
  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
69.9k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
612
55.8k
    if (cinfo->upsample->need_context_rows) {
613
19.4k
      cinfo->output_scanline += lines_to_skip;
614
19.4k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
615
19.4k
      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
19.4k
      read_and_discard_scanlines(cinfo, lines_to_read);
620
36.3k
    } else {
621
36.3k
      cinfo->output_scanline += lines_to_skip;
622
36.3k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
623
36.3k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
624
36.3k
    }
625
55.8k
    if (!master->using_merged_upsample)
626
55.8k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
627
55.8k
    return num_lines;
628
55.8k
  }
629
630
  /* Skip the iMCU rows that we can safely skip. */
631
25.7k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
632
24.3k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
633
3.67M
      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
3.65M
        if (!cinfo->entropy->insufficient_data)
639
1.14M
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
640
#ifdef WITH_PROFILE
641
        cinfo->master->start = getTime();
642
#endif
643
3.65M
        (*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
3.65M
      }
650
12.7k
    }
651
11.6k
    cinfo->input_iMCU_row++;
652
11.6k
    cinfo->output_iMCU_row++;
653
11.6k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
654
11.6k
      start_iMCU_row(cinfo);
655
0
    else
656
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
657
11.6k
  }
658
14.0k
  cinfo->output_scanline += lines_to_skip;
659
660
14.0k
  if (cinfo->upsample->need_context_rows) {
661
    /* Context-based upsampling keeps track of iMCU rows. */
662
814
    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
814
    read_and_discard_scanlines(cinfo, lines_to_read);
668
13.2k
  } else {
669
13.2k
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
670
13.2k
  }
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
14.0k
  if (!master->using_merged_upsample)
678
13.9k
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
679
680
  /* Always skip the requested number of lines. */
681
14.0k
  return num_lines;
682
69.9k
}
jpeg_skip_scanlines
Line
Count
Source
493
97.5k
{
494
97.5k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
495
97.5k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
496
97.5k
  my_master_ptr master = (my_master_ptr)cinfo->master;
497
97.5k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
498
97.5k
  JDIMENSION i, x;
499
97.5k
  int y;
500
97.5k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
501
97.5k
  JDIMENSION lines_to_skip, lines_to_read;
502
503
97.5k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
504
148
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
505
506
97.5k
  if (cinfo->master->lossless)
507
0
    ERREXIT(cinfo, JERR_NOTIMPL);
508
509
  /* Two-pass color quantization is not supported. */
510
97.5k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
511
0
    ERREXIT(cinfo, JERR_NOTIMPL);
512
513
97.5k
  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
97.5k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
518
97.5k
      cinfo->output_height) {
519
4.54k
    num_lines = cinfo->output_height - cinfo->output_scanline;
520
4.54k
    cinfo->output_scanline = cinfo->output_height;
521
4.54k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
522
4.54k
    cinfo->inputctl->eoi_reached = TRUE;
523
4.54k
    return num_lines;
524
4.54k
  }
525
526
93.0k
  if (num_lines == 0)
527
0
    return 0;
528
529
93.0k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
530
93.0k
  lines_left_in_iMCU_row =
531
93.0k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
532
93.0k
    lines_per_iMCU_row;
533
93.0k
  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
93.0k
  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
36.3k
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
549
31.8k
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
550
16.1k
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
551
16.1k
      read_and_discard_scanlines(cinfo, num_lines);
552
16.1k
      return num_lines;
553
16.1k
    }
554
555
    /* If the next iMCU row has already been entropy-decoded, make sure that
556
     * we do not skip too far.
557
     */
558
20.2k
    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
20.2k
    } else {
562
20.2k
      cinfo->output_scanline += lines_left_in_iMCU_row;
563
20.2k
    }
564
565
    /* If we have just completed the first block, adjust the buffer pointers */
566
20.2k
    if (main_ptr->iMCU_row_ctr == 0 ||
567
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
568
20.2k
      set_wraparound_pointers(cinfo);
569
20.2k
    main_ptr->buffer_full = FALSE;
570
20.2k
    main_ptr->rowgroup_ctr = 0;
571
20.2k
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
572
20.2k
    if (!master->using_merged_upsample) {
573
20.2k
      upsample->next_row_out = cinfo->max_v_samp_factor;
574
20.2k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
575
20.2k
    }
576
20.2k
  }
577
578
  /* Skipping is much simpler when context rows are not required. */
579
56.6k
  else {
580
56.6k
    if (num_lines < lines_left_in_iMCU_row) {
581
9.06k
      increment_simple_rowgroup_ctr(cinfo, num_lines);
582
9.06k
      return num_lines;
583
47.5k
    } else {
584
47.5k
      cinfo->output_scanline += lines_left_in_iMCU_row;
585
47.5k
      main_ptr->buffer_full = FALSE;
586
47.5k
      main_ptr->rowgroup_ctr = 0;
587
47.5k
      if (!master->using_merged_upsample) {
588
47.4k
        upsample->next_row_out = cinfo->max_v_samp_factor;
589
47.4k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
590
47.4k
      }
591
47.5k
    }
592
56.6k
  }
593
594
  /* Calculate how many full iMCU rows we can skip. */
595
67.8k
  if (cinfo->upsample->need_context_rows)
596
20.2k
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
597
20.2k
                    lines_per_iMCU_row;
598
47.5k
  else
599
47.5k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
600
47.5k
                    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
67.8k
  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
67.8k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
612
54.5k
    if (cinfo->upsample->need_context_rows) {
613
19.4k
      cinfo->output_scanline += lines_to_skip;
614
19.4k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
615
19.4k
      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
19.4k
      read_and_discard_scanlines(cinfo, lines_to_read);
620
35.1k
    } else {
621
35.1k
      cinfo->output_scanline += lines_to_skip;
622
35.1k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
623
35.1k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
624
35.1k
    }
625
54.5k
    if (!master->using_merged_upsample)
626
54.5k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
627
54.5k
    return num_lines;
628
54.5k
  }
629
630
  /* Skip the iMCU rows that we can safely skip. */
631
23.5k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
632
21.0k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
633
3.42M
      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
3.40M
        if (!cinfo->entropy->insufficient_data)
639
1.03M
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
640
#ifdef WITH_PROFILE
641
        cinfo->master->start = getTime();
642
#endif
643
3.40M
        (*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
3.40M
      }
650
10.7k
    }
651
10.3k
    cinfo->input_iMCU_row++;
652
10.3k
    cinfo->output_iMCU_row++;
653
10.3k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
654
10.3k
      start_iMCU_row(cinfo);
655
0
    else
656
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
657
10.3k
  }
658
13.2k
  cinfo->output_scanline += lines_to_skip;
659
660
13.2k
  if (cinfo->upsample->need_context_rows) {
661
    /* Context-based upsampling keeps track of iMCU rows. */
662
760
    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
760
    read_and_discard_scanlines(cinfo, lines_to_read);
668
12.4k
  } else {
669
12.4k
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
670
12.4k
  }
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
13.2k
  if (!master->using_merged_upsample)
678
13.0k
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
679
680
  /* Always skip the requested number of lines. */
681
13.2k
  return num_lines;
682
67.8k
}
jpeg12_skip_scanlines
Line
Count
Source
493
4.23k
{
494
4.23k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
495
4.23k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
496
4.23k
  my_master_ptr master = (my_master_ptr)cinfo->master;
497
4.23k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
498
4.23k
  JDIMENSION i, x;
499
4.23k
  int y;
500
4.23k
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
501
4.23k
  JDIMENSION lines_to_skip, lines_to_read;
502
503
4.23k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
504
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
505
506
4.23k
  if (cinfo->master->lossless)
507
0
    ERREXIT(cinfo, JERR_NOTIMPL);
508
509
  /* Two-pass color quantization is not supported. */
510
4.23k
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
511
0
    ERREXIT(cinfo, JERR_NOTIMPL);
512
513
4.23k
  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
4.23k
  if ((unsigned long long)cinfo->output_scanline + num_lines >=
518
4.23k
      cinfo->output_height) {
519
2.10k
    num_lines = cinfo->output_height - cinfo->output_scanline;
520
2.10k
    cinfo->output_scanline = cinfo->output_height;
521
2.10k
    (*cinfo->inputctl->finish_input_pass) (cinfo);
522
2.10k
    cinfo->inputctl->eoi_reached = TRUE;
523
2.10k
    return num_lines;
524
2.10k
  }
525
526
2.12k
  if (num_lines == 0)
527
0
    return 0;
528
529
2.12k
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
530
2.12k
  lines_left_in_iMCU_row =
531
2.12k
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
532
2.12k
    lines_per_iMCU_row;
533
2.12k
  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
2.12k
  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
66
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
549
66
        (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
66
    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
66
    } else {
562
66
      cinfo->output_scanline += lines_left_in_iMCU_row;
563
66
    }
564
565
    /* If we have just completed the first block, adjust the buffer pointers */
566
66
    if (main_ptr->iMCU_row_ctr == 0 ||
567
0
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
568
66
      set_wraparound_pointers(cinfo);
569
66
    main_ptr->buffer_full = FALSE;
570
66
    main_ptr->rowgroup_ctr = 0;
571
66
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
572
66
    if (!master->using_merged_upsample) {
573
66
      upsample->next_row_out = cinfo->max_v_samp_factor;
574
66
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
575
66
    }
576
66
  }
577
578
  /* Skipping is much simpler when context rows are not required. */
579
2.06k
  else {
580
2.06k
    if (num_lines < lines_left_in_iMCU_row) {
581
0
      increment_simple_rowgroup_ctr(cinfo, num_lines);
582
0
      return num_lines;
583
2.06k
    } else {
584
2.06k
      cinfo->output_scanline += lines_left_in_iMCU_row;
585
2.06k
      main_ptr->buffer_full = FALSE;
586
2.06k
      main_ptr->rowgroup_ctr = 0;
587
2.06k
      if (!master->using_merged_upsample) {
588
2.06k
        upsample->next_row_out = cinfo->max_v_samp_factor;
589
2.06k
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
590
2.06k
      }
591
2.06k
    }
592
2.06k
  }
593
594
  /* Calculate how many full iMCU rows we can skip. */
595
2.12k
  if (cinfo->upsample->need_context_rows)
596
66
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
597
66
                    lines_per_iMCU_row;
598
2.06k
  else
599
2.06k
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
600
2.06k
                    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
2.12k
  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
2.12k
  if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
612
1.24k
    if (cinfo->upsample->need_context_rows) {
613
12
      cinfo->output_scanline += lines_to_skip;
614
12
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
615
12
      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
12
      read_and_discard_scanlines(cinfo, lines_to_read);
620
1.22k
    } else {
621
1.22k
      cinfo->output_scanline += lines_to_skip;
622
1.22k
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
623
1.22k
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
624
1.22k
    }
625
1.24k
    if (!master->using_merged_upsample)
626
1.24k
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
627
1.24k
    return num_lines;
628
1.24k
  }
629
630
  /* Skip the iMCU rows that we can safely skip. */
631
2.19k
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
632
3.34k
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
633
249k
      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
247k
        if (!cinfo->entropy->insufficient_data)
639
105k
          cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
640
#ifdef WITH_PROFILE
641
        cinfo->master->start = getTime();
642
#endif
643
247k
        (*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
247k
      }
650
2.03k
    }
651
1.31k
    cinfo->input_iMCU_row++;
652
1.31k
    cinfo->output_iMCU_row++;
653
1.31k
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
654
1.31k
      start_iMCU_row(cinfo);
655
0
    else
656
0
      (*cinfo->inputctl->finish_input_pass) (cinfo);
657
1.31k
  }
658
887
  cinfo->output_scanline += lines_to_skip;
659
660
887
  if (cinfo->upsample->need_context_rows) {
661
    /* Context-based upsampling keeps track of iMCU rows. */
662
54
    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
54
    read_and_discard_scanlines(cinfo, lines_to_read);
668
833
  } else {
669
833
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
670
833
  }
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
887
  if (!master->using_merged_upsample)
678
887
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
679
680
  /* Always skip the requested number of lines. */
681
887
  return num_lines;
682
2.12k
}
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
6.40M
{
693
6.40M
  JDIMENSION lines_per_iMCU_row;
694
695
6.40M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
696
9.00k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
697
698
6.40M
  if (cinfo->master->lossless)
699
1.81k
    ERREXIT(cinfo, JERR_NOTIMPL);
700
701
6.40M
  if (cinfo->global_state != DSTATE_RAW_OK)
702
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
703
6.40M
  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
6.40M
  if (cinfo->progress != NULL) {
710
6.39M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
711
6.39M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
712
6.39M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
713
6.39M
  }
714
715
  /* Verify that at least one iMCU row can be returned. */
716
6.40M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
717
6.40M
  if (max_lines < lines_per_iMCU_row)
718
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
719
720
  /* Decompress directly into user's buffer. */
721
6.40M
  if (cinfo->coef->_decompress_data == NULL)
722
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
723
6.40M
  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
6.40M
  cinfo->output_scanline += lines_per_iMCU_row;
728
6.40M
  return lines_per_iMCU_row;
729
6.40M
}
jpeg_read_raw_data
Line
Count
Source
692
6.40M
{
693
6.40M
  JDIMENSION lines_per_iMCU_row;
694
695
6.40M
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
696
9.00k
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
697
698
6.40M
  if (cinfo->master->lossless)
699
1.81k
    ERREXIT(cinfo, JERR_NOTIMPL);
700
701
6.40M
  if (cinfo->global_state != DSTATE_RAW_OK)
702
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
703
6.40M
  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
6.40M
  if (cinfo->progress != NULL) {
710
6.39M
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
711
6.39M
    cinfo->progress->pass_limit = (long)cinfo->output_height;
712
6.39M
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
713
6.39M
  }
714
715
  /* Verify that at least one iMCU row can be returned. */
716
6.40M
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
717
6.40M
  if (max_lines < lines_per_iMCU_row)
718
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
719
720
  /* Decompress directly into user's buffer. */
721
6.40M
  if (cinfo->coef->_decompress_data == NULL)
722
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
723
6.40M
  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
6.40M
  cinfo->output_scanline += lines_per_iMCU_row;
728
6.40M
  return lines_per_iMCU_row;
729
6.40M
}
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
57.8k
{
747
57.8k
  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
57.8k
  if (scan_number <= 0)
752
0
    scan_number = 1;
753
57.8k
  if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
754
0
    scan_number = cinfo->input_scan_number;
755
57.8k
  cinfo->output_scan_number = scan_number;
756
  /* Perform any dummy output passes, and set up for the real pass */
757
57.8k
  return output_pass_setup(cinfo);
758
57.8k
}
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
57.4k
{
771
57.4k
  if ((cinfo->global_state == DSTATE_SCANNING ||
772
57.4k
       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
57.4k
    (*cinfo->master->finish_output_pass) (cinfo);
776
57.4k
    cinfo->global_state = DSTATE_BUFPOST;
777
57.4k
  } 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
105k
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
783
58.1k
         !cinfo->inputctl->eoi_reached) {
784
48.6k
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
785
161
      return FALSE;             /* Suspend, come back later */
786
48.6k
  }
787
57.2k
  cinfo->global_state = DSTATE_BUFIMAGE;
788
57.2k
  return TRUE;
789
57.4k
}
790
791
#endif /* D_MULTISCAN_FILES_SUPPORTED */
792
793
#endif /* BITS_IN_JSAMPLE == 8 */