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
22.4k
{
57
22.4k
  int ci;
58
22.4k
  jpeg_component_info *compptr;
59
22.4k
  long samplesperrow;
60
22.4k
  JDIMENSION jd_samplesperrow;
61
22.4k
  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
22.4k
  if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
72
22.4k
      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
22.4k
  if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||
77
22.4k
      (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)
78
62
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
79
80
  /* Width of an input scanline must be representable as JDIMENSION. */
81
22.4k
  samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;
82
22.4k
  jd_samplesperrow = (JDIMENSION)samplesperrow;
83
22.4k
  if ((long)jd_samplesperrow != samplesperrow)
84
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
85
86
22.4k
#ifdef C_LOSSLESS_SUPPORTED
87
22.4k
  if (cinfo->data_precision != 8 && cinfo->data_precision != 12 &&
88
22.4k
      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
22.4k
  if (cinfo->num_components > MAX_COMPONENTS)
96
0
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
97
22.4k
             MAX_COMPONENTS);
98
99
  /* Compute maximum sampling factors; check factor validity */
100
22.4k
  cinfo->max_h_samp_factor = 1;
101
22.4k
  cinfo->max_v_samp_factor = 1;
102
75.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
103
52.9k
       ci++, compptr++) {
104
52.9k
    if (compptr->h_samp_factor <= 0 ||
105
52.9k
        compptr->h_samp_factor > MAX_SAMP_FACTOR ||
106
52.9k
        compptr->v_samp_factor <= 0 ||
107
52.9k
        compptr->v_samp_factor > MAX_SAMP_FACTOR)
108
0
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
109
52.9k
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
110
52.9k
                                   compptr->h_samp_factor);
111
52.9k
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
112
52.9k
                                   compptr->v_samp_factor);
113
52.9k
  }
114
115
  /* Compute dimensions of components */
116
75.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
117
52.9k
       ci++, compptr++) {
118
    /* Fill in the correct component_index value; don't rely on application */
119
52.9k
    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
52.9k
    compptr->DCT_scaled_size = data_unit;
125
52.9k
#endif
126
    /* Size in data units */
127
52.9k
    compptr->width_in_blocks = (JDIMENSION)
128
52.9k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
129
52.9k
                    (long)(cinfo->max_h_samp_factor * data_unit));
130
52.9k
    compptr->height_in_blocks = (JDIMENSION)
131
52.9k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
132
52.9k
                    (long)(cinfo->max_v_samp_factor * data_unit));
133
    /* Size in samples */
134
52.9k
    compptr->downsampled_width = (JDIMENSION)
135
52.9k
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
136
52.9k
                    (long)cinfo->max_h_samp_factor);
137
52.9k
    compptr->downsampled_height = (JDIMENSION)
138
52.9k
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
139
52.9k
                    (long)cinfo->max_v_samp_factor);
140
    /* Mark component needed (this flag isn't actually used for compression) */
141
52.9k
    compptr->component_needed = TRUE;
142
52.9k
  }
143
144
  /* Compute number of fully interleaved MCU rows (number of times that
145
   * main controller will call coefficient or difference controller).
146
   */
147
22.4k
  cinfo->total_iMCU_rows = (JDIMENSION)
148
22.4k
    jdiv_round_up((long)cinfo->_jpeg_height,
149
22.4k
                  (long)(cinfo->max_v_samp_factor * data_unit));
