Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libjpeg-turbo/src/jcmaster.c
9904 views
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
{
56
int i;
57
58
static const UINT8 bits_dc_luminance[17] = {
59
/* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
60
};
61
static const UINT8 val_dc_luminance[] = {
62
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
63
};
64
65
static const UINT8 bits_dc_chrominance[17] = {
66
/* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
67
};
68
static const UINT8 val_dc_chrominance[] = {
69
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
70
};
71
72
static const UINT8 bits_ac_luminance[17] = {
73
/* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
74
};
75
static const UINT8 val_ac_luminance[] = {
76
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
77
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
78
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
79
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
80
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
81
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
82
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
83
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
84
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
85
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
86
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
87
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
88
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
89
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
90
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
91
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
92
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
93
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
94
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
95
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
96
0xf9, 0xfa
97
};
98
99
static const UINT8 bits_ac_chrominance[17] = {
100
/* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
101
};
102
static const UINT8 val_ac_chrominance[] = {
103
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
104
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
105
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
106
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
107
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
108
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
109
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
110
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
111
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
112
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
113
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
114
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
115
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
116
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
117
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
118
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
119
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
120
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
121
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
122
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
123
0xf9, 0xfa
124
};
125
126
if (cinfo->dc_huff_tbl_ptrs[0] == NULL ||
127
cinfo->ac_huff_tbl_ptrs[0] == NULL ||
128
cinfo->dc_huff_tbl_ptrs[1] == NULL ||
129
cinfo->ac_huff_tbl_ptrs[1] == NULL)
130
return FALSE;
131
132
for (i = 2; i < NUM_HUFF_TBLS; i++) {
133
if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||
134
cinfo->ac_huff_tbl_ptrs[i] != NULL)
135
return FALSE;
136
}
137
138
if (memcmp(cinfo->dc_huff_tbl_ptrs[0]->bits, bits_dc_luminance,
139
sizeof(bits_dc_luminance)) ||
140
memcmp(cinfo->dc_huff_tbl_ptrs[0]->huffval, val_dc_luminance,
141
sizeof(val_dc_luminance)) ||
142
memcmp(cinfo->ac_huff_tbl_ptrs[0]->bits, bits_ac_luminance,
143
sizeof(bits_ac_luminance)) ||
144
memcmp(cinfo->ac_huff_tbl_ptrs[0]->huffval, val_ac_luminance,
145
sizeof(val_ac_luminance)) ||
146
memcmp(cinfo->dc_huff_tbl_ptrs[1]->bits, bits_dc_chrominance,
147
sizeof(bits_dc_chrominance)) ||
148
memcmp(cinfo->dc_huff_tbl_ptrs[1]->huffval, val_dc_chrominance,
149
sizeof(val_dc_chrominance)) ||
150
memcmp(cinfo->ac_huff_tbl_ptrs[1]->bits, bits_ac_chrominance,
151
sizeof(bits_ac_chrominance)) ||
152
memcmp(cinfo->ac_huff_tbl_ptrs[1]->huffval, val_ac_chrominance,
153
sizeof(val_ac_chrominance)))
154
return FALSE;
155
156
return TRUE;
157
}
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
{
164
int ci;
165
jpeg_component_info *compptr;
166
long samplesperrow;
167
JDIMENSION jd_samplesperrow;
168
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
if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
179
cinfo->num_components <= 0 || cinfo->input_components <= 0)
180
ERREXIT(cinfo, JERR_EMPTY_IMAGE);
181
182
/* Make sure image isn't bigger than I can handle */
183
if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||
184
(long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)
185
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
186
187
/* Width of an input scanline must be representable as JDIMENSION. */
188
samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;
189
jd_samplesperrow = (JDIMENSION)samplesperrow;
190
if ((long)jd_samplesperrow != samplesperrow)
191
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
#ifdef C_LOSSLESS_SUPPORTED
197
if (cinfo->master->lossless) {
198
if (cinfo->data_precision < 2 || cinfo->data_precision > 16)
199
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
200
} else
201
#endif
202
{
203
if (cinfo->data_precision != 8 && cinfo->data_precision != 12)
204
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
205
}
206
207
/* Check that number of components won't exceed internal array sizes */
208
if (cinfo->num_components > MAX_COMPONENTS)
209
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
210
MAX_COMPONENTS);
211
212
/* Compute maximum sampling factors; check factor validity */
213
cinfo->max_h_samp_factor = 1;
214
cinfo->max_v_samp_factor = 1;
215
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
216
ci++, compptr++) {
217
if (compptr->h_samp_factor <= 0 ||
218
compptr->h_samp_factor > MAX_SAMP_FACTOR ||
219
compptr->v_samp_factor <= 0 ||
220
compptr->v_samp_factor > MAX_SAMP_FACTOR)
221
ERREXIT(cinfo, JERR_BAD_SAMPLING);
222
cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
223
compptr->h_samp_factor);
224
cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
225
compptr->v_samp_factor);
226
}
227
228
/* Compute dimensions of components */
229
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
230
ci++, compptr++) {
231
/* Fill in the correct component_index value; don't rely on application */
232
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
compptr->DCT_scaled_size = data_unit;
238
#endif
239
/* Size in data units */
240
compptr->width_in_blocks = (JDIMENSION)
241
jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
242
(long)(cinfo->max_h_samp_factor * data_unit));
243
compptr->height_in_blocks = (JDIMENSION)
244
jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
245
(long)(cinfo->max_v_samp_factor * data_unit));
246
/* Size in samples */
247
compptr->downsampled_width = (JDIMENSION)
248
jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
249
(long)cinfo->max_h_samp_factor);
250
compptr->downsampled_height = (JDIMENSION)
251
jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
252
(long)cinfo->max_v_samp_factor);
253
/* Mark component needed (this flag isn't actually used for compression) */
254
compptr->component_needed = TRUE;
255
}
256
257
/* Compute number of fully interleaved MCU rows (number of times that
258
* main controller will call coefficient or difference controller).
259
*/
260
cinfo->total_iMCU_rows = (JDIMENSION)
261
jdiv_round_up((long)cinfo->_jpeg_height,
262
(long)(cinfo->max_v_samp_factor * data_unit));
263
}
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
{
278
const jpeg_scan_info *scanptr;
279
int scanno, ncomps, ci, coefi, thisi;
280
int Ss, Se, Ah, Al;
281
boolean component_sent[MAX_COMPONENTS];
282
#ifdef C_PROGRESSIVE_SUPPORTED
283
int *last_bitpos_ptr;
284
int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
285
/* -1 until that coefficient has been seen; then last Al for it */
286
#endif
287
288
if (cinfo->num_scans <= 0)
289
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
scanptr = cinfo->scan_info;
297
if (scanptr->Ss != 0 && scanptr->Se == 0) {
298
#ifdef C_LOSSLESS_SUPPORTED
299
cinfo->master->lossless = TRUE;
300
cinfo->progressive_mode = FALSE;
301
for (ci = 0; ci < cinfo->num_components; ci++)
302
component_sent[ci] = FALSE;
303
#else
304
ERREXIT(cinfo, JERR_NOT_COMPILED);
305
#endif
306
}
307
/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
308
* for progressive JPEG, no scan can have this.
309
*/
310
else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {
311
#ifdef C_PROGRESSIVE_SUPPORTED
312
cinfo->progressive_mode = TRUE;
313
cinfo->master->lossless = FALSE;
314
last_bitpos_ptr = &last_bitpos[0][0];
315
for (ci = 0; ci < cinfo->num_components; ci++)
316
for (coefi = 0; coefi < DCTSIZE2; coefi++)
317
*last_bitpos_ptr++ = -1;
318
#else
319
ERREXIT(cinfo, JERR_NOT_COMPILED);
320
#endif
321
} else {
322
cinfo->progressive_mode = cinfo->master->lossless = FALSE;
323
for (ci = 0; ci < cinfo->num_components; ci++)
324
component_sent[ci] = FALSE;
325
}
326
327
for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
328
/* Validate component indexes */
329
ncomps = scanptr->comps_in_scan;
330
if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
331
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
332
for (ci = 0; ci < ncomps; ci++) {
333
thisi = scanptr->component_index[ci];
334
if (thisi < 0 || thisi >= cinfo->num_components)
335
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
336
/* Components must appear in SOF order within each scan */
337
if (ci > 0 && thisi <= scanptr->component_index[ci - 1])
338
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
339
}
340
/* Validate progression parameters */
341
Ss = scanptr->Ss;
342
Se = scanptr->Se;
343
Ah = scanptr->Ah;
344
Al = scanptr->Al;
345
if (cinfo->progressive_mode) {
346
#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
int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;
355
356
if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
357
Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)
358
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
359
if (Ss == 0) {
360
if (Se != 0) /* DC and AC together not OK */
361
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
362
} else {
363
if (ncomps != 1) /* AC scans must be for only one component */
364
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
365
}
366
for (ci = 0; ci < ncomps; ci++) {
367
last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
368
if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
369
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
370
for (coefi = Ss; coefi <= Se; coefi++) {
371
if (last_bitpos_ptr[coefi] < 0) {
372
/* first scan of this coefficient */
373
if (Ah != 0)
374
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
375
} else {
376
/* not first scan */
377
if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)
378
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
379
}
380
last_bitpos_ptr[coefi] = Al;
381
}
382
}
383
#endif
384
} else {
385
#ifdef C_LOSSLESS_SUPPORTED
386
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
if (Ss < 1 || Ss > 7 || /* predictor selection value */
394
Se != 0 || Ah != 0 ||
395
Al < 0 || Al >= cinfo->data_precision) /* point transform */
396
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
397
} else
398
#endif
399
{
400
/* For sequential JPEG, all progression parameters must be these: */
401
if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)
402
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
403
}
404
/* Make sure components are not sent twice */
405
for (ci = 0; ci < ncomps; ci++) {
406
thisi = scanptr->component_index[ci];
407
if (component_sent[thisi])
408
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
409
component_sent[thisi] = TRUE;
410
}
411
}
412
}
413
414
/* Now verify that everything got sent. */
415
if (cinfo->progressive_mode) {
416
#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
for (ci = 0; ci < cinfo->num_components; ci++) {
423
if (last_bitpos[ci][0] < 0)
424
ERREXIT(cinfo, JERR_MISSING_DATA);
425
}
426
#endif
427
} else {
428
for (ci = 0; ci < cinfo->num_components; ci++) {
429
if (!component_sent[ci])
430
ERREXIT(cinfo, JERR_MISSING_DATA);
431
}
432
}
433
}
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
{
442
int ci;
443
444
#ifdef NEED_SCAN_SCRIPT
445
if (cinfo->scan_info != NULL) {
446
/* Prepare for current scan --- the script is already validated */
447
my_master_ptr master = (my_master_ptr)cinfo->master;
448
const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
449
450
cinfo->comps_in_scan = scanptr->comps_in_scan;
451
for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
452
cinfo->cur_comp_info[ci] =
453
&cinfo->comp_info[scanptr->component_index[ci]];
454
}
455
cinfo->Ss = scanptr->Ss;
456
cinfo->Se = scanptr->Se;
457
cinfo->Ah = scanptr->Ah;
458
cinfo->Al = scanptr->Al;
459
} else
460
#endif
461
{
462
/* Prepare for single sequential-JPEG scan containing all components */
463
if (cinfo->num_components > MAX_COMPS_IN_SCAN)
464
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
465
MAX_COMPS_IN_SCAN);
466
cinfo->comps_in_scan = cinfo->num_components;
467
for (ci = 0; ci < cinfo->num_components; ci++) {
468
cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
469
}
470
if (!cinfo->master->lossless) {
471
cinfo->Ss = 0;
472
cinfo->Se = DCTSIZE2 - 1;
473
cinfo->Ah = 0;
474
cinfo->Al = 0;
475
}
476
}
477
}
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
{
485
int ci, mcublks, tmp;
486
jpeg_component_info *compptr;
487
int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
488
489
if (cinfo->comps_in_scan == 1) {
490
491
/* Noninterleaved (single-component) scan */
492
compptr = cinfo->cur_comp_info[0];
493
494
/* Overall image size in MCUs */
495
cinfo->MCUs_per_row = compptr->width_in_blocks;
496
cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
497
498
/* For noninterleaved scan, always one block per MCU */
499
compptr->MCU_width = 1;
500
compptr->MCU_height = 1;
501
compptr->MCU_blocks = 1;
502
compptr->MCU_sample_width = data_unit;
503
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
tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
508
if (tmp == 0) tmp = compptr->v_samp_factor;
509
compptr->last_row_height = tmp;
510
511
/* Prepare array describing MCU composition */
512
cinfo->blocks_in_MCU = 1;
513
cinfo->MCU_membership[0] = 0;
514
515
} else {
516
517
/* Interleaved (multi-component) scan */
518
if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
519
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
520
MAX_COMPS_IN_SCAN);
521
522
/* Overall image size in MCUs */
523
cinfo->MCUs_per_row = (JDIMENSION)
524
jdiv_round_up((long)cinfo->_jpeg_width,
525
(long)(cinfo->max_h_samp_factor * data_unit));
526
cinfo->MCU_rows_in_scan = (JDIMENSION)
527
jdiv_round_up((long)cinfo->_jpeg_height,
528
(long)(cinfo->max_v_samp_factor * data_unit));
529
530
cinfo->blocks_in_MCU = 0;
531
532
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
533
compptr = cinfo->cur_comp_info[ci];
534
/* Sampling factors give # of blocks of component in each MCU */
535
compptr->MCU_width = compptr->h_samp_factor;
536
compptr->MCU_height = compptr->v_samp_factor;
537
compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
538
compptr->MCU_sample_width = compptr->MCU_width * data_unit;
539
/* Figure number of non-dummy blocks in last MCU column & row */
540
tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
541
if (tmp == 0) tmp = compptr->MCU_width;
542
compptr->last_col_width = tmp;
543
tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
544
if (tmp == 0) tmp = compptr->MCU_height;
545
compptr->last_row_height = tmp;
546
/* Prepare array describing MCU composition */
547
mcublks = compptr->MCU_blocks;
548
if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
549
ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
550
while (mcublks-- > 0) {
551
cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
552
}
553
}
554
555
}
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
if (cinfo->restart_in_rows > 0) {
560
long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;
561
cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);
562
}
563
}
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
{
577
my_master_ptr master = (my_master_ptr)cinfo->master;
578
579
switch (master->pass_type) {
580
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
select_scan_parameters(cinfo);
585
per_scan_setup(cinfo);
586
if (!cinfo->raw_data_in) {
587
(*cinfo->cconvert->start_pass) (cinfo);
588
(*cinfo->downsample->start_pass) (cinfo);
589
(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
590
}
591
(*cinfo->fdct->start_pass) (cinfo);
592
(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
593
(*cinfo->coef->start_pass) (cinfo,
594
(master->total_passes > 1 ?
595
JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
596
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
597
if (cinfo->optimize_coding) {
598
/* No immediate data output; postpone writing frame/scan headers */
599
master->pub.call_pass_startup = FALSE;
600
} else {
601
/* Will write frame/scan headers at first jpeg_write_scanlines call */
602
master->pub.call_pass_startup = TRUE;
603
}
604
break;
605
#ifdef ENTROPY_OPT_SUPPORTED
606
case huff_opt_pass:
607
/* Do Huffman optimization for a scan after the first one. */
608
select_scan_parameters(cinfo);
609
per_scan_setup(cinfo);
610
if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||
611
cinfo->master->lossless) {
612
(*cinfo->entropy->start_pass) (cinfo, TRUE);
613
(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
614
master->pub.call_pass_startup = FALSE;
615
break;
616
}
617
/* Special case: Huffman DC refinement scans need no Huffman table
618
* and therefore we can skip the optimization pass for them.
619
*/
620
master->pass_type = output_pass;
621
master->pass_number++;
622
#endif
623
FALLTHROUGH /*FALLTHROUGH*/
624
case output_pass:
625
/* Do a data-output pass. */
626
/* We need not repeat per-scan setup if prior optimization pass did it. */
627
if (!cinfo->optimize_coding) {
628
select_scan_parameters(cinfo);
629
per_scan_setup(cinfo);
630
}
631
(*cinfo->entropy->start_pass) (cinfo, FALSE);
632
(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
633
/* We emit frame/scan headers now */
634
if (master->scan_number == 0)
635
(*cinfo->marker->write_frame_header) (cinfo);
636
(*cinfo->marker->write_scan_header) (cinfo);
637
master->pub.call_pass_startup = FALSE;
638
break;
639
default:
640
ERREXIT(cinfo, JERR_NOT_COMPILED);
641
}
642
643
master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
644
645
/* Set up progress monitor's pass info if present */
646
if (cinfo->progress != NULL) {
647
cinfo->progress->completed_passes = master->pass_number;
648
cinfo->progress->total_passes = master->total_passes;
649
}
650
}
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
{
666
cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
667
668
(*cinfo->marker->write_frame_header) (cinfo);
669
(*cinfo->marker->write_scan_header) (cinfo);
670
}
671
672
673
/*
674
* Finish up at end of pass.
675
*/
676
677
METHODDEF(void)
678
finish_pass_master(j_compress_ptr cinfo)
679
{
680
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
(*cinfo->entropy->finish_pass) (cinfo);
686
687
/* Update state for next pass */
688
switch (master->pass_type) {
689
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
master->pass_type = output_pass;
694
if (!cinfo->optimize_coding)
695
master->scan_number++;
696
break;
697
case huff_opt_pass:
698
/* next pass is always output of current scan */
699
master->pass_type = output_pass;
700
break;
701
case output_pass:
702
/* next pass is either optimization or output of next scan */
703
if (cinfo->optimize_coding)
704
master->pass_type = huff_opt_pass;
705
master->scan_number++;
706
break;
707
}
708
709
master->pass_number++;
710
}
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
{
720
my_master_ptr master = (my_master_ptr)cinfo->master;
721
boolean empty_huff_tables = TRUE;
722
int i;
723
724
master->pub.prepare_for_pass = prepare_for_pass;
725
master->pub.pass_startup = pass_startup;
726
master->pub.finish_pass = finish_pass_master;
727
master->pub.is_last_pass = FALSE;
728
729
if (cinfo->scan_info != NULL) {
730
#ifdef NEED_SCAN_SCRIPT
731
validate_script(cinfo);
732
#else
733
ERREXIT(cinfo, JERR_NOT_COMPILED);
734
#endif
735
} else {
736
cinfo->progressive_mode = FALSE;
737
cinfo->num_scans = 1;
738
}
739
740
#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
if (cinfo->master->lossless) {
747
int ci;
748
jpeg_component_info *compptr;
749
750
cinfo->raw_data_in = FALSE;
751
cinfo->smoothing_factor = 0;
752
jpeg_default_colorspace(cinfo);
753
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
754
ci++, compptr++)
755
compptr->h_samp_factor = compptr->v_samp_factor = 1;
756
}
757
#endif
758
759
/* Validate parameters, determine derived values */
760
initial_setup(cinfo, transcode_only);
761
762
if (cinfo->arith_code)
763
cinfo->optimize_coding = FALSE;
764
else {
765
if (cinfo->master->lossless || /* TEMPORARY HACK ??? */
766
cinfo->progressive_mode)
767
cinfo->optimize_coding = TRUE; /* assume default tables no good for
768
progressive mode or lossless mode */
769
for (i = 0; i < NUM_HUFF_TBLS; i++) {
770
if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||
771
cinfo->ac_huff_tbl_ptrs[i] != NULL) {
772
empty_huff_tables = FALSE;
773
break;
774
}
775
}
776
if (cinfo->data_precision == 12 && !cinfo->optimize_coding &&
777
(empty_huff_tables || using_std_huff_tables(cinfo)))
778
cinfo->optimize_coding = TRUE; /* assume default tables no good for
779
12-bit data precision */
780
}
781
782
/* Initialize my private state */
783
if (transcode_only) {
784
/* no main pass in transcoding */
785
if (cinfo->optimize_coding)
786
master->pass_type = huff_opt_pass;
787
else
788
master->pass_type = output_pass;
789
} else {
790
/* for normal compression, first pass is always this type: */
791
master->pass_type = main_pass;
792
}
793
master->scan_number = 0;
794
master->pass_number = 0;
795
if (cinfo->optimize_coding)
796
master->total_passes = cinfo->num_scans * 2;
797
else
798
master->total_passes = cinfo->num_scans;
799
800
master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
801
}
802
803