-
Notifications
You must be signed in to change notification settings - Fork 0
/
svitlyna.tal
2030 lines (1670 loc) · 45.8 KB
/
svitlyna.tal
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
|00 @System [
&vector $2 &expansion $2 &wst $1 &rst $1
&metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
]
|10 @Console [
&vector $2 &read $1 &pad $4
&type $1 &write $1 &error $1
]
|20 @Screen [
&vector $2 &width $2 &height $2 &auto $1 &pad
$1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1600
]
|80 @Controller [
&vector $2 &button $1 &key $1
]
|a0 @File [
&vector $2 &success $2 &stat $2 &delete $1
&append $1 &name $2 &length $2 &read $2 &write $2
]
|0000
( color channels )
%Mr { #00 } %Mg { #01 } %Mb { #02 }
@decode-x $2 @decode-y $2
@xfrac $2 @yfrac $2
@img-w $2 @img-h $2 @img-size $4
%Mmax-width { #1000 }
%Mmax-height { #1000 }
%Mmax-name-length { #07ff }
( 8-bit linear RGB colors, used for most processing )
( display device palette is in sRGB )
@color0 [ &r $1 &g $1 &b $1 ]
@color1 [ &r $1 &g $1 &b $1 ]
@color2 [ &r $1 &g $1 &b $1 ]
@color3 [ &r $1 &g $1 &b $1 ]
( 7-bit linear RGB colors, used for dithering,
one bit reserved for signed arithmetic with quantization errors )
@color0-7 [ &r $1 &g $1 &b $1 ]
@color1-7 [ &r $1 &g $1 &b $1 ]
@color2-7 [ &r $1 &g $1 &b $1 ]
@color3-7 [ &r $1 &g $1 &b $1 ]
%Mfile-buffer-length { #4000 }
@part-split-step $1
@current-palette $2
@current-mode $1
%Mmode-streaming { #01 }
%Mmode-no-dithering { #02 }
%Mmode-partition-color-func { #04 }
%Mcout { .Console/write DEO }
%Mendl { #0a Mcout }
%Mprint-short { SWP print-byte print-byte }
%Mdebug-byte { Mcout DUP print-byte }
%Mdebug-short { Mcout OVR print-byte DUP print-byte }
%Mdebug32 { Mcout OVR2 Mprint-short DUP2 Mprint-short }
%Mdebug-rgb {
Mcout LIT "# Mcout
ROT DUP print-byte
ROT DUP print-byte
ROT DUP print-byte
}
%Mprint-bgr {
LIT "# Mcout
print-byte print-byte print-byte Mendl
}
%Mprint-rgb {
LIT "# Mcout
STH OVR print-byte
DUP print-byte
STHr DUP print-byte Mendl
}
( common pattern for adding offset to pointer )
%Msk+ { STH2kr ADD2 }
%Ms+ { STH2r ADD2 }
%Mload32 { LDA2k SWP2 INC2 INC2 LDA2 }
%Mnon-zero2 { ORA }
|0100
( init )
( memory check )
;str/memory-check print-str
;end Mprint-short
#1234 ;end STA2
;end LDA2 #1234 EQU2 ?{ ;str/memory-error !fail }
LIT 20 Mcout LIT "O Mcout LIT "K Mcout Mendl
;str/init print-str
;on-controller-input .Controller/vector DEO2
.Console/type DEI ?{
;read-stdin .Console/vector DEO2
;palettes/color .current-palette STZ2
prepare-palette-builtin
Mmode-streaming .current-mode STZ
;str/read print-str ;str/read-stdin print-str
;detect-magic set-on-bytes-received
#0008 set-bytes-expected
;display set-on-pixel-received
!display-init
}
;read-argument .Console/vector DEO2
BRK
@read-stdin ( -> )
[ LIT2 &index 0000 ]
DUP2 ;file-buf ADD2
.Console/read DEI ROT ROT STA
INC2 DUP2 [ LIT2 &bytes-expected $2 ] NEQ2 ?{
( call subroutine )
;file-buf [ LIT2 &on-bytes-received $2 ] JSR2
POP2 #0000 [ ,&index STR2 ]
BRK
}
[ ,&index STR2 ]
BRK
@read-argument ( -> )
.Console/type DEI
DUP #03 NEQ ?{
;str/usage !fail-str
}
#02 EQU ?{
#00 [ ,&name-length LDR2 ] ;filename ADD2 STA
Mendl
LIT2 00 -part-split-step STZ
( ;palettes/bw .current-palette STZ2 )
( prepare-palette-builtin )
( !display-init )
;palettes/detected-from-img .current-palette STZ2
!palette-split-init
}
.Console/read DEI
[ ,&name-length LDR2 ] ;filename ADD2 STA
[ ,&name-length LDR2 ] INC2
DUP2 Mmax-name-length LTH2 ?{
;str/filename-big !fail-str
}
[ ,&name-length STR2 ]
BRK
&name-length 0000
@read-file ( -> )
;str/read print-str ;str/read-file print-str
;filename print-str Mendl
;detect-magic set-on-bytes-received
#0008 set-bytes-expected
;filename .File/name DEO2
Mfile-buffer-length .File/length DEO2
;file-buf ;read-buffer/window STA2
;read-buffer .Screen/vector DEO2
BRK
@read-buffer ( -> )
;file-buf
DUP2 .File/read DEO2
.File/success DEI2 ADD2 [ ,&window-end STR2 ]
&loop
[ LIT2 &window =file-buf ]
DUP2 [ LIT2 &bytes-expected $2 ] ADD2 ( next window )
[ LIT2 &window-end =file-buf/end ]
GTH2k ?{
POP2
[ ,&window STR2 ]
[ LIT2 &on-bytes-received $2 ] JSR2
!&loop
( copy cmd )
&cmd [
01 0020 0000 =file-buf/pre-end
0000 =file-buf-pre
]
}
( not enough bytes )
;file-buf/end NEQ2 ?{
( buffer full )
( copy last 20 bytes to pre-buffer )
;&cmd .System/expansion DEO2
POP2
Mfile-buffer-length SUB2 [ ,&window STR2 ]
( a good time to take a break? )
[ LIT &break-counter 00 ] INC
DUP #10 NEQ ?{
POP #00 [ ,&break-counter STR ]
BRK
}
[ ,&break-counter STR ]
!read-buffer
}
#0000 .Screen/vector DEO2
[ LIT2 &on-file-end $2 ] JMP2
@set-on-bytes-received ( {routine}* -- )
DUP2 ;read-buffer/on-bytes-received STA2
;read-stdin/on-bytes-received STA2
JMP2r
@set-bytes-expected ( bytes* -- )
DUP2 ;read-buffer/bytes-expected STA2
;read-stdin/bytes-expected STA2
JMP2r
@set-on-pixel-received ( bytes* -- )
DUP2 ;ff-next-pixel/on-pixel-received STA2
;qoi-on-pixel-received/routine STA2
JMP2r
@set-on-file-end ( bytes* -- )
;read-buffer/on-file-end STA2
JMP2r
( ui )
@on-controller-input ( -> )
.Controller/button DEI
DUP #01 AND ?on-a-ctrl
DUP #02 AND ?on-b-alt
DUP #10 AND ?on-dpad-up
#20 AND ?on-dpad-down
BRK
@on-a-ctrl ( -> )
Mmode-streaming is-mode ?end
;palettes/detected-from-img .current-palette STZ2
.part-split-step LDZ [ #03 ] EQU ?{
!palette-split-init
}
prepare-palette-avg
!display-init
@on-b-alt ( -> )
Mmode-streaming is-mode ?end
.current-palette LDZ2 ;palettes/detected-from-img NEQ2 ?{
Mmode-partition-color-func is-mode ?{
Mmode-partition-color-func #01 set-mode
!display-init
}
Mmode-partition-color-func #00 set-mode
( also toggle dithering, allowing 4 modes )
}
Mmode-no-dithering is-mode ?{
Mmode-no-dithering #01 set-mode
!display-init
}
Mmode-no-dithering #00 set-mode
!display-init
@on-dpad-up ( -> )
Mmode-streaming is-mode ?end
.current-palette LDZ2
#000c ADD2
DUP2 ;palettes/detected-from-img LTH2 ?{
POP2 ;palettes
}
.current-palette STZ2
prepare-palette-builtin
!display-init
@on-dpad-down ( -> )
Mmode-streaming is-mode ?end
.current-palette LDZ2
DUP2 ;palettes GTH2 ?{
POP2 ;palettes/detected-from-img
}
#000c SUB2
.current-palette STZ2
prepare-palette-builtin
!display-init
@is-mode ( mode -- bool )
.current-mode LDZ AND
JMP2r
@set-mode ( mode value -- bool )
?{ .current-mode LDZ EOR .current-mode STZ JMP2r }
.current-mode LDZ ORA .current-mode STZ
JMP2r
( image formats )
@detect-magic ( {buffer}* -- )
DUP2 ;&farbfeld str-starts-with ?{
!&try-qoi
}
;&farbfeld print-str Mendl
POP2
;ff-size set-on-bytes-received
#0008 set-bytes-expected
JMP2r
[ &farbfeld "farbfeld 00 ]
[ &qoif "qoif 00 ]
&try-qoi
DUP2 ;&qoif str-starts-with ?{
!bad-file
}
;&qoif print-str Mendl
!qoi-header1
@ff-size ( {buffer}* -- )
LDA2k Mnon-zero2 ?too-large
INC2 INC2 LDA2k ( width ) SWP2
INC2 INC2 LDA2k Mnon-zero2 ?too-large
INC2 INC2 LDA2 ( height )
handle-image-size
;ff-next-pixel set-on-bytes-received
#0008 set-bytes-expected
JMP2r
( farbfeld uses 16-bit depth, not alpha-premultiplied )
( we don't load the lower 8 bits )
@ff-next-pixel ( {buffer}* -- )
STH2k #0006 ADD2 LDA ( alpha )
( 255 INC becomes 0, so lambda isn't skipped -> opaque )
INCk ?{
( alpha is zero )
POP
LDAkr STHr ( r )
#0002 Msk+ LDA ( g )
#0004 Ms+ LDA ( b )
!&result
}
#00 SWP
DUP2 #00 LDAkr STHr MUL2 POP ( 00 aa ar )
ROT ROT
DUP2 #00 #0002 Msk+ LDA MUL2 POP ( ar 00 aa ag )
ROT ROT
#00 #0004 Ms+ LDA MUL2 POP ( ar ag ab )
&result
srgb-to-linear
[ LIT2 &on-pixel-received $2 ] JMP2
@alpha-multiply ( r g b a -- r*a g*a b*a )
INCk ?{ POP JMP2r } ( don't multiply if opaque )
( r g b a )
STHk #00 ROT #00 STHr MUL2 POP STH
( r g a | b*a )
STHk #00 ROT #00 STHr MUL2 POP STH
( r a | b*a g*a )
STH #00 SWP #00 STHr MUL2 POP
STH2r SWP2r
JMP2r
@qoi-header1 ( {buffer}* -- )
#0004 ADD2
( width )
DUP2 LDA2 Mnon-zero2 ?too-large
INC2 INC2 LDA2 ;&w STA2
;qoi-header2 set-on-bytes-received
#0006 set-bytes-expected
JMP2r
&w $2
@qoi-header2 ( {buffer}* -- )
( height )
LDA2k Mnon-zero2 ?too-large
INC2 INC2 LDA2k
( width, again )
;qoi-header1/w LDA2
SWP2 handle-image-size
INC2 INC2 INC2 ( ignore channel count )
LDA
LIT "S Mdebug-byte Mendl
;qoi-is-linear STA
;qoi-last
&loop
#0000 OVR2 STA2
INC2 INC2
DUP2 ;qoi-index/end LTH2
?&loop
POP2
#ff ;qoi-last/a STA
;qoi-chunk set-on-bytes-received
#0001 set-bytes-expected
JMP2r
@qoi-last [ &r $1 &g $1 &b $1 &a $1 ]
@qoi-index [ &r $40 &g $40 &b $40 &a $40 ] &end $2
%Mqoi-stlast {
STH2 DUP2 ;qoi-last/r STA2
STH2r DUP2 ;qoi-last/b STA2
}
@qoi-stindex ( r g b a -- r g b a )
SWP2k
( r g b a b a r g )
#05 MUL ( 5g )
SWP #03 MUL ADD ( 3r )
SWP #0b MUL ADD ( 11a )
SWP #07 MUL ADD ( 7b )
#3f AND LITr 00 STH
SWP2k
;qoi-index/g Msk+ STA
;qoi-index/r Msk+ STA
;qoi-index/a Msk+ STA
;qoi-index/b Ms+ STA
JMP2r
@qoi-chunk ( {buffer}* -- )
LDA
( QOI_OP_RGB )
#fe NEQk ?{
POP2
;qoi-chunk-rgb set-on-bytes-received
#0003 set-bytes-expected
JMP2r
}
POP
( QOI_OP_RGBA )
#ff NEQk ?{
POP2
;qoi-chunk-rgba set-on-bytes-received
#0004 set-bytes-expected
JMP2r
}
POP
( QOI_OP_INDEX )
DUP #06 SFT ?{
( 7 6 5 4 3 2 1 0 )
( 0 0| index )
#00 SWP #3f AND STH2
;qoi-index/r Msk+ LDA
;qoi-index/g Msk+ LDA
;qoi-index/b Msk+ LDA
;qoi-index/a Ms+ LDA
Mqoi-stlast
alpha-multiply
!qoi-on-pixel-received
}
( QOI_OP_DIFF )
DUP #06 SFT #01 NEQ ?{
( 7 6 5 4 3 2 1 0 )
( 0 1| dr| dg| db )
DUP #04 SFT #03 AND ;qoi-last/r LDA ADD #02 SUB
OVR #02 SFT #03 AND ;qoi-last/g LDA ADD #02 SUB
ROT #03 AND ;qoi-last/b LDA ADD #02 SUB
;qoi-last/a LDA
Mqoi-stlast
qoi-stindex
alpha-multiply
!qoi-on-pixel-received
}
( QOI_OP_LUMA )
DUP #06 SFT #02 NEQ ?{
( 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 )
( 1 0| diff green | dr-rg | db-dg )
#3f AND ;qoi-chunk-luma/dg STA
;qoi-chunk-luma set-on-bytes-received
#0001 set-bytes-expected
JMP2r
}
( QOI_OP_RUN )
( 7 6 5 4 3 2 1 0 )
( 1 1| run length )
#3f AND INC
;qoi-last/r LDA
;qoi-last/g LDA
;qoi-last/b LDA
;qoi-last/a LDA
alpha-multiply
ROT STH STH2
#00
( wst: length count | rst: r g b )
&run-loop
ROTkr STHr STH2r
( wst: length count r g b | rst: r g b )
qoi-on-pixel-received
INC
NEQk
?&run-loop
POP2 POP2r POPr
JMP2r
@qoi-chunk-rgb ( {buffer}* -- )
LDAk
ROT ROT INC2 LDAk
ROT ROT INC2 LDA
;qoi-last/a LDA
qoi-stindex
Mqoi-stlast
alpha-multiply
qoi-on-pixel-received
;qoi-chunk set-on-bytes-received
#0001 set-bytes-expected
JMP2r
@qoi-chunk-rgba ( {buffer}* -- )
LDAk
ROT ROT INC2 LDAk
ROT ROT INC2 LDAk
ROT ROT INC2 LDA
qoi-stindex
Mqoi-stlast
alpha-multiply
qoi-on-pixel-received
;qoi-chunk set-on-bytes-received
#0001 set-bytes-expected
JMP2r
( 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 )
( 0 1| diff green | dr-rg | db-dg )
@qoi-chunk-luma ( {buffer}* -- )
LDA
DUP #04 SFT
[ ;&dg LDA ] ADD #28 SUB
;qoi-last/r LDA ADD
[ ;&dg LDA ] #20 SUB
;qoi-last/g LDA ADD
ROT #0f AND
[ ;&dg LDA ] ADD #28 SUB
;qoi-last/b LDA ADD
;qoi-last/a LDA
qoi-stindex
Mqoi-stlast
alpha-multiply
qoi-on-pixel-received
;qoi-chunk set-on-bytes-received
#0001 set-bytes-expected
JMP2r
&dg $1
@qoi-on-pixel-received ( r g b -- )
[ ,qoi-is-linear LDR ] ?{
srgb-to-linear
}
[ LIT2 &routine $2 ] JMP2
@qoi-is-linear $1
( misc. )
@handle-image-size ( w* h* -- )
;str/height print-str DUP2 Mprint-short Mendl
DUP2 .Screen/height DEO2 .img-h STZ2
;str/width print-str DUP2 Mprint-short Mendl
DUP2 .Screen/width DEO2 .img-w STZ2
JMP2r
@prepare-palette-builtin ( -- )
.current-palette LDZ2
LDA2k .color0/g STZ .color0/r STZ
INC2 INC2 LDA2k .color1/r STZ .color0/b STZ
INC2 INC2 LDA2k .color1/b STZ .color1/g STZ
INC2 INC2 LDA2k .color2/g STZ .color2/r STZ
INC2 INC2 LDA2k .color3/r STZ .color2/b STZ
INC2 INC2 LDA2 .color3/b STZ .color3/g STZ
( fallthrough )
@prepare-palette ( -- )
.color0/r LDZ linear-to-srgb-single #f0 AND
.color1/r LDZ linear-to-srgb-single #04 SFT ORA
.color2/r LDZ linear-to-srgb-single #f0 AND
.color3/r LDZ linear-to-srgb-single #04 SFT ORA
.System/r DEO2
.color0/g LDZ linear-to-srgb-single #f0 AND
.color1/g LDZ linear-to-srgb-single #04 SFT ORA
.color2/g LDZ linear-to-srgb-single #f0 AND
.color3/g LDZ linear-to-srgb-single #04 SFT ORA
.System/g DEO2
.color0/b LDZ linear-to-srgb-single #f0 AND
.color1/b LDZ linear-to-srgb-single #04 SFT ORA
.color2/b LDZ linear-to-srgb-single #f0 AND
.color3/b LDZ linear-to-srgb-single #04 SFT ORA
.System/b DEO2
JMP2r
( @pixel-to-4bit ( r8 g8 b8 -- r g b ) )
( ROT DUP #f0 LTH ?{ POP #0f !&g } #08 ADD #04 SFT )
( &g )
( ROT DUP #f0 LTH ?{ POP #0f !&b } #08 ADD #04 SFT )
( &b )
( ROT DUP #f0 LTH ?{ POP #0f JMP2r } #08 ADD #04 SFT )
( JMP2r )
( 6-bit linear RGB is used for color partitions )
@rgb-to-rgb6 ( r g b -- r6 g6 b6 )
ROT #02 SFT
ROT #02 SFT
ROT #02 SFT
JMP2r
( image processing )
( 8-bit sRGB triplet to 8-bit linear RGB triplet )
@srgb-to-linear ( sr sg sb -- r g b )
ROT #00 SWP ;&lut ADD2 LDA
ROT #00 SWP ;&lut ADD2 LDA
ROT #00 SWP ;&lut ADD2 LDA
JMP2r
&lut [
00 00 00 00 00 00 00 01 01 01 01 01 01 01 01 01
01 01 02 02 02 02 02 02 02 02 03 03 03 03 03 03
04 04 04 04 04 05 05 05 05 06 06 06 06 07 07 07
08 08 08 08 09 09 09 0a 0a 0a 0b 0b 0c 0c 0c 0d
0d 0d 0e 0e 0f 0f 10 10 11 11 11 12 12 13 13 14
14 15 16 16 17 17 18 18 19 19 1a 1b 1b 1c 1d 1d
1e 1e 1f 20 20 21 22 23 23 24 25 25 26 27 28 29
29 2a 2b 2c 2d 2d 2e 2f 30 31 32 33 33 34 35 36
37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46
47 48 49 4a 4c 4d 4e 4f 50 51 52 54 55 56 57 58
5a 5b 5c 5d 5f 60 61 63 64 65 67 68 69 6b 6c 6d
6f 70 72 73 74 76 77 79 7a 7c 7d 7f 80 82 83 85
86 88 8a 8b 8d 8e 90 92 93 95 97 98 9a 9c 9d 9f
a1 a3 a4 a6 a8 aa ab ad af b1 b3 b5 b7 b8 ba bc
be c0 c2 c4 c6 c8 ca cc ce d0 d2 d4 d6 d8 da dc
de e0 e2 e5 e7 e9 eb ed ef f2 f4 f6 f8 fa fd ff
]
( single 8-bit linear RGB value to 8-bit sRGB value )
@linear-to-srgb-single ( scolor -- color )
#00 SWP ;&lut ADD2 LDA
JMP2r
&lut [
00 0d 16 1c 22 26 2a 2e 32 35 38 3b 3d 40 42 45
47 49 4b 4d 4f 51 53 55 56 58 5a 5c 5d 5f 60 62
63 65 66 68 69 6a 6c 6d 6e 70 71 72 73 75 76 77
78 79 7a 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88
89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 94 95 96 97
98 99 9a 9b 9b 9c 9d 9e 9f 9f a0 a1 a2 a3 a3 a4
a5 a6 a7 a7 a8 a9 aa aa ab ac ad ad ae af af b0
b1 b2 b2 b3 b4 b4 b5 b6 b6 b7 b8 b9 b9 ba bb bb
bc bd bd be be bf c0 c0 c1 c2 c2 c3 c4 c4 c5 c5
c6 c7 c7 c8 c8 c9 ca ca cb cb cc cd cd ce ce cf
d0 d0 d1 d1 d2 d2 d3 d4 d4 d5 d5 d6 d6 d7 d7 d8
d8 d9 da da db db dc dc dd dd de de df df e0 e0
e1 e2 e2 e3 e3 e4 e4 e5 e5 e6 e6 e7 e7 e8 e8 e9
e9 ea ea eb eb ec ec ed ed ee ee ee ef ef f0 f0
f1 f1 f2 f2 f3 f3 f4 f4 f5 f5 f6 f6 f6 f7 f7 f8
f8 f9 f9 fa fa fb fb fb fc fc fd fd fe fe ff ff
]
( partitioning color space, via median cut algorithm )
( see bottom of file for partition structure )
%Mpart-sort-buffer-size { #0100 }
%Mpart-sort-buffers-size { #0300 }
%Mpart-size { #030a }
%Mpart-max-color { #3f }
@part-debug ( part -- )
#00 OVR Mpart-size MUL2 STH2
( { "part 20 00 } STH2r print-str DUP print-byte Mcout )
LIT "R Mcout ;part/r-beg Msk+ LDA2
SWP print-byte LIT "- Mcout print-byte LIT ", Mcout
LIT "G Mcout ;part/g-beg Msk+ LDA2
SWP print-byte LIT "- Mcout print-byte LIT ", Mcout
LIT "B Mcout ;part/b-beg Msk+ LDA2
SWP print-byte LIT "- Mcout print-byte Mendl
.part-split-step LDZ INC LTH ?{ POP2r JMP2r }
LIT "C Mcout ;part/count Msk+
LDA2k Mprint-short INC2 INC2 LDA2 Mprint-short Mendl
#0000
LIT "S Mcout LIT "R Mcout Mendl
&loopr
DUP2 #02 SFT2 NIP
print-byte LIT "| Mcout
DUP2 ;part/r-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2 ;part/r-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2
DUP #40 SFT ?{ Mendl !&end-print-r }
#20 Mcout
&end-print-r
Mpart-sort-buffer-size NEQ2
?&loopr
POP2 #0000
LIT "S Mcout LIT "G Mcout Mendl
&loopg
DUP2 #02 SFT2 NIP
print-byte LIT "| Mcout
DUP2 ;part/g-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2 ;part/g-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2
DUP #40 SFT ?{ Mendl !&end-print-g }
#20 Mcout
&end-print-g
Mpart-sort-buffer-size NEQ2
?&loopg
#0000
LIT "S Mcout LIT "B Mcout Mendl
&loopb
DUP2 #02 SFT2 NIP
print-byte LIT "| Mcout
DUP2 ;part/b-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2 ;part/b-sort Msk+ ADD2 LDA2 Mprint-short
INC2 INC2
DUP2
DUP #40 SFT ?{ Mendl !&end-print-b }
#20 Mcout
&end-print-b
Mpart-sort-buffer-size NEQ2
?&loopb
POP2
Mendl POP2r
JMP2r
@part-init ( part -- )
#00 SWP Mpart-size MUL2 STH2
#0000 ;part/count Msk+ STA2k
INC2 INC2 STA2
#0000
&loop
#0000 OVR2 ;part/r-sort ADD2 Msk+ STA2
INC2 INC2 DUP2 Mpart-sort-buffers-size NEQ2
?&loop
POP2 POP2r
JMP2r
@part-add-pixel ( r6 g6 b6 part -- )
#00 SWP Mpart-size MUL2 STH2
#00 SWP #20 SFT2 ;part/b-sort Msk+ ADD2 inc32-addr
#00 SWP #20 SFT2 ;part/g-sort Msk+ ADD2 inc32-addr
#00 SWP #20 SFT2 ;part/r-sort Msk+ ADD2 inc32-addr
;part/count Ms+
!inc32-addr
( get color variation within partition )
@part-get-max-var ( part -- max-var-chan max-var )
#00 SWP Mpart-size MUL2
STH2
;part/count Msk+ Mload32
non-zero32 ?{ POP2r #0000 JMP2r }
( order of preference:
if green has equal variation to red or blue, prefer green
if red has equal variation to blue, prefer red )
( we will ignore values less than 1/16 of max value )
%Mpart-var-filter { #04 }
LIT "G Mcout
( find max value )
#0000 DUP2 [ ,&g-max-h STR2 ] [ ,&g-max-l STR2 ]
;part/g-beg Msk+ LDA
&find-g-max
#00 OVR #20 SFT ;part/g-sort ADD2 Msk+ Mload32
OVR2 OVR2
[ ,&g-max-h LDR2 ] [ ,&g-max-l LDR2 ]
( index valh* vall* valh* vall* maxh* maxl* )
lt32 ?{ [ ,&g-max-l STR2 ] [ ,&g-max-h STR2 ] !&g-max }
POP2 POP2
&g-max
INC
DUP ;part/g-end Msk+ LDA NEQ ?{ !&found-g-max }
!&find-g-max
[ &g-max-h $2 &g-max-l $2 ]
&found-g-max
POP
[ ,&g-max-h LDR2 ] [ ,&g-max-l LDR2 ]
LIT "M Mdebug32
Mpart-var-filter rshift32
LIT "> Mdebug32
[ ,&g-max-l STR2 ] [ ,&g-max-h STR2 ]
( find index of first non-zero element )
;part/g-beg Msk+ LDA
&find-g-beg
#00 OVR #20 SFT ;part/g-sort ADD2 Msk+ Mload32
[ ,&g-max-h LDR2 ] [ ,&g-max-l LDR2 ]
gteq32 ?&found-g-beg
INC
!&find-g-beg
&found-g-beg
LIT "B Mdebug-byte
( find index of last non-zero element )
;part/g-end Msk+ LDA #01 SUB
&find-g-end
#00 OVR #20 SFT ;part/g-sort ADD2 Msk+ Mload32
;&g-max-h LDA2 ;&g-max-l LDA2
gteq32 ?&found-g-end
#01 SUB
!&find-g-end
&found-g-end
LIT "E Mdebug-byte
SWP SUB
LIT "V Mdebug-byte Mendl
LIT "R Mcout
( find max value )
#0000 DUP2 [ ,&r-max-h STR2 ] [ ,&r-max-l STR2 ]
;part/r-beg Msk+ LDA
&find-r-max
#00 OVR #20 SFT ;part/r-sort ADD2 Msk+ Mload32
OVR2 OVR2
[ ,&r-max-h LDR2 ] [ ,&r-max-l LDR2 ]
( index valh* vall* valh* vall* maxh* maxl* )
lt32 ?{ [ ,&r-max-l STR2 ] [ ,&r-max-h STR2 ] !&r-max }
POP2 POP2
&r-max
INC
DUP ;part/r-end Msk+ LDA NEQ ?{ !&found-r-max }
!&find-r-max
[ &r-max-h $2 &r-max-l $2 ]
&found-r-max
POP
[ ,&r-max-h LDR2 ] [ ,&r-max-l LDR2 ]
LIT "M Mdebug32
Mpart-var-filter rshift32
LIT "> Mdebug32
[ ,&r-max-l STR2 ] [ ,&r-max-h STR2 ]
( find index of first non-zero element )
;part/r-beg Msk+ LDA
&find-r-beg
#00 OVR #20 SFT ;part/r-sort ADD2 Msk+ Mload32
[ ,&r-max-h LDR2 ] [ ,&r-max-l LDR2 ]
gteq32 ?&found-r-beg
INC
!&find-r-beg
&found-r-beg
LIT "B Mdebug-byte
( find index of last non-zero element )
;part/r-end Msk+ LDA #01 SUB
&find-r-end
#00 OVR #20 SFT ;part/r-sort ADD2 Msk+ Mload32
;&r-max-h LDA2 ;&r-max-l LDA2
gteq32 ?&found-r-end
#01 SUB
!&find-r-end
&found-r-end
LIT "E Mdebug-byte
SWP SUB
LIT "V Mdebug-byte Mendl
LIT "B Mcout
( find max value )
#0000 DUP2 [ ,&b-max-h STR2 ] [ ,&b-max-l STR2 ]
;part/b-beg Msk+ LDA
&find-b-max
#00 OVR #20 SFT ;part/b-sort ADD2 Msk+ Mload32
OVR2 OVR2
[ ,&b-max-h LDR2 ] [ ,&b-max-l LDR2 ]
( index valh* vall* valh* vall* maxh* maxl* )
lt32 ?{ [ ,&b-max-l STR2 ] [ ,&b-max-h STR2 ] !&b-max }
POP2 POP2
&b-max
INC
DUP ;part/b-end Msk+ LDA NEQ ?{ !&found-b-max }
!&find-b-max
[ &b-max-h $2 &b-max-l $2 ]
&found-b-max
POP
[ ,&b-max-h LDR2 ] [ ,&b-max-l LDR2 ]
LIT "M Mdebug32
Mpart-var-filter rshift32
LIT "> Mdebug32
[ ,&b-max-l STR2 ] [ ,&b-max-h STR2 ]
( find index of first non-zero element )
;part/b-beg Msk+ LDA
&find-b-beg
#00 OVR #20 SFT ;part/b-sort ADD2 Msk+ Mload32
[ ,&b-max-h LDR2 ] [ ,&b-max-l LDR2 ]
gteq32 ?&found-b-beg
INC
!&find-b-beg
&found-b-beg
LIT "B Mdebug-byte
( find index of last non-zero element )
;part/b-end Msk+ LDA #01 SUB
&find-b-end
#00 OVR #20 SFT ;part/b-sort ADD2 Msk+ Mload32
;&b-max-h LDA2 ;&b-max-l LDA2
gteq32 ?&found-b-end
#01 SUB
!&find-b-end
&found-b-end
LIT "E Mdebug-byte
SWP SUB
LIT "V Mdebug-byte Mendl
Mendl
POP2r
Mb STH
( wst: gvar rvar max-var | rst: max-var-chan )
LTHk ?{ POPr Mr STH SWP } NIP
( wst: gvar max-var | rst: max-var-chan )
LTHk ?{ POPr Mg STH SWP } NIP
STHr SWP