xref: /aosp_15_r20/external/libjpeg-turbo/jdatasrc-tj.c (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker  * jdatasrc-tj.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) 1994-1996, Thomas G. Lane.
6*dfc6aa5cSAndroid Build Coastguard Worker  * Modified 2009-2011 by Guido Vollbeding.
7*dfc6aa5cSAndroid Build Coastguard Worker  * libjpeg-turbo Modifications:
8*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright (C) 2011, 2016, 2019, D. R. Commander.
9*dfc6aa5cSAndroid Build Coastguard Worker  * For conditions of distribution and use, see the accompanying README.ijg
10*dfc6aa5cSAndroid Build Coastguard Worker  * file.
11*dfc6aa5cSAndroid Build Coastguard Worker  *
12*dfc6aa5cSAndroid Build Coastguard Worker  * This file contains decompression data source routines for the case of
13*dfc6aa5cSAndroid Build Coastguard Worker  * reading JPEG data from memory or from a file (or any stdio stream).
14*dfc6aa5cSAndroid Build Coastguard Worker  * While these routines are sufficient for most applications,
15*dfc6aa5cSAndroid Build Coastguard Worker  * some will want to use a different source manager.
16*dfc6aa5cSAndroid Build Coastguard Worker  * IMPORTANT: we assume that fread() will correctly transcribe an array of
17*dfc6aa5cSAndroid Build Coastguard Worker  * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
18*dfc6aa5cSAndroid Build Coastguard Worker  * than 8 bits on your machine, you may need to do some tweaking.
19*dfc6aa5cSAndroid Build Coastguard Worker  */
20*dfc6aa5cSAndroid Build Coastguard Worker 
21*dfc6aa5cSAndroid Build Coastguard Worker /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
22*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
23*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
24*dfc6aa5cSAndroid Build Coastguard Worker #include "jerror.h"
25*dfc6aa5cSAndroid Build Coastguard Worker 
26*dfc6aa5cSAndroid Build Coastguard Worker void jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
27*dfc6aa5cSAndroid Build Coastguard Worker                      unsigned long insize);
28*dfc6aa5cSAndroid Build Coastguard Worker 
29*dfc6aa5cSAndroid Build Coastguard Worker 
30*dfc6aa5cSAndroid Build Coastguard Worker /*
31*dfc6aa5cSAndroid Build Coastguard Worker  * Initialize source --- called by jpeg_read_header
32*dfc6aa5cSAndroid Build Coastguard Worker  * before any data is actually read.
33*dfc6aa5cSAndroid Build Coastguard Worker  */
34*dfc6aa5cSAndroid Build Coastguard Worker 
35*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
init_mem_source(j_decompress_ptr cinfo)36*dfc6aa5cSAndroid Build Coastguard Worker init_mem_source(j_decompress_ptr cinfo)
37*dfc6aa5cSAndroid Build Coastguard Worker {
38*dfc6aa5cSAndroid Build Coastguard Worker   /* no work necessary here */
39*dfc6aa5cSAndroid Build Coastguard Worker }
40*dfc6aa5cSAndroid Build Coastguard Worker 
41*dfc6aa5cSAndroid Build Coastguard Worker 
42*dfc6aa5cSAndroid Build Coastguard Worker /*
43*dfc6aa5cSAndroid Build Coastguard Worker  * Fill the input buffer --- called whenever buffer is emptied.
44*dfc6aa5cSAndroid Build Coastguard Worker  *
45*dfc6aa5cSAndroid Build Coastguard Worker  * In typical applications, this should read fresh data into the buffer
46*dfc6aa5cSAndroid Build Coastguard Worker  * (ignoring the current state of next_input_byte & bytes_in_buffer),
47*dfc6aa5cSAndroid Build Coastguard Worker  * reset the pointer & count to the start of the buffer, and return TRUE
48*dfc6aa5cSAndroid Build Coastguard Worker  * indicating that the buffer has been reloaded.  It is not necessary to
49*dfc6aa5cSAndroid Build Coastguard Worker  * fill the buffer entirely, only to obtain at least one more byte.
50*dfc6aa5cSAndroid Build Coastguard Worker  *
51*dfc6aa5cSAndroid Build Coastguard Worker  * There is no such thing as an EOF return.  If the end of the file has been
52*dfc6aa5cSAndroid Build Coastguard Worker  * reached, the routine has a choice of ERREXIT() or inserting fake data into
53*dfc6aa5cSAndroid Build Coastguard Worker  * the buffer.  In most cases, generating a warning message and inserting a
54*dfc6aa5cSAndroid Build Coastguard Worker  * fake EOI marker is the best course of action --- this will allow the
55*dfc6aa5cSAndroid Build Coastguard Worker  * decompressor to output however much of the image is there.  However,
56*dfc6aa5cSAndroid Build Coastguard Worker  * the resulting error message is misleading if the real problem is an empty
57*dfc6aa5cSAndroid Build Coastguard Worker  * input file, so we handle that case specially.
58*dfc6aa5cSAndroid Build Coastguard Worker  *
59*dfc6aa5cSAndroid Build Coastguard Worker  * In applications that need to be able to suspend compression due to input
60*dfc6aa5cSAndroid Build Coastguard Worker  * not being available yet, a FALSE return indicates that no more data can be
61*dfc6aa5cSAndroid Build Coastguard Worker  * obtained right now, but more may be forthcoming later.  In this situation,
62*dfc6aa5cSAndroid Build Coastguard Worker  * the decompressor will return to its caller (with an indication of the
63*dfc6aa5cSAndroid Build Coastguard Worker  * number of scanlines it has read, if any).  The application should resume
64*dfc6aa5cSAndroid Build Coastguard Worker  * decompression after it has loaded more data into the input buffer.  Note
65*dfc6aa5cSAndroid Build Coastguard Worker  * that there are substantial restrictions on the use of suspension --- see
66*dfc6aa5cSAndroid Build Coastguard Worker  * the documentation.
67*dfc6aa5cSAndroid Build Coastguard Worker  *
68*dfc6aa5cSAndroid Build Coastguard Worker  * When suspending, the decompressor will back up to a convenient restart point
69*dfc6aa5cSAndroid Build Coastguard Worker  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
70*dfc6aa5cSAndroid Build Coastguard Worker  * indicate where the restart point will be if the current call returns FALSE.
71*dfc6aa5cSAndroid Build Coastguard Worker  * Data beyond this point must be rescanned after resumption, so move it to
72*dfc6aa5cSAndroid Build Coastguard Worker  * the front of the buffer rather than discarding it.
73*dfc6aa5cSAndroid Build Coastguard Worker  */
74*dfc6aa5cSAndroid Build Coastguard Worker 
75*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(boolean)
fill_mem_input_buffer(j_decompress_ptr cinfo)76*dfc6aa5cSAndroid Build Coastguard Worker fill_mem_input_buffer(j_decompress_ptr cinfo)
77*dfc6aa5cSAndroid Build Coastguard Worker {
78*dfc6aa5cSAndroid Build Coastguard Worker   static const JOCTET mybuffer[4] = {
79*dfc6aa5cSAndroid Build Coastguard Worker     (JOCTET)0xFF, (JOCTET)JPEG_EOI, 0, 0
80*dfc6aa5cSAndroid Build Coastguard Worker   };
81*dfc6aa5cSAndroid Build Coastguard Worker 
82*dfc6aa5cSAndroid Build Coastguard Worker   /* The whole JPEG data is expected to reside in the supplied memory
83*dfc6aa5cSAndroid Build Coastguard Worker    * buffer, so any request for more data beyond the given buffer size
84*dfc6aa5cSAndroid Build Coastguard Worker    * is treated as an error.
85*dfc6aa5cSAndroid Build Coastguard Worker    */
86*dfc6aa5cSAndroid Build Coastguard Worker   WARNMS(cinfo, JWRN_JPEG_EOF);
87*dfc6aa5cSAndroid Build Coastguard Worker 
88*dfc6aa5cSAndroid Build Coastguard Worker   /* Insert a fake EOI marker */
89*dfc6aa5cSAndroid Build Coastguard Worker 
90*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->src->next_input_byte = mybuffer;
91*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->src->bytes_in_buffer = 2;
92*dfc6aa5cSAndroid Build Coastguard Worker 
93*dfc6aa5cSAndroid Build Coastguard Worker   return TRUE;
94*dfc6aa5cSAndroid Build Coastguard Worker }
95*dfc6aa5cSAndroid Build Coastguard Worker 
96*dfc6aa5cSAndroid Build Coastguard Worker 
97*dfc6aa5cSAndroid Build Coastguard Worker /*
98*dfc6aa5cSAndroid Build Coastguard Worker  * Skip data --- used to skip over a potentially large amount of
99*dfc6aa5cSAndroid Build Coastguard Worker  * uninteresting data (such as an APPn marker).
100*dfc6aa5cSAndroid Build Coastguard Worker  *
101*dfc6aa5cSAndroid Build Coastguard Worker  * Writers of suspendable-input applications must note that skip_input_data
102*dfc6aa5cSAndroid Build Coastguard Worker  * is not granted the right to give a suspension return.  If the skip extends
103*dfc6aa5cSAndroid Build Coastguard Worker  * beyond the data currently in the buffer, the buffer can be marked empty so
104*dfc6aa5cSAndroid Build Coastguard Worker  * that the next read will cause a fill_input_buffer call that can suspend.
105*dfc6aa5cSAndroid Build Coastguard Worker  * Arranging for additional bytes to be discarded before reloading the input
106*dfc6aa5cSAndroid Build Coastguard Worker  * buffer is the application writer's problem.
107*dfc6aa5cSAndroid Build Coastguard Worker  */
108*dfc6aa5cSAndroid Build Coastguard Worker 
109*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
skip_input_data(j_decompress_ptr cinfo,long num_bytes)110*dfc6aa5cSAndroid Build Coastguard Worker skip_input_data(j_decompress_ptr cinfo, long num_bytes)
111*dfc6aa5cSAndroid Build Coastguard Worker {
112*dfc6aa5cSAndroid Build Coastguard Worker   struct jpeg_source_mgr *src = cinfo->src;
113*dfc6aa5cSAndroid Build Coastguard Worker 
114*dfc6aa5cSAndroid Build Coastguard Worker   /* Just a dumb implementation for now.  Could use fseek() except
115*dfc6aa5cSAndroid Build Coastguard Worker    * it doesn't work on pipes.  Not clear that being smart is worth
116*dfc6aa5cSAndroid Build Coastguard Worker    * any trouble anyway --- large skips are infrequent.
117*dfc6aa5cSAndroid Build Coastguard Worker    */
118*dfc6aa5cSAndroid Build Coastguard Worker   if (num_bytes > 0) {
119*dfc6aa5cSAndroid Build Coastguard Worker     while (num_bytes > (long)src->bytes_in_buffer) {
120*dfc6aa5cSAndroid Build Coastguard Worker       num_bytes -= (long)src->bytes_in_buffer;
121*dfc6aa5cSAndroid Build Coastguard Worker       (void)(*src->fill_input_buffer) (cinfo);
122*dfc6aa5cSAndroid Build Coastguard Worker       /* note we assume that fill_input_buffer will never return FALSE,
123*dfc6aa5cSAndroid Build Coastguard Worker        * so suspension need not be handled.
124*dfc6aa5cSAndroid Build Coastguard Worker        */
125*dfc6aa5cSAndroid Build Coastguard Worker     }
126*dfc6aa5cSAndroid Build Coastguard Worker     src->next_input_byte += (size_t)num_bytes;
127*dfc6aa5cSAndroid Build Coastguard Worker     src->bytes_in_buffer -= (size_t)num_bytes;
128*dfc6aa5cSAndroid Build Coastguard Worker   }
129*dfc6aa5cSAndroid Build Coastguard Worker }
130*dfc6aa5cSAndroid Build Coastguard Worker 
131*dfc6aa5cSAndroid Build Coastguard Worker 
132*dfc6aa5cSAndroid Build Coastguard Worker /*
133*dfc6aa5cSAndroid Build Coastguard Worker  * An additional method that can be provided by data source modules is the
134*dfc6aa5cSAndroid Build Coastguard Worker  * resync_to_restart method for error recovery in the presence of RST markers.
135*dfc6aa5cSAndroid Build Coastguard Worker  * For the moment, this source module just uses the default resync method
136*dfc6aa5cSAndroid Build Coastguard Worker  * provided by the JPEG library.  That method assumes that no backtracking
137*dfc6aa5cSAndroid Build Coastguard Worker  * is possible.
138*dfc6aa5cSAndroid Build Coastguard Worker  */
139*dfc6aa5cSAndroid Build Coastguard Worker 
140*dfc6aa5cSAndroid Build Coastguard Worker 
141*dfc6aa5cSAndroid Build Coastguard Worker /*
142*dfc6aa5cSAndroid Build Coastguard Worker  * Terminate source --- called by jpeg_finish_decompress
143*dfc6aa5cSAndroid Build Coastguard Worker  * after all data has been read.  Often a no-op.
144*dfc6aa5cSAndroid Build Coastguard Worker  *
145*dfc6aa5cSAndroid Build Coastguard Worker  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
146*dfc6aa5cSAndroid Build Coastguard Worker  * application must deal with any cleanup that should happen even
147*dfc6aa5cSAndroid Build Coastguard Worker  * for error exit.
148*dfc6aa5cSAndroid Build Coastguard Worker  */
149*dfc6aa5cSAndroid Build Coastguard Worker 
150*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
term_source(j_decompress_ptr cinfo)151*dfc6aa5cSAndroid Build Coastguard Worker term_source(j_decompress_ptr cinfo)
152*dfc6aa5cSAndroid Build Coastguard Worker {
153*dfc6aa5cSAndroid Build Coastguard Worker   /* no work necessary here */
154*dfc6aa5cSAndroid Build Coastguard Worker }
155*dfc6aa5cSAndroid Build Coastguard Worker 
156*dfc6aa5cSAndroid Build Coastguard Worker 
157*dfc6aa5cSAndroid Build Coastguard Worker /*
158*dfc6aa5cSAndroid Build Coastguard Worker  * Prepare for input from a supplied memory buffer.
159*dfc6aa5cSAndroid Build Coastguard Worker  * The buffer must contain the whole JPEG data.
160*dfc6aa5cSAndroid Build Coastguard Worker  */
161*dfc6aa5cSAndroid Build Coastguard Worker 
162*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jpeg_mem_src_tj(j_decompress_ptr cinfo,const unsigned char * inbuffer,unsigned long insize)163*dfc6aa5cSAndroid Build Coastguard Worker jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
164*dfc6aa5cSAndroid Build Coastguard Worker                 unsigned long insize)
165*dfc6aa5cSAndroid Build Coastguard Worker {
166*dfc6aa5cSAndroid Build Coastguard Worker   struct jpeg_source_mgr *src;
167*dfc6aa5cSAndroid Build Coastguard Worker 
168*dfc6aa5cSAndroid Build Coastguard Worker   if (inbuffer == NULL || insize == 0)  /* Treat empty input as fatal error */
169*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_INPUT_EMPTY);
170*dfc6aa5cSAndroid Build Coastguard Worker 
171*dfc6aa5cSAndroid Build Coastguard Worker   /* The source object is made permanent so that a series of JPEG images
172*dfc6aa5cSAndroid Build Coastguard Worker    * can be read from the same buffer by calling jpeg_mem_src only before
173*dfc6aa5cSAndroid Build Coastguard Worker    * the first one.
174*dfc6aa5cSAndroid Build Coastguard Worker    */
175*dfc6aa5cSAndroid Build Coastguard Worker   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
176*dfc6aa5cSAndroid Build Coastguard Worker     cinfo->src = (struct jpeg_source_mgr *)
177*dfc6aa5cSAndroid Build Coastguard Worker       (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
178*dfc6aa5cSAndroid Build Coastguard Worker                                   sizeof(struct jpeg_source_mgr));
179*dfc6aa5cSAndroid Build Coastguard Worker   } else if (cinfo->src->init_source != init_mem_source) {
180*dfc6aa5cSAndroid Build Coastguard Worker     /* It is unsafe to reuse the existing source manager unless it was created
181*dfc6aa5cSAndroid Build Coastguard Worker      * by this function.
182*dfc6aa5cSAndroid Build Coastguard Worker      */
183*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BUFFER_SIZE);
184*dfc6aa5cSAndroid Build Coastguard Worker   }
185*dfc6aa5cSAndroid Build Coastguard Worker 
186*dfc6aa5cSAndroid Build Coastguard Worker   src = cinfo->src;
187*dfc6aa5cSAndroid Build Coastguard Worker   src->init_source = init_mem_source;
188*dfc6aa5cSAndroid Build Coastguard Worker   src->fill_input_buffer = fill_mem_input_buffer;
189*dfc6aa5cSAndroid Build Coastguard Worker   src->skip_input_data = skip_input_data;
190*dfc6aa5cSAndroid Build Coastguard Worker   src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
191*dfc6aa5cSAndroid Build Coastguard Worker   src->term_source = term_source;
192*dfc6aa5cSAndroid Build Coastguard Worker   src->bytes_in_buffer = (size_t)insize;
193*dfc6aa5cSAndroid Build Coastguard Worker   src->next_input_byte = (const JOCTET *)inbuffer;
194*dfc6aa5cSAndroid Build Coastguard Worker }
195