/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jdinput.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * jdinput.c  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 1991-1997, Thomas G. Lane.  | 
5  |  |  * Modified 2002-2013 by Guido Vollbeding.  | 
6  |  |  * This file is part of the Independent JPEG Group's software.  | 
7  |  |  * For conditions of distribution and use, see the accompanying README file.  | 
8  |  |  *  | 
9  |  |  * This file contains input control logic for the JPEG decompressor.  | 
10  |  |  * These routines are concerned with controlling the decompressor's input  | 
11  |  |  * processing (marker reading and coefficient decoding).  The actual input  | 
12  |  |  * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.  | 
13  |  |  */  | 
14  |  |  | 
15  |  | #define JPEG_INTERNALS  | 
16  |  | #include "jinclude.h"  | 
17  |  | #include "jpeglib.h"  | 
18  |  |  | 
19  |  |  | 
20  |  | /* Private state */  | 
21  |  |  | 
22  |  | typedef struct { | 
23  |  |   struct jpeg_input_controller pub; /* public fields */  | 
24  |  |  | 
25  |  |   int inheaders;    /* Nonzero until first SOS is reached */  | 
26  |  | } my_input_controller;  | 
27  |  |  | 
28  |  | typedef my_input_controller * my_inputctl_ptr;  | 
29  |  |  | 
30  |  |  | 
31  |  | /* Forward declarations */  | 
32  |  | METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));  | 
33  |  |  | 
34  |  |  | 
35  |  | /*  | 
36  |  |  * Routines to calculate various quantities related to the size of the image.  | 
37  |  |  */  | 
38  |  |  | 
39  |  |  | 
40  |  | /*  | 
41  |  |  * Compute output image dimensions and related values.  | 
42  |  |  * NOTE: this is exported for possible use by application.  | 
43  |  |  * Hence it mustn't do anything that can't be done twice.  | 
44  |  |  */  | 
45  |  |  | 
46  |  | GLOBAL(void)  | 
47  |  | jpeg_core_output_dimensions (j_decompress_ptr cinfo)  | 
48  |  | /* Do computations that are needed before master selection phase.  | 
49  |  |  * This function is used for transcoding and full decompression.  | 
50  |  |  */  | 
51  | 0  | { | 
52  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
53  | 0  |   int ci;  | 
54  | 0  |   jpeg_component_info *compptr;  | 
55  |  |  | 
56  |  |   /* Compute actual output image dimensions and DCT scaling choices. */  | 
57  | 0  |   if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) { | 
58  |  |     /* Provide 1/block_size scaling */  | 
59  | 0  |     cinfo->output_width = (JDIMENSION)  | 
60  | 0  |       jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);  | 
61  | 0  |     cinfo->output_height = (JDIMENSION)  | 
62  | 0  |       jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);  | 
63  | 0  |     cinfo->min_DCT_h_scaled_size = 1;  | 
64  | 0  |     cinfo->min_DCT_v_scaled_size = 1;  | 
65  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) { | 
66  |  |     /* Provide 2/block_size scaling */  | 
67  | 0  |     cinfo->output_width = (JDIMENSION)  | 
68  | 0  |       jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);  | 
69  | 0  |     cinfo->output_height = (JDIMENSION)  | 
70  | 0  |       jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);  | 
71  | 0  |     cinfo->min_DCT_h_scaled_size = 2;  | 
72  | 0  |     cinfo->min_DCT_v_scaled_size = 2;  | 
73  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) { | 
74  |  |     /* Provide 3/block_size scaling */  | 
75  | 0  |     cinfo->output_width = (JDIMENSION)  | 
76  | 0  |       jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);  | 
77  | 0  |     cinfo->output_height = (JDIMENSION)  | 
78  | 0  |       jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);  | 
79  | 0  |     cinfo->min_DCT_h_scaled_size = 3;  | 
80  | 0  |     cinfo->min_DCT_v_scaled_size = 3;  | 
81  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) { | 
82  |  |     /* Provide 4/block_size scaling */  | 
83  | 0  |     cinfo->output_width = (JDIMENSION)  | 
84  | 0  |       jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);  | 
85  | 0  |     cinfo->output_height = (JDIMENSION)  | 
86  | 0  |       jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);  | 
87  | 0  |     cinfo->min_DCT_h_scaled_size = 4;  | 
88  | 0  |     cinfo->min_DCT_v_scaled_size = 4;  | 
89  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) { | 
90  |  |     /* Provide 5/block_size scaling */  | 
91  | 0  |     cinfo->output_width = (JDIMENSION)  | 
92  | 0  |       jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);  | 
93  | 0  |     cinfo->output_height = (JDIMENSION)  | 
94  | 0  |       jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);  | 
95  | 0  |     cinfo->min_DCT_h_scaled_size = 5;  | 
96  | 0  |     cinfo->min_DCT_v_scaled_size = 5;  | 
97  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) { | 
98  |  |     /* Provide 6/block_size scaling */  | 
99  | 0  |     cinfo->output_width = (JDIMENSION)  | 
100  | 0  |       jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);  | 
101  | 0  |     cinfo->output_height = (JDIMENSION)  | 
102  | 0  |       jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);  | 
103  | 0  |     cinfo->min_DCT_h_scaled_size = 6;  | 
104  | 0  |     cinfo->min_DCT_v_scaled_size = 6;  | 
105  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) { | 
106  |  |     /* Provide 7/block_size scaling */  | 
107  | 0  |     cinfo->output_width = (JDIMENSION)  | 
108  | 0  |       jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);  | 
109  | 0  |     cinfo->output_height = (JDIMENSION)  | 
110  | 0  |       jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);  | 
111  | 0  |     cinfo->min_DCT_h_scaled_size = 7;  | 
112  | 0  |     cinfo->min_DCT_v_scaled_size = 7;  | 
113  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) { | 
114  |  |     /* Provide 8/block_size scaling */  | 
115  | 0  |     cinfo->output_width = (JDIMENSION)  | 
116  | 0  |       jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);  | 
117  | 0  |     cinfo->output_height = (JDIMENSION)  | 
118  | 0  |       jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);  | 
119  | 0  |     cinfo->min_DCT_h_scaled_size = 8;  | 
120  | 0  |     cinfo->min_DCT_v_scaled_size = 8;  | 
121  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) { | 
122  |  |     /* Provide 9/block_size scaling */  | 
123  | 0  |     cinfo->output_width = (JDIMENSION)  | 
124  | 0  |       jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);  | 
125  | 0  |     cinfo->output_height = (JDIMENSION)  | 
126  | 0  |       jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);  | 
127  | 0  |     cinfo->min_DCT_h_scaled_size = 9;  | 
128  | 0  |     cinfo->min_DCT_v_scaled_size = 9;  | 
129  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) { | 
130  |  |     /* Provide 10/block_size scaling */  | 
131  | 0  |     cinfo->output_width = (JDIMENSION)  | 
132  | 0  |       jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);  | 
133  | 0  |     cinfo->output_height = (JDIMENSION)  | 
134  | 0  |       jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);  | 
135  | 0  |     cinfo->min_DCT_h_scaled_size = 10;  | 
136  | 0  |     cinfo->min_DCT_v_scaled_size = 10;  | 
137  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) { | 
138  |  |     /* Provide 11/block_size scaling */  | 
139  | 0  |     cinfo->output_width = (JDIMENSION)  | 
140  | 0  |       jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);  | 
141  | 0  |     cinfo->output_height = (JDIMENSION)  | 
142  | 0  |       jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);  | 
143  | 0  |     cinfo->min_DCT_h_scaled_size = 11;  | 
144  | 0  |     cinfo->min_DCT_v_scaled_size = 11;  | 
145  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) { | 
146  |  |     /* Provide 12/block_size scaling */  | 
147  | 0  |     cinfo->output_width = (JDIMENSION)  | 
148  | 0  |       jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);  | 
149  | 0  |     cinfo->output_height = (JDIMENSION)  | 
150  | 0  |       jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);  | 
151  | 0  |     cinfo->min_DCT_h_scaled_size = 12;  | 
152  | 0  |     cinfo->min_DCT_v_scaled_size = 12;  | 
153  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) { | 
154  |  |     /* Provide 13/block_size scaling */  | 
155  | 0  |     cinfo->output_width = (JDIMENSION)  | 
156  | 0  |       jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);  | 
157  | 0  |     cinfo->output_height = (JDIMENSION)  | 
158  | 0  |       jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);  | 
159  | 0  |     cinfo->min_DCT_h_scaled_size = 13;  | 
160  | 0  |     cinfo->min_DCT_v_scaled_size = 13;  | 
161  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) { | 
162  |  |     /* Provide 14/block_size scaling */  | 
163  | 0  |     cinfo->output_width = (JDIMENSION)  | 
164  | 0  |       jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);  | 
165  | 0  |     cinfo->output_height = (JDIMENSION)  | 
166  | 0  |       jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);  | 
167  | 0  |     cinfo->min_DCT_h_scaled_size = 14;  | 
168  | 0  |     cinfo->min_DCT_v_scaled_size = 14;  | 
169  | 0  |   } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) { | 
170  |  |     /* Provide 15/block_size scaling */  | 
171  | 0  |     cinfo->output_width = (JDIMENSION)  | 
172  | 0  |       jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);  | 
173  | 0  |     cinfo->output_height = (JDIMENSION)  | 
174  | 0  |       jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);  | 
175  | 0  |     cinfo->min_DCT_h_scaled_size = 15;  | 
176  | 0  |     cinfo->min_DCT_v_scaled_size = 15;  | 
177  | 0  |   } else { | 
178  |  |     /* Provide 16/block_size scaling */  | 
179  | 0  |     cinfo->output_width = (JDIMENSION)  | 
180  | 0  |       jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);  | 
181  | 0  |     cinfo->output_height = (JDIMENSION)  | 
182  | 0  |       jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);  | 
183  | 0  |     cinfo->min_DCT_h_scaled_size = 16;  | 
184  | 0  |     cinfo->min_DCT_v_scaled_size = 16;  | 
185  | 0  |   }  | 
186  |  |  | 
187  |  |   /* Recompute dimensions of components */  | 
188  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
189  | 0  |        ci++, compptr++) { | 
190  | 0  |     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;  | 
191  | 0  |     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;  | 
192  | 0  |   }  | 
193  |  | 
  | 
