4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains master control logic for the JPEG decompressor.
9 * These routines are concerned with selecting the modules to be executed
10 * and with determining the number of passes and the work to be done in each
14 #define JPEG_INTERNALS
18 /* Use static array */
20 const JSAMPLE static_range_table
[ (5 * (MAXJSAMPLE
+1) + CENTERJSAMPLE
) * SIZEOF(JSAMPLE
) ]={
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
39 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
40 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
41 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a,
42 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
43 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
44 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
45 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
46 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
47 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94,
48 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3,
49 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2,
50 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1,
51 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
52 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
53 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
54 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
55 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
107 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
108 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
109 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
110 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
111 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
112 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
113 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72,
114 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
120 struct jpeg_decomp_master pub
; /* public fields */
122 int pass_number
; /* # of passes completed */
124 boolean using_merged_upsample
; /* TRUE if using merged upsample/cconvert */
126 /* Saved references to initialized quantizer modules,
127 * in case we need to switch modes.
129 struct jpeg_color_quantizer
* quantizer_1pass
;
130 struct jpeg_color_quantizer
* quantizer_2pass
;
133 typedef my_decomp_master
* my_master_ptr
;
137 * Determine whether merged upsample/color conversion should be used.
138 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
142 use_merged_upsample (j_decompress_ptr cinfo
)
144 #ifdef UPSAMPLE_MERGING_SUPPORTED
145 /* Merging is the equivalent of plain box-filter upsampling */
146 if (cinfo
->do_fancy_upsampling
|| cinfo
->CCIR601_sampling
)
148 /* jdmerge.c only supports YCC=>RGB color conversion */
149 if (cinfo
->jpeg_color_space
!= JCS_YCbCr
|| cinfo
->num_components
!= 3 ||
150 cinfo
->out_color_space
!= JCS_RGB
||
151 cinfo
->out_color_components
!= RGB_PIXELSIZE
)
153 /* and it only handles 2h1v or 2h2v sampling ratios */
154 if (cinfo
->comp_info
[0].h_samp_factor
!= 2 ||
155 cinfo
->comp_info
[1].h_samp_factor
!= 1 ||
156 cinfo
->comp_info
[2].h_samp_factor
!= 1 ||
157 cinfo
->comp_info
[0].v_samp_factor
> 2 ||
158 cinfo
->comp_info
[1].v_samp_factor
!= 1 ||
159 cinfo
->comp_info
[2].v_samp_factor
!= 1)
161 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
162 if (cinfo
->comp_info
[0].DCT_scaled_size
!= cinfo
->min_DCT_scaled_size
||
163 cinfo
->comp_info
[1].DCT_scaled_size
!= cinfo
->min_DCT_scaled_size
||
164 cinfo
->comp_info
[2].DCT_scaled_size
!= cinfo
->min_DCT_scaled_size
)
166 /* ??? also need to test for upsample-time rescaling, when & if supported */
167 return TRUE
; /* by golly, it'll work... */
175 * Compute output image dimensions and related values.
176 * NOTE: this is exported for possible use by application.
177 * Hence it mustn't do anything that can't be done twice.
178 * Also note that it may be called before the master module is initialized!
182 jpeg_calc_output_dimensions (j_decompress_ptr cinfo
)
183 /* Do computations that are needed before master selection phase */
185 #ifdef IDCT_SCALING_SUPPORTED
187 jpeg_component_info
*compptr
;
190 /* Prevent application from calling me at wrong times */
191 if (cinfo
->global_state
!= DSTATE_READY
)
192 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
194 #ifdef IDCT_SCALING_SUPPORTED
196 /* Compute actual output image dimensions and DCT scaling choices. */
197 if (cinfo
->scale_num
* 8 <= cinfo
->scale_denom
) {
198 /* Provide 1/8 scaling */
199 cinfo
->output_width
= (JDIMENSION
)
200 jdiv_round_up((long) cinfo
->image_width
, 8L);
201 cinfo
->output_height
= (JDIMENSION
)
202 jdiv_round_up((long) cinfo
->image_height
, 8L);
203 cinfo
->min_DCT_scaled_size
= 1;
204 } else if (cinfo
->scale_num
* 4 <= cinfo
->scale_denom
) {
205 /* Provide 1/4 scaling */
206 cinfo
->output_width
= (JDIMENSION
)
207 jdiv_round_up((long) cinfo
->image_width
, 4L);
208 cinfo
->output_height
= (JDIMENSION
)
209 jdiv_round_up((long) cinfo
->image_height
, 4L);
210 cinfo
->min_DCT_scaled_size
= 2;
211 } else if (cinfo
->scale_num
* 2 <= cinfo
->scale_denom
) {
212 /* Provide 1/2 scaling */
213 cinfo
->output_width
= (JDIMENSION
)
214 jdiv_round_up((long) cinfo
->image_width
, 2L);
215 cinfo
->output_height
= (JDIMENSION
)
216 jdiv_round_up((long) cinfo
->image_height
, 2L);
217 cinfo
->min_DCT_scaled_size
= 4;
219 /* Provide 1/1 scaling */
220 cinfo
->output_width
= cinfo
->image_width
;
221 cinfo
->output_height
= cinfo
->image_height
;
222 cinfo
->min_DCT_scaled_size
= DCTSIZE
;
224 /* In selecting the actual DCT scaling for each component, we try to
225 * scale up the chroma components via IDCT scaling rather than upsampling.
226 * This saves time if the upsampler gets to use 1:1 scaling.
227 * Note this code assumes that the supported DCT scalings are powers of 2.
229 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
231 int ssize
= cinfo
->min_DCT_scaled_size
;
232 while (ssize
< DCTSIZE
&&
233 (compptr
->h_samp_factor
* ssize
* 2 <=
234 cinfo
->max_h_samp_factor
* cinfo
->min_DCT_scaled_size
) &&
235 (compptr
->v_samp_factor
* ssize
* 2 <=
236 cinfo
->max_v_samp_factor
* cinfo
->min_DCT_scaled_size
)) {
239 compptr
->DCT_scaled_size
= ssize
;
242 /* Recompute downsampled dimensions of components;
243 * application needs to know these if using raw downsampled data.
245 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
247 /* Size in samples, after IDCT scaling */
248 compptr
->downsampled_width
= (JDIMENSION
)
249 jdiv_round_up((long) cinfo
->image_width
*
250 (long) (compptr
->h_samp_factor
* compptr
->DCT_scaled_size
),
251 (long) (cinfo
->max_h_samp_factor
* DCTSIZE
));
252 compptr
->downsampled_height
= (JDIMENSION
)
253 jdiv_round_up((long) cinfo
->image_height
*
254 (long) (compptr
->v_samp_factor
* compptr
->DCT_scaled_size
),
255 (long) (cinfo
->max_v_samp_factor
* DCTSIZE
));
258 #else /* !IDCT_SCALING_SUPPORTED */
260 /* Hardwire it to "no scaling" */
261 cinfo
->output_width
= cinfo
->image_width
;
262 cinfo
->output_height
= cinfo
->image_height
;
263 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
264 * and has computed unscaled downsampled_width and downsampled_height.
267 #endif /* IDCT_SCALING_SUPPORTED */
269 /* Report number of components in selected colorspace. */
270 /* Probably this should be in the color conversion module... */
271 switch (cinfo
->out_color_space
) {
273 cinfo
->out_color_components
= 1;
276 #if RGB_PIXELSIZE != 3
277 cinfo
->out_color_components
= RGB_PIXELSIZE
;
279 #endif /* else share code with YCbCr */
281 cinfo
->out_color_components
= 3;
285 cinfo
->out_color_components
= 4;
287 default: /* else must be same colorspace as in file */
288 cinfo
->out_color_components
= cinfo
->num_components
;
291 cinfo
->output_components
= (cinfo
->quantize_colors
? 1 :
292 cinfo
->out_color_components
);
294 /* See if upsampler will want to emit more than one row at a time */
295 if (use_merged_upsample(cinfo
))
296 cinfo
->rec_outbuf_height
= cinfo
->max_v_samp_factor
;
298 cinfo
->rec_outbuf_height
= 1;
303 * Several decompression processes need to range-limit values to the range
304 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
305 * due to noise introduced by quantization, roundoff error, etc. These
306 * processes are inner loops and need to be as fast as possible. On most
307 * machines, particularly CPUs with pipelines or instruction prefetch,
308 * a (subscript-check-less) C table lookup
309 * x = sample_range_limit[x];
310 * is faster than explicit tests
312 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
313 * These processes all use a common table prepared by the routine below.
315 * For most steps we can mathematically guarantee that the initial value
316 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
317 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
318 * limiting step (just after the IDCT), a wildly out-of-range value is
319 * possible if the input data is corrupt. To avoid any chance of indexing
320 * off the end of memory and getting a bad-pointer trap, we perform the
321 * post-IDCT limiting thus:
322 * x = range_limit[x & MASK];
323 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
324 * samples. Under normal circumstances this is more than enough range and
325 * a correct output will be generated; with bogus input data the mask will
326 * cause wraparound, and we will safely generate a bogus-but-in-range output.
327 * For the post-IDCT step, we want to convert the data from signed to unsigned
328 * representation by adding CENTERJSAMPLE at the same time that we limit it.
329 * So the post-IDCT limiting table ends up looking like this:
330 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
331 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
332 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
333 * 0,1,...,CENTERJSAMPLE-1
334 * Negative inputs select values from the upper half of the table after
337 * We can save some space by overlapping the start of the post-IDCT table
338 * with the simpler range limiting table. The post-IDCT table begins at
339 * sample_range_limit + CENTERJSAMPLE.
341 * Note that the table is allocated in near data space on PCs; it's small
342 * enough and used often enough to justify this.
346 prepare_range_limit_table (j_decompress_ptr cinfo
)
347 /* Allocate and fill in the sample_range_limit table */
349 /* Use a static table and allow negative subscripts of simple table */
351 cinfo
->sample_range_limit
= (JSAMPLE
*) static_range_table
+ (MAXJSAMPLE
+1);
353 /* This code is used to create the values for the static table used above */
361 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
362 (5 * (MAXJSAMPLE
+1) + CENTERJSAMPLE
) * SIZEOF(JSAMPLE
));
363 table
+= (MAXJSAMPLE
+1); /* allow negative subscripts of simple table */
364 cinfo
->sample_range_limit
= table
;
365 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
366 MEMZERO(table
- (MAXJSAMPLE
+1), (MAXJSAMPLE
+1) * SIZEOF(JSAMPLE
));
367 /* Main part of "simple" table: limit[x] = x */
368 for (i
= 0; i
<= MAXJSAMPLE
; i
++)
369 table
[i
] = (JSAMPLE
) i
;
370 table
+= CENTERJSAMPLE
; /* Point to where post-IDCT table starts */
371 /* End of simple table, rest of first half of post-IDCT table */
372 for (i
= CENTERJSAMPLE
; i
< 2*(MAXJSAMPLE
+1); i
++)
373 table
[i
] = MAXJSAMPLE
;
374 /* Second half of post-IDCT table */
375 MEMZERO(table
+ (2 * (MAXJSAMPLE
+1)),
376 (2 * (MAXJSAMPLE
+1) - CENTERJSAMPLE
) * SIZEOF(JSAMPLE
));
377 MEMCOPY(table
+ (4 * (MAXJSAMPLE
+1) - CENTERJSAMPLE
),
378 cinfo
->sample_range_limit
, CENTERJSAMPLE
* SIZEOF(JSAMPLE
));
385 * Master selection of decompression modules.
386 * This is done once at jpeg_start_decompress time. We determine
387 * which modules will be used and give them appropriate initialization calls.
388 * We also initialize the decompressor input side to begin consuming data.
390 * Since jpeg_read_header has finished, we know what is in the SOF
391 * and (first) SOS markers. We also have all the application parameter
396 master_selection (j_decompress_ptr cinfo
)
398 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
399 boolean use_c_buffer
;
401 JDIMENSION jd_samplesperrow
;
403 /* Initialize dimensions and other stuff */
404 jpeg_calc_output_dimensions(cinfo
);
405 prepare_range_limit_table(cinfo
);
407 /* Width of an output scanline must be representable as JDIMENSION. */
408 samplesperrow
= (long) cinfo
->output_width
* (long) cinfo
->out_color_components
;
409 jd_samplesperrow
= (JDIMENSION
) samplesperrow
;
410 if ((long) jd_samplesperrow
!= samplesperrow
)
411 ERREXIT(cinfo
, JERR_WIDTH_OVERFLOW
);
413 /* Initialize my private state */
414 master
->pass_number
= 0;
415 master
->using_merged_upsample
= use_merged_upsample(cinfo
);
417 /* Color quantizer selection */
418 master
->quantizer_1pass
= NULL
;
419 master
->quantizer_2pass
= NULL
;
420 /* No mode changes if not using buffered-image mode. */
421 if (! cinfo
->quantize_colors
|| ! cinfo
->buffered_image
) {
422 cinfo
->enable_1pass_quant
= FALSE
;
423 cinfo
->enable_external_quant
= FALSE
;
424 cinfo
->enable_2pass_quant
= FALSE
;
426 if (cinfo
->quantize_colors
) {
427 if (cinfo
->raw_data_out
)
428 ERREXIT(cinfo
, JERR_NOTIMPL
);
429 /* 2-pass quantizer only works in 3-component color space. */
430 if (cinfo
->out_color_components
!= 3) {
431 cinfo
->enable_1pass_quant
= TRUE
;
432 cinfo
->enable_external_quant
= FALSE
;
433 cinfo
->enable_2pass_quant
= FALSE
;
434 cinfo
->colormap
= NULL
;
435 } else if (cinfo
->colormap
!= NULL
) {
436 cinfo
->enable_external_quant
= TRUE
;
437 } else if (cinfo
->two_pass_quantize
) {
438 cinfo
->enable_2pass_quant
= TRUE
;
440 cinfo
->enable_1pass_quant
= TRUE
;
443 if (cinfo
->enable_1pass_quant
) {
444 #ifdef QUANT_1PASS_SUPPORTED
445 jinit_1pass_quantizer(cinfo
);
446 master
->quantizer_1pass
= cinfo
->cquantize
;
448 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
452 /* We use the 2-pass code to map to external colormaps. */
453 if (cinfo
->enable_2pass_quant
|| cinfo
->enable_external_quant
) {
454 #ifdef QUANT_2PASS_SUPPORTED
455 jinit_2pass_quantizer(cinfo
);
456 master
->quantizer_2pass
= cinfo
->cquantize
;
458 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
461 /* If both quantizers are initialized, the 2-pass one is left active;
462 * this is necessary for starting with quantization to an external map.
466 /* Post-processing: in particular, color conversion first */
467 if (! cinfo
->raw_data_out
) {
468 if (master
->using_merged_upsample
) {
469 #ifdef UPSAMPLE_MERGING_SUPPORTED
470 jinit_merged_upsampler(cinfo
); /* does color conversion too */
472 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
475 jinit_color_deconverter(cinfo
);
476 jinit_upsampler(cinfo
);
478 jinit_d_post_controller(cinfo
, cinfo
->enable_2pass_quant
);
481 jinit_inverse_dct(cinfo
);
482 /* Entropy decoding: either Huffman or arithmetic coding. */
483 if (cinfo
->arith_code
) {
484 ERREXIT(cinfo
, JERR_ARITH_NOTIMPL
);
486 if (cinfo
->progressive_mode
) {
487 #ifdef D_PROGRESSIVE_SUPPORTED
488 jinit_phuff_decoder(cinfo
);
490 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
493 jinit_huff_decoder(cinfo
);
496 /* Initialize principal buffer controllers. */
497 use_c_buffer
= cinfo
->inputctl
->has_multiple_scans
|| cinfo
->buffered_image
;
498 jinit_d_coef_controller(cinfo
, use_c_buffer
);
500 if (! cinfo
->raw_data_out
)
501 jinit_d_main_controller(cinfo
, FALSE
/* never need full buffer here */);
503 /* We can now tell the memory manager to allocate virtual arrays. */
504 (*cinfo
->mem
->realize_virt_arrays
) ((j_common_ptr
) cinfo
);
506 /* Initialize input side of decompressor to consume first scan. */
507 (*cinfo
->inputctl
->start_input_pass
) (cinfo
);
509 #ifdef D_MULTISCAN_FILES_SUPPORTED
510 /* If jpeg_start_decompress will read the whole file, initialize
511 * progress monitoring appropriately. The input step is counted
514 if (cinfo
->progress
!= NULL
&& ! cinfo
->buffered_image
&&
515 cinfo
->inputctl
->has_multiple_scans
) {
517 /* Estimate number of scans to set pass_limit. */
518 if (cinfo
->progressive_mode
) {
519 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
520 nscans
= 2 + 3 * cinfo
->num_components
;
522 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
523 nscans
= cinfo
->num_components
;
525 cinfo
->progress
->pass_counter
= 0L;
526 cinfo
->progress
->pass_limit
= (long) cinfo
->total_iMCU_rows
* nscans
;
527 cinfo
->progress
->completed_passes
= 0;
528 cinfo
->progress
->total_passes
= (cinfo
->enable_2pass_quant
? 3 : 2);
529 /* Count the input pass as done */
530 master
->pass_number
++;
532 #endif /* D_MULTISCAN_FILES_SUPPORTED */
538 * This is called at the beginning of each output pass. We determine which
539 * modules will be active during this pass and give them appropriate
540 * start_pass calls. We also set is_dummy_pass to indicate whether this
541 * is a "real" output pass or a dummy pass for color quantization.
542 * (In the latter case, jdapistd.c will crank the pass to completion.)
546 prepare_for_output_pass (j_decompress_ptr cinfo
)
548 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
550 if (master
->pub
.is_dummy_pass
) {
551 #ifdef QUANT_2PASS_SUPPORTED
552 /* Final pass of 2-pass quantization */
553 master
->pub
.is_dummy_pass
= FALSE
;
554 (*cinfo
->cquantize
->start_pass
) (cinfo
, FALSE
);
555 (*cinfo
->post
->start_pass
) (cinfo
, JBUF_CRANK_DEST
);
556 (*cinfo
->main
->start_pass
) (cinfo
, JBUF_CRANK_DEST
);
558 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
559 #endif /* QUANT_2PASS_SUPPORTED */
561 if (cinfo
->quantize_colors
&& cinfo
->colormap
== NULL
) {
562 /* Select new quantization method */
563 if (cinfo
->two_pass_quantize
&& cinfo
->enable_2pass_quant
) {
564 cinfo
->cquantize
= master
->quantizer_2pass
;
565 master
->pub
.is_dummy_pass
= TRUE
;
566 } else if (cinfo
->enable_1pass_quant
) {
567 cinfo
->cquantize
= master
->quantizer_1pass
;
569 ERREXIT(cinfo
, JERR_MODE_CHANGE
);
572 (*cinfo
->idct
->start_pass
) (cinfo
);
573 (*cinfo
->coef
->start_output_pass
) (cinfo
);
574 if (! cinfo
->raw_data_out
) {
575 if (! master
->using_merged_upsample
)
576 (*cinfo
->cconvert
->start_pass
) (cinfo
);
577 (*cinfo
->upsample
->start_pass
) (cinfo
);
578 if (cinfo
->quantize_colors
)
579 (*cinfo
->cquantize
->start_pass
) (cinfo
, master
->pub
.is_dummy_pass
);
580 (*cinfo
->post
->start_pass
) (cinfo
,
581 (master
->pub
.is_dummy_pass
? JBUF_SAVE_AND_PASS
: JBUF_PASS_THRU
));
582 (*cinfo
->main
->start_pass
) (cinfo
, JBUF_PASS_THRU
);
586 /* Set up progress monitor's pass info if present */
587 if (cinfo
->progress
!= NULL
) {
588 cinfo
->progress
->completed_passes
= master
->pass_number
;
589 cinfo
->progress
->total_passes
= master
->pass_number
+
590 (master
->pub
.is_dummy_pass
? 2 : 1);
591 /* In buffered-image mode, we assume one more output pass if EOI not
592 * yet reached, but no more passes if EOI has been reached.
594 if (cinfo
->buffered_image
&& ! cinfo
->inputctl
->eoi_reached
) {
595 cinfo
->progress
->total_passes
+= (cinfo
->enable_2pass_quant
? 2 : 1);
602 * Finish up at end of an output pass.
606 finish_output_pass (j_decompress_ptr cinfo
)
608 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
610 if (cinfo
->quantize_colors
)
611 (*cinfo
->cquantize
->finish_pass
) (cinfo
);
612 master
->pass_number
++;
616 #ifdef D_MULTISCAN_FILES_SUPPORTED
619 * Switch to a new external colormap between output passes.
623 jpeg_new_colormap (j_decompress_ptr cinfo
)
625 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
627 /* Prevent application from calling me at wrong times */
628 if (cinfo
->global_state
!= DSTATE_BUFIMAGE
)
629 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
631 if (cinfo
->quantize_colors
&& cinfo
->enable_external_quant
&&
632 cinfo
->colormap
!= NULL
) {
633 /* Select 2-pass quantizer for external colormap use */
634 cinfo
->cquantize
= master
->quantizer_2pass
;
635 /* Notify quantizer of colormap change */
636 (*cinfo
->cquantize
->new_color_map
) (cinfo
);
637 master
->pub
.is_dummy_pass
= FALSE
; /* just in case */
639 ERREXIT(cinfo
, JERR_MODE_CHANGE
);
642 #endif /* D_MULTISCAN_FILES_SUPPORTED */
646 * Initialize master decompression control and select active modules.
647 * This is performed at the start of jpeg_start_decompress.
651 jinit_master_decompress (j_decompress_ptr cinfo
)
653 my_master_ptr master
;
655 master
= (my_master_ptr
)
656 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
657 SIZEOF(my_decomp_master
));
658 cinfo
->master
= (struct jpeg_decomp_master
*) master
;
659 master
->pub
.prepare_for_output_pass
= prepare_for_output_pass
;
660 master
->pub
.finish_output_pass
= finish_output_pass
;
662 master
->pub
.is_dummy_pass
= FALSE
;
664 master_selection(cinfo
);