blob: 56713efe64d6b808d58c887a8c138a139a7bf32a [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdtrans.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1995-1997, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000010 *
11 * This file contains library routines for transcoding decompression,
12 * that is, reading raw DCT coefficient arrays from an input JPEG file.
13 * The routines in jdapimin.c will also be needed by a transcoder.
14 */
15
16#define JPEG_INTERNALS
17#include "jinclude.h"
18#include "jpeglib.h"
19
20
21/* Forward declarations */
Leon Scroggins III3993b372018-07-16 10:43:45 -040022LOCAL(void) transdecode_master_selection(j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000023
24
25/*
26 * Read the coefficient arrays from a JPEG file.
27 * jpeg_read_header must be completed before calling this.
28 *
29 * The entire image is read into a set of virtual coefficient-block arrays,
30 * one per component. The return value is a pointer to the array of
31 * virtual-array descriptors. These can be manipulated directly via the
32 * JPEG memory manager, or handed off to jpeg_write_coefficients().
33 * To release the memory occupied by the virtual arrays, call
34 * jpeg_finish_decompress() when done with the data.
35 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000036 * An alternative usage is to simply obtain access to the coefficient arrays
37 * during a buffered-image-mode decompression operation. This is allowed
38 * after any jpeg_finish_output() call. The arrays can be accessed until
39 * jpeg_finish_decompress() is called. (Note that any call to the library
40 * may reposition the arrays, so don't rely on access_virt_barray() results
41 * to stay valid across library calls.)
42 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +000043 * Returns NULL if suspended. This case need be checked only if
44 * a suspending data source is used.
45 */
46
Thomas G. Lane489583f1996-02-07 00:00:00 +000047GLOBAL(jvirt_barray_ptr *)
Leon Scroggins III3993b372018-07-16 10:43:45 -040048jpeg_read_coefficients(j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000049{
50 if (cinfo->global_state == DSTATE_READY) {
51 /* First call: initialize active modules */
52 transdecode_master_selection(cinfo);
53 cinfo->global_state = DSTATE_RDCOEFS;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000054 }
55 if (cinfo->global_state == DSTATE_RDCOEFS) {
56 /* Absorb whole file into the coef buffer */
57 for (;;) {
58 int retcode;
59 /* Call progress monitor hook if present */
60 if (cinfo->progress != NULL)
Leon Scroggins III3993b372018-07-16 10:43:45 -040061 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000062 /* Absorb some more input */
63 retcode = (*cinfo->inputctl->consume_input) (cinfo);
64 if (retcode == JPEG_SUSPENDED)
DRCe5eaf372014-05-09 18:00:32 +000065 return NULL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000066 if (retcode == JPEG_REACHED_EOI)
DRCe5eaf372014-05-09 18:00:32 +000067 break;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000068 /* Advance progress counter if appropriate */
69 if (cinfo->progress != NULL &&
DRCe5eaf372014-05-09 18:00:32 +000070 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
71 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
72 /* startup underestimated number of scans; ratchet up one scan */
Leon Scroggins III3993b372018-07-16 10:43:45 -040073 cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
DRCe5eaf372014-05-09 18:00:32 +000074 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +000075 }
76 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000077 /* Set state so that jpeg_finish_decompress does the right thing */
78 cinfo->global_state = DSTATE_STOPPING;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000079 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000080 /* At this point we should be in state DSTATE_STOPPING if being used
81 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
82 * to the coefficients during a full buffered-image-mode decompression.
83 */
84 if ((cinfo->global_state == DSTATE_STOPPING ||
85 cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
86 return cinfo->coef->coef_arrays;
87 }
88 /* Oops, improper usage */
89 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
DRCe5eaf372014-05-09 18:00:32 +000090 return NULL; /* keep compiler happy */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000091}
92
93
94/*
95 * Master selection of decompression modules for transcoding.
96 * This substitutes for jdmaster.c's initialization of the full decompressor.
97 */
98
Thomas G. Lane489583f1996-02-07 00:00:00 +000099LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400100transdecode_master_selection(j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000101{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000102 /* This is effectively a buffered-image operation. */
103 cinfo->buffered_image = TRUE;
104
DRCdf489452011-04-02 05:37:49 +0000105#if JPEG_LIB_VERSION >= 80
Guido Vollbeding989630f2010-01-10 00:00:00 +0000106 /* Compute output image dimensions and related values. */
107 jpeg_core_output_dimensions(cinfo);
DRCdf489452011-04-02 05:37:49 +0000108#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +0000109
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000110 /* Entropy decoding: either Huffman or arithmetic coding. */
111 if (cinfo->arith_code) {
DRC66f97e62010-11-23 05:49:54 +0000112#ifdef D_ARITH_CODING_SUPPORTED
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000113 jinit_arith_decoder(cinfo);
DRC66f97e62010-11-23 05:49:54 +0000114#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000115 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
DRC66f97e62010-11-23 05:49:54 +0000116#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000117 } else {
118 if (cinfo->progressive_mode) {
119#ifdef D_PROGRESSIVE_SUPPORTED
120 jinit_phuff_decoder(cinfo);
121#else
122 ERREXIT(cinfo, JERR_NOT_COMPILED);
123#endif
124 } else
125 jinit_huff_decoder(cinfo);
126 }
127
128 /* Always get a full-image coefficient buffer. */
129 jinit_d_coef_controller(cinfo, TRUE);
130
131 /* We can now tell the memory manager to allocate virtual arrays. */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400132 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000133
134 /* Initialize input side of decompressor to consume first scan. */
135 (*cinfo->inputctl->start_input_pass) (cinfo);
136
137 /* Initialize progress monitoring. */
138 if (cinfo->progress != NULL) {
139 int nscans;
140 /* Estimate number of scans to set pass_limit. */
141 if (cinfo->progressive_mode) {
142 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
143 nscans = 2 + 3 * cinfo->num_components;
144 } else if (cinfo->inputctl->has_multiple_scans) {
145 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
146 nscans = cinfo->num_components;
147 } else {
148 nscans = 1;
149 }
150 cinfo->progress->pass_counter = 0L;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400151 cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000152 cinfo->progress->completed_passes = 0;
153 cinfo->progress->total_passes = 1;
154 }
155}