Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.main/jcmaster.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcmaster.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2003-2010 by Guido Vollbeding.
7
 * Lossless JPEG Modifications:
8
 * Copyright (C) 1999, Ken Murchison.
9
 * libjpeg-turbo Modifications:
10
 * Copyright (C) 2010, 2016, 2018, 2022-2023, 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(void)
54
initial_setup(j_compress_ptr cinfo, boolean transcode_only)
55
/* Do computations that are needed before master selection phase */
56
12.9k
{
57
12.9k
  int ci;
58
12.9k
  jpeg_component_info *compptr;
59
12.9k
  long samplesperrow;
60
12.9k
  JDIMENSION jd_samplesperrow;
61
12.9k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
62
63
#if JPEG_LIB_VERSION >= 70
64
#if JPEG_LIB_VERSION >= 80
65
  if (!transcode_only)
66
#endif
67
    jpeg_calc_jpeg_dimensions(cinfo);
68
#endif
69
70
  /* Sanity check on image dimensions */
71
12.9k
  if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
72
12.9k
      cinfo->num_components <= 0 || cinfo->input_components <= 0)
73
0
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
74
75
  /* Make sure image isn't bigger than I can handle */
76
12.9k
  if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||
77
12.9k
      (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)
78
42
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
79
80
  /* Width of an input scanline must be representable as JDIMENSION. */
81
12.9k
  samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;
82
12.9k
  jd_samplesperrow = (JDIMENSION)samplesperrow;
83
12.9k
  if ((long)jd_samplesperrow != samplesperrow)
84
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
85
86
12.9k
#ifdef C_LOSSLESS_SUPPORTED
87
12.9k
  if (cinfo->data_precision != 8 && cinfo->data_precision != 12 &&
88
12.9k
      cinfo->data_precision != 16)
89
#else
90
  if (cinfo->data_precision != 8 && cinfo->data_precision != 12)
91
#endif
92
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
93
94
  /* Check that number of components won't exceed internal array sizes */
95
12.9k
  if (cinfo->num_components > MAX_COMPONENTS)
96
0
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
97
12.9k
             MAX_COMPONENTS);
98
99
  /* Compute maximum sampling factors; check factor validity */
100
12.9k
  cinfo->max_h_samp_factor = 1;
101
12.9k
  cinfo->max_v_samp_factor = 1;
102
46.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
103
33.6k
       ci++, compptr++) {
104
33.6k
    if (compptr->h_samp_factor <= 0 ||
105
33.6k
        compptr->h_samp_factor > MAX_SAMP_FACTOR ||
106
33.6k
        compptr->v_samp_factor <= 0 ||
107
33.6k
        compptr->v_samp_factor > MAX_SAMP_FACTOR)
108
0
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
109
33.6k
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
110
33.6k
                                   compptr->h_samp_factor);
111
33.6k
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
112
33.6k
                                   compptr->v_samp_factor);
113
33.6k
  }
114
115
  /* Compute dimensions of components */
116
46.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
117
33.6k
       ci++, compptr++) {
118
    /* Fill in the correct component_index value; don't rely on application */
119
33.6k
    compptr->component_index = ci;
120
    /* For compression, we never do DCT scaling. */
121
#if JPEG_LIB_VERSION >= 70
122
    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit;
123
#else
124
33.6k
    compptr->DCT_scaled_size = data_unit;
125
33.6k
#endif
126
    /* Size in data units */
127
33.6k
    compptr->width_in_blocks = (JDIMENSION)
128
33.6k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
129
33.6k
                    (long)(cinfo->max_h_samp_factor * data_unit));
130
33.6k
    compptr->height_in_blocks = (JDIMENSION)
131
33.6k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
132
33.6k
                    (long)(cinfo->max_v_samp_factor * data_unit));
133
    /* Size in samples */
134
33.6k
    compptr->downsampled_width = (JDIMENSION)
135
33.6k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
136
33.6k
                    (long)cinfo->max_h_samp_factor);
137
33.6k
    compptr->downsampled_height = (JDIMENSION)
138
33.6k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
139
33.6k
                    (long)cinfo->max_v_samp_factor);
140
    /* Mark component needed (this flag isn't actually used for compression) */
141
33.6k
    compptr->component_needed = TRUE;
142
33.6k
  }
143
144
  /* Compute number of fully interleaved MCU rows (number of times that
145
   * main controller will call coefficient or difference controller).
146
   */
147
12.9k
  cinfo->total_iMCU_rows = (JDIMENSION)
