Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jdtrans.c
1 /* $XConsortium: jdtrans.c /main/2 1996/05/09 03:50:06 drk $ */
2 /*
3  * jdtrans.c
4  *
5  * Copyright (C) 1995-1996, Thomas G. Lane.
6  * This file is part of the Independent JPEG Group's software.
7  * For conditions of distribution and use, see the accompanying README file.
8  *
9  * This file contains library routines for transcoding decompression,
10  * that is, reading raw DCT coefficient arrays from an input JPEG file.
11  * The routines in jdapimin.c will also be needed by a transcoder.
12  */
13
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
17
18
19 /* Forward declarations */
20 LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
21
22
23 /*
24  * Read the coefficient arrays from a JPEG file.
25  * jpeg_read_header must be completed before calling this.
26  *
27  * The entire image is read into a set of virtual coefficient-block arrays,
28  * one per component.  The return value is a pointer to the array of
29  * virtual-array descriptors.  These can be manipulated directly via the
30  * JPEG memory manager, or handed off to jpeg_write_coefficients().
31  * To release the memory occupied by the virtual arrays, call
32  * jpeg_finish_decompress() when done with the data.
33  *
34  * Returns NULL if suspended.  This case need be checked only if
35  * a suspending data source is used.
36  */
37
38 GLOBAL(jvirt_barray_ptr *)
39 jpeg_read_coefficients (j_decompress_ptr cinfo)
40 {
41   if (cinfo->global_state == DSTATE_READY) {
42     /* First call: initialize active modules */
43     transdecode_master_selection(cinfo);
44     cinfo->global_state = DSTATE_RDCOEFS;
45   } else if (cinfo->global_state != DSTATE_RDCOEFS)
46     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
47   /* Absorb whole file into the coef buffer */
48   for (;;) {
49     int retcode;
50     /* Call progress monitor hook if present */
51     if (cinfo->progress != NULL)
52       (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
53     /* Absorb some more input */
54     retcode = (*cinfo->inputctl->consume_input) (cinfo);
55     if (retcode == JPEG_SUSPENDED)
56       return NULL;
57     if (retcode == JPEG_REACHED_EOI)
58       break;
59     /* Advance progress counter if appropriate */
60     if (cinfo->progress != NULL &&
61         (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
62       if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
63         /* startup underestimated number of scans; ratchet up one scan */
64         cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
65       }
66     }
67   }
68   /* Set state so that jpeg_finish_decompress does the right thing */
69   cinfo->global_state = DSTATE_STOPPING;
70   return cinfo->coef->coef_arrays;
71 }
72
73
74 /*
75  * Master selection of decompression modules for transcoding.
76  * This substitutes for jdmaster.c's initialization of the full decompressor.
77  */
78
79 LOCAL(void)
80 transdecode_master_selection (j_decompress_ptr cinfo)
81 {
82   /* Entropy decoding: either Huffman or arithmetic coding. */
83   if (cinfo->arith_code) {
84     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
85   } else {
86     if (cinfo->progressive_mode) {
87 #ifdef D_PROGRESSIVE_SUPPORTED
88       jinit_phuff_decoder(cinfo);
89 #else
90       ERREXIT(cinfo, JERR_NOT_COMPILED);
91 #endif
92     } else
93       jinit_huff_decoder(cinfo);
94   }
95
96   /* Always get a full-image coefficient buffer. */
97   jinit_d_coef_controller(cinfo, TRUE);
98
99   /* We can now tell the memory manager to allocate virtual arrays. */
100   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
101
102   /* Initialize input side of decompressor to consume first scan. */
103   (*cinfo->inputctl->start_input_pass) (cinfo);
104
105   /* Initialize progress monitoring. */
106   if (cinfo->progress != NULL) {
107     int nscans;
108     /* Estimate number of scans to set pass_limit. */
109     if (cinfo->progressive_mode) {
110       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
111       nscans = 2 + 3 * cinfo->num_components;
112     } else if (cinfo->inputctl->has_multiple_scans) {
113       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
114       nscans = cinfo->num_components;
115     } else {
116       nscans = 1;
117     }
118     cinfo->progress->pass_counter = 0L;
119     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
120     cinfo->progress->completed_passes = 0;
121     cinfo->progress->total_passes = 1;
122   }
123 }