forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GfxMaps.Mod
2874 lines (2634 loc) · 92.4 KB
/
GfxMaps.Mod
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
MODULE GfxMaps; (** eos **)
(**
Raster maps and image processing
**)
(*
9.12.98 - this should now be the one to be released
25.1.99 - made maps be objects
3.3.99 - handle Display.Depth() > 8 in InitDisplayPalette (tnx to [email protected])
5.3.99 - made Copy deal with overlapping source and destination area
10.3.99 - made Create more robust
15.4.99 - added InitBuf
15.4.99 - bugfix in Transform: can't overlay temporary maps for pixel format with alpha
20.4.99 - optimized blend procedures for alpha sources
*)
IMPORT
SYSTEM, Files, Display, Objects, Texts, Oberon, GfxMatrix;
// TODO: get rid of SYSTEM!
(* Ports to big-endian architectures: search for ">>byte order!" *)
CONST
B* = 0; G* = 1; R* = 2; A* = 3; (** indices of components within pixel **)
TYPE
(** pixels with red, green, blue, and alpha information in range 0..255 **)
Pixel* = ARRAY 4 OF CHAR;
(** color structure with red, green, and blue values in range 0..255 **)
Color* = RECORD
r*, g*, b*: INTEGER;
END;
(** palette structure for indexed map formats **)
Palette* = POINTER TO PaletteDesc;
PaletteDesc* = RECORD
col*: ARRAY 256 OF Color; (** color table **)
used*: INTEGER; (** number of valid entries in color table **)
cube: ARRAY 8*8*8+1 OF CHAR;
index: ARRAY 256 OF CHAR;
END;
VAR
DisplayPal*: Palette; (** display palette **)
(**
Although abstract operations on raster maps are available, efficient algorithms often use highly specialized
pixel formats and need direct access to the memory of the raster map. The internal structure of a map is
therefore laid open to clients and has the following properties:
- a map references a contiguous block in memory holding pixel data
- pixels are organized in rows (usually bottom-up, but can also be top-down)
- rows can be aligned to an arbitrary number of bytes
- the leftmost pixel in a row has the lowest address of all pixels in that row
- every pixel uses the same number of bits; this number must be a multiple or a factor of 8 in range [1..32]
- pixels using less than 8 bits may be ordered in ascending or descending order within a byte, but...
- bit addresses within a byte ascend from left to right
- the components in a pixel are managed by a format record, with procedures for storing and loading pixels
- optimized pixel transfer routines exist for predefined formats exported as global variables
**)
CONST
Red* = 0; Green* = 1; Blue* = 2; Alpha* = 3; Grey* = 4; Index* = 5; (** component names **)
a1* = 1; a8* = 2; i8* = 3; bgr555* = 4; bgr565* = 5; bgr24* = 6; bgra32* = 7; (** format codes **)
TYPE
Format0* = RECORD
code*: INTEGER; (** format code for quick format checks **)
bpp*: SHORTINT; (** number of bits per pixel **)
align*: SHORTINT; (** bytes per row must be multiple of this **)
components*: SET; (** set of components that are stored in a pixel **)
col*: Color; (** color used when transfering from pure alpha formats to colored ones **)
pal*: Palette; (** palette for indexed formats **)
END;
PackProc* = PROCEDURE (VAR fmt: Format0; adr, bit: LONGINT; VAR pix: Pixel);
(** map format **)
Format* = RECORD (Format0)
pack*, unpack*: PackProc; (** procedures storing and loading pixel at given address **)
END;
(** raster map **)
Map* = POINTER TO MapDesc;
MapDesc* = RECORD (Objects.ObjDesc)
width*, height*: INTEGER; (** map dimensions **)
fmt*: Format; (** pixel format **)
bpr*: LONGINT; (** number of bytes per row (may be negative) **)
adr*: LONGINT; (** offset into mem of lower left pixel **)
mem*: POINTER TO ARRAY OF CHAR; (** block where pixels are stored; mem#NIL implies adr=ADR(mem[0]) **)
END;
VAR
A1*, A8*, I8*, BGR555*, BGR565*, BGR24*, BGRA32*, PixelFormat*: Format; (** predefined formats **)
(**
In the context of image processing and alpha channels, an operation may sometimes have to blend
source and destination pixels to compute the final value for destination pixels. Most operations therefore
expect a blend operator that specifies how to combine source with destination pixels. Blend operators
exported in global variables get special treatment using optimized code.
**)
TYPE
BlendOp0 = RECORD
pix: Pixel; (* constant area for special-case moving *)
src, dst: Format; (* source and destination format if needed for transfer *)
used: ARRAY 8 OF SET; (* colors already mapped in I8 => I8 transformation *)
map: ARRAY 256 OF CHAR; (* color mapping for I8 => I8 transformation *)
END;
BlendProc* = PROCEDURE (VAR bl: BlendOp0; src, dst: Pixel; VAR res: Pixel);
MoveProc* = PROCEDURE (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
(** blend operator **)
BlendOp* = RECORD (BlendOp0)
blend*: BlendProc; (** procedure blending source with destination pixel **)
move*: MoveProc; (** procedure transfering pixels from source to destination **)
END;
VAR
SrcCopy*: BlendOp; (** direct copy of source to destination (cf. Display.replace) **)
SrcAlpha*: BlendOp; (** dest = src.alpha * src + (1-src.alpha) * dest; (cf. Display.paint) **)
SrcCopyBlend, SrcAlphaBlend: BlendProc; (* for comparisons *)
(**
Map transform operations are decomposed into a series of one-dimensional shift and scale transforms. These
are delegated to a filter object provided by the caller. The caller controls visual quality and execution time
by selecting a filter which complies with its demands.
**)
TYPE
Filter0* = RECORD (BlendOp)
END;
ShiftProc* = PROCEDURE (VAR filter: Filter0; src, dst: Map; sadr, sbit, dadr, dbit, len: LONGINT; t: REAL);
ScaleProc* = PROCEDURE (VAR filter: Filter0; src, dst: Map; sadr, sbit, dadr, dbit, len: LONGINT; xy, dxy: REAL);
(** transformation filter **)
Filter* = RECORD (Filter0)
hshift*, vshift*: ShiftProc; (** procedures for shifting rows and columns **)
hscale*, vscale*: ScaleProc; (** procedures for scaling rows and columns **)
END;
VAR
NoFilter*: Filter; (** uses nearest-neighbor resampling (box filter); bad aliasing when downscaling **)
LinearFilter*: Filter; (** uses linear interpolation (triangle filter); blurry when upscaling **)
(**
The mechanism for loading and storing image maps relies on a section 'ImageFormats' in the registry
(Oberon.Text). It consists of a list of file types associated with command procedures. When one of these
commands is called, it should initialize the global 'LoadProc' and 'StoreProc' variables. These, when called,
should read an image from or write an image to the file and set 'done' to TRUE if successful. If no
procedure is associated with the filename extension, all of them are tried for loading, therefore 'LoadProc'
procedures must not set 'done' to TRUE in case they don't recognize the file.
**)
VAR
LoadProc*: PROCEDURE (map: Map; VAR fname: ARRAY OF CHAR; VAR done: BOOLEAN);
StoreProc*: PROCEDURE (map: Map; VAR fname: ARRAY OF CHAR; VAR done: BOOLEAN);
VAR
Bit: ARRAY 100H, 8 OF BOOLEAN; (* Bit[b, i] means bit i (from left) in byte b is set *)
Set, Clear: ARRAY 100H, 8 OF CHAR; (* Set/Clear[b, i] is byte b with bit i (from left) set/cleared *)
Clamp: ARRAY 500H OF CHAR; (* Clamp[200H+i] clamps i to 0..0FFX *)
PreCache, Cache: Map; (* caches for image transformations *)
(**--- Color/Pixel conversions ---**)
PROCEDURE ColorToPixel* (col: Color; VAR pix: Pixel);
BEGIN
pix[B] := CHR(col.b); pix[G] := CHR(col.g); pix[R] := CHR(col.r); pix[A] := 0FFX
END ColorToPixel;
PROCEDURE PixelToColor* (pix: Pixel; VAR col: Color);
BEGIN
col.r := ORD(pix[R]); col.g := ORD(pix[G]); col.b := ORD(pix[B])
END PixelToColor;
(**--- Image Maps ---**)
PROCEDURE^ Handle* (obj: Objects.Object; VAR msg: Objects.ObjMsg);
(** initialize custom format **)
PROCEDURE InitFormat* (VAR fmt: Format; bpp, align: SHORTINT; comps: SET; pal: Palette; pack, unpack: PackProc);
BEGIN
fmt.code := 0; fmt.bpp := bpp; fmt.align := align; fmt.components := comps; fmt.pal := pal;
fmt.pack := pack; fmt.unpack := unpack;
fmt.col.r := 0; fmt.col.g := 0; fmt.col.b := 0
END InitFormat;
(** initialize custom map **)
PROCEDURE Init* (map: Map; width, height: INTEGER; VAR fmt: Format;
bpr: LONGINT; adr: LONGINT);
BEGIN
ASSERT((width > 0) & (height > 0), 100);
map.handle := Handle; map.width := width; map.height := height; map.fmt := fmt;
map.bpr := bpr; map.adr := adr
END Init;
(** initialize custom map on byte buffer **)
PROCEDURE InitBuf* (map: Map; width, height: INTEGER; VAR fmt: Format; bpr: LONGINT; VAR buf: ARRAY OF CHAR);
BEGIN
ASSERT(height * ABS(bpr) <= LEN(buf), 100);
IF bpr >= 0 THEN Init(map, width, height, fmt, bpr, 0)
ELSE Init(map, width, height, fmt, bpr, LEN(buf) - bpr)
END
END InitBuf;
(** create image map in requested format (allocating or reusing necessary memory) **)
PROCEDURE Create* (map: Map; width, height: INTEGER; VAR fmt: Format);
VAR size, a0, a1: LONGINT;
BEGIN
ASSERT((width > 0) & (height > 0), 100);
map.handle := Handle;
map.width := width; map.height := height;
map.fmt := fmt;
map.bpr := (width * fmt.bpp + 7) DIV 8;
IF fmt.align > 1 THEN
map.bpr := (map.bpr + fmt.align - 1) DIV fmt.align * fmt.align
END;
size := height * map.bpr; INC(size, (-size) MOD 4);
IF (map.mem = NIL) OR (size < LEN(map.mem^) DIV 2) OR (LEN(map.mem^) < size) THEN
NEW(map.mem, size)
ELSE
a0 := 0; // index into map.mem
a1 := a0 + size;
WHILE a0 # a1 DO
map.mem[a0] := 0X; INC(a0)
END
END;
map.adr := 0
END Create;
PROCEDURE GetExtension (VAR name, ext: ARRAY OF CHAR);
VAR i, j: LONGINT; ch: CHAR;
BEGIN
i := 0; j := 0;
WHILE name[i] # 0X DO
IF name[i] = "." THEN j := i+1 END;
INC(i)
END;
i := 0;
REPEAT
ch := name[j]; ext[i] := ch; INC(i); INC(j)
UNTIL ch = 0X
END GetExtension;
PROCEDURE Install (VAR s: Texts.Scanner; VAR ext: ARRAY OF CHAR);
VAR str: ARRAY 64 OF CHAR; res: INTEGER;
BEGIN
ASSERT(s.class IN {Texts.Name, Texts.String});
str := s.s; Texts.Scan(s);
IF (s.class = Texts.Char) & (s.c = "=") THEN (* str contains extension *)
IF (ext # "") & (ext # str) THEN
str := ""; Texts.Scan(s) (* don't try if extension doesn't match *)
ELSE
str := "";
Texts.Scan(s);
IF s.class = Texts.Name THEN
str := s.s; Texts.Scan(s)
END
END
END;
IF str # "" THEN
Oberon.Call(str, Oberon.Par, FALSE, res)
END
END Install;
PROCEDURE TryLoad (map: Map; VAR name: ARRAY OF CHAR; ext: ARRAY OF CHAR; VAR done: BOOLEAN);
VAR s: Texts.Scanner;
BEGIN
done := FALSE;
Oberon.OpenScanner(s, "ImageFormats");
WHILE ~done & (s.class IN {Texts.Name, Texts.String}) DO
LoadProc := NIL;
Install(s, ext);
IF LoadProc # NIL THEN
LoadProc(map, name, done)
END
END
END TryLoad;
(** initialize image map from file (see comment for LoadProc and StoreProc) **)
PROCEDURE Load* (map: Map; name: ARRAY OF CHAR; VAR done: BOOLEAN);
VAR file: Files.File; ext: ARRAY 64 OF CHAR;
BEGIN
done := FALSE; map.handle := Handle; map.width := 0; map.height := 0;
file := Files.Old(name);
IF file # NIL THEN
GetExtension(name, ext);
TryLoad(map, name, ext, done);
IF ~done & (ext # "") THEN
TryLoad(map, name, "", done) (* try all converters *)
END
END
END Load;
(** store image map in file (see comment for LoadProc and StoreProc) **)
PROCEDURE Store* (map: Map; name: ARRAY OF CHAR; VAR done: BOOLEAN);
VAR ext: ARRAY 64 OF CHAR; s: Texts.Scanner;
BEGIN
done := FALSE;
GetExtension(name, ext);
IF ext # "" THEN
Oberon.OpenScanner(s, "ImageFormats");
WHILE ~done & (s.class IN {Texts.Name, Texts.String}) DO
StoreProc := NIL;
Install(s, ext);
IF StoreProc # NIL THEN
StoreProc(map, name, done)
END
END
END
END Store;
(** predefined blend procedures **)
PROCEDURE SrcCopyProc* (VAR bl: BlendOp0; src, dst: Pixel; VAR res: Pixel);
BEGIN
res := src
END SrcCopyProc;
PROCEDURE SrcAlphaProc* (VAR bl: BlendOp0; src, dst: Pixel; VAR res: Pixel);
VAR as, ad: LONGINT;
BEGIN
IF src[A] = 0FFX THEN
res := src
ELSIF src[A] # 0X THEN
as := ORD(src[A]); ad := 0FFH - as;
res[R] := CHR((ORD(src[R]) * as + ORD(dst[R]) * ad) DIV 255);
res[G] := CHR((ORD(src[G]) * as + ORD(dst[G]) * ad) DIV 255);
res[B] := CHR((ORD(src[B]) * as + ORD(dst[B]) * ad) DIV 255);
res[A] := CHR(as + ORD(dst[A]) * ad DIV 255)
END
END SrcAlphaProc;
(*--- Predefined Pack/Unpack Procedures ---*)
PROCEDURE^ BestIndex* (pal: Palette; red, green, blue: INTEGER): INTEGER;
(*--- A1 ---*)
PROCEDURE PackA1 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR b: CHAR;
BEGIN
b := adr[off];
IF pix[A] >= 80X THEN adr[off] := Set[ORD(b), 7-bit]
ELSE adr[0] := Clear[ORD(b), 7-bit]
END
END PackA1;
PROCEDURE UnpackA1 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off , bit: LONGINT; VAR pix: Pixel);
VAR b: CHAR;
BEGIN
b := adr[off];
pix[R] := CHR(fmt.col.r); pix[G] := CHR(fmt.col.g); pix[B] := CHR(fmt.col.b);
IF Bit[ORD(b), 7-bit] THEN pix[A] := 0FFX
ELSE pix[A] := 0X
END
END UnpackA1;
(*--- A8 ---*)
PROCEDURE PackA8 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
BEGIN
adr[off] := pix[A]
END PackA8;
PROCEDURE UnpackA8 (VAR fmt: Format0; IN adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
BEGIN
pix[R] := CHR(fmt.col.r); pix[G] := CHR(fmt.col.g); pix[B] := CHR(fmt.col.b);
pix[A] := adr[off]
END UnpackA8;
(*--- I8 ---*)
PROCEDURE PackI8 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
BEGIN
adr[off] := CHR(BestIndex(fmt.pal, ORD(pix[R]), ORD(pix[G]), ORD(pix[B])))
END PackI8;
PROCEDURE UnpackI8 (VAR fmt: Format0; IN adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR idx: CHAR;
BEGIN
idx := adr[off];
ColorToPixel(fmt.pal.col[ORD(idx)], pix)
END UnpackI8;
(*--- BGR555 ---*)
PROCEDURE PackBGR555 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR int: INTEGER; tmp: ARRAY 2 OF CHAR;
BEGIN (* >>byte order! *)
int := SHORT(ASH(ORD(pix[B]), -3) + ASH(ASH(ORD(pix[G]), -3), 5) + ASH(ASH(ORD(pix[R]), -3), 10));
BYTES(tmp,int); adr[off] := tmp[0]; adr[off+1] := tmp[1]
END PackBGR555;
PROCEDURE UnpackBGR555 (VAR fmt: Format0; IN adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR int: INTEGER; tmp: ARRAY 2 OF CHAR;
BEGIN (* >>byte order! *)
tmp[0] := adr[off]; tmp[1] := adr[off+1];
NUMBER(int,tmp);
pix[B] := CHR(ASH(int MOD 20H, 3));
pix[G] := CHR(ASH(ASH(int, -5) MOD 20H, 3));
pix[R] := CHR(ASH(ASH(int, -10) MOD 20H, 3));
pix[A] := 0FFX
END UnpackBGR555;
(*--- BGR565 ---*)
PROCEDURE PackBGR565 (VAR fmt: Format0; VAR adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR int: INTEGER; tmp: ARRAY 2 OF CHAR;
BEGIN (* >>byte order! *)
int := SHORT(ASH(ORD(pix[B]), -3) + ASH(ASH(ORD(pix[G]), -2), 5) + ASH(ASH(ORD(pix[R]), -3), 11));
BYTES(tmp,int); adr[off] := tmp[0]; adr[off+1] := tmp[1]
END PackBGR565;
PROCEDURE UnpackBGR565 (VAR fmt: Format0; IN adr: ARRAY OF CHAR; off, bit: LONGINT; VAR pix: Pixel);
VAR int: INTEGER; tmp: ARRAY 2 OF CHAR;
BEGIN (* >>byte order! *)
tmp[0] := adr[off]; tmp[1] := adr[off+1];
NUMBER(int,tmp);
pix[B] := CHR(ASH(int MOD 20H, 3));
pix[G] := CHR(ASH(ASH(int, -5) MOD 40H, 2));
pix[R] := CHR(ASH(ASH(int, -11) MOD 20H, 3));
pix[A] := 0FFX
END UnpackBGR565;
(*--- BGR24 ---*)
PROCEDURE PackBGR24 (VAR fmt: Format0; adr, bit: LONGINT; VAR pix: Pixel);
BEGIN
SYSTEM.PUT(adr, pix[B]); SYSTEM.PUT(adr+1, pix[G]); SYSTEM.PUT(adr+2, pix[R])
END PackBGR24;
PROCEDURE UnpackBGR24 (VAR fmt: Format0; adr, bit: LONGINT; VAR pix: Pixel);
BEGIN
SYSTEM.GET(adr, pix[B]); SYSTEM.GET(adr+1, pix[G]); SYSTEM.GET(adr+2, pix[R]); pix[A] := 0FFX
END UnpackBGR24;
(*--- BGRA32 ---*)
PROCEDURE PackBGRA32 (VAR fmt: Format0; adr, bit: LONGINT; VAR pix: Pixel);
BEGIN
SYSTEM.MOVE(SYSTEM.ADR(pix[0]), adr, 4)
END PackBGRA32;
PROCEDURE UnpackBGRA32 (VAR fmt: Format0; adr, bit: LONGINT; VAR pix: Pixel);
BEGIN
SYSTEM.MOVE(adr, SYSTEM.ADR(pix[0]), 4)
END UnpackBGRA32;
(*--- Optimized SrcAlpha Procedures ---*)
PROCEDURE BlendA1A1 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
SYSTEM.GET(sadr, in); SYSTEM.GET(dadr, out);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
out := Set[ORD(out), 7-dbit]
END;
INC(sbit); INC(dbit); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END;
IF dbit = 8 THEN
SYSTEM.PUT(dadr, out);
INC(dadr); dbit := 0;
SYSTEM.GET(dadr, out)
END
END;
SYSTEM.PUT(dadr, out)
END BlendA1A1;
PROCEDURE BlendA1Const8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
SYSTEM.PUT(dadr, bl.pix[0])
END;
INC(sbit); INC(dadr); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END BlendA1Const8;
PROCEDURE BlendA1Const16 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
SYSTEM.MOVE(SYSTEM.ADR(bl.pix[0]), dadr, 2)
END;
INC(sbit); INC(dadr, 2); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END BlendA1Const16;
PROCEDURE BlendA1Const24 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
SYSTEM.MOVE(SYSTEM.ADR(bl.pix[0]), dadr, 3)
END;
INC(sbit); INC(dadr, 3); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END BlendA1Const24;
PROCEDURE BlendA1Const32 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
SYSTEM.MOVE(SYSTEM.ADR(bl.pix[0]), dadr, 4)
END;
INC(sbit); INC(dadr, 4); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END BlendA1Const32;
PROCEDURE BlendA1Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
bl.dst.pack(bl.dst, dadr, dbit, bl.pix)
END;
INC(sbit); dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8; DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END BlendA1Any;
PROCEDURE BlendA8A1 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
SYSTEM.GET(dadr, out);
WHILE len > 0 DO
SYSTEM.GET(sadr, in);
IF in >= 80X THEN
out := Set[ORD(out), 7-dbit]
END;
INC(sadr); INC(dbit); DEC(len);
IF dbit = 8 THEN
SYSTEM.PUT(dadr, out);
INC(dadr); dbit := 0;
SYSTEM.GET(dadr, out)
END
END;
SYSTEM.PUT(dadr, out)
END BlendA8A1;
PROCEDURE BlendA8A8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, in);
IF in = 0FFX THEN
SYSTEM.PUT(dadr, 0FFX)
ELSIF in # 0X THEN
SYSTEM.GET(dadr, out);
SYSTEM.PUT(dadr, CHR(ORD(in) + ORD(out) * (0FFH - ORD(in)) DIV 0FFH))
END;
INC(sadr); INC(dadr); DEC(len)
END
END BlendA8A8;
PROCEDURE BlendA8Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR dpix: Pixel;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, bl.pix[A]);
IF bl.pix[A] # 0X THEN
bl.dst.unpack(bl.dst, dadr, dbit, dpix);
SrcAlphaProc(bl, bl.pix, dpix, dpix);
bl.dst.pack(bl.dst, dadr, dbit, dpix)
END;
INC(sadr); DEC(len);
dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8
END
END BlendA8Any;
PROCEDURE BlendBGRA32A1 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
SYSTEM.GET(dadr, out);
WHILE len > 0 DO
SYSTEM.GET(sadr + A, in);
IF in >= 80X THEN
out := Set[ORD(out), 7-dbit]
END;
INC(sadr, 4); INC(dbit); DEC(len);
IF dbit = 8 THEN
SYSTEM.PUT(dadr, out);
INC(dadr); dbit := 0;
SYSTEM.GET(dadr, out)
END
END;
SYSTEM.PUT(dadr, out)
END BlendBGRA32A1;
PROCEDURE BlendBGRA32A8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr + A, in);
IF in = 0FFX THEN
SYSTEM.PUT(dadr, 0FFX)
ELSIF in # 0X THEN
SYSTEM.GET(dadr, out);
SYSTEM.PUT(dadr, CHR(ORD(in) + ORD(out) * (0FFH - ORD(in)) DIV 0FFH))
END;
INC(sadr, 4); INC(dadr); DEC(len)
END
END BlendBGRA32A8;
PROCEDURE BlendBGRA32Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR spix, dpix: Pixel;
BEGIN
WHILE len > 0 DO
SYSTEM.MOVE(sadr, SYSTEM.ADR(spix[0]), 4);
IF spix[A] # 0X THEN
bl.dst.unpack(bl.dst, dadr, dbit, dpix);
SrcAlphaProc(bl, spix, dpix, dpix);
bl.dst.pack(bl.dst, dadr, dbit, dpix)
END;
INC(sadr, 4); DEC(len);
dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8
END
END BlendBGRA32Any;
PROCEDURE BlendAnyAny (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR spix, dpix: Pixel;
BEGIN
WITH bl: BlendOp DO
WHILE len > 0 DO
bl.src.unpack(bl.src, sadr, sbit, spix);
bl.dst.unpack(bl.dst, dadr, dbit, dpix);
bl.blend(bl, spix, dpix, dpix);
bl.dst.pack(bl.dst, dadr, dbit, dpix);
sbit := sbit + bl.src.bpp; INC(sadr, sbit DIV 8); sbit := sbit MOD 8;
dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8;
DEC(len)
END
END
END BlendAnyAny;
(*--- Optimized SrcCopy Procedures ---*)
PROCEDURE MoveSetBit (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR out: CHAR;
BEGIN
IF (dbit > 0) OR (len < 8) THEN
SYSTEM.GET(dadr, out);
WHILE (dbit < 8) & (len > 0) DO
out := Set[ORD(out), 7-dbit];
INC(dbit); DEC(len)
END;
SYSTEM.PUT(dadr, out)
END;
WHILE len >= 8 DO
SYSTEM.PUT(dadr, 0FFX);
INC(dadr); DEC(len, 8)
END;
IF len > 0 THEN
SYSTEM.GET(dadr, out); dbit := 0;
REPEAT
out := Set[ORD(out), 7-dbit];
INC(dbit); DEC(len)
UNTIL len = 0;
SYSTEM.PUT(dadr, out)
END
END MoveSetBit;
PROCEDURE Move1 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in, out: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE (sbit = 0) & (dbit = 0) & (len >= 8) DO
SYSTEM.PUT(dadr, in);
INC(sadr); INC(dadr); DEC(len, 8);
SYSTEM.GET(sadr, in)
END;
IF (dbit > 0) OR (len < 8) THEN
SYSTEM.GET(dadr, out)
END;
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
out := Set[ORD(out), 7-dbit]
ELSE
out := Clear[ORD(out), 7-dbit]
END;
INC(sbit); INC(dbit); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END;
IF dbit = 8 THEN
SYSTEM.PUT(dadr, out);
INC(dadr); dbit := 0;
IF len < 8 THEN
SYSTEM.GET(dadr, out)
END
END
END;
IF dbit > 0 THEN
SYSTEM.PUT(dadr, out)
END
END Move1;
PROCEDURE MoveA1A8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR in: CHAR;
BEGIN
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
SYSTEM.PUT(dadr, 0FFX)
ELSE
SYSTEM.PUT(dadr, 0X)
END;
INC(sbit); INC(dadr); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END MoveA1A8;
PROCEDURE MoveA1BGRA32 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR pix: Pixel; in: CHAR;
BEGIN
pix := bl.pix;
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
pix[A] := 0FFX
ELSE
pix[A] := 0X
END;
SYSTEM.MOVE(SYSTEM.ADR(pix), dadr, 4);
INC(sbit); INC(dadr, 4); DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END MoveA1BGRA32;
PROCEDURE MoveA1Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR pix: Pixel; in: CHAR;
BEGIN
pix := bl.pix;
SYSTEM.GET(sadr, in);
WHILE len > 0 DO
IF Bit[ORD(in), 7-sbit] THEN
pix[A] := 0FFX
ELSE
pix[A] := 0X
END;
bl.dst.pack(bl.dst, dadr, dbit, pix);
INC(sbit); dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8; DEC(len);
IF sbit = 8 THEN
INC(sadr); sbit := 0;
SYSTEM.GET(sadr, in)
END
END
END MoveA1Any;
PROCEDURE Move8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR byte: CHAR;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, byte); SYSTEM.PUT(dadr, byte);
INC(sadr); INC(dadr); DEC(len)
END
END Move8;
PROCEDURE MoveConst8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
BEGIN
WHILE len > 0 DO
SYSTEM.PUT(dadr, bl.pix[0]);
INC(dadr); DEC(len)
END
END MoveConst8;
PROCEDURE MoveA8A1 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR out, in: CHAR;
BEGIN
IF (dbit > 0) OR (len < 8) THEN
SYSTEM.GET(dadr, out)
END;
WHILE len > 0 DO
SYSTEM.GET(sadr, in);
IF in >= 80X THEN
out := Set[ORD(out), 7-dbit]
ELSE
out := Clear[ORD(out), 7-dbit]
END;
INC(sadr); INC(dbit); DEC(len);
IF dbit = 8 THEN
SYSTEM.PUT(dadr, out);
INC(dadr); dbit := 0;
IF len < 8 THEN
SYSTEM.GET(dadr, out)
END
END
END;
IF dbit > 0 THEN
SYSTEM.PUT(dadr, out)
END
END MoveA8A1;
PROCEDURE MoveA8BGRA32 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR pix: Pixel;
BEGIN
pix := bl.pix;
WHILE len > 0 DO
SYSTEM.GET(sadr, pix[A]);
SYSTEM.MOVE(SYSTEM.ADR(pix), dadr, 4);
INC(sadr); INC(dadr, 4); DEC(len)
END
END MoveA8BGRA32;
PROCEDURE MoveA8Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR pix: Pixel;
BEGIN
pix := bl.pix;
WHILE len > 0 DO
SYSTEM.GET(sadr, pix[A]);
bl.dst.pack(bl.dst, dadr, dbit, pix);
INC(sadr); dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8; DEC(len)
END
END MoveA8Any;
PROCEDURE MoveI8I8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR byte: CHAR; idx: LONGINT; col: Color;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, byte); idx := ORD(byte);
IF ~(idx MOD 32 IN bl.used[idx DIV 32]) THEN
INCL(bl.used[idx DIV 32], idx MOD 32);
col := bl.src.pal.col[idx];
bl.map[idx] := CHR(BestIndex(bl.dst.pal, col.r, col.g, col.b))
END;
SYSTEM.PUT(dadr, bl.map[idx]);
INC(sadr); INC(dadr); DEC(len)
END
END MoveI8I8;
PROCEDURE MoveI8BGR555 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR b: CHAR; col: Color;
BEGIN (* >>byte order! *)
WHILE len > 0 DO
SYSTEM.GET(sadr, b);
col := bl.src.pal.col[ORD(b)];
SYSTEM.PUT(dadr, SHORT(ASH(col.b, -3) + ASH(ASH(col.g, -3), 5) + ASH(ASH(col.r, -3), 10)));
INC(sadr); INC(dadr, 2); DEC(len)
END
END MoveI8BGR555;
PROCEDURE MoveI8BGR565 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR b: CHAR; col: Color;
BEGIN (* >>byte order! *)
WHILE len > 0 DO
SYSTEM.GET(sadr, b);
col := bl.src.pal.col[ORD(b)];
SYSTEM.PUT(dadr, SHORT(ASH(col.b, -3) + ASH(ASH(col.g, -2), 5) + ASH(ASH(col.r, -3), 11)));
INC(sadr); INC(dadr, 2); DEC(len)
END
END MoveI8BGR565;
PROCEDURE MoveI8BGR24 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR byte: CHAR; pix: Pixel;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, byte);
ColorToPixel(bl.src.pal.col[ORD(byte)], pix);
SYSTEM.MOVE(SYSTEM.ADR(pix[0]), dadr, 3);
INC(sadr); INC(dadr, 3); DEC(len)
END
END MoveI8BGR24;
PROCEDURE MoveI8BGRA32 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR byte: CHAR; pix: Pixel;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, byte);
ColorToPixel(bl.src.pal.col[ORD(byte)], pix);
SYSTEM.MOVE(SYSTEM.ADR(pix[0]), dadr, 4);
INC(sadr); INC(dadr, 4); DEC(len)
END
END MoveI8BGRA32;
PROCEDURE MoveI8Any (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR byte: CHAR; pix: Pixel;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, byte);
ColorToPixel(bl.src.pal.col[ORD(byte)], pix);
bl.dst.pack(bl.dst, dadr, dbit, pix);
INC(sadr); dbit := dbit + bl.dst.bpp; INC(dadr, dbit DIV 8); dbit := dbit MOD 8; DEC(len)
END
END MoveI8Any;
PROCEDURE Move16 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR int: INTEGER;
BEGIN
WHILE len > 0 DO
SYSTEM.GET(sadr, int); SYSTEM.PUT(dadr, int);
INC(sadr, 2); INC(dadr, 2); DEC(len)
END
END Move16;
PROCEDURE MoveConst16 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR int: INTEGER;
BEGIN
SYSTEM.GET(SYSTEM.ADR(bl.pix[0]), int);
WHILE len > 0 DO
SYSTEM.PUT(dadr, int);
INC(dadr, 2); DEC(len)
END
END MoveConst16;
PROCEDURE MoveBGR555I8 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR int, r, g, b: INTEGER;
BEGIN (* >>byte order! *)
WHILE len > 0 DO
SYSTEM.GET(sadr, int);
r := SHORT(ASH(ASH(int, -10) MOD 20H, 3));
g := SHORT(ASH(ASH(int, -5) MOD 20H, 3));
b := SHORT(ASH(int MOD 20H, 3));
SYSTEM.PUT(dadr, CHR(BestIndex(bl.dst.pal, r, g, b)));
INC(sadr, 2); INC(dadr); DEC(len)
END
END MoveBGR555I8;
PROCEDURE MoveBGR555BGR24 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR int: INTEGER;
BEGIN (* >>byte order! *)
WHILE len > 0 DO
SYSTEM.GET(sadr, int);
SYSTEM.PUT(dadr, CHR(ASH(int MOD 20H, 3)));
SYSTEM.PUT(dadr+1, CHR(ASH(ASH(int, -5) MOD 20H, 3)));
SYSTEM.PUT(dadr+2, CHR(ASH(ASH(int, -10) MOD 20H, 3)));
INC(sadr, 2); INC(dadr, 3); DEC(len)
END
END MoveBGR555BGR24;
PROCEDURE MoveBGR555BGRA32 (VAR bl: BlendOp0; sadr, sbit, dadr, dbit, len: LONGINT);
VAR int: INTEGER;
BEGIN (* >>byte order! *)
WHILE len > 0 DO
SYSTEM.GET(sadr, int);
SYSTEM.PUT(dadr, CHR(ASH(int MOD 20H, 3)));
SYSTEM.PUT(dadr+1, CHR(ASH(ASH(int, -5) MOD 20H, 3)));