148
12.9k
    jdiv_round_up((long)cinfo->_jpeg_height,
149
12.9k
                  (long)(cinfo->max_v_samp_factor * data_unit));
150
12.9k
}
151
152
153
#if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED)
154
#define NEED_SCAN_SCRIPT
155
#endif
156
157
#ifdef NEED_SCAN_SCRIPT
158
159
LOCAL(void)
160
validate_script(j_compress_ptr cinfo)
161
/* Verify that the scan script in cinfo->scan_info[] is valid; also
162
 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
163
 */
164
3.78k
{
165
3.78k
  const jpeg_scan_info *scanptr;
166
3.78k
  int scanno, ncomps, ci, coefi, thisi;
167
3.78k
  int Ss, Se, Ah, Al;
168
3.78k
  boolean component_sent[MAX_COMPONENTS];
169
3.78k
#ifdef C_PROGRESSIVE_SUPPORTED
170
3.78k
  int *last_bitpos_ptr;
171
3.78k
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
172
  /* -1 until that coefficient has been seen; then last Al for it */
173
3.78k
#endif
174
175
3.78k
  if (cinfo->num_scans <= 0)
176
0
    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
177
178
#ifndef C_MULTISCAN_FILES_SUPPORTED
179
  if (cinfo->num_scans > 1)
180
    ERREXIT(cinfo, JERR_NOT_COMPILED);
181
#endif
182
183
3.78k
  scanptr = cinfo->scan_info;
184
3.78k
  if (scanptr->Ss != 0 && scanptr->Se == 0) {
185
0
#ifdef C_LOSSLESS_SUPPORTED
186
0
    cinfo->master->lossless = TRUE;
187
0
    cinfo->progressive_mode = FALSE;
188
0
    for (ci = 0; ci < cinfo->num_components; ci++)
189
0
      component_sent[ci] = FALSE;
190
#else
191
    ERREXIT(cinfo, JERR_NOT_COMPILED);
192
#endif
193
0
  }
194
  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
195
   * for progressive JPEG, no scan can have this.
196
   */
197
3.78k
  else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {
198
3.78k
#ifdef C_PROGRESSIVE_SUPPORTED
199
3.78k
    cinfo->progressive_mode = TRUE;
200
3.78k
    cinfo->master->lossless = FALSE;
201
3.78k
    last_bitpos_ptr = &last_bitpos[0][0];
202
15.1k
    for (ci = 0; ci < cinfo->num_components; ci++)
203
737k
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
204
726k
        *last_bitpos_ptr++ = -1;
205
#else
206
    ERREXIT(cinfo, JERR_NOT_COMPILED);
207
#endif
208
3.78k
  } else {
209
0
    cinfo->progressive_mode = cinfo->master->lossless = FALSE;
210
0
    for (ci = 0; ci < cinfo->num_components; ci++)
211
0
      component_sent[ci] = FALSE;
212
0
  }
213
214
41.6k
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
215
    /* Validate component indexes */
216
37.8k
    ncomps = scanptr->comps_in_scan;
217
37.8k
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
218
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
219
90.8k
    for (ci = 0; ci < ncomps; ci++) {
220
52.9k
      thisi = scanptr->component_index[ci];
221
52.9k
      if (thisi < 0 || thisi >= cinfo->num_components)
222
0
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
223
      /* Components must appear in SOF order within each scan */
224
52.9k
      if (ci > 0 && thisi <= scanptr->component_index[ci - 1])
225
0
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
226
52.9k
    }
227
    /* Validate progression parameters */
228
37.8k
    Ss = scanptr->Ss;
229
37.8k
    Se = scanptr->Se;
230
37.8k
    Ah = scanptr->Ah;
231
37.8k
    Al = scanptr->Al;
232
37.8k
    if (cinfo->progressive_mode) {
233
37.8k
#ifdef C_PROGRESSIVE_SUPPORTED
234
      /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah
235
       * and Al, but that seems wrong: the upper bound ought to depend on data
236
       * precision.  Perhaps they really meant 0..N+1 for N-bit precision.
237
       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
238
       * out-of-range reconstructed DC values during the first DC scan,
239
       * which might cause problems for some decoders.
240
       */
241
37.8k
      int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;
242
243
37.8k
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
244
37.8k
          Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)
245
0
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
246
37.8k
      if (Ss == 0) {
247
7.56k
        if (Se != 0)            /* DC and AC together not OK */
248
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
249
30.2k
      } else {
250
30.2k
        if (ncomps != 1)        /* AC scans must be for only one component */
251
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
252
30.2k
      }
253
90.8k
      for (ci = 0; ci < ncomps; ci++) {
254
52.9k
        last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
255
52.9k
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
256
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
257
1.74M
        for (coefi = Ss; coefi <= Se; coefi++) {
258
1.69M
          if (last_bitpos_ptr[coefi] < 0) {
259
            /* first scan of this coefficient */
260
726k
            if (Ah != 0)
261
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
262
964k
          } else {
263
            /* not first scan */
264
964k
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)
265
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
266
964k
          }
267
1.69M
          last_bitpos_ptr[coefi] = Al;
268
1.69M
        }
269
52.9k
      }
