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