150
22.4k
}
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
7.66k
{
165
7.66k
  const jpeg_scan_info *scanptr;
166
7.66k
  int scanno, ncomps, ci, coefi, thisi;
167
7.66k
  int Ss, Se, Ah, Al;
168
7.66k
  boolean component_sent[MAX_COMPONENTS];
169
7.66k
#ifdef C_PROGRESSIVE_SUPPORTED
170
7.66k
  int *last_bitpos_ptr;
171
7.66k
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
172
  /* -1 until that coefficient has been seen; then last Al for it */
173
7.66k
#endif
174
175
7.66k
  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
7.66k
  scanptr = cinfo->scan_info;
184
7.66k
  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
7.66k
  else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {
198
7.66k
#ifdef C_PROGRESSIVE_SUPPORTED
199
7.66k
    cinfo->progressive_mode = TRUE;
200
7.66k
    cinfo->master->lossless = FALSE;
201
7.66k
    last_bitpos_ptr = &last_bitpos[0][0];
202
30.6k
    for (ci = 0; ci < cinfo->num_components; ci++)
203
1.49M
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
204
1.47M
        *last_bitpos_ptr++ = -1;
205
#else
206
    ERREXIT(cinfo, JERR_NOT_COMPILED);
207
#endif
208
7.66k
  } 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
84.2k
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
215
    /* Validate component indexes */
216
76.6k
    ncomps = scanptr->comps_in_scan;
217
76.6k
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
218
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
219
183k
    for (ci = 0; ci < ncomps; ci++) {
220
107k
      thisi = scanptr->component_index[ci];
221
107k
      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
107k
      if (ci > 0 && thisi <= scanptr->component_index[ci - 1])
225
0
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
226
107k
    }
227
    /* Validate progression parameters */
228
76.6k
    Ss = scanptr->Ss;
229
76.6k
    Se = scanptr->Se;
230
76.6k
    Ah = scanptr->Ah;
231
76.6k
    Al = scanptr->Al;
232
76.6k
    if (cinfo->progressive_mode) {
233
76.6k
#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
76.6k
      int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;
242
243
76.6k
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
244
76.6k
          Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)
245
0
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
246
76.6k
      if (Ss == 0) {
247
15.3k
        if (Se != 0)            /* DC and AC together not OK */
248
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
249
61.2k
      } else {
250
61.2k
        if (ncomps != 1)        /* AC scans must be for only one component */
251
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
252
61.2k
      }
253
183k
      for (ci = 0; ci < ncomps; ci++) {
254
107k
        last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
255
107k
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
256
0
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
257
3.53M
        for (coefi = Ss; coefi <= Se; coefi++) {
258
3.42M
          if (last_bitpos_ptr[coefi] < 0) {
259
            /* first scan of this coefficient */
260
1.47M
            if (Ah != 0)
261
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
262
1.95M
          } else {
263
            /* not first scan */
264
1.95M
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)
265
0
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
266
1.95M
          }
267
3.42M
          last_bitpos_ptr[coefi] = Al;
268
3.42M
        }
269
107k
      }
270
76.6k
#endif
271
76.6k
    } 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
76.6k
  }
300
301
  /* Now verify that everything got sent. */
302
7.66k
  if (cinfo->progressive_mode) {
303
7.66k
#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
30.6k
    for (ci = 0; ci < cinfo->num_components; ci++) {
310
22.9k
      if (last_bitpos[ci][0] < 0)
311
0
        ERREXIT(cinfo, JERR_MISSING_DATA);
312
22.9k
    }
313
7.66k
#endif
314
7.66k
  } 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
7.66k
}
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
45.5k
{
329
45.5k
  int ci;
330
331
45.5k
#ifdef NEED_SCAN_SCRIPT
332
45.5k
  if (cinfo->scan_info != NULL) {
333
    /* Prepare for current scan --- the script is already validated */
334
38.2k
    my_master_ptr master = (my_master_ptr)cinfo->master;
335
38.2k
    const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
336
337
38.2k
    cinfo->comps_in_scan = scanptr->comps_in_scan;
338
91.6k
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
339
53.4k
      cinfo->cur_comp_info[ci] =
340
53.4k
        &cinfo->comp_info[scanptr->component_index[ci]];
341
53.4k
    }
342
38.2k
    cinfo->Ss = scanptr->Ss;
343
38.2k
    cinfo->Se = scanptr->Se;
344
38.2k
    cinfo->Ah = scanptr->Ah;
345
38.2k
    cinfo->Al = scanptr->Al;
346
38.2k
  } else
347
7.39k
#endif
348
7.39k
  {
349
    /* Prepare for single sequential-JPEG scan containing all components */
350
7.39k
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
351
0
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
352
7.39k
               MAX_COMPS_IN_SCAN);
353
7.39k
    cinfo->comps_in_scan = cinfo->num_components;
354
22.4k
    for (ci = 0; ci < cinfo->num_components; ci++) {
355
15.0k
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
356
15.0k
    }
357
7.39k
    if (!cinfo->master->lossless) {
358
7.39k
      cinfo->Ss = 0;
359
7.39k
      cinfo->Se = DCTSIZE2 - 1;
360
7.39k
      cinfo->Ah = 0;
361
7.39k
      cinfo->Al = 0;
362
7.39k
    }
363
7.39k
  }