270
37.8k
#endif
271
37.8k
    } else {
272
0
#ifdef C_LOSSLESS_SUPPORTED
273
0
      if (cinfo->master->lossless) {
274
        /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that
275
         * seems wrong: the upper bound ought to depend on data precision.
276
         * Perhaps they really meant 0..N-1 for N-bit precision, which is what
277
         * we allow here.  Values greater than or equal to the data precision
278
         * will result in a blank image.
279
         */
280
0
        if (Ss < 1 || Ss > 7 ||         /* predictor selection value */
281
0
            Se != 0 || Ah != 0 ||
282
0
            Al < 0 || Al >= cinfo->data_precision) /* point transform */
283
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
284
0
      } else
285
0
#endif
286
0
      {
287
        /* For sequential JPEG, all progression parameters must be these: */
288
0
        if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)
289
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
290
0
      }
291
      /* Make sure components are not sent twice */
292
0
      for (ci = 0; ci < ncomps; ci++) {
293
0
        thisi = scanptr->component_index[ci];
294
0
        if (component_sent[thisi])
295
0
          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
296
0
        component_sent[thisi] = TRUE;
297
0
      }
298
0
    }
299
37.8k
  }
300
301
  /* Now verify that everything got sent. */
302
3.78k
  if (cinfo->progressive_mode) {
303
3.78k
#ifdef C_PROGRESSIVE_SUPPORTED
304
    /* For progressive mode, we only check that at least some DC data
305
     * got sent for each component; the spec does not require that all bits
306
     * of all coefficients be transmitted.  Would it be wiser to enforce
307
     * transmission of all coefficient bits??
308
     */
309
15.1k
    for (ci = 0; ci < cinfo->num_components; ci++) {
310
11.3k
      if (last_bitpos[ci][0] < 0)
311
0
        ERREXIT(cinfo, JERR_MISSING_DATA);
312
11.3k
    }
313
3.78k
#endif
314
3.78k
  } else {
315
0
    for (ci = 0; ci < cinfo->num_components; ci++) {
316
0
      if (!component_sent[ci])
317
0
        ERREXIT(cinfo, JERR_MISSING_DATA);
318
0
    }
319
0
  }
320
3.78k
}
321
322
#endif /* NEED_SCAN_SCRIPT */
323
324
325
LOCAL(void)
326
select_scan_parameters(j_compress_ptr cinfo)
327
/* Set up the scan parameters for the current scan */
328
46.8k
{
329
46.8k
  int ci;
330
331
46.8k
#ifdef NEED_SCAN_SCRIPT
332
46.8k
  if (cinfo->scan_info != NULL) {
333
    /* Prepare for current scan --- the script is already validated */
334
37.7k
    my_master_ptr master = (my_master_ptr)cinfo->master;
335
37.7k
    const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
336
337
37.7k
    cinfo->comps_in_scan = scanptr->comps_in_scan;
338
90.5k
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
339
52.8k
      cinfo->cur_comp_info[ci] =
340
52.8k
        &cinfo->comp_info[scanptr->component_index[ci]];
341
52.8k
    }
342
37.7k
    cinfo->Ss = scanptr->Ss;
343
37.7k
    cinfo->Se = scanptr->Se;
344
37.7k
    cinfo->Ah = scanptr->Ah;
345
37.7k
    cinfo->Al = scanptr->Al;
346
37.7k
  } else
347
9.14k
#endif
348
9.14k
  {
349
    /* Prepare for single sequential-JPEG scan containing all components */
350
9.14k
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
351
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
352
9.14k
               MAX_COMPS_IN_SCAN);
353
9.14k
    cinfo->comps_in_scan = cinfo->num_components;
354
31.4k
    for (ci = 0; ci < cinfo->num_components; ci++) {
355
22.3k
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
356
22.3k
    }
