1 ! Copyright (C) 2009, 2010 Joe Groff, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.data alien.data.map fry generalizations kernel locals math.vectors
4 math.vectors.conversion math math.vectors.simd ranges sequences
5 specialized-arrays tools.test ;
6 FROM: alien.c-types => uchar short int float ;
7 SPECIALIZED-ARRAYS: int float float-4 uchar-16 ;
8 IN: alien.data.map.tests
10 { float-array{ 1.0 1.0 3.0 3.0 5.0 5.0 } }
12 int-array{ 1 3 5 } [ dup ] data-map( int -- float[2] )
18 float-4{ 0.0 0.0 0.0 0.0 }
19 float-4{ 1.0 1.0 1.0 1.0 }
20 float-4{ 2.0 2.0 2.0 2.0 }
23 3 <iota> [ float-4-with ] data-map( object -- float-4 )
29 float-4{ 0.0 1.0 2.0 3.0 }
30 float-4{ 4.0 5.0 6.0 7.0 }
31 float-4{ 8.0 9.0 10.0 11.0 }
34 12 <iota> [ float-4-boa ] data-map( object[4] -- float-4 )
38 { float-array{ 1.0 1.0 3.0 3.0 5.0 5.0 0.0 0.0 } }
40 int-array{ 1 3 5 } float-array{ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 }
41 [ dup ] data-map!( int -- float[2] )
44 :: float-pixels>byte-pixels-locals ( floats scale bias -- bytes )
46 [ scale 255.0 * v*n bias 255.0 * v+n float-4 int-4 vconvert ] 4 napply
47 [ int-4 short-8 vconvert ] 2bi@
48 short-8 uchar-16 vconvert
49 ] data-map( float-4[4] -- uchar-16 ) ; inline
51 : float-pixels>byte-pixels* ( floats scale bias -- bytes )
53 [ _ 255.0 * v*n _ 255.0 * v+n float-4 int-4 vconvert ] 4 napply
54 [ int-4 short-8 vconvert ] 2bi@
55 short-8 uchar-16 vconvert
56 ] data-map( float-4[4] -- uchar-16 ) ; inline
58 : float-pixels>byte-pixels ( floats -- bytes )
59 1.0 0.0 float-pixels>byte-pixels* ;
74 } 1.0 0.0 float-pixels>byte-pixels-locals
90 } float-pixels>byte-pixels
108 [ 255.0 v*n float-4 int-4 vconvert ] 4 napply
109 [ int-4 short-8 vconvert ] 2bi@
110 short-8 uchar-16 vconvert
111 ] data-map( float-4[4] -- uchar-16 )
114 : vmerge-transpose ( a b c d -- ac bd ac bd )
115 [ (vmerge) ] bi-curry@ bi* ; inline
117 CONSTANT: plane-count 4
119 : fold-rgba-planes ( r g b a -- rgba )
120 [ vmerge-transpose vmerge-transpose ]
121 data-map( uchar-16 uchar-16 uchar-16 uchar-16 -- uchar-16[plane-count] ) ;
143 B{ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 }
144 B{ 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 }
145 B{ 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 }
146 B{ 15 25 35 45 55 65 75 85 95 105 115 125 135 145 155 165 }
150 : data-map-compiler-bug-test ( n -- byte-array )
151 [ 0.0 1.0 1.0 ] dip /f <range>
152 [ ] data-map( object -- float ) ;
154 { float-array{ 0.0 0.5 1.0 } }
155 [ 2 data-map-compiler-bug-test float cast-array ]