Coverage Report

Created: 2026-04-12 06:05

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