/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jcparam.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jcparam.c |
3 | | * |
4 | | * Copyright (C) 1991-1998, Thomas G. Lane. |
5 | | * Modified 2003-2019 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 optional default-setting code for the JPEG compressor. |
10 | | * Applications do not have to use this file, but those that don't use it |
11 | | * must know a lot more about the innards of the JPEG code. |
12 | | */ |
13 | | |
14 | | #define JPEG_INTERNALS |
15 | | #include "jinclude.h" |
16 | | #include "jpeglib.h" |
17 | | |
18 | | |
19 | | /* |
20 | | * Quantization table setup routines |
21 | | */ |
22 | | |
23 | | GLOBAL(void) |
24 | | jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, |
25 | | const unsigned int *basic_table, |
26 | | int scale_factor, boolean force_baseline) |
27 | | /* Define a quantization table equal to the basic_table times |
28 | | * a scale factor (given as a percentage). |
29 | | * If force_baseline is TRUE, the computed quantization table entries |
30 | | * are limited to 1..255 for JPEG baseline compatibility. |
31 | | */ |
32 | 0 | { |
33 | 0 | JQUANT_TBL ** qtblptr; |
34 | 0 | int i; |
35 | 0 | long temp; |
36 | | |
37 | | /* Safety check to ensure start_compress not called yet. */ |
38 | 0 | if (cinfo->global_state != CSTATE_START) |
39 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
40 | |
|
41 | 0 | if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS) |
42 | 0 | ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl); |
43 | |
|
44 | 0 | qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; |
45 | |
|
46 | 0 | if (*qtblptr == NULL) |
47 | 0 | *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); |
48 | |
|
49 | 0 | for (i = 0; i < DCTSIZE2; i++) { |
50 | 0 | temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; |
51 | | /* limit the values to the valid range */ |
52 | 0 | if (temp <= 0L) temp = 1L; |
53 | 0 | if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ |
54 | 0 | if (force_baseline && temp > 255L) |
55 | 0 | temp = 255L; /* limit to baseline range if requested */ |
56 | 0 | (*qtblptr)->quantval[i] = (UINT16) temp; |
57 | 0 | } |
58 | | |
59 | | /* Initialize sent_table FALSE so table will be written to JPEG file. */ |
60 | 0 | (*qtblptr)->sent_table = FALSE; |
61 | 0 | } |
62 | | |
63 | | |
64 | | /* These are the sample quantization tables given in JPEG spec section K.1. |
65 | | * The spec says that the values given produce "good" quality, and |
66 | | * when divided by 2, "very good" quality. |
67 | | */ |
68 | | static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = { |
69 | | 16, 11, 10, 16, 24, 40, 51, 61, |
70 | | 12, 12, 14, 19, 26, 58, 60, 55, |
71 | | 14, 13, 16, 24, 40, 57, 69, 56, |
72 | | 14, 17, 22, 29, 51, 87, 80, 62, |
73 | | 18, 22, 37, 56, 68, 109, 103, 77, |
74 | | 24, 35, 55, 64, 81, 104, 113, 92, |
75 | | 49, 64, 78, 87, 103, 121, 120, 101, |
76 | | 72, 92, 95, 98, 112, 100, 103, 99 |
77 | | }; |
78 | | static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = { |
79 | | 17, 18, 24, 47, 99, 99, 99, 99, |
80 | | 18, 21, 26, 66, 99, 99, 99, 99, |
81 | | 24, 26, 56, 99, 99, 99, 99, 99, |
82 | | 47, 66, 99, 99, 99, 99, 99, 99, |
83 | | 99, 99, 99, 99, 99, 99, 99, 99, |
84 | | 99, 99, 99, 99, 99, 99, 99, 99, |
85 | | 99, 99, 99, 99, 99, 99, 99, 99, |
86 | | 99, 99, 99, 99, 99, 99, 99, 99 |
87 | | }; |
88 | | |
89 | | |
90 | | GLOBAL(void) |
91 | | jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline) |
92 | | /* Set or change the 'quality' (quantization) setting, using default tables |
93 | | * and straight percentage-scaling quality scales. |
94 | | * This entry point allows different scalings for luminance and chrominance. |
95 | | */ |
96 | 0 | { |
97 | | /* Set up two quantization tables using the specified scaling */ |
98 | 0 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, |
99 | 0 | cinfo->q_scale_factor[0], force_baseline); |
100 | 0 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, |
101 | 0 | cinfo->q_scale_factor[1], force_baseline); |
102 | 0 | } |
103 | | |
104 | | |
105 | | GLOBAL(void) |
106 | | jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor, |
107 | | boolean force_baseline) |
108 | | /* Set or change the 'quality' (quantization) setting, using default tables |
109 | | * and a straight percentage-scaling quality scale. In most cases it's better |
110 | | * to use jpeg_set_quality (below); this entry point is provided for |
111 | | * applications that insist on a linear percentage scaling. |
112 | | */ |
113 | 0 | { |
114 | | /* Set up two quantization tables using the specified scaling */ |
115 | 0 | jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, |
116 | 0 | scale_factor, force_baseline); |
117 | 0 | jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, |
118 | 0 | scale_factor, force_baseline); |
119 | 0 | } |
120 | | |
121 | | |
122 | | GLOBAL(int) |
123 | | jpeg_quality_scaling (int quality) |
124 | | /* Convert a user-specified quality rating to a percentage scaling factor |
125 | | * for an underlying quantization table, using our recommended scaling curve. |
126 | | * The input 'quality' factor should be 0 (terrible) to 100 (very good). |
127 | | */ |
128 | 0 | { |
129 | | /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */ |
130 | 0 | if (quality <= 0) quality = 1; |
131 | 0 | if (quality > 100) quality = 100; |
132 | | |
133 | | /* The basic table is used as-is (scaling 100) for a quality of 50. |
134 | | * Qualities 50..100 are converted to scaling percentage 200 - 2*Q; |
135 | | * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table |
136 | | * to make all the table entries 1 (hence, minimum quantization loss). |
137 | | * Qualities 1..50 are converted to scaling percentage 5000/Q. |
138 | | */ |
139 | 0 | if (quality < 50) |
140 | 0 | quality = 5000 / quality; |
141 | 0 | else |
142 | 0 | quality = 200 - quality*2; |
143 | |
|
144 | 0 | return quality; |
145 | 0 | } |
146 | | |
147 | | |
148 | | GLOBAL(void) |
149 | | jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline) |
150 | | /* Set or change the 'quality' (quantization) setting, using default tables. |
151 | | * This is the standard quality-adjusting entry point for typical user |
152 | | * interfaces; only those who want detailed control over quantization tables |
153 | | * would use the preceding routines directly. |
154 | | */ |
155 | 0 | { |
156 | | /* Convert user 0-100 rating to percentage scaling */ |
157 | 0 | quality = jpeg_quality_scaling(quality); |
158 | | |
159 | | /* Set up standard quality tables */ |
160 | 0 | jpeg_set_linear_quality(cinfo, quality, force_baseline); |
161 | 0 | } |
162 | | |
163 | | |
164 | | /* |
165 | | * Reset standard Huffman tables |
166 | | */ |
167 | | |
168 | | LOCAL(void) |
169 | | std_huff_tables (j_compress_ptr cinfo) |
170 | 0 | { |
171 | 0 | if (cinfo->dc_huff_tbl_ptrs[0] != NULL) |
172 | 0 | (void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 0); |
173 | |
|
174 | 0 | if (cinfo->ac_huff_tbl_ptrs[0] != NULL) |
175 | 0 | (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 0); |
176 | |
|
177 | 0 | if (cinfo->dc_huff_tbl_ptrs[1] != NULL) |
178 | 0 | (void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 1); |
179 | |
|
180 | 0 | if (cinfo->ac_huff_tbl_ptrs[1] != NULL) |
181 | 0 | (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 1); |
182 | 0 | } |
183 | | |
184 | | |
185 | | /* |
186 | | * Default parameter setup for compression. |
187 | | * |
188 | | * Applications that don't choose to use this routine must do their |
189 | | * own setup of all these parameters. Alternately, you can call this |
190 | | * to establish defaults and then alter parameters selectively. This |
191 | | * is the recommended approach since, if we add any new parameters, |
192 | | * your code will still work (they'll be set to reasonable defaults). |
193 | | */ |
194 | | |
195 | | GLOBAL(void) |
196 | | jpeg_set_defaults (j_compress_ptr cinfo) |
197 | 0 | { |
198 | 0 | int i; |
199 | | |
200 | | /* Safety check to ensure start_compress not called yet. */ |
201 | 0 | if (cinfo->global_state != CSTATE_START) |
202 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
203 | | |
204 | | /* Allocate comp_info array large enough for maximum component count. |
205 | | * Array is made permanent in case application wants to compress |
206 | | * multiple images at same param settings. |
207 | | */ |
208 | 0 | if (cinfo->comp_info == NULL) |
209 | 0 | cinfo->comp_info = (jpeg_component_info *) |
210 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
211 | 0 | MAX_COMPONENTS * SIZEOF(jpeg_component_info)); |
212 | | |
213 | | /* Initialize everything not dependent on the color space */ |
214 | |
|
215 | 0 | cinfo->scale_num = 1; /* 1:1 scaling */ |
216 | 0 | cinfo->scale_denom = 1; |
217 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
218 | | /* Set up two quantization tables using default quality of 75 */ |
219 | 0 | jpeg_set_quality(cinfo, 75, TRUE); |
220 | | /* Reset standard Huffman tables */ |
221 | 0 | std_huff_tables(cinfo); |
222 | | |
223 | | /* Initialize default arithmetic coding conditioning */ |
224 | 0 | for (i = 0; i < NUM_ARITH_TBLS; i++) { |
225 | 0 | cinfo->arith_dc_L[i] = 0; |
226 | 0 | cinfo->arith_dc_U[i] = 1; |
227 | 0 | cinfo->arith_ac_K[i] = 5; |
228 | 0 | } |
229 | | |
230 | | /* Default is no multiple-scan output */ |
231 | 0 | cinfo->scan_info = NULL; |
232 | 0 | cinfo->num_scans = 0; |
233 | | |
234 | | /* Expect normal source image, not raw downsampled data */ |
235 | 0 | cinfo->raw_data_in = FALSE; |
236 | | |
237 | | /* The standard Huffman tables are only valid for 8-bit data precision. |
238 | | * If the precision is higher, use arithmetic coding. |
239 | | * (Alternatively, using Huffman coding would be possible with forcing |
240 | | * optimization on so that usable tables will be computed, or by |
241 | | * supplying default tables that are valid for the desired precision.) |
242 | | * Otherwise, use Huffman coding by default. |
243 | | */ |
244 | 0 | cinfo->arith_code = cinfo->data_precision > 8 ? TRUE : FALSE; |
245 | | |
246 | | /* By default, don't do extra passes to optimize entropy coding */ |
247 | 0 | cinfo->optimize_coding = FALSE; |
248 | | |
249 | | /* By default, use the simpler non-cosited sampling alignment */ |
250 | 0 | cinfo->CCIR601_sampling = FALSE; |
251 | | |
252 | | /* By default, apply fancy downsampling */ |
253 | 0 | cinfo->do_fancy_downsampling = TRUE; |
254 | | |
255 | | /* No input smoothing */ |
256 | 0 | cinfo->smoothing_factor = 0; |
257 | | |
258 | | /* DCT algorithm preference */ |
259 | 0 | cinfo->dct_method = JDCT_DEFAULT; |
260 | | |
261 | | /* No restart markers */ |
262 | 0 | cinfo->restart_interval = 0; |
263 | 0 | cinfo->restart_in_rows = 0; |
264 | | |
265 | | /* Fill in default JFIF marker parameters. Note that whether the marker |
266 | | * will actually be written is determined by jpeg_set_colorspace. |
267 | | * |
268 | | * By default, the library emits JFIF version code 1.01. |
269 | | * An application that wants to emit JFIF 1.02 extension markers should set |
270 | | * JFIF_minor_version to 2. We could probably get away with just defaulting |
271 | | * to 1.02, but there may still be some decoders in use that will complain |
272 | | * about that; saying 1.01 should minimize compatibility problems. |
273 | | * |
274 | | * For wide gamut colorspaces (BG_RGB and BG_YCC), the major version will be |
275 | | * overridden by jpeg_set_colorspace and set to 2. |
276 | | */ |
277 | 0 | cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */ |
278 | 0 | cinfo->JFIF_minor_version = 1; |
279 | 0 | cinfo->density_unit = 0; /* Pixel size is unknown by default */ |
280 | 0 | cinfo->X_density = 1; /* Pixel aspect ratio is square by default */ |
281 | 0 | cinfo->Y_density = 1; |
282 | | |
283 | | /* No color transform */ |
284 | 0 | cinfo->color_transform = JCT_NONE; |
285 | | |
286 | | /* Choose JPEG colorspace based on input space, set defaults accordingly */ |
287 | |
|
288 | 0 | jpeg_default_colorspace(cinfo); |
289 | 0 | } |
290 | | |
291 | | |
292 | | /* |
293 | | * Select an appropriate JPEG colorspace for in_color_space. |
294 | | */ |
295 | | |
296 | | GLOBAL(void) |
297 | | jpeg_default_colorspace (j_compress_ptr cinfo) |
298 | 0 | { |
299 | 0 | switch (cinfo->in_color_space) { |
300 | 0 | case JCS_UNKNOWN: |
301 | 0 | jpeg_set_colorspace(cinfo, JCS_UNKNOWN); |
302 | 0 | break; |
303 | 0 | case JCS_GRAYSCALE: |
304 | 0 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
305 | 0 | break; |
306 | 0 | case JCS_RGB: |
307 | 0 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
308 | 0 | break; |
309 | 0 | case JCS_YCbCr: |
310 | 0 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
311 | 0 | break; |
312 | 0 | case JCS_CMYK: |
313 | 0 | jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */ |
314 | 0 | break; |
315 | 0 | case JCS_YCCK: |
316 | 0 | jpeg_set_colorspace(cinfo, JCS_YCCK); |
317 | 0 | break; |
318 | 0 | case JCS_BG_RGB: |
319 | | /* No translation for now -- conversion to BG_YCC not yet supportet */ |
320 | 0 | jpeg_set_colorspace(cinfo, JCS_BG_RGB); |
321 | 0 | break; |
322 | 0 | case JCS_BG_YCC: |
323 | 0 | jpeg_set_colorspace(cinfo, JCS_BG_YCC); |
324 | 0 | break; |
325 | 0 | default: |
326 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
327 | 0 | } |
328 | 0 | } |
329 | | |
330 | | |
331 | | /* |
332 | | * Set the JPEG colorspace, and choose colorspace-dependent default values. |
333 | | */ |
334 | | |
335 | | GLOBAL(void) |
336 | | jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace) |
337 | 0 | { |
338 | 0 | jpeg_component_info * compptr; |
339 | 0 | int ci; |
340 | |
|
341 | 0 | #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \ |
342 | 0 | (compptr = &cinfo->comp_info[index], \ |
343 | 0 | compptr->component_id = (id), \ |
344 | 0 | compptr->h_samp_factor = (hsamp), \ |
345 | 0 | compptr->v_samp_factor = (vsamp), \ |
346 | 0 | compptr->quant_tbl_no = (quant), \ |
347 | 0 | compptr->dc_tbl_no = (dctbl), \ |
348 | 0 | compptr->ac_tbl_no = (actbl) ) |
349 | | |
350 | | /* Safety check to ensure start_compress not called yet. */ |
351 | 0 | if (cinfo->global_state != CSTATE_START) |
352 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
353 | | |
354 | | /* For all colorspaces, we use Q and Huff tables 0 for luminance components, |
355 | | * tables 1 for chrominance components. |
356 | | */ |
357 | |
|
358 | 0 | cinfo->jpeg_color_space = colorspace; |
359 | |
|
360 | 0 | cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */ |
361 | 0 | cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */ |
362 | |
|
363 | 0 | switch (colorspace) { |
364 | 0 | case JCS_UNKNOWN: |
365 | 0 | cinfo->num_components = cinfo->input_components; |
366 | 0 | if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS) |
367 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
368 | 0 | MAX_COMPONENTS); |
369 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
370 | 0 | SET_COMP(ci, ci, 1,1, 0, 0,0); |
371 | 0 | } |
372 | 0 | break; |
373 | 0 | case JCS_GRAYSCALE: |
374 | 0 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
375 | 0 | cinfo->num_components = 1; |
376 | | /* JFIF specifies component ID 1 */ |
377 | 0 | SET_COMP(0, 0x01, 1,1, 0, 0,0); |
378 | 0 | break; |
379 | 0 | case JCS_RGB: |
380 | 0 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */ |
381 | 0 | cinfo->num_components = 3; |
382 | 0 | SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, |
383 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0, |
384 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0); |
385 | 0 | SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0); |
386 | 0 | SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, |
387 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0, |
388 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0); |
389 | 0 | break; |
390 | 0 | case JCS_YCbCr: |
391 | 0 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
392 | 0 | cinfo->num_components = 3; |
393 | | /* JFIF specifies component IDs 1,2,3 */ |
394 | | /* We default to 2x2 subsamples of chrominance */ |
395 | 0 | SET_COMP(0, 0x01, 2,2, 0, 0,0); |
396 | 0 | SET_COMP(1, 0x02, 1,1, 1, 1,1); |
397 | 0 | SET_COMP(2, 0x03, 1,1, 1, 1,1); |
398 | 0 | break; |
399 | 0 | case JCS_CMYK: |
400 | 0 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */ |
401 | 0 | cinfo->num_components = 4; |
402 | 0 | SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0); |
403 | 0 | SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0); |
404 | 0 | SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0); |
405 | 0 | SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0); |
406 | 0 | break; |
407 | 0 | case JCS_YCCK: |
408 | 0 | cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */ |
409 | 0 | cinfo->num_components = 4; |
410 | 0 | SET_COMP(0, 0x01, 2,2, 0, 0,0); |
411 | 0 | SET_COMP(1, 0x02, 1,1, 1, 1,1); |
412 | 0 | SET_COMP(2, 0x03, 1,1, 1, 1,1); |
413 | 0 | SET_COMP(3, 0x04, 2,2, 0, 0,0); |
414 | 0 | break; |
415 | 0 | case JCS_BG_RGB: |
416 | 0 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
417 | 0 | cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */ |
418 | 0 | cinfo->num_components = 3; |
419 | | /* Add offset 0x20 to the normal R/G/B component IDs */ |
420 | 0 | SET_COMP(0, 0x72 /* 'r' */, 1,1, 0, |
421 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0, |
422 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0); |
423 | 0 | SET_COMP(1, 0x67 /* 'g' */, 1,1, 0, 0,0); |
424 | 0 | SET_COMP(2, 0x62 /* 'b' */, 1,1, 0, |
425 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0, |
426 | 0 | cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0); |
427 | 0 | break; |
428 | 0 | case JCS_BG_YCC: |
429 | 0 | cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ |
430 | 0 | cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */ |
431 | 0 | cinfo->num_components = 3; |
432 | | /* Add offset 0x20 to the normal Cb/Cr component IDs */ |
433 | | /* We default to 2x2 subsamples of chrominance */ |
434 | 0 | SET_COMP(0, 0x01, 2,2, 0, 0,0); |
435 | 0 | SET_COMP(1, 0x22, 1,1, 1, 1,1); |
436 | 0 | SET_COMP(2, 0x23, 1,1, 1, 1,1); |
437 | 0 | break; |
438 | 0 | default: |
439 | 0 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
440 | 0 | } |
441 | 0 | } |
442 | | |
443 | | |
444 | | #ifdef C_PROGRESSIVE_SUPPORTED |
445 | | |
446 | | LOCAL(jpeg_scan_info *) |
447 | | fill_a_scan (jpeg_scan_info * scanptr, int ci, |
448 | | int Ss, int Se, int Ah, int Al) |
449 | | /* Support routine: generate one scan for specified component */ |
450 | 0 | { |
451 | 0 | scanptr->comps_in_scan = 1; |
452 | 0 | scanptr->component_index[0] = ci; |
453 | 0 | scanptr->Ss = Ss; |
454 | 0 | scanptr->Se = Se; |
455 | 0 | scanptr->Ah = Ah; |
456 | 0 | scanptr->Al = Al; |
457 | 0 | scanptr++; |
458 | 0 | return scanptr; |
459 | 0 | } |
460 | | |
461 | | LOCAL(jpeg_scan_info *) |
462 | | fill_scans (jpeg_scan_info * scanptr, int ncomps, |
463 | | int Ss, int Se, int Ah, int Al) |
464 | | /* Support routine: generate one scan for each component */ |
465 | 0 | { |
466 | 0 | int ci; |
467 | |
|
468 | 0 | for (ci = 0; ci < ncomps; ci++) { |
469 | 0 | scanptr->comps_in_scan = 1; |
470 | 0 | scanptr->component_index[0] = ci; |
471 | 0 | scanptr->Ss = Ss; |
472 | 0 | scanptr->Se = Se; |
473 | 0 | scanptr->Ah = Ah; |
474 | 0 | scanptr->Al = Al; |
475 | 0 | scanptr++; |
476 | 0 | } |
477 | 0 | return scanptr; |
478 | 0 | } |
479 | | |
480 | | LOCAL(jpeg_scan_info *) |
481 | | fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al) |
482 | | /* Support routine: generate interleaved DC scan if possible, else N scans */ |
483 | 0 | { |
484 | 0 | int ci; |
485 | |
|
486 | 0 | if (ncomps <= MAX_COMPS_IN_SCAN) { |
487 | | /* Single interleaved DC scan */ |
488 | 0 | scanptr->comps_in_scan = ncomps; |
489 | 0 | for (ci = 0; ci < ncomps; ci++) |
490 | 0 | scanptr->component_index[ci] = ci; |
491 | 0 | scanptr->Ss = scanptr->Se = 0; |
492 | 0 | scanptr->Ah = Ah; |
493 | 0 | scanptr->Al = Al; |
494 | 0 | scanptr++; |
495 | 0 | } else { |
496 | | /* Noninterleaved DC scan for each component */ |
497 | 0 | scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al); |
498 | 0 | } |
499 | 0 | return scanptr; |
500 | 0 | } |
501 | | |
502 | | |
503 | | /* |
504 | | * Create a recommended progressive-JPEG script. |
505 | | * cinfo->num_components and cinfo->jpeg_color_space must be correct. |
506 | | */ |
507 | | |
508 | | GLOBAL(void) |
509 | | jpeg_simple_progression (j_compress_ptr cinfo) |
510 | 0 | { |
511 | 0 | int ncomps = cinfo->num_components; |
512 | 0 | int nscans; |
513 | 0 | jpeg_scan_info * scanptr; |
514 | | |
515 | | /* Safety check to ensure start_compress not called yet. */ |
516 | 0 | if (cinfo->global_state != CSTATE_START) |
517 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
518 | | |
519 | | /* Figure space needed for script. Calculation must match code below! */ |
520 | 0 | if (ncomps == 3 && |
521 | 0 | (cinfo->jpeg_color_space == JCS_YCbCr || |
522 | 0 | cinfo->jpeg_color_space == JCS_BG_YCC)) { |
523 | | /* Custom script for YCC color images. */ |
524 | 0 | nscans = 10; |
525 | 0 | } else { |
526 | | /* All-purpose script for other color spaces. */ |
527 | 0 | if (ncomps > MAX_COMPS_IN_SCAN) |
528 | 0 | nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */ |
529 | 0 | else |
530 | 0 | nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */ |
531 | 0 | } |
532 | | |
533 | | /* Allocate space for script. |
534 | | * We need to put it in the permanent pool in case the application performs |
535 | | * multiple compressions without changing the settings. To avoid a memory |
536 | | * leak if jpeg_simple_progression is called repeatedly for the same JPEG |
537 | | * object, we try to re-use previously allocated space, and we allocate |
538 | | * enough space to handle YCC even if initially asked for grayscale. |
539 | | */ |
540 | 0 | if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) { |
541 | 0 | cinfo->script_space_size = MAX(nscans, 10); |
542 | 0 | cinfo->script_space = (jpeg_scan_info *) |
543 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
544 | 0 | cinfo->script_space_size * SIZEOF(jpeg_scan_info)); |
545 | 0 | } |
546 | 0 | scanptr = cinfo->script_space; |
547 | 0 | cinfo->scan_info = scanptr; |
548 | 0 | cinfo->num_scans = nscans; |
549 | |
|
550 | 0 | if (ncomps == 3 && |
551 | 0 | (cinfo->jpeg_color_space == JCS_YCbCr || |
552 | 0 | cinfo->jpeg_color_space == JCS_BG_YCC)) { |
553 | | /* Custom script for YCC color images. */ |
554 | | /* Initial DC scan */ |
555 | 0 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); |
556 | | /* Initial AC scan: get some luma data out in a hurry */ |
557 | 0 | scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2); |
558 | | /* Chroma data is too small to be worth expending many scans on */ |
559 | 0 | scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1); |
560 | 0 | scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1); |
561 | | /* Complete spectral selection for luma AC */ |
562 | 0 | scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2); |
563 | | /* Refine next bit of luma AC */ |
564 | 0 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1); |
565 | | /* Finish DC successive approximation */ |
566 | 0 | scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); |
567 | | /* Finish AC successive approximation */ |
568 | 0 | scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0); |
569 | 0 | scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0); |
570 | | /* Luma bottom bit comes last since it's usually largest scan */ |
571 | 0 | scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0); |
572 | 0 | } else { |
573 | | /* All-purpose script for other color spaces. */ |
574 | | /* Successive approximation first pass */ |
575 | 0 | scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); |
576 | 0 | scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2); |
577 | 0 | scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2); |
578 | | /* Successive approximation second pass */ |
579 | 0 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1); |
580 | | /* Successive approximation final pass */ |
581 | 0 | scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); |
582 | 0 | scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0); |
583 | 0 | } |
584 | 0 | } |
585 | | |
586 | | #endif /* C_PROGRESSIVE_SUPPORTED */ |