3 { This file contains optional default-setting code for the JPEG compressor.
4 Applications do not have to use this file, but those that don't use it
5 must know a lot more about the innards of the JPEG code. }
7 { Original: jcparam.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
9 interface
11 {$I imjconfig.inc}
13 uses
14 imjmorecfg,
15 imjinclude,
16 imjdeferr,
17 imjerror,
18 imjcomapi,
19 imjpeglib;
21 { Quantization table setup routines }
23 {GLOBAL}
30 {GLOBAL}
34 { Set or change the 'quality' (quantization) setting, using default tables
35 and a straight percentage-scaling quality scale. In most cases it's better
36 to use jpeg_set_quality (below); this entry point is provided for
37 applications that insist on a linear percentage scaling. }
39 {GLOBAL}
41 { Convert a user-specified quality rating to a percentage scaling factor
42 for an underlying quantization table, using our recommended scaling curve.
43 The input 'quality' factor should be 0 (terrible) to 100 (very good). }
45 {GLOBAL}
49 { Set or change the 'quality' (quantization) setting, using default tables.
50 This is the standard quality-adjusting entry point for typical user
51 interfaces; only those who want detailed control over quantization tables
52 would use the preceding three routines directly. }
54 {GLOBAL}
57 { Create a recommended progressive-JPEG script.
58 cinfo^.num_components and cinfo^.jpeg_color_space must be correct. }
60 { Set the JPEG colorspace, and choose colorspace-dependent default values. }
62 {GLOBAL}
66 { Select an appropriate JPEG colorspace for in_color_space. }
68 {GLOBAL}
71 {GLOBAL}
75 implementation
77 { Quantization table setup routines }
79 {GLOBAL}
85 { Define a quantization table equal to the basic_table times
86 a scale factor (given as a percentage).
87 If force_baseline is TRUE, the computed quantization table entries
88 are limited to 1..255 for JPEG baseline compatibility. }
89 var
93 begin
94 { Safety check to ensure start_compress not called yet. }
107 begin
109 { limit the values to the valid range }
119 { Initialize sent_table FALSE so table will be written to JPEG file. }
124 {GLOBAL}
128 { Set or change the 'quality' (quantization) setting, using default tables
129 and a straight percentage-scaling quality scale. In most cases it's better
130 to use jpeg_set_quality (below); this entry point is provided for
131 applications that insist on a linear percentage scaling. }
133 { These are the sample quantization tables given in JPEG spec section K.1.
134 The spec says that the values given produce "good" quality, and
135 when divided by 2, "very good" quality. }
137 const
148 const
158 begin
159 { Set up two quantization tables using the specified scaling }
167 {GLOBAL}
169 { Convert a user-specified quality rating to a percentage scaling factor
170 for an underlying quantization table, using our recommended scaling curve.
171 The input 'quality' factor should be 0 (terrible) to 100 (very good). }
172 begin
173 { Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. }
179 { The basic table is used as-is (scaling 100) for a quality of 50.
180 Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
181 note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
182 to make all the table entries 1 (hence, minimum quantization loss).
183 Qualities 1..50 are converted to scaling percentage 5000/Q. }
186 else
193 {GLOBAL}
197 { Set or change the 'quality' (quantization) setting, using default tables.
198 This is the standard quality-adjusting entry point for typical user
199 interfaces; only those who want detailed control over quantization tables
200 would use the preceding three routines directly. }
201 begin
202 { Convert user 0-100 rating to percentage scaling }
205 { Set up standard quality tables }
210 { Huffman table setup routines }
212 {LOCAL}
217 { Define a Huffman table }
218 var
220 begin
224 { Copy the number-of-symbols-of-each-code-length counts }
228 { Validate the counts. We do this here mainly so we can copy the right
229 number of symbols from the val[] array, without risking marching off
230 the end of memory. jchuff.c will do a more thorough test later. }
240 { Initialize sent_table FALSE so table will be written to JPEG file. }
245 {$J+}
246 {LOCAL}
248 { Set up the standard Huffman tables (cf. JPEG standard section K.3) }
249 { IMPORTANT: these are only valid for 8-bit data precision! }
309 begin
321 { Default parameter setup for compression.
323 Applications that don't choose to use this routine must do their
324 own setup of all these parameters. Alternately, you can call this
325 to establish defaults and then alter parameters selectively. This
326 is the recommended approach since, if we add any new parameters,
327 your code will still work (they'll be set to reasonable defaults). }
329 {GLOBAL}
331 var
333 begin
334 { Safety check to ensure start_compress not called yet. }
338 { Allocate comp_info array large enough for maximum component count.
339 Array is made permanent in case application wants to compress
340 multiple images at same param settings. }
347 { Initialize everything not dependent on the color space }
350 { Set up two quantization tables using default quality of 75 }
352 { Set up two Huffman tables }
355 { Initialize default arithmetic coding conditioning }
357 begin
363 { Default is no multiple-scan output }
367 { Expect normal source image, not raw downsampled data }
370 { Use Huffman coding, not arithmetic coding, by default }
373 { By default, don't do extra passes to optimize entropy coding }
375 { The standard Huffman tables are only valid for 8-bit data precision.
376 If the precision is higher, force optimization on so that usable
377 tables will be computed. This test can be removed if default tables
378 are supplied that are valid for the desired precision. }
383 { By default, use the simpler non-cosited sampling alignment }
386 { No input smoothing }
389 { DCT algorithm preference }
392 { No restart markers }
396 { Fill in default JFIF marker parameters. Note that whether the marker
397 will actually be written is determined by jpeg_set_colorspace.
399 By default, the library emits JFIF version code 1.01.
400 An application that wants to emit JFIF 1.02 extension markers should set
401 JFIF_minor_version to 2. We could probably get away with just defaulting
402 to 1.02, but there may still be some decoders in use that will complain
403 about that; saying 1.01 should minimize compatibility problems. }
411 { Choose JPEG colorspace based on input space, set defaults accordingly }
417 { Select an appropriate JPEG colorspace for in_color_space. }
419 {GLOBAL}
421 begin
423 JCS_GRAYSCALE:
425 JCS_RGB:
427 JCS_YCbCr:
429 JCS_CMYK:
431 JCS_YCCK:
433 JCS_UNKNOWN:
435 else
441 { Set the JPEG colorspace, and choose colorspace-dependent default values. }
443 {GLOBAL}
446 { macro }
448 begin
450 begin
460 var
462 begin
463 { Safety check to ensure start_compress not called yet. }
467 { For all colorspaces, we use Q and Huff tables 0 for luminance components,
468 tables 1 for chrominance components. }
476 JCS_GRAYSCALE:
477 begin
480 { JFIF specifies component ID 1 }
483 JCS_RGB:
484 begin
491 JCS_YCbCr:
492 begin
495 { JFIF specifies component IDs 1,2,3 }
496 { We default to 2x2 subsamples of chrominance }
501 JCS_CMYK:
502 begin
510 JCS_YCCK:
511 begin
519 JCS_UNKNOWN:
520 begin
527 begin
531 else
537 {$ifdef C_PROGRESSIVE_SUPPORTED}
539 {LOCAL}
544 { Support routine: generate one scan for specified component }
545 begin
556 {LOCAL}
561 { Support routine: generate one scan for each component }
562 var
564 begin
567 begin
579 {LOCAL}
583 { Support routine: generate interleaved DC scan if possible, else N scans }
584 var
586 begin
589 begin
590 { Single interleaved DC scan }
599 end
600 else
601 begin
602 { Noninterleaved DC scan for each component }
609 { Create a recommended progressive-JPEG script.
610 cinfo^.num_components and cinfo^.jpeg_color_space must be correct. }
612 {GLOBAL}
614 var
618 begin
621 { Safety check to ensure start_compress not called yet. }
625 { Figure space needed for script. Calculation must match code below! }
627 begin
628 { Custom script for YCbCr color images. }
630 end
631 else
632 begin
633 { All-purpose script for other color spaces. }
636 else
640 { Allocate space for script.
641 We need to put it in the permanent pool in case the application performs
642 multiple compressions without changing the settings. To avoid a memory
643 leak if jpeg_simple_progression is called repeatedly for the same JPEG
644 object, we try to re-use previously allocated space, and we allocate
645 enough space to handle YCbCr even if initially asked for grayscale. }
648 begin
651 else
664 begin
665 { Custom script for YCbCr color images. }
666 { Initial DC scan }
668 { Initial AC scan: get some luma data out in a hurry }
670 { Chroma data is too small to be worth expending many scans on }
673 { Complete spectral selection for luma AC }
675 { Refine next bit of luma AC }
677 { Finish DC successive approximation }
679 { Finish AC successive approximation }
682 { Luma bottom bit comes last since it's usually largest scan }
684 end
685 else
686 begin
687 { All-purpose script for other color spaces. }
688 { Successive approximation first pass }
692 { Successive approximation second pass }
694 { Successive approximation final pass }
700 {$endif}