357
9.14k
    if (!cinfo->master->lossless) {
358
9.14k
      cinfo->Ss = 0;
359
9.14k
      cinfo->Se = DCTSIZE2 - 1;
360
9.14k
      cinfo->Ah = 0;
361
9.14k
      cinfo->Al = 0;
362
9.14k
    }
363
9.14k
  }
364
46.8k
}
365
366
367
LOCAL(void)
368
per_scan_setup(j_compress_ptr cinfo)
369
/* Do computations that are needed before processing a JPEG scan */
370
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
371
46.8k
{
372
46.8k
  int ci, mcublks, tmp;
373
46.8k
  jpeg_component_info *compptr;
374
46.8k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
375
376
46.8k
  if (cinfo->comps_in_scan == 1) {
377
378
    /* Noninterleaved (single-component) scan */
379
33.6k
    compptr = cinfo->cur_comp_info[0];
380
381
    /* Overall image size in MCUs */
382
33.6k
    cinfo->MCUs_per_row = compptr->width_in_blocks;
383
33.6k
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
384
385
    /* For noninterleaved scan, always one block per MCU */
386
33.6k
    compptr->MCU_width = 1;
387
33.6k
    compptr->MCU_height = 1;
388
33.6k
    compptr->MCU_blocks = 1;
389
33.6k
    compptr->MCU_sample_width = data_unit;
390
33.6k
    compptr->last_col_width = 1;
391
    /* For noninterleaved scans, it is convenient to define last_row_height
392
     * as the number of block rows present in the last iMCU row.
393
     */
394
33.6k
    tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
395
33.6k
    if (tmp == 0) tmp = compptr->v_samp_factor;
396
33.6k
    compptr->last_row_height = tmp;
397
398
    /* Prepare array describing MCU composition */
399
33.6k
    cinfo->blocks_in_MCU = 1;
400
33.6k
    cinfo->MCU_membership[0] = 0;
401
402
33.6k
  } else {
403
404
    /* Interleaved (multi-component) scan */
405
13.2k
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
406
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
407
13.2k
               MAX_COMPS_IN_SCAN);
408
409
    /* Overall image size in MCUs */
410
13.2k
    cinfo->MCUs_per_row = (JDIMENSION)
411
13.2k
      jdiv_round_up((long)cinfo->_jpeg_width,
412
13.2k
                    (long)(cinfo->max_h_samp_factor * data_unit));
413
13.2k
    cinfo->MCU_rows_in_scan = (JDIMENSION)
414
13.2k
      jdiv_round_up((long)cinfo->_jpeg_height,
415
13.2k
                    (long)(cinfo->max_v_samp_factor * data_unit));
416
417
13.2k
    cinfo->blocks_in_MCU = 0;
418
419
54.6k
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
420
41.4k
      compptr = cinfo->cur_comp_info[ci];
421
      /* Sampling factors give # of blocks of component in each MCU */
422
41.4k
      compptr->MCU_width = compptr->h_samp_factor;
423
41.4k
      compptr->MCU_height = compptr->v_samp_factor;
424
41.4k
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
425
41.4k
      compptr->MCU_sample_width = compptr->MCU_width * data_unit;
426
      /* Figure number of non-dummy blocks in last MCU column & row */
427
41.4k
      tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
428
41.4k
      if (tmp == 0) tmp = compptr->MCU_width;
429
41.4k
      compptr->last_col_width = tmp;
430
41.4k
      tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
431
41.4k
      if (tmp == 0) tmp = compptr->MCU_height;
432
41.4k
      compptr->last_row_height = tmp;
433
      /* Prepare array describing MCU composition */
434
41.4k
      mcublks = compptr->MCU_blocks;
435
41.4k
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
436
0
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
437
107k
      while (mcublks-- > 0) {
438
66.0k
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
439
66.0k
      }
440
41.4k
    }
441
442
13.2k
  }
443
444
  /* Convert restart specified in rows to actual MCU count. */
445
  /* Note that count must fit in 16 bits, so we provide limiting. */
446
46.8k
  if (cinfo->restart_in_rows > 0) {
447
20.7k
    long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;
448
20.7k
    cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);
449
20.7k
  }
450
46.8k
}
451
452
453
/*
454
 * Per-pass setup.
455
 * This is called at the beginning of each pass.  We determine which modules
456
 * will be active during this pass and give them appropriate start_pass calls.
457
 * We also set is_last_pass to indicate whether any more passes will be
458
 * required.
459
 */
