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