4609b1322fb842cacbbf607610279c3c3d502fa5
[platform/upstream/libjpeg-turbo.git] / jdapimin.c
1 /*
2  * jdapimin.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1994-1998, Thomas G. Lane.
6  * libjpeg-turbo Modifications:
7  * Copyright (C) 2016, D. R. Commander.
8  * For conditions of distribution and use, see the accompanying README.ijg
9  * file.
10  *
11  * This file contains application interface code for the decompression half
12  * of the JPEG library.  These are the "minimum" API routines that may be
13  * needed in either the normal full-decompression case or the
14  * transcoding-only case.
15  *
16  * Most of the routines intended to be called directly by an application
17  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
18  * shared by compression and decompression, and jdtrans.c for the transcoding
19  * case.
20  */
21
22 #define JPEG_INTERNALS
23 #include "jinclude.h"
24 #include "jpeglib.h"
25 #include "jdmaster.h"
26 #include "jconfigint.h"
27
28
29 /*
30  * Initialization of a JPEG decompression object.
31  * The error manager must already be set up (in case memory manager fails).
32  */
33
34 GLOBAL(void)
35 jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
36 {
37   int i;
38
39   /* Guard against version mismatches between library and caller. */
40   cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
41   if (version != JPEG_LIB_VERSION)
42     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
43   if (structsize != sizeof(struct jpeg_decompress_struct))
44     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
45              (int)sizeof(struct jpeg_decompress_struct), (int)structsize);
46
47   /* For debugging purposes, we zero the whole master structure.
48    * But the application has already set the err pointer, and may have set
49    * client_data, so we have to save and restore those fields.
50    * Note: if application hasn't set client_data, tools like Purify may
51    * complain here.
52    */
53   {
54     struct jpeg_error_mgr *err = cinfo->err;
55     void *client_data = cinfo->client_data; /* ignore Purify complaint here */
56     MEMZERO(cinfo, sizeof(struct jpeg_decompress_struct));
57     cinfo->err = err;
58     cinfo->client_data = client_data;
59   }
60   cinfo->is_decompressor = TRUE;
61
62   /* Initialize a memory manager instance for this object */
63   jinit_memory_mgr((j_common_ptr)cinfo);
64
65   /* Zero out pointers to permanent structures. */
66   cinfo->progress = NULL;
67   cinfo->src = NULL;
68
69   for (i = 0; i < NUM_QUANT_TBLS; i++)
70     cinfo->quant_tbl_ptrs[i] = NULL;
71
72   for (i = 0; i < NUM_HUFF_TBLS; i++) {
73     cinfo->dc_huff_tbl_ptrs[i] = NULL;
74     cinfo->ac_huff_tbl_ptrs[i] = NULL;
75   }
76
77   /* Initialize marker processor so application can override methods
78    * for COM, APPn markers before calling jpeg_read_header.
79    */
80   cinfo->marker_list = NULL;
81   jinit_marker_reader(cinfo);
82
83   /* And initialize the overall input controller. */
84   jinit_input_controller(cinfo);
85
86   /* OK, I'm ready */
87   cinfo->global_state = DSTATE_START;
88
89   /* The master struct is used to store extension parameters, so we allocate it
90    * here.
91    */
92   cinfo->master = (struct jpeg_decomp_master *)
93     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
94                                 sizeof(my_decomp_master));
95   MEMZERO(cinfo->master, sizeof(my_decomp_master));
96 }
97
98
99 /*
100  * Destruction of a JPEG decompression object
101  */
102
103 GLOBAL(void)
104 jpeg_destroy_decompress(j_decompress_ptr cinfo)
105 {
106   jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
107 }
108
109
110 /*
111  * Abort processing of a JPEG decompression operation,
112  * but don't destroy the object itself.
113  */
114
115 GLOBAL(void)
116 jpeg_abort_decompress(j_decompress_ptr cinfo)
117 {
118   jpeg_abort((j_common_ptr)cinfo); /* use common routine */
119 }
120
121
122 /*
123  * Set default decompression parameters.
124  */
125
126 LOCAL(void)
127 default_decompress_parms(j_decompress_ptr cinfo)
128 {
129   /* Guess the input colorspace, and set output colorspace accordingly. */
130   /* (Wish JPEG committee had provided a real way to specify this...) */
131   /* Note application may override our guesses. */
132   switch (cinfo->num_components) {
133   case 1:
134     cinfo->jpeg_color_space = JCS_GRAYSCALE;
135     cinfo->out_color_space = JCS_GRAYSCALE;
136     break;
137
138   case 3:
139     if (cinfo->saw_JFIF_marker) {
140       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
141     } else if (cinfo->saw_Adobe_marker) {
142       switch (cinfo->Adobe_transform) {
143       case 0:
144         cinfo->jpeg_color_space = JCS_RGB;
145         break;
146       case 1:
147         cinfo->jpeg_color_space = JCS_YCbCr;
148         break;
149       default:
150         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
151         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
152         break;
153       }
154     } else {
155       /* Saw no special markers, try to guess from the component IDs */
156       int cid0 = cinfo->comp_info[0].component_id;
157       int cid1 = cinfo->comp_info[1].component_id;
158       int cid2 = cinfo->comp_info[2].component_id;
159
160       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
161         cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
162       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
163         cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
164       else {
165         TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
166         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
167       }
168     }
169     /* Always guess RGB is proper output colorspace. */
170     cinfo->out_color_space = JCS_RGB;
171     break;
172
173   case 4:
174     if (cinfo->saw_Adobe_marker) {
175       switch (cinfo->Adobe_transform) {
176       case 0:
177         cinfo->jpeg_color_space = JCS_CMYK;
178         break;
179       case 2:
180         cinfo->jpeg_color_space = JCS_YCCK;
181         break;
182       default:
183         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
184         cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
185         break;
186       }
187     } else {
188       /* No special markers, assume straight CMYK. */
189       cinfo->jpeg_color_space = JCS_CMYK;
190     }
191     cinfo->out_color_space = JCS_CMYK;
192     break;
193
194   default:
195     cinfo->jpeg_color_space = JCS_UNKNOWN;
196     cinfo->out_color_space = JCS_UNKNOWN;
197     break;
198   }
199
200   /* Set defaults for other decompression parameters. */
201   cinfo->scale_num = 1;         /* 1:1 scaling */
202   cinfo->scale_denom = 1;
203   cinfo->output_gamma = 1.0;
204   cinfo->buffered_image = FALSE;
205   cinfo->raw_data_out = FALSE;
206   cinfo->dct_method = JDCT_DEFAULT;
207   cinfo->do_fancy_upsampling = TRUE;
208   cinfo->do_block_smoothing = TRUE;
209   cinfo->quantize_colors = FALSE;
210   /* We set these in case application only sets quantize_colors. */
211   cinfo->dither_mode = JDITHER_FS;
212 #ifdef QUANT_2PASS_SUPPORTED
213   cinfo->two_pass_quantize = TRUE;
214 #else
215   cinfo->two_pass_quantize = FALSE;
216 #endif
217   cinfo->desired_number_of_colors = 256;
218   cinfo->colormap = NULL;
219   /* Initialize for no mode change in buffered-image mode. */
220   cinfo->enable_1pass_quant = FALSE;
221   cinfo->enable_external_quant = FALSE;
222   cinfo->enable_2pass_quant = FALSE;
223 }
224
225
226 /*
227  * Decompression startup: read start of JPEG datastream to see what's there.
228  * Need only initialize JPEG object and supply a data source before calling.
229  *
230  * This routine will read as far as the first SOS marker (ie, actual start of
231  * compressed data), and will save all tables and parameters in the JPEG
232  * object.  It will also initialize the decompression parameters to default
233  * values, and finally return JPEG_HEADER_OK.  On return, the application may
234  * adjust the decompression parameters and then call jpeg_start_decompress.
235  * (Or, if the application only wanted to determine the image parameters,
236  * the data need not be decompressed.  In that case, call jpeg_abort or
237  * jpeg_destroy to release any temporary space.)
238  * If an abbreviated (tables only) datastream is presented, the routine will
239  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
240  * re-use the JPEG object to read the abbreviated image datastream(s).
241  * It is unnecessary (but OK) to call jpeg_abort in this case.
242  * The JPEG_SUSPENDED return code only occurs if the data source module
243  * requests suspension of the decompressor.  In this case the application
244  * should load more source data and then re-call jpeg_read_header to resume
245  * processing.
246  * If a non-suspending data source is used and require_image is TRUE, then the
247  * return code need not be inspected since only JPEG_HEADER_OK is possible.
248  *
249  * This routine is now just a front end to jpeg_consume_input, with some
250  * extra error checking.
251  */
252
253 GLOBAL(int)
254 jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
255 {
256   int retcode;
257
258   if (cinfo->global_state != DSTATE_START &&
259       cinfo->global_state != DSTATE_INHEADER)
260     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
261
262   retcode = jpeg_consume_input(cinfo);
263
264   switch (retcode) {
265   case JPEG_REACHED_SOS:
266     retcode = JPEG_HEADER_OK;
267     break;
268   case JPEG_REACHED_EOI:
269     if (require_image)          /* Complain if application wanted an image */
270       ERREXIT(cinfo, JERR_NO_IMAGE);
271     /* Reset to start state; it would be safer to require the application to
272      * call jpeg_abort, but we can't change it now for compatibility reasons.
273      * A side effect is to free any temporary memory (there shouldn't be any).
274      */
275     jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
276     retcode = JPEG_HEADER_TABLES_ONLY;
277     break;
278   case JPEG_SUSPENDED:
279     /* no work */
280     break;
281   }
282
283   return retcode;
284 }
285
286
287 /*
288  * Consume data in advance of what the decompressor requires.
289  * This can be called at any time once the decompressor object has
290  * been created and a data source has been set up.
291  *
292  * This routine is essentially a state machine that handles a couple
293  * of critical state-transition actions, namely initial setup and
294  * transition from header scanning to ready-for-start_decompress.
295  * All the actual input is done via the input controller's consume_input
296  * method.
297  */
298
299 GLOBAL(int)
300 jpeg_consume_input(j_decompress_ptr cinfo)
301 {
302   int retcode = JPEG_SUSPENDED;
303
304   /* NB: every possible DSTATE value should be listed in this switch */
305   switch (cinfo->global_state) {
306   case DSTATE_START:
307     /* Start-of-datastream actions: reset appropriate modules */
308     (*cinfo->inputctl->reset_input_controller) (cinfo);
309     /* Initialize application's data source module */
310     (*cinfo->src->init_source) (cinfo);
311     cinfo->global_state = DSTATE_INHEADER;
312     FALLTHROUGH                 /*FALLTHROUGH*/
313   case DSTATE_INHEADER:
314     retcode = (*cinfo->inputctl->consume_input) (cinfo);
315     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
316       /* Set up default parameters based on header data */
317       default_decompress_parms(cinfo);
318       /* Set global state: ready for start_decompress */
319       cinfo->global_state = DSTATE_READY;
320     }
321     break;
322   case DSTATE_READY:
323     /* Can't advance past first SOS until start_decompress is called */
324     retcode = JPEG_REACHED_SOS;
325     break;
326   case DSTATE_PRELOAD:
327   case DSTATE_PRESCAN:
328   case DSTATE_SCANNING:
329   case DSTATE_RAW_OK:
330   case DSTATE_BUFIMAGE:
331   case DSTATE_BUFPOST:
332   case DSTATE_STOPPING:
333     retcode = (*cinfo->inputctl->consume_input) (cinfo);
334     break;
335   default:
336     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
337   }
338   return retcode;
339 }
340
341
342 /*
343  * Have we finished reading the input file?
344  */
345
346 GLOBAL(boolean)
347 jpeg_input_complete(j_decompress_ptr cinfo)
348 {
349   /* Check for valid jpeg object */
350   if (cinfo->global_state < DSTATE_START ||
351       cinfo->global_state > DSTATE_STOPPING)
352     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
353   return cinfo->inputctl->eoi_reached;
354 }
355
356
357 /*
358  * Is there more than one scan?
359  */
360
361 GLOBAL(boolean)
362 jpeg_has_multiple_scans(j_decompress_ptr cinfo)
363 {
364   /* Only valid after jpeg_read_header completes */
365   if (cinfo->global_state < DSTATE_READY ||
366       cinfo->global_state > DSTATE_STOPPING)
367     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
368   return cinfo->inputctl->has_multiple_scans;
369 }
370
371
372 /*
373  * Finish JPEG decompression.
374  *
375  * This will normally just verify the file trailer and release temp storage.
376  *
377  * Returns FALSE if suspended.  The return value need be inspected only if
378  * a suspending data source is used.
379  */
380
381 GLOBAL(boolean)
382 jpeg_finish_decompress(j_decompress_ptr cinfo)
383 {
384   if ((cinfo->global_state == DSTATE_SCANNING ||
385        cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {
386     /* Terminate final pass of non-buffered mode */
387     if (cinfo->output_scanline < cinfo->output_height)
388       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
389     (*cinfo->master->finish_output_pass) (cinfo);
390     cinfo->global_state = DSTATE_STOPPING;
391   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
392     /* Finishing after a buffered-image operation */
393     cinfo->global_state = DSTATE_STOPPING;
394   } else if (cinfo->global_state != DSTATE_STOPPING) {
395     /* STOPPING = repeat call after a suspension, anything else is error */
396     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
397   }
398   /* Read until EOI */
399   while (!cinfo->inputctl->eoi_reached) {
400     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
401       return FALSE;             /* Suspend, come back later */
402   }
403   /* Do final cleanup */
404   (*cinfo->src->term_source) (cinfo);
405   /* We can use jpeg_abort to release memory and reset global_state */
406   jpeg_abort((j_common_ptr)cinfo);
407   return TRUE;
408 }