460
461
METHODDEF(void)
462
prepare_for_pass(j_compress_ptr cinfo)
463
71.0k
{
464
71.0k
  my_master_ptr master = (my_master_ptr)cinfo->master;
465
466
71.0k
  switch (master->pass_type) {
467
12.9k
  case main_pass:
468
    /* Initial pass: will collect input data, and do either Huffman
469
     * optimization or data output for the first scan.
470
     */
471
12.9k
    select_scan_parameters(cinfo);
472
12.9k
    per_scan_setup(cinfo);
473
12.9k
    if (!cinfo->raw_data_in) {
474
12.9k
      (*cinfo->cconvert->start_pass) (cinfo);
475
12.9k
      (*cinfo->downsample->start_pass) (cinfo);
476
12.9k
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
477
12.9k
    }
478
12.9k
    (*cinfo->fdct->start_pass) (cinfo);
479
12.9k
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
480
12.9k
    (*cinfo->coef->start_pass) (cinfo,
481
12.9k
                                (master->total_passes > 1 ?
482
11.0k
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
483
12.9k
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
484
12.9k
    if (cinfo->optimize_coding) {
485
      /* No immediate data output; postpone writing frame/scan headers */
486
9.14k
      master->pub.call_pass_startup = FALSE;
487
9.14k
    } else {
488
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
489
3.77k
      master->pub.call_pass_startup = TRUE;
490
3.77k
    }
491
12.9k
    break;
492
0
#ifdef ENTROPY_OPT_SUPPORTED
493
16.9k
  case huff_opt_pass:
494
    /* Do Huffman optimization for a scan after the first one. */
495
16.9k
    select_scan_parameters(cinfo);
496
16.9k
    per_scan_setup(cinfo);
497
16.9k
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||
498
16.9k
        cinfo->master->lossless) {
499
15.0k
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
500
15.0k
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
501
15.0k
      master->pub.call_pass_startup = FALSE;
502
15.0k
      break;
503
15.0k
    }
504
    /* Special case: Huffman DC refinement scans need no Huffman table
505
     * and therefore we can skip the optimization pass for them.
506
     */
507
1.88k
    master->pass_type = output_pass;
508
1.88k
    master->pass_number++;
509
1.88k
#endif
510
    FALLTHROUGH                 /*FALLTHROUGH*/
511
43.0k
  case output_pass:
512
    /* Do a data-output pass. */
513
    /* We need not repeat per-scan setup if prior optimization pass did it. */
514
43.0k
    if (!cinfo->optimize_coding) {
515
16.9k
      select_scan_parameters(cinfo);
516
16.9k
      per_scan_setup(cinfo);
517
16.9k
    }
518
43.0k
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
519
43.0k
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
520
    /* We emit frame/scan headers now */
521
43.0k
    if (master->scan_number == 0)
522
9.14k
      (*cinfo->marker->write_frame_header) (cinfo);
523
43.0k
    (*cinfo->marker->write_scan_header) (cinfo);
524
43.0k
    master->pub.call_pass_startup = FALSE;
525
43.0k
    break;
526
0
  default:
527
0
    ERREXIT(cinfo, JERR_NOT_COMPILED);
528
71.0k
  }
529
530
71.0k
  master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
531
532
  /* Set up progress monitor's pass info if present */
533
71.0k
  if (cinfo->progress != NULL) {
534
0
    cinfo->progress->completed_passes = master->pass_number;
535
0
    cinfo->progress->total_passes = master->total_passes;
536
0
  }
537
71.0k
}
538
539
540
/*
541
 * Special start-of-pass hook.
542
 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
543
 * In single-pass processing, we need this hook because we don't want to
544
 * write frame/scan headers during jpeg_start_compress; we want to let the
545
 * application write COM markers etc. between jpeg_start_compress and the
546
 * jpeg_write_scanlines loop.
547
 * In multi-pass processing, this routine is not used.
548
 */
549
550
METHODDEF(void)
551
pass_startup(j_compress_ptr cinfo)
552
3.77k
{
553
3.77k
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
554
555
3.77k
  (*cinfo->marker->write_frame_header) (cinfo);
556
3.77k
  (*cinfo->marker->write_scan_header) (cinfo);
557
3.77k
}
558
559
560
/*
561
 * Finish up at end of pass.
562
 */
563
564
METHODDEF(void)
565
finish_pass_master(j_compress_ptr cinfo)
566
71.0k
{
567
71.0k
  my_master_ptr master = (my_master_ptr)cinfo->master;
568
569
  /* The entropy coder always needs an end-of-pass call,
570
   * either to analyze statistics or to flush its output buffer.
571
   */
572
71.0k
  (*cinfo->entropy->finish_pass) (cinfo);
573
574
  /* Update state for next pass */
575
71.0k
  switch (master->pass_type) {
576
12.9k
  case main_pass:
577
    /* next pass is either output of scan 0 (after optimization)
578
     * or output of scan 1 (if no optimization).
579
     */
580
12.9k
    master->pass_type = output_pass;
581
12.9k
    if (!cinfo->optimize_coding)
582
3.77k
      master->scan_number++;
583
12.9k
    break;
584
15.0k
  case huff_opt_pass:
585
    /* next pass is always output of current scan */
586
15.0k
    master->pass_type = output_pass;
587
15.0k
    break;
588
43.0k
  case output_pass:
589
    /* next pass is either optimization or output of next scan */
590
43.0k
    if (cinfo->optimize_coding)
591
26.1k
      master->pass_type = huff_opt_pass;
592
43.0k
    master->scan_number++;
593
43.0k
    break;
594
71.0k
  }
595
596
71.0k
  master->pass_number++;
597
71.0k
}
598
599
600
/*
601
 * Initialize master compression control.
602
 */
603
604
GLOBAL(void)
605
jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)
606
12.9k
{
607
12.9k
  my_master_ptr master = (my_master_ptr)cinfo->master;
608
609
12.9k
  master->pub.prepare_for_pass = prepare_for_pass;
610
12.9k
  master->pub.pass_startup = pass_startup;
611
12.9k
  master->pub.finish_pass = finish_pass_master;
612
12.9k
  master->pub.is_last_pass = FALSE;
613
614
12.9k
  if (cinfo->scan_info != NULL) {
615
3.78k
#ifdef NEED_SCAN_SCRIPT
616
3.78k
    validate_script(cinfo);
617
#else
618
    ERREXIT(cinfo, JERR_NOT_COMPILED);
619
#endif
620
9.17k
  } else {
621
9.17k
    cinfo->progressive_mode = FALSE;
622
9.17k
    cinfo->num_scans = 1;
623
9.17k
  }
624
625
  /* Disable smoothing and subsampling in lossless mode, since those are lossy
626
   * algorithms.  Set the JPEG colorspace to the input colorspace.  Disable raw
627
   * (downsampled) data input, because it isn't particularly useful without
628
   * subsampling and has not been tested in lossless mode.
629
   */
630
12.9k
  if (cinfo->master->lossless) {
631
0
    int ci;
632
0
    jpeg_component_info *compptr;
633
634
0
    cinfo->raw_data_in = FALSE;
635
0
    cinfo->smoothing_factor = 0;
636
0
    jpeg_default_colorspace(cinfo);
637
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
638
0
         ci++, compptr++)
639
0
      compptr->h_samp_factor = compptr->v_samp_factor = 1;
640
0
  }
