-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJCMARKER.PAS
695 lines (568 loc) · 17.2 KB
/
JCMARKER.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
Unit jcmarker;
{ This file contains routines to write JPEG datastream markers. }
{ Original: jcmarker.c; Copyright (C) 1991-1996, Thomas G. Lane. }
interface
{$I jconfig.inc}
uses
jinclude, jmorecfg, jerror,
jdeferr, jpeglib, jutils;
const
{ JPEG marker codes }
M_SOF0 = $c0;
M_SOF1 = $c1;
M_SOF2 = $c2;
M_SOF3 = $c3;
M_SOF5 = $c5;
M_SOF6 = $c6;
M_SOF7 = $c7;
M_JPG = $c8;
M_SOF9 = $c9;
M_SOF10 = $ca;
M_SOF11 = $cb;
M_SOF13 = $cd;
M_SOF14 = $ce;
M_SOF15 = $cf;
M_DHT = $c4;
M_DAC = $cc;
M_RST0 = $d0;
M_RST1 = $d1;
M_RST2 = $d2;
M_RST3 = $d3;
M_RST4 = $d4;
M_RST5 = $d5;
M_RST6 = $d6;
M_RST7 = $d7;
M_SOI = $d8;
M_EOI = $d9;
M_SOS = $da;
M_DQT = $db;
M_DNL = $dc;
M_DRI = $dd;
M_DHP = $de;
M_EXP = $df;
M_APP0 = $e0;
M_APP1 = $e1;
M_APP2 = $e2;
M_APP3 = $e3;
M_APP4 = $e4;
M_APP5 = $e5;
M_APP6 = $e6;
M_APP7 = $e7;
M_APP8 = $e8;
M_APP9 = $e9;
M_APP10 = $ea;
M_APP11 = $eb;
M_APP12 = $ec;
M_APP13 = $ed;
M_APP14 = $ee;
M_APP15 = $ef;
M_JPG0 = $f0;
M_JPG13 = $fd;
M_COM = $fe;
M_TEM = $01;
M_ERROR = $100;
type
JPEG_MARKER = Word;
{GLOBAL}
procedure jinit_marker_writer (cinfo : j_compress_ptr);
implementation
{ Basic output routines.
Note that we do not support suspension while writing a marker.
Therefore, an application using suspension must ensure that there is
enough buffer space for the initial markers (typ. 600-700 bytes) before
calling jpeg_start_compress, and enough space to write the trailing EOI
(a few bytes) before calling jpeg_finish_compress. Multipass compression
modes are not supported at all with suspension, so those two are the only
points where markers will be written. }
{LOCAL}
procedure emit_byte (cinfo : j_compress_ptr; val : int);
{ Emit a byte }
var
dest : jpeg_destination_mgr_ptr;
begin
dest := cinfo^.dest;
dest^.next_output_byte^ := JOCTET(val);
Inc(dest^.next_output_byte);
Dec(dest^.free_in_buffer);
if (dest^.free_in_buffer = 0) then
begin
if not dest^.empty_output_buffer(cinfo) then
ERREXIT(j_common_ptr(cinfo), JERR_CANT_SUSPEND);
end;
end;
{LOCAL}
procedure emit_marker(cinfo : j_compress_ptr; mark : JPEG_MARKER);
{ Emit a marker code }
begin
emit_byte(cinfo, $FF);
emit_byte(cinfo, int(mark));
end;
{LOCAL}
procedure emit_2bytes (cinfo : j_compress_ptr; value : int);
{ Emit a 2-byte integer; these are always MSB first in JPEG files }
begin
emit_byte(cinfo, (value shr 8) and $FF);
emit_byte(cinfo, value and $FF);
end;
{ Routines to write specific marker types. }
{LOCAL}
function emit_dqt (cinfo : j_compress_ptr; index : int) : int;
{ Emit a DQT marker }
{ Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking }
var
qtbl : JQUANT_TBL_PTR;
prec : int;
i : int;
qval : uint;
begin
qtbl := cinfo^.quant_tbl_ptrs[index];
if (qtbl = NIL) then
ERREXIT1(j_common_ptr(cinfo), JERR_NO_QUANT_TABLE, index);
prec := 0;
for i := 0 to Pred(DCTSIZE2) do
begin
if (qtbl^.quantval[i] > 255) then
prec := 1;
end;
if not qtbl^.sent_table then
begin
emit_marker(cinfo, M_DQT);
if (prec <> 0) then
emit_2bytes(cinfo, DCTSIZE2*2 + 1 + 2)
else
emit_2bytes(cinfo, DCTSIZE2 + 1 + 2);
emit_byte(cinfo, index + (prec shl 4));
for i := 0 to Pred(DCTSIZE2) do
begin
{ The table entries must be emitted in zigzag order. }
qval := qtbl^.quantval[jpeg_natural_order[i]];
if (prec <> 0) then
emit_byte(cinfo, qval shr 8);
emit_byte(cinfo, qval and $FF);
end;
qtbl^.sent_table := TRUE;
end;
emit_dqt := prec;
end;
{LOCAL}
procedure emit_dht (cinfo : j_compress_ptr; index : int; is_ac : boolean);
{ Emit a DHT marker }
var
htbl : JHUFF_TBL_PTR;
length, i : int;
begin
if (is_ac) then
begin
htbl := cinfo^.ac_huff_tbl_ptrs[index];
index := index + $10; { output index has AC bit set }
end
else
begin
htbl := cinfo^.dc_huff_tbl_ptrs[index];
end;
if (htbl = NIL) then
ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, index);
if not htbl^.sent_table then
begin
emit_marker(cinfo, M_DHT);
length := 0;
for i := 1 to 16 do
length := length + htbl^.bits[i];
emit_2bytes(cinfo, length + 2 + 1 + 16);
emit_byte(cinfo, index);
for i := 1 to 16 do
emit_byte(cinfo, htbl^.bits[i]);
for i := 0 to Pred(length) do
emit_byte(cinfo, htbl^.huffval[i]);
htbl^.sent_table := TRUE;
end;
end;
{LOCAL}
procedure emit_dac (cinfo : j_compress_ptr);
{ Emit a DAC marker }
{ Since the useful info is so small, we want to emit all the tables in }
{ one DAC marker. Therefore this routine does its own scan of the table. }
{$ifdef C_ARITH_CODING_SUPPORTED}
var
dc_in_use : array[0..NUM_ARITH_TBLS] of byte;
ac_in_use : array[0..NUM_ARITH_TBLS] of byte;
length, i : int;
compptr : jpeg_component_info_ptr;
begin
for i := 0 to pred(NUM_ARITH_TBLS) do
begin
dc_in_use[i] := 0;
ac_in_use[i] := 0;
end;
for i := 0 to pred(cinfo^.comps_in_scan) do
begin
compptr := cinfo^.cur_comp_info[i];
dc_in_use[compptr^.dc_tbl_no] := 1;
ac_in_use[compptr^.ac_tbl_no] := 1;
end;
length := 0;
for i := 0 to pred(NUM_ARITH_TBLS) do
Inc(length, dc_in_use[i] + ac_in_use[i]);
emit_marker(cinfo, M_DAC);
emit_2bytes(cinfo, length*2 + 2);
for i := 0 to pred(NUM_ARITH_TBLS) do
begin
if (dc_in_use[i] <> 0) then
begin
emit_byte(cinfo, i);
emit_byte(cinfo, cinfo^.arith_dc_L[i] + (cinfo^.arith_dc_U[i] shl 4));
end;
if (ac_in_use[i] <> 0) then
begin
emit_byte(cinfo, i + $10);
emit_byte(cinfo, cinfo^.arith_ac_K[i]);
end;
end;
end;
{$else}
begin
end;
{$endif} {C_ARITH_CODING_SUPPORTED}
{LOCAL}
procedure emit_dri (cinfo : j_compress_ptr);
{ Emit a DRI marker }
begin
emit_marker(cinfo, M_DRI);
emit_2bytes(cinfo, 4); { fixed length }
emit_2bytes(cinfo, int(cinfo^.restart_interval));
end;
{LOCAL}
procedure emit_sof (cinfo : j_compress_ptr; code : JPEG_MARKER);
{ Emit a SOF marker }
var
ci : int;
compptr : jpeg_component_info_ptr;
begin
emit_marker(cinfo, code);
emit_2bytes(cinfo, 3 * cinfo^.num_components + 2 + 5 + 1); { length }
{ Make sure image isn't bigger than SOF field can handle }
if (long(cinfo^.image_height) > long(65535)) or
(long(cinfo^.image_width) > long(65535)) then
ERREXIT1(j_common_ptr(cinfo), JERR_IMAGE_TOO_BIG, uInt(65535));
emit_byte(cinfo, cinfo^.data_precision);
emit_2bytes(cinfo, int(cinfo^.image_height));
emit_2bytes(cinfo, int(cinfo^.image_width));
emit_byte(cinfo, cinfo^.num_components);
compptr := cinfo^.comp_info;
for ci := 0 to Pred(cinfo^.num_components) do
begin
emit_byte(cinfo, compptr^.component_id);
emit_byte(cinfo, (compptr^.h_samp_factor shl 4) + compptr^.v_samp_factor);
emit_byte(cinfo, compptr^.quant_tbl_no);
Inc(compptr);
end;
end;
{LOCAL}
procedure emit_sos (cinfo : j_compress_ptr);
{ Emit a SOS marker }
var
i, td, ta : int;
compptr : jpeg_component_info_ptr;
begin
emit_marker(cinfo, M_SOS);
emit_2bytes(cinfo, 2 * cinfo^.comps_in_scan + 2 + 1 + 3); { length }
emit_byte(cinfo, cinfo^.comps_in_scan);
for i := 0 to Pred(cinfo^.comps_in_scan) do
begin
compptr := cinfo^.cur_comp_info[i];
emit_byte(cinfo, compptr^.component_id);
td := compptr^.dc_tbl_no;
ta := compptr^.ac_tbl_no;
if (cinfo^.progressive_mode) then
begin
{ Progressive mode: only DC or only AC tables are used in one scan;
furthermore, Huffman coding of DC refinement uses no table at all.
We emit 0 for unused field(s); this is recommended by the P&M text
but does not seem to be specified in the standard. }
if (cinfo^.Ss = 0) then
begin
ta := 0; { DC scan }
if (cinfo^.Ah <> 0) and not cinfo^.arith_code then
td := 0; { no DC table either }
end
else
begin
td := 0; { AC scan }
end;
end;
emit_byte(cinfo, (td shl 4) + ta);
end;
emit_byte(cinfo, cinfo^.Ss);
emit_byte(cinfo, cinfo^.Se);
emit_byte(cinfo, (cinfo^.Ah shl 4) + cinfo^.Al);
end;
{LOCAL}
procedure emit_jfif_app0 (cinfo : j_compress_ptr);
{ Emit a JFIF-compliant APP0 marker }
{
Length of APP0 block (2 bytes)
Block ID (4 bytes - ASCII "JFIF")
Zero byte (1 byte to terminate the ID string)
Version Major, Minor (2 bytes - $01, $01)
Units (1 byte - $00 = none, $01 = inch, $02 = cm)
Xdpu (2 bytes - dots per unit horizontal)
Ydpu (2 bytes - dots per unit vertical)
Thumbnail X size (1 byte)
Thumbnail Y size (1 byte)
}
begin
emit_marker(cinfo, M_APP0);
emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); { length }
emit_byte(cinfo, $4A); { Identifier: ASCII "JFIF" }
emit_byte(cinfo, $46);
emit_byte(cinfo, $49);
emit_byte(cinfo, $46);
emit_byte(cinfo, 0);
{ We currently emit version code 1.01 since we use no 1.02 features.
This may avoid complaints from some older decoders. }
emit_byte(cinfo, 1); { Major version }
emit_byte(cinfo, 1); { Minor version }
emit_byte(cinfo, cinfo^.density_unit); { Pixel size information }
emit_2bytes(cinfo, int(cinfo^.X_density));
emit_2bytes(cinfo, int(cinfo^.Y_density));
emit_byte(cinfo, 0); { No thumbnail image }
emit_byte(cinfo, 0);
end;
{LOCAL}
procedure emit_adobe_app14 (cinfo : j_compress_ptr);
{ Emit an Adobe APP14 marker }
{
Length of APP14 block (2 bytes)
Block ID (5 bytes - ASCII "Adobe")
Version Number (2 bytes - currently 100)
Flags0 (2 bytes - currently 0)
Flags1 (2 bytes - currently 0)
Color transform (1 byte)
Although Adobe TN 5116 mentions Version = 101, all the Adobe files
now in circulation seem to use Version = 100, so that's what we write.
We write the color transform byte as 1 if the JPEG color space is
YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
whether the encoder performed a transformation, which is pretty useless.
}
begin
emit_marker(cinfo, M_APP14);
emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); { length }
emit_byte(cinfo, $41); { Identifier: ASCII "Adobe" }
emit_byte(cinfo, $64);
emit_byte(cinfo, $6F);
emit_byte(cinfo, $62);
emit_byte(cinfo, $65);
emit_2bytes(cinfo, 100); { Version }
emit_2bytes(cinfo, 0); { Flags0 }
emit_2bytes(cinfo, 0); { Flags1 }
case (cinfo^.jpeg_color_space) of
JCS_YCbCr:
emit_byte(cinfo, 1); { Color transform = 1 }
JCS_YCCK:
emit_byte(cinfo, 2); { Color transform = 2 }
else
emit_byte(cinfo, 0); { Color transform = 0 }
end;
end;
{ This routine is exported for possible use by applications.
The intended use is to emit COM or APPn markers after calling
jpeg_start_compress() and before the first jpeg_write_scanlines() call
(hence, after write_file_header but before write_frame_header).
Other uses are not guaranteed to produce desirable results. }
{METHODDEF}
procedure write_any_marker (cinfo : j_compress_ptr; marker : int;
dataptr : JOCTETptr;
datalen : uint); far;
{ Emit an arbitrary marker with parameters }
begin
if (datalen <= uInt(65533)) then { safety check }
begin
emit_marker(cinfo, JPEG_MARKER(marker));
emit_2bytes(cinfo, int(datalen + 2)); { total length }
while (datalen <> 0) do
begin
Dec(datalen);
emit_byte(cinfo, dataptr^);
Inc(dataptr);
end;
end;
end;
{ Write datastream header.
This consists of an SOI and optional APPn markers.
We recommend use of the JFIF marker, but not the Adobe marker,
when using YCbCr or grayscale data. The JFIF marker should NOT
be used for any other JPEG colorspace. The Adobe marker is helpful
to distinguish RGB, CMYK, and YCCK colorspaces.
Note that an application can write additional header markers after
jpeg_start_compress returns. }
{METHODDEF}
procedure write_file_header (cinfo : j_compress_ptr); far;
begin
emit_marker(cinfo, M_SOI); { first the SOI }
if (cinfo^.write_JFIF_header) then { next an optional JFIF APP0 }
emit_jfif_app0(cinfo);
if (cinfo^.write_Adobe_marker) then { next an optional Adobe APP14 }
emit_adobe_app14(cinfo);
end;
{ Write frame header.
This consists of DQT and SOFn markers.
Note that we do not emit the SOF until we have emitted the DQT(s).
This avoids compatibility problems with incorrect implementations that
try to error-check the quant table numbers as soon as they see the SOF. }
{METHODDEF}
procedure write_frame_header (cinfo : j_compress_ptr); far;
var
ci, prec : int;
is_baseline : boolean;
compptr : jpeg_component_info_ptr;
begin
{ Emit DQT for each quantization table.
Note that emit_dqt() suppresses any duplicate tables. }
prec := 0;
compptr := cinfo^.comp_info;
for ci := 0 to Pred(cinfo^.num_components) do
begin
prec := prec + emit_dqt(cinfo, compptr^.quant_tbl_no);
Inc(compptr);
end;
{ now prec is nonzero iff there are any 16-bit quant tables. }
{ Check for a non-baseline specification.
Note we assume that Huffman table numbers won't be changed later. }
if (cinfo^.arith_code) or (cinfo^.progressive_mode)
or (cinfo^.data_precision <> 8) then
begin
is_baseline := FALSE;
end
else
begin
is_baseline := TRUE;
compptr := cinfo^.comp_info;
for ci := 0 to Pred(cinfo^.num_components) do
begin
if (compptr^.dc_tbl_no > 1) or (compptr^.ac_tbl_no > 1) then
is_baseline := FALSE;
Inc(compptr);
end;
if (prec <> 0) and (is_baseline) then
begin
is_baseline := FALSE;
{ If it's baseline except for quantizer size, warn the user }
TRACEMS(j_common_ptr(cinfo), 0, JTRC_16BIT_TABLES);
end;
end;
{ Emit the proper SOF marker }
if (cinfo^.arith_code) then
begin
emit_sof(cinfo, M_SOF9); { SOF code for arithmetic coding }
end
else
begin
if (cinfo^.progressive_mode) then
emit_sof(cinfo, M_SOF2) { SOF code for progressive Huffman }
else if (is_baseline) then
emit_sof(cinfo, M_SOF0) { SOF code for baseline implementation }
else
emit_sof(cinfo, M_SOF1); { SOF code for non-baseline Huffman file }
end;
end;
{ Write scan header.
This consists of DHT or DAC markers, optional DRI, and SOS.
Compressed data will be written following the SOS. }
{METHODDEF}
procedure write_scan_header (cinfo : j_compress_ptr); far;
var
i : int;
compptr : jpeg_component_info_ptr;
begin
if (cinfo^.arith_code) then
begin
{ Emit arith conditioning info. We may have some duplication
if the file has multiple scans, but it's so small it's hardly
worth worrying about. }
emit_dac(cinfo);
end
else
begin
{ Emit Huffman tables.
Note that emit_dht() suppresses any duplicate tables. }
for i := 0 to Pred(cinfo^.comps_in_scan) do
begin
compptr := cinfo^.cur_comp_info[i];
if (cinfo^.progressive_mode) then
begin
{ Progressive mode: only DC or only AC tables are used in one scan }
if (cinfo^.Ss = 0) then
begin
if (cinfo^.Ah = 0) then { DC needs no table for refinement scan }
emit_dht(cinfo, compptr^.dc_tbl_no, FALSE);
end
else
begin
emit_dht(cinfo, compptr^.ac_tbl_no, TRUE);
end;
end
else
begin
{ Sequential mode: need both DC and AC tables }
emit_dht(cinfo, compptr^.dc_tbl_no, FALSE);
emit_dht(cinfo, compptr^.ac_tbl_no, TRUE);
end;
end;
end;
{ Emit DRI if required --- note that DRI value could change for each scan.
If it doesn't, a tiny amount of space is wasted in multiple-scan files.
We assume DRI will never be nonzero for one scan and zero for a later one.}
if (cinfo^.restart_interval <> 0) then
emit_dri(cinfo);
emit_sos(cinfo);
end;
{ Write datastream trailer. }
{METHODDEF}
procedure write_file_trailer (cinfo : j_compress_ptr); far;
begin
emit_marker(cinfo, M_EOI);
end;
{ Write an abbreviated table-specification datastream.
This consists of SOI, DQT and DHT tables, and EOI.
Any table that is defined and not marked sent_table = TRUE will be
emitted. Note that all tables will be marked sent_table = TRUE at exit. }
{METHODDEF}
procedure write_tables_only (cinfo : j_compress_ptr); far;
var
i : int;
begin
emit_marker(cinfo, M_SOI);
for i := 0 to Pred(NUM_QUANT_TBLS) do
begin
if (cinfo^.quant_tbl_ptrs[i] <> NIL) then
emit_dqt(cinfo, i); { dummy := ... }
end;
if (not cinfo^.arith_code) then
begin
for i := 0 to Pred(NUM_HUFF_TBLS) do
begin
if (cinfo^.dc_huff_tbl_ptrs[i] <> NIL) then
emit_dht(cinfo, i, FALSE);
if (cinfo^.ac_huff_tbl_ptrs[i] <> NIL) then
emit_dht(cinfo, i, TRUE);
end;
end;
emit_marker(cinfo, M_EOI);
end;
{ Initialize the marker writer module. }
{GLOBAL}
procedure jinit_marker_writer (cinfo : j_compress_ptr);
begin
{ Create the subobject }
cinfo^.marker := jpeg_marker_writer_ptr
( cinfo^.mem^.alloc_small(j_common_ptr(cinfo), JPOOL_IMAGE,
SIZEOF(jpeg_marker_writer)));
{ Initialize method pointers }
cinfo^.marker^.write_any_marker := write_any_marker;
cinfo^.marker^.write_file_header := write_file_header;
cinfo^.marker^.write_frame_header := write_frame_header;
cinfo^.marker^.write_scan_header := write_scan_header;
cinfo^.marker^.write_file_trailer := write_file_trailer;
cinfo^.marker^.write_tables_only := write_tables_only;
end;
end.