-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtga.dats
1280 lines (1237 loc) · 34.5 KB
/
loadtga.dats
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
//
// Author: Artyom Shakhakov (artyom DOT shalkhakov AT gmail DOT com)
// Time: May, 2011
// Time: Dec, 2015 (ported to ATS2)
//
#include
"share/atspre_staload.hats"
staload STDIO = "libc/SATS/stdio.sats"
datatype PIXELTYPE (t@ype) =
| PTrgb (uint32)
| PTrgba (uint32)
| PTbw (uint8)
macdef
implies (x, y) = (if ,(x) then ,(y) else false)
(* ****** ****** *)
extern
praxi
opt_reset
{a:t0p}{b:bool}(x: !opt(INV(a), b) >> opt (a, false)):<prf> void
local
macrodef
rec
auxlist
(f, xs, y) =
(
//
if iscons! (xs) then
`(let prval () = ,(f) (,(car! xs)) in ,(auxlist (f, cdr! xs, y)) end)
else y // end of [if]
//
) // end of [auxlist]
in (* in of [local] *)
macdef
opt_none_mac (x) =
,(
if islist! (x) then auxlist(`(opt_none), x, `()) else `(opt_none ,(x))
)
macdef
opt_some_mac (x) =
,(
if islist! (x) then auxlist (`(opt_some), x, `()) else `(opt_some ,(x))
)
macdef
opt_unsome_mac (x) =
,(
if islist! (x) then auxlist (`(opt_unsome), x, `()) else `(opt_unsome ,(x))
)
macdef
opt_clear_mac (x) =
,(
if islist! (x) then auxlist (`(opt_clear), x, `()) else `(opt_clear ,(x))
)
macdef
opt_reset_mac (x) =
,(
if islist! (x) then auxlist (`(opt_reset), x, `()) else `(opt_reset ,(x))
) // end of [opt_reset_mac]
macdef
opt_clear_mac(x) =
,(
if islist! (x) then auxlist (`(opt_clear), x, `()) else `(opt_clear ,(x))
) // end of [opt_clear_mac]
end // end of [local]
(* ****** ****** *)
//
// primitive file reading functions
//
extern
fun{a:t@ype}
file_read (FILEref, &a? >> opt (a, r)): #[r:bool] bool (r)
typedef
file_read_type (a:t@ype) = (FILEref, &a? >> opt (a, r)) -> #[r:bool] bool (r)
extern
fun{a,b:t@ype}
file_read_tup2 (FILEref, &a? >> opt (a, r), &b? >> opt (b, r)): #[r:bool] bool (r)
extern
fun{a,b,c:t@ype}
file_read_tup3 (FILEref, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r)): #[r:bool] bool (r)
extern
fun{a,b,c,d:t@ype}
file_read_tup4 (FILEref, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r), &d? >> opt (d, r)): #[r:bool] bool (r)
extern
fun{a,b,c,d,e:t@ype}
file_read_tup5 (FILEref, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r), &d? >> opt (d, r), &e? >> opt (e, r)): #[r:bool] bool (r)
extern
fun{a,b,c,d,e,f:t@ype}
file_read_tup6 (FILEref, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r), &d? >> opt (d, r), &e? >> opt (e, r), &f? >> opt (f, r)): #[r:bool] bool (r)
extern
fun{a,b,c,d,e,f,g:t@ype}
file_read_tup7 (
FILEref
, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r), &d? >> opt (d, r), &e? >> opt (e, r)
, &f? >> opt (f, r), &g? >> opt (g, r)
): #[r:bool] bool (r)
extern
fun{a,b,c,d,e,f,g,h:t@ype}
file_read_tup8 (
FILEref
, &a? >> opt (a, r), &b? >> opt (b, r), &c? >> opt (c, r), &d? >> opt (d, r), &e? >> opt (e, r)
, &f? >> opt (f, r), &g? >> opt (g, r), &h? >> opt (h, r)
): #[r:bool] bool (r)
implement{a,b}
file_read_tup2 (fp, a, b) = let
val ar = file_read<a> (fp, a) in
if ar then let
val br = file_read<b> (fp, b) in
if br then true
else let
prval () = opt_reset_mac (a) in
false
end
end else let
prval () = opt_none {b} (b) in
false
end
end // end of [file_read_tup2]
implement{a,b,c}
file_read_tup3 (fp, a, b, c) = let
val abr = file_read_tup2<a,b> (fp, a, b) in
if abr then let
val cr = file_read<c> (fp, c) in
if cr then true
else let
prval () = opt_reset_mac (a, b) in
false
end
end else let
prval () = opt_none {c} (c) in
false
end
end // end of [file_read_tup3]
implement{a,b,c,d}
file_read_tup4 (fp, a, b, c, d) = let
val abcr = file_read_tup3<a,b,c> (fp, a, b, c) in
if abcr then let
val dr = file_read<d> (fp, d) in
if dr then true
else let
prval () = opt_reset_mac (a, b, c) in
false
end
end else let
prval () = opt_none {d} (d) in
false
end
end // end of [file_read_tup4]
implement{a,b,c,d,e}
file_read_tup5 (fp, a, b, c, d, e) = let
val abcdr = file_read_tup4<a,b,c,d> (fp, a, b, c, d) in
if abcdr then let
val er = file_read<e> (fp, e) in
if er then true
else let
prval () = opt_reset_mac (a, b, c, d) in
false
end
end else let
prval () = opt_none {e} (e) in
false
end
end // end of [file_read_tup5]
implement{a,b,c,d,e,f}
file_read_tup6 (fp, a, b, c, d, e, f) = let
val abcder = file_read_tup5<a,b,c,d,e> (fp, a, b, c, d, e) in
if abcder then let
val fr = file_read<f> (fp, f) in
if fr then true
else let
prval () = opt_reset_mac (a, b, c, d, e) in
false
end
end else let
prval () = opt_none {f} (f) in
false
end
end // end of [file_read_tup6]
implement{a,b,c,d,e,f,g}
file_read_tup7 (fp, a, b, c, d, e, f, g) = let
val abcdefr = file_read_tup6<a,b,c,d,e,f> (fp, a, b, c, d, e, f) in
if abcdefr then let
val gr = file_read<g> (fp, g) in
if gr then true
else let
prval () = opt_reset_mac (a, b, c, d, e, f) in
false
end
end else let
prval () = opt_none {g} (g) in
false
end
end // end of [file_read_tup7]
implement{a,b,c,d,e,f,g,h}
file_read_tup8 (fp, a, b, c, d, e, f, g, h) = let
val abcdefgr = file_read_tup7<a,b,c,d,e,f,g> (fp, a, b, c, d, e, f, g) in
if abcdefgr then let
val hr = file_read<h> (fp, h) in
if hr then true
else let
prval () = opt_reset_mac (a, b, c, d, e, f, g) in
false
end
end else let
prval () = opt_none {h} (h) in
false
end
end // end of [file_read_tup8]
(* ****** ****** *)
extern
fun
file_read_uint8_le : file_read_type (uint8)
extern
fun
file_read_uint16_le : file_read_type (uint16)
extern
fun
file_read_uint32_le : file_read_type (uint32)
implement
file_read_uint8_le (f, r) = let
val res = $STDIO.fgetc(f)
in
if res < 0 then let
prval () = opt_none {uint8} (r)
in
false
end else let
val () = r := $UNSAFE.castvwtp0 {uint8} (res)
prval () = opt_some {uint8} (r)
in
true
end
end // end of [file_read_uint8_le]
//
implement
file_read_uint16_le (f, r) = let
var r1: uint8
and r2: uint8
val b1 = file_read_uint8_le (f, r1)
in
if :(r1: uint8?, r2: uint8?) => b1 then let
val b2 = file_read_uint8_le (f, r2)
in
if :(r1: uint8?, r2: uint8?) => b2 then let
val r1_ = $UNSAFE.castvwtp0{uint16} (opt_unsome_get<uint8> (r1))
val r2_ = $UNSAFE.castvwtp0{uint16} (opt_unsome_get<uint8> (r2))
val () = r := g0uint_lor_uint16 (g0uint_lsl_uint16 (r2_, 8), r1_)
prval () = opt_some {uint16} (r)
in
true
end else let
prval () = opt_none {uint16} (r)
prval () = opt_clear {uint8} (r1)
prval () = opt_unnone {uint8} (r2)
in
false
end
end else let
prval () = opt_none {uint16} (r)
prval () = opt_unnone {uint8} (r1)
in
false
end
end
//
implement
file_read_uint32_le (f, r) = let
var r1: uint16
and r2: uint16
val b1 = file_read_uint16_le (f, r1)
in
if :(r1: uint16?, r2: uint16?) => b1 then let
val b2 = file_read_uint16_le (f, r2)
in
if :(r1: uint16?, r2: uint16?) => b2 then let
val r1_ = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint16> (r1))
val r2_ = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint16> (r2))
val () = r := g0uint_lor_uint32 (g0uint_lsl_uint32 (r2_, 16), r1_)
prval () = opt_some {uint32} (r)
in
true
end else let
prval () = opt_none {uint32} (r)
prval () = opt_clear {uint16} (r1)
prval () = opt_unnone {uint16} (r2)
in
false
end
end else let
prval () = opt_none {uint32} (r)
prval () = opt_unnone {uint16} (r1)
in
false
end
end
//
(* ****** ****** *)
extern
fun{a:t@ype}
file_read_array$elt (
FILEref
, &a? >> opt (a, r)
): #[r:bool] bool (r)
extern
fun{a:t@ype}
file_read_array_lr {n:int} ( // left to right
FILEref
, &(@[a?][n]) >> opt (@[a][n], r)
, size_t n
) : #[r:bool] bool (r)
extern
fun{a:t@ype}
file_read_array_rl {n:int} ( // right to left
FILEref
, &(@[a?][n]) >> opt (@[a][n], r)
, size_t n
) : #[r:bool] bool (r)
implement{a}
file_read_array$elt (fp, x) = file_read<a> (fp, x)
//
implement{a}
file_read_array_lr {n} (fp, A, asz) = let
//
fun
aux {i,n:nat | i <= n} {l:addr} (
pf1_arr: array_v (a, l, i), pf2_arr: array_v (a?, l+i*sizeof(a), n-i)
| p_arr: ptr (l+i*sizeof(a)), i: size_t i, n: size_t n
): [r:int] (vor (array_v (a, l, n), array_v (a?, l, n), r) | int r) = (
if i < n then let
prval (pf_at, pf2_arr) = array_v_uncons {a?} (pf2_arr)
prval [l0:addr] EQADDR () = ptr_get_index (p_arr)
val p_at = p_arr : ptr l0
prval pf_at = pf_at : a? @ l0
val res = file_read_array$elt<a> (fp, !p_at)
in
if res then let
prval () = opt_unsome {a} (!p_at)
prval pf1_arr = array_v_extend {a} (pf1_arr, pf_at)
val p1_arr = ptr1_succ<a> (p_arr)
in
aux (pf1_arr, pf2_arr | p1_arr, succ(i), n)
end else let
prval p_bas = praxi_ptr {l} ()
prval () = opt_unnone {a} (!p_at)
prval () = topize {@[a][i]} (!p_bas)
prval pf2_arr = array_v_cons {a?} (pf_at, pf2_arr)
prval pf_res = array_v_unsplit {a?} (pf1_arr, pf2_arr)
in
#[.. | (VORright (pf_res) | 1)]
end
end else let
prval () = array_v_unnil {a?} (pf2_arr)
prval pf_res = pf1_arr
in
#[.. | (VORleft (pf_res) | 0)]
end // end of [if]
) (* end of [aux] *)
//
prval pf1_arr = array_v_nil {a} ()
prval pf2_arr = view@(A)
val p_arr = addr@(A)
prval [l:addr] EQADDR () = eqaddr_make_ptr (p_arr)
prval () = lemma_g1uint_param (asz)
val (pf_vor | r) = aux {0,n} {l} (pf1_arr, pf2_arr | p_arr, (i2sz)0, asz)
//
in
//
if r = 0 then let
prval VORleft pf_arr = pf_vor
prval () = view@A := pf_arr
prval () = opt_some {@[a][n]} (A)
in
true
end else let
prval VORright pf_arr = pf_vor
prval () = view@A := pf_arr
prval () = opt_none {@[a][n]} (A)
in
false
end // end of [if]
//
end // end of [file_read_array_lr]
//
implement{a}
file_read_array_rl {n} (fp, A, asz) = let
//
fun
aux {i,n:nat | i <= n} {l:addr} (
pf1_arr: array_v (a?, l, i), pf2_arr: array_v (a, l+i*sizeof(a), n-i)
| p_arr: ptr (l+i*sizeof(a)), i: size_t i, n: size_t n
): [r:int] (vor (array_v (a, l, n), array_v (a?, l, n), r) | int r) = (
if i > 0 then let
prval (pf1_arr, pf_at) = array_v_unextend {a?} (pf1_arr)
val p1_arr = ptr1_pred<a> (p_arr)
prval [l0:addr] EQADDR () = ptr_get_index (p1_arr)
val p_at = p1_arr : ptr l0
prval pf_at = pf_at : a? @ l0
val res = file_read_array$elt<a> (fp, !p_at)
in
if res then let
prval () = opt_unsome {a} (!p_at)
prval pf2_arr = array_v_cons {a}{l0} (pf_at, pf2_arr)
in
aux (pf1_arr, pf2_arr | p1_arr, pred(i), n)
end else let
prval p_bas = praxi_ptr {l} ()
prval () = opt_unnone {a} (!p_at)
prval pf1_arr = array_v_extend {a?} (pf1_arr, pf_at)
prval pf_res = array_v_unsplit {a?} (pf1_arr, pf2_arr)
in
#[.. | (VORright (pf_res) | 1)]
end
end else let
prval () = array_v_unnil {a?} (pf1_arr)
prval pf_res = pf2_arr
prval MULbas () = mul_make {i,sizeof(a)} ()
in
#[.. | (VORleft (pf_res) | 0)]
end // end of [if]
) (* end of [aux] *)
//
val p_arr = addr@(A)
prval [l0:addr] EQADDR () = eqaddr_make_ptr (p_arr)
val p_arr = ptr1_add_guint<a> (p_arr, asz)
prval [l:addr] EQADDR () = eqaddr_make_ptr (p_arr)
//
prval pf1_arr = view@(A)
prval pf2_arr = array_v_nil {a} ()
prval () = lemma_g1uint_param (asz)
val (pf_vor | r) = aux (pf1_arr, pf2_arr | p_arr, asz, asz)
//
in
//
if r = 0 then let
prval VORleft pf_arr = pf_vor
prval () = view@A := pf_arr
prval () = opt_some {@[a][n]} (A)
in
true
end else let
prval VORright pf_arr = pf_vor
prval () = view@A := pf_arr
prval () = opt_none {@[a][n]} (A)
in
false
end // end of [if]
//
end // end of [file_read_array_rl]
//
(* ****** ****** *)
extern
fun{a:t@ype}
file_read_matrix_row {n:int} (
FILEref
, &(@[a?][n]) >> opt (@[a][n], r)
, size_t n
): #[r:bool] bool (r)
extern
fun{a:t@ype}
file_read_matrix_td(*top-down*) {m,n:int} (
FILEref
, &(@[a?][m*n]) >> opt (@[a][m*n], r)
, size_t m
, size_t n
) : #[r:bool] bool (r)
//
extern
fun{a:t@ype}
file_read_matrix_bu(*bottom-up*) {m,n:int} (
FILEref
, &(@[a?][m*n]) >> opt (@[a][m*n], r)
, size_t m
, size_t n
) : #[r:bool] bool (r)
//
extern
praxi
lemma_array_sizeof {a:vt@ype} {n:nat} (): [sizeof(@[a][n]) == n*sizeof(a)] void
//
extern
praxi
lemma_array2_sizeof {a:vt@ype} {m,n:nat} (): [sizeof(@[a][m*n]) == m*n*sizeof(a)] void
//
implement{a}
file_read_matrix_row {n} (fp, A, asz) = file_read_array_lr<a> (fp, A, asz)
//
implement{a}
file_read_matrix_td {m,n} (fp, mat, m, n) = let
//
typedef T = @[a][n]
prval () = lemma_g1uint_param (n)
//
fun
aux {i,m:nat | i <= m} {l:addr} (
pf1_arr: array_v (T, l, i), pf2_arr: array_v (T?, l+i*sizeof(T), m-i)
| p_arr: ptr (l+i*sizeof(T)), i: size_t i, m: size_t m
): [r:int] (vor (array_v (T, l, m), array_v (T?, l, m), r) | int r) = (
if i < m then let
prval (pf_at, pf2_arr) = array_v_uncons {T?} (pf2_arr)
prval [l0:addr] EQADDR () = ptr_get_index (p_arr)
val p_at = p_arr : ptr l0
prval pf_at = pf_at : T? @ l0
val res = file_read_matrix_row<a> (fp, !p_at, n)
in
if res then let
prval () = opt_unsome {T} (!p_at)
prval pf1_arr = array_v_extend {T} (pf1_arr, pf_at)
val p1_arr = ptr1_add_guint<a> (p_arr, n)
prval () = lemma_array_sizeof {a}{n} ()
in
aux (pf1_arr, pf2_arr | p1_arr, succ(i), m)
end else let
prval p_bas = praxi_ptr {l} ()
prval () = opt_unnone {T} (!p_at)
prval () = topize {@[T][i]} (!p_bas)
prval pf2_arr = array_v_cons {T?} (pf_at, pf2_arr)
prval pf_res = array_v_unsplit {T?} (pf1_arr, pf2_arr)
in
#[.. | (VORright (pf_res) | 1)]
end
end else let
prval () = array_v_unnil {T?} (pf2_arr)
prval pf_res = pf1_arr
in
#[.. | (VORleft (pf_res) | 0)]
end // end of [if]
) (* end of [aux] *)
//
prval pf_arr = view@(mat)
prval pf_mat = array_v_group {a?} (pf_arr)
//
prval pf1_arr = array_v_nil {T} ()
prval pf2_arr = pf_mat
val p_arr = addr@(mat)
prval [l:addr] EQADDR () = eqaddr_make_ptr (p_arr)
prval () = lemma_g1uint_param (m)
val (pf_vor | r) = aux {0,m} {l} (pf1_arr, pf2_arr | p_arr, (i2sz)0, m)
//
in
//
if r = 0 then let
prval VORleft pf_arr = pf_vor
prval () = view@(mat) := array_v_ungroup {a} (pf_arr)
prval () = opt_some {@[a][m*n]} (mat)
in
true
end else let
prval VORright pf_arr = pf_vor
prval () = view@(mat) := array_v_ungroup {a?} (pf_arr)
prval () = opt_none {@[a][m*n]} (mat)
in
false
end // end of [if]
//
end // end of [file_read_matrix_td]
//
implement{a}
file_read_matrix_bu {m,n} (fp, mat, m, n) = let
//
typedef T = @[a][n]
prval () = lemma_g1uint_param (n)
//
fun
aux {i,m:nat | i <= m} {l:addr} (
pf1_arr: array_v (T?, l, i), pf2_arr: array_v (T, l+i*sizeof(T), m-i)
| p_arr: ptr (l+i*sizeof(T)), i: size_t i, m: size_t m
) : [r:int] (vor (array_v (T, l, m), array_v (T?, l, m), r) | int r) = (
if i > 0 then let
prval (pf1_arr, pf_at) = array_v_unextend {T?} (pf1_arr)
val p1_arr = ptr1_sub_guint<a> (p_arr, n)
prval () = lemma_array_sizeof {a}{n} ()
prval [l0:addr] EQADDR () = ptr_get_index (p1_arr)
val p_at = p1_arr : ptr l0
prval pf_at = pf_at : T? @ l0
val res = file_read_matrix_row<a> (fp, !p_at, n)
in
if res then let
prval () = opt_unsome {T} (!p_at)
prval pf2_arr = array_v_cons {T}{l0} (pf_at, pf2_arr)
in
aux (pf1_arr, pf2_arr | p1_arr, pred(i), m)
end else let
prval p_bas = praxi_ptr {l} ()
prval () = opt_unnone {T} (!p_at)
prval pf1_arr = array_v_extend {T?} (pf1_arr, pf_at)
prval pf_res = array_v_unsplit {T?} (pf1_arr, pf2_arr)
in
#[.. | (VORright (pf_res) | 1)]
end
end else let
prval () = array_v_unnil {T?} (pf1_arr)
prval pf_res = pf2_arr
prval MULbas () = mul_make {i,sizeof(T)} ()
in
(VORleft (pf_res) | 0)
end // end of [if]
) (* end of [aux] *)
//
val p_arr = addr@(mat)
prval [l0:addr] EQADDR () = eqaddr_make_ptr (p_arr)
prval () = lemma_g1uint_param (m)
prval () = lemma_g1uint_param (n)
val mn = m*n
prval pf_mul_mn = mul_make {m,n} ()
prval [mn:int] EQINT () = eqint_make_guint (mn)
prval () = mul_nat_nat_nat (pf_mul_mn)
prval () = mul_elim (pf_mul_mn)
val p_arr = ptr1_add_guint<a> (p_arr, mn)
prval () = lemma_array_sizeof {a?} {m} ()
prval () = lemma_array2_sizeof {a?} {m,n} ()
prval [l:addr] EQADDR () = eqaddr_make_ptr (p_arr)
prval () = __assert () where {
extern
prfun
__assert (): [mn*sizeof(a?) == m*sizeof(@[a?][n])] void
} (* end of [prval] *)
//
prval pf_arr = view@(mat)
prval pf_mat = array_v_group {a?}{l0}{m,n} (pf_arr)
prval pf1_arr = pf_mat
prval pf2_arr = array_v_nil {T} ()
val (pf_vor | r) = aux (pf1_arr, pf2_arr | p_arr, m, m)
//
in
//
if r = 0 then let
prval VORleft pf_arr = pf_vor
prval () = view@(mat) := array_v_ungroup (pf_arr)
prval () = opt_some {@[a][m*n]} (mat)
in
true
end else let
prval VORright pf_arr = pf_vor
prval () = view@(mat) := array_v_ungroup (pf_arr)
prval () = opt_none {@[a][m*n]} (mat)
in
false
end // end of [if]
//
end // end of [file_read_matrix_bu]
//
(* ****** ****** *)
// TGA image loading
local
//
extern
fun{a:t@ype}
row_read {n:int} (
fp: FILEref
, A: &(@[a?][n]) >> opt (@[a][n], r)
, n: size_t n
) : #[r:bool] bool (r)
//
extern
fun{a:t@ype}
row_read_rle {n:int} (
fp: FILEref
, A: &(@[a?][n]) >> opt (@[a][n], r)
, n: size_t n
) : #[r:bool] bool (r)
//
implement{a}
row_read {n} (fp, A, asz) = file_read_array_lr<a> (fp, A, asz)
//
implement{a}
row_read_rle {n} (fp, A, asz) = let
//
fun
row_read_cst {n:int} (
A: &(@[a?][n]) >> @[a][n], asz: size_t n, x: a
): void = array_initize_elt<a> (A, asz, x)
//
fun loop {i,n:nat | i <= n} {l:addr} .<n-i>. (
pf1_arr: array_v (a, l, i)
, pf2_arr: array_v (a?, l+i*sizeof(a), n-i)
| p1: ptr l
, p2: ptr (l+i*sizeof(a))
, m: size_t (n-i)
) : [r:int] (vor (array_v (a, l, n), array_v (a?, l, n), r) | int r) =
if m = 0 then let
prval () = array_v_unnil {a?} (pf2_arr)
prval pf_res = VORleft (pf1_arr)
in
(pf_res | 0)
end else let
prval p_bas = praxi_ptr {l} ()
var hd: uint8
val b_hd = file_read<uint8> (fp, hd)
in
if :(hd: uint8?) => b_hd then let
val hd = opt_unsome_get<uint8> (hd)
val hd = g0uint2uint_uint8_uint (hd)
val sz = (u2sz)((g1ofg0)((hd land (g0ofg1)0x7fu)))
val sz = (g1ofg0)sz
val sz = sz + (i2sz)1
prval [j:int] EQINT () = eqint_make_guint (sz)
val x = (hd land 0x80u) > 0u
in
if sz > m then let
val () = prerrln!("[row_read_rle]: spans rows")
prval () = topize {@[a][i]} (!p_bas)
prval pf_arr = array_v_unsplit {a?} (pf1_arr, pf2_arr)
prval pf_res = VORright (pf_arr)
in
(pf_res | 1)
end else let // sz <= m
prval (pf31_arr, pf32_arr) = array_v_split_at {a?} (pf2_arr | sz)
prval [l0:addr] EQADDR () = ptr_get_index (p2)
val p_arr = p2 : ptr l0
prval pf31_arr = pf31_arr : array_v (a?, l0, j)
in
if x then let
var v: a
val bv = file_read<a> (fp, v)
in
if :(v: a?) => bv then let
val v = opt_unsome_get<a> (v)
val () = row_read_cst (!p_arr, sz, v)
val p2 = ptr1_add_guint<a> (p_arr, sz)
prval pf_arr = array_v_unsplit {a} (pf1_arr, pf31_arr)
prval () = lemma_g1uint_param (sz)
val m1 = m - sz
in
loop (pf_arr, pf32_arr | p1, p2, m1)
end else let
prval () = opt_unnone {a} (v)
prval pf2_arr = array_v_unsplit (pf31_arr, pf32_arr)
prval () = topize (!p_bas)
prval pf_arr = array_v_unsplit (pf1_arr, pf2_arr)
in
(VORright (pf_arr) | 1)
end
end else let
val br = row_read (fp, !p_arr, sz)
in
if br then let
prval () = opt_unsome (!p_arr)
val p2 = ptr1_add_guint<a> (p2, sz)
prval pf_arr = array_v_unsplit {a} (pf1_arr, pf31_arr)
val m = m - sz
in
loop (pf_arr, pf32_arr | p1, p2, m)
end else let
prval () = opt_unnone (!p_arr)
prval pf3_arr = array_v_unsplit {a?} (pf31_arr, pf32_arr)
prval () = topize (!p_bas)
prval pf_arr = array_v_unsplit {a?} (pf1_arr, pf3_arr)
prval pf_res = VORright (pf_arr)
in
(pf_res | 1)
end
end
end
end else let
prval () = opt_unnone {uint8} (hd)
prval () = topize {@[a][i]} (!p_bas)
prval pf_res = array_v_unsplit {a?} (pf1_arr, pf2_arr)
in
#[.. | (VORright (pf_res) | 1)]
end
end // end of [loop]
//
prval pf1_arr = array_v_nil {a} ()
val p_arr = addr@(A)
prval pf2_arr = view@(A)
prval () = lemma_g1uint_param (asz)
val (pf_res | res) = loop (pf1_arr, pf2_arr | p_arr, p_arr, asz)
//
in
if res = 0 then let
prval VORleft pf_arr = pf_res
prval () = view@(A) := pf_arr
prval () = opt_some {@[a][n]} (A)
in
true
end else let
prval VORright pf_arr = pf_res
prval () = view@(A) := pf_arr
prval () = opt_none {@[a][n]} (A)
in
false
end
end // end of [row_read_rle]
//
in // of [local]
//
extern
fun
load_TGA {i,j:int;l0:addr} (
FILEref
, &size_t(i)? >> opt (size_t w, r)
, &size_t(j)? >> opt (size_t h, r)
, &ptr(l0)? >> opt (ptr l, r)
) : #[w,h:int;l:addr;r:bool] (option_v ((matrix_v (uint32, l, w, h), mfree_gc_v (l)), r) | bool (r))
//
implement
file_read<uint8> (fp, x) = file_read_uint8_le (fp, x)
implement
file_read<uint16> (fp, x) = file_read_uint16_le (fp, x)
implement
file_read<uint32> (fp, x) = file_read_uint32_le (fp, x)
//
typedef TGA_hdr = @{
id_len= uint8
, color_map_type= uint8
, image_type= uint8
, color_map_index= uint16
, color_map_length= uint16
, color_map_size= uint8
, origin_x= uint16
, origin_y= uint16
, width= uint16
, height= uint16
, pixel_size= uint8
, attribs= uint8
}
//
implement
file_read<TGA_hdr> (fp, res) = let
val b1 = file_read_tup8<uint8,uint8,uint8,uint16,uint16,uint8,uint16,uint16> (
fp
, res.id_len, res.color_map_type
, res.image_type
, res.color_map_index, res.color_map_length, res.color_map_size
, res.origin_x, res.origin_y
)
in
if b1 then let
val b2 = file_read_tup4<uint16,uint16,uint8,uint8> (fp, res.width, res.height, res.pixel_size, res.attribs)
in
if b2 then let
prval () = opt_unsome (res.id_len)
// skip comment
val id_len = g0uint2uint_uint8_uint (res.id_len)
val id_len = g0uint2int_uint_lint (id_len)
val b3 = $STDIO.fseek0 (fp, id_len, $STDIO.SEEK_CUR)
in
if b3 = 0 then let
prval () = opt_unsome_mac (
res.color_map_type, res.image_type, res.color_map_index, res.color_map_length, res.color_map_size
, res.origin_x, res.origin_y, res.width, res.height, res.pixel_size, res.attribs
) (* end of [prval] *)
prval () = opt_some {TGA_hdr} (res)
in
true
end else let
prval () = topize (res.id_len)
prval () = opt_clear_mac (
res.color_map_type, res.image_type, res.color_map_index, res.color_map_length, res.color_map_size
, res.origin_x, res.origin_y, res.width, res.height, res.pixel_size, res.attribs
) (* end of [prval] *)
prval () = opt_none {TGA_hdr} (res)
in
false
end
end else let
prval () = opt_clear_mac (
res.id_len
, res.color_map_type, res.image_type, res.color_map_index, res.color_map_length, res.color_map_size
, res.origin_x, res.origin_y, res.width, res.height, res.pixel_size, res.attribs
) (* end of [prval] *)
prval () = opt_none {TGA_hdr} (res)
in
false
end
end else let
prval () = opt_clear_mac (
res.id_len, res.color_map_type, res.image_type, res.color_map_index, res.color_map_length, res.color_map_size
, res.origin_x, res.origin_y
) (* end of [prval] *)
prval () = opt_none {TGA_hdr} (res)
in
false
end
end // end of [file_read<TGA_hdr>]
//
extern
fun
file_read_grayscale : file_read_type (uint32)
extern
fun
file_read_rgb : file_read_type (uint32)
extern
fun
file_read_rgba : file_read_type (uint32)
//
overload lsl with g0uint_lsl_uint32
//
implement
file_read_grayscale (fp, res) = let
var b: uint8
val xb = file_read<uint8> (fp, b)
in
if :(b: uint8?) => xb then let
val b = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (b))
val () = res := (($UNSAFE.castvwtp0{uint32}(255u) lsl 24) lor (b lsl 16) lor (b lsl 8) lor b)
prval () = opt_some (res)
in
true
end else let
prval () = opt_clear (b)
prval () = opt_none (res)
in
false
end
end // end of [file_read_grayscale]
//
implement
file_read_rgb (fp, res) = let
var b: uint8
var g: uint8
var r: uint8
val bgr = file_read_tup3<uint8,uint8,uint8> (fp, b, g, r)
in
if :(b: uint8?, g: uint8?, r: uint8?) => bgr then let
val b = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (b))
val g = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (g))
val r = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (r))
val () = res := (($UNSAFE.castvwtp0{uint32}(255u) lsl 24) lor (b lsl 16) lor (g lsl 8) lor r)
prval () = opt_some (res)
in
true
end else let
prval () = opt_clear_mac (b, g, r)
prval () = opt_none (res)
in
false
end
end // end of [file_read_rgb]
//
implement
file_read_rgba (fp, res) = let
var b: uint8
and g: uint8
and r: uint8
and a: uint8
val bgra = file_read_tup4<uint8,uint8,uint8,uint8> (fp, b, g, r, a)
in
if :(b: uint8?, g: uint8?, r: uint8?, a: uint8?) => bgra then let
val b = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (b))
val g = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (g))
val r = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (r))
val a = $UNSAFE.castvwtp0{uint32} (opt_unsome_get<uint8> (a))
val () = res := ((a lsl 24) lor (b lsl 16) lor (g lsl 8) lor r)
prval () = opt_some (res)
in
true
end else let
prval () = opt_clear_mac (b, g, r, a)
prval () = opt_none (res)
in
false
end // end of [if]
end // end of [file_read_rgba]
//
extern
fun
file_read_TGA_image {m,n:int} {i:pos | i <= 4} (
FILEref
, int(i)(*pixelsize, bytes*)
, bool(*rle*)
, bool(*topdown*)
, size_t m
, size_t n