364
45.5k
}
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
45.5k
{
372
45.5k
  int ci, mcublks, tmp;
373
45.5k
  jpeg_component_info *compptr;
374
45.5k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
375
376
45.5k
  if (cinfo->comps_in_scan == 1) {
377
378
    /* Noninterleaved (single-component) scan */
379
34.1k
    compptr = cinfo->cur_comp_info[0];
380
381
    /* Overall image size in MCUs */
382
34.1k
    cinfo->MCUs_per_row = compptr->width_in_blocks;
383
34.1k
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
384
385
    /* For noninterleaved scan, always one block per MCU */
386
34.1k
    compptr->MCU_width = 1;
387
34.1k
    compptr->MCU_height = 1;
388
34.1k
    compptr->MCU_blocks = 1;
389
34.1k
    compptr->MCU_sample_width = data_unit;
390
34.1k
    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
34.1k
    tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
395
34.1k
    if (tmp == 0) tmp = compptr->v_samp_factor;
396
34.1k
    compptr->last_row_height = tmp;
397
398
    /* Prepare array describing MCU composition */
399
34.1k
    cinfo->blocks_in_MCU = 1;
400
34.1k
    cinfo->MCU_membership[0] = 0;
401
402
34.1k
  } else {
403
404
    /* Interleaved (multi-component) scan */
405
11.4k
    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
11.4k
               MAX_COMPS_IN_SCAN);
408
409
    /* Overall image size in MCUs */
410
11.4k
    cinfo->MCUs_per_row = (JDIMENSION)
411
11.4k
      jdiv_round_up((long)cinfo->_jpeg_width,
412
11.4k
                    (long)(cinfo->max_h_samp_factor * data_unit));
413
11.4k
    cinfo->MCU_rows_in_scan = (JDIMENSION)
414
11.4k
      jdiv_round_up((long)cinfo->_jpeg_height,
415
11.4k
                    (long)(cinfo->max_v_samp_factor * data_unit));
416
417
11.4k
    cinfo->blocks_in_MCU = 0;
418
419
45.8k
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
420
34.3k
      compptr = cinfo->cur_comp_info[ci];
421
      /* Sampling factors give # of blocks of component in each MCU */
422
34.3k
      compptr->MCU_width = compptr->h_samp_factor;
423
34.3k
      compptr->MCU_height = compptr->v_samp_factor;
424
34.3k
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
425
34.3k
      compptr->MCU_sample_width = compptr->MCU_width * data_unit;
426
      /* Figure number of non-dummy blocks in last MCU column & row */
427
34.3k
      tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
428
34.3k
      if (tmp == 0) tmp = compptr->MCU_width;
429
34.3k
      compptr->last_col_width = tmp;
430
34.3k
      tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
431
34.3k
      if (tmp == 0) tmp = compptr->MCU_height;
432
34.3k
      compptr->last_row_height = tmp;
433
      /* Prepare array describing MCU composition */
434
34.3k
      mcublks = compptr->MCU_blocks;
435
34.3k
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
436
0
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
437
89.7k
      while (mcublks-- > 0) {
438
55.3k
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
439
55.3k
      }
440
34.3k
    }
441
442
11.4k
  }
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
45.5k
  if (cinfo->restart_in_rows > 0) {
447
0
    long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;
448
0
    cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);
449
0
  }