641
642
  /* Validate parameters, determine derived values */
643
12.9k
  initial_setup(cinfo, transcode_only);
644
645
12.9k
  if (cinfo->master->lossless ||        /*  TEMPORARY HACK ??? */
646
12.9k
      (cinfo->progressive_mode && !cinfo->arith_code))
647
1.88k
    cinfo->optimize_coding = TRUE; /* assume default tables no good for
648
                                      progressive mode or lossless mode */
649
12.9k
  if (cinfo->data_precision == 12 && !cinfo->arith_code)
650
9.14k
    cinfo->optimize_coding = TRUE; /* assume default tables no good for 12-bit
651
                                      data precision */
652
653
  /* Initialize my private state */
654
12.9k
  if (transcode_only) {
655
    /* no main pass in transcoding */
656
0
    if (cinfo->optimize_coding)
657
0
      master->pass_type = huff_opt_pass;
658
0
    else
659
0
      master->pass_type = output_pass;
660
12.9k
  } else {
661
    /* for normal compression, first pass is always this type: */
662
12.9k
    master->pass_type = main_pass;
663
12.9k
  }
664
12.9k
  master->scan_number = 0;
665
12.9k
  master->pass_number = 0;
666
12.9k
  if (cinfo->optimize_coding)
667
9.14k
    master->total_passes = cinfo->num_scans * 2;
668
3.81k
  else
669
3.81k
    master->total_passes = cinfo->num_scans;
670
671
12.9k
  master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
672
12.9k
}