DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjdmaster.pas
1 unit imjdmaster;
3 { This file contains master control logic for the JPEG decompressor.
4 These routines are concerned with selecting the modules to be executed
5 and with determining the number of passes and the work to be done in each
6 pass. }
8 { Original: jdmaster.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
10 interface
12 {$I imjconfig.inc}
14 uses
15 imjmorecfg,
16 imjinclude,
17 imjutils,
18 imjerror,
19 imjdeferr,
20 imjdcolor, imjdsample, imjdpostct, imjddctmgr, imjdphuff,
21 imjdhuff, imjdcoefct, imjdmainct,
22 {$ifdef QUANT_1PASS_SUPPORTED}
23 imjquant1,
24 {$endif}
25 {$ifdef QUANT_2PASS_SUPPORTED}
26 imjquant2,
27 {$endif}
28 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
29 imjdmerge,
30 {$endif}
31 imjpeglib;
34 { Compute output image dimensions and related values.
35 NOTE: this is exported for possible use by application.
36 Hence it mustn't do anything that can't be done twice.
37 Also note that it may be called before the master module is initialized! }
39 {GLOBAL}
40 procedure jpeg_calc_output_dimensions (cinfo : j_decompress_ptr);
41 { Do computations that are needed before master selection phase }
44 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
46 {GLOBAL}
47 procedure jpeg_new_colormap (cinfo : j_decompress_ptr);
49 {$endif}
51 { Initialize master decompression control and select active modules.
52 This is performed at the start of jpeg_start_decompress. }
54 {GLOBAL}
55 procedure jinit_master_decompress (cinfo : j_decompress_ptr);
57 implementation
59 { Private state }
61 type
62 my_master_ptr = ^my_decomp_master;
63 my_decomp_master = record
64 pub : jpeg_decomp_master; { public fields }
66 pass_number : int; { # of passes completed }
68 using_merged_upsample : boolean; { TRUE if using merged upsample/cconvert }
70 { Saved references to initialized quantizer modules,
71 in case we need to switch modes. }
73 quantizer_1pass : jpeg_color_quantizer_ptr;
74 quantizer_2pass : jpeg_color_quantizer_ptr;
75 end;
77 { Determine whether merged upsample/color conversion should be used.
78 CRUCIAL: this must match the actual capabilities of jdmerge.c! }
80 {LOCAL}
81 function use_merged_upsample (cinfo : j_decompress_ptr) : boolean;
82 var
83 compptr : jpeg_component_info_list_ptr;
84 begin
85 compptr := cinfo^.comp_info;
87 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
88 { Merging is the equivalent of plain box-filter upsampling }
89 if (cinfo^.do_fancy_upsampling) or (cinfo^.CCIR601_sampling) then
90 begin
91 use_merged_upsample := FALSE;
92 exit;
93 end;
94 { jdmerge.c only supports YCC=>RGB color conversion }
95 if (cinfo^.jpeg_color_space <> JCS_YCbCr) or (cinfo^.num_components <> 3)
96 or (cinfo^.out_color_space <> JCS_RGB)
97 or (cinfo^.out_color_components <> RGB_PIXELSIZE) then
98 begin
99 use_merged_upsample := FALSE;
100 exit;
101 end;
103 { and it only handles 2h1v or 2h2v sampling ratios }
104 if (compptr^[0].h_samp_factor <> 2) or
105 (compptr^[1].h_samp_factor <> 1) or
106 (compptr^[2].h_samp_factor <> 1) or
107 (compptr^[0].v_samp_factor > 2) or
108 (compptr^[1].v_samp_factor <> 1) or
109 (compptr^[2].v_samp_factor <> 1) then
110 begin
111 use_merged_upsample := FALSE;
112 exit;
113 end;
114 { furthermore, it doesn't work if we've scaled the IDCTs differently }
115 if (compptr^[0].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) or
116 (compptr^[1].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) or
117 (compptr^[2].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) then
118 begin
119 use_merged_upsample := FALSE;
120 exit;
121 end;
122 { ??? also need to test for upsample-time rescaling, when & if supported }
123 use_merged_upsample := TRUE; { by golly, it'll work... }
124 {$else}
125 use_merged_upsample := FALSE;
126 {$endif}
127 end;
130 { Compute output image dimensions and related values.
131 NOTE: this is exported for possible use by application.
132 Hence it mustn't do anything that can't be done twice.
133 Also note that it may be called before the master module is initialized! }
135 {GLOBAL}
136 procedure jpeg_calc_output_dimensions (cinfo : j_decompress_ptr);
137 { Do computations that are needed before master selection phase }
138 {$ifdef IDCT_SCALING_SUPPORTED}
139 var
140 ci : int;
141 compptr : jpeg_component_info_ptr;
142 {$endif}
143 var
144 ssize : int;
145 begin
146 { Prevent application from calling me at wrong times }
147 if (cinfo^.global_state <> DSTATE_READY) then
148 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
150 {$ifdef IDCT_SCALING_SUPPORTED}
152 { Compute actual output image dimensions and DCT scaling choices. }
153 if (cinfo^.scale_num * 8 <= cinfo^.scale_denom) then
154 begin
155 { Provide 1/8 scaling }
156 cinfo^.output_width := JDIMENSION (
157 jdiv_round_up( long(cinfo^.image_width), long(8)) );
158 cinfo^.output_height := JDIMENSION (
159 jdiv_round_up( long(cinfo^.image_height), long(8)) );
160 cinfo^.min_DCT_scaled_size := 1;
161 end
162 else
163 if (cinfo^.scale_num * 4 <= cinfo^.scale_denom) then
164 begin
165 { Provide 1/4 scaling }
166 cinfo^.output_width := JDIMENSION (
167 jdiv_round_up( long (cinfo^.image_width), long(4)) );
168 cinfo^.output_height := JDIMENSION (
169 jdiv_round_up( long (cinfo^.image_height), long(4)) );
170 cinfo^.min_DCT_scaled_size := 2;
171 end
172 else
173 if (cinfo^.scale_num * 2 <= cinfo^.scale_denom) then
174 begin
175 { Provide 1/2 scaling }
176 cinfo^.output_width := JDIMENSION (
177 jdiv_round_up( long(cinfo^.image_width), long(2)) );
178 cinfo^.output_height := JDIMENSION (
179 jdiv_round_up( long(cinfo^.image_height), long(2)) );
180 cinfo^.min_DCT_scaled_size := 4;
181 end
182 else
183 begin
184 { Provide 1/1 scaling }
185 cinfo^.output_width := cinfo^.image_width;
186 cinfo^.output_height := cinfo^.image_height;
187 cinfo^.min_DCT_scaled_size := DCTSIZE;
188 end;
189 { In selecting the actual DCT scaling for each component, we try to
190 scale up the chroma components via IDCT scaling rather than upsampling.
191 This saves time if the upsampler gets to use 1:1 scaling.
192 Note this code assumes that the supported DCT scalings are powers of 2. }
194 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
195 for ci := 0 to pred(cinfo^.num_components) do
196 begin
197 ssize := cinfo^.min_DCT_scaled_size;
198 while (ssize < DCTSIZE) and
199 ((compptr^.h_samp_factor * ssize * 2 <=
200 cinfo^.max_h_samp_factor * cinfo^.min_DCT_scaled_size) and
201 (compptr^.v_samp_factor * ssize * 2 <=
202 cinfo^.max_v_samp_factor * cinfo^.min_DCT_scaled_size)) do
203 begin
204 ssize := ssize * 2;
205 end;
206 compptr^.DCT_scaled_size := ssize;
207 Inc(compptr);
208 end;
210 { Recompute downsampled dimensions of components;
211 application needs to know these if using raw downsampled data. }
213 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
214 for ci := 0 to pred(cinfo^.num_components) do
215 begin
216 { Size in samples, after IDCT scaling }
217 compptr^.downsampled_width := JDIMENSION (
218 jdiv_round_up(long (cinfo^.image_width) *
219 long (compptr^.h_samp_factor * compptr^.DCT_scaled_size),
220 long (cinfo^.max_h_samp_factor * DCTSIZE)) );
221 compptr^.downsampled_height := JDIMENSION (
222 jdiv_round_up(long (cinfo^.image_height) *
223 long (compptr^.v_samp_factor * compptr^.DCT_scaled_size),
224 long (cinfo^.max_v_samp_factor * DCTSIZE)) );
225 Inc(compptr);
226 end;
228 {$else} { !IDCT_SCALING_SUPPORTED }
230 { Hardwire it to "no scaling" }
231 cinfo^.output_width := cinfo^.image_width;
232 cinfo^.output_height := cinfo^.image_height;
233 { jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
234 and has computed unscaled downsampled_width and downsampled_height. }
236 {$endif} { IDCT_SCALING_SUPPORTED }
238 { Report number of components in selected colorspace. }
239 { Probably this should be in the color conversion module... }
240 case (cinfo^.out_color_space) of
241 JCS_GRAYSCALE:
242 cinfo^.out_color_components := 1;
243 {$ifndef RGB_PIXELSIZE_IS_3}
244 JCS_RGB:
245 cinfo^.out_color_components := RGB_PIXELSIZE;
246 {$else}
247 JCS_RGB,
248 {$endif} { else share code with YCbCr }
249 JCS_YCbCr:
250 cinfo^.out_color_components := 3;
251 JCS_CMYK,
252 JCS_YCCK:
253 cinfo^.out_color_components := 4;
254 else { else must be same colorspace as in file }
255 cinfo^.out_color_components := cinfo^.num_components;
256 end;
257 if (cinfo^.quantize_colors) then
258 cinfo^.output_components := 1
259 else
260 cinfo^.output_components := cinfo^.out_color_components;
262 { See if upsampler will want to emit more than one row at a time }
263 if (use_merged_upsample(cinfo)) then
264 cinfo^.rec_outbuf_height := cinfo^.max_v_samp_factor
265 else
266 cinfo^.rec_outbuf_height := 1;
267 end;
270 { Several decompression processes need to range-limit values to the range
271 0..MAXJSAMPLE; the input value may fall somewhat outside this range
272 due to noise introduced by quantization, roundoff error, etc. These
273 processes are inner loops and need to be as fast as possible. On most
274 machines, particularly CPUs with pipelines or instruction prefetch,
275 a (subscript-check-less) C table lookup
276 x := sample_range_limit[x];
277 is faster than explicit tests
278 if (x < 0) x := 0;
279 else if (x > MAXJSAMPLE) x := MAXJSAMPLE;
280 These processes all use a common table prepared by the routine below.
282 For most steps we can mathematically guarantee that the initial value
283 of x is within MAXJSAMPLE+1 of the legal range, so a table running from
284 -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
285 limiting step (just after the IDCT), a wildly out-of-range value is
286 possible if the input data is corrupt. To avoid any chance of indexing
287 off the end of memory and getting a bad-pointer trap, we perform the
288 post-IDCT limiting thus:
289 x := range_limit[x & MASK];
290 where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
291 samples. Under normal circumstances this is more than enough range and
292 a correct output will be generated; with bogus input data the mask will
293 cause wraparound, and we will safely generate a bogus-but-in-range output.
294 For the post-IDCT step, we want to convert the data from signed to unsigned
295 representation by adding CENTERJSAMPLE at the same time that we limit it.
296 So the post-IDCT limiting table ends up looking like this:
297 CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
298 MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
299 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
300 0,1,...,CENTERJSAMPLE-1
301 Negative inputs select values from the upper half of the table after
302 masking.
304 We can save some space by overlapping the start of the post-IDCT table
305 with the simpler range limiting table. The post-IDCT table begins at
306 sample_range_limit + CENTERJSAMPLE.
308 Note that the table is allocated in near data space on PCs; it's small
309 enough and used often enough to justify this. }
311 {LOCAL}
312 procedure prepare_range_limit_table (cinfo : j_decompress_ptr);
313 { Allocate and fill in the sample_range_limit table }
314 var
315 table : range_limit_table_ptr;
316 idct_table : JSAMPROW;
317 i : int;
318 begin
319 table := range_limit_table_ptr (
320 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
321 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)) );
323 { First segment of "simple" table: limit[x] := 0 for x < 0 }
324 MEMZERO(table, (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
326 cinfo^.sample_range_limit := (table);
327 { allow negative subscripts of simple table }
328 { is noop, handled via type definition (Nomssi) }
329 { Main part of "simple" table: limit[x] := x }
330 for i := 0 to MAXJSAMPLE do
331 table^[i] := JSAMPLE (i);
332 idct_table := JSAMPROW(@ table^[CENTERJSAMPLE]);
333 { Point to where post-IDCT table starts }
334 { End of simple table, rest of first half of post-IDCT table }
335 for i := CENTERJSAMPLE to pred(2*(MAXJSAMPLE+1)) do
336 idct_table^[i] := MAXJSAMPLE;
337 { Second half of post-IDCT table }
338 MEMZERO(@(idct_table^[2 * (MAXJSAMPLE+1)]),
339 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
340 MEMCOPY(@(idct_table^[(4 * (MAXJSAMPLE+1) - CENTERJSAMPLE)]),
341 @cinfo^.sample_range_limit^[0], CENTERJSAMPLE * SIZEOF(JSAMPLE));
343 end;
346 { Master selection of decompression modules.
347 This is done once at jpeg_start_decompress time. We determine
348 which modules will be used and give them appropriate initialization calls.
349 We also initialize the decompressor input side to begin consuming data.
351 Since jpeg_read_header has finished, we know what is in the SOF
352 and (first) SOS markers. We also have all the application parameter
353 settings. }
355 {LOCAL}
356 procedure master_selection (cinfo : j_decompress_ptr);
357 var
358 master : my_master_ptr;
359 use_c_buffer : boolean;
360 samplesperrow : long;
361 jd_samplesperrow : JDIMENSION;
362 var
363 nscans : int;
364 begin
365 master := my_master_ptr (cinfo^.master);
367 { Initialize dimensions and other stuff }
368 jpeg_calc_output_dimensions(cinfo);
369 prepare_range_limit_table(cinfo);
371 { Width of an output scanline must be representable as JDIMENSION. }
372 samplesperrow := long(cinfo^.output_width) * long (cinfo^.out_color_components);
373 jd_samplesperrow := JDIMENSION (samplesperrow);
374 if (long(jd_samplesperrow) <> samplesperrow) then
375 ERREXIT(j_common_ptr(cinfo), JERR_WIDTH_OVERFLOW);
377 { Initialize my private state }
378 master^.pass_number := 0;
379 master^.using_merged_upsample := use_merged_upsample(cinfo);
381 { Color quantizer selection }
382 master^.quantizer_1pass := NIL;
383 master^.quantizer_2pass := NIL;
384 { No mode changes if not using buffered-image mode. }
385 if (not cinfo^.quantize_colors) or (not cinfo^.buffered_image) then
386 begin
387 cinfo^.enable_1pass_quant := FALSE;
388 cinfo^.enable_external_quant := FALSE;
389 cinfo^.enable_2pass_quant := FALSE;
390 end;
391 if (cinfo^.quantize_colors) then
392 begin
393 if (cinfo^.raw_data_out) then
394 ERREXIT(j_common_ptr(cinfo), JERR_NOTIMPL);
395 { 2-pass quantizer only works in 3-component color space. }
396 if (cinfo^.out_color_components <> 3) then
397 begin
398 cinfo^.enable_1pass_quant := TRUE;
399 cinfo^.enable_external_quant := FALSE;
400 cinfo^.enable_2pass_quant := FALSE;
401 cinfo^.colormap := NIL;
402 end
403 else
404 if (cinfo^.colormap <> NIL) then
405 begin
406 cinfo^.enable_external_quant := TRUE;
407 end
408 else
409 if (cinfo^.two_pass_quantize) then
410 begin
411 cinfo^.enable_2pass_quant := TRUE;
412 end
413 else
414 begin
415 cinfo^.enable_1pass_quant := TRUE;
416 end;
418 if (cinfo^.enable_1pass_quant) then
419 begin
420 {$ifdef QUANT_1PASS_SUPPORTED}
421 jinit_1pass_quantizer(cinfo);
422 master^.quantizer_1pass := cinfo^.cquantize;
423 {$else}
424 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
425 {$endif}
426 end;
428 { We use the 2-pass code to map to external colormaps. }
429 if (cinfo^.enable_2pass_quant) or (cinfo^.enable_external_quant) then
430 begin
431 {$ifdef QUANT_2PASS_SUPPORTED}
432 jinit_2pass_quantizer(cinfo);
433 master^.quantizer_2pass := cinfo^.cquantize;
434 {$else}
435 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
436 {$endif}
437 end;
438 { If both quantizers are initialized, the 2-pass one is left active;
439 this is necessary for starting with quantization to an external map. }
440 end;
442 { Post-processing: in particular, color conversion first }
443 if (not cinfo^.raw_data_out) then
444 begin
445 if (master^.using_merged_upsample) then
446 begin
447 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
448 jinit_merged_upsampler(cinfo); { does color conversion too }
449 {$else}
450 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
451 {$endif}
452 end
453 else
454 begin
455 jinit_color_deconverter(cinfo);
456 jinit_upsampler(cinfo);
457 end;
458 jinit_d_post_controller(cinfo, cinfo^.enable_2pass_quant);
459 end;
460 { Inverse DCT }
461 jinit_inverse_dct(cinfo);
462 { Entropy decoding: either Huffman or arithmetic coding. }
463 if (cinfo^.arith_code) then
464 begin
465 ERREXIT(j_common_ptr(cinfo), JERR_ARITH_NOTIMPL);
466 end
467 else
468 begin
469 if (cinfo^.progressive_mode) then
470 begin
471 {$ifdef D_PROGRESSIVE_SUPPORTED}
472 jinit_phuff_decoder(cinfo);
473 {$else}
474 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
475 {$endif}
476 end
477 else
478 jinit_huff_decoder(cinfo);
479 end;
481 { Initialize principal buffer controllers. }
482 use_c_buffer := cinfo^.inputctl^.has_multiple_scans or cinfo^.buffered_image;
483 jinit_d_coef_controller(cinfo, use_c_buffer);
485 if (not cinfo^.raw_data_out) then
486 jinit_d_main_controller(cinfo, FALSE { never need full buffer here });
488 { We can now tell the memory manager to allocate virtual arrays. }
489 cinfo^.mem^.realize_virt_arrays (j_common_ptr(cinfo));
491 { Initialize input side of decompressor to consume first scan. }
492 cinfo^.inputctl^.start_input_pass (cinfo);
494 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
495 { If jpeg_start_decompress will read the whole file, initialize
496 progress monitoring appropriately. The input step is counted
497 as one pass. }
499 if (cinfo^.progress <> NIL) and (not cinfo^.buffered_image) and
500 (cinfo^.inputctl^.has_multiple_scans) then
501 begin
503 { Estimate number of scans to set pass_limit. }
504 if (cinfo^.progressive_mode) then
505 begin
506 { Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. }
507 nscans := 2 + 3 * cinfo^.num_components;
508 end
509 else
510 begin
511 { For a nonprogressive multiscan file, estimate 1 scan per component. }
512 nscans := cinfo^.num_components;
513 end;
514 cinfo^.progress^.pass_counter := Long(0);
515 cinfo^.progress^.pass_limit := long (cinfo^.total_iMCU_rows) * nscans;
516 cinfo^.progress^.completed_passes := 0;
517 if cinfo^.enable_2pass_quant then
518 cinfo^.progress^.total_passes := 3
519 else
520 cinfo^.progress^.total_passes := 2;
521 { Count the input pass as done }
522 Inc(master^.pass_number);
523 end;
524 {$endif} { D_MULTISCAN_FILES_SUPPORTED }
525 end;
528 { Per-pass setup.
529 This is called at the beginning of each output pass. We determine which
530 modules will be active during this pass and give them appropriate
531 start_pass calls. We also set is_dummy_pass to indicate whether this
532 is a "real" output pass or a dummy pass for color quantization.
533 (In the latter case, jdapistd.c will crank the pass to completion.) }
535 {METHODDEF}
536 procedure prepare_for_output_pass (cinfo : j_decompress_ptr);
537 var
538 master : my_master_ptr;
539 begin
540 master := my_master_ptr (cinfo^.master);
542 if (master^.pub.is_dummy_pass) then
543 begin
544 {$ifdef QUANT_2PASS_SUPPORTED}
545 { Final pass of 2-pass quantization }
546 master^.pub.is_dummy_pass := FALSE;
547 cinfo^.cquantize^.start_pass (cinfo, FALSE);
548 cinfo^.post^.start_pass (cinfo, JBUF_CRANK_DEST);
549 cinfo^.main^.start_pass (cinfo, JBUF_CRANK_DEST);
550 {$else}
551 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
552 {$endif} { QUANT_2PASS_SUPPORTED }
553 end
554 else
555 begin
556 if (cinfo^.quantize_colors) and (cinfo^.colormap = NIL) then
557 begin
558 { Select new quantization method }
559 if (cinfo^.two_pass_quantize) and (cinfo^.enable_2pass_quant) then
560 begin
561 cinfo^.cquantize := master^.quantizer_2pass;
562 master^.pub.is_dummy_pass := TRUE;
563 end
564 else
565 if (cinfo^.enable_1pass_quant) then
566 begin
567 cinfo^.cquantize := master^.quantizer_1pass;
568 end
569 else
570 begin
571 ERREXIT(j_common_ptr(cinfo), JERR_MODE_CHANGE);
572 end;
573 end;
574 cinfo^.idct^.start_pass (cinfo);
575 cinfo^.coef^.start_output_pass (cinfo);
576 if (not cinfo^.raw_data_out) then
577 begin
578 if (not master^.using_merged_upsample) then
579 cinfo^.cconvert^.start_pass (cinfo);
580 cinfo^.upsample^.start_pass (cinfo);
581 if (cinfo^.quantize_colors) then
582 cinfo^.cquantize^.start_pass (cinfo, master^.pub.is_dummy_pass);
583 if master^.pub.is_dummy_pass then
584 cinfo^.post^.start_pass (cinfo, JBUF_SAVE_AND_PASS)
585 else
586 cinfo^.post^.start_pass (cinfo, JBUF_PASS_THRU);
587 cinfo^.main^.start_pass (cinfo, JBUF_PASS_THRU);
588 end;
589 end;
591 { Set up progress monitor's pass info if present }
592 if (cinfo^.progress <> NIL) then
593 begin
594 cinfo^.progress^.completed_passes := master^.pass_number;
595 if master^.pub.is_dummy_pass then
596 cinfo^.progress^.total_passes := master^.pass_number + 2
597 else
598 cinfo^.progress^.total_passes := master^.pass_number + 1;
599 { In buffered-image mode, we assume one more output pass if EOI not
600 yet reached, but no more passes if EOI has been reached. }
602 if (cinfo^.buffered_image) and (not cinfo^.inputctl^.eoi_reached) then
603 begin
604 if cinfo^.enable_2pass_quant then
605 Inc(cinfo^.progress^.total_passes, 2)
606 else
607 Inc(cinfo^.progress^.total_passes, 1);
608 end;
609 end;
610 end;
613 { Finish up at end of an output pass. }
615 {METHODDEF}
616 procedure finish_output_pass (cinfo : j_decompress_ptr);
617 var
618 master : my_master_ptr;
619 begin
620 master := my_master_ptr (cinfo^.master);
622 if (cinfo^.quantize_colors) then
623 cinfo^.cquantize^.finish_pass (cinfo);
624 Inc(master^.pass_number);
625 end;
628 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
630 { Switch to a new external colormap between output passes. }
632 {GLOBAL}
633 procedure jpeg_new_colormap (cinfo : j_decompress_ptr);
634 var
635 master : my_master_ptr;
636 begin
637 master := my_master_ptr (cinfo^.master);
639 { Prevent application from calling me at wrong times }
640 if (cinfo^.global_state <> DSTATE_BUFIMAGE) then
641 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
643 if (cinfo^.quantize_colors) and (cinfo^.enable_external_quant) and
644 (cinfo^.colormap <> NIL) then
645 begin
646 { Select 2-pass quantizer for external colormap use }
647 cinfo^.cquantize := master^.quantizer_2pass;
648 { Notify quantizer of colormap change }
649 cinfo^.cquantize^.new_color_map (cinfo);
650 master^.pub.is_dummy_pass := FALSE; { just in case }
651 end
652 else
653 ERREXIT(j_common_ptr(cinfo), JERR_MODE_CHANGE);
654 end;
656 {$endif} { D_MULTISCAN_FILES_SUPPORTED }
659 { Initialize master decompression control and select active modules.
660 This is performed at the start of jpeg_start_decompress. }
662 {GLOBAL}
663 procedure jinit_master_decompress (cinfo : j_decompress_ptr);
664 var
665 master : my_master_ptr;
666 begin
667 master := my_master_ptr (
668 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
669 SIZEOF(my_decomp_master)) );
670 cinfo^.master := jpeg_decomp_master_ptr(master);
671 master^.pub.prepare_for_output_pass := prepare_for_output_pass;
672 master^.pub.finish_output_pass := finish_output_pass;
674 master^.pub.is_dummy_pass := FALSE;
676 master_selection(cinfo);
677 end;
679 end.