blob: 9d9c1b1d5e0d0ddb343f23f1a25770f06c3e9ad4 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdtrans.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1995-1997, Thomas G. Lane.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains library routines for transcoding decompression,
9 * that is, reading raw DCT coefficient arrays from an input JPEG file.
10 * The routines in jdapimin.c will also be needed by a transcoder.
11 */
12
13#define JPEG_INTERNALS
14#include "jinclude.h"
15#include "jpeglib.h"
16
17
18/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000019LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000020
21
22/*
23 * Read the coefficient arrays from a JPEG file.
24 * jpeg_read_header must be completed before calling this.
25 *
26 * The entire image is read into a set of virtual coefficient-block arrays,
27 * one per component. The return value is a pointer to the array of
28 * virtual-array descriptors. These can be manipulated directly via the
29 * JPEG memory manager, or handed off to jpeg_write_coefficients().
30 * To release the memory occupied by the virtual arrays, call
31 * jpeg_finish_decompress() when done with the data.
32 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000033 * An alternative usage is to simply obtain access to the coefficient arrays
34 * during a buffered-image-mode decompression operation. This is allowed
35 * after any jpeg_finish_output() call. The arrays can be accessed until
36 * jpeg_finish_decompress() is called. (Note that any call to the library
37 * may reposition the arrays, so don't rely on access_virt_barray() results
38 * to stay valid across library calls.)
39 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +000040 * Returns NULL if suspended. This case need be checked only if
41 * a suspending data source is used.
42 */
43
Thomas G. Lane489583f1996-02-07 00:00:00 +000044GLOBAL(jvirt_barray_ptr *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000045jpeg_read_coefficients (j_decompress_ptr cinfo)
46{
47 if (cinfo->global_state == DSTATE_READY) {
48 /* First call: initialize active modules */
49 transdecode_master_selection(cinfo);
50 cinfo->global_state = DSTATE_RDCOEFS;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000051 }
52 if (cinfo->global_state == DSTATE_RDCOEFS) {
53 /* Absorb whole file into the coef buffer */
54 for (;;) {
55 int retcode;
56 /* Call progress monitor hook if present */
57 if (cinfo->progress != NULL)
58 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
59 /* Absorb some more input */
60 retcode = (*cinfo->inputctl->consume_input) (cinfo);
61 if (retcode == JPEG_SUSPENDED)
62 return NULL;
63 if (retcode == JPEG_REACHED_EOI)
64 break;
65 /* Advance progress counter if appropriate */
66 if (cinfo->progress != NULL &&
67 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
68 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
69 /* startup underestimated number of scans; ratchet up one scan */
70 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
71 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +000072 }
73 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000074 /* Set state so that jpeg_finish_decompress does the right thing */
75 cinfo->global_state = DSTATE_STOPPING;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000076 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000077 /* At this point we should be in state DSTATE_STOPPING if being used
78 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
79 * to the coefficients during a full buffered-image-mode decompression.
80 */
81 if ((cinfo->global_state == DSTATE_STOPPING ||
82 cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
83 return cinfo->coef->coef_arrays;
84 }
85 /* Oops, improper usage */
86 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
87 return NULL; /* keep compiler happy */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000088}
89
90
91/*
92 * Master selection of decompression modules for transcoding.
93 * This substitutes for jdmaster.c's initialization of the full decompressor.
94 */
95
Thomas G. Lane489583f1996-02-07 00:00:00 +000096LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097transdecode_master_selection (j_decompress_ptr cinfo)
98{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000099 /* This is effectively a buffered-image operation. */
100 cinfo->buffered_image = TRUE;
101
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000102 /* Entropy decoding: either Huffman or arithmetic coding. */
103 if (cinfo->arith_code) {
DRC66f97e62010-11-23 05:49:54 +0000104#ifdef D_ARITH_CODING_SUPPORTED
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000105 jinit_arith_decoder(cinfo);
DRC66f97e62010-11-23 05:49:54 +0000106#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000107 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
DRC66f97e62010-11-23 05:49:54 +0000108#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000109 } else {
110 if (cinfo->progressive_mode) {
111#ifdef D_PROGRESSIVE_SUPPORTED
112 jinit_phuff_decoder(cinfo);
113#else
114 ERREXIT(cinfo, JERR_NOT_COMPILED);
115#endif
116 } else
117 jinit_huff_decoder(cinfo);
118 }
119
120 /* Always get a full-image coefficient buffer. */
121 jinit_d_coef_controller(cinfo, TRUE);
122
123 /* We can now tell the memory manager to allocate virtual arrays. */
124 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
125
126 /* Initialize input side of decompressor to consume first scan. */
127 (*cinfo->inputctl->start_input_pass) (cinfo);
128
129 /* Initialize progress monitoring. */
130 if (cinfo->progress != NULL) {
131 int nscans;
132 /* Estimate number of scans to set pass_limit. */
133 if (cinfo->progressive_mode) {
134 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
135 nscans = 2 + 3 * cinfo->num_components;
136 } else if (cinfo->inputctl->has_multiple_scans) {
137 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
138 nscans = cinfo->num_components;
139 } else {
140 nscans = 1;
141 }
142 cinfo->progress->pass_counter = 0L;
143 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
144 cinfo->progress->completed_passes = 0;
145 cinfo->progress->total_passes = 1;
146 }
147}