Coverage Report

Created: 2018-09-25 14:53

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