3 { This file contains routines to write JPEG datastream markers. }
5 { Original: jcmarker.c; Copyright (C) 1991-1998, Thomas G. Lane. }
7 interface
9 {$I imjconfig.inc}
11 uses
16 const
17 { JPEG marker codes }
83 type
86 { Private state }
88 type
99 {GLOBAL}
102 implementation
104 { Basic output routines.
106 Note that we do not support suspension while writing a marker.
107 Therefore, an application using suspension must ensure that there is
108 enough buffer space for the initial markers (typ. 600-700 bytes) before
109 calling jpeg_start_compress, and enough space to write the trailing EOI
110 (a few bytes) before calling jpeg_finish_compress. Multipass compression
111 modes are not supported at all with suspension, so those two are the only
112 points where markers will be written. }
115 {LOCAL}
117 { Emit a byte }
118 var
120 begin
128 begin
135 {LOCAL}
137 { Emit a marker code }
138 begin
144 {LOCAL}
146 { Emit a 2-byte integer; these are always MSB first in JPEG files }
147 begin
153 { Routines to write specific marker types. }
155 {LOCAL}
157 { Emit a DQT marker }
158 { Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking }
159 var
163 var
165 begin
172 begin
178 begin
183 else
189 begin
190 { The table entries must be emitted in zigzag order. }
204 {LOCAL}
206 { Emit a DHT marker }
207 var
210 begin
212 begin
215 end
216 else
217 begin
225 begin
246 {LOCAL}
248 { Emit a DAC marker }
249 { Since the useful info is so small, we want to emit all the tables in }
250 { one DAC marker. Therefore this routine does its own scan of the table. }
251 {$ifdef C_ARITH_CODING_SUPPORTED}
252 var
257 begin
259 begin
265 begin
280 begin
282 begin
287 begin
293 {$else}
294 begin
299 {LOCAL}
301 { Emit a DRI marker }
302 begin
311 {LOCAL}
313 { Emit a SOF marker }
314 var
317 begin
322 { Make sure image isn't bigger than SOF field can handle }
335 begin
344 {LOCAL}
346 { Emit a SOS marker }
347 var
350 begin
358 begin
364 begin
365 { Progressive mode: only DC or only AC tables are used in one scan;
366 furthermore, Huffman coding of DC refinement uses no table at all.
367 We emit 0 for unused field(s); this is recommended by the P&M text
368 but does not seem to be specified in the standard. }
371 begin
375 end
376 else
377 begin
390 {LOCAL}
392 { Emit a JFIF-compliant APP0 marker }
393 {
394 Length of APP0 block (2 bytes)
395 Block ID (4 bytes - ASCII "JFIF")
396 Zero byte (1 byte to terminate the ID string)
397 Version Major, Minor (2 bytes - major first)
398 Units (1 byte - $00 = none, $01 = inch, $02 = cm)
399 Xdpu (2 bytes - dots per unit horizontal)
400 Ydpu (2 bytes - dots per unit vertical)
401 Thumbnail X size (1 byte)
402 Thumbnail Y size (1 byte)
403 }
404 begin
424 {LOCAL}
426 { Emit an Adobe APP14 marker }
427 {
428 Length of APP14 block (2 bytes)
429 Block ID (5 bytes - ASCII "Adobe")
430 Version Number (2 bytes - currently 100)
431 Flags0 (2 bytes - currently 0)
432 Flags1 (2 bytes - currently 0)
433 Color transform (1 byte)
435 Although Adobe TN 5116 mentions Version = 101, all the Adobe files
436 now in circulation seem to use Version = 100, so that's what we write.
438 We write the color transform byte as 1 if the JPEG color space is
439 YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
440 whether the encoder performed a transformation, which is pretty useless.
441 }
442 begin
456 JCS_YCbCr:
458 JCS_YCCK:
460 else
466 { These routines allow writing an arbitrary marker with parameters.
467 The only intended use is to emit COM or APPn markers after calling
468 write_file_header and before calling write_frame_header.
469 Other uses are not guaranteed to produce desirable results.
470 Counting the parameter bytes properly is the caller's responsibility. }
472 {METHODDEF}
476 { Emit an arbitrary marker header }
477 begin
486 {METHODDEF}
488 { Emit one byte of marker parameters following write_marker_header }
489 begin
493 { Write datastream header.
494 This consists of an SOI and optional APPn markers.
495 We recommend use of the JFIF marker, but not the Adobe marker,
496 when using YCbCr or grayscale data. The JFIF marker should NOT
497 be used for any other JPEG colorspace. The Adobe marker is helpful
498 to distinguish RGB, CMYK, and YCCK colorspaces.
499 Note that an application can write additional header markers after
500 jpeg_start_compress returns. }
503 {METHODDEF}
505 var
507 begin
512 { SOI is defined to reset restart interval to 0 }
522 { Write frame header.
523 This consists of DQT and SOFn markers.
524 Note that we do not emit the SOF until we have emitted the DQT(s).
525 This avoids compatibility problems with incorrect implementations that
526 try to error-check the quant table numbers as soon as they see the SOF. }
529 {METHODDEF}
531 var
535 begin
536 { Emit DQT for each quantization table.
537 Note that emit_dqt() suppresses any duplicate tables. }
542 begin
546 { now prec is nonzero iff there are any 16-bit quant tables. }
548 { Check for a non-baseline specification.
549 Note we assume that Huffman table numbers won't be changed later. }
553 begin
555 end
556 else
557 begin
561 begin
567 begin
569 { If it's baseline except for quantizer size, warn the user }
570 {$IFDEF DEBUG}
572 {$ENDIF}
576 { Emit the proper SOF marker }
578 begin
580 end
581 else
582 begin
587 else
593 { Write scan header.
594 This consists of DHT or DAC markers, optional DRI, and SOS.
595 Compressed data will be written following the SOS. }
597 {METHODDEF}
599 var
603 begin
606 begin
607 { Emit arith conditioning info. We may have some duplication
608 if the file has multiple scans, but it's so small it's hardly
609 worth worrying about. }
611 end
612 else
613 begin
614 { Emit Huffman tables.
615 Note that emit_dht() suppresses any duplicate tables. }
617 begin
620 begin
621 { Progressive mode: only DC or only AC tables are used in one scan }
623 begin
626 end
627 else
628 begin
631 end
632 else
633 begin
634 { Sequential mode: need both DC and AC tables }
641 { Emit DRI if required --- note that DRI value could change for each scan.
642 We avoid wasting space with unnecessary DRIs, however. }
645 begin
655 { Write datastream trailer. }
658 {METHODDEF}
660 begin
665 { Write an abbreviated table-specification datastream.
666 This consists of SOI, DQT and DHT tables, and EOI.
667 Any table that is defined and not marked sent_table = TRUE will be
668 emitted. Note that all tables will be marked sent_table = TRUE at exit. }
671 {METHODDEF}
673 var
675 begin
679 begin
685 begin
687 begin
699 { Initialize the marker writer module. }
701 {GLOBAL}
703 var
705 begin
706 { Create the subobject }
711 { Initialize method pointers }
719 { Initialize private state }