Coverage Report

Created: 2024-05-04 12:45

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