450
45.5k
}
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
64.6k
{
464
64.6k
  my_master_ptr master = (my_master_ptr)cinfo->master;
465
466
64.6k
  switch (master->pass_type) {
467
11.2k
  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
11.2k
    select_scan_parameters(cinfo);
472
11.2k
    per_scan_setup(cinfo);
473
11.2k
    if (!cinfo->raw_data_in) {
474
0
      (*cinfo->cconvert->start_pass) (cinfo);
475
0
      (*cinfo->downsample->start_pass) (cinfo);
476
0
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
477
0
    }
478
11.2k
    (*cinfo->fdct->start_pass) (cinfo);
479
11.2k
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
480
11.2k
    (*cinfo->coef->start_pass) (cinfo,
481
11.2k
                                (master->total_passes > 1 ?
482
5.73k
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
483
11.2k
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
484
11.2k
    if (cinfo->optimize_coding) {
485
      /* No immediate data output; postpone writing frame/scan headers */
486
3.82k
      master->pub.call_pass_startup = FALSE;
487
7.39k
    } else {
488
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
489
7.39k
      master->pub.call_pass_startup = TRUE;
490
7.39k
    }
491
11.2k
    break;
492
0
#ifdef ENTROPY_OPT_SUPPORTED
493
17.1k
  case huff_opt_pass:
494
    /* Do Huffman optimization for a scan after the first one. */
495
17.1k
    select_scan_parameters(cinfo);
496
17.1k
    per_scan_setup(cinfo);
497
17.1k
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||
498
17.1k
        cinfo->master->lossless) {
499
15.2k
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
500
15.2k
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
501
15.2k
      master->pub.call_pass_startup = FALSE;
502
15.2k
      break;
503
15.2k
    }
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.91k
    master->pass_type = output_pass;
508
1.91k
    master->pass_number++;
509
1.91k
#endif
510
    FALLTHROUGH                 /*FALLTHROUGH*/
511
38.2k
  case output_pass:
512
    /* Do a data-output pass. */
513
    /* We need not repeat per-scan setup if prior optimization pass did it. */
514
38.2k
    if (!cinfo->optimize_coding) {
515
17.1k
      select_scan_parameters(cinfo);
516
17.1k
      per_scan_setup(cinfo);
517
17.1k
    }
518
38.2k
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
519
38.2k
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
520
    /* We emit frame/scan headers now */
521
38.2k
    if (master->scan_number == 0)
522
3.82k
      (*cinfo->marker->write_frame_header) (cinfo);
523
38.2k
    (*cinfo->marker->write_scan_header) (cinfo);
524
38.2k
    master->pub.call_pass_startup = FALSE;
525
38.2k
    break;
526
0
  default:
527
0
    ERREXIT(cinfo, JERR_NOT_COMPILED);
528
64.6k
  }
529
530
64.6k
  master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
531
532
  /* Set up progress monitor's pass info if present */
533
64.6k
  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
64.6k
}
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
7.39k
{
553
7.39k
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
554
555
7.39k
  (*cinfo->marker->write_frame_header) (cinfo);
556
7.39k
  (*cinfo->marker->write_scan_header) (cinfo);
557
7.39k
}
558
559
560
/*
561
 * Finish up at end of pass.
562
 */
563
564
METHODDEF(void)
565
finish_pass_master(j_compress_ptr cinfo)
566
64.6k
{
567
64.6k
  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
64.6k
  (*cinfo->entropy->finish_pass) (cinfo);
573
574
  /* Update state for next pass */
575
64.6k
  switch (master->pass_type) {
576
11.2k
  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
11.2k
    master->pass_type = output_pass;
581
11.2k
    if (!cinfo->optimize_coding)
582
7.39k
      master->scan_number++;
583
11.2k
    break;
584
15.2k
  case huff_opt_pass:
585
    /* next pass is always output of current scan */
586
15.2k
    master->pass_type = output_pass;
587
15.2k
    break;
588
38.2k
  case output_pass:
589
    /* next pass is either optimization or output of next scan */
590
38.2k
    if (cinfo->optimize_coding)
591
21.0k
      master->pass_type = huff_opt_pass;
592
38.2k
    master->scan_number++;
593
38.2k
    break;
594
64.6k
  }
595
596
64.6k
  master->pass_number++;
597
64.6k
}
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
22.4k
{
607
22.4k
  my_master_ptr master = (my_master_ptr)cinfo->master;
608
609
22.4k
  master->pub.prepare_for_pass = prepare_for_pass;
610
22.4k
  master->pub.pass_startup = pass_startup;
611
22.4k
  master->pub.finish_pass = finish_pass_master;
612
22.4k
  master->pub.is_last_pass = FALSE;
613
614
22.4k
  if (cinfo->scan_info != NULL) {
615
7.66k
#ifdef NEED_SCAN_SCRIPT
616
7.66k
    validate_script(cinfo);
617
#else
618
    ERREXIT(cinfo, JERR_NOT_COMPILED);
619
#endif
620
14.8k
  } else {
621
14.8k
    cinfo->progressive_mode = FALSE;
622
14.8k
    cinfo->num_scans = 1;
623
14.8k
  }
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
22.4k
  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
22.4k
  initial_setup(cinfo, transcode_only);
644
645
22.4k
  if (cinfo->master->lossless ||        /*  TEMPORARY HACK ??? */
646
22.4k
      (cinfo->progressive_mode && !cinfo->arith_code))
647
3.82k
    cinfo->optimize_coding = TRUE; /* assume default tables no good for
648
                                      progressive mode or lossless mode */
649
22.4k
  if (cinfo->data_precision == 12 && !cinfo->arith_code)
650
0
    cinfo->optimize_coding = TRUE; /* assume default tables no good for 12-bit
651
                                      data precision */
652
653
  /* Initialize my private state */
654
22.4k
  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
22.4k
  } else {
661
    /* for normal compression, first pass is always this type: */
662
22.4k
    master->pass_type = main_pass;
663
22.4k
  }
664
22.4k
  master->scan_number = 0;
665
22.4k
  master->pass_number = 0;
666
22.4k
  if (cinfo->optimize_coding)
667
7.64k
    master->total_passes = cinfo->num_scans * 2;
668
14.8k
  else
669
14.8k
    master->total_passes = cinfo->num_scans;
670
671
22.4k
  master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
672
22.4k
}