1 /*
2  * jcmaster.c
3  *
4  * Copyright (C) 1991-1994, 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 selecting the modules to be executed
10  * and with determining the number of passes and the work to be done in each
11  * pass.
12  */
13 
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
17 
18 
19 /* Private state */
20 
21 typedef struct {
22   struct jpeg_comp_master pub;	/* public fields */
23 
24   int pass_number;		/* eventually need more complex state... */
25 } my_comp_master;
26 
27 typedef my_comp_master * my_master_ptr;
28 
29 
30 /*
31  * Support routines that do various essential calculations.
32  */
33 
34 LOCAL void
initial_setup(j_compress_ptr cinfo)35 initial_setup (j_compress_ptr cinfo)
36 /* Do computations that are needed before master selection phase */
37 {
38   int ci;
39   jpeg_component_info *compptr;
40   long samplesperrow;
41   JDIMENSION jd_samplesperrow;
42 
43   /* Sanity check on image dimensions */
44   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
45       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
46     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
47 
48   /* Make sure image isn't bigger than I can handle */
49   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
50       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
51     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
52 
53   /* Width of an input scanline must be representable as JDIMENSION. */
54   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
55   jd_samplesperrow = (JDIMENSION) samplesperrow;
56   if ((long) jd_samplesperrow != samplesperrow)
57     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
58 
59   /* For now, precision must match compiled-in value... */
60   if (cinfo->data_precision != BITS_IN_JSAMPLE)
61     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
62 
63   /* Check that number of components won't exceed internal array sizes */
64   if (cinfo->num_components > MAX_COMPONENTS)
65     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
66 	     MAX_COMPONENTS);
67 
68   if (cinfo->num_components == 1) {
69     /* subsampling is a no-op, so it's safe to ignore it */
70     cinfo->comp_info[0].h_samp_factor = 1;
71     cinfo->comp_info[0].v_samp_factor = 1;
72   }
73 
74   /* Compute maximum sampling factors; check factor validity */
75   cinfo->max_h_samp_factor = 1;
76   cinfo->max_v_samp_factor = 1;
77   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
78        ci++, compptr++) {
79     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
80 	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
81       ERREXIT(cinfo, JERR_BAD_SAMPLING);
82     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
83 				   compptr->h_samp_factor);
84     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
85 				   compptr->v_samp_factor);
86   }
87 
88   /* Compute dimensions of components */
89   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90        ci++, compptr++) {
91     /* For compression, we never do DCT scaling. */
92     compptr->DCT_scaled_size = DCTSIZE;
93     /* Size in DCT blocks */
94     compptr->width_in_blocks = (JDIMENSION)
95       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
96 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
97     compptr->height_in_blocks = (JDIMENSION)
98       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
99 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
100     /* Size in samples */
101     compptr->downsampled_width = (JDIMENSION)
102       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
103 		    (long) cinfo->max_h_samp_factor);
104     compptr->downsampled_height = (JDIMENSION)
105       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
106 		    (long) cinfo->max_v_samp_factor);
107     /* Mark component needed (this flag isn't actually used for compression) */
108     compptr->component_needed = TRUE;
109   }
110 
111   /* Compute number of fully interleaved MCU rows (number of times that
112    * main controller will call coefficient controller).
113    */
114   cinfo->total_iMCU_rows = (JDIMENSION)
115     jdiv_round_up((long) cinfo->image_height,
116 		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
117 }
118 
119 
120 LOCAL void
per_scan_setup(j_compress_ptr cinfo)121 per_scan_setup (j_compress_ptr cinfo)
122 /* Do computations that are needed before processing a JPEG scan */
123 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
124 {
125   int ci, mcublks, tmp;
126   jpeg_component_info *compptr;
127 
128   if (cinfo->comps_in_scan == 1) {
129 
130     /* Noninterleaved (single-component) scan */
131     compptr = cinfo->cur_comp_info[0];
132 
133     /* Overall image size in MCUs */
134     cinfo->MCUs_per_row = compptr->width_in_blocks;
135     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
136 
137     /* For noninterleaved scan, always one block per MCU */
138     compptr->MCU_width = 1;
139     compptr->MCU_height = 1;
140     compptr->MCU_blocks = 1;
141     compptr->MCU_sample_width = DCTSIZE;
142     compptr->last_col_width = 1;
143     compptr->last_row_height = 1;
144 
145     /* Prepare array describing MCU composition */
146     cinfo->blocks_in_MCU = 1;
147     cinfo->MCU_membership[0] = 0;
148 
149   } else {
150 
151     /* Interleaved (multi-component) scan */
152     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
153       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
154 	       MAX_COMPS_IN_SCAN);
155 
156     /* Overall image size in MCUs */
157     cinfo->MCUs_per_row = (JDIMENSION)
158       jdiv_round_up((long) cinfo->image_width,
159 		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
160     cinfo->MCU_rows_in_scan = (JDIMENSION)
161       jdiv_round_up((long) cinfo->image_height,
162 		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
163 
164     cinfo->blocks_in_MCU = 0;
165 
166     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
167       compptr = cinfo->cur_comp_info[ci];
168       /* Sampling factors give # of blocks of component in each MCU */
169       compptr->MCU_width = compptr->h_samp_factor;
170       compptr->MCU_height = compptr->v_samp_factor;
171       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
172       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
173       /* Figure number of non-dummy blocks in last MCU column & row */
174       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
175       if (tmp == 0) tmp = compptr->MCU_width;
176       compptr->last_col_width = tmp;
177       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
178       if (tmp == 0) tmp = compptr->MCU_height;
179       compptr->last_row_height = tmp;
180       /* Prepare array describing MCU composition */
181       mcublks = compptr->MCU_blocks;
182       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
183 	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
184       while (mcublks-- > 0) {
185 	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
186       }
187     }
188 
189   }
190 
191   /* Convert restart specified in rows to actual MCU count. */
192   /* Note that count must fit in 16 bits, so we provide limiting. */
193   if (cinfo->restart_in_rows > 0) {
194     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
195     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
196   }
197 }
198 
199 
200 /*
201  * Master selection of compression modules.
202  * This is done once at the start of processing an image.  We determine
203  * which modules will be used and give them appropriate initialization calls.
204  */
205 
206 LOCAL void
master_selection(j_compress_ptr cinfo)207 master_selection (j_compress_ptr cinfo)
208 {
209   my_master_ptr master = (my_master_ptr) cinfo->master;
210 
211   initial_setup(cinfo);
212   master->pass_number = 0;
213 
214   /* There's not a lot of smarts here right now, but it'll get more
215    * complicated when we have multiple implementations available...
216    */
217 
218   /* Preprocessing */
219   if (! cinfo->raw_data_in) {
220     jinit_color_converter(cinfo);
221     jinit_downsampler(cinfo);
222     jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
223   }
224   /* Forward DCT */
225   jinit_forward_dct(cinfo);
226   /* Entropy encoding: either Huffman or arithmetic coding. */
227   if (cinfo->arith_code) {
228 #ifdef C_ARITH_CODING_SUPPORTED
229     jinit_arith_encoder(cinfo);
230 #else
231     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
232 #endif
233   } else
234     jinit_huff_encoder(cinfo);
235 
236   /* For now, a full buffer is needed only for Huffman optimization. */
237   jinit_c_coef_controller(cinfo, cinfo->optimize_coding);
238   jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
239 
240   jinit_marker_writer(cinfo);
241 
242   /* We can now tell the memory manager to allocate virtual arrays. */
243   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
244 
245   /* Write the datastream header (SOI) immediately.
246    * Frame and scan headers are postponed till later.
247    * This lets application insert special markers after the SOI.
248    */
249   (*cinfo->marker->write_file_header) (cinfo);
250 }
251 
252 
253 /*
254  * Per-pass setup.
255  * This is called at the beginning of each pass.  We determine which modules
256  * will be active during this pass and give them appropriate start_pass calls.
257  * We also set is_last_pass to indicate whether any more passes will be
258  * required.
259  */
260 
261 METHODDEF void
prepare_for_pass(j_compress_ptr cinfo)262 prepare_for_pass (j_compress_ptr cinfo)
263 {
264   my_master_ptr master = (my_master_ptr) cinfo->master;
265   int ci;
266   int npasses;
267 
268   /* ???? JUST A QUICK CROCK FOR NOW ??? */
269 
270   /* For now, handle only single interleaved output scan; */
271   /* we support two passes for Huffman optimization. */
272 
273   /* Prepare for single scan containing all components */
274   if (cinfo->num_components > MAX_COMPS_IN_SCAN)
275     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
276 	     MAX_COMPS_IN_SCAN);
277   cinfo->comps_in_scan = cinfo->num_components;
278   for (ci = 0; ci < cinfo->num_components; ci++) {
279     cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
280   }
281 
282   per_scan_setup(cinfo);
283 
284   if (! cinfo->optimize_coding) {
285     /* Standard single-pass case */
286     npasses = 1;
287     master->pub.call_pass_startup = TRUE;
288     master->pub.is_last_pass = TRUE;
289     if (! cinfo->raw_data_in) {
290       (*cinfo->cconvert->start_pass) (cinfo);
291       (*cinfo->downsample->start_pass) (cinfo);
292       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
293     }
294     (*cinfo->fdct->start_pass) (cinfo);
295     (*cinfo->entropy->start_pass) (cinfo, FALSE);
296     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
297     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
298   } else {
299     npasses = 2;
300     switch (master->pass_number) {
301     case 0:
302       /* Huffman optimization: run all modules, gather statistics */
303       master->pub.call_pass_startup = FALSE;
304       master->pub.is_last_pass = FALSE;
305       if (! cinfo->raw_data_in) {
306 	(*cinfo->cconvert->start_pass) (cinfo);
307 	(*cinfo->downsample->start_pass) (cinfo);
308 	(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
309       }
310       (*cinfo->fdct->start_pass) (cinfo);
311       (*cinfo->entropy->start_pass) (cinfo, TRUE);
312       (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_AND_PASS);
313       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
314       break;
315     case 1:
316       /* Second pass: reread data from coefficient buffer */
317       master->pub.is_last_pass = TRUE;
318       (*cinfo->entropy->start_pass) (cinfo, FALSE);
319       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
320       /* We emit frame/scan headers now */
321       (*cinfo->marker->write_frame_header) (cinfo);
322       (*cinfo->marker->write_scan_header) (cinfo);
323       break;
324     }
325   }
326 
327   /* Set up progress monitor's pass info if present */
328   if (cinfo->progress != NULL) {
329     cinfo->progress->completed_passes = master->pass_number;
330     cinfo->progress->total_passes = npasses;
331   }
332 
333   master->pass_number++;
334 }
335 
336 
337 /*
338  * Special start-of-pass hook.
339  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
340  * In single-pass processing, we need this hook because we don't want to
341  * write frame/scan headers during jpeg_start_compress; we want to let the
342  * application write COM markers etc. between jpeg_start_compress and the
343  * jpeg_write_scanlines loop.
344  * In multi-pass processing, this routine is not used.
345  */
346 
347 METHODDEF void
pass_startup(j_compress_ptr cinfo)348 pass_startup (j_compress_ptr cinfo)
349 {
350   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
351 
352   (*cinfo->marker->write_frame_header) (cinfo);
353   (*cinfo->marker->write_scan_header) (cinfo);
354 }
355 
356 
357 /*
358  * Finish up at end of pass.
359  */
360 
361 METHODDEF void
finish_pass_master(j_compress_ptr cinfo)362 finish_pass_master (j_compress_ptr cinfo)
363 {
364   /* More complex logic later ??? */
365 
366   /* The entropy coder needs an end-of-pass call, either to analyze
367    * statistics or to flush its output buffer.
368    */
369   (*cinfo->entropy->finish_pass) (cinfo);
370 }
371 
372 
373 /*
374  * Initialize master compression control.
375  * This creates my own subrecord and also performs the master selection phase,
376  * which causes other modules to create their subrecords.
377  */
378 
379 GLOBAL void
jinit_master_compress(j_compress_ptr cinfo)380 jinit_master_compress (j_compress_ptr cinfo)
381 {
382   my_master_ptr master;
383 
384   master = (my_master_ptr)
385       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
386 				  SIZEOF(my_comp_master));
387   cinfo->master = (struct jpeg_comp_master *) master;
388   master->pub.prepare_for_pass = prepare_for_pass;
389   master->pub.pass_startup = pass_startup;
390   master->pub.finish_pass = finish_pass_master;
391 
392   master_selection(cinfo);
393 }
394