194  |  | #else /* !IDCT_SCALING_SUPPORTED */  | 
195  |  |  | 
196  |  |   /* Hardwire it to "no scaling" */  | 
197  |  |   cinfo->output_width = cinfo->image_width;  | 
198  |  |   cinfo->output_height = cinfo->image_height;  | 
199  |  |   /* initial_setup has already initialized DCT_scaled_size,  | 
200  |  |    * and has computed unscaled downsampled_width and downsampled_height.  | 
201  |  |    */  | 
202  |  |  | 
203  |  | #endif /* IDCT_SCALING_SUPPORTED */  | 
204  | 0  | }  | 
205  |  |  | 
206  |  |  | 
207  |  | LOCAL(void)  | 
208  |  | initial_setup (j_decompress_ptr cinfo)  | 
209  |  | /* Called once, when first SOS marker is reached */  | 
210  | 0  | { | 
211  | 0  |   int ci;  | 
212  | 0  |   jpeg_component_info *compptr;  | 
213  |  |  | 
214  |  |   /* Make sure image isn't bigger than I can handle */  | 
215  | 0  |   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||  | 
216  | 0  |       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)  | 
217  | 0  |     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);  | 
218  |  |  | 
219  |  |   /* Only 8 to 12 bits data precision are supported for DCT based JPEG */  | 
220  | 0  |   if (cinfo->data_precision < 8 || cinfo->data_precision > 12)  | 
221  | 0  |     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);  | 
222  |  |  | 
223  |  |   /* Check that number of components won't exceed internal array sizes */  | 
224  | 0  |   if (cinfo->num_components > MAX_COMPONENTS)  | 
225  | 0  |     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,  | 
226  | 0  |        MAX_COMPONENTS);  | 
227  |  |  | 
228  |  |   /* Compute maximum sampling factors; check factor validity */  | 
229  | 0  |   cinfo->max_h_samp_factor = 1;  | 
230  | 0  |   cinfo->max_v_samp_factor = 1;  | 
231  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
232  | 0  |        ci++, compptr++) { | 
233  | 0  |     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||  | 
234  | 0  |   compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)  | 
235  | 0  |       ERREXIT(cinfo, JERR_BAD_SAMPLING);  | 
236  | 0  |     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,  | 
237  | 0  |            compptr->h_samp_factor);  | 
238  | 0  |     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,  | 
239  | 0  |            compptr->v_samp_factor);  | 
240  | 0  |   }  | 
241  |  |  | 
242  |  |   /* Derive block_size, natural_order, and lim_Se */  | 
243  | 0  |   if (cinfo->is_baseline || (cinfo->progressive_mode &&  | 
244  | 0  |       cinfo->comps_in_scan)) { /* no pseudo SOS marker */ | 
245  | 0  |     cinfo->block_size = DCTSIZE;  | 
246  | 0  |     cinfo->natural_order = jpeg_natural_order;  | 
247  | 0  |     cinfo->lim_Se = DCTSIZE2-1;  | 
248  | 0  |   } else  | 
249  | 0  |     switch (cinfo->Se) { | 
250  | 0  |     case (1*1-1):  | 
251  | 0  |       cinfo->block_size = 1;  | 
252  | 0  |       cinfo->natural_order = jpeg_natural_order; /* not needed */  | 
253  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
254  | 0  |       break;  | 
255  | 0  |     case (2*2-1):  | 
256  | 0  |       cinfo->block_size = 2;  | 
257  | 0  |       cinfo->natural_order = jpeg_natural_order2;  | 
258  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
259  | 0  |       break;  | 
260  | 0  |     case (3*3-1):  | 
261  | 0  |       cinfo->block_size = 3;  | 
262  | 0  |       cinfo->natural_order = jpeg_natural_order3;  | 
263  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
264  | 0  |       break;  | 
265  | 0  |     case (4*4-1):  | 
266  | 0  |       cinfo->block_size = 4;  | 
267  | 0  |       cinfo->natural_order = jpeg_natural_order4;  | 
268  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
269  | 0  |       break;  | 
270  | 0  |     case (5*5-1):  | 
271  | 0  |       cinfo->block_size = 5;  | 
272  | 0  |       cinfo->natural_order = jpeg_natural_order5;  | 
273  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
274  | 0  |       break;  | 
275  | 0  |     case (6*6-1):  | 
276  | 0  |       cinfo->block_size = 6;  | 
277  | 0  |       cinfo->natural_order = jpeg_natural_order6;  | 
278  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
279  | 0  |       break;  | 
280  | 0  |     case (7*7-1):  | 
281  | 0  |       cinfo->block_size = 7;  | 
282  | 0  |       cinfo->natural_order = jpeg_natural_order7;  | 
283  | 0  |       cinfo->lim_Se = cinfo->Se;  | 
284  | 0  |       break;  | 
285  | 0  |     case (8*8-1):  | 
286  | 0  |       cinfo->block_size = 8;  | 
287  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
288  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
289  | 0  |       break;  | 
290  | 0  |     case (9*9-1):  | 
291  | 0  |       cinfo->block_size = 9;  | 
292  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
293  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
294  | 0  |       break;  | 
295  | 0  |     case (10*10-1):  | 
296  | 0  |       cinfo->block_size = 10;  | 
297  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
298  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
299  | 0  |       break;  | 
300  | 0  |     case (11*11-1):  | 
301  | 0  |       cinfo->block_size = 11;  | 
302  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
303  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
304  | 0  |       break;  | 
305  | 0  |     case (12*12-1):  | 
306  | 0  |       cinfo->block_size = 12;  | 
307  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
308  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
309  | 0  |       break;  | 
310  | 0  |     case (13*13-1):  | 
311  | 0  |       cinfo->block_size = 13;  | 
312  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
313  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
314  | 0  |       break;  | 
315  | 0  |     case (14*14-1):  | 
316  | 0  |       cinfo->block_size = 14;  | 
317  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
318  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
319  | 0  |       break;  | 
320  | 0  |     case (15*15-1):  | 
321  | 0  |       cinfo->block_size = 15;  | 
322  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
323  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
324  | 0  |       break;  | 
325  | 0  |     case (16*16-1):  | 
326  | 0  |       cinfo->block_size = 16;  | 
327  | 0  |       cinfo->natural_order = jpeg_natural_order;  | 
328  | 0  |       cinfo->lim_Se = DCTSIZE2-1;  | 
329  | 0  |       break;  | 
330  | 0  |     default:  | 
331  | 0  |       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,  | 
332  | 0  |          cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);  | 
333  | 0  |       break;  | 
334  | 0  |     }  | 
335  |  |  | 
336  |  |   /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.  | 
337  |  |    * In the full decompressor,  | 
338  |  |    * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;  | 
339  |  |    * but in the transcoder,  | 
340  |  |    * jpeg_calc_output_dimensions is not used, so we must do it here.  | 
341  |  |    */  | 
342  | 0  |   cinfo->min_DCT_h_scaled_size = cinfo->block_size;  | 
343  | 0  |   cinfo->min_DCT_v_scaled_size = cinfo->block_size;  | 
344  |  |  | 
345  |  |   /* Compute dimensions of components */  | 
346  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
347  | 0  |        ci++, compptr++) { | 
348  | 0  |     compptr->DCT_h_scaled_size = cinfo->block_size;  | 
349  | 0  |     compptr->DCT_v_scaled_size = cinfo->block_size;  | 
350  |  |     /* Size in DCT blocks */  | 
351  | 0  |     compptr->width_in_blocks = (JDIMENSION)  | 
352  | 0  |       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,  | 
353  | 0  |         (long) (cinfo->max_h_samp_factor * cinfo->block_size));  | 
354  | 0  |     compptr->height_in_blocks = (JDIMENSION)  | 
355  | 0  |       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,  | 
356  | 0  |         (long) (cinfo->max_v_samp_factor * cinfo->block_size));  | 
357  |  |     /* downsampled_width and downsampled_height will also be overridden by  | 
358  |  |      * jdmaster.c if we are doing full decompression.  The transcoder library  | 
359  |  |      * doesn't use these values, but the calling application might.  | 
360  |  |      */  | 
361  |  |     /* Size in samples */  | 
362  | 0  |     compptr->downsampled_width = (JDIMENSION)  | 
363  | 0  |       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,  | 
364  | 0  |         (long) cinfo->max_h_samp_factor);  | 
365  | 0  |     compptr->downsampled_height = (JDIMENSION)  | 
366  | 0  |       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,  | 
367  | 0  |         (long) cinfo->max_v_samp_factor);  | 
368  |  |     /* Mark component needed, until color conversion says otherwise */  | 
369  | 0  |     compptr->component_needed = TRUE;  | 
370  |  |     /* Mark no quantization table yet saved for component */  | 
371  | 0  |     compptr->quant_table = NULL;  | 
372  | 0  |   }  | 
373  |  |  | 
374  |  |   /* Compute number of fully interleaved MCU rows. */  | 
375  | 0  |   cinfo->total_iMCU_rows = (JDIMENSION)  | 
376  | 0  |     jdiv_round_up((long) cinfo->image_height,  | 
377  | 0  |             (long) (cinfo->max_v_samp_factor * cinfo->block_size));  | 
378  |  |  | 
379  |  |   /* Decide whether file contains multiple scans */  | 
380  | 0  |   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)  | 
381  | 0  |     cinfo->inputctl->has_multiple_scans = TRUE;  | 
382  | 0  |   else  | 
383  | 0  |     cinfo->inputctl->has_multiple_scans = FALSE;  | 
384  | 0  | }  | 
385  |  |  | 
386  |  |  | 
387  |  | LOCAL(void)  | 
388  |  | per_scan_setup (j_decompress_ptr cinfo)  | 
389  |  | /* Do computations that are needed before processing a JPEG scan */  | 
390  |  | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */  | 
391  | 0  | { | 
392  | 0  |   int ci, mcublks, tmp;  | 
393  | 0  |   jpeg_component_info *compptr;  | 
394  |  |     | 
395  | 0  |   if (cinfo->comps_in_scan == 1) { | 
396  |  |       | 
397  |  |     /* Noninterleaved (single-component) scan */  | 
398  | 0  |     compptr = cinfo->cur_comp_info[0];  | 
399  |  |       | 
400  |  |     /* Overall image size in MCUs */  | 
401  | 0  |     cinfo->MCUs_per_row = compptr->width_in_blocks;  | 
402  | 0  |     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;  | 
403  |  |       | 
404  |  |     /* For noninterleaved scan, always one block per MCU */  | 
405  | 0  |     compptr->MCU_width = 1;  | 
406  | 0  |     compptr->MCU_height = 1;  | 
407  | 0  |     compptr->MCU_blocks = 1;  | 
408  | 0  |     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;  | 
409  | 0  |     compptr->last_col_width = 1;  | 
410  |  |     /* For noninterleaved scans, it is convenient to define last_row_height  | 
411  |  |      * as the number of block rows present in the last iMCU row.  | 
412  |  |      */  | 
413  | 0  |     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);  | 
414  | 0  |     if (tmp == 0) tmp = compptr->v_samp_factor;  | 
415  | 0  |     compptr->last_row_height = tmp;  | 
416  |  |       | 
417  |  |     /* Prepare array describing MCU composition */  | 
418  | 0  |     cinfo->blocks_in_MCU = 1;  | 
419  | 0  |     cinfo->MCU_membership[0] = 0;  | 
420  |  |       | 
421  | 0  |   } else { | 
422  |  |       | 
423  |  |     /* Interleaved (multi-component) scan */  | 
424  | 0  |     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)  | 
425  | 0  |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,  | 
426  | 0  |          MAX_COMPS_IN_SCAN);  | 
427  |  |       | 
428  |  |     /* Overall image size in MCUs */  | 
429  | 0  |     cinfo->MCUs_per_row = (JDIMENSION)  | 
430  | 0  |       jdiv_round_up((long) cinfo->image_width,  | 
431  | 0  |         (long) (cinfo->max_h_samp_factor * cinfo->block_size));  | 
432  | 0  |     cinfo->MCU_rows_in_scan = (JDIMENSION)  | 
433  | 0  |       jdiv_round_up((long) cinfo->image_height,  | 
434  | 0  |         (long) (cinfo->max_v_samp_factor * cinfo->block_size));  | 
435  |  |       | 
436  | 0  |     cinfo->blocks_in_MCU = 0;  | 
437  |  |       | 
438  | 0  |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
439  | 0  |       compptr = cinfo->cur_comp_info[ci];  | 
440  |  |       /* Sampling factors give # of blocks of component in each MCU */  | 
441  | 0  |       compptr->MCU_width = compptr->h_samp_factor;  | 
442  | 0  |       compptr->MCU_height = compptr->v_samp_factor;  | 
443  | 0  |       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;  | 
444  | 0  |       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;  | 
445  |  |       /* Figure number of non-dummy blocks in last MCU column & row */  | 
446  | 0  |       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);  | 
447  | 0  |       if (tmp == 0) tmp = compptr->MCU_width;  | 
448  | 0  |       compptr->last_col_width = tmp;  | 
449  | 0  |       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);  | 
450  | 0  |       if (tmp == 0) tmp = compptr->MCU_height;  | 
451  | 0  |       compptr->last_row_height = tmp;  | 
452  |  |       /* Prepare array describing MCU composition */  | 
453  | 0  |       mcublks = compptr->MCU_blocks;  | 
454  | 0  |       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)  | 
455  | 0  |   ERREXIT(cinfo, JERR_BAD_MCU_SIZE);  | 
456  | 0  |       while (mcublks-- > 0) { | 
457  | 0  |   cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;  | 
458  | 0  |       }  | 
459  | 0  |     }  | 
460  |  |       | 
461  | 0  |   }  | 
462  | 0  | }  | 
463  |  |  | 
464  |  |  | 
465  |  | /*  | 
466  |  |  * Save away a copy of the Q-table referenced by each component present  | 
467  |  |  * in the current scan, unless already saved during a prior scan.  | 
468  |  |  *  | 
469  |  |  * In a multiple-scan JPEG file, the encoder could assign different components  | 
470  |  |  * the same Q-table slot number, but change table definitions between scans  | 
471  |  |  * so that each component uses a different Q-table.  (The IJG encoder is not  | 
472  |  |  * currently capable of doing this, but other encoders might.)  Since we want  | 
473  |  |  * to be able to dequantize all the components at the end of the file, this  | 
474  |  |  * means that we have to save away the table actually used for each component.  | 
475  |  |  * We do this by copying the table at the start of the first scan containing  | 
476  |  |  * the component.  | 
477  |  |  * The JPEG spec prohibits the encoder from changing the contents of a Q-table  | 
478  |  |  * slot between scans of a component using that slot.  If the encoder does so  | 
479  |  |  * anyway, this decoder will simply use the Q-table values that were current  | 
480  |  |  * at the start of the first scan for the component.  | 
481  |  |  *  | 
482  |  |  * The decompressor output side looks only at the saved quant tables,  | 
483  |  |  * not at the current Q-table slots.  | 
484  |  |  */  | 
485  |  |  | 
486  |  | LOCAL(void)  | 
487  |  | latch_quant_tables (j_decompress_ptr cinfo)  | 
488  | 0  | { | 
489  | 0  |   int ci, qtblno;  | 
490  | 0  |   jpeg_component_info *compptr;  | 
491  | 0  |   JQUANT_TBL * qtbl;  | 
492  |  | 
  | 
493  | 0  |   for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
494  | 0  |     compptr = cinfo->cur_comp_info[ci];  | 
495  |  |     /* No work if we already saved Q-table for this component */  | 
496  | 0  |     if (compptr->quant_table != NULL)  | 
497  | 0  |       continue;  | 
498  |  |     /* Make sure specified quantization table is present */  | 
499  | 0  |     qtblno = compptr->quant_tbl_no;  | 
500  | 0  |     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||  | 
501  | 0  |   cinfo->quant_tbl_ptrs[qtblno] == NULL)  | 
502  | 0  |       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);  | 
503  |  |     /* OK, save away the quantization table */  | 
504  | 0  |     qtbl = (JQUANT_TBL *)  | 
505  | 0  |       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,  | 
506  | 0  |           SIZEOF(JQUANT_TBL));  | 
507  | 0  |     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));  | 
508  | 0  |     compptr->quant_table = qtbl;  | 
509  | 0  |   }  | 
510  | 0  | }  | 
511  |  |  | 
512  |  |  | 
513  |  | /*  | 
514  |  |  * Initialize the input modules to read a scan of compressed data.  | 
515  |  |  * The first call to this is done by jdmaster.c after initializing  | 
516  |  |  * the entire decompressor (during jpeg_start_decompress).  | 
517  |  |  * Subsequent calls come from consume_markers, below.  | 
518  |  |  */  | 
519  |  |  | 
520  |  | METHODDEF(void)  | 
521  |  | start_input_pass (j_decompress_ptr cinfo)  | 
522  | 0  | { | 
523  | 0  |   per_scan_setup(cinfo);  | 
524  | 0  |   latch_quant_tables(cinfo);  | 
525  | 0  |   (*cinfo->entropy->start_pass) (cinfo);  | 
526  | 0  |   (*cinfo->coef->start_input_pass) (cinfo);  | 
527  | 0  |   cinfo->inputctl->consume_input = cinfo->coef->consume_data;  | 
528  | 0  | }  | 
529  |  |  | 
530  |  |  | 
531  |  | /*  | 
532  |  |  * Finish up after inputting a compressed-data scan.  | 
533  |  |  * This is called by the coefficient controller after it's read all  | 
534  |  |  * the expected data of the scan.  | 
535  |  |  */  | 
536  |  |  | 
537  |  | METHODDEF(void)  | 
538  |  | finish_input_pass (j_decompress_ptr cinfo)  | 
539  | 0  | { | 
540  | 0  |   (*cinfo->entropy->finish_pass) (cinfo);  | 
541  | 0  |   cinfo->inputctl->consume_input = consume_markers;  | 
542  | 0  | }  | 
543  |  |  | 
544  |  |  | 
545  |  | /*  | 
546  |  |  * Read JPEG markers before, between, or after compressed-data scans.  | 
547  |  |  * Change state as necessary when a new scan is reached.  | 
548  |  |  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.  | 
549  |  |  *  | 
550  |  |  * The consume_input method pointer points either here or to the  | 
551  |  |  * coefficient controller's consume_data routine, depending on whether  | 
552  |  |  * we are reading a compressed data segment or inter-segment markers.  | 
553  |  |  *  | 
554  |  |  * Note: This function should NOT return a pseudo SOS marker (with zero  | 
555  |  |  * component number) to the caller.  A pseudo marker received by  | 
556  |  |  * read_markers is processed and then skipped for other markers.  | 
557  |  |  */  | 
558  |  |  | 
559  |  | METHODDEF(int)  | 
560  |  | consume_markers (j_decompress_ptr cinfo)  | 
561  | 0  | { | 
562  | 0  |   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;  | 
563  | 0  |   int val;  | 
564  |  | 
  | 
565  | 0  |   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */  | 
566  | 0  |     return JPEG_REACHED_EOI;  | 
567  |  |  | 
568  | 0  |   for (;;) {     /* Loop to pass pseudo SOS marker */ | 
569  | 0  |     val = (*cinfo->marker->read_markers) (cinfo);  | 
570  |  | 
  | 
571  | 0  |     switch (val) { | 
572  | 0  |     case JPEG_REACHED_SOS: /* Found SOS */  | 
573  | 0  |       if (inputctl->inheaders) { /* 1st SOS */ | 
574  | 0  |   if (inputctl->inheaders == 1)  | 
575  | 0  |     initial_setup(cinfo);  | 
576  | 0  |   if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */ | 
577  | 0  |     inputctl->inheaders = 2;  | 
578  | 0  |     break;  | 
579  | 0  |   }  | 
580  | 0  |   inputctl->inheaders = 0;  | 
581  |  |   /* Note: start_input_pass must be called by jdmaster.c  | 
582  |  |    * before any more input can be consumed.  jdapimin.c is  | 
583  |  |    * responsible for enforcing this sequencing.  | 
584  |  |    */  | 
585  | 0  |       } else {     /* 2nd or later SOS marker */ | 
586  | 0  |   if (! inputctl->pub.has_multiple_scans)  | 
587  | 0  |     ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */  | 
588  | 0  |   if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */  | 
589  | 0  |     break;  | 
590  | 0  |   start_input_pass(cinfo);  | 
591  | 0  |       }  | 
592  | 0  |       return val;  | 
593  | 0  |     case JPEG_REACHED_EOI: /* Found EOI */  | 
594  | 0  |       inputctl->pub.eoi_reached = TRUE;  | 
595  | 0  |       if (inputctl->inheaders) { /* Tables-only datastream, apparently */ | 
596  | 0  |   if (cinfo->marker->saw_SOF)  | 
597  | 0  |     ERREXIT(cinfo, JERR_SOF_NO_SOS);  | 
598  | 0  |       } else { | 
599  |  |   /* Prevent infinite loop in coef ctlr's decompress_data routine  | 
600  |  |    * if user set output_scan_number larger than number of scans.  | 
601  |  |    */  | 
602  | 0  |   if (cinfo->output_scan_number > cinfo->input_scan_number)  | 
603  | 0  |     cinfo->output_scan_number = cinfo->input_scan_number;  | 
604  | 0  |       }  | 
605  | 0  |       return val;  | 
606  | 0  |     case JPEG_SUSPENDED:  | 
607  | 0  |       return val;  | 
608  | 0  |     default:  | 
609  | 0  |       return val;  | 
610  | 0  |     }  | 
611  | 0  |   }  | 
612  | 0  | }  | 
613  |  |  | 
614  |  |  | 
615  |  | /*  | 
616  |  |  * Reset state to begin a fresh datastream.  | 
617  |  |  */  | 
618  |  |  | 
619  |  | METHODDEF(void)  | 
620  |  | reset_input_controller (j_decompress_ptr cinfo)  | 
621  | 0  | { | 
622  | 0  |   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;  | 
623  |  | 
  | 
624  | 0  |   inputctl->pub.consume_input = consume_markers;  | 
625  | 0  |   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */  | 
626  | 0  |   inputctl->pub.eoi_reached = FALSE;  | 
627  | 0  |   inputctl->inheaders = 1;  | 
628  |  |   /* Reset other modules */  | 
629  | 0  |   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);  | 
630  | 0  |   (*cinfo->marker->reset_marker_reader) (cinfo);  | 
631  |  |   /* Reset progression state -- would be cleaner if entropy decoder did this */  | 
632  | 0  |   cinfo->coef_bits = NULL;  | 
633  | 0  | }  | 
634  |  |  | 
635  |  |  | 
636  |  | /*  | 
637  |  |  * Initialize the input controller module.  | 
638  |  |  * This is called only once, when the decompression object is created.  | 
639  |  |  */  | 
640  |  |  | 
641  |  | GLOBAL(void)  | 
642  |  | jinit_input_controller (j_decompress_ptr cinfo)  | 
643  | 0  | { | 
644  | 0  |   my_inputctl_ptr inputctl;  | 
645  |  |  | 
646  |  |   /* Create subobject in permanent pool */  | 
647  | 0  |   inputctl = (my_inputctl_ptr)  | 
648  | 0  |     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,  | 
649  | 0  |         SIZEOF(my_input_controller));  | 
650  | 0  |   cinfo->inputctl = &inputctl->pub;  | 
651  |  |   /* Initialize method pointers */  | 
652  | 0  |   inputctl->pub.consume_input = consume_markers;  | 
653  | 0  |   inputctl->pub.reset_input_controller = reset_input_controller;  | 
654  | 0  |   inputctl->pub.start_input_pass = start_input_pass;  | 
655  | 0  |   inputctl->pub.finish_input_pass = finish_input_pass;  | 
656  |  |   /* Initialize state: can't use reset_input_controller since we don't  | 
657  |  |    * want to try to reset other modules yet.  | 
658  |  |    */  | 
659  | 0  |   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */  | 
660  | 0  |   inputctl->pub.eoi_reached = FALSE;  | 
661  | 0  |   inputctl->inheaders = 1;  | 
662  | 0  | }  |