Coverage Report

Created: 2025-06-24 07:01

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