Coverage Report

Created: 2025-06-24 07:01

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