]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdtrans.c
ENH: Final -hopefully- change to jpeg lib. In order to match ITK structure, and be...
[gdcm.git] / src / gdcmjpeg / jdtrans.c
1 /*
2  * jdtrans.c
3  *
4  * Copyright (C) 1995-1998, Thomas G. Lane.
5  * 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 #include "jlossy.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  * An alternative usage is to simply obtain access to the coefficient arrays
35  * during a buffered-image-mode decompression operation.  This is allowed
36  * after any jpeg_finish_output() call.  The arrays can be accessed until
37  * jpeg_finish_decompress() is called.  (Note that any call to the library
38  * may reposition the arrays, so don't rely on access_virt_barray() results
39  * to stay valid across library calls.)
40  *
41  * Returns NULL if suspended.  This case need be checked only if
42  * a suspending data source is used.
43  */
44
45 GLOBAL(jvirt_barray_ptr *)
46 jpeg_read_coefficients (j_decompress_ptr cinfo)
47 {
48   /* j_lossy_d_ptr decomp; */
49
50   /* Can't read coefficients from lossless streams */
51   if (cinfo->process == JPROC_LOSSLESS) {
52     ERREXIT(cinfo, JERR_CANT_TRANSCODE);
53     return NULL;
54   }
55
56   if (cinfo->global_state == DSTATE_READY) {
57     /* First call: initialize active modules */
58     transdecode_master_selection(cinfo);
59     cinfo->global_state = DSTATE_RDCOEFS;
60   }
61   if (cinfo->global_state == DSTATE_RDCOEFS) {
62     /* Absorb whole file into the coef buffer */
63     for (;;) {
64       int retcode;
65       /* Call progress monitor hook if present */
66       if (cinfo->progress != NULL)
67   (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
68       /* Absorb some more input */
69       retcode = (*cinfo->inputctl->consume_input) (cinfo);
70       if (retcode == JPEG_SUSPENDED)
71   return NULL;
72       if (retcode == JPEG_REACHED_EOI)
73   break;
74       /* Advance progress counter if appropriate */
75       if (cinfo->progress != NULL &&
76     (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
77   if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
78     /* startup underestimated number of scans; ratchet up one scan */
79     cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
80   }
81       }
82     }
83     /* Set state so that jpeg_finish_decompress does the right thing */
84     cinfo->global_state = DSTATE_STOPPING;
85   }
86   /* At this point we should be in state DSTATE_STOPPING if being used
87    * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
88    * to the coefficients during a full buffered-image-mode decompression.
89    */
90   if ((cinfo->global_state == DSTATE_STOPPING ||
91        cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
92     return ((j_lossy_d_ptr) cinfo->codec)->coef_arrays;
93   }
94   /* Oops, improper usage */
95   ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
96   return NULL;      /* keep compiler happy */
97 }
98
99
100 /*
101  * Master selection of decompression modules for transcoding.
102  * This substitutes for jdmaster.c's initialization of the full decompressor.
103  */
104
105 LOCAL(void)
106 transdecode_master_selection (j_decompress_ptr cinfo)
107 {
108   /* This is effectively a buffered-image operation. */
109   cinfo->buffered_image = TRUE;
110
111   /* Initialize decompression codec */
112   jinit_d_codec(cinfo);
113
114   /* We can now tell the memory manager to allocate virtual arrays. */
115   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
116
117   /* Initialize input side of decompressor to consume first scan. */
118   (*cinfo->inputctl->start_input_pass) (cinfo);
119
120   /* Initialize progress monitoring. */
121   if (cinfo->progress != NULL) {
122     int nscans;
123     /* Estimate number of scans to set pass_limit. */
124     if (cinfo->process == JPROC_PROGRESSIVE) {
125       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
126       nscans = 2 + 3 * cinfo->num_components;
127     } else if (cinfo->inputctl->has_multiple_scans) {
128       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
129       nscans = cinfo->num_components;
130     } else {
131       nscans = 1;
132     }
133     cinfo->progress->pass_counter = 0L;
134     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
135     cinfo->progress->completed_passes = 0;
136     cinfo->progress->total_passes = 1;
137   }
138 }