-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDMARKER.PAS
2264 lines (1953 loc) · 66.8 KB
/
JDMARKER.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit JdMarker;
{ This file contains routines to decode JPEG datastream markers.
Most of the complexity arises from our desire to support input
suspension: if not all of the data for a marker is available;
we must exit back to the application. On resumption; we reprocess
the marker. }
{ Original: jdmarker.c; Copyright (C) 1991-1996; Thomas G. Lane. }
{ History
9.7.96 Conversion to pascal started jnn }
interface
{$I jconfig.inc}
uses
jmorecfg,
jinclude,
jdeferr,
jerror,
jcomapi,
jpeglib;
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 = uint; { JPEG marker codes }
{GLOBAL}
function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
desired : int) : boolean;
{GLOBAL}
procedure jinit_marker_reader (cinfo : j_decompress_ptr);
implementation
uses
jutils;
{ Macro used:
TRACEMS
TRACEMS1
TRACEMS3
ERREXIT
ERREXIT1
WARNMS2
TRACEMS2
GETJOCTET is better defined as a type = byte
The error constants
}
{ At all times, cinfo1.src.next_input_byte and .bytes_in_buffer reflect
the current restart point; we update them only when we have reached a
suitable place to restart if a suspension occurs. }
{ Routines to process JPEG markers.
Entry condition: JPEG marker itself has been read and its code saved
in cinfo^.unread_marker; input restart point is just after the marker.
Exit: if return TRUE, have read and processed any parameters, and have
updated the restart point to point after the parameters.
If return FALSE, was forced to suspend before reaching end of
marker parameters; restart point has not been moved. Same routine
will be called again after application supplies more input data.
This approach to suspension assumes that all of a marker's parameters can
fit into a single input bufferload. This should hold for "normal"
markers. Some COM/APPn markers might have large parameter segments,
but we use skip_input_data to get past those, and thereby put the problem
on the source manager's shoulders.
Note that we don't bother to avoid duplicate trace messages if a
suspension occurs within marker parameters. Other side effects
require more care. }
{LOCAL}
function get_soi (cinfo : j_decompress_ptr) : boolean;
{ Process an SOI marker }
var
i : int;
begin
TRACEMS(j_common_ptr(cinfo), 1, JTRC_SOI);
if (cinfo^.marker^.saw_SOI) then
ERREXIT(j_common_ptr(cinfo), JERR_SOI_DUPLICATE);
{ Reset all parameters that are defined to be reset by SOI }
for i := 0 to Pred(NUM_ARITH_TBLS) do
with cinfo^ do
begin
arith_dc_L[i] := 0;
arith_dc_U[i] := 1;
arith_ac_K[i] := 5;
end;
cinfo^.restart_interval := 0;
{ Set initial assumptions for colorspace etc }
with cinfo^ do
begin
jpeg_color_space := JCS_UNKNOWN;
CCIR601_sampling := FALSE; { Assume non-CCIR sampling??? }
saw_JFIF_marker := FALSE;
density_unit := 0; { set default JFIF APP0 values }
X_density := 1;
Y_density := 1;
saw_Adobe_marker := FALSE;
Adobe_transform := 0;
marker^.saw_SOI := TRUE;
end;
get_soi := TRUE;
end; { get_soi }
{LOCAL}
function get_sof(cinfo : j_decompress_ptr;
is_prog : boolean;
is_arith : boolean) : boolean;
{ Process a SOFn marker }
var
length : INT32;
c, ci : int;
compptr : jpeg_component_info_ptr;
{ Declare and initialize local copies of input pointer/count }
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{}
cinfo^.progressive_mode := is_prog;
cinfo^.arith_code := is_arith;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable cinfo^.data_precision.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.data_precision := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read two bytes interpreted as an unsigned 16-bit integer.
cinfo^.image_height should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.image_height := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( cinfo^.image_height, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read two bytes interpreted as an unsigned 16-bit integer.
cinfo^.image_width should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.image_width := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( cinfo^.image_width, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable cinfo^.num_components.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.num_components := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
Dec(length, 8);
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF, cinfo^.unread_marker,
int(cinfo^.image_width), int(cinfo^.image_height),
cinfo^.num_components);
if (cinfo^.marker^.saw_SOF) then
ERREXIT(j_common_ptr(cinfo), JERR_SOF_DUPLICATE);
{ We don't support files in which the image height is initially specified }
{ as 0 and is later redefined by DNL. As long as we have to check that, }
{ might as well have a general sanity check. }
if (cinfo^.image_height <= 0) or (cinfo^.image_width <= 0)
or (cinfo^.num_components <= 0) then
ERREXIT(j_common_ptr(cinfo), JERR_EMPTY_IMAGE);
if (length <> (cinfo^.num_components * 3)) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
if (cinfo^.comp_info = NIL) then { do only once, even if suspend }
cinfo^.comp_info := jpeg_component_info_ptr(cinfo^.mem^.alloc_small(
j_common_ptr(cinfo), JPOOL_IMAGE,
cinfo^.num_components * SIZEOF(jpeg_component_info)));
compptr := cinfo^.comp_info;
for ci := 0 to pred(cinfo^.num_components) do
begin
compptr^.component_index := ci;
{ Read a byte into variable compptr^.component_id.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
compptr^.component_id := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
compptr^.h_samp_factor := (c shr 4) and 15;
compptr^.v_samp_factor := (c ) and 15;
{ Read a byte into variable compptr^.quant_tbl_no.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
compptr^.quant_tbl_no := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF_COMPONENT,
compptr^.component_id, compptr^.h_samp_factor,
compptr^.v_samp_factor, compptr^.quant_tbl_no);
Inc(compptr);
end;
cinfo^.marker^.saw_SOF := TRUE;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_sof := TRUE;
end; { get_sof }
{LOCAL}
function get_sos (cinfo : j_decompress_ptr) : boolean;
{ Process a SOS marker }
label
id_found;
var
length : INT32;
i, ci, n, c, cc : int;
compptr : jpeg_component_info_ptr;
{ Declare and initialize local copies of input pointer/count }
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr; { Array[] of JOCTET; }
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{}
if not cinfo^.marker^.saw_SOF then
ERREXIT(j_common_ptr(cinfo), JERR_SOS_NO_SOF);
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable n (Number of components).
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
n := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
if ((length <> (n * 2 + 6)) or (n < 1) or (n > MAX_COMPS_IN_SCAN)) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_SOS, n);
cinfo^.comps_in_scan := n;
{ Collect the component-spec parameters }
for i := 0 to Pred(n) do
begin
{ Read a byte into variable cc. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cc := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
compptr := cinfo^.comp_info;
for ci := 0 to Pred(cinfo^.num_components) do
begin
if (cc = compptr^.component_id) then
goto id_found;
Inc(compptr);
end;
ERREXIT1(j_common_ptr(cinfo), JERR_BAD_COMPONENT_ID, cc);
id_found:
cinfo^.cur_comp_info[i] := compptr;
compptr^.dc_tbl_no := (c shr 4) and 15;
compptr^.ac_tbl_no := (c ) and 15;
TRACEMS3(j_common_ptr(cinfo), 1, JTRC_SOS_COMPONENT, cc,
compptr^.dc_tbl_no, compptr^.ac_tbl_no);
end;
{ Collect the additional scan parameters Ss, Se, Ah/Al. }
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Ss := c;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Se := c;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Ah := (c shr 4) and 15;
cinfo^.Al := (c ) and 15;
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOS_PARAMS, cinfo^.Ss, cinfo^.Se,
cinfo^.Ah, cinfo^.Al);
{ Prepare to scan data & restart markers }
cinfo^.marker^.next_restart_num := 0;
{ Count another SOS marker }
Inc( cinfo^.input_scan_number );
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_sos := TRUE;
end; { get_sos }
{METHODDEF}
function get_app0 (cinfo : j_decompress_ptr) : boolean; far;
{ Process an APP0 marker }
const
JFIF_LEN = 14;
var
length : INT32;
b : Array[0..JFIF_LEN-1] of UINT8;
buffp : int;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app0 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app0 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
length := length - 2;
{ See if a JFIF APP0 marker is present }
if (length >= JFIF_LEN) then
begin
for buffp := 0 to Pred(JFIF_LEN) do
begin
{ Read a byte into variable b[buffp].
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app0 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
b[buffp] := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
length := length - JFIF_LEN;
if (b[0]=$4A) and (b[1]=$46) and (b[2]=$49)
and (b[3]=$46) and (b[4]=0) then
begin
{ Found JFIF APP0 marker: check version }
{ Major version must be 1, anything else signals an incompatible change.
We used to treat this as an error, but now it's a nonfatal warning,
because some bozo at Hijaak couldn't read the spec.
Minor version should be 0..2, but process anyway if newer. }
if (b[5] <> 1) then
WARNMS2(j_common_ptr(cinfo) , JWRN_JFIF_MAJOR, b[5], b[6])
else
if (b[6] > 2) then
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_MINOR, b[5], b[6]);
{ Save info }
cinfo^.saw_JFIF_marker := TRUE;
cinfo^.density_unit := b[7];
cinfo^.X_density := (b[8] shl 8) + b[9];
cinfo^.Y_density := (b[10] shl 8) + b[11];
TRACEMS3(j_common_ptr(cinfo), 1, JTRC_JFIF,
cinfo^.X_density, cinfo^.Y_density, cinfo^.density_unit);
if (b[12] or b[13]) <> 0 then
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);
if (length <> (INT32(b[12]) * INT32(b[13]) * INT32(3))) then
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_JFIF_BADTHUMBNAILSIZE,
int(length));
end
else
begin
{ Start of APP0 does not match "JFIF" }
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP0, int(length) + JFIF_LEN);
end;
end
else
begin
{ Too short to be JFIF marker }
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP0, int(length));
end;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
if (length > 0) then { skip any remaining data -- could be lots }
cinfo^.src^.skip_input_data(cinfo, long(length));
get_app0 := TRUE;
end; { get_app0 }
{METHODDEF}
function get_app14 (cinfo : j_decompress_ptr) : boolean; far;
{ Process an APP14 marker }
const
ADOBE_LEN = 12;
var
length : INT32;
b : Array[0..ADOBE_LEN-1] of UINT8;
buffp : int;
version, flags0, flags1, transform : uint;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app14 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app14 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
{ See if an Adobe APP14 marker is present }
if (length >= ADOBE_LEN) then
begin
for buffp := 0 to Pred(ADOBE_LEN) do
begin
{ Read a byte into variable b[buffp]. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_app14 := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
b[buffp] := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
length := length - ADOBE_LEN;
if (b[0]=$41) and (b[1]=$64) and (b[2]=$6F) and (b[3]=$62) and (b[4]=$65) then
begin
{ Found Adobe APP14 marker }
version := (b[5] shl 8) + b[6];
flags0 := (b[7] shl 8) + b[8];
flags1 := (b[9] shl 8) + b[10];
transform := b[11];
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_ADOBE,
version, flags0, flags1, transform);
cinfo^.saw_Adobe_marker := TRUE;
cinfo^.Adobe_transform := UINT8(transform);
end
else
begin
{ Start of APP14 does not match "Adobe" }
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP14, int(length) + ADOBE_LEN);
end;
end
else
begin
{ Too short to be Adobe marker }
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP14, int(length));
end;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
if (length > 0) then { skip any remaining data -- could be lots }
cinfo^.src^.skip_input_data (cinfo, long(length));
get_app14 := TRUE;
end; { get_app14 }
{LOCAL}
function get_dac (cinfo : j_decompress_ptr) : boolean;
{ Process a DAC marker }
var
length : INT32;
index, val : int;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin