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