Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libjpeg-turbo/src/jdmaster.c
9904 views
1
/*
2
* jdmaster.c
3
*
4
* This file was part of the Independent JPEG Group's software:
5
* Copyright (C) 1991-1997, Thomas G. Lane.
6
* Modified 2002-2009 by Guido Vollbeding.
7
* Lossless JPEG Modifications:
8
* Copyright (C) 1999, Ken Murchison.
9
* libjpeg-turbo Modifications:
10
* Copyright (C) 2009-2011, 2016, 2019, 2022-2024, D. R. Commander.
11
* Copyright (C) 2013, Linaro Limited.
12
* Copyright (C) 2015, Google, Inc.
13
* For conditions of distribution and use, see the accompanying README.ijg
14
* file.
15
*
16
* This file contains master control logic for the JPEG decompressor.
17
* These routines are concerned with selecting the modules to be executed
18
* and with determining the number of passes and the work to be done in each
19
* pass.
20
*/
21
22
#define JPEG_INTERNALS
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
#include "jpegapicomp.h"
26
#include "jdmaster.h"
27
28
29
/*
30
* Determine whether merged upsample/color conversion should be used.
31
* CRUCIAL: this must match the actual capabilities of jdmerge.c!
32
*/
33
34
LOCAL(boolean)
35
use_merged_upsample(j_decompress_ptr cinfo)
36
{
37
#ifdef UPSAMPLE_MERGING_SUPPORTED
38
/* Colorspace conversion is not supported with lossless JPEG images */
39
if (cinfo->master->lossless)
40
return FALSE;
41
/* Merging is the equivalent of plain box-filter upsampling */
42
if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
43
return FALSE;
44
/* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
45
if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
46
(cinfo->out_color_space != JCS_RGB &&
47
cinfo->out_color_space != JCS_RGB565 &&
48
cinfo->out_color_space != JCS_EXT_RGB &&
49
cinfo->out_color_space != JCS_EXT_RGBX &&
50
cinfo->out_color_space != JCS_EXT_BGR &&
51
cinfo->out_color_space != JCS_EXT_BGRX &&
52
cinfo->out_color_space != JCS_EXT_XBGR &&
53
cinfo->out_color_space != JCS_EXT_XRGB &&
54
cinfo->out_color_space != JCS_EXT_RGBA &&
55
cinfo->out_color_space != JCS_EXT_BGRA &&
56
cinfo->out_color_space != JCS_EXT_ABGR &&
57
cinfo->out_color_space != JCS_EXT_ARGB))
58
return FALSE;
59
if ((cinfo->out_color_space == JCS_RGB565 &&
60
cinfo->out_color_components != 3) ||
61
(cinfo->out_color_space != JCS_RGB565 &&
62
cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
63
return FALSE;
64
/* and it only handles 2h1v or 2h2v sampling ratios */
65
if (cinfo->comp_info[0].h_samp_factor != 2 ||
66
cinfo->comp_info[1].h_samp_factor != 1 ||
67
cinfo->comp_info[2].h_samp_factor != 1 ||
68
cinfo->comp_info[0].v_samp_factor > 2 ||
69
cinfo->comp_info[1].v_samp_factor != 1 ||
70
cinfo->comp_info[2].v_samp_factor != 1)
71
return FALSE;
72
/* furthermore, it doesn't work if we've scaled the IDCTs differently */
73
if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
74
cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
75
cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
76
return FALSE;
77
/* ??? also need to test for upsample-time rescaling, when & if supported */
78
return TRUE; /* by golly, it'll work... */
79
#else
80
return FALSE;
81
#endif
82
}
83
84
85
/*
86
* Compute output image dimensions and related values.
87
* NOTE: this is exported for possible use by application.
88
* Hence it mustn't do anything that can't be done twice.
89
*/
90
91
#if JPEG_LIB_VERSION >= 80
92
GLOBAL(void)
93
#else
94
LOCAL(void)
95
#endif
96
jpeg_core_output_dimensions(j_decompress_ptr cinfo)
97
/* Do computations that are needed before master selection phase.
98
* This function is used for transcoding and full decompression.
99
*/
100
{
101
#ifdef IDCT_SCALING_SUPPORTED
102
int ci;
103
jpeg_component_info *compptr;
104
105
if (!cinfo->master->lossless) {
106
/* Compute actual output image dimensions and DCT scaling choices. */
107
if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
108
/* Provide 1/block_size scaling */
109
cinfo->output_width = (JDIMENSION)
110
jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
111
cinfo->output_height = (JDIMENSION)
112
jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
113
cinfo->_min_DCT_h_scaled_size = 1;
114
cinfo->_min_DCT_v_scaled_size = 1;
115
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
116
/* Provide 2/block_size scaling */
117
cinfo->output_width = (JDIMENSION)
118
jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
119
cinfo->output_height = (JDIMENSION)
120
jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
121
cinfo->_min_DCT_h_scaled_size = 2;
122
cinfo->_min_DCT_v_scaled_size = 2;
123
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
124
/* Provide 3/block_size scaling */
125
cinfo->output_width = (JDIMENSION)
126
jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
127
cinfo->output_height = (JDIMENSION)
128
jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
129
cinfo->_min_DCT_h_scaled_size = 3;
130
cinfo->_min_DCT_v_scaled_size = 3;
131
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
132
/* Provide 4/block_size scaling */
133
cinfo->output_width = (JDIMENSION)
134
jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
135
cinfo->output_height = (JDIMENSION)
136
jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
137
cinfo->_min_DCT_h_scaled_size = 4;
138
cinfo->_min_DCT_v_scaled_size = 4;
139
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
140
/* Provide 5/block_size scaling */
141
cinfo->output_width = (JDIMENSION)
142
jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
143
cinfo->output_height = (JDIMENSION)
144
jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
145
cinfo->_min_DCT_h_scaled_size = 5;
146
cinfo->_min_DCT_v_scaled_size = 5;
147
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
148
/* Provide 6/block_size scaling */
149
cinfo->output_width = (JDIMENSION)
150
jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
151
cinfo->output_height = (JDIMENSION)
152
jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
153
cinfo->_min_DCT_h_scaled_size = 6;
154
cinfo->_min_DCT_v_scaled_size = 6;
155
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
156
/* Provide 7/block_size scaling */
157
cinfo->output_width = (JDIMENSION)
158
jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
159
cinfo->output_height = (JDIMENSION)
160
jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
161
cinfo->_min_DCT_h_scaled_size = 7;
162
cinfo->_min_DCT_v_scaled_size = 7;
163
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
164
/* Provide 8/block_size scaling */
165
cinfo->output_width = (JDIMENSION)
166
jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
167
cinfo->output_height = (JDIMENSION)
168
jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
169
cinfo->_min_DCT_h_scaled_size = 8;
170
cinfo->_min_DCT_v_scaled_size = 8;
171
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
172
/* Provide 9/block_size scaling */
173
cinfo->output_width = (JDIMENSION)
174
jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
175
cinfo->output_height = (JDIMENSION)
176
jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
177
cinfo->_min_DCT_h_scaled_size = 9;
178
cinfo->_min_DCT_v_scaled_size = 9;
179
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
180
/* Provide 10/block_size scaling */
181
cinfo->output_width = (JDIMENSION)
182
jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
183
cinfo->output_height = (JDIMENSION)
184
jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
185
cinfo->_min_DCT_h_scaled_size = 10;
186
cinfo->_min_DCT_v_scaled_size = 10;
187
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
188
/* Provide 11/block_size scaling */
189
cinfo->output_width = (JDIMENSION)
190
jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
191
cinfo->output_height = (JDIMENSION)
192
jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
193
cinfo->_min_DCT_h_scaled_size = 11;
194
cinfo->_min_DCT_v_scaled_size = 11;
195
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
196
/* Provide 12/block_size scaling */
197
cinfo->output_width = (JDIMENSION)
198
jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
199
cinfo->output_height = (JDIMENSION)
200
jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
201
cinfo->_min_DCT_h_scaled_size = 12;
202
cinfo->_min_DCT_v_scaled_size = 12;
203
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
204
/* Provide 13/block_size scaling */
205
cinfo->output_width = (JDIMENSION)
206
jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
207
cinfo->output_height = (JDIMENSION)
208
jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
209
cinfo->_min_DCT_h_scaled_size = 13;
210
cinfo->_min_DCT_v_scaled_size = 13;
211
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
212
/* Provide 14/block_size scaling */
213
cinfo->output_width = (JDIMENSION)
214
jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
215
cinfo->output_height = (JDIMENSION)
216
jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
217
cinfo->_min_DCT_h_scaled_size = 14;
218
cinfo->_min_DCT_v_scaled_size = 14;
219
} else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
220
/* Provide 15/block_size scaling */
221
cinfo->output_width = (JDIMENSION)
222
jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
223
cinfo->output_height = (JDIMENSION)
224
jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
225
cinfo->_min_DCT_h_scaled_size = 15;
226
cinfo->_min_DCT_v_scaled_size = 15;
227
} else {
228
/* Provide 16/block_size scaling */
229
cinfo->output_width = (JDIMENSION)
230
jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
231
cinfo->output_height = (JDIMENSION)
232
jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
233
cinfo->_min_DCT_h_scaled_size = 16;
234
cinfo->_min_DCT_v_scaled_size = 16;
235
}
236
237
/* Recompute dimensions of components */
238
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
239
ci++, compptr++) {
240
compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
241
compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
242
}
243
} else
244
#endif /* !IDCT_SCALING_SUPPORTED */
245
{
246
/* Hardwire it to "no scaling" */
247
cinfo->output_width = cinfo->image_width;
248
cinfo->output_height = cinfo->image_height;
249
/* jdinput.c has already initialized DCT_scaled_size,
250
* and has computed unscaled downsampled_width and downsampled_height.
251
*/
252
}
253
}
254
255
256
/*
257
* Compute output image dimensions and related values.
258
* NOTE: this is exported for possible use by application.
259
* Hence it mustn't do anything that can't be done twice.
260
* Also note that it may be called before the master module is initialized!
261
*/
262
263
GLOBAL(void)
264
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
265
/* Do computations that are needed before master selection phase */
266
{
267
#ifdef IDCT_SCALING_SUPPORTED
268
int ci;
269
jpeg_component_info *compptr;
270
#endif
271
272
/* Prevent application from calling me at wrong times */
273
if (cinfo->global_state != DSTATE_READY)
274
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
275
276
/* Compute core output image dimensions and DCT scaling choices. */
277
jpeg_core_output_dimensions(cinfo);
278
279
#ifdef IDCT_SCALING_SUPPORTED
280
281
if (!cinfo->master->lossless) {
282
/* In selecting the actual DCT scaling for each component, we try to
283
* scale up the chroma components via IDCT scaling rather than upsampling.
284
* This saves time if the upsampler gets to use 1:1 scaling.
285
* Note this code adapts subsampling ratios which are powers of 2.
286
*/
287
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
288
ci++, compptr++) {
289
int ssize = cinfo->_min_DCT_scaled_size;
290
while (ssize < DCTSIZE &&
291
((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
292
(compptr->h_samp_factor * ssize * 2) == 0) &&
293
((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
294
(compptr->v_samp_factor * ssize * 2) == 0)) {
295
ssize = ssize * 2;
296
}
297
#if JPEG_LIB_VERSION >= 70
298
compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
299
#else
300
compptr->DCT_scaled_size = ssize;
301
#endif
302
}
303
304
/* Recompute downsampled dimensions of components;
305
* application needs to know these if using raw downsampled data.
306
*/
307
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
308
ci++, compptr++) {
309
/* Size in samples, after IDCT scaling */
310
compptr->downsampled_width = (JDIMENSION)
311
jdiv_round_up((long)cinfo->image_width *
312
(long)(compptr->h_samp_factor *
313
compptr->_DCT_scaled_size),
314
(long)(cinfo->max_h_samp_factor * DCTSIZE));
315
compptr->downsampled_height = (JDIMENSION)
316
jdiv_round_up((long)cinfo->image_height *
317
(long)(compptr->v_samp_factor *
318
compptr->_DCT_scaled_size),
319
(long)(cinfo->max_v_samp_factor * DCTSIZE));
320
}
321
} else
322
#endif /* IDCT_SCALING_SUPPORTED */
323
{
324
/* Hardwire it to "no scaling" */
325
cinfo->output_width = cinfo->image_width;
326
cinfo->output_height = cinfo->image_height;
327
/* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
328
* and has computed unscaled downsampled_width and downsampled_height.
329
*/
330
}
331
332
/* Report number of components in selected colorspace. */
333
/* Probably this should be in the color conversion module... */
334
switch (cinfo->out_color_space) {
335
case JCS_GRAYSCALE:
336
cinfo->out_color_components = 1;
337
break;
338
case JCS_RGB:
339
case JCS_EXT_RGB:
340
case JCS_EXT_RGBX:
341
case JCS_EXT_BGR:
342
case JCS_EXT_BGRX:
343
case JCS_EXT_XBGR:
344
case JCS_EXT_XRGB:
345
case JCS_EXT_RGBA:
346
case JCS_EXT_BGRA:
347
case JCS_EXT_ABGR:
348
case JCS_EXT_ARGB:
349
cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
350
break;
351
case JCS_YCbCr:
352
case JCS_RGB565:
353
cinfo->out_color_components = 3;
354
break;
355
case JCS_CMYK:
356
case JCS_YCCK:
357
cinfo->out_color_components = 4;
358
break;
359
default: /* else must be same colorspace as in file */
360
cinfo->out_color_components = cinfo->num_components;
361
break;
362
}
363
cinfo->output_components = (cinfo->quantize_colors ? 1 :
364
cinfo->out_color_components);
365
366
/* See if upsampler will want to emit more than one row at a time */
367
if (use_merged_upsample(cinfo))
368
cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
369
else
370
cinfo->rec_outbuf_height = 1;
371
}
372
373
374
/*
375
* Several decompression processes need to range-limit values to the range
376
* 0..MAXJSAMPLE; the input value may fall somewhat outside this range
377
* due to noise introduced by quantization, roundoff error, etc. These
378
* processes are inner loops and need to be as fast as possible. On most
379
* machines, particularly CPUs with pipelines or instruction prefetch,
380
* a (subscript-check-less) C table lookup
381
* x = sample_range_limit[x];
382
* is faster than explicit tests
383
* if (x < 0) x = 0;
384
* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
385
* These processes all use a common table prepared by the routine below.
386
*
387
* For most steps we can mathematically guarantee that the initial value
388
* of x is within MAXJSAMPLE+1 of the legal range, so a table running from
389
* -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
390
* limiting step (just after the IDCT), a wildly out-of-range value is
391
* possible if the input data is corrupt. To avoid any chance of indexing
392
* off the end of memory and getting a bad-pointer trap, we perform the
393
* post-IDCT limiting thus:
394
* x = range_limit[x & MASK];
395
* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
396
* samples. Under normal circumstances this is more than enough range and
397
* a correct output will be generated; with bogus input data the mask will
398
* cause wraparound, and we will safely generate a bogus-but-in-range output.
399
* For the post-IDCT step, we want to convert the data from signed to unsigned
400
* representation by adding CENTERJSAMPLE at the same time that we limit it.
401
* So the post-IDCT limiting table ends up looking like this:
402
* CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
403
* MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
404
* 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
405
* 0,1,...,CENTERJSAMPLE-1
406
* Negative inputs select values from the upper half of the table after
407
* masking.
408
*
409
* We can save some space by overlapping the start of the post-IDCT table
410
* with the simpler range limiting table. The post-IDCT table begins at
411
* sample_range_limit + CENTERJSAMPLE.
412
*/
413
414
LOCAL(void)
415
prepare_range_limit_table(j_decompress_ptr cinfo)
416
/* Allocate and fill in the sample_range_limit table */
417
{
418
JSAMPLE *table;
419
J12SAMPLE *table12;
420
#ifdef D_LOSSLESS_SUPPORTED
421
J16SAMPLE *table16;
422
#endif
423
int i;
424
425
if (cinfo->data_precision <= 8) {
426
table = (JSAMPLE *)
427
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
428
(5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
429
table += (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */
430
cinfo->sample_range_limit = table;
431
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
432
memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
433
/* Main part of "simple" table: limit[x] = x */
434
for (i = 0; i <= MAXJSAMPLE; i++)
435
table[i] = (JSAMPLE)i;
436
table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
437
/* End of simple table, rest of first half of post-IDCT table */
438
for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
439
table[i] = MAXJSAMPLE;
440
/* Second half of post-IDCT table */
441
memset(table + (2 * (MAXJSAMPLE + 1)), 0,
442
(2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
443
memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
444
cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
445
} else if (cinfo->data_precision <= 12) {
446
table12 = (J12SAMPLE *)
447
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
448
(5 * (MAXJ12SAMPLE + 1) + CENTERJ12SAMPLE) *
449
sizeof(J12SAMPLE));
450
table12 += (MAXJ12SAMPLE + 1); /* allow negative subscripts of simple
451
table */
452
cinfo->sample_range_limit = (JSAMPLE *)table12;
453
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
454
memset(table12 - (MAXJ12SAMPLE + 1), 0,
455
(MAXJ12SAMPLE + 1) * sizeof(J12SAMPLE));
456
/* Main part of "simple" table: limit[x] = x */
457
for (i = 0; i <= MAXJ12SAMPLE; i++)
458
table12[i] = (J12SAMPLE)i;
459
table12 += CENTERJ12SAMPLE; /* Point to where post-IDCT table starts */
460
/* End of simple table, rest of first half of post-IDCT table */
461
for (i = CENTERJ12SAMPLE; i < 2 * (MAXJ12SAMPLE + 1); i++)
462
table12[i] = MAXJ12SAMPLE;
463
/* Second half of post-IDCT table */
464
memset(table12 + (2 * (MAXJ12SAMPLE + 1)), 0,
465
(2 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE) * sizeof(J12SAMPLE));
466
memcpy(table12 + (4 * (MAXJ12SAMPLE + 1) - CENTERJ12SAMPLE),
467
cinfo->sample_range_limit, CENTERJ12SAMPLE * sizeof(J12SAMPLE));
468
} else {
469
#ifdef D_LOSSLESS_SUPPORTED
470
table16 = (J16SAMPLE *)
471
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
472
(5 * (MAXJ16SAMPLE + 1) + CENTERJ16SAMPLE) *
473
sizeof(J16SAMPLE));
474
table16 += (MAXJ16SAMPLE + 1); /* allow negative subscripts of simple
475
table */
476
cinfo->sample_range_limit = (JSAMPLE *)table16;
477
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
478
memset(table16 - (MAXJ16SAMPLE + 1), 0,
479
(MAXJ16SAMPLE + 1) * sizeof(J16SAMPLE));
480
/* Main part of "simple" table: limit[x] = x */
481
for (i = 0; i <= MAXJ16SAMPLE; i++)
482
table16[i] = (J16SAMPLE)i;
483
table16 += CENTERJ16SAMPLE; /* Point to where post-IDCT table starts */
484
/* End of simple table, rest of first half of post-IDCT table */
485
for (i = CENTERJ16SAMPLE; i < 2 * (MAXJ16SAMPLE + 1); i++)
486
table16[i] = MAXJ16SAMPLE;
487
/* Second half of post-IDCT table */
488
memset(table16 + (2 * (MAXJ16SAMPLE + 1)), 0,
489
(2 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE) * sizeof(J16SAMPLE));
490
memcpy(table16 + (4 * (MAXJ16SAMPLE + 1) - CENTERJ16SAMPLE),
491
cinfo->sample_range_limit, CENTERJ16SAMPLE * sizeof(J16SAMPLE));
492
#else
493
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
494
#endif
495
}
496
}
497
498
499
/*
500
* Master selection of decompression modules.
501
* This is done once at jpeg_start_decompress time. We determine
502
* which modules will be used and give them appropriate initialization calls.
503
* We also initialize the decompressor input side to begin consuming data.
504
*
505
* Since jpeg_read_header has finished, we know what is in the SOF
506
* and (first) SOS markers. We also have all the application parameter
507
* settings.
508
*/
509
510
LOCAL(void)
511
master_selection(j_decompress_ptr cinfo)
512
{
513
my_master_ptr master = (my_master_ptr)cinfo->master;
514
boolean use_c_buffer;
515
long samplesperrow;
516
JDIMENSION jd_samplesperrow;
517
518
/* Disable IDCT scaling and raw (downsampled) data output in lossless mode.
519
* IDCT scaling is not useful in lossless mode, and it must be disabled in
520
* order to properly calculate the output dimensions. Raw data output isn't
521
* particularly useful without subsampling and has not been tested in
522
* lossless mode.
523
*/
524
#ifdef D_LOSSLESS_SUPPORTED
525
if (cinfo->master->lossless) {
526
cinfo->raw_data_out = FALSE;
527
cinfo->scale_num = cinfo->scale_denom = 1;
528
}
529
#endif
530
531
/* Initialize dimensions and other stuff */
532
jpeg_calc_output_dimensions(cinfo);
533
prepare_range_limit_table(cinfo);
534
535
/* Width of an output scanline must be representable as JDIMENSION. */
536
samplesperrow = (long)cinfo->output_width *
537
(long)cinfo->out_color_components;
538
jd_samplesperrow = (JDIMENSION)samplesperrow;
539
if ((long)jd_samplesperrow != samplesperrow)
540
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
541
542
/* Initialize my private state */
543
master->pass_number = 0;
544
master->using_merged_upsample = use_merged_upsample(cinfo);
545
546
/* Color quantizer selection */
547
master->quantizer_1pass = NULL;
548
master->quantizer_2pass = NULL;
549
/* No mode changes if not using buffered-image mode. */
550
if (!cinfo->quantize_colors || !cinfo->buffered_image) {
551
cinfo->enable_1pass_quant = FALSE;
552
cinfo->enable_external_quant = FALSE;
553
cinfo->enable_2pass_quant = FALSE;
554
}
555
if (cinfo->quantize_colors) {
556
if (cinfo->raw_data_out)
557
ERREXIT(cinfo, JERR_NOTIMPL);
558
/* 2-pass quantizer only works in 3-component color space. */
559
if (cinfo->out_color_components != 3 ||
560
cinfo->out_color_space == JCS_RGB565) {
561
cinfo->enable_1pass_quant = TRUE;
562
cinfo->enable_external_quant = FALSE;
563
cinfo->enable_2pass_quant = FALSE;
564
cinfo->colormap = NULL;
565
} else if (cinfo->colormap != NULL) {
566
cinfo->enable_external_quant = TRUE;
567
} else if (cinfo->two_pass_quantize) {
568
cinfo->enable_2pass_quant = TRUE;
569
} else {
570
cinfo->enable_1pass_quant = TRUE;
571
}
572
573
if (cinfo->enable_1pass_quant) {
574
#ifdef QUANT_1PASS_SUPPORTED
575
if (cinfo->data_precision == 8)
576
jinit_1pass_quantizer(cinfo);
577
else if (cinfo->data_precision == 12)
578
j12init_1pass_quantizer(cinfo);
579
else
580
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
581
master->quantizer_1pass = cinfo->cquantize;
582
#else
583
ERREXIT(cinfo, JERR_NOT_COMPILED);
584
#endif
585
}
586
587
/* We use the 2-pass code to map to external colormaps. */
588
if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
589
#ifdef QUANT_2PASS_SUPPORTED
590
if (cinfo->data_precision == 8)
591
jinit_2pass_quantizer(cinfo);
592
else if (cinfo->data_precision == 12)
593
j12init_2pass_quantizer(cinfo);
594
else
595
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
596
master->quantizer_2pass = cinfo->cquantize;
597
#else
598
ERREXIT(cinfo, JERR_NOT_COMPILED);
599
#endif
600
}
601
/* If both quantizers are initialized, the 2-pass one is left active;
602
* this is necessary for starting with quantization to an external map.
603
*/
604
}
605
606
/* Post-processing: in particular, color conversion first */
607
if (!cinfo->raw_data_out) {
608
if (master->using_merged_upsample) {
609
#ifdef UPSAMPLE_MERGING_SUPPORTED
610
if (cinfo->data_precision == 8)
611
jinit_merged_upsampler(cinfo); /* does color conversion too */
612
else if (cinfo->data_precision == 12)
613
j12init_merged_upsampler(cinfo); /* does color conversion too */
614
else
615
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
616
#else
617
ERREXIT(cinfo, JERR_NOT_COMPILED);
618
#endif
619
} else {
620
if (cinfo->data_precision <= 8) {
621
jinit_color_deconverter(cinfo);
622
jinit_upsampler(cinfo);
623
} else if (cinfo->data_precision <= 12) {
624
j12init_color_deconverter(cinfo);
625
j12init_upsampler(cinfo);
626
} else {
627
#ifdef D_LOSSLESS_SUPPORTED
628
j16init_color_deconverter(cinfo);
629
j16init_upsampler(cinfo);
630
#else
631
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
632
#endif
633
}
634
}
635
if (cinfo->data_precision <= 8)
636
jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
637
else if (cinfo->data_precision <= 12)
638
j12init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
639
else
640
#ifdef D_LOSSLESS_SUPPORTED
641
j16init_d_post_controller(cinfo, cinfo->enable_2pass_quant);
642
#else
643
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
644
#endif
645
}
646
647
if (cinfo->master->lossless) {
648
#ifdef D_LOSSLESS_SUPPORTED
649
/* Prediction, sample undifferencing, point transform, and sample size
650
* scaling
651
*/
652
if (cinfo->data_precision <= 8)
653
jinit_lossless_decompressor(cinfo);
654
else if (cinfo->data_precision <= 12)
655
j12init_lossless_decompressor(cinfo);
656
else
657
j16init_lossless_decompressor(cinfo);
658
/* Entropy decoding: either Huffman or arithmetic coding. */
659
if (cinfo->arith_code) {
660
ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
661
} else {
662
jinit_lhuff_decoder(cinfo);
663
}
664
665
/* Initialize principal buffer controllers. */
666
use_c_buffer = cinfo->inputctl->has_multiple_scans ||
667
cinfo->buffered_image;
668
if (cinfo->data_precision <= 8)
669
jinit_d_diff_controller(cinfo, use_c_buffer);
670
else if (cinfo->data_precision <= 12)
671
j12init_d_diff_controller(cinfo, use_c_buffer);
672
else
673
j16init_d_diff_controller(cinfo, use_c_buffer);
674
#else
675
ERREXIT(cinfo, JERR_NOT_COMPILED);
676
#endif
677
} else {
678
/* Inverse DCT */
679
if (cinfo->data_precision == 8)
680
jinit_inverse_dct(cinfo);
681
else if (cinfo->data_precision == 12)
682
j12init_inverse_dct(cinfo);
683
else
684
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
685
/* Entropy decoding: either Huffman or arithmetic coding. */
686
if (cinfo->arith_code) {
687
#ifdef D_ARITH_CODING_SUPPORTED
688
jinit_arith_decoder(cinfo);
689
#else
690
ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
691
#endif
692
} else {
693
if (cinfo->progressive_mode) {
694
#ifdef D_PROGRESSIVE_SUPPORTED
695
jinit_phuff_decoder(cinfo);
696
#else
697
ERREXIT(cinfo, JERR_NOT_COMPILED);
698
#endif
699
} else
700
jinit_huff_decoder(cinfo);
701
}
702
703
/* Initialize principal buffer controllers. */
704
use_c_buffer = cinfo->inputctl->has_multiple_scans ||
705
cinfo->buffered_image;
706
if (cinfo->data_precision == 12)
707
j12init_d_coef_controller(cinfo, use_c_buffer);
708
else
709
jinit_d_coef_controller(cinfo, use_c_buffer);
710
}
711
712
if (!cinfo->raw_data_out) {
713
if (cinfo->data_precision <= 8)
714
jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
715
else if (cinfo->data_precision <= 12)
716
j12init_d_main_controller(cinfo,
717
FALSE /* never need full buffer here */);
718
else
719
#ifdef D_LOSSLESS_SUPPORTED
720
j16init_d_main_controller(cinfo,
721
FALSE /* never need full buffer here */);
722
#else
723
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
724
#endif
725
}
726
727
/* We can now tell the memory manager to allocate virtual arrays. */
728
(*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
729
730
/* Initialize input side of decompressor to consume first scan. */
731
(*cinfo->inputctl->start_input_pass) (cinfo);
732
733
/* Set the first and last iMCU columns to decompress from single-scan images.
734
* By default, decompress all of the iMCU columns.
735
*/
736
cinfo->master->first_iMCU_col = 0;
737
cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
738
cinfo->master->last_good_iMCU_row = 0;
739
740
#ifdef D_MULTISCAN_FILES_SUPPORTED
741
/* If jpeg_start_decompress will read the whole file, initialize
742
* progress monitoring appropriately. The input step is counted
743
* as one pass.
744
*/
745
if (cinfo->progress != NULL && !cinfo->buffered_image &&
746
cinfo->inputctl->has_multiple_scans) {
747
int nscans;
748
/* Estimate number of scans to set pass_limit. */
749
if (cinfo->progressive_mode) {
750
/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
751
nscans = 2 + 3 * cinfo->num_components;
752
} else {
753
/* For a nonprogressive multiscan file, estimate 1 scan per component. */
754
nscans = cinfo->num_components;
755
}
756
cinfo->progress->pass_counter = 0L;
757
cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
758
cinfo->progress->completed_passes = 0;
759
cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
760
/* Count the input pass as done */
761
master->pass_number++;
762
}
763
#endif /* D_MULTISCAN_FILES_SUPPORTED */
764
}
765
766
767
/*
768
* Per-pass setup.
769
* This is called at the beginning of each output pass. We determine which
770
* modules will be active during this pass and give them appropriate
771
* start_pass calls. We also set is_dummy_pass to indicate whether this
772
* is a "real" output pass or a dummy pass for color quantization.
773
* (In the latter case, jdapistd.c will crank the pass to completion.)
774
*/
775
776
METHODDEF(void)
777
prepare_for_output_pass(j_decompress_ptr cinfo)
778
{
779
my_master_ptr master = (my_master_ptr)cinfo->master;
780
781
if (master->pub.is_dummy_pass) {
782
#ifdef QUANT_2PASS_SUPPORTED
783
/* Final pass of 2-pass quantization */
784
master->pub.is_dummy_pass = FALSE;
785
(*cinfo->cquantize->start_pass) (cinfo, FALSE);
786
(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
787
(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
788
#else
789
ERREXIT(cinfo, JERR_NOT_COMPILED);
790
#endif /* QUANT_2PASS_SUPPORTED */
791
} else {
792
if (cinfo->quantize_colors && cinfo->colormap == NULL) {
793
/* Select new quantization method */
794
if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
795
cinfo->cquantize = master->quantizer_2pass;
796
master->pub.is_dummy_pass = TRUE;
797
} else if (cinfo->enable_1pass_quant) {
798
cinfo->cquantize = master->quantizer_1pass;
799
} else {
800
ERREXIT(cinfo, JERR_MODE_CHANGE);
801
}
802
}
803
(*cinfo->idct->start_pass) (cinfo);
804
(*cinfo->coef->start_output_pass) (cinfo);
805
if (!cinfo->raw_data_out) {
806
if (!master->using_merged_upsample)
807
(*cinfo->cconvert->start_pass) (cinfo);
808
(*cinfo->upsample->start_pass) (cinfo);
809
if (cinfo->quantize_colors)
810
(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
811
(*cinfo->post->start_pass) (cinfo,
812
(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
813
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
814
}
815
}
816
817
/* Set up progress monitor's pass info if present */
818
if (cinfo->progress != NULL) {
819
cinfo->progress->completed_passes = master->pass_number;
820
cinfo->progress->total_passes = master->pass_number +
821
(master->pub.is_dummy_pass ? 2 : 1);
822
/* In buffered-image mode, we assume one more output pass if EOI not
823
* yet reached, but no more passes if EOI has been reached.
824
*/
825
if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
826
cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
827
}
828
}
829
}
830
831
832
/*
833
* Finish up at end of an output pass.
834
*/
835
836
METHODDEF(void)
837
finish_output_pass(j_decompress_ptr cinfo)
838
{
839
my_master_ptr master = (my_master_ptr)cinfo->master;
840
841
if (cinfo->quantize_colors)
842
(*cinfo->cquantize->finish_pass) (cinfo);
843
master->pass_number++;
844
}
845
846
847
#ifdef D_MULTISCAN_FILES_SUPPORTED
848
849
/*
850
* Switch to a new external colormap between output passes.
851
*/
852
853
GLOBAL(void)
854
jpeg_new_colormap(j_decompress_ptr cinfo)
855
{
856
my_master_ptr master = (my_master_ptr)cinfo->master;
857
858
/* Prevent application from calling me at wrong times */
859
if (cinfo->global_state != DSTATE_BUFIMAGE)
860
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
861
862
if (cinfo->quantize_colors && cinfo->enable_external_quant &&
863
cinfo->colormap != NULL) {
864
/* Select 2-pass quantizer for external colormap use */
865
cinfo->cquantize = master->quantizer_2pass;
866
/* Notify quantizer of colormap change */
867
(*cinfo->cquantize->new_color_map) (cinfo);
868
master->pub.is_dummy_pass = FALSE; /* just in case */
869
} else
870
ERREXIT(cinfo, JERR_MODE_CHANGE);
871
}
872
873
#endif /* D_MULTISCAN_FILES_SUPPORTED */
874
875
876
/*
877
* Initialize master decompression control and select active modules.
878
* This is performed at the start of jpeg_start_decompress.
879
*/
880
881
GLOBAL(void)
882
jinit_master_decompress(j_decompress_ptr cinfo)
883
{
884
my_master_ptr master = (my_master_ptr)cinfo->master;
885
886
master->pub.prepare_for_output_pass = prepare_for_output_pass;
887
master->pub.finish_output_pass = finish_output_pass;
888
889
master->pub.is_dummy_pass = FALSE;
890
master->pub.jinit_upsampler_no_alloc = FALSE;
891
892
master_selection(cinfo);
893
}
894
895