6be6e809eb684d4eb6d0426b268e86afa66f46d3
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jdtrans.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: jdtrans.c /main/2 1996/05/09 03:50:06 drk $ */
24 /*
25  * jdtrans.c
26  *
27  * Copyright (C) 1995-1996, Thomas G. Lane.
28  * This file is part of the Independent JPEG Group's software.
29  * For conditions of distribution and use, see the accompanying README file.
30  *
31  * This file contains library routines for transcoding decompression,
32  * that is, reading raw DCT coefficient arrays from an input JPEG file.
33  * The routines in jdapimin.c will also be needed by a transcoder.
34  */
35
36 #define JPEG_INTERNALS
37 #include "jinclude.h"
38 #include "jpeglib.h"
39
40
41 /* Forward declarations */
42 LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
43
44
45 /*
46  * Read the coefficient arrays from a JPEG file.
47  * jpeg_read_header must be completed before calling this.
48  *
49  * The entire image is read into a set of virtual coefficient-block arrays,
50  * one per component.  The return value is a pointer to the array of
51  * virtual-array descriptors.  These can be manipulated directly via the
52  * JPEG memory manager, or handed off to jpeg_write_coefficients().
53  * To release the memory occupied by the virtual arrays, call
54  * jpeg_finish_decompress() when done with the data.
55  *
56  * Returns NULL if suspended.  This case need be checked only if
57  * a suspending data source is used.
58  */
59
60 GLOBAL(jvirt_barray_ptr *)
61 jpeg_read_coefficients (j_decompress_ptr cinfo)
62 {
63   if (cinfo->global_state == DSTATE_READY) {
64     /* First call: initialize active modules */
65     transdecode_master_selection(cinfo);
66     cinfo->global_state = DSTATE_RDCOEFS;
67   } else if (cinfo->global_state != DSTATE_RDCOEFS)
68     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
69   /* Absorb whole file into the coef buffer */
70   for (;;) {
71     int retcode;
72     /* Call progress monitor hook if present */
73     if (cinfo->progress != NULL)
74       (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
75     /* Absorb some more input */
76     retcode = (*cinfo->inputctl->consume_input) (cinfo);
77     if (retcode == JPEG_SUSPENDED)
78       return NULL;
79     if (retcode == JPEG_REACHED_EOI)
80       break;
81     /* Advance progress counter if appropriate */
82     if (cinfo->progress != NULL &&
83         (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
84       if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
85         /* startup underestimated number of scans; ratchet up one scan */
86         cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
87       }
88     }
89   }
90   /* Set state so that jpeg_finish_decompress does the right thing */
91   cinfo->global_state = DSTATE_STOPPING;
92   return cinfo->coef->coef_arrays;
93 }
94
95
96 /*
97  * Master selection of decompression modules for transcoding.
98  * This substitutes for jdmaster.c's initialization of the full decompressor.
99  */
100
101 LOCAL(void)
102 transdecode_master_selection (j_decompress_ptr cinfo)
103 {
104   /* Entropy decoding: either Huffman or arithmetic coding. */
105   if (cinfo->arith_code) {
106     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
107   } else {
108     if (cinfo->progressive_mode) {
109 #ifdef D_PROGRESSIVE_SUPPORTED
110       jinit_phuff_decoder(cinfo);
111 #else
112       ERREXIT(cinfo, JERR_NOT_COMPILED);
113 #endif
114     } else
115       jinit_huff_decoder(cinfo);
116   }
117
118   /* Always get a full-image coefficient buffer. */
119   jinit_d_coef_controller(cinfo, TRUE);
120
121   /* We can now tell the memory manager to allocate virtual arrays. */
122   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
123
124   /* Initialize input side of decompressor to consume first scan. */
125   (*cinfo->inputctl->start_input_pass) (cinfo);
126
127   /* Initialize progress monitoring. */
128   if (cinfo->progress != NULL) {
129     int nscans;
130     /* Estimate number of scans to set pass_limit. */
131     if (cinfo->progressive_mode) {
132       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
133       nscans = 2 + 3 * cinfo->num_components;
134     } else if (cinfo->inputctl->has_multiple_scans) {
135       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
136       nscans = cinfo->num_components;
137     } else {
138       nscans = 1;
139     }
140     cinfo->progress->pass_counter = 0L;
141     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
142     cinfo->progress->completed_passes = 0;
143     cinfo->progress->total_passes = 1;
144   }
145 }