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