blob: 86fda3b8b27cc7748db9638b7e213e63cadcb673 [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.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains library routines for transcoding decompression,
11 * that is, reading raw DCT coefficient arrays from an input JPEG file.
12 * The routines in jdapimin.c will also be needed by a transcoder.
13 */
14
15#define JPEG_INTERNALS
16#include "jinclude.h"
17#include "jpeglib.h"
18
19
20/* Forward declarations */
DRCbc56b752014-05-16 10:43:44 +000021LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000022
23
24/*
25 * Read the coefficient arrays from a JPEG file.
26 * jpeg_read_header must be completed before calling this.
27 *
28 * The entire image is read into a set of virtual coefficient-block arrays,
29 * one per component. The return value is a pointer to the array of
30 * virtual-array descriptors. These can be manipulated directly via the
31 * JPEG memory manager, or handed off to jpeg_write_coefficients().
32 * To release the memory occupied by the virtual arrays, call
33 * jpeg_finish_decompress() when done with the data.
34 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000035 * An alternative usage is to simply obtain access to the coefficient arrays
36 * during a buffered-image-mode decompression operation. This is allowed
37 * after any jpeg_finish_output() call. The arrays can be accessed until
38 * jpeg_finish_decompress() is called. (Note that any call to the library
39 * may reposition the arrays, so don't rely on access_virt_barray() results
40 * to stay valid across library calls.)
41 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +000042 * Returns NULL if suspended. This case need be checked only if
43 * a suspending data source is used.
44 */
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046GLOBAL(jvirt_barray_ptr *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047jpeg_read_coefficients (j_decompress_ptr cinfo)
48{
49 if (cinfo->global_state == DSTATE_READY) {
50 /* First call: initialize active modules */
51 transdecode_master_selection(cinfo);
52 cinfo->global_state = DSTATE_RDCOEFS;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000053 }
54 if (cinfo->global_state == DSTATE_RDCOEFS) {
55 /* Absorb whole file into the coef buffer */
56 for (;;) {
57 int retcode;
58 /* Call progress monitor hook if present */
59 if (cinfo->progress != NULL)
DRCe5eaf372014-05-09 18:00:32 +000060 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000061 /* Absorb some more input */
62 retcode = (*cinfo->inputctl->consume_input) (cinfo);
63 if (retcode == JPEG_SUSPENDED)
DRCe5eaf372014-05-09 18:00:32 +000064 return NULL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000065 if (retcode == JPEG_REACHED_EOI)
DRCe5eaf372014-05-09 18:00:32 +000066 break;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000067 /* Advance progress counter if appropriate */
68 if (cinfo->progress != NULL &&
DRCe5eaf372014-05-09 18:00:32 +000069 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
70 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
71 /* startup underestimated number of scans; ratchet up one scan */
72 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
73 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +000074 }
75 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000076 /* Set state so that jpeg_finish_decompress does the right thing */
77 cinfo->global_state = DSTATE_STOPPING;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000078 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000079 /* At this point we should be in state DSTATE_STOPPING if being used
80 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
81 * to the coefficients during a full buffered-image-mode decompression.
82 */
83 if ((cinfo->global_state == DSTATE_STOPPING ||
84 cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
85 return cinfo->coef->coef_arrays;
86 }
87 /* Oops, improper usage */
88 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
DRCe5eaf372014-05-09 18:00:32 +000089 return NULL; /* keep compiler happy */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000090}
91
92
93/*
94 * Master selection of decompression modules for transcoding.
95 * This substitutes for jdmaster.c's initialization of the full decompressor.
96 */
97
Thomas G. Lane489583f1996-02-07 00:00:00 +000098LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000099transdecode_master_selection (j_decompress_ptr cinfo)
100{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000101 /* This is effectively a buffered-image operation. */
102 cinfo->buffered_image = TRUE;
103
DRCdf489452011-04-02 05:37:49 +0000104#if JPEG_LIB_VERSION >= 80
Guido Vollbeding989630f2010-01-10 00:00:00 +0000105 /* Compute output image dimensions and related values. */
106 jpeg_core_output_dimensions(cinfo);
DRCdf489452011-04-02 05:37:49 +0000107#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +0000108
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000109 /* Entropy decoding: either Huffman or arithmetic coding. */
110 if (cinfo->arith_code) {
DRC66f97e62010-11-23 05:49:54 +0000111#ifdef D_ARITH_CODING_SUPPORTED
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000112 jinit_arith_decoder(cinfo);
DRC66f97e62010-11-23 05:49:54 +0000113#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000114 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
DRC66f97e62010-11-23 05:49:54 +0000115#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000116 } else {
117 if (cinfo->progressive_mode) {
118#ifdef D_PROGRESSIVE_SUPPORTED
119 jinit_phuff_decoder(cinfo);
120#else
121 ERREXIT(cinfo, JERR_NOT_COMPILED);
122#endif
123 } else
124 jinit_huff_decoder(cinfo);
125 }
126
127 /* Always get a full-image coefficient buffer. */
128 jinit_d_coef_controller(cinfo, TRUE);
129
130 /* We can now tell the memory manager to allocate virtual arrays. */
131 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
132
133 /* Initialize input side of decompressor to consume first scan. */
134 (*cinfo->inputctl->start_input_pass) (cinfo);
135
136 /* Initialize progress monitoring. */
137 if (cinfo->progress != NULL) {
138 int nscans;
139 /* Estimate number of scans to set pass_limit. */
140 if (cinfo->progressive_mode) {
141 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
142 nscans = 2 + 3 * cinfo->num_components;
143 } else if (cinfo->inputctl->has_multiple_scans) {
144 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
145 nscans = cinfo->num_components;
146 } else {
147 nscans = 1;
148 }
149 cinfo->progress->pass_counter = 0L;
150 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
151 cinfo->progress->completed_passes = 0;
152 cinfo->progress->total_passes = 1;
153 }
154}