Remove 'oldcode'
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jdinput.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: jdinput.c /main/2 1996/05/09 03:48:17 drk $ */
24 /*
25  * jdinput.c
26  *
27  * Copyright (C) 1991-1996, Thomas G. Lane.
28  * This file is part of the Independent JPEG Group's software.
29  * For conditions of distribution and use, see the accompanying README file.
30  *
31  * This file contains input control logic for the JPEG decompressor.
32  * These routines are concerned with controlling the decompressor's input
33  * processing (marker reading and coefficient decoding).  The actual input
34  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
35  */
36
37 #define JPEG_INTERNALS
38 #include "jinclude.h"
39 #include "jpeglib.h"
40
41
42 /* Private state */
43
44 typedef struct {
45   struct jpeg_input_controller pub; /* public fields */
46
47   boolean inheaders;            /* TRUE until first SOS is reached */
48 } my_input_controller;
49
50 typedef my_input_controller * my_inputctl_ptr;
51
52
53 /* Forward declarations */
54 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
55
56
57 /*
58  * Routines to calculate various quantities related to the size of the image.
59  */
60
61 LOCAL(void)
62 initial_setup (j_decompress_ptr cinfo)
63 /* Called once, when first SOS marker is reached */
64 {
65   int ci;
66   jpeg_component_info *compptr;
67
68   /* Make sure image isn't bigger than I can handle */
69   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
70       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
71     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
72
73   /* For now, precision must match compiled-in value... */
74   if (cinfo->data_precision != BITS_IN_JSAMPLE)
75     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
76
77   /* Check that number of components won't exceed internal array sizes */
78   if (cinfo->num_components > MAX_COMPONENTS)
79     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
80              MAX_COMPONENTS);
81
82   /* Compute maximum sampling factors; check factor validity */
83   cinfo->max_h_samp_factor = 1;
84   cinfo->max_v_samp_factor = 1;
85   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
86        ci++, compptr++) {
87     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
88         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
89       ERREXIT(cinfo, JERR_BAD_SAMPLING);
90     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
91                                    compptr->h_samp_factor);
92     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
93                                    compptr->v_samp_factor);
94   }
95
96   /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
97    * In the full decompressor, this will be overridden by jdmaster.c;
98    * but in the transcoder, jdmaster.c is not used, so we must do it here.
99    */
100   cinfo->min_DCT_scaled_size = DCTSIZE;
101
102   /* Compute dimensions of components */
103   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104        ci++, compptr++) {
105     compptr->DCT_scaled_size = DCTSIZE;
106     /* Size in DCT blocks */
107     compptr->width_in_blocks = (JDIMENSION)
108       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
109                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
110     compptr->height_in_blocks = (JDIMENSION)
111       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
112                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
113     /* downsampled_width and downsampled_height will also be overridden by
114      * jdmaster.c if we are doing full decompression.  The transcoder library
115      * doesn't use these values, but the calling application might.
116      */
117     /* Size in samples */
118     compptr->downsampled_width = (JDIMENSION)
119       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
120                     (long) cinfo->max_h_samp_factor);
121     compptr->downsampled_height = (JDIMENSION)
122       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
123                     (long) cinfo->max_v_samp_factor);
124     /* Mark component needed, until color conversion says otherwise */
125     compptr->component_needed = TRUE;
126     /* Mark no quantization table yet saved for component */
127     compptr->quant_table = NULL;
128   }
129
130   /* Compute number of fully interleaved MCU rows. */
131   cinfo->total_iMCU_rows = (JDIMENSION)
132     jdiv_round_up((long) cinfo->image_height,
133                   (long) (cinfo->max_v_samp_factor*DCTSIZE));
134
135   /* Decide whether file contains multiple scans */
136   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
137     cinfo->inputctl->has_multiple_scans = TRUE;
138   else
139     cinfo->inputctl->has_multiple_scans = FALSE;
140 }
141
142
143 LOCAL(void)
144 per_scan_setup (j_decompress_ptr cinfo)
145 /* Do computations that are needed before processing a JPEG scan */
146 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
147 {
148   int ci, mcublks, tmp;
149   jpeg_component_info *compptr;
150   
151   if (cinfo->comps_in_scan == 1) {
152     
153     /* Noninterleaved (single-component) scan */
154     compptr = cinfo->cur_comp_info[0];
155     
156     /* Overall image size in MCUs */
157     cinfo->MCUs_per_row = compptr->width_in_blocks;
158     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
159     
160     /* For noninterleaved scan, always one block per MCU */
161     compptr->MCU_width = 1;
162     compptr->MCU_height = 1;
163     compptr->MCU_blocks = 1;
164     compptr->MCU_sample_width = compptr->DCT_scaled_size;
165     compptr->last_col_width = 1;
166     /* For noninterleaved scans, it is convenient to define last_row_height
167      * as the number of block rows present in the last iMCU row.
168      */
169     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
170     if (tmp == 0) tmp = compptr->v_samp_factor;
171     compptr->last_row_height = tmp;
172     
173     /* Prepare array describing MCU composition */
174     cinfo->blocks_in_MCU = 1;
175     cinfo->MCU_membership[0] = 0;
176     
177   } else {
178     
179     /* Interleaved (multi-component) scan */
180     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
181       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
182                MAX_COMPS_IN_SCAN);
183     
184     /* Overall image size in MCUs */
185     cinfo->MCUs_per_row = (JDIMENSION)
186       jdiv_round_up((long) cinfo->image_width,
187                     (long) (cinfo->max_h_samp_factor*DCTSIZE));
188     cinfo->MCU_rows_in_scan = (JDIMENSION)
189       jdiv_round_up((long) cinfo->image_height,
190                     (long) (cinfo->max_v_samp_factor*DCTSIZE));
191     
192     cinfo->blocks_in_MCU = 0;
193     
194     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
195       compptr = cinfo->cur_comp_info[ci];
196       /* Sampling factors give # of blocks of component in each MCU */
197       compptr->MCU_width = compptr->h_samp_factor;
198       compptr->MCU_height = compptr->v_samp_factor;
199       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
200       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
201       /* Figure number of non-dummy blocks in last MCU column & row */
202       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
203       if (tmp == 0) tmp = compptr->MCU_width;
204       compptr->last_col_width = tmp;
205       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
206       if (tmp == 0) tmp = compptr->MCU_height;
207       compptr->last_row_height = tmp;
208       /* Prepare array describing MCU composition */
209       mcublks = compptr->MCU_blocks;
210       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
211         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
212       while (mcublks-- > 0) {
213         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
214       }
215     }
216     
217   }
218 }
219
220
221 /*
222  * Save away a copy of the Q-table referenced by each component present
223  * in the current scan, unless already saved during a prior scan.
224  *
225  * In a multiple-scan JPEG file, the encoder could assign different components
226  * the same Q-table slot number, but change table definitions between scans
227  * so that each component uses a different Q-table.  (The IJG encoder is not
228  * currently capable of doing this, but other encoders might.)  Since we want
229  * to be able to dequantize all the components at the end of the file, this
230  * means that we have to save away the table actually used for each component.
231  * We do this by copying the table at the start of the first scan containing
232  * the component.
233  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
234  * slot between scans of a component using that slot.  If the encoder does so
235  * anyway, this decoder will simply use the Q-table values that were current
236  * at the start of the first scan for the component.
237  *
238  * The decompressor output side looks only at the saved quant tables,
239  * not at the current Q-table slots.
240  */
241
242 LOCAL(void)
243 latch_quant_tables (j_decompress_ptr cinfo)
244 {
245   int ci, qtblno;
246   jpeg_component_info *compptr;
247   JQUANT_TBL * qtbl;
248
249   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
250     compptr = cinfo->cur_comp_info[ci];
251     /* No work if we already saved Q-table for this component */
252     if (compptr->quant_table != NULL)
253       continue;
254     /* Make sure specified quantization table is present */
255     qtblno = compptr->quant_tbl_no;
256     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
257         cinfo->quant_tbl_ptrs[qtblno] == NULL)
258       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
259     /* OK, save away the quantization table */
260     qtbl = (JQUANT_TBL *)
261       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
262                                   SIZEOF(JQUANT_TBL));
263     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
264     compptr->quant_table = qtbl;
265   }
266 }
267
268
269 /*
270  * Initialize the input modules to read a scan of compressed data.
271  * The first call to this is done by jdmaster.c after initializing
272  * the entire decompressor (during jpeg_start_decompress).
273  * Subsequent calls come from consume_markers, below.
274  */
275
276 METHODDEF(void)
277 start_input_pass (j_decompress_ptr cinfo)
278 {
279   per_scan_setup(cinfo);
280   latch_quant_tables(cinfo);
281   (*cinfo->entropy->start_pass) (cinfo);
282   (*cinfo->coef->start_input_pass) (cinfo);
283   cinfo->inputctl->consume_input = cinfo->coef->consume_data;
284 }
285
286
287 /*
288  * Finish up after inputting a compressed-data scan.
289  * This is called by the coefficient controller after it's read all
290  * the expected data of the scan.
291  */
292
293 METHODDEF(void)
294 finish_input_pass (j_decompress_ptr cinfo)
295 {
296   cinfo->inputctl->consume_input = consume_markers;
297 }
298
299
300 /*
301  * Read JPEG markers before, between, or after compressed-data scans.
302  * Change state as necessary when a new scan is reached.
303  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
304  *
305  * The consume_input method pointer points either here or to the
306  * coefficient controller's consume_data routine, depending on whether
307  * we are reading a compressed data segment or inter-segment markers.
308  */
309
310 METHODDEF(int)
311 consume_markers (j_decompress_ptr cinfo)
312 {
313   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
314   int val;
315
316   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
317     return JPEG_REACHED_EOI;
318
319   val = (*cinfo->marker->read_markers) (cinfo);
320
321   switch (val) {
322   case JPEG_REACHED_SOS:        /* Found SOS */
323     if (inputctl->inheaders) {  /* 1st SOS */
324       initial_setup(cinfo);
325       inputctl->inheaders = FALSE;
326       /* Note: start_input_pass must be called by jdmaster.c
327        * before any more input can be consumed.  jdapi.c is
328        * responsible for enforcing this sequencing.
329        */
330     } else {                    /* 2nd or later SOS marker */
331       if (! inputctl->pub.has_multiple_scans)
332         ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
333       start_input_pass(cinfo);
334     }
335     break;
336   case JPEG_REACHED_EOI:        /* Found EOI */
337     inputctl->pub.eoi_reached = TRUE;
338     if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
339       if (cinfo->marker->saw_SOF)
340         ERREXIT(cinfo, JERR_SOF_NO_SOS);
341     } else {
342       /* Prevent infinite loop in coef ctlr's decompress_data routine
343        * if user set output_scan_number larger than number of scans.
344        */
345       if (cinfo->output_scan_number > cinfo->input_scan_number)
346         cinfo->output_scan_number = cinfo->input_scan_number;
347     }
348     break;
349   case JPEG_SUSPENDED:
350     break;
351   }
352
353   return val;
354 }
355
356
357 /*
358  * Reset state to begin a fresh datastream.
359  */
360
361 METHODDEF(void)
362 reset_input_controller (j_decompress_ptr cinfo)
363 {
364   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
365
366   inputctl->pub.consume_input = consume_markers;
367   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
368   inputctl->pub.eoi_reached = FALSE;
369   inputctl->inheaders = TRUE;
370   /* Reset other modules */
371   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
372   (*cinfo->marker->reset_marker_reader) (cinfo);
373   /* Reset progression state -- would be cleaner if entropy decoder did this */
374   cinfo->coef_bits = NULL;
375 }
376
377
378 /*
379  * Initialize the input controller module.
380  * This is called only once, when the decompression object is created.
381  */
382
383 GLOBAL(void)
384 jinit_input_controller (j_decompress_ptr cinfo)
385 {
386   my_inputctl_ptr inputctl;
387
388   /* Create subobject in permanent pool */
389   inputctl = (my_inputctl_ptr)
390     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
391                                 SIZEOF(my_input_controller));
392   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
393   /* Initialize method pointers */
394   inputctl->pub.consume_input = consume_markers;
395   inputctl->pub.reset_input_controller = reset_input_controller;
396   inputctl->pub.start_input_pass = start_input_pass;
397   inputctl->pub.finish_input_pass = finish_input_pass;
398   /* Initialize state: can't use reset_input_controller since we don't
399    * want to try to reset other modules yet.
400    */
401   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
402   inputctl->pub.eoi_reached = FALSE;
403   inputctl->inheaders = TRUE;
404 }