Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libjpeg-turbo/src/jdapistd.c
9904 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-2024, 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
(*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
133
&cinfo->output_scanline, (JDIMENSION)0);
134
else if (cinfo->data_precision <= 12)
135
(*cinfo->main->process_data_12) (cinfo, (J12SAMPARRAY)NULL,
136
&cinfo->output_scanline,
137
(JDIMENSION)0);
138
#ifdef D_LOSSLESS_SUPPORTED
139
else
140
(*cinfo->main->process_data_16) (cinfo, (J16SAMPARRAY)NULL,
141
&cinfo->output_scanline,
142
(JDIMENSION)0);
143
#endif
144
if (cinfo->output_scanline == last_scanline)
145
return FALSE; /* No progress made, must suspend */
146
}
147
/* Finish up dummy pass, and set up for another one */
148
(*cinfo->master->finish_output_pass) (cinfo);
149
(*cinfo->master->prepare_for_output_pass) (cinfo);
150
cinfo->output_scanline = 0;
151
#else
152
ERREXIT(cinfo, JERR_NOT_COMPILED);
153
#endif /* QUANT_2PASS_SUPPORTED */
154
}
155
/* Ready for application to drive output pass through
156
* _jpeg_read_scanlines or _jpeg_read_raw_data.
157
*/
158
cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
159
return TRUE;
160
}
161
162
#endif /* BITS_IN_JSAMPLE == 8 */
163
164
165
#if BITS_IN_JSAMPLE != 16
166
167
/*
168
* Enable partial scanline decompression
169
*
170
* Must be called after jpeg_start_decompress() and before any calls to
171
* _jpeg_read_scanlines() or _jpeg_skip_scanlines().
172
*
173
* Refer to libjpeg.txt for more information.
174
*/
175
176
GLOBAL(void)
177
_jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
178
JDIMENSION *width)
179
{
180
int ci, align, orig_downsampled_width;
181
JDIMENSION input_xoffset;
182
boolean reinit_upsampler = FALSE;
183
jpeg_component_info *compptr;
184
#ifdef UPSAMPLE_MERGING_SUPPORTED
185
my_master_ptr master = (my_master_ptr)cinfo->master;
186
#endif
187
188
if (cinfo->data_precision != BITS_IN_JSAMPLE)
189
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
190
191
if (cinfo->master->lossless)
192
ERREXIT(cinfo, JERR_NOTIMPL);
193
194
if ((cinfo->global_state != DSTATE_SCANNING &&
195
cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
196
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
197
198
if (!xoffset || !width)
199
ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
200
201
/* xoffset and width must fall within the output image dimensions. */
202
if (*width == 0 ||
203
(unsigned long long)(*xoffset) + *width > cinfo->output_width)
204
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
205
206
/* No need to do anything if the caller wants the entire width. */
207
if (*width == cinfo->output_width)
208
return;
209
210
/* Ensuring the proper alignment of xoffset is tricky. At minimum, it
211
* must align with an MCU boundary, because:
212
*
213
* (1) The IDCT is performed in blocks, and it is not feasible to modify
214
* the algorithm so that it can transform partial blocks.
215
* (2) Because of the SIMD extensions, any input buffer passed to the
216
* upsampling and color conversion routines must be aligned to the
217
* SIMD word size (for instance, 128-bit in the case of SSE2.) The
218
* easiest way to accomplish this without copying data is to ensure
219
* that upsampling and color conversion begin at the start of the
220
* first MCU column that will be inverse transformed.
221
*
222
* In practice, we actually impose a stricter alignment requirement. We
223
* require that xoffset be a multiple of the maximum MCU column width of all
224
* of the components (the "iMCU column width.") This is to simplify the
225
* single-pass decompression case, allowing us to use the same MCU column
226
* width for all of the components.
227
*/
228
if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
229
align = cinfo->_min_DCT_scaled_size;
230
else
231
align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
232
233
/* Adjust xoffset to the nearest iMCU boundary <= the requested value */
234
input_xoffset = *xoffset;
235
*xoffset = (input_xoffset / align) * align;
236
237
/* Adjust the width so that the right edge of the output image is as
238
* requested (only the left edge is altered.) It is important that calling
239
* programs check this value after this function returns, so that they can
240
* allocate an output buffer with the appropriate size.
241
*/
242
*width = *width + input_xoffset - *xoffset;
243
cinfo->output_width = *width;
244
#ifdef UPSAMPLE_MERGING_SUPPORTED
245
if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
246
my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
247
upsample->out_row_width =
248
cinfo->output_width * cinfo->out_color_components;
249
}
250
#endif
251
252
/* Set the first and last iMCU columns that we must decompress. These values
253
* will be used in single-scan decompressions.
254
*/
255
cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
256
cinfo->master->last_iMCU_col =
257
(JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
258
(long)align) - 1;
259
260
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
261
ci++, compptr++) {
262
int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
263
1 : compptr->h_samp_factor;
264
265
/* Set downsampled_width to the new output width. */
266
orig_downsampled_width = compptr->downsampled_width;
267
compptr->downsampled_width =
268
(JDIMENSION)jdiv_round_up((long)cinfo->output_width *
269
(long)(compptr->h_samp_factor *
270
compptr->_DCT_scaled_size),
271
(long)(cinfo->max_h_samp_factor *
272
cinfo->_min_DCT_scaled_size));
273
if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
274
reinit_upsampler = TRUE;
275
276
/* Set the first and last iMCU columns that we must decompress. These
277
* values will be used in multi-scan decompressions.
278
*/
279
cinfo->master->first_MCU_col[ci] =
280
(JDIMENSION)(long)(*xoffset * hsf) / (long)align;
281
cinfo->master->last_MCU_col[ci] =
282
(JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
283
(long)align) - 1;
284
}
285
286
if (reinit_upsampler) {
287
cinfo->master->jinit_upsampler_no_alloc = TRUE;
288
_jinit_upsampler(cinfo);
289
cinfo->master->jinit_upsampler_no_alloc = FALSE;
290
}
291
}
292
293
#endif /* BITS_IN_JSAMPLE != 16 */
294
295
296
/*
297
* Read some scanlines of data from the JPEG decompressor.
298
*
299
* The return value will be the number of lines actually read.
300
* This may be less than the number requested in several cases,
301
* including bottom of image, data source suspension, and operating
302
* modes that emit multiple scanlines at a time.
303
*
304
* Note: we warn about excess calls to _jpeg_read_scanlines() since
305
* this likely signals an application programmer error. However,
306
* an oversize buffer (max_lines > scanlines remaining) is not an error.
307
*/
308
309
GLOBAL(JDIMENSION)
310
_jpeg_read_scanlines(j_decompress_ptr cinfo, _JSAMPARRAY scanlines,
311
JDIMENSION max_lines)
312
{
313
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
314
JDIMENSION row_ctr;
315
316
#ifdef D_LOSSLESS_SUPPORTED
317
if (cinfo->master->lossless) {
318
#if BITS_IN_JSAMPLE == 8
319
if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
320
#else
321
if (cinfo->data_precision > BITS_IN_JSAMPLE ||
322
cinfo->data_precision < BITS_IN_JSAMPLE - 3)
323
#endif
324
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
325
} else
326
#endif
327
{
328
if (cinfo->data_precision != BITS_IN_JSAMPLE)
329
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
330
}
331
332
if (cinfo->global_state != DSTATE_SCANNING)
333
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
334
if (cinfo->output_scanline >= cinfo->output_height) {
335
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
336
return 0;
337
}
338
339
/* Call progress monitor hook if present */
340
if (cinfo->progress != NULL) {
341
cinfo->progress->pass_counter = (long)cinfo->output_scanline;
342
cinfo->progress->pass_limit = (long)cinfo->output_height;
343
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
344
}
345
346
/* Process some data */
347
row_ctr = 0;
348
(*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
349
cinfo->output_scanline += row_ctr;
350
return row_ctr;
351
#else
352
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
353
return 0;
354
#endif
355
}
356
357
358
#if BITS_IN_JSAMPLE != 16
359
360
/* Dummy color convert function used by _jpeg_skip_scanlines() */
361
LOCAL(void)
362
noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
363
JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)
364
{
365
}
366
367
368
/* Dummy quantize function used by _jpeg_skip_scanlines() */
369
LOCAL(void)
370
noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
371
_JSAMPARRAY output_buf, int num_rows)
372
{
373
}
374
375
376
/*
377
* In some cases, it is best to call _jpeg_read_scanlines() and discard the
378
* output, rather than skipping the scanlines, because this allows us to
379
* maintain the internal state of the context-based upsampler. In these cases,
380
* we set up and tear down a dummy color converter in order to avoid valgrind
381
* errors and to achieve the best possible performance.
382
*/
383
384
LOCAL(void)
385
read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
386
{
387
JDIMENSION n;
388
#ifdef UPSAMPLE_MERGING_SUPPORTED
389
my_master_ptr master = (my_master_ptr)cinfo->master;
390
#endif
391
_JSAMPLE dummy_sample[1] = { 0 };
392
_JSAMPROW dummy_row = dummy_sample;
393
_JSAMPARRAY scanlines = NULL;
394
void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
395
JDIMENSION input_row, _JSAMPARRAY output_buf,
396
int num_rows) = NULL;
397
void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
398
_JSAMPARRAY output_buf, int num_rows) = NULL;
399
400
if (cinfo->cconvert && cinfo->cconvert->_color_convert) {
401
color_convert = cinfo->cconvert->_color_convert;
402
cinfo->cconvert->_color_convert = noop_convert;
403
/* This just prevents UBSan from complaining about adding 0 to a NULL
404
* pointer. The pointer isn't actually used.
405
*/
406
scanlines = &dummy_row;
407
}
408
409
if (cinfo->cquantize && cinfo->cquantize->_color_quantize) {
410
color_quantize = cinfo->cquantize->_color_quantize;
411
cinfo->cquantize->_color_quantize = noop_quantize;
412
}
413
414
#ifdef UPSAMPLE_MERGING_SUPPORTED
415
if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
416
my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
417
scanlines = &upsample->spare_row;
418
}
419
#endif
420
421
for (n = 0; n < num_lines; n++)
422
_jpeg_read_scanlines(cinfo, scanlines, 1);
423
424
if (color_convert)
425
cinfo->cconvert->_color_convert = color_convert;
426
427
if (color_quantize)
428
cinfo->cquantize->_color_quantize = color_quantize;
429
}
430
431
432
/*
433
* Called by _jpeg_skip_scanlines(). This partially skips a decompress block
434
* by incrementing the rowgroup counter.
435
*/
436
437
LOCAL(void)
438
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
439
{
440
JDIMENSION rows_left;
441
my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
442
my_master_ptr master = (my_master_ptr)cinfo->master;
443
444
if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
445
read_and_discard_scanlines(cinfo, rows);
446
return;
447
}
448
449
/* Increment the counter to the next row group after the skipped rows. */
450
main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
451
452
/* Partially skipping a row group would involve modifying the internal state
453
* of the upsampler, so read the remaining rows into a dummy buffer instead.
454
*/
455
rows_left = rows % cinfo->max_v_samp_factor;
456
cinfo->output_scanline += rows - rows_left;
457
458
read_and_discard_scanlines(cinfo, rows_left);
459
}
460
461
/*
462
* Skips some scanlines of data from the JPEG decompressor.
463
*
464
* The return value will be the number of lines actually skipped. If skipping
465
* num_lines would move beyond the end of the image, then the actual number of
466
* lines remaining in the image is returned. Otherwise, the return value will
467
* be equal to num_lines.
468
*
469
* Refer to libjpeg.txt for more information.
470
*/
471
472
GLOBAL(JDIMENSION)
473
_jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
474
{
475
my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
476
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
477
my_master_ptr master = (my_master_ptr)cinfo->master;
478
my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
479
JDIMENSION i, x;
480
int y;
481
JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
482
JDIMENSION lines_to_skip, lines_to_read;
483
484
if (cinfo->data_precision != BITS_IN_JSAMPLE)
485
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
486
487
if (cinfo->master->lossless)
488
ERREXIT(cinfo, JERR_NOTIMPL);
489
490
/* Two-pass color quantization is not supported. */
491
if (cinfo->quantize_colors && cinfo->two_pass_quantize)
492
ERREXIT(cinfo, JERR_NOTIMPL);
493
494
if (cinfo->global_state != DSTATE_SCANNING)
495
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
496
497
/* Do not skip past the bottom of the image. */
498
if ((unsigned long long)cinfo->output_scanline + num_lines >=
499
cinfo->output_height) {
500
num_lines = cinfo->output_height - cinfo->output_scanline;
501
cinfo->output_scanline = cinfo->output_height;
502
(*cinfo->inputctl->finish_input_pass) (cinfo);
503
cinfo->inputctl->eoi_reached = TRUE;
504
return num_lines;
505
}
506
507
if (num_lines == 0)
508
return 0;
509
510
lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
511
lines_left_in_iMCU_row =
512
(lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
513
lines_per_iMCU_row;
514
lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
515
516
/* Skip the lines remaining in the current iMCU row. When upsampling
517
* requires context rows, we need the previous and next rows in order to read
518
* the current row. This adds some complexity.
519
*/
520
if (cinfo->upsample->need_context_rows) {
521
/* If the skipped lines would not move us past the current iMCU row, we
522
* read the lines and ignore them. There might be a faster way of doing
523
* this, but we are facing increasing complexity for diminishing returns.
524
* The increasing complexity would be a by-product of meddling with the
525
* state machine used to skip context rows. Near the end of an iMCU row,
526
* the next iMCU row may have already been entropy-decoded. In this unique
527
* case, we will read the next iMCU row if we cannot skip past it as well.
528
*/
529
if ((num_lines < lines_left_in_iMCU_row + 1) ||
530
(lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
531
lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
532
read_and_discard_scanlines(cinfo, num_lines);
533
return num_lines;
534
}
535
536
/* If the next iMCU row has already been entropy-decoded, make sure that
537
* we do not skip too far.
538
*/
539
if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
540
cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
541
lines_after_iMCU_row -= lines_per_iMCU_row;
542
} else {
543
cinfo->output_scanline += lines_left_in_iMCU_row;
544
}
545
546
/* If we have just completed the first block, adjust the buffer pointers */
547
if (main_ptr->iMCU_row_ctr == 0 ||
548
(main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
549
set_wraparound_pointers(cinfo);
550
main_ptr->buffer_full = FALSE;
551
main_ptr->rowgroup_ctr = 0;
552
main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
553
if (!master->using_merged_upsample) {
554
upsample->next_row_out = cinfo->max_v_samp_factor;
555
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
556
}
557
}
558
559
/* Skipping is much simpler when context rows are not required. */
560
else {
561
if (num_lines < lines_left_in_iMCU_row) {
562
increment_simple_rowgroup_ctr(cinfo, num_lines);
563
return num_lines;
564
} else {
565
cinfo->output_scanline += lines_left_in_iMCU_row;
566
main_ptr->buffer_full = FALSE;
567
main_ptr->rowgroup_ctr = 0;
568
if (!master->using_merged_upsample) {
569
upsample->next_row_out = cinfo->max_v_samp_factor;
570
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
571
}
572
}
573
}
574
575
/* Calculate how many full iMCU rows we can skip. */
576
if (cinfo->upsample->need_context_rows)
577
lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
578
lines_per_iMCU_row;
579
else
580
lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
581
lines_per_iMCU_row;
582
/* Calculate the number of lines that remain to be skipped after skipping all
583
* of the full iMCU rows that we can. We will not read these lines unless we
584
* have to.
585
*/
586
lines_to_read = lines_after_iMCU_row - lines_to_skip;
587
588
/* For images requiring multiple scans (progressive, non-interleaved, etc.),
589
* all of the entropy decoding occurs in jpeg_start_decompress(), assuming
590
* that the input data source is non-suspending. This makes skipping easy.
591
*/
592
if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
593
if (cinfo->upsample->need_context_rows) {
594
cinfo->output_scanline += lines_to_skip;
595
cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
596
main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
597
/* It is complex to properly move to the middle of a context block, so
598
* read the remaining lines instead of skipping them.
599
*/
600
read_and_discard_scanlines(cinfo, lines_to_read);
601
} else {
602
cinfo->output_scanline += lines_to_skip;
603
cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
604
increment_simple_rowgroup_ctr(cinfo, lines_to_read);
605
}
606
if (!master->using_merged_upsample)
607
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
608
return num_lines;
609
}
610
611
/* Skip the iMCU rows that we can safely skip. */
612
for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
613
for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
614
for (x = 0; x < cinfo->MCUs_per_row; x++) {
615
/* Calling decode_mcu() with a NULL pointer causes it to discard the
616
* decoded coefficients. This is ~5% faster for large subsets, but
617
* it's tough to tell a difference for smaller images.
618
*/
619
if (!cinfo->entropy->insufficient_data)
620
cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
621
(*cinfo->entropy->decode_mcu) (cinfo, NULL);
622
}
623
}
624
cinfo->input_iMCU_row++;
625
cinfo->output_iMCU_row++;
626
if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
627
start_iMCU_row(cinfo);
628
else
629
(*cinfo->inputctl->finish_input_pass) (cinfo);
630
}
631
cinfo->output_scanline += lines_to_skip;
632
633
if (cinfo->upsample->need_context_rows) {
634
/* Context-based upsampling keeps track of iMCU rows. */
635
main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
636
637
/* It is complex to properly move to the middle of a context block, so
638
* read the remaining lines instead of skipping them.
639
*/
640
read_and_discard_scanlines(cinfo, lines_to_read);
641
} else {
642
increment_simple_rowgroup_ctr(cinfo, lines_to_read);
643
}
644
645
/* Since skipping lines involves skipping the upsampling step, the value of
646
* "rows_to_go" will become invalid unless we set it here. NOTE: This is a
647
* bit odd, since "rows_to_go" seems to be redundantly keeping track of
648
* output_scanline.
649
*/
650
if (!master->using_merged_upsample)
651
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
652
653
/* Always skip the requested number of lines. */
654
return num_lines;
655
}
656
657
/*
658
* Alternate entry point to read raw data.
659
* Processes exactly one iMCU row per call, unless suspended.
660
*/
661
662
GLOBAL(JDIMENSION)
663
_jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,
664
JDIMENSION max_lines)
665
{
666
JDIMENSION lines_per_iMCU_row;
667
668
if (cinfo->data_precision != BITS_IN_JSAMPLE)
669
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
670
671
if (cinfo->master->lossless)
672
ERREXIT(cinfo, JERR_NOTIMPL);
673
674
if (cinfo->global_state != DSTATE_RAW_OK)
675
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
676
if (cinfo->output_scanline >= cinfo->output_height) {
677
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
678
return 0;
679
}
680
681
/* Call progress monitor hook if present */
682
if (cinfo->progress != NULL) {
683
cinfo->progress->pass_counter = (long)cinfo->output_scanline;
684
cinfo->progress->pass_limit = (long)cinfo->output_height;
685
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
686
}
687
688
/* Verify that at least one iMCU row can be returned. */
689
lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
690
if (max_lines < lines_per_iMCU_row)
691
ERREXIT(cinfo, JERR_BUFFER_SIZE);
692
693
/* Decompress directly into user's buffer. */
694
if (!(*cinfo->coef->_decompress_data) (cinfo, data))
695
return 0; /* suspension forced, can do nothing more */
696
697
/* OK, we processed one iMCU row. */
698
cinfo->output_scanline += lines_per_iMCU_row;
699
return lines_per_iMCU_row;
700
}
701
702
#endif /* BITS_IN_JSAMPLE != 16 */
703
704
705
#if BITS_IN_JSAMPLE == 8
706
707
/* Additional entry points for buffered-image mode. */
708
709
#ifdef D_MULTISCAN_FILES_SUPPORTED
710
711
/*
712
* Initialize for an output pass in buffered-image mode.
713
*/
714
715
GLOBAL(boolean)
716
jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
717
{
718
if (cinfo->global_state != DSTATE_BUFIMAGE &&
719
cinfo->global_state != DSTATE_PRESCAN)
720
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
721
/* Limit scan number to valid range */
722
if (scan_number <= 0)
723
scan_number = 1;
724
if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
725
scan_number = cinfo->input_scan_number;
726
cinfo->output_scan_number = scan_number;
727
/* Perform any dummy output passes, and set up for the real pass */
728
return output_pass_setup(cinfo);
729
}
730
731
732
/*
733
* Finish up after an output pass in buffered-image mode.
734
*
735
* Returns FALSE if suspended. The return value need be inspected only if
736
* a suspending data source is used.
737
*/
738
739
GLOBAL(boolean)
740
jpeg_finish_output(j_decompress_ptr cinfo)
741
{
742
if ((cinfo->global_state == DSTATE_SCANNING ||
743
cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
744
/* Terminate this pass. */
745
/* We do not require the whole pass to have been completed. */
746
(*cinfo->master->finish_output_pass) (cinfo);
747
cinfo->global_state = DSTATE_BUFPOST;
748
} else if (cinfo->global_state != DSTATE_BUFPOST) {
749
/* BUFPOST = repeat call after a suspension, anything else is error */
750
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
751
}
752
/* Read markers looking for SOS or EOI */
753
while (cinfo->input_scan_number <= cinfo->output_scan_number &&
754
!cinfo->inputctl->eoi_reached) {
755
if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
756
return FALSE; /* Suspend, come back later */
757
}
758
cinfo->global_state = DSTATE_BUFIMAGE;
759
return TRUE;
760
}
761
762
#endif /* D_MULTISCAN_FILES_SUPPORTED */
763
764
#endif /* BITS_IN_JSAMPLE == 8 */
765
766