/src/libjpeg-turbo/src/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-2024, 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(boolean) |
54 | | using_std_huff_tables(j_compress_ptr cinfo) |
55 | 0 | { |
56 | 0 | int i; |
57 | |
|
58 | 0 | static const UINT8 bits_dc_luminance[17] = { |
59 | 0 | /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 |
60 | 0 | }; |
61 | 0 | static const UINT8 val_dc_luminance[] = { |
62 | 0 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
63 | 0 | }; |
64 | |
|
65 | 0 | static const UINT8 bits_dc_chrominance[17] = { |
66 | 0 | /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 |
67 | 0 | }; |
68 | 0 | static const UINT8 val_dc_chrominance[] = { |
69 | 0 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
70 | 0 | }; |
71 | |
|
72 | 0 | static const UINT8 bits_ac_luminance[17] = { |
73 | 0 | /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d |
74 | 0 | }; |
75 | 0 | static const UINT8 val_ac_luminance[] = { |
76 | 0 | 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, |
77 | 0 | 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, |
78 | 0 | 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, |
79 | 0 | 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, |
80 | 0 | 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, |
81 | 0 | 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, |
82 | 0 | 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, |
83 | 0 | 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, |
84 | 0 | 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, |
85 | 0 | 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, |
86 | 0 | 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, |
87 | 0 | 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, |
88 | 0 | 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
89 | 0 | 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
90 | 0 | 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, |
91 | 0 | 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, |
92 | 0 | 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, |
93 | 0 | 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, |
94 | 0 | 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, |
95 | 0 | 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
96 | 0 | 0xf9, 0xfa |
97 | 0 | }; |
98 | |
|
99 | 0 | static const UINT8 bits_ac_chrominance[17] = { |
100 | 0 | /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 |
101 | 0 | }; |
102 | 0 | static const UINT8 val_ac_chrominance[] = { |
103 | 0 | 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, |
104 | 0 | 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, |
105 | 0 | 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, |
106 | 0 | 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, |
107 | 0 | 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, |
108 | 0 | 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, |
109 | 0 | 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, |
110 | 0 | 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, |
111 | 0 | 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, |
112 | 0 | 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, |
113 | 0 | 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, |
114 | 0 | 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, |
115 | 0 | 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, |
116 | 0 | 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, |
117 | 0 | 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, |
118 | 0 | 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, |
119 | 0 | 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, |
120 | 0 | 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, |
121 | 0 | 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, |
122 | 0 | 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
123 | 0 | 0xf9, 0xfa |
124 | 0 | }; |
125 | |
|
126 | 0 | if (cinfo->dc_huff_tbl_ptrs[0] == NULL || |
127 | 0 | cinfo->ac_huff_tbl_ptrs[0] == NULL || |
128 | 0 | cinfo->dc_huff_tbl_ptrs[1] == NULL || |
129 | 0 | cinfo->ac_huff_tbl_ptrs[1] == NULL) |
130 | 0 | return FALSE; |
131 | | |
132 | 0 | for (i = 2; i < NUM_HUFF_TBLS; i++) { |
133 | 0 | if (cinfo->dc_huff_tbl_ptrs[i] != NULL || |
134 | 0 | cinfo->ac_huff_tbl_ptrs[i] != NULL) |
135 | 0 | return FALSE; |
136 | 0 | } |
137 | | |
138 | 0 | if (memcmp(cinfo->dc_huff_tbl_ptrs[0]->bits, bits_dc_luminance, |
139 | 0 | sizeof(bits_dc_luminance)) || |
140 | 0 | memcmp(cinfo->dc_huff_tbl_ptrs[0]->huffval, val_dc_luminance, |
141 | 0 | sizeof(val_dc_luminance)) || |
142 | 0 | memcmp(cinfo->ac_huff_tbl_ptrs[0]->bits, bits_ac_luminance, |
143 | 0 | sizeof(bits_ac_luminance)) || |
144 | 0 | memcmp(cinfo->ac_huff_tbl_ptrs[0]->huffval, val_ac_luminance, |
145 | 0 | sizeof(val_ac_luminance)) || |
146 | 0 | memcmp(cinfo->dc_huff_tbl_ptrs[1]->bits, bits_dc_chrominance, |
147 | 0 | sizeof(bits_dc_chrominance)) || |
148 | 0 | memcmp(cinfo->dc_huff_tbl_ptrs[1]->huffval, val_dc_chrominance, |
149 | 0 | sizeof(val_dc_chrominance)) || |
150 | 0 | memcmp(cinfo->ac_huff_tbl_ptrs[1]->bits, bits_ac_chrominance, |
151 | 0 | sizeof(bits_ac_chrominance)) || |
152 | 0 | memcmp(cinfo->ac_huff_tbl_ptrs[1]->huffval, val_ac_chrominance, |
153 | 0 | sizeof(val_ac_chrominance))) |
154 | 0 | return FALSE; |
155 | | |
156 | 0 | return TRUE; |
157 | 0 | } |
158 | | |
159 | | |
160 | | LOCAL(void) |
161 | | initial_setup(j_compress_ptr cinfo, boolean transcode_only) |
162 | | /* Do computations that are needed before master selection phase */ |
163 | 0 | { |
164 | 0 | int ci; |
165 | 0 | jpeg_component_info *compptr; |
166 | 0 | long samplesperrow; |
167 | 0 | JDIMENSION jd_samplesperrow; |
168 | 0 | int data_unit = cinfo->master->lossless ? 1 : DCTSIZE; |
169 | |
|
170 | | #if JPEG_LIB_VERSION >= 70 |
171 | | #if JPEG_LIB_VERSION >= 80 |
172 | | if (!transcode_only) |
173 | | #endif |
174 | | jpeg_calc_jpeg_dimensions(cinfo); |
175 | | #endif |
176 | | |
177 | | /* Sanity check on image dimensions */ |
178 | 0 | if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || |
179 | 0 | cinfo->num_components <= 0 || cinfo->input_components <= 0) |
180 | 0 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
181 | | |
182 | | /* Make sure image isn't bigger than I can handle */ |
183 | 0 | if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION || |
184 | 0 | (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION) |
185 | 0 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION); |
186 | | |
187 | | /* Width of an input scanline must be representable as JDIMENSION. */ |
188 | 0 | samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components; |
189 | 0 | jd_samplesperrow = (JDIMENSION)samplesperrow; |
190 | 0 | if ((long)jd_samplesperrow != samplesperrow) |
191 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
192 | | |
193 | | /* Lossy JPEG images must have 8 or 12 bits per sample. Lossless JPEG images |
194 | | * can have 2 to 16 bits per sample. |
195 | | */ |
196 | 0 | #ifdef C_LOSSLESS_SUPPORTED |
197 | 0 | if (cinfo->master->lossless) { |
198 | 0 | if (cinfo->data_precision < 2 || cinfo->data_precision > 16) |
199 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
200 | 0 | } else |
201 | 0 | #endif |
202 | 0 | { |
203 | 0 | if (cinfo->data_precision != 8 && cinfo->data_precision != 12) |
204 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
205 | 0 | } |
206 | | |
207 | | /* Check that number of components won't exceed internal array sizes */ |
208 | 0 | if (cinfo->num_components > MAX_COMPONENTS) |
209 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
210 | 0 | MAX_COMPONENTS); |
211 | | |
212 | | /* Compute maximum sampling factors; check factor validity */ |
213 | 0 | cinfo->max_h_samp_factor = 1; |
214 | 0 | cinfo->max_v_samp_factor = 1; |
215 | 0 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
216 | 0 | ci++, compptr++) { |
217 | 0 | if (compptr->h_samp_factor <= 0 || |
218 | 0 | compptr->h_samp_factor > MAX_SAMP_FACTOR || |
219 | 0 | compptr->v_samp_factor <= 0 || |
220 | 0 | compptr->v_samp_factor > MAX_SAMP_FACTOR) |
221 | 0 | ERREXIT(cinfo, JERR_BAD_SAMPLING); |
222 | 0 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, |
223 | 0 | compptr->h_samp_factor); |
224 | 0 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, |
225 | 0 | compptr->v_samp_factor); |
226 | 0 | } |
227 | | |
228 | | /* Compute dimensions of components */ |
229 | 0 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
230 | 0 | ci++, compptr++) { |
231 | | /* Fill in the correct component_index value; don't rely on application */ |
232 | 0 | compptr->component_index = ci; |
233 | | /* For compression, we never do DCT scaling. */ |
234 | | #if JPEG_LIB_VERSION >= 70 |
235 | | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit; |
236 | | #else |
237 | 0 | compptr->DCT_scaled_size = data_unit; |
238 | 0 | #endif |
239 | | /* Size in data units */ |
240 | 0 | compptr->width_in_blocks = (JDIMENSION) |
241 | 0 | jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, |
242 | 0 | (long)(cinfo->max_h_samp_factor * data_unit)); |
243 | 0 | compptr->height_in_blocks = (JDIMENSION) |
244 | 0 | jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, |
245 | 0 | (long)(cinfo->max_v_samp_factor * data_unit)); |
246 | | /* Size in samples */ |
247 | 0 | compptr->downsampled_width = (JDIMENSION) |
248 | 0 | jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, |
249 | 0 | (long)cinfo->max_h_samp_factor); |
250 | 0 | compptr->downsampled_height = (JDIMENSION) |
251 | 0 | jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, |
252 | 0 | (long)cinfo->max_v_samp_factor); |
253 | | /* Mark component needed (this flag isn't actually used for compression) */ |
254 | 0 | compptr->component_needed = TRUE; |
255 | 0 | } |
256 | | |
257 | | /* Compute number of fully interleaved MCU rows (number of times that |
258 | | * main controller will call coefficient or difference controller). |
259 | | */ |
260 | 0 | cinfo->total_iMCU_rows = (JDIMENSION) |
261 | 0 | jdiv_round_up((long)cinfo->_jpeg_height, |
262 | 0 | (long)(cinfo->max_v_samp_factor * data_unit)); |
263 | 0 | } |
264 | | |
265 | | |
266 | | #if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED) |
267 | | #define NEED_SCAN_SCRIPT |
268 | | #endif |
269 | | |
270 | | #ifdef NEED_SCAN_SCRIPT |
271 | | |
272 | | LOCAL(void) |
273 | | validate_script(j_compress_ptr cinfo) |
274 | | /* Verify that the scan script in cinfo->scan_info[] is valid; also |
275 | | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. |
276 | | */ |
277 | 0 | { |
278 | 0 | const jpeg_scan_info *scanptr; |
279 | 0 | int scanno, ncomps, ci, coefi, thisi; |
280 | 0 | int Ss, Se, Ah, Al; |
281 | 0 | boolean component_sent[MAX_COMPONENTS]; |
282 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
283 | 0 | int *last_bitpos_ptr; |
284 | 0 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; |
285 | | /* -1 until that coefficient has been seen; then last Al for it */ |
286 | 0 | #endif |
287 | |
|
288 | 0 | if (cinfo->num_scans <= 0) |
289 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); |
290 | |
|
291 | | #ifndef C_MULTISCAN_FILES_SUPPORTED |
292 | | if (cinfo->num_scans > 1) |
293 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
294 | | #endif |
295 | |
|
296 | 0 | scanptr = cinfo->scan_info; |
297 | 0 | if (scanptr->Ss != 0 && scanptr->Se == 0) { |
298 | 0 | #ifdef C_LOSSLESS_SUPPORTED |
299 | 0 | cinfo->master->lossless = TRUE; |
300 | 0 | cinfo->progressive_mode = FALSE; |
301 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) |
302 | 0 | component_sent[ci] = FALSE; |
303 | | #else |
304 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
305 | | #endif |
306 | 0 | } |
307 | | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; |
308 | | * for progressive JPEG, no scan can have this. |
309 | | */ |
310 | 0 | else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) { |
311 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
312 | 0 | cinfo->progressive_mode = TRUE; |
313 | 0 | cinfo->master->lossless = FALSE; |
314 | 0 | last_bitpos_ptr = &last_bitpos[0][0]; |
315 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) |
316 | 0 | for (coefi = 0; coefi < DCTSIZE2; coefi++) |
317 | 0 | *last_bitpos_ptr++ = -1; |
318 | | #else |
319 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
320 | | #endif |
321 | 0 | } else { |
322 | 0 | cinfo->progressive_mode = cinfo->master->lossless = FALSE; |
323 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) |
324 | 0 | component_sent[ci] = FALSE; |
325 | 0 | } |
326 | |
|
327 | 0 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { |
328 | | /* Validate component indexes */ |
329 | 0 | ncomps = scanptr->comps_in_scan; |
330 | 0 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) |
331 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); |
332 | 0 | for (ci = 0; ci < ncomps; ci++) { |
333 | 0 | thisi = scanptr->component_index[ci]; |
334 | 0 | if (thisi < 0 || thisi >= cinfo->num_components) |
335 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
336 | | /* Components must appear in SOF order within each scan */ |
337 | 0 | if (ci > 0 && thisi <= scanptr->component_index[ci - 1]) |
338 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
339 | 0 | } |
340 | | /* Validate progression parameters */ |
341 | 0 | Ss = scanptr->Ss; |
342 | 0 | Se = scanptr->Se; |
343 | 0 | Ah = scanptr->Ah; |
344 | 0 | Al = scanptr->Al; |
345 | 0 | if (cinfo->progressive_mode) { |
346 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
347 | | /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah |
348 | | * and Al, but that seems wrong: the upper bound ought to depend on data |
349 | | * precision. Perhaps they really meant 0..N+1 for N-bit precision. |
350 | | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in |
351 | | * out-of-range reconstructed DC values during the first DC scan, |
352 | | * which might cause problems for some decoders. |
353 | | */ |
354 | 0 | int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10; |
355 | |
|
356 | 0 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || |
357 | 0 | Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al) |
358 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
359 | 0 | if (Ss == 0) { |
360 | 0 | if (Se != 0) /* DC and AC together not OK */ |
361 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
362 | 0 | } else { |
363 | 0 | if (ncomps != 1) /* AC scans must be for only one component */ |
364 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
365 | 0 | } |
366 | 0 | for (ci = 0; ci < ncomps; ci++) { |
367 | 0 | last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0]; |
368 | 0 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ |
369 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
370 | 0 | for (coefi = Ss; coefi <= Se; coefi++) { |
371 | 0 | if (last_bitpos_ptr[coefi] < 0) { |
372 | | /* first scan of this coefficient */ |
373 | 0 | if (Ah != 0) |
374 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
375 | 0 | } else { |
376 | | /* not first scan */ |
377 | 0 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1) |
378 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
379 | 0 | } |
380 | 0 | last_bitpos_ptr[coefi] = Al; |
381 | 0 | } |
382 | 0 | } |
383 | 0 | #endif |
384 | 0 | } else { |
385 | 0 | #ifdef C_LOSSLESS_SUPPORTED |
386 | 0 | if (cinfo->master->lossless) { |
387 | | /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that |
388 | | * seems wrong: the upper bound ought to depend on data precision. |
389 | | * Perhaps they really meant 0..N-1 for N-bit precision, which is what |
390 | | * we allow here. Values greater than or equal to the data precision |
391 | | * will result in a blank image. |
392 | | */ |
393 | 0 | if (Ss < 1 || Ss > 7 || /* predictor selection value */ |
394 | 0 | Se != 0 || Ah != 0 || |
395 | 0 | Al < 0 || Al >= cinfo->data_precision) /* point transform */ |
396 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
397 | 0 | } else |
398 | 0 | #endif |
399 | 0 | { |
400 | | /* For sequential JPEG, all progression parameters must be these: */ |
401 | 0 | if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0) |
402 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
403 | 0 | } |
404 | | /* Make sure components are not sent twice */ |
405 | 0 | for (ci = 0; ci < ncomps; ci++) { |
406 | 0 | thisi = scanptr->component_index[ci]; |
407 | 0 | if (component_sent[thisi]) |
408 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
409 | 0 | component_sent[thisi] = TRUE; |
410 | 0 | } |
411 | 0 | } |
412 | 0 | } |
413 | | |
414 | | /* Now verify that everything got sent. */ |
415 | 0 | if (cinfo->progressive_mode) { |
416 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
417 | | /* For progressive mode, we only check that at least some DC data |
418 | | * got sent for each component; the spec does not require that all bits |
419 | | * of all coefficients be transmitted. Would it be wiser to enforce |
420 | | * transmission of all coefficient bits?? |
421 | | */ |
422 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
423 | 0 | if (last_bitpos[ci][0] < 0) |
424 | 0 | ERREXIT(cinfo, JERR_MISSING_DATA); |
425 | 0 | } |
426 | 0 | #endif |
427 | 0 | } else { |
428 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
429 | 0 | if (!component_sent[ci]) |
430 | 0 | ERREXIT(cinfo, JERR_MISSING_DATA); |
431 | 0 | } |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | | #endif /* NEED_SCAN_SCRIPT */ |
436 | | |
437 | | |
438 | | LOCAL(void) |
439 | | select_scan_parameters(j_compress_ptr cinfo) |
440 | | /* Set up the scan parameters for the current scan */ |
441 | 0 | { |
442 | 0 | int ci; |
443 | |
|
444 | 0 | #ifdef NEED_SCAN_SCRIPT |
445 | 0 | if (cinfo->scan_info != NULL) { |
446 | | /* Prepare for current scan --- the script is already validated */ |
447 | 0 | my_master_ptr master = (my_master_ptr)cinfo->master; |
448 | 0 | const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number; |
449 | |
|
450 | 0 | cinfo->comps_in_scan = scanptr->comps_in_scan; |
451 | 0 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { |
452 | 0 | cinfo->cur_comp_info[ci] = |
453 | 0 | &cinfo->comp_info[scanptr->component_index[ci]]; |
454 | 0 | } |
455 | 0 | cinfo->Ss = scanptr->Ss; |
456 | 0 | cinfo->Se = scanptr->Se; |
457 | 0 | cinfo->Ah = scanptr->Ah; |
458 | 0 | cinfo->Al = scanptr->Al; |
459 | 0 | } else |
460 | 0 | #endif |
461 | 0 | { |
462 | | /* Prepare for single sequential-JPEG scan containing all components */ |
463 | 0 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) |
464 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
465 | 0 | MAX_COMPS_IN_SCAN); |
466 | 0 | cinfo->comps_in_scan = cinfo->num_components; |
467 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
468 | 0 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; |
469 | 0 | } |
470 | 0 | if (!cinfo->master->lossless) { |
471 | 0 | cinfo->Ss = 0; |
472 | 0 | cinfo->Se = DCTSIZE2 - 1; |
473 | 0 | cinfo->Ah = 0; |
474 | 0 | cinfo->Al = 0; |
475 | 0 | } |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | | |
480 | | LOCAL(void) |
481 | | per_scan_setup(j_compress_ptr cinfo) |
482 | | /* Do computations that are needed before processing a JPEG scan */ |
483 | | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ |
484 | 0 | { |
485 | 0 | int ci, mcublks, tmp; |
486 | 0 | jpeg_component_info *compptr; |
487 | 0 | int data_unit = cinfo->master->lossless ? 1 : DCTSIZE; |
488 | |
|
489 | 0 | if (cinfo->comps_in_scan == 1) { |
490 | | |
491 | | /* Noninterleaved (single-component) scan */ |
492 | 0 | compptr = cinfo->cur_comp_info[0]; |
493 | | |
494 | | /* Overall image size in MCUs */ |
495 | 0 | cinfo->MCUs_per_row = compptr->width_in_blocks; |
496 | 0 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; |
497 | | |
498 | | /* For noninterleaved scan, always one block per MCU */ |
499 | 0 | compptr->MCU_width = 1; |
500 | 0 | compptr->MCU_height = 1; |
501 | 0 | compptr->MCU_blocks = 1; |
502 | 0 | compptr->MCU_sample_width = data_unit; |
503 | 0 | compptr->last_col_width = 1; |
504 | | /* For noninterleaved scans, it is convenient to define last_row_height |
505 | | * as the number of block rows present in the last iMCU row. |
506 | | */ |
507 | 0 | tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor); |
508 | 0 | if (tmp == 0) tmp = compptr->v_samp_factor; |
509 | 0 | compptr->last_row_height = tmp; |
510 | | |
511 | | /* Prepare array describing MCU composition */ |
512 | 0 | cinfo->blocks_in_MCU = 1; |
513 | 0 | cinfo->MCU_membership[0] = 0; |
514 | |
|
515 | 0 | } else { |
516 | | |
517 | | /* Interleaved (multi-component) scan */ |
518 | 0 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) |
519 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
520 | 0 | MAX_COMPS_IN_SCAN); |
521 | | |
522 | | /* Overall image size in MCUs */ |
523 | 0 | cinfo->MCUs_per_row = (JDIMENSION) |
524 | 0 | jdiv_round_up((long)cinfo->_jpeg_width, |
525 | 0 | (long)(cinfo->max_h_samp_factor * data_unit)); |
526 | 0 | cinfo->MCU_rows_in_scan = (JDIMENSION) |
527 | 0 | jdiv_round_up((long)cinfo->_jpeg_height, |
528 | 0 | (long)(cinfo->max_v_samp_factor * data_unit)); |
529 | |
|
530 | 0 | cinfo->blocks_in_MCU = 0; |
531 | |
|
532 | 0 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
533 | 0 | compptr = cinfo->cur_comp_info[ci]; |
534 | | /* Sampling factors give # of blocks of component in each MCU */ |
535 | 0 | compptr->MCU_width = compptr->h_samp_factor; |
536 | 0 | compptr->MCU_height = compptr->v_samp_factor; |
537 | 0 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
538 | 0 | compptr->MCU_sample_width = compptr->MCU_width * data_unit; |
539 | | /* Figure number of non-dummy blocks in last MCU column & row */ |
540 | 0 | tmp = (int)(compptr->width_in_blocks % compptr->MCU_width); |
541 | 0 | if (tmp == 0) tmp = compptr->MCU_width; |
542 | 0 | compptr->last_col_width = tmp; |
543 | 0 | tmp = (int)(compptr->height_in_blocks % compptr->MCU_height); |
544 | 0 | if (tmp == 0) tmp = compptr->MCU_height; |
545 | 0 | compptr->last_row_height = tmp; |
546 | | /* Prepare array describing MCU composition */ |
547 | 0 | mcublks = compptr->MCU_blocks; |
548 | 0 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) |
549 | 0 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); |
550 | 0 | while (mcublks-- > 0) { |
551 | 0 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; |
552 | 0 | } |
553 | 0 | } |
554 | |
|
555 | 0 | } |
556 | | |
557 | | /* Convert restart specified in rows to actual MCU count. */ |
558 | | /* Note that count must fit in 16 bits, so we provide limiting. */ |
559 | 0 | if (cinfo->restart_in_rows > 0) { |
560 | 0 | long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row; |
561 | 0 | cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L); |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | | |
566 | | /* |
567 | | * Per-pass setup. |
568 | | * This is called at the beginning of each pass. We determine which modules |
569 | | * will be active during this pass and give them appropriate start_pass calls. |
570 | | * We also set is_last_pass to indicate whether any more passes will be |
571 | | * required. |
572 | | */ |
573 | | |
574 | | METHODDEF(void) |
575 | | prepare_for_pass(j_compress_ptr cinfo) |
576 | 0 | { |
577 | 0 | my_master_ptr master = (my_master_ptr)cinfo->master; |
578 | |
|
579 | 0 | switch (master->pass_type) { |
580 | 0 | case main_pass: |
581 | | /* Initial pass: will collect input data, and do either Huffman |
582 | | * optimization or data output for the first scan. |
583 | | */ |
584 | 0 | select_scan_parameters(cinfo); |
585 | 0 | per_scan_setup(cinfo); |
586 | 0 | if (!cinfo->raw_data_in) { |
587 | 0 | (*cinfo->cconvert->start_pass) (cinfo); |
588 | 0 | (*cinfo->downsample->start_pass) (cinfo); |
589 | 0 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); |
590 | 0 | } |
591 | 0 | (*cinfo->fdct->start_pass) (cinfo); |
592 | 0 | (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); |
593 | 0 | (*cinfo->coef->start_pass) (cinfo, |
594 | 0 | (master->total_passes > 1 ? |
595 | 0 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
596 | 0 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
597 | 0 | if (cinfo->optimize_coding) { |
598 | | /* No immediate data output; postpone writing frame/scan headers */ |
599 | 0 | master->pub.call_pass_startup = FALSE; |
600 | 0 | } else { |
601 | | /* Will write frame/scan headers at first jpeg_write_scanlines call */ |
602 | 0 | master->pub.call_pass_startup = TRUE; |
603 | 0 | } |
604 | 0 | break; |
605 | 0 | #ifdef ENTROPY_OPT_SUPPORTED |
606 | 0 | case huff_opt_pass: |
607 | | /* Do Huffman optimization for a scan after the first one. */ |
608 | 0 | select_scan_parameters(cinfo); |
609 | 0 | per_scan_setup(cinfo); |
610 | 0 | if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code || |
611 | 0 | cinfo->master->lossless) { |
612 | 0 | (*cinfo->entropy->start_pass) (cinfo, TRUE); |
613 | 0 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
614 | 0 | master->pub.call_pass_startup = FALSE; |
615 | 0 | break; |
616 | 0 | } |
617 | | /* Special case: Huffman DC refinement scans need no Huffman table |
618 | | * and therefore we can skip the optimization pass for them. |
619 | | */ |
620 | 0 | master->pass_type = output_pass; |
621 | 0 | master->pass_number++; |
622 | 0 | #endif |
623 | 0 | FALLTHROUGH /*FALLTHROUGH*/ |
624 | 0 | case output_pass: |
625 | | /* Do a data-output pass. */ |
626 | | /* We need not repeat per-scan setup if prior optimization pass did it. */ |
627 | 0 | if (!cinfo->optimize_coding) { |
628 | 0 | select_scan_parameters(cinfo); |
629 | 0 | per_scan_setup(cinfo); |
630 | 0 | } |
631 | 0 | (*cinfo->entropy->start_pass) (cinfo, FALSE); |
632 | 0 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
633 | | /* We emit frame/scan headers now */ |
634 | 0 | if (master->scan_number == 0) |
635 | 0 | (*cinfo->marker->write_frame_header) (cinfo); |
636 | 0 | (*cinfo->marker->write_scan_header) (cinfo); |
637 | 0 | master->pub.call_pass_startup = FALSE; |
638 | 0 | break; |
639 | 0 | default: |
640 | 0 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
641 | 0 | } |
642 | | |
643 | 0 | master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); |
644 | | |
645 | | /* Set up progress monitor's pass info if present */ |
646 | 0 | if (cinfo->progress != NULL) { |
647 | 0 | cinfo->progress->completed_passes = master->pass_number; |
648 | 0 | cinfo->progress->total_passes = master->total_passes; |
649 | 0 | } |
650 | 0 | } |
651 | | |
652 | | |
653 | | /* |
654 | | * Special start-of-pass hook. |
655 | | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. |
656 | | * In single-pass processing, we need this hook because we don't want to |
657 | | * write frame/scan headers during jpeg_start_compress; we want to let the |
658 | | * application write COM markers etc. between jpeg_start_compress and the |
659 | | * jpeg_write_scanlines loop. |
660 | | * In multi-pass processing, this routine is not used. |
661 | | */ |
662 | | |
663 | | METHODDEF(void) |
664 | | pass_startup(j_compress_ptr cinfo) |
665 | 0 | { |
666 | 0 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ |
667 | |
|
668 | 0 | (*cinfo->marker->write_frame_header) (cinfo); |
669 | 0 | (*cinfo->marker->write_scan_header) (cinfo); |
670 | 0 | } |
671 | | |
672 | | |
673 | | /* |
674 | | * Finish up at end of pass. |
675 | | */ |
676 | | |
677 | | METHODDEF(void) |
678 | | finish_pass_master(j_compress_ptr cinfo) |
679 | 0 | { |
680 | 0 | my_master_ptr master = (my_master_ptr)cinfo->master; |
681 | | |
682 | | /* The entropy coder always needs an end-of-pass call, |
683 | | * either to analyze statistics or to flush its output buffer. |
684 | | */ |
685 | 0 | (*cinfo->entropy->finish_pass) (cinfo); |
686 | | |
687 | | /* Update state for next pass */ |
688 | 0 | switch (master->pass_type) { |
689 | 0 | case main_pass: |
690 | | /* next pass is either output of scan 0 (after optimization) |
691 | | * or output of scan 1 (if no optimization). |
692 | | */ |
693 | 0 | master->pass_type = output_pass; |
694 | 0 | if (!cinfo->optimize_coding) |
695 | 0 | master->scan_number++; |
696 | 0 | break; |
697 | 0 | case huff_opt_pass: |
698 | | /* next pass is always output of current scan */ |
699 | 0 | master->pass_type = output_pass; |
700 | 0 | break; |
701 | 0 | case output_pass: |
702 | | /* next pass is either optimization or output of next scan */ |
703 | 0 | if (cinfo->optimize_coding) |
704 | 0 | master->pass_type = huff_opt_pass; |
705 | 0 | master->scan_number++; |
706 | 0 | break; |
707 | 0 | } |
708 | | |
709 | 0 | master->pass_number++; |
710 | 0 | } |
711 | | |
712 | | |
713 | | /* |
714 | | * Initialize master compression control. |
715 | | */ |
716 | | |
717 | | GLOBAL(void) |
718 | | jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only) |
719 | 0 | { |
720 | 0 | my_master_ptr master = (my_master_ptr)cinfo->master; |
721 | 0 | boolean empty_huff_tables = TRUE; |
722 | 0 | int i; |
723 | |
|
724 | 0 | master->pub.prepare_for_pass = prepare_for_pass; |
725 | 0 | master->pub.pass_startup = pass_startup; |
726 | 0 | master->pub.finish_pass = finish_pass_master; |
727 | 0 | master->pub.is_last_pass = FALSE; |
728 | |
|
729 | 0 | if (cinfo->scan_info != NULL) { |
730 | 0 | #ifdef NEED_SCAN_SCRIPT |
731 | 0 | validate_script(cinfo); |
732 | | #else |
733 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
734 | | #endif |
735 | 0 | } else { |
736 | 0 | cinfo->progressive_mode = FALSE; |
737 | 0 | cinfo->num_scans = 1; |
738 | 0 | } |
739 | |
|
740 | 0 | #ifdef C_LOSSLESS_SUPPORTED |
741 | | /* Disable smoothing and subsampling in lossless mode, since those are lossy |
742 | | * algorithms. Set the JPEG colorspace to the input colorspace. Disable raw |
743 | | * (downsampled) data input, because it isn't particularly useful without |
744 | | * subsampling and has not been tested in lossless mode. |
745 | | */ |
746 | 0 | if (cinfo->master->lossless) { |
747 | 0 | int ci; |
748 | 0 | jpeg_component_info *compptr; |
749 | |
|
750 | 0 | cinfo->raw_data_in = FALSE; |
751 | 0 | cinfo->smoothing_factor = 0; |
752 | 0 | jpeg_default_colorspace(cinfo); |
753 | 0 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
754 | 0 | ci++, compptr++) |
755 | 0 | compptr->h_samp_factor = compptr->v_samp_factor = 1; |
756 | 0 | } |
757 | 0 | #endif |
758 | | |
759 | | /* Validate parameters, determine derived values */ |
760 | 0 | initial_setup(cinfo, transcode_only); |
761 | |
|
762 | 0 | if (cinfo->arith_code) |
763 | 0 | cinfo->optimize_coding = FALSE; |
764 | 0 | else { |
765 | 0 | if (cinfo->master->lossless || /* TEMPORARY HACK ??? */ |
766 | 0 | cinfo->progressive_mode) |
767 | 0 | cinfo->optimize_coding = TRUE; /* assume default tables no good for |
768 | | progressive mode or lossless mode */ |
769 | 0 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
770 | 0 | if (cinfo->dc_huff_tbl_ptrs[i] != NULL || |
771 | 0 | cinfo->ac_huff_tbl_ptrs[i] != NULL) { |
772 | 0 | empty_huff_tables = FALSE; |
773 | 0 | break; |
774 | 0 | } |
775 | 0 | } |
776 | 0 | if (cinfo->data_precision == 12 && !cinfo->optimize_coding && |
777 | 0 | (empty_huff_tables || using_std_huff_tables(cinfo))) |
778 | 0 | cinfo->optimize_coding = TRUE; /* assume default tables no good for |
779 | | 12-bit data precision */ |
780 | 0 | } |
781 | | |
782 | | /* Initialize my private state */ |
783 | 0 | if (transcode_only) { |
784 | | /* no main pass in transcoding */ |
785 | 0 | if (cinfo->optimize_coding) |
786 | 0 | master->pass_type = huff_opt_pass; |
787 | 0 | else |
788 | 0 | master->pass_type = output_pass; |
789 | 0 | } else { |
790 | | /* for normal compression, first pass is always this type: */ |
791 | 0 | master->pass_type = main_pass; |
792 | 0 | } |
793 | 0 | master->scan_number = 0; |
794 | 0 | master->pass_number = 0; |
795 | 0 | if (cinfo->optimize_coding) |
796 | 0 | master->total_passes = cinfo->num_scans * 2; |
797 | 0 | else |
798 | 0 | master->total_passes = cinfo->num_scans; |
799 | |
|
800 | 0 | master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")"; |
801 | 0 | } |