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