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