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