1 ! (c)Joe Groff bsd license
2 USING: alien.data alien.data.map fry generalizations kernel locals math.vectors
3 math.vectors.conversion math math.vectors.simd math.ranges sequences
4 specialized-arrays tools.test ;
5 FROM: alien.c-types => uchar short int float ;
6 SPECIALIZED-ARRAYS: int float float-4 uchar-16 ;
7 IN: alien.data.map.tests
9 { float-array{ 1.0 1.0 3.0 3.0 5.0 5.0 } }
11 int-array{ 1 3 5 } [ dup ] data-map( int -- float[2] )
17 float-4{ 0.0 0.0 0.0 0.0 }
18 float-4{ 1.0 1.0 1.0 1.0 }
19 float-4{ 2.0 2.0 2.0 2.0 }
22 3 iota [ float-4-with ] data-map( object -- float-4 )
28 float-4{ 0.0 1.0 2.0 3.0 }
29 float-4{ 4.0 5.0 6.0 7.0 }
30 float-4{ 8.0 9.0 10.0 11.0 }
33 12 iota [ float-4-boa ] data-map( object[4] -- float-4 )
37 { float-array{ 1.0 1.0 3.0 3.0 5.0 5.0 0.0 0.0 } }
39 int-array{ 1 3 5 } float-array{ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 }
40 [ dup ] data-map!( int -- float[2] )
43 :: float-pixels>byte-pixels-locals ( floats scale bias -- bytes )
45 [ scale 255.0 * v*n bias 255.0 * v+n float-4 int-4 vconvert ] 4 napply
46 [ int-4 short-8 vconvert ] 2bi@
47 short-8 uchar-16 vconvert
48 ] data-map( float-4[4] -- uchar-16 ) ; inline
50 : float-pixels>byte-pixels* ( floats scale bias -- bytes )
52 [ _ 255.0 * v*n _ 255.0 * v+n float-4 int-4 vconvert ] 4 napply
53 [ int-4 short-8 vconvert ] 2bi@
54 short-8 uchar-16 vconvert
55 ] data-map( float-4[4] -- uchar-16 ) ; inline
57 : float-pixels>byte-pixels ( floats -- bytes )
58 1.0 0.0 float-pixels>byte-pixels* ;
73 } 1.0 0.0 float-pixels>byte-pixels-locals
89 } float-pixels>byte-pixels
107 [ 255.0 v*n float-4 int-4 vconvert ] 4 napply
108 [ int-4 short-8 vconvert ] 2bi@
109 short-8 uchar-16 vconvert
110 ] data-map( float-4[4] -- uchar-16 )
113 : vmerge-transpose ( a b c d -- ac bd ac bd )
114 [ (vmerge) ] bi-curry@ bi* ; inline
116 CONSTANT: plane-count 4
118 : fold-rgba-planes ( r g b a -- rgba )
119 [ vmerge-transpose vmerge-transpose ]
120 data-map( uchar-16 uchar-16 uchar-16 uchar-16 -- uchar-16[plane-count] ) ;
142 B{ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 }
143 B{ 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 }
144 B{ 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 }
145 B{ 15 25 35 45 55 65 75 85 95 105 115 125 135 145 155 165 }
149 : data-map-compiler-bug-test ( n -- byte-array )
150 [ 0.0 1.0 1.0 ] dip /f <range>
151 [ ] data-map( object -- float ) ;
153 { float-array{ 0.0 0.5 1.0 } }
154 [ 2 data-map-compiler-bug-test float cast-array ]