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