-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gid-decoding_jpg.adb
2114 lines (1959 loc) · 75.8 KB
/
gid-decoding_jpg.adb
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
-- GID's JPEG baseline DCT decoder is largely inspired
-- by the NanoJPEG code by Martin J. Fiedler,
-- available under the MIT Licence. Web link:
-- https://keyj.emphy.de/nanojpeg/
-- The JPEG progressive DCT decoder is largely inspired
-- by the PyJpegDecoder code by Tiago Becerra Paolini,
-- available under the MIT Licence. Web link:
-- https://github.com/tbpaolini/PyJpegDecoder
-- Other informations:
-- JPEG standard
-- ISO/IEC 10918-1 : 1993(E)
-- CCITT Rec. T.81 (1992 E)
-- https://www.w3.org/Graphics/JPEG/itu-t81.pdf
--
-- General information:
-- http://en.wikipedia.org/wiki/JPEG
-- Steps for decoding a JPEG image
--
-- 1. Huffman decompression
-- 2. Inverse quantization
-- 3. Inverse cosine transform
-- 4. Upsampling
-- 5. Color transformation
-- 6. Image reconstruction
with GID.Buffering;
with Ada.Integer_Text_IO, Ada.IO_Exceptions,
Ada.Text_IO, Ada.Unchecked_Deallocation;
package body GID.Decoding_JPG is
use Buffering, Ada.Text_IO, Interfaces;
generic
type Number is mod <>;
procedure Big_Endian_Number
(from : in out Input_Buffer;
n : out Number);
pragma Inline (Big_Endian_Number);
procedure Big_Endian_Number
(from : in out Input_Buffer;
n : out Number)
is
b : U8;
begin
n := 0;
for i in 1 .. Number'Size / 8 loop
Get_Byte (from, b);
n := n * 256 + Number (b);
end loop;
end Big_Endian_Number;
procedure Big_Endian is new Big_Endian_Number (U16);
-- B.1.1.3 Marker assignments
COM_code : constant := 16#FE#;
DHT_code : constant := 16#C4#;
DRI_code : constant := 16#DD#;
EOI_code : constant := 16#D9#;
--
RST_0_code : constant := 16#D0#;
RST_1_code : constant := 16#D1#;
RST_2_code : constant := 16#D2#;
RST_3_code : constant := 16#D3#;
RST_4_code : constant := 16#D4#;
RST_5_code : constant := 16#D5#;
RST_6_code : constant := 16#D6#;
RST_7_code : constant := 16#D7#;
--
SOS_code : constant := 16#DA#;
marker_id : constant array (JPEG_Marker) of U8 :=
(SOI => 16#D8#,
--
SOF_0 => 16#C0#, SOF_1 => 16#C1#, SOF_2 => 16#C2#, SOF_3 => 16#C3#,
SOF_5 => 16#C5#, SOF_6 => 16#C6#, SOF_7 => 16#C7#, SOF_8 => 16#C8#,
SOF_9 => 16#C9#, SOF_10 => 16#CA#, SOF_11 => 16#CB#, SOF_13 => 16#CD#,
SOF_14 => 16#CE#, SOF_15 => 16#CF#,
--
DAC => 16#CC#,
DHT => DHT_code,
DQT => 16#DB#,
DRI => DRI_code,
--
RST_0 => RST_0_code,
RST_1 => RST_1_code,
RST_2 => RST_2_code,
RST_3 => RST_3_code,
RST_4 => RST_4_code,
RST_5 => RST_5_code,
RST_6 => RST_6_code,
RST_7 => RST_7_code,
--
APP_0 => 16#E0#, APP_1 => 16#E1#, APP_2 => 16#E2#, APP_3 => 16#E3#,
APP_4 => 16#E4#, APP_5 => 16#E5#, APP_6 => 16#E6#, APP_7 => 16#E7#,
APP_8 => 16#E8#, APP_9 => 16#E9#, APP_10 => 16#EA#, APP_11 => 16#EB#,
APP_12 => 16#EC#, APP_13 => 16#ED#, APP_14 => 16#EE#,
--
COM => COM_code,
SOS => SOS_code,
EOI => EOI_code);
function Marker_Image (m : U8) return String is
package BIO is new Modular_IO (U8);
hexa : String (1 .. 6);
begin
BIO.Put (hexa, m, 16);
for jm in JPEG_Marker loop
if marker_id (jm) = m then
return hexa & ", marker: " & jm'Image;
end if;
end loop;
return hexa;
end Marker_Image;
procedure Read
(image : in out Image_Descriptor;
known_marker : in Boolean;
buffered_marker : in U8;
head : out Segment_Head)
is
b : U8;
begin
if known_marker then
if full_trace then
Put_Line ("Segment Marker has been read previously.");
end if;
b := buffered_marker;
else
Get_Byte (image.buffer, b);
if b /= 16#FF# then
raise error_in_image_data
with "JPEG: expected marker prefix (16#FF#) here";
end if;
Get_Byte (image.buffer, b);
if full_trace then
Put_Line ("Segment Marker has been just read from stream.");
end if;
end if;
for m in JPEG_Marker loop
if marker_id (m) = b then
head.kind := m;
case m is
when EOI =>
-- No header following this marker (there are perhaps others).
head.length := 0;
when others =>
Big_Endian (image.buffer, head.length);
-- We consider length of contents, without the FFxx marker.
head.length := head.length - 2;
end case;
if some_trace then
Put_Line
("Segment [" & head.kind'Image & "], length:" & head.length'Image);
end if;
return;
end if;
end loop;
raise error_in_image_data
with "JPEG: unknown marker here: 16#FF#, then" & Marker_Image (b);
end Read;
procedure Skip_Segment_Data
(image : in out Image_Descriptor;
head : in Segment_Head)
is
dummy : U8;
begin
if full_trace then
Put_Line ("Skipping segment: " & head.kind'Image);
end if;
for i in 1 .. head.length loop
Get_Byte (image.buffer, dummy);
end loop;
end Skip_Segment_Data;
-- SOF - Start Of Frame (the real header)
procedure Read_SOF (image : in out Image_Descriptor; sh : Segment_Head) is
use Bounded_255;
b, bits_pp_primary, id_base : U8;
w, h : U16;
compo : JPEG_Defs.Component;
begin
case sh.kind is
when SOF_0 =>
image.detailed_format := To_Bounded_String ("JPEG, Baseline DCT (SOF_0)");
when SOF_2 =>
image.detailed_format := To_Bounded_String ("JPEG, Progressive DCT (SOF_2)");
image.progressive := True;
when others =>
raise unsupported_image_subformat with
"JPEG: image type not yet supported: " & sh.kind'Image;
end case;
Get_Byte (image.buffer, bits_pp_primary);
if bits_pp_primary /= 8 then
raise unsupported_image_subformat with
"JPEG: bits per primary color=" & bits_pp_primary'Image & " (not supported)";
end if;
image.bits_per_pixel := 3 * Positive (bits_pp_primary);
Big_Endian (image.buffer, h);
Big_Endian (image.buffer, w);
if w = 0 then
raise error_in_image_data with "JPEG: zero image width";
end if;
if h = 0 then
raise error_in_image_data with "JPEG: zero image height";
end if;
image.width := Positive_32 (w);
image.height := Positive_32 (h);
-- Number of components:
Get_Byte (image.buffer, b);
image.subformat_id := Integer (b);
--
image.JPEG_stuff.max_samples_hor := 0;
image.JPEG_stuff.max_samples_ver := 0;
id_base := 1;
-- For each component: 3 bytes information: ID, sampling factors, quantization table number
for i in 1 .. image.subformat_id loop
-- Component ID (1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q)
Get_Byte (image.buffer, b);
if b = 0 then
-- Workaround for a bug in some encoders, for instance Intel(R) JPEG Library,
-- version [2.0.18.50] as in some Photoshop versions : IDs are numbered 0, 1, 2.
id_base := 0;
if full_trace then
Put_Line ("SOF: Off-by one error in image data: component Id");
end if;
end if;
if b - id_base > Component'Pos (Component'Last) then
raise error_in_image_data with "JPEG: SOF: invalid component ID: " & b'Image;
end if;
compo := JPEG_Defs.Component'Val (b - id_base);
image.JPEG_stuff.compo_set (compo) := True;
declare
stuff : JPEG_Stuff_Type renames image.JPEG_stuff;
info : JPEG_Defs.Info_per_Component_A renames stuff.info (compo);
begin
-- Sampling factors (bit 0-3 vert., 4-7 hor.)
Get_Byte (image.buffer, b);
info.ups.samples_hor := Natural (b / 16);
info.ups.samples_ver := Natural (b mod 16);
info.repeat := info.ups.samples_hor * info.ups.samples_ver;
info.shape_x := info.ups.samples_hor * 8;
info.shape_y := info.ups.samples_ver * 8;
stuff.max_samples_hor :=
Integer'Max (stuff.max_samples_hor, info.ups.samples_hor);
stuff.max_samples_ver :=
Integer'Max (stuff.max_samples_ver, info.ups.samples_ver);
-- Quantization table number
Get_Byte (image.buffer, b);
info.qt_assoc := Natural (b);
end;
end loop;
if some_trace then
Put_Line ("Frame has following components:");
for c in JPEG_Defs.Component loop
Put_Line (c'Image & " -> " & image.JPEG_stuff.compo_set (c)'Image);
end loop;
New_Line;
end if;
for c in Component loop
if image.JPEG_stuff.compo_set (c) then
declare
stuff : JPEG_Stuff_Type renames image.JPEG_stuff;
info : JPEG_Defs.Info_per_Component_A renames stuff.info (c);
begin
info.ups.up_factor_x := stuff.max_samples_hor / info.ups.samples_hor;
info.ups.up_factor_y := stuff.max_samples_ver / info.ups.samples_ver;
info.upsampling_profile := Identify (info.ups);
if some_trace then
Put_Line ("For component " & c'Image);
Put_Line
(" samples . . . . . :" &
info.ups.samples_hor'Image & " horizontal and" &
info.ups.samples_ver'Image & " vertical");
Put_Line
(" upsampling factors:" &
info.ups.up_factor_x'Image & " horizontally and" &
info.ups.up_factor_y'Image & " vertically");
Put_Line
(" upsampling profile: " & info.upsampling_profile'Image);
end if;
end;
end if;
end loop;
if Natural (sh.length) < 6 + 3 * image.subformat_id then
raise error_in_image_data with "JPEG: SOF segment too short";
end if;
if image.JPEG_stuff.compo_set = YCbCr_set then
image.JPEG_stuff.color_space := YCbCr;
elsif image.JPEG_stuff.compo_set = Y_Grey_set then
image.JPEG_stuff.color_space := Y_Grey;
image.greyscale := True;
elsif image.JPEG_stuff.compo_set = CMYK_set then
image.JPEG_stuff.color_space := CMYK;
else
raise unsupported_image_subformat with
"JPEG: only YCbCr, Y_Grey and CMYK color spaces are currently defined";
end if;
image.detailed_format := image.detailed_format & ", " &
image.JPEG_stuff.color_space'Image;
if some_trace then
Put_Line ("Color space: " & image.JPEG_stuff.color_space'Image);
end if;
if image.JPEG_stuff.color_space = CMYK then
raise unsupported_image_subformat with
"JPEG: CMYK color space is currently not properly decoded";
end if;
end Read_SOF;
procedure Read_DHT (image : in out Image_Descriptor; data_length : Natural) is
remaining : Integer_M32 := Integer_M32 (data_length);
-- ^ data remaining in segment
b : U8;
ht_idx : Natural;
kind : AC_DC;
counts : array (1 .. 16) of Integer_M32;
idx : Natural;
current_count, spread, remain_vlc : Integer_M32;
begin
Multi_DHT_Tables :
while remaining > 0 loop
-- ^ Test is at the beginning of the loop because
-- some encoders produce empty DHT segments!
Get_Byte (image.buffer, b);
remaining := remaining - 1;
if b >= 8 then
kind := AC;
else
kind := DC;
end if;
ht_idx := Natural (b and 7);
if some_trace then
Put_Line
(" Huffman Table (HT) #" & ht_idx'Image &
", of kind (AC/DC): " & kind'Image);
end if;
if image.JPEG_stuff.vlc_defs (kind, ht_idx) = null then
image.JPEG_stuff.vlc_defs (kind, ht_idx) := new VLC_Table;
end if;
for i in counts'Range loop
Get_Byte (image.buffer, b);
remaining := remaining - 1;
counts (i) := Integer_M32 (b);
end loop;
remain_vlc := 65_536;
spread := 65_536;
idx := 0;
for code_len in counts'Range loop
spread := spread / 2;
current_count := counts (code_len);
if current_count > 0 then
if remaining < current_count then
raise error_in_image_data
with
"JPEG: DHT data too short [1]: remaining =" & remaining'Image;
end if;
remain_vlc := remain_vlc - current_count * spread;
if remain_vlc < 0 then
raise error_in_image_data
with
"JPEG: DHT data too short [2]: remain_vlc =" &
remain_vlc'Image;
end if;
for i in reverse 1 .. current_count loop
Get_Byte (image.buffer, b);
for j in reverse 1 .. spread loop
image.JPEG_stuff.vlc_defs (kind, ht_idx)(idx) :=
(bits => U8 (code_len), code => b);
idx := idx + 1;
end loop;
end loop;
remaining := remaining - current_count;
end if;
end loop;
while remain_vlc > 0 loop
remain_vlc := remain_vlc - 1;
image.JPEG_stuff.vlc_defs (kind, ht_idx)(idx).bits := 0;
idx := idx + 1;
end loop;
end loop Multi_DHT_Tables;
end Read_DHT;
procedure Read_DQT (image : in out Image_Descriptor; data_length : Natural) is
remaining : Integer := data_length; -- Data remaining in segment
b, q8 : U8; q16 : U16;
qt_idx : Natural;
high_prec : Boolean;
begin
Multi_DQT_Tables :
while remaining > 0 loop
-- ^ Test is at the beginning of the loop because
-- an encoder could produce an empty DQT segment.
Get_Byte (image.buffer, b);
remaining := remaining - 1;
high_prec := b >= 8;
qt_idx := Natural (b and 7);
if some_trace then
Put_Line (" Quantization Table (QT) #" & b'Image);
end if;
for i in Quantization_Table'Range loop
if high_prec then
Big_Endian (image.buffer, q16);
remaining := remaining - 2;
image.JPEG_stuff.qt_list (qt_idx)(i) := Natural (q16);
else
Get_Byte (image.buffer, q8);
remaining := remaining - 1;
image.JPEG_stuff.qt_list (qt_idx)(i) := Natural (q8);
end if;
end loop;
end loop Multi_DQT_Tables;
end Read_DQT;
procedure Read_DRI (image : in out Image_Descriptor) is
-- B.2.4.4 Restart interval definition syntax
-- DRI: Define restart interval marker
ri : U16;
begin
Big_Endian (image.buffer, ri);
if some_trace then
Put_Line (" Restart interval (DRI) set to:" & ri'Image);
end if;
image.JPEG_stuff.restart_interval := Natural (ri);
end Read_DRI;
procedure Read_EXIF (image : in out Image_Descriptor; data_length : Natural) is
b, orientation_value : U8;
x, ifd0_entries : Natural;
Exif_signature : constant String := "Exif" & ASCII.NUL & ASCII.NUL;
signature : String (1 .. 6);
IFD_tag : U16;
endianness : Character;
-- 'M' (Motorola) or 'I' (Intel): EXIF chunks may have different endiannesses,
-- even though the whole JPEG format has a fixed endianness!
begin
if some_trace then
Put_Line ("APP1");
end if;
if data_length < 6 then
-- Skip segment data
for i in 1 .. data_length loop
Get_Byte (image.buffer, b);
end loop;
else
for i in 1 .. 6 loop
Get_Byte (image.buffer, b);
signature (i) := Character'Val (b);
end loop;
if signature /= Exif_signature then
for i in 7 .. data_length loop -- Skip remaining of APP1 data
Get_Byte (image.buffer, b); -- since we don't know how to use it.
end loop;
if some_trace then
Put_Line ("APP1 is not Exif");
end if;
return;
end if;
Get_Byte (image.buffer, b); -- TIFF 6.0 header (1st of 8 bytes)
endianness := Character'Val (b);
if some_trace then
Put_Line ("APP1 is Exif; endianness is " & endianness);
end if;
for i in 8 .. 14 loop -- TIFF 6.0 header (2-8 of 8 bytes)
Get_Byte (image.buffer, b);
end loop;
-- Number of IFD0 entries (2 bytes)
ifd0_entries := 0;
Get_Byte (image.buffer, b);
ifd0_entries := Natural (b);
Get_Byte (image.buffer, b);
if endianness = 'I' then
ifd0_entries := ifd0_entries + 16#100# * Natural (b);
else
ifd0_entries := Natural (b) + 16#100# * ifd0_entries;
end if;
if some_trace then
Put_Line ("EXIF's IFD0 has" & ifd0_entries'Image & " entries.");
end if;
x := 17;
while x <= data_length - 12 loop
Get_Byte (image.buffer, b);
IFD_tag := U16 (b);
Get_Byte (image.buffer, b);
if endianness = 'I' then
IFD_tag := IFD_tag + 16#100# * U16 (b);
else
IFD_tag := U16 (b) + 16#100# * IFD_tag;
end if;
if some_trace then
Put ("IFD tag:"); Ada.Integer_Text_IO.Put (Natural (IFD_tag), Base => 16); New_Line;
end if;
for i in 3 .. 8 loop
Get_Byte (image.buffer, b);
end loop;
if endianness = 'I' then
Get_Byte (image.buffer, orientation_value);
for i in 10 .. 12 loop
Get_Byte (image.buffer, b);
end loop;
else
Get_Byte (image.buffer, b);
Get_Byte (image.buffer, orientation_value);
Get_Byte (image.buffer, b);
Get_Byte (image.buffer, b);
end if;
x := x + 12;
if IFD_tag = 16#112# then
case orientation_value is
when 1 =>
image.display_orientation := Unchanged;
when 8 =>
image.display_orientation := Rotation_90;
when 3 =>
image.display_orientation := Rotation_180;
when 6 =>
image.display_orientation := Rotation_270;
when others =>
image.display_orientation := Unchanged;
end case;
if some_trace then
Put_Line
("IFD tag 0112: Orientation set to: " &
image.display_orientation'Image);
end if;
exit;
end if;
end loop;
-- Skip rest of data
for i in x .. data_length loop
Get_Byte (image.buffer, b);
end loop;
end if;
end Read_EXIF;
---------------------------
-- JPEG image decoding --
---------------------------
type Block_8x8 is array (0 .. 63) of Integer;
-- Reverse the zig-zag. If you follow the sequence 0, 1, 2, 3, ...
-- in the array below you find the original zig-zag sequence.
-- See also:
-- Figure A.6 - Zig-zag sequence of quantized DCT coefficients.
--
undo_zig_zag : constant array (0 .. 63) of Integer :=
(0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63);
-- Ordering within a 8x8 block, in zig-zag
-- See: Figure 5 - Preparation of quantized
-- coefficients for entropy encoding
--
zig_zag : constant Block_8x8 :=
(0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63);
zag_zig : constant array (0 .. 63, 1 .. 2) of Integer_32 :=
((0, 0), (1, 0), (0, 1), (0, 2), (1, 1), (2, 0), (3, 0), (2, 1),
(1, 2), (0, 3), (0, 4), (1, 3), (2, 2), (3, 1), (4, 0), (5, 0),
(4, 1), (3, 2), (2, 3), (1, 4), (0, 5), (0, 6), (1, 5), (2, 4),
(3, 3), (4, 2), (5, 1), (6, 0), (7, 0), (6, 1), (5, 2), (4, 3),
(3, 4), (2, 5), (1, 6), (0, 7), (1, 7), (2, 6), (3, 5), (4, 4),
(5, 3), (6, 2), (7, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6),
(2, 7), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (7, 4), (6, 5),
(5, 6), (4, 7), (5, 7), (6, 6), (7, 5), (7, 6), (6, 7), (7, 7));
procedure Load (image : in out Image_Descriptor) is
--
-- Bit buffer
--
bit_buffer : U32;
bit_buffer_length : Natural;
-- A marker can appear when filling the bit buffer
-- during a scan's decoding. Actually, at the end of
-- the scan's data, it *will* appear (usually:
-- the final EOI, or the next SOS).
memo_marker : U8 := 0;
procedure Initialize_Bit_Buffer is
begin
bit_buffer := 0;
bit_buffer_length := 0;
memo_marker := 0;
end Initialize_Bit_Buffer;
function Show_Bits (bits : Natural) return Natural is
newbyte, possible_marker : U8;
begin
if bits = 0 then
return 0;
end if;
while bit_buffer_length < bits loop
begin
Get_Byte (image.buffer, newbyte);
bit_buffer_length := bit_buffer_length + 8;
bit_buffer := Shift_Left (bit_buffer, 8) + U32 (newbyte);
if newbyte = 16#FF# then
Get_Byte (image.buffer, possible_marker);
if possible_marker = 0 then
-- Escape code for a normal value 16#FF#.
-- F.1.2.3 Byte stuffing:
-- "If a X'00' byte is detected after a X'FF' byte, the
-- decoder must discard it."
if full_trace then
New_Line;
Put_Line
("Bit buffer: byte stuffing: FF, then 00 -> value FF");
end if; else
-- "If the byte is not zero, a marker has been detected,
-- and shall be interpreted to the extent needed to
-- complete the decoding of the scan."
-- It is the case at least for restart markers (RST).
if full_trace then
New_Line;
Put_Line
("Bit buffer: possible marker found: " &
Marker_Image (possible_marker));
end if;
bit_buffer_length := bit_buffer_length + 8;
bit_buffer := Shift_Left (bit_buffer, 8) + U32 (possible_marker);
-- Many possible markers are naturally
-- buffered in the bit buffer at the very end of the
-- scan: EOI, DHT, SOS (next scan), ...
-- We need not to discard those markers!
memo_marker := possible_marker;
--
case possible_marker is
when EOI_code =>
if full_trace then
Put_Line ("Bit buffer: acquired EOI marker");
end if;
when COM_code | DHT_code | DRI_code |
RST_0_code .. RST_7_code | SOS_code =>
null;
when others =>
raise error_in_image_data with
"JPEG: Invalid marker within filling of bit buffer: " &
Marker_Image (possible_marker);
end case;
end if;
end if;
exception
when Ada.IO_Exceptions.End_Error =>
newbyte := 16#FF#;
bit_buffer_length := bit_buffer_length + 8;
bit_buffer := Shift_Left (bit_buffer, 8) + U32 (newbyte);
end;
end loop;
return
Natural
(Shift_Right (bit_buffer, bit_buffer_length - bits)
and
(Shift_Left (1, bits) - 1));
end Show_Bits;
procedure Skip_Bits (bits : Natural) is
pragma Inline (Skip_Bits);
dummy : Integer;
pragma Unreferenced (dummy);
begin
if bit_buffer_length < bits then
dummy := Show_Bits (bits);
end if;
bit_buffer_length := bit_buffer_length - bits;
end Skip_Bits;
function Get_Bits (bits : Natural) return Integer is
pragma Inline (Get_Bits);
res : constant Integer := Show_Bits (bits);
begin
Skip_Bits (bits);
return res;
end Get_Bits;
--
type Info_per_component_B is record
ht_idx_AC : Natural;
ht_idx_DC : Natural;
width, height, stride : Natural;
dc_predictor : Integer := 0;
end record;
info_A : Component_Info_A renames image.JPEG_stuff.info;
info_B : array (Component) of Info_per_component_B;
procedure Get_VLC
(vlc : in VLC_Table;
code : out U8;
value_ret : out Integer)
is
--------------------------------------------------
-- Step 1 happens here: Huffman decompression --
--------------------------------------------------
value : Integer := Show_Bits (16);
bits : Natural := Natural (vlc (value).bits);
begin
if bits = 0 then
raise error_in_image_data with "JPEG: VLC table: bits = 0";
end if;
Skip_Bits (bits);
value := Integer (vlc (value).code);
code := U8 (value);
bits := Natural (U32 (value) and 15);
value_ret := 0;
if bits /= 0 then
value := Get_Bits (bits);
if value < Integer (Shift_Left (U32'(1), bits - 1)) then
value := value + 1 - Integer (Shift_Left (U32'(1), bits));
end if;
value_ret := value;
end if;
end Get_VLC;
function Next_Huffval (vlc : VLC_Table) return Integer is
value : constant Integer := Show_Bits (16);
bits : constant Natural := Natural (vlc (value).bits);
begin
if bits = 0 then
raise error_in_image_data with "JPEG: Huffman value: bits = 0";
end if;
Skip_Bits (bits);
return Integer (vlc (value).code);
end Next_Huffval;
function Bin_Twos_Complement (value, bit_length : Integer) return Integer is
(if value < Integer (Shift_Left (U32'(1), bit_length - 1)) then
value + 1 - Integer (Shift_Left (U32'(1), bit_length))
else
value);
function Clip (x : Integer) return Integer is
(if x < 0 then 0 elsif x > 255 then 255 else x) with Inline;
procedure Decode_8x8_Block (c : Component; block : in out Block_8x8) is
value, coef : Integer;
code : U8;
qt_local : JPEG_Defs.Quantization_Table
renames image.JPEG_stuff.qt_list (info_A (c).qt_assoc);
begin
-------------------------------------------------
-- Step 2 happens here: Inverse quantization --
-------------------------------------------------
-- DC value:
Get_VLC (image.JPEG_stuff.vlc_defs (DC, info_B (c).ht_idx_DC).all, code, value);
-- First value in block (0: top left) uses a predictor.
info_B (c).dc_predictor := info_B (c).dc_predictor + value;
block := (0 => info_B (c).dc_predictor * qt_local (0), others => 0);
coef := 0;
loop
-- AC value:
Get_VLC (image.JPEG_stuff.vlc_defs (AC, info_B (c).ht_idx_AC).all, code, value);
exit when code = 0; -- EOB
if (code and 16#0F#) = 0 and code /= 16#F0# then
raise error_in_image_data with "JPEG: error in VLC AC code for de-quantization";
end if;
coef := coef + Integer (Shift_Right (code, 4)) + 1;
if coef > 63 then
raise error_in_image_data with "JPEG: coefficient for de-quantization is > 63";
end if;
-- Undo the zig-zag scan and apply de-quantization:
block (zig_zag (coef)) := value * qt_local (coef);
exit when coef = 63;
end loop;
end Decode_8x8_Block;
procedure Inverse_DCT_8x8_Block (block : in out Block_8x8) is
--
W1 : constant := 2841;
W2 : constant := 2676;
W3 : constant := 2408;
W5 : constant := 1609;
W6 : constant := 1108;
W7 : constant := 565;
--
procedure Row_IDCT (start : Integer) is
pragma Inline (Row_IDCT);
x0, x1, x2, x3, x4, x5, x6, x7, x8, val : Integer;
begin
x1 := block (start + 4) * 2**11;
x2 := block (start + 6);
x3 := block (start + 2);
x4 := block (start + 1);
x5 := block (start + 7);
x6 := block (start + 5);
x7 := block (start + 3);
if x1 = 0 and x2 = 0 and x3 = 0 and x4 = 0 and x5 = 0 and x6 = 0 and x7 = 0 then
val := block (start + 0) * 8;
block (start + 0 .. start + 7) := (others => val);
else
x0 := (block (start + 0) * 2**11) + 128;
x8 := W7 * (x4 + x5);
x4 := x8 + (W1 - W7) * x4;
x5 := x8 - (W1 + W7) * x5;
x8 := W3 * (x6 + x7);
x6 := x8 - (W3 - W5) * x6;
x7 := x8 - (W3 + W5) * x7;
x8 := x0 + x1;
x0 := x0 - x1;
x1 := W6 * (x3 + x2);
x2 := x1 - (W2 + W6) * x2;
x3 := x1 + (W2 - W6) * x3;
x1 := x4 + x6;
x4 := x4 - x6;
x6 := x5 + x7;
x5 := x5 - x7;
x7 := x8 + x3;
x8 := x8 - x3;
x3 := x0 + x2;
x0 := x0 - x2;
x2 := (181 * (x4 + x5) + 128) / 256;
x4 := (181 * (x4 - x5) + 128) / 256;
block (start + 0) := (x7 + x1) / 256;
block (start + 1) := (x3 + x2) / 256;
block (start + 2) := (x0 + x4) / 256;
block (start + 3) := (x8 + x6) / 256;
block (start + 4) := (x8 - x6) / 256;
block (start + 5) := (x0 - x4) / 256;
block (start + 6) := (x3 - x2) / 256;
block (start + 7) := (x7 - x1) / 256;
end if;
end Row_IDCT;
procedure Col_IDCT (start : Integer) is
pragma Inline (Col_IDCT);
x0, x1, x2, x3, x4, x5, x6, x7, x8, val : Integer;
begin
x1 := block (start + 8 * 4) * 256;
x2 := block (start + 8 * 6);
x3 := block (start + 8 * 2);
x4 := block (start + 8 * 1);
x5 := block (start + 8 * 7);
x6 := block (start + 8 * 5);
x7 := block (start + 8 * 3);
if x1 = 0 and x2 = 0 and x3 = 0 and x4 = 0 and x5 = 0 and x6 = 0 and x7 = 0 then
val := Clip (((block (start) + 32) / 2**6) + 128);
for row in reverse 0 .. 7 loop
block (start + row * 8) := val;
end loop;
else
x0 := (block (start) * 256) + 8192;
x8 := W7 * (x4 + x5) + 4;
x4 := (x8 + (W1 - W7) * x4) / 8;
x5 := (x8 - (W1 + W7) * x5) / 8;
x8 := W3 * (x6 + x7) + 4;
x6 := (x8 - (W3 - W5) * x6) / 8;
x7 := (x8 - (W3 + W5) * x7) / 8;
x8 := x0 + x1;
x0 := x0 - x1;
x1 := W6 * (x3 + x2) + 4;
x2 := (x1 - (W2 + W6) * x2) / 8;
x3 := (x1 + (W2 - W6) * x3) / 8;
x1 := x4 + x6;
x4 := x4 - x6;
x6 := x5 + x7;
x5 := x5 - x7;
x7 := x8 + x3;
x8 := x8 - x3;
x3 := x0 + x2;
x0 := x0 - x2;
x2 := (181 * (x4 + x5) + 128) / 256;
x4 := (181 * (x4 - x5) + 128) / 256;
block (start + 8 * 0) := Clip (((x7 + x1) / 2**14) + 128);
block (start + 8 * 1) := Clip (((x3 + x2) / 2**14) + 128);
block (start + 8 * 2) := Clip (((x0 + x4) / 2**14) + 128);
block (start + 8 * 3) := Clip (((x8 + x6) / 2**14) + 128);
block (start + 8 * 4) := Clip (((x8 - x6) / 2**14) + 128);
block (start + 8 * 5) := Clip (((x0 - x4) / 2**14) + 128);
block (start + 8 * 6) := Clip (((x3 - x2) / 2**14) + 128);
block (start + 8 * 7) := Clip (((x7 - x1) / 2**14) + 128);
end if;
end Col_IDCT;
begin
-----------------------------------------------------
-- Step 3 happens here: Inverse cosine transform --
-----------------------------------------------------
for row in 0 .. 7 loop
Row_IDCT (row * 8);
end loop;
for column in 0 .. 7 loop
Col_IDCT (column);
end loop;
end Inverse_DCT_8x8_Block;
procedure Out_Pixel_8 (br, bg, bb : U8) is
pragma Inline (Out_Pixel_8);
function Times_257 (x : Primary_Color_Range) return Primary_Color_Range
is
(16 * (16 * x) + x) with Inline; -- This is 257 * x, = 16#0101# * x
-- Numbers are 8-bit -> no OA warning at instantiation.
-- Returns x if type Primary_Color_Range is mod 2**8.
full_opaque : constant Primary_Color_Range := Primary_Color_Range'Last;
begin
case Primary_Color_Range'Modulus is
when 256 =>
Put_Pixel
(Primary_Color_Range (br),
Primary_Color_Range (bg),
Primary_Color_Range (bb),
full_opaque);
when 65_536 =>
Put_Pixel
(Times_257 (Primary_Color_Range (br)),
Times_257 (Primary_Color_Range (bg)),
Times_257 (Primary_Color_Range (bb)),
full_opaque);
-- Times_257 makes max intensity FF go to FFFF
when others =>
raise invalid_primary_color_range
with "JPEG: color range not supported";
end case;
end Out_Pixel_8;
ssxmax : constant Natural := image.JPEG_stuff.max_samples_hor;
ssymax : constant Natural := image.JPEG_stuff.max_samples_ver;
sample_shape_max_x : constant Natural := 8 * ssxmax;
sample_shape_max_y : constant Natural := 8 * ssymax;
scan_compo_set : Compo_Set_Type;
type Macro_8x8_Block is array
(Component range <>, -- component
Positive range <>, -- x sample range
Positive range <>) -- y sample range
of Block_8x8;
generic
flat_last_x : Natural; -- Often 8 - 1, sometimes 16 - 1
flat_last_y : Natural; -- Often 8 - 1, sometimes 16 - 1
procedure Upsampling_and_Output_Fixed_Dims
(macro_block : Macro_8x8_Block; x0, y0 : Natural_32);
procedure Upsampling_and_Output_Fixed_Dims
(macro_block : Macro_8x8_Block; x0, y0 : Natural_32)
is
flat :
array (0 .. flat_last_x, 0 .. flat_last_y, Component) of Integer;
generic
samples_x : Positive; -- Invariants of the image
up_factor_x : Positive; -- Invariants of the image
samples_y : Positive; -- Invariants of the image
up_factor_y : Positive; -- Invariants of the image
procedure Perform_Upsampling (c : Component)
with Inline;
procedure Perform_Upsampling (c : Component)
is
blk_idx : Integer;
begin
-- NB: when the four generic parameters are = 1,
-- the macro_block (c, 1, 1) (idx), for idx in 0 .. 63, is
-- just copied to flat (x8, y8, c), for x8, y8 in 0 .. 7,
-- without actual upsampling.
-- !! To do: in this case, bypass this step and make
-- Col_IDCT write directly to flat.
for x in reverse 1 .. samples_x loop
for y in reverse 1 .. samples_y loop
-- We are at the 8x8 block level
blk_idx := 63;
for y8 in reverse 0 .. 7 loop
for x8 in reverse 0 .. 7 loop
declare
val : constant Integer := macro_block (c, x, y)(blk_idx);
big_pixel_x : constant Natural := up_factor_x * (x8 + 8 * (x - 1));
big_pixel_y : constant Natural := up_factor_y * (y8 + 8 * (y - 1));
begin
-- Repeat pixels for component c, sample (x, y),
-- position (x8, y8).
for rx in reverse 0 .. up_factor_x - 1 loop
for ry in reverse 0 .. up_factor_y - 1 loop
flat (rx + big_pixel_x, ry + big_pixel_y, c) := val;
end loop;