40 |
41 |
41 /* |
42 /* |
42 * Support routines that do various essential calculations. |
43 * Support routines that do various essential calculations. |
43 */ |
44 */ |
44 |
45 |
|
46 /* |
|
47 * Compute JPEG image dimensions and related values. |
|
48 * NOTE: this is exported for possible use by application. |
|
49 * Hence it mustn't do anything that can't be done twice. |
|
50 */ |
|
51 |
|
52 GLOBAL(void) |
|
53 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) |
|
54 /* Do computations that are needed before master selection phase */ |
|
55 { |
|
56 #ifdef DCT_SCALING_SUPPORTED |
|
57 |
|
58 /* Compute actual JPEG image dimensions and DCT scaling choices. */ |
|
59 if (cinfo->scale_num >= cinfo->scale_denom * 8) { |
|
60 /* Provide 8/1 scaling */ |
|
61 cinfo->jpeg_width = cinfo->image_width << 3; |
|
62 cinfo->jpeg_height = cinfo->image_height << 3; |
|
63 cinfo->min_DCT_h_scaled_size = 1; |
|
64 cinfo->min_DCT_v_scaled_size = 1; |
|
65 } else if (cinfo->scale_num >= cinfo->scale_denom * 4) { |
|
66 /* Provide 4/1 scaling */ |
|
67 cinfo->jpeg_width = cinfo->image_width << 2; |
|
68 cinfo->jpeg_height = cinfo->image_height << 2; |
|
69 cinfo->min_DCT_h_scaled_size = 2; |
|
70 cinfo->min_DCT_v_scaled_size = 2; |
|
71 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) { |
|
72 /* Provide 8/3 scaling */ |
|
73 cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION) |
|
74 jdiv_round_up((long) cinfo->image_width * 2, 3L); |
|
75 cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION) |
|
76 jdiv_round_up((long) cinfo->image_height * 2, 3L); |
|
77 cinfo->min_DCT_h_scaled_size = 3; |
|
78 cinfo->min_DCT_v_scaled_size = 3; |
|
79 } else if (cinfo->scale_num >= cinfo->scale_denom * 2) { |
|
80 /* Provide 2/1 scaling */ |
|
81 cinfo->jpeg_width = cinfo->image_width << 1; |
|
82 cinfo->jpeg_height = cinfo->image_height << 1; |
|
83 cinfo->min_DCT_h_scaled_size = 4; |
|
84 cinfo->min_DCT_v_scaled_size = 4; |
|
85 } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) { |
|
86 /* Provide 8/5 scaling */ |
|
87 cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) |
|
88 jdiv_round_up((long) cinfo->image_width * 3, 5L); |
|
89 cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) |
|
90 jdiv_round_up((long) cinfo->image_height * 3, 5L); |
|
91 cinfo->min_DCT_h_scaled_size = 5; |
|
92 cinfo->min_DCT_v_scaled_size = 5; |
|
93 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) { |
|
94 /* Provide 4/3 scaling */ |
|
95 cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) |
|
96 jdiv_round_up((long) cinfo->image_width, 3L); |
|
97 cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) |
|
98 jdiv_round_up((long) cinfo->image_height, 3L); |
|
99 cinfo->min_DCT_h_scaled_size = 6; |
|
100 cinfo->min_DCT_v_scaled_size = 6; |
|
101 } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) { |
|
102 /* Provide 8/7 scaling */ |
|
103 cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) |
|
104 jdiv_round_up((long) cinfo->image_width, 7L); |
|
105 cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) |
|
106 jdiv_round_up((long) cinfo->image_height, 7L); |
|
107 cinfo->min_DCT_h_scaled_size = 7; |
|
108 cinfo->min_DCT_v_scaled_size = 7; |
|
109 } else if (cinfo->scale_num >= cinfo->scale_denom) { |
|
110 /* Provide 1/1 scaling */ |
|
111 cinfo->jpeg_width = cinfo->image_width; |
|
112 cinfo->jpeg_height = cinfo->image_height; |
|
113 cinfo->min_DCT_h_scaled_size = 8; |
|
114 cinfo->min_DCT_v_scaled_size = 8; |
|
115 } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) { |
|
116 /* Provide 8/9 scaling */ |
|
117 cinfo->jpeg_width = (JDIMENSION) |
|
118 jdiv_round_up((long) cinfo->image_width * 8, 9L); |
|
119 cinfo->jpeg_height = (JDIMENSION) |
|
120 jdiv_round_up((long) cinfo->image_height * 8, 9L); |
|
121 cinfo->min_DCT_h_scaled_size = 9; |
|
122 cinfo->min_DCT_v_scaled_size = 9; |
|
123 } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) { |
|
124 /* Provide 4/5 scaling */ |
|
125 cinfo->jpeg_width = (JDIMENSION) |
|
126 jdiv_round_up((long) cinfo->image_width * 4, 5L); |
|
127 cinfo->jpeg_height = (JDIMENSION) |
|
128 jdiv_round_up((long) cinfo->image_height * 4, 5L); |
|
129 cinfo->min_DCT_h_scaled_size = 10; |
|
130 cinfo->min_DCT_v_scaled_size = 10; |
|
131 } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) { |
|
132 /* Provide 8/11 scaling */ |
|
133 cinfo->jpeg_width = (JDIMENSION) |
|
134 jdiv_round_up((long) cinfo->image_width * 8, 11L); |
|
135 cinfo->jpeg_height = (JDIMENSION) |
|
136 jdiv_round_up((long) cinfo->image_height * 8, 11L); |
|
137 cinfo->min_DCT_h_scaled_size = 11; |
|
138 cinfo->min_DCT_v_scaled_size = 11; |
|
139 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) { |
|
140 /* Provide 2/3 scaling */ |
|
141 cinfo->jpeg_width = (JDIMENSION) |
|
142 jdiv_round_up((long) cinfo->image_width * 2, 3L); |
|
143 cinfo->jpeg_height = (JDIMENSION) |
|
144 jdiv_round_up((long) cinfo->image_height * 2, 3L); |
|
145 cinfo->min_DCT_h_scaled_size = 12; |
|
146 cinfo->min_DCT_v_scaled_size = 12; |
|
147 } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) { |
|
148 /* Provide 8/13 scaling */ |
|
149 cinfo->jpeg_width = (JDIMENSION) |
|
150 jdiv_round_up((long) cinfo->image_width * 8, 13L); |
|
151 cinfo->jpeg_height = (JDIMENSION) |
|
152 jdiv_round_up((long) cinfo->image_height * 8, 13L); |
|
153 cinfo->min_DCT_h_scaled_size = 13; |
|
154 cinfo->min_DCT_v_scaled_size = 13; |
|
155 } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) { |
|
156 /* Provide 4/7 scaling */ |
|
157 cinfo->jpeg_width = (JDIMENSION) |
|
158 jdiv_round_up((long) cinfo->image_width * 4, 7L); |
|
159 cinfo->jpeg_height = (JDIMENSION) |
|
160 jdiv_round_up((long) cinfo->image_height * 4, 7L); |
|
161 cinfo->min_DCT_h_scaled_size = 14; |
|
162 cinfo->min_DCT_v_scaled_size = 14; |
|
163 } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) { |
|
164 /* Provide 8/15 scaling */ |
|
165 cinfo->jpeg_width = (JDIMENSION) |
|
166 jdiv_round_up((long) cinfo->image_width * 8, 15L); |
|
167 cinfo->jpeg_height = (JDIMENSION) |
|
168 jdiv_round_up((long) cinfo->image_height * 8, 15L); |
|
169 cinfo->min_DCT_h_scaled_size = 15; |
|
170 cinfo->min_DCT_v_scaled_size = 15; |
|
171 } else { |
|
172 /* Provide 1/2 scaling */ |
|
173 cinfo->jpeg_width = (JDIMENSION) |
|
174 jdiv_round_up((long) cinfo->image_width, 2L); |
|
175 cinfo->jpeg_height = (JDIMENSION) |
|
176 jdiv_round_up((long) cinfo->image_height, 2L); |
|
177 cinfo->min_DCT_h_scaled_size = 16; |
|
178 cinfo->min_DCT_v_scaled_size = 16; |
|
179 } |
|
180 |
|
181 #else /* !DCT_SCALING_SUPPORTED */ |
|
182 |
|
183 /* Hardwire it to "no scaling" */ |
|
184 cinfo->jpeg_width = cinfo->image_width; |
|
185 cinfo->jpeg_height = cinfo->image_height; |
|
186 cinfo->min_DCT_h_scaled_size = DCTSIZE; |
|
187 cinfo->min_DCT_v_scaled_size = DCTSIZE; |
|
188 |
|
189 #endif /* DCT_SCALING_SUPPORTED */ |
|
190 |
|
191 cinfo->block_size = DCTSIZE; |
|
192 cinfo->natural_order = jpeg_natural_order; |
|
193 cinfo->lim_Se = DCTSIZE2-1; |
|
194 } |
|
195 |
|
196 |
45 LOCAL(void) |
197 LOCAL(void) |
46 initial_setup (j_compress_ptr cinfo) |
198 jpeg_calc_trans_dimensions (j_compress_ptr cinfo) |
|
199 { |
|
200 if (cinfo->min_DCT_h_scaled_size < 1 || cinfo->min_DCT_h_scaled_size > 16 |
|
201 || cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size) |
|
202 ERREXIT2(cinfo, JERR_BAD_DCTSIZE, |
|
203 cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size); |
|
204 |
|
205 cinfo->block_size = cinfo->min_DCT_h_scaled_size; |
|
206 |
|
207 switch (cinfo->block_size) { |
|
208 case 2: cinfo->natural_order = jpeg_natural_order2; break; |
|
209 case 3: cinfo->natural_order = jpeg_natural_order3; break; |
|
210 case 4: cinfo->natural_order = jpeg_natural_order4; break; |
|
211 case 5: cinfo->natural_order = jpeg_natural_order5; break; |
|
212 case 6: cinfo->natural_order = jpeg_natural_order6; break; |
|
213 case 7: cinfo->natural_order = jpeg_natural_order7; break; |
|
214 default: cinfo->natural_order = jpeg_natural_order; break; |
|
215 } |
|
216 |
|
217 cinfo->lim_Se = cinfo->block_size < DCTSIZE ? |
|
218 cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; |
|
219 } |
|
220 |
|
221 |
|
222 LOCAL(void) |
|
223 initial_setup (j_compress_ptr cinfo, boolean transcode_only) |
47 /* Do computations that are needed before master selection phase */ |
224 /* Do computations that are needed before master selection phase */ |
48 { |
225 { |
49 int ci; |
226 int ci, ssize; |
50 jpeg_component_info *compptr; |
227 jpeg_component_info *compptr; |
51 long samplesperrow; |
228 long samplesperrow; |
52 JDIMENSION jd_samplesperrow; |
229 JDIMENSION jd_samplesperrow; |
53 |
230 |
|
231 if (transcode_only) |
|
232 jpeg_calc_trans_dimensions(cinfo); |
|
233 else |
|
234 jpeg_calc_jpeg_dimensions(cinfo); |
|
235 |
54 /* Sanity check on image dimensions */ |
236 /* Sanity check on image dimensions */ |
55 if (cinfo->image_height <= 0 || cinfo->image_width <= 0 |
237 if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || |
56 || cinfo->num_components <= 0 || cinfo->input_components <= 0) |
238 cinfo->num_components <= 0 || cinfo->input_components <= 0) |
57 ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
239 ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
58 |
240 |
59 /* Make sure image isn't bigger than I can handle */ |
241 /* Make sure image isn't bigger than I can handle */ |
60 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || |
242 if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || |
61 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) |
243 (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) |
62 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); |
244 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); |
63 |
245 |
64 /* Width of an input scanline must be representable as JDIMENSION. */ |
246 /* Width of an input scanline must be representable as JDIMENSION. */ |
65 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; |
247 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; |
66 jd_samplesperrow = (JDIMENSION) samplesperrow; |
248 jd_samplesperrow = (JDIMENSION) samplesperrow; |
93 /* Compute dimensions of components */ |
275 /* Compute dimensions of components */ |
94 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
276 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
95 ci++, compptr++) { |
277 ci++, compptr++) { |
96 /* Fill in the correct component_index value; don't rely on application */ |
278 /* Fill in the correct component_index value; don't rely on application */ |
97 compptr->component_index = ci; |
279 compptr->component_index = ci; |
98 /* For compression, we never do DCT scaling. */ |
280 /* In selecting the actual DCT scaling for each component, we try to |
99 compptr->DCT_scaled_size = DCTSIZE; |
281 * scale down the chroma components via DCT scaling rather than downsampling. |
|
282 * This saves time if the downsampler gets to use 1:1 scaling. |
|
283 * Note this code adapts subsampling ratios which are powers of 2. |
|
284 */ |
|
285 ssize = 1; |
|
286 #ifdef DCT_SCALING_SUPPORTED |
|
287 while (cinfo->min_DCT_h_scaled_size * ssize <= |
|
288 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && |
|
289 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { |
|
290 ssize = ssize * 2; |
|
291 } |
|
292 #endif |
|
293 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; |
|
294 ssize = 1; |
|
295 #ifdef DCT_SCALING_SUPPORTED |
|
296 while (cinfo->min_DCT_v_scaled_size * ssize <= |
|
297 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && |
|
298 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { |
|
299 ssize = ssize * 2; |
|
300 } |
|
301 #endif |
|
302 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; |
|
303 |
|
304 /* We don't support DCT ratios larger than 2. */ |
|
305 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) |
|
306 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; |
|
307 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) |
|
308 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; |
|
309 |
100 /* Size in DCT blocks */ |
310 /* Size in DCT blocks */ |
101 compptr->width_in_blocks = (JDIMENSION) |
311 compptr->width_in_blocks = (JDIMENSION) |
102 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, |
312 jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, |
103 (long) (cinfo->max_h_samp_factor * DCTSIZE)); |
313 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
104 compptr->height_in_blocks = (JDIMENSION) |
314 compptr->height_in_blocks = (JDIMENSION) |
105 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, |
315 jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, |
106 (long) (cinfo->max_v_samp_factor * DCTSIZE)); |
316 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
107 /* Size in samples */ |
317 /* Size in samples */ |
108 compptr->downsampled_width = (JDIMENSION) |
318 compptr->downsampled_width = (JDIMENSION) |
109 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, |
319 jdiv_round_up((long) cinfo->jpeg_width * |
110 (long) cinfo->max_h_samp_factor); |
320 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), |
|
321 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
111 compptr->downsampled_height = (JDIMENSION) |
322 compptr->downsampled_height = (JDIMENSION) |
112 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, |
323 jdiv_round_up((long) cinfo->jpeg_height * |
113 (long) cinfo->max_v_samp_factor); |
324 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), |
|
325 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
114 /* Mark component needed (this flag isn't actually used for compression) */ |
326 /* Mark component needed (this flag isn't actually used for compression) */ |
115 compptr->component_needed = TRUE; |
327 compptr->component_needed = TRUE; |
116 } |
328 } |
117 |
329 |
118 /* Compute number of fully interleaved MCU rows (number of times that |
330 /* Compute number of fully interleaved MCU rows (number of times that |
119 * main controller will call coefficient controller). |
331 * main controller will call coefficient controller). |
120 */ |
332 */ |
121 cinfo->total_iMCU_rows = (JDIMENSION) |
333 cinfo->total_iMCU_rows = (JDIMENSION) |
122 jdiv_round_up((long) cinfo->image_height, |
334 jdiv_round_up((long) cinfo->jpeg_height, |
123 (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
335 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
124 } |
336 } |
125 |
337 |
126 |
338 |
127 #ifdef C_MULTISCAN_FILES_SUPPORTED |
339 #ifdef C_MULTISCAN_FILES_SUPPORTED |
128 |
340 |
345 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
593 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
346 MAX_COMPS_IN_SCAN); |
594 MAX_COMPS_IN_SCAN); |
347 |
595 |
348 /* Overall image size in MCUs */ |
596 /* Overall image size in MCUs */ |
349 cinfo->MCUs_per_row = (JDIMENSION) |
597 cinfo->MCUs_per_row = (JDIMENSION) |
350 jdiv_round_up((long) cinfo->image_width, |
598 jdiv_round_up((long) cinfo->jpeg_width, |
351 (long) (cinfo->max_h_samp_factor*DCTSIZE)); |
599 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
352 cinfo->MCU_rows_in_scan = (JDIMENSION) |
600 cinfo->MCU_rows_in_scan = (JDIMENSION) |
353 jdiv_round_up((long) cinfo->image_height, |
601 jdiv_round_up((long) cinfo->jpeg_height, |
354 (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
602 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
355 |
603 |
356 cinfo->blocks_in_MCU = 0; |
604 cinfo->blocks_in_MCU = 0; |
357 |
605 |
358 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
606 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
359 compptr = cinfo->cur_comp_info[ci]; |
607 compptr = cinfo->cur_comp_info[ci]; |
360 /* Sampling factors give # of blocks of component in each MCU */ |
608 /* Sampling factors give # of blocks of component in each MCU */ |
361 compptr->MCU_width = compptr->h_samp_factor; |
609 compptr->MCU_width = compptr->h_samp_factor; |
362 compptr->MCU_height = compptr->v_samp_factor; |
610 compptr->MCU_height = compptr->v_samp_factor; |
363 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
611 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
364 compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; |
612 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; |
365 /* Figure number of non-dummy blocks in last MCU column & row */ |
613 /* Figure number of non-dummy blocks in last MCU column & row */ |
366 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); |
614 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); |
367 if (tmp == 0) tmp = compptr->MCU_width; |
615 if (tmp == 0) tmp = compptr->MCU_width; |
368 compptr->last_col_width = tmp; |
616 compptr->last_col_width = tmp; |
369 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); |
617 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); |