/src/libjpeg-turbo.main/jcmaster.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * jcmaster.c  | 
3  |  |  *  | 
4  |  |  * This file was part of the Independent JPEG Group's software:  | 
5  |  |  * Copyright (C) 1991-1997, Thomas G. Lane.  | 
6  |  |  * Modified 2003-2010 by Guido Vollbeding.  | 
7  |  |  * Lossless JPEG Modifications:  | 
8  |  |  * Copyright (C) 1999, Ken Murchison.  | 
9  |  |  * libjpeg-turbo Modifications:  | 
10  |  |  * Copyright (C) 2010, 2016, 2018, 2022-2023, D. R. Commander.  | 
11  |  |  * For conditions of distribution and use, see the accompanying README.ijg  | 
12  |  |  * file.  | 
13  |  |  *  | 
14  |  |  * This file contains master control logic for the JPEG compressor.  | 
15  |  |  * These routines are concerned with parameter validation, initial setup,  | 
16  |  |  * and inter-pass control (determining the number of passes and the work  | 
17  |  |  * to be done in each pass).  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #define JPEG_INTERNALS  | 
21  |  | #include "jinclude.h"  | 
22  |  | #include "jpeglib.h"  | 
23  |  | #include "jpegapicomp.h"  | 
24  |  | #include "jcmaster.h"  | 
25  |  |  | 
26  |  |  | 
27  |  | /*  | 
28  |  |  * Support routines that do various essential calculations.  | 
29  |  |  */  | 
30  |  |  | 
31  |  | #if JPEG_LIB_VERSION >= 70  | 
32  |  | /*  | 
33  |  |  * Compute JPEG image dimensions and related values.  | 
34  |  |  * NOTE: this is exported for possible use by application.  | 
35  |  |  * Hence it mustn't do anything that can't be done twice.  | 
36  |  |  */  | 
37  |  |  | 
38  |  | GLOBAL(void)  | 
39  |  | jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo)  | 
40  |  | /* Do computations that are needed before master selection phase */  | 
41  |  | { | 
42  |  |   int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;  | 
43  |  |  | 
44  |  |   /* Hardwire it to "no scaling" */  | 
45  |  |   cinfo->jpeg_width = cinfo->image_width;  | 
46  |  |   cinfo->jpeg_height = cinfo->image_height;  | 
47  |  |   cinfo->min_DCT_h_scaled_size = data_unit;  | 
48  |  |   cinfo->min_DCT_v_scaled_size = data_unit;  | 
49  |  | }  | 
50  |  | #endif  | 
51  |  |  | 
52  |  |  | 
53  |  | LOCAL(void)  | 
54  |  | initial_setup(j_compress_ptr cinfo, boolean transcode_only)  | 
55  |  | /* Do computations that are needed before master selection phase */  | 
56  | 7.03k  | { | 
57  | 7.03k  |   int ci;  | 
58  | 7.03k  |   jpeg_component_info *compptr;  | 
59  | 7.03k  |   long samplesperrow;  | 
60  | 7.03k  |   JDIMENSION jd_samplesperrow;  | 
61  | 7.03k  |   int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;  | 
62  |  |  | 
63  |  | #if JPEG_LIB_VERSION >= 70  | 
64  |  | #if JPEG_LIB_VERSION >= 80  | 
65  |  |   if (!transcode_only)  | 
66  |  | #endif  | 
67  |  |     jpeg_calc_jpeg_dimensions(cinfo);  | 
68  |  | #endif  | 
69  |  |  | 
70  |  |   /* Sanity check on image dimensions */  | 
71  | 7.03k  |   if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||  | 
72  | 7.03k  |       cinfo->num_components <= 0 || cinfo->input_components <= 0)  | 
73  | 0  |     ERREXIT(cinfo, JERR_EMPTY_IMAGE);  | 
74  |  |  | 
75  |  |   /* Make sure image isn't bigger than I can handle */  | 
76  | 7.03k  |   if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||  | 
77  | 7.03k  |       (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)  | 
78  | 0  |     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);  | 
79  |  |  | 
80  |  |   /* Width of an input scanline must be representable as JDIMENSION. */  | 
81  | 7.03k  |   samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;  | 
82  | 7.03k  |   jd_samplesperrow = (JDIMENSION)samplesperrow;  | 
83  | 7.03k  |   if ((long)jd_samplesperrow != samplesperrow)  | 
84  | 0  |     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);  | 
85  |  |  | 
86  | 7.03k  | #ifdef C_LOSSLESS_SUPPORTED  | 
87  | 7.03k  |   if (cinfo->data_precision != 8 && cinfo->data_precision != 12 &&  | 
88  | 7.03k  |       cinfo->data_precision != 16)  | 
89  |  | #else  | 
90  |  |   if (cinfo->data_precision != 8 && cinfo->data_precision != 12)  | 
91  |  | #endif  | 
92  | 0  |     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);  | 
93  |  |  | 
94  |  |   /* Check that number of components won't exceed internal array sizes */  | 
95  | 7.03k  |   if (cinfo->num_components > MAX_COMPONENTS)  | 
96  | 0  |     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,  | 
97  | 7.03k  |              MAX_COMPONENTS);  | 
98  |  |  | 
99  |  |   /* Compute maximum sampling factors; check factor validity */  | 
100  | 7.03k  |   cinfo->max_h_samp_factor = 1;  | 
101  | 7.03k  |   cinfo->max_v_samp_factor = 1;  | 
102  | 16.2k  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
103  | 9.23k  |        ci++, compptr++) { | 
104  | 9.23k  |     if (compptr->h_samp_factor <= 0 ||  | 
105  | 9.23k  |         compptr->h_samp_factor > MAX_SAMP_FACTOR ||  | 
106  | 9.23k  |         compptr->v_samp_factor <= 0 ||  | 
107  | 9.23k  |         compptr->v_samp_factor > MAX_SAMP_FACTOR)  | 
108  | 0  |       ERREXIT(cinfo, JERR_BAD_SAMPLING);  | 
109  | 9.23k  |     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,  | 
110  | 9.23k  |                                    compptr->h_samp_factor);  | 
111  | 9.23k  |     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,  | 
112  | 9.23k  |                                    compptr->v_samp_factor);  | 
113  | 9.23k  |   }  | 
114  |  |  | 
115  |  |   /* Compute dimensions of components */  | 
116  | 16.2k  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
117  | 9.23k  |        ci++, compptr++) { | 
118  |  |     /* Fill in the correct component_index value; don't rely on application */  | 
119  | 9.23k  |     compptr->component_index = ci;  | 
120  |  |     /* For compression, we never do DCT scaling. */  | 
121  |  | #if JPEG_LIB_VERSION >= 70  | 
122  |  |     compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit;  | 
123  |  | #else  | 
124  | 9.23k  |     compptr->DCT_scaled_size = data_unit;  | 
125  | 9.23k  | #endif  | 
126  |  |     /* Size in data units */  | 
127  | 9.23k  |     compptr->width_in_blocks = (JDIMENSION)  | 
128  | 9.23k  |       jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,  | 
129  | 9.23k  |                     (long)(cinfo->max_h_samp_factor * data_unit));  | 
130  | 9.23k  |     compptr->height_in_blocks = (JDIMENSION)  | 
131  | 9.23k  |       jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,  | 
132  | 9.23k  |                     (long)(cinfo->max_v_samp_factor * data_unit));  | 
133  |  |     /* Size in samples */  | 
134  | 9.23k  |     compptr->downsampled_width = (JDIMENSION)  | 
135  | 9.23k  |       jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,  | 
136  | 9.23k  |                     (long)cinfo->max_h_samp_factor);  | 
137  | 9.23k  |     compptr->downsampled_height = (JDIMENSION)  | 
138  | 9.23k  |       jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,  | 
139  | 9.23k  |                     (long)cinfo->max_v_samp_factor);  | 
140  |  |     /* Mark component needed (this flag isn't actually used for compression) */  | 
141  | 9.23k  |     compptr->component_needed = TRUE;  | 
142  | 9.23k  |   }  | 
143  |  |  | 
144  |  |   /* Compute number of fully interleaved MCU rows (number of times that  | 
145  |  |    * main controller will call coefficient or difference controller).  | 
146  |  |    */  | 
147  | 7.03k  |   cinfo->total_iMCU_rows = (JDIMENSION)  | 
148  | 7.03k  |     jdiv_round_up((long)cinfo->_jpeg_height,  | 
149  | 7.03k  |                   (long)(cinfo->max_v_samp_factor * data_unit));  | 
150  | 7.03k  | }  | 
151  |  |  | 
152  |  |  | 
153  |  | #if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED)  | 
154  |  | #define NEED_SCAN_SCRIPT  | 
155  |  | #endif  | 
156  |  |  | 
157  |  | #ifdef NEED_SCAN_SCRIPT  | 
158  |  |  | 
159  |  | LOCAL(void)  | 
160  |  | validate_script(j_compress_ptr cinfo)  | 
161  |  | /* Verify that the scan script in cinfo->scan_info[] is valid; also  | 
162  |  |  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.  | 
163  |  |  */  | 
164  | 5.41k  | { | 
165  | 5.41k  |   const jpeg_scan_info *scanptr;  | 
166  | 5.41k  |   int scanno, ncomps, ci, coefi, thisi;  | 
167  | 5.41k  |   int Ss, Se, Ah, Al;  | 
168  | 5.41k  |   boolean component_sent[MAX_COMPONENTS];  | 
169  | 5.41k  | #ifdef C_PROGRESSIVE_SUPPORTED  | 
170  | 5.41k  |   int *last_bitpos_ptr;  | 
171  | 5.41k  |   int last_bitpos[MAX_COMPONENTS][DCTSIZE2];  | 
172  |  |   /* -1 until that coefficient has been seen; then last Al for it */  | 
173  | 5.41k  | #endif  | 
174  |  |  | 
175  | 5.41k  |   if (cinfo->num_scans <= 0)  | 
176  | 0  |     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);  | 
177  |  |  | 
178  |  | #ifndef C_MULTISCAN_FILES_SUPPORTED  | 
179  |  |   if (cinfo->num_scans > 1)  | 
180  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
181  |  | #endif  | 
182  |  |  | 
183  | 5.41k  |   scanptr = cinfo->scan_info;  | 
184  | 5.41k  |   if (scanptr->Ss != 0 && scanptr->Se == 0) { | 
185  | 0  | #ifdef C_LOSSLESS_SUPPORTED  | 
186  | 0  |     cinfo->master->lossless = TRUE;  | 
187  | 0  |     cinfo->progressive_mode = FALSE;  | 
188  | 0  |     for (ci = 0; ci < cinfo->num_components; ci++)  | 
189  | 0  |       component_sent[ci] = FALSE;  | 
190  |  | #else  | 
191  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
192  |  | #endif  | 
193  | 0  |   }  | 
194  |  |   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;  | 
195  |  |    * for progressive JPEG, no scan can have this.  | 
196  |  |    */  | 
197  | 5.41k  |   else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) { | 
198  | 5.41k  | #ifdef C_PROGRESSIVE_SUPPORTED  | 
199  | 5.41k  |     cinfo->progressive_mode = TRUE;  | 
200  | 5.41k  |     cinfo->master->lossless = FALSE;  | 
201  | 5.41k  |     last_bitpos_ptr = &last_bitpos[0][0];  | 
202  | 12.8k  |     for (ci = 0; ci < cinfo->num_components; ci++)  | 
203  | 480k  |       for (coefi = 0; coefi < DCTSIZE2; coefi++)  | 
204  | 473k  |         *last_bitpos_ptr++ = -1;  | 
205  |  | #else  | 
206  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
207  |  | #endif  | 
208  | 5.41k  |   } else { | 
209  | 0  |     cinfo->progressive_mode = cinfo->master->lossless = FALSE;  | 
210  | 0  |     for (ci = 0; ci < cinfo->num_components; ci++)  | 
211  | 0  |       component_sent[ci] = FALSE;  | 
212  | 0  |   }  | 
213  |  |  | 
214  | 42.9k  |   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { | 
215  |  |     /* Validate component indexes */  | 
216  | 37.5k  |     ncomps = scanptr->comps_in_scan;  | 
217  | 37.5k  |     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)  | 
218  | 0  |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);  | 
219  | 79.0k  |     for (ci = 0; ci < ncomps; ci++) { | 
220  | 41.5k  |       thisi = scanptr->component_index[ci];  | 
221  | 41.5k  |       if (thisi < 0 || thisi >= cinfo->num_components)  | 
222  | 0  |         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);  | 
223  |  |       /* Components must appear in SOF order within each scan */  | 
224  | 41.5k  |       if (ci > 0 && thisi <= scanptr->component_index[ci - 1])  | 
225  | 0  |         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);  | 
226  | 41.5k  |     }  | 
227  |  |     /* Validate progression parameters */  | 
228  | 37.5k  |     Ss = scanptr->Ss;  | 
229  | 37.5k  |     Se = scanptr->Se;  | 
230  | 37.5k  |     Ah = scanptr->Ah;  | 
231  | 37.5k  |     Al = scanptr->Al;  | 
232  | 37.5k  |     if (cinfo->progressive_mode) { | 
233  | 37.5k  | #ifdef C_PROGRESSIVE_SUPPORTED  | 
234  |  |       /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah  | 
235  |  |        * and Al, but that seems wrong: the upper bound ought to depend on data  | 
236  |  |        * precision.  Perhaps they really meant 0..N+1 for N-bit precision.  | 
237  |  |        * Here we allow 0..10 for 8-bit data; Al larger than 10 results in  | 
238  |  |        * out-of-range reconstructed DC values during the first DC scan,  | 
239  |  |        * which might cause problems for some decoders.  | 
240  |  |        */  | 
241  | 37.5k  |       int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;  | 
242  |  |  | 
243  | 37.5k  |       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||  | 
244  | 37.5k  |           Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)  | 
245  | 0  |         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
246  | 37.5k  |       if (Ss == 0) { | 
247  | 10.8k  |         if (Se != 0)            /* DC and AC together not OK */  | 
248  | 0  |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
249  | 26.7k  |       } else { | 
250  | 26.7k  |         if (ncomps != 1)        /* AC scans must be for only one component */  | 
251  | 0  |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
252  | 26.7k  |       }  | 
253  | 79.0k  |       for (ci = 0; ci < ncomps; ci++) { | 
254  | 41.5k  |         last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];  | 
255  | 41.5k  |         if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */  | 
256  | 0  |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
257  | 1.36M  |         for (coefi = Ss; coefi <= Se; coefi++) { | 
258  | 1.32M  |           if (last_bitpos_ptr[coefi] < 0) { | 
259  |  |             /* first scan of this coefficient */  | 
260  | 473k  |             if (Ah != 0)  | 
261  | 0  |               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
262  | 849k  |           } else { | 
263  |  |             /* not first scan */  | 
264  | 849k  |             if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)  | 
265  | 0  |               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
266  | 849k  |           }  | 
267  | 1.32M  |           last_bitpos_ptr[coefi] = Al;  | 
268  | 1.32M  |         }  | 
269  | 41.5k  |       }  | 
270  | 37.5k  | #endif  | 
271  | 37.5k  |     } else { | 
272  | 0  | #ifdef C_LOSSLESS_SUPPORTED  | 
273  | 0  |       if (cinfo->master->lossless) { | 
274  |  |         /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that  | 
275  |  |          * seems wrong: the upper bound ought to depend on data precision.  | 
276  |  |          * Perhaps they really meant 0..N-1 for N-bit precision, which is what  | 
277  |  |          * we allow here.  Values greater than or equal to the data precision  | 
278  |  |          * will result in a blank image.  | 
279  |  |          */  | 
280  | 0  |         if (Ss < 1 || Ss > 7 ||         /* predictor selection value */  | 
281  | 0  |             Se != 0 || Ah != 0 ||  | 
282  | 0  |             Al < 0 || Al >= cinfo->data_precision) /* point transform */  | 
283  | 0  |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
284  | 0  |       } else  | 
285  | 0  | #endif  | 
286  | 0  |       { | 
287  |  |         /* For sequential JPEG, all progression parameters must be these: */  | 
288  | 0  |         if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)  | 
289  | 0  |           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);  | 
290  | 0  |       }  | 
291  |  |       /* Make sure components are not sent twice */  | 
292  | 0  |       for (ci = 0; ci < ncomps; ci++) { | 
293  | 0  |         thisi = scanptr->component_index[ci];  | 
294  | 0  |         if (component_sent[thisi])  | 
295  | 0  |           ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);  | 
296  | 0  |         component_sent[thisi] = TRUE;  | 
297  | 0  |       }  | 
298  | 0  |     }  | 
299  | 37.5k  |   }  | 
300  |  |  | 
301  |  |   /* Now verify that everything got sent. */  | 
302  | 5.41k  |   if (cinfo->progressive_mode) { | 
303  | 5.41k  | #ifdef C_PROGRESSIVE_SUPPORTED  | 
304  |  |     /* For progressive mode, we only check that at least some DC data  | 
305  |  |      * got sent for each component; the spec does not require that all bits  | 
306  |  |      * of all coefficients be transmitted.  Would it be wiser to enforce  | 
307  |  |      * transmission of all coefficient bits??  | 
308  |  |      */  | 
309  | 12.8k  |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
310  | 7.39k  |       if (last_bitpos[ci][0] < 0)  | 
311  | 0  |         ERREXIT(cinfo, JERR_MISSING_DATA);  | 
312  | 7.39k  |     }  | 
313  | 5.41k  | #endif  | 
314  | 5.41k  |   } else { | 
315  | 0  |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
316  | 0  |       if (!component_sent[ci])  | 
317  | 0  |         ERREXIT(cinfo, JERR_MISSING_DATA);  | 
318  | 0  |     }  | 
319  | 0  |   }  | 
320  | 5.41k  | }  | 
321  |  |  | 
322  |  | #endif /* NEED_SCAN_SCRIPT */  | 
323  |  |  | 
324  |  |  | 
325  |  | LOCAL(void)  | 
326  |  | select_scan_parameters(j_compress_ptr cinfo)  | 
327  |  | /* Set up the scan parameters for the current scan */  | 
328  | 37.4k  | { | 
329  | 37.4k  |   int ci;  | 
330  |  |  | 
331  | 37.4k  | #ifdef NEED_SCAN_SCRIPT  | 
332  | 37.4k  |   if (cinfo->scan_info != NULL) { | 
333  |  |     /* Prepare for current scan --- the script is already validated */  | 
334  | 35.8k  |     my_master_ptr master = (my_master_ptr)cinfo->master;  | 
335  | 35.8k  |     const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;  | 
336  |  |  | 
337  | 35.8k  |     cinfo->comps_in_scan = scanptr->comps_in_scan;  | 
338  | 75.5k  |     for (ci = 0; ci < scanptr->comps_in_scan; ci++) { | 
339  | 39.6k  |       cinfo->cur_comp_info[ci] =  | 
340  | 39.6k  |         &cinfo->comp_info[scanptr->component_index[ci]];  | 
341  | 39.6k  |     }  | 
342  | 35.8k  |     cinfo->Ss = scanptr->Ss;  | 
343  | 35.8k  |     cinfo->Se = scanptr->Se;  | 
344  | 35.8k  |     cinfo->Ah = scanptr->Ah;  | 
345  | 35.8k  |     cinfo->Al = scanptr->Al;  | 
346  | 35.8k  |   } else  | 
347  | 1.61k  | #endif  | 
348  | 1.61k  |   { | 
349  |  |     /* Prepare for single sequential-JPEG scan containing all components */  | 
350  | 1.61k  |     if (cinfo->num_components > MAX_COMPS_IN_SCAN)  | 
351  | 0  |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,  | 
352  | 1.61k  |                MAX_COMPS_IN_SCAN);  | 
353  | 1.61k  |     cinfo->comps_in_scan = cinfo->num_components;  | 
354  | 3.45k  |     for (ci = 0; ci < cinfo->num_components; ci++) { | 
355  | 1.84k  |       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];  | 
356  | 1.84k  |     }  | 
357  | 1.61k  |     if (!cinfo->master->lossless) { | 
358  | 1.61k  |       cinfo->Ss = 0;  | 
359  | 1.61k  |       cinfo->Se = DCTSIZE2 - 1;  | 
360  | 1.61k  |       cinfo->Ah = 0;  | 
361  | 1.61k  |       cinfo->Al = 0;  | 
362  | 1.61k  |     }  | 
363  | 1.61k  |   }  | 
364  | 37.4k  | }  | 
365  |  |  | 
366  |  |  | 
367  |  | LOCAL(void)  | 
368  |  | per_scan_setup(j_compress_ptr cinfo)  | 
369  |  | /* Do computations that are needed before processing a JPEG scan */  | 
370  |  | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */  | 
371  | 37.4k  | { | 
372  | 37.4k  |   int ci, mcublks, tmp;  | 
373  | 37.4k  |   jpeg_component_info *compptr;  | 
374  | 37.4k  |   int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;  | 
375  |  |  | 
376  | 37.4k  |   if (cinfo->comps_in_scan == 1) { | 
377  |  |  | 
378  |  |     /* Noninterleaved (single-component) scan */  | 
379  | 35.5k  |     compptr = cinfo->cur_comp_info[0];  | 
380  |  |  | 
381  |  |     /* Overall image size in MCUs */  | 
382  | 35.5k  |     cinfo->MCUs_per_row = compptr->width_in_blocks;  | 
383  | 35.5k  |     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;  | 
384  |  |  | 
385  |  |     /* For noninterleaved scan, always one block per MCU */  | 
386  | 35.5k  |     compptr->MCU_width = 1;  | 
387  | 35.5k  |     compptr->MCU_height = 1;  | 
388  | 35.5k  |     compptr->MCU_blocks = 1;  | 
389  | 35.5k  |     compptr->MCU_sample_width = data_unit;  | 
390  | 35.5k  |     compptr->last_col_width = 1;  | 
391  |  |     /* For noninterleaved scans, it is convenient to define last_row_height  | 
392  |  |      * as the number of block rows present in the last iMCU row.  | 
393  |  |      */  | 
394  | 35.5k  |     tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);  | 
395  | 35.5k  |     if (tmp == 0) tmp = compptr->v_samp_factor;  | 
396  | 35.5k  |     compptr->last_row_height = tmp;  | 
397  |  |  | 
398  |  |     /* Prepare array describing MCU composition */  | 
399  | 35.5k  |     cinfo->blocks_in_MCU = 1;  | 
400  | 35.5k  |     cinfo->MCU_membership[0] = 0;  | 
401  |  |  | 
402  | 35.5k  |   } else { | 
403  |  |  | 
404  |  |     /* Interleaved (multi-component) scan */  | 
405  | 1.89k  |     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)  | 
406  | 0  |       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,  | 
407  | 1.89k  |                MAX_COMPS_IN_SCAN);  | 
408  |  |  | 
409  |  |     /* Overall image size in MCUs */  | 
410  | 1.89k  |     cinfo->MCUs_per_row = (JDIMENSION)  | 
411  | 1.89k  |       jdiv_round_up((long)cinfo->_jpeg_width,  | 
412  | 1.89k  |                     (long)(cinfo->max_h_samp_factor * data_unit));  | 
413  | 1.89k  |     cinfo->MCU_rows_in_scan = (JDIMENSION)  | 
414  | 1.89k  |       jdiv_round_up((long)cinfo->_jpeg_height,  | 
415  | 1.89k  |                     (long)(cinfo->max_v_samp_factor * data_unit));  | 
416  |  |  | 
417  | 1.89k  |     cinfo->blocks_in_MCU = 0;  | 
418  |  |  | 
419  | 7.74k  |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
420  | 5.85k  |       compptr = cinfo->cur_comp_info[ci];  | 
421  |  |       /* Sampling factors give # of blocks of component in each MCU */  | 
422  | 5.85k  |       compptr->MCU_width = compptr->h_samp_factor;  | 
423  | 5.85k  |       compptr->MCU_height = compptr->v_samp_factor;  | 
424  | 5.85k  |       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;  | 
425  | 5.85k  |       compptr->MCU_sample_width = compptr->MCU_width * data_unit;  | 
426  |  |       /* Figure number of non-dummy blocks in last MCU column & row */  | 
427  | 5.85k  |       tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);  | 
428  | 5.85k  |       if (tmp == 0) tmp = compptr->MCU_width;  | 
429  | 5.85k  |       compptr->last_col_width = tmp;  | 
430  | 5.85k  |       tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);  | 
431  | 5.85k  |       if (tmp == 0) tmp = compptr->MCU_height;  | 
432  | 5.85k  |       compptr->last_row_height = tmp;  | 
433  |  |       /* Prepare array describing MCU composition */  | 
434  | 5.85k  |       mcublks = compptr->MCU_blocks;  | 
435  | 5.85k  |       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)  | 
436  | 38  |         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);  | 
437  | 16.6k  |       while (mcublks-- > 0) { | 
438  | 10.8k  |         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;  | 
439  | 10.8k  |       }  | 
440  | 5.85k  |     }  | 
441  |  |  | 
442  | 1.89k  |   }  | 
443  |  |  | 
444  |  |   /* Convert restart specified in rows to actual MCU count. */  | 
445  |  |   /* Note that count must fit in 16 bits, so we provide limiting. */  | 
446  | 37.4k  |   if (cinfo->restart_in_rows > 0) { | 
447  | 0  |     long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;  | 
448  | 0  |     cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);  | 
449  | 0  |   }  | 
450  | 37.4k  | }  | 
451  |  |  | 
452  |  |  | 
453  |  | /*  | 
454  |  |  * Per-pass setup.  | 
455  |  |  * This is called at the beginning of each pass.  We determine which modules  | 
456  |  |  * will be active during this pass and give them appropriate start_pass calls.  | 
457  |  |  * We also set is_last_pass to indicate whether any more passes will be  | 
458  |  |  * required.  | 
459  |  |  */  | 
460  |  |  | 
461  |  | METHODDEF(void)  | 
462  |  | prepare_for_pass(j_compress_ptr cinfo)  | 
463  | 53.2k  | { | 
464  | 53.2k  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
465  |  |  | 
466  | 53.2k  |   switch (master->pass_type) { | 
467  | 0  |   case main_pass:  | 
468  |  |     /* Initial pass: will collect input data, and do either Huffman  | 
469  |  |      * optimization or data output for the first scan.  | 
470  |  |      */  | 
471  | 0  |     select_scan_parameters(cinfo);  | 
472  | 0  |     per_scan_setup(cinfo);  | 
473  | 0  |     if (!cinfo->raw_data_in) { | 
474  | 0  |       (*cinfo->cconvert->start_pass) (cinfo);  | 
475  | 0  |       (*cinfo->downsample->start_pass) (cinfo);  | 
476  | 0  |       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);  | 
477  | 0  |     }  | 
478  | 0  |     (*cinfo->fdct->start_pass) (cinfo);  | 
479  | 0  |     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);  | 
480  | 0  |     (*cinfo->coef->start_pass) (cinfo,  | 
481  | 0  |                                 (master->total_passes > 1 ?  | 
482  | 0  |                                  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));  | 
483  | 0  |     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);  | 
484  | 0  |     if (cinfo->optimize_coding) { | 
485  |  |       /* No immediate data output; postpone writing frame/scan headers */  | 
486  | 0  |       master->pub.call_pass_startup = FALSE;  | 
487  | 0  |     } else { | 
488  |  |       /* Will write frame/scan headers at first jpeg_write_scanlines call */  | 
489  | 0  |       master->pub.call_pass_startup = TRUE;  | 
490  | 0  |     }  | 
491  | 0  |     break;  | 
492  | 0  | #ifdef ENTROPY_OPT_SUPPORTED  | 
493  | 18.6k  |   case huff_opt_pass:  | 
494  |  |     /* Do Huffman optimization for a scan after the first one. */  | 
495  | 18.6k  |     select_scan_parameters(cinfo);  | 
496  | 18.6k  |     per_scan_setup(cinfo);  | 
497  | 18.6k  |     if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||  | 
498  | 18.6k  |         cinfo->master->lossless) { | 
499  | 16.0k  |       (*cinfo->entropy->start_pass) (cinfo, TRUE);  | 
500  | 16.0k  |       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
501  | 16.0k  |       master->pub.call_pass_startup = FALSE;  | 
502  | 16.0k  |       break;  | 
503  | 16.0k  |     }  | 
504  |  |     /* Special case: Huffman DC refinement scans need no Huffman table  | 
505  |  |      * and therefore we can skip the optimization pass for them.  | 
506  |  |      */  | 
507  | 2.64k  |     master->pass_type = output_pass;  | 
508  | 2.64k  |     master->pass_number++;  | 
509  | 2.64k  | #endif  | 
510  |  |     FALLTHROUGH                 /*FALLTHROUGH*/  | 
511  | 37.2k  |   case output_pass:  | 
512  |  |     /* Do a data-output pass. */  | 
513  |  |     /* We need not repeat per-scan setup if prior optimization pass did it. */  | 
514  | 37.2k  |     if (!cinfo->optimize_coding) { | 
515  | 18.8k  |       select_scan_parameters(cinfo);  | 
516  | 18.8k  |       per_scan_setup(cinfo);  | 
517  | 18.8k  |     }  | 
518  | 37.2k  |     (*cinfo->entropy->start_pass) (cinfo, FALSE);  | 
519  | 37.2k  |     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
520  |  |     /* We emit frame/scan headers now */  | 
521  | 37.2k  |     if (master->scan_number == 0)  | 
522  | 6.85k  |       (*cinfo->marker->write_frame_header) (cinfo);  | 
523  | 37.2k  |     (*cinfo->marker->write_scan_header) (cinfo);  | 
524  | 37.2k  |     master->pub.call_pass_startup = FALSE;  | 
525  | 37.2k  |     break;  | 
526  | 0  |   default:  | 
527  | 0  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
528  | 53.2k  |   }  | 
529  |  |  | 
530  | 53.2k  |   master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);  | 
531  |  |  | 
532  |  |   /* Set up progress monitor's pass info if present */  | 
533  | 53.2k  |   if (cinfo->progress != NULL) { | 
534  | 0  |     cinfo->progress->completed_passes = master->pass_number;  | 
535  | 0  |     cinfo->progress->total_passes = master->total_passes;  | 
536  | 0  |   }  | 
537  | 53.2k  | }  | 
538  |  |  | 
539  |  |  | 
540  |  | /*  | 
541  |  |  * Special start-of-pass hook.  | 
542  |  |  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.  | 
543  |  |  * In single-pass processing, we need this hook because we don't want to  | 
544  |  |  * write frame/scan headers during jpeg_start_compress; we want to let the  | 
545  |  |  * application write COM markers etc. between jpeg_start_compress and the  | 
546  |  |  * jpeg_write_scanlines loop.  | 
547  |  |  * In multi-pass processing, this routine is not used.  | 
548  |  |  */  | 
549  |  |  | 
550  |  | METHODDEF(void)  | 
551  |  | pass_startup(j_compress_ptr cinfo)  | 
552  | 0  | { | 
553  | 0  |   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */  | 
554  |  | 
  | 
555  | 0  |   (*cinfo->marker->write_frame_header) (cinfo);  | 
556  | 0  |   (*cinfo->marker->write_scan_header) (cinfo);  | 
557  | 0  | }  | 
558  |  |  | 
559  |  |  | 
560  |  | /*  | 
561  |  |  * Finish up at end of pass.  | 
562  |  |  */  | 
563  |  |  | 
564  |  | METHODDEF(void)  | 
565  |  | finish_pass_master(j_compress_ptr cinfo)  | 
566  | 52.9k  | { | 
567  | 52.9k  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
568  |  |  | 
569  |  |   /* The entropy coder always needs an end-of-pass call,  | 
570  |  |    * either to analyze statistics or to flush its output buffer.  | 
571  |  |    */  | 
572  | 52.9k  |   (*cinfo->entropy->finish_pass) (cinfo);  | 
573  |  |  | 
574  |  |   /* Update state for next pass */  | 
575  | 52.9k  |   switch (master->pass_type) { | 
576  | 0  |   case main_pass:  | 
577  |  |     /* next pass is either output of scan 0 (after optimization)  | 
578  |  |      * or output of scan 1 (if no optimization).  | 
579  |  |      */  | 
580  | 0  |     master->pass_type = output_pass;  | 
581  | 0  |     if (!cinfo->optimize_coding)  | 
582  | 0  |       master->scan_number++;  | 
583  | 0  |     break;  | 
584  | 15.7k  |   case huff_opt_pass:  | 
585  |  |     /* next pass is always output of current scan */  | 
586  | 15.7k  |     master->pass_type = output_pass;  | 
587  | 15.7k  |     break;  | 
588  | 37.2k  |   case output_pass:  | 
589  |  |     /* next pass is either optimization or output of next scan */  | 
590  | 37.2k  |     if (cinfo->optimize_coding)  | 
591  | 18.4k  |       master->pass_type = huff_opt_pass;  | 
592  | 37.2k  |     master->scan_number++;  | 
593  | 37.2k  |     break;  | 
594  | 52.9k  |   }  | 
595  |  |  | 
596  | 52.9k  |   master->pass_number++;  | 
597  | 52.9k  | }  | 
598  |  |  | 
599  |  |  | 
600  |  | /*  | 
601  |  |  * Initialize master compression control.  | 
602  |  |  */  | 
603  |  |  | 
604  |  | GLOBAL(void)  | 
605  |  | jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)  | 
606  | 7.03k  | { | 
607  | 7.03k  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
608  |  |  | 
609  | 7.03k  |   master->pub.prepare_for_pass = prepare_for_pass;  | 
610  | 7.03k  |   master->pub.pass_startup = pass_startup;  | 
611  | 7.03k  |   master->pub.finish_pass = finish_pass_master;  | 
612  | 7.03k  |   master->pub.is_last_pass = FALSE;  | 
613  |  |  | 
614  | 7.03k  |   if (cinfo->scan_info != NULL) { | 
615  | 5.41k  | #ifdef NEED_SCAN_SCRIPT  | 
616  | 5.41k  |     validate_script(cinfo);  | 
617  |  | #else  | 
618  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
619  |  | #endif  | 
620  | 5.41k  |   } else { | 
621  | 1.61k  |     cinfo->progressive_mode = FALSE;  | 
622  | 1.61k  |     cinfo->num_scans = 1;  | 
623  | 1.61k  |   }  | 
624  |  |  | 
625  |  |   /* Disable smoothing and subsampling in lossless mode, since those are lossy  | 
626  |  |    * algorithms.  Set the JPEG colorspace to the input colorspace.  Disable raw  | 
627  |  |    * (downsampled) data input, because it isn't particularly useful without  | 
628  |  |    * subsampling and has not been tested in lossless mode.  | 
629  |  |    */  | 
630  | 7.03k  |   if (cinfo->master->lossless) { | 
631  | 0  |     int ci;  | 
632  | 0  |     jpeg_component_info *compptr;  | 
633  |  | 
  | 
634  | 0  |     cinfo->raw_data_in = FALSE;  | 
635  | 0  |     cinfo->smoothing_factor = 0;  | 
636  | 0  |     jpeg_default_colorspace(cinfo);  | 
637  | 0  |     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
638  | 0  |          ci++, compptr++)  | 
639  | 0  |       compptr->h_samp_factor = compptr->v_samp_factor = 1;  | 
640  | 0  |   }  | 
641  |  |  | 
642  |  |   /* Validate parameters, determine derived values */  | 
643  | 7.03k  |   initial_setup(cinfo, transcode_only);  | 
644  |  |  | 
645  | 7.03k  |   if (cinfo->master->lossless ||        /*  TEMPORARY HACK ??? */  | 
646  | 7.03k  |       (cinfo->progressive_mode && !cinfo->arith_code))  | 
647  | 2.86k  |     cinfo->optimize_coding = TRUE; /* assume default tables no good for  | 
648  |  |                                       progressive mode or lossless mode */  | 
649  | 7.03k  |   if (cinfo->data_precision == 12 && !cinfo->arith_code)  | 
650  | 900  |     cinfo->optimize_coding = TRUE; /* assume default tables no good for 12-bit  | 
651  |  |                                       data precision */  | 
652  |  |  | 
653  |  |   /* Initialize my private state */  | 
654  | 7.03k  |   if (transcode_only) { | 
655  |  |     /* no main pass in transcoding */  | 
656  | 7.03k  |     if (cinfo->optimize_coding)  | 
657  | 3.11k  |       master->pass_type = huff_opt_pass;  | 
658  | 3.91k  |     else  | 
659  | 3.91k  |       master->pass_type = output_pass;  | 
660  | 7.03k  |   } else { | 
661  |  |     /* for normal compression, first pass is always this type: */  | 
662  | 0  |     master->pass_type = main_pass;  | 
663  | 0  |   }  | 
664  | 7.03k  |   master->scan_number = 0;  | 
665  | 7.03k  |   master->pass_number = 0;  | 
666  | 7.03k  |   if (cinfo->optimize_coding)  | 
667  | 3.11k  |     master->total_passes = cinfo->num_scans * 2;  | 
668  | 3.91k  |   else  | 
669  | 3.91k  |     master->total_passes = cinfo->num_scans;  | 
670  |  |  | 
671  | 7.03k  |   master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";  | 
672  | 7.03k  | }  |