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