Coverage Report

Created: 2025-10-10 07:05

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