-
Notifications
You must be signed in to change notification settings - Fork 3
/
PV_BitmapFormats.pas
9984 lines (7842 loc) · 228 KB
/
PV_BitmapFormats.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 PV_BitmapFormats;
//Lazzy Image Viewer
//github.com/PascalVault
//License: GNU/GPL
interface
uses Classes, Graphics, SysUtils, Dialogs,
PV_Bitmap, PV_Streams, PV_CRC32Stream,
ZStream, FPImage, IntfGraphics,
FPWriteJPEG, FPReadJPEG, FPReadXPM, FPWriteXPM, FPReadPCX, FPReadBMP,
FPReadPNG, FPReadTGA, FPReadTiff, FPWriteGIF, FPReadGIF, FPReadPSD,
FPReadXWD, Math;
type TC64Mem = record
Bitmap: array[0..7999] of Byte;
ScreenRAM: array[0..999] of Byte;
ColorRAM: array[0..999] of Byte;
bgColor: Byte;
end;
implementation
const AtariPal: array[0..255] of TPal = (
(r:$2D; g:$2D; b:$2D),(r:$3B; g:$3B; b:$3B),(r:$49; g:$49; b:$49),(r:$57; g:$57; b:$57),
(r:$65; g:$65; b:$65),(r:$73; g:$73; b:$73),(r:$81; g:$81; b:$81),(r:$8F; g:$8F; b:$8F),
(r:$9D; g:$9D; b:$9D),(r:$AB; g:$AB; b:$AB),(r:$B9; g:$B9; b:$B9),(r:$C7; g:$C7; b:$C7),
(r:$D5; g:$D5; b:$D5),(r:$E3; g:$E3; b:$E3),(r:$F1; g:$F1; b:$F1),(r:$FF; g:$FF; b:$FF),
(r:$5C; g:$23; b:$00),(r:$6A; g:$31; b:$00),(r:$78; g:$3F; b:$00),(r:$86; g:$4D; b:$A),
(r:$94; g:$5B; b:$18),(r:$A2; g:$69; b:$26),(r:$B0; g:$77; b:$34),(r:$BE; g:$85; b:$42),
(r:$CC; g:$93; b:$50),(r:$DA; g:$A1; b:$5E),(r:$E8; g:$AF; b:$6C),(r:$F6; g:$BD; b:$7A),
(r:$FF; g:$CB; b:$88),(r:$FF; g:$D9; b:$96),(r:$FF; g:$E7; b:$A4),(r:$FF; g:$F5; b:$B2),
(r:$69; g:$14; b:$09),(r:$77; g:$22; b:$17),(r:$85; g:$30; b:$25),(r:$93; g:$3E; b:$33),
(r:$A1; g:$4C; b:$41),(r:$AF; g:$5A; b:$4F),(r:$BD; g:$68; b:$5D),(r:$CB; g:$76; b:$6B),
(r:$D9; g:$84; b:$79),(r:$E7; g:$92; b:$87),(r:$F5; g:$A0; b:$95),(r:$FF; g:$AE; b:$A3),
(r:$FF; g:$BC; b:$B1),(r:$FF; g:$CA; b:$BF),(r:$FF; g:$D8; b:$CD),(r:$FF; g:$E6; b:$DB),
(r:$6C; g:$0A; b:$38),(r:$7A; g:$18; b:$46),(r:$88; g:$26; b:$54),(r:$96; g:$34; b:$62),
(r:$A4; g:$42; b:$70),(r:$B2; g:$50; b:$7E),(r:$C0; g:$5E; b:$8C),(r:$CE; g:$6C; b:$9A),
(r:$DC; g:$7A; b:$A8),(r:$EA; g:$88; b:$B6),(r:$F8; g:$96; b:$C4),(r:$FF; g:$A4; b:$D2),
(r:$FF; g:$B2; b:$E0),(r:$FF; g:$C0; b:$EE),(r:$FF; g:$CE; b:$FC),(r:$FF; g:$DC; b:$FF),
(r:$64; g:$05; b:$65),(r:$72; g:$13; b:$73),(r:$80; g:$21; b:$81),(r:$8E; g:$2F; b:$8F),
(r:$9C; g:$3D; b:$9D),(r:$AA; g:$4B; b:$AB),(r:$B8; g:$59; b:$B9),(r:$C6; g:$67; b:$C7),
(r:$D4; g:$75; b:$D5),(r:$E2; g:$83; b:$E3),(r:$F0; g:$91; b:$F1),(r:$FE; g:$9F; b:$FF),
(r:$FF; g:$AD; b:$FF),(r:$FF; g:$BB; b:$FF),(r:$FF; g:$C9; b:$FF),(r:$FF; g:$D7; b:$FF),
(r:$52; g:$07; b:$89),(r:$60; g:$15; b:$97),(r:$6E; g:$23; b:$A5),(r:$7C; g:$31; b:$B3),
(r:$8A; g:$3F; b:$C1),(r:$98; g:$4D; b:$CF),(r:$A6; g:$5B; b:$DD),(r:$B4; g:$69; b:$EB),
(r:$C2; g:$77; b:$F9),(r:$D0; g:$85; b:$FF),(r:$DE; g:$93; b:$FF),(r:$EC; g:$A1; b:$FF),
(r:$FA; g:$AF; b:$FF),(r:$FF; g:$BD; b:$FF),(r:$FF; g:$CB; b:$FF),(r:$FF; g:$D9; b:$FF),
(r:$3A; g:$10; b:$9C),(r:$48; g:$1E; b:$AA),(r:$56; g:$2C; b:$B8),(r:$64; g:$3A; b:$C6),
(r:$72; g:$48; b:$D4),(r:$80; g:$56; b:$E2),(r:$8E; g:$64; b:$F0),(r:$9C; g:$72; b:$FE),
(r:$AA; g:$80; b:$FF),(r:$B8; g:$8E; b:$FF),(r:$C6; g:$9C; b:$FF),(r:$D4; g:$AA; b:$FF),
(r:$E2; g:$B8; b:$FF),(r:$F0; g:$C6; b:$FF),(r:$FE; g:$D4; b:$FF),(r:$FF; g:$E2; b:$FF),
(r:$1F; g:$1E; b:$9C),(r:$2D; g:$2C; b:$AA),(r:$3B; g:$3A; b:$B8),(r:$49; g:$48; b:$C6),
(r:$57; g:$56; b:$D4),(r:$65; g:$64; b:$E2),(r:$73; g:$72; b:$F0),(r:$81; g:$80; b:$FE),
(r:$8F; g:$8E; b:$FF),(r:$9D; g:$9C; b:$FF),(r:$AB; g:$AA; b:$FF),(r:$B9; g:$B8; b:$FF),
(r:$C7; g:$C6; b:$FF),(r:$D5; g:$D4; b:$FF),(r:$E3; g:$E2; b:$FF),(r:$F1; g:$F0; b:$FF),
(r:$07; g:$2E; b:$89),(r:$15; g:$3C; b:$97),(r:$23; g:$4A; b:$A5),(r:$31; g:$58; b:$B3),
(r:$3F; g:$66; b:$C1),(r:$4D; g:$74; b:$CF),(r:$5B; g:$82; b:$DD),(r:$69; g:$90; b:$EB),
(r:$77; g:$9E; b:$F9),(r:$85; g:$AC; b:$FF),(r:$93; g:$BA; b:$FF),(r:$A1; g:$C8; b:$FF),
(r:$AF; g:$D6; b:$FF),(r:$BD; g:$E4; b:$FF),(r:$CB; g:$F2; b:$FF),(r:$D9; g:$FF; b:$FF),
(r:$00; g:$3E; b:$65),(r:$03; g:$4C; b:$73),(r:$11; g:$5A; b:$81),(r:$1F; g:$68; b:$8F),
(r:$2D; g:$76; b:$9D),(r:$3B; g:$84; b:$AB),(r:$49; g:$92; b:$B9),(r:$57; g:$A0; b:$C7),
(r:$65; g:$AE; b:$D5),(r:$73; g:$BC; b:$E3),(r:$81; g:$CA; b:$F1),(r:$8F; g:$D8; b:$FF),
(r:$9D; g:$E6; b:$FF),(r:$AB; g:$F4; b:$FF),(r:$B9; g:$FF; b:$FF),(r:$C7; g:$FF; b:$FF),
(r:$00; g:$4B; b:$38),(r:$00; g:$59; b:$46),(r:$09; g:$67; b:$54),(r:$17; g:$75; b:$62),
(r:$25; g:$83; b:$70),(r:$33; g:$91; b:$7E),(r:$41; g:$9F; b:$8C),(r:$4F; g:$AD; b:$9A),
(r:$5D; g:$BB; b:$A8),(r:$6B; g:$C9; b:$B6),(r:$79; g:$D7; b:$C4),(r:$87; g:$E5; b:$D2),
(r:$95; g:$F3; b:$E0),(r:$A3; g:$FF; b:$EE),(r:$B1; g:$FF; b:$FC),(r:$BF; g:$FF; b:$FF),
(r:$00; g:$52; b:$09),(r:$00; g:$60; b:$17),(r:$0C; g:$6E; b:$25),(r:$1A; g:$7C; b:$33),
(r:$28; g:$8A; b:$41),(r:$36; g:$98; b:$4F),(r:$44; g:$A6; b:$5D),(r:$52; g:$B4; b:$6B),
(r:$60; g:$C2; b:$79),(r:$6E; g:$D0; b:$87),(r:$7C; g:$DE; b:$95),(r:$8A; g:$EC; b:$A3),
(r:$98; g:$FA; b:$B1),(r:$A6; g:$FF; b:$BF),(r:$B4; g:$FF; b:$CD),(r:$C2; g:$FF; b:$DB),
(r:$00; g:$53; b:$00),(r:$0B; g:$61; b:$00),(r:$19; g:$6F; b:$00),(r:$27; g:$7D; b:$0A),
(r:$35; g:$8B; b:$18),(r:$43; g:$99; b:$26),(r:$51; g:$A7; b:$34),(r:$5F; g:$B5; b:$42),
(r:$6D; g:$C3; b:$50),(r:$7B; g:$D1; b:$5E),(r:$89; g:$DF; b:$6C),(r:$97; g:$ED; b:$7A),
(r:$A5; g:$FB; b:$88),(r:$B3; g:$FF; b:$96),(r:$C1; g:$FF; b:$A4),(r:$CF; g:$FF; b:$B2),
(r:$13; g:$4E; b:$00),(r:$21; g:$5C; b:$00),(r:$2F; g:$6A; b:$00),(r:$3D; g:$78; b:$00),
(r:$4B; g:$86; b:$00),(r:$59; g:$94; b:$0B),(r:$67; g:$A2; b:$19),(r:$75; g:$B0; b:$27),
(r:$83; g:$BE; b:$35),(r:$91; g:$CC; b:$43),(r:$9F; g:$DA; b:$51),(r:$AD; g:$E8; b:$5F),
(r:$BB; g:$F6; b:$6D),(r:$C9; g:$FF; b:$7B),(r:$D7; g:$FF; b:$89),(r:$E5; g:$FF; b:$97),
(r:$2D; g:$43; b:$00),(r:$3B; g:$51; b:$00),(r:$49; g:$5F; b:$00),(r:$57; g:$6D; b:$00),
(r:$65; g:$7B; b:$00),(r:$73; g:$89; b:$01),(r:$81; g:$97; b:$0F),(r:$8F; g:$A5; b:$1D),
(r:$9D; g:$B3; b:$2B),(r:$AB; g:$C1; b:$39),(r:$B9; g:$CF; b:$47),(r:$C7; g:$DD; b:$55),
(r:$D5; g:$EB; b:$63),(r:$E3; g:$F9; b:$71),(r:$F1; g:$FF; b:$7F),(r:$FF; g:$FF; b:$8D),
(r:$46; g:$33; b:$00),(r:$54; g:$41; b:$00),(r:$62; g:$4F; b:$00),(r:$70; g:$5D; b:$00),
(r:$7E; g:$6B; b:$00),(r:$8C; g:$79; b:$0B),(r:$9A; g:$87; b:$19),(r:$A8; g:$95; b:$27),
(r:$B6; g:$A3; b:$35),(r:$C4; g:$B1; b:$43),(r:$D2; g:$BF; b:$51),(r:$E0; g:$CD; b:$5F),
(r:$EE; g:$DB; b:$6D),(r:$FC; g:$E9; b:$7B),(r:$FF; g:$F7; b:$89),(r:$FF; g:$FF; b:$97)
);
function yuv2rgb(y, u, v: Integer; out RR,GG,BB: Byte): Cardinal;
var R,G,B: Integer;
begin
u := u - 128;
v := v - 128;
R := y + round(1.40200 * v);
G := y - round(0.71414 * v + 0.34414 * u);
B := y + round(1.77200 * u);
if r < 0 then r := 0;
if g < 0 then g := 0;
if b < 0 then b := 0;
if r > 255 then r := 255;
if g > 255 then g := 255;
if b > 255 then b := 255;
RR := R;
GG := G;
BB := B;
end;
procedure Unrle_Psd(Src: TStream; UnpackedSize: Integer; Str: TStream);
var LengthOfLine: Word;
Saved, Count: Integer;
Temp,Val: Byte;
i: Integer;
begin
Saved := 0;
repeat
Temp := Src.ReadByte;
If Temp >= 128 then begin
Count := 256 - Temp;
Val := Src.ReadByte;
for i:=0 to Count do Str.Write(Val, 1);
Inc(Saved, Count+1);
end
else begin
Count := Temp;
for i:=0 to Count do Str.Write(Src.ReadByte, 1);
Inc(Saved, Count+1);
end;
until Saved >= UnpackedSize;
end;
procedure DecodeMulticolor(Bmp: TPV_Bitmap; Mem: TC64Mem);
const colodore: array[0..15] of Cardinal = (
$000000,$ffffff,$96282e,$5bd6ce,
$9f2dad,$41b936,$2724c4,$eff347,
$9f4815,$5e3500,$da5f66,$474747,
$787878,$91ff84,$6864ff,$aeaeae);
var Width, Height: Integer;
i,j,k: Integer;
x,y: Integer;
pal: array[0..1999, 0..3] of Byte;
R,G,B: Byte;
begin
Bmp.SetSize(320, 200);
//set C64 palette
for i:=0 to 15 do begin
B := colodore[i] and $FF;
G := (colodore[i] shr 8) and $FF;
R := (colodore[i] shr 16) and $FF;
Bmp.AddPal(R,G,B,255);
end;
//screen ram
for i:=0 to 999 do begin
G := Mem.ScreenRAM[i];
pal[i][1] := G shr 4;
pal[i][2] := G and $F;
end;
//color ram
for i:=0 to 999 do begin
G := Mem.ColorRAM[i];
pal[i][3] := G and $F;
pal[i][0] := Mem.bgColor;
end;
i:= 0;
for y:=0 to 24 do
for x:=0 to 39 do
for k:=0 to 7 do begin
G := Mem.Bitmap[i];
Inc(i);
for j:=0 to 3 do begin
B := GetBits(G, 6-2*j, 2);
Bmp.SetPal(x*8 + 2*j, y*8 + k, pal[y*40 + x][B]);
Bmp.SetPal(x*8 + 2*j+1, y*8 + k, pal[y*40 + x][B]);
end;
end;
end;
procedure DecodeHires(Bmp: TPV_Bitmap; Mem: TC64Mem);
const colodore: array[0..15] of Cardinal = (
$000000,$ffffff,$96282e,$5bd6ce,
$9f2dad,$41b936,$2724c4,$eff347,
$9f4815,$5e3500,$da5f66,$474747,
$787878,$91ff84,$6864ff,$aeaeae);
var Width, Height: Integer;
i,j,k: Integer;
x,y: Integer;
pal: array[0..1999, 0..3] of Byte;
R,G,B: Byte;
begin
Bmp.SetSize(320, 200);
//set C64 palette
for i:=0 to 15 do begin
B := colodore[i] and $FF;
G := (colodore[i] shr 8) and $FF;
R := (colodore[i] shr 16) and $FF;
Bmp.AddPal(R,G,B,255);
end;
//screen ram
for i:=0 to 999 do begin
G := Mem.ScreenRAM[i];
pal[i][1] := G shr 4;
pal[i][0] := G and $F;
end;
i := 0;
for y:=0 to 24 do
for x:=0 to 39 do
for k:=0 to 7 do begin
G := Mem.Bitmap[i];
Inc(i);
for j:=0 to 7 do begin
B := GetBits(G, 7-j, 1);
Bmp.SetPal(x*8 + j, y*8 + k, pal[y*40 + x][B]);
end;
end;
end;
procedure UnGzip(Data: TStream; Result: TStream); //gzdecode from PHP
const BuffSize = 4096;
var Z: TDecompressionStream;
Read: Integer;
Buff: array of Byte;
ID: Word;
Method: Byte;
Head: Word;
Date: Cardinal;
Flag, OS: Byte;
Skip: Word;
Tmp: String;
Null: Integer;
begin
ID := Data.ReadWord;
Method := Data.ReadByte;
Head := Data.ReadByte;
Date := Data.ReadDWord;
Flag := Data.ReadByte;
OS := Data.ReadByte;
//FEXTRA
if ((Head shr 2) and 1 = 1) then begin
Skip := Data.ReadByte;
Data.Position := Data.Position + Skip;
end;
//FNAME
if ((Head shr 3) and 1 = 1) then begin
SetLength(Tmp, 256);
Data.Read(Tmp[1], 256);
Null := Pos(chr(0), Tmp);
Data.Position := Data.Position - Length(Tmp) + Null;
end;
//FCOMMENT
if ((Head shr 4) and 1 = 1) then begin
SetLength(Tmp, 256);
Data.Read(Tmp[1], 256);
Null := Pos(chr(0), Tmp);
Data.Position := Data.Position - Length(Tmp) + Null;
end;
//FHCRC
if ((Head shr 1) and 1 = 1) then begin
Data.Position := Data.Position + Skip;
end;
//https://datatracker.ietf.org/doc/html/rfc1952#page-5
Z := TDecompressionStream.Create(Data, True);
SetLength(Buff, BuffSize);
try
repeat
Read := Z.Read(Buff[0], BuffSize);
Result.Write(Buff[0], Read);
until Read<BuffSize-1;
finally
Z.Free;
end;
end;
function LBM_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
var Reader: TPV_Reader;
Reader2: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i,k,j: Integer;
form, ilbm: String;
size: Cardinal;
bmWidth, bmHeight, bmX, bmY, bmPlanesCount, bmMasking, bmCompression,
bmPad1, bmTransColor, bmXAspect, bmYAspect, bmPageWidth, bmPageHeight: Cardinal;
chName: String;
chSize: Integer;
col: Byte;
pal: array[0..255] of TPix;
rr: array of Cardinal;
gg: Byte;
p: TPix;
Mem: TMemoryStream;
begin
Reader := TPV_Reader.Create(Str);
form := Reader.getS(4);
size := Reader.getMU4;
ilbm := Reader.getS(4);
if form <> 'FORM' then begin
Reader.Free;
Exit(False);
end;
if (ilbm <> 'ILBM') and (ilbm <> 'PBM ') then begin
Reader.Free;
Exit(False);
end;
while Reader.Offset < Reader.Size do begin
chName := Reader.getS(4); //LBM Chunk
chSize := Reader.getMU4;
if chName = 'BMHD' then begin
bmWidth := Reader.getMU2;
bmHeight := Reader.getMU2;
bmX := Reader.getMU2;
bmY := Reader.getMU2;
bmPlanesCount := Reader.getU;
bmMasking := Reader.getU;
bmCompression := Reader.getU;
bmPad1 := Reader.getU;
bmTransColor := Reader.getMU2;
bmXAspect := Reader.getU;
bmYAspect := Reader.getU;
bmPageWidth := Reader.getMU2;
bmPageHeight := Reader.getMU2;
Bmp.SetSize(bmWidth, bmHeight);
end
else if chName = 'CMAP' then begin //palette
for i:=0 to ceil(chSize/3)-1 do begin
pal[i].r := Reader.getU;
pal[i].g := Reader.getU;
pal[i].b := Reader.getU;
end;
end
else if chName = 'BODY' then begin //image body
Mem := TMemoryStream.Create;
Str.Position := Reader.Offset;
setLength(rr, bmWidth);
if bmCompression = 1 then begin//RLE
Unrle_LBM(Str, Mem, Str.Size-Str.Position);
Mem.Position := 0;
Reader2 := TPV_Reader.Create(Mem);
Reader.Offset := Str.Size;
end
else begin
Reader2 := TPV_Reader.Create(Str, chSize);
Reader.Offset := Reader.Offset + chSize;
end;
for y:=0 to bmHeight-1 do begin
for i:=0 to bmWidth-1 do rr[i] := 0;
//showmessage(IntToStr(bmPlanesCount));
for k:=0 to bmPlanesCount-1 do //for every bitplane
for x:=0 to ceil(bmWidth/8)-1 do begin
b := Reader2.getU;
for j:=0 to 8-1 do begin
gg := getBits(b, (8-1)-j, 1);
rr[8*x + j] := rr[8*x + j] + (gg shl k);
end
end;
if bmPlanesCount<9 then
for x:=0 to bmWidth-1 do begin
gg := rr[x];
Bmp.SetRGBA(x, y, pal[gg].r, pal[gg].g, pal[gg].b, 255);
end
else
for x:=0 to bmWidth-1 do begin
P.RGBA := rr[x];
Bmp.SetRGBA(x,y, p.B, p.G, p.R, 255);
end;
end;
Mem.Free;
Reader2.free;
// break; //TODO: remove
end //Other block- ignore
else begin
Reader.offset := Reader.offset + chSize;
end
end;
Result := True;
Reader.Free;
end;
function A4MI_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Atari 4MI
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
pal: array[0..3] of Byte;
begin
Reader := TPV_Reader.Create(Str);
if (Str.size <> 244) then begin
Reader.Free;
Exit(False);
end;
Bmp.SetSize(16, 240);
Reader.get(pal, 4);
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do Bmp.Set32(x, y, atariPal[0]);
for y:=0 to Bmp.Height-1 do begin
g := Reader.getU;
for x:=0 to 3 do begin
a := getBits(g, 2*x, 2);
if (a=2) or (a=3) then Bmp.Set32(4*x, y, atariPal[pal[x]] );
if (a=3) or (a=1) then Bmp.Set32(4*x+1, y, atariPal[pal[x]] );
end;
end;
Result := True;
Reader.Free;
end;
function A4PL_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Atari 4PL
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
pal: array[0..3] of Byte;
begin
Reader := TPV_Reader.Create(Str);
if (Str.size <> 964) then begin
Reader.Free;
Exit(False);
end;
//Format sklada sie z 4 paskow pionowych. Kazdy pasek ma 8 pikseli (1bajt)
//szerokosci i pomiedzy nimi sa 2 piksele "prozni"
//Poniewaz wysokosc to 240 pikseli, wiec kazdy pasek zajmuje 240 bajtow
//Pierwsze 4 bajty to paleta- po 1 bajcie dla kazdego paska
//Ta paleta to tak naprawde indeks do palety atariPal
Bmp.SetSize(40, 240);
Reader.get(pal, 4);
for i:=0 to 255 do
Bmp.AddPal(atariPal[i].r, atariPal[i].g, atariPal[i].b, 255);
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do Bmp.SetPal(x, y, 0);
for x:=0 to 3 do
for y:=0 to Bmp.Height-1 do begin
g := Reader.getU;
for i:=0 to 7 do begin
a := getBits(g, 7-i, 1);
if a=1 then Bmp.SetPal(10*x + i, y, pal[x]);
end;
end;
Result := True;
Reader.Free;
end;
function PAM_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Portable Arbitrary Map
const colors: array[0..1] of Cardinal = ($FF000000, $FFFFFFFF);
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
id, line, type1: String;
version, type2: Byte;
maxVal, depth: Integer;
temp: TStringList;
bpp: Integer;
begin
Reader := TPV_Reader.Create(Str);
id := Reader.getLn;
version := strToInt(id[2]);
if (id[1] <> 'P') or (version <> 7) then begin
Reader.Free;
Exit(False);
end;
//read header
temp := TStringList.Create;
for i:=0 to 99 do begin
line := Reader.getLn;
if line = '' then continue;
if line[1] <> '#' then temp.add(line);
if line = 'ENDHDR' then break;
end;
temp.text := StringReplace(temp.text, ' ', '=', [rfReplaceAll]);
width := strToInt(temp.Values['WIDTH']);
height := strToInt(temp.Values['HEIGHT']);
maxVal := strToInt(temp.Values['MAXVAL']); //255
depth := strToInt(temp.Values['DEPTH']); //3
type1 := temp.Values['TUPLTYPE']; //RGB
type1 := UpperCase(type1);
if type1 = 'BLACKANDWHITE_ALPHA' then type2 := 4
else if type1 = 'BLACKANDWHITE' then type2 := 1
else if type1 = 'GRAYSCALE_ALPHA' then type2 := 5
else if type1 = 'GRAYSCALE' then type2 := 2
else if type1 = 'RGB_ALPHA' then type2 := 6
else if type1 = 'RGB' then type2 := 3
else begin
//Unsupported type: ' + type1
Reader.Free;
Exit(False);
end;
Bmp.SetSize(width, height);
for y:=0 to height-1 do
for x:=0 to width-1 do
case (type2) of
1: begin
g := Reader.getU;
Bmp.Set32(x, y, colors[g]);
end;
2: begin
g := Reader.getU;
Bmp.SetRGBA(x, y, g, g, g, 255);
end;
3: begin
r := Reader.getU;
g := Reader.getU;
b := Reader.getU;
Bmp.SetRGBA(x, y, r, g, b, 255);
end;
4: begin
g := Reader.getU;
a := Reader.getU;
Bmp.Set32(x, y, colors[g]); //TODO: use alfa
end;
5: begin
g := Reader.getU;
a := Reader.getU;
Bmp.SetRGBA(x, y, g, g, g, a);
end;
6: begin
r := Reader.getU;
g := Reader.getU;
b := Reader.getU;
a := Reader.getU;
Bmp.SetRGBA(x, y, r, g, b, a);
end;
end;
case (type2) of
1,4: bpp := 1;
2,5: bpp := 8;
3,6: bpp := 24;
end;
Result := True;
Reader.Free;
end;
function FITS_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Flexible Image Transport System
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
temp: TStringList;
p: Integer;
line: String;
begin
Reader := TPV_Reader.Create(Str);
//read head
temp := TStringList.Create;
for i:=0 to 99 do begin
line := Reader.getS(80);
if pos('END', line) > 0 then break;
if pos('HISTORY', line) = 1 then temp.add(line)
else begin
p := pos('=', line);
temp.add(trim(copy(line, 1, p-1)) + '=' + trim(Copy(line, p+1)));
end;
end;
if (temp.Values['SIMPLE'] = '') then begin
Reader.Free;
Exit(False);
end;
width := strToInt(temp.values['NAXIS1']);
height := strToInt(temp.values['NAXIS2']);
Bmp.SetSize(width, height);
//read the body
Str.position := 2880;
for y:=height-1 downto 0 do
for x:=0 to width-1 do begin
g := Reader.getU;
Bmp.SetRGB(x, y, g, g, g);
end;
Result := True;
Reader.Free;
end;
function G10_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Atari G10
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
pal: array[0..8] of TPal;
Bmp2: TPV_Bitmap;
half: Integer;
begin
if Str.size <> 7689 then begin
Exit(False);
end;
Reader := TPV_Reader.Create(Str);
Bmp2 := TPV_Bitmap.Create;
Bmp.SetSize(2*80, 2*48);
Bmp2.SetSize(Bmp.Width, Bmp.Height);
//read pallete- at the end of file
Reader.offset := Reader.size - 9;
for i:=0 to 8 do
pal[i] := atariPal[Reader.getU];
Reader.offset := 0;
for y:=0 to Bmp2.Height-1 do
for x:=0 to (Bmp2.Width div 2)-1 do begin
g := Reader.getU;
for i:=0 to 1 do begin
a := getBits(g, 4-4*i, 4);
Bmp2.SetRGB(2*x + i, y, pal[a].R, pal[a].G, pal[a].B);
end;
end;
//the picture in Bmp2 is in fact 2 pictures- one on the left, one on the right
//we need to merge them
Half := Bmp.Width div 2;
for y:=0 to Bmp.Height-1 do
for x:=0 to Half-1 do begin
Bmp[2*x, y] := Bmp2[x, y];
Bmp[2*x + 1,y] := Bmp2[x+Half, y];
end;
Bmp2.Free;
Result := True;
Reader.Free;
end;
function INP_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Atari INP
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
GG: Byte;
x,y: Integer;
i,k: Integer;
pal: array of Byte;
palLen: Byte;
buff,buff2: array[0..7999] of Byte;
P: TPix;
S: TPal;
begin
Reader := TPV_Reader.Create(Str); //Future: more strict recognition
if (Str.size<16000) or (Str.size > 17000) then begin
Reader.Free;
Exit(False);
end;
Reader.AtLeast := 160*200;
Bmp.SetSize(160, 200);
Reader.offset := 16000;
palLen := Reader.size - Reader.offset;
setLength(pal, palLen);
Reader.get(pal[0], palLen);
for i:=0 to palLen-1 do pal[i] := pal[i] - (pal[i] mod 2);
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do
Bmp.SetRGB(x,y, 0,0,0);
//read 2 images, each is 8000 bytes in size
//then merge them so NewBmp[x,y] := (Bmp1[x,y] + Bmp2[x,y]) div 2;
Reader.offset := 0;
for k:=0 to 1 do begin
for y:=0 to Bmp.Height-1 do
for x:=0 to (Bmp.Width div 4)-1 do begin
gg := Reader.getU;
for i:=0 to 3 do begin
a := getBits(gg, 6-2*i, 2);
P := Bmp[4*x + i, y];
S := atariPal[pal[a]];
R := P.R + (S.R div 2);
G := P.G + (S.G div 2);
B := P.B + (S.B div 2);
Bmp.SetRGB(4*x + i, y, R,G,B);
end;
end;
end;
Result := True;
Reader.Free;
end;
function MIC_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Movie Maker Background
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
pal: array[0..3] of Byte;
size: Cardinal;
begin
Bmp.SetSize(160, 192);
size := Str.size mod 40;
if (size <> 0) and (size <> 3) and (size <> 4) and (size <> 5) then begin
Exit(False);
end;
Reader := TPV_Reader.Create(Str);
case Str.size mod 40 of
0,3: begin
pal[0] := 0;
pal[1] := 4;
pal[2] := 8;
pal[3] := 12;
end;
4 : begin
Reader.offset := Reader.size - 4;
Reader.get(pal, 4);
end;
5 : begin
Reader.offset := Reader.size - 6;
Reader.get(pal, 4);
end;
end;
Reader.offset := 0;
for y:=0 to 191 do
for x:=0 to 39 do begin
b := Reader.getU;
for i:=0 to 3 do begin
g := getBits(b, 6-(2*i), 2);
Bmp.Set32(4*x+i, y, atariPal[pal[g]]);
end;
end;
Result := True;
Reader.Free;
end;
function MIS_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Atari MIS
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i: Integer;
index: Byte;
buff: array[0..255] of Byte;
begin
Reader := TPV_Reader.Create(Str);
if (Str.size <> 241) and (Str.size <> 61) then begin
Reader.Free;
Exit(False);
end;
Bmp.SetSize(2, 240);
index := Reader.getU;
for i:=0 to 255 do Bmp.AddPal(atariPal[i].R, atariPal[i].G, atariPal[i].B, 255);
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do Bmp.SetPal(x,y, 0);
for y:=0 to (Bmp.Height div 4)-1 do begin
g := Reader.getU;
for i:=0 to 3 do begin
a := getBits(g, 6-2*i, 2);
if getBits(a, 1, 1) = 1 then Bmp.SetPal(0, 4*y+i, index);
if getBits(a, 0, 1) = 1 then Bmp.SetPal(1, 4*y+i, index);
end;
end;
Result := True;
Reader.Free;
end;
function MTV_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//MTV / RayShade Image
var Reader: TPV_Reader;
width, height: Integer;
x,y: Integer;
R,G,B,A: Byte;
begin
Reader := TPV_Reader.Create(Str);
width := StrToIntDef(Reader.getLn(' '), 0);
height := StrToIntDef(Reader.getLn(), 0);
Reader.getU;
if (width < 1) or (height < 1) then begin
Reader.Free;
Exit(False);
end;
if (width > 3000) or (height > 3000) then begin
Reader.Free;
Exit(False);
end;
Bmp.SetSize(width, height);
for y:=0 to height-1 do
for x:=0 to width-1 do begin
r := Reader.getU;
g := Reader.getU;
b := Reader.getU;
Bmp.SetRGB(x, y, r, g, b);
end;
Result := True;
Reader.Free;
end;
function OTB_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//Nokia Over The Air
const colors: array[0..1] of Cardinal = ($FF66CC66, $FF000000);
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
i, j: Integer;
buff, temp: String;
id, colorss: Integer;
begin
Reader := TPV_Reader.Create(Str);
buff := Reader.getS;
if (copy(buff, 0, 2) = '00') then begin //must be hex file
temp := StringReplace(buff, ' ', '', [rfReplaceAll]);
buff := '';
for i:=1 to system.length(temp) do
buff := buff + chr(hexInt(copy(temp, i*2-1, 2)));
end;
id := ord(buff[1]);
width := ord(buff[2]);
height := ord(buff[3]);
colorss:= ord(buff[4]);
if (id <> 0) and (colorss <> 1) then begin
Reader.Free;
Exit(False);
end;
if (width>3000) or (height>3000) then begin
Reader.Free;
Exit(False);
end;
Bmp.SetSize(width, height);
j := 5;
for y:=0 to height-1 do
for x:=0 to (width div 8)-1 do begin
b := ord(buff[j]);
inc(j);
for i:=0 to 7 do begin
g := getBits(b, 7-i, 1);
Bmp.Set32(8*x+i, y, colors[g]);
end;
end;
Result := True;
Reader.Free;
end;
function PGX_Read(Bmp: TPV_Bitmap; Str: TStream): Boolean;
//PGX (JPEG 2000)
var Reader: TPV_Reader;
Width, Height: Integer;
R,G,B,A: Byte;
x,y: Integer;
id: String;
p: Integer;
unk, w, h: String;
begin