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