1 ! Copyright (C) Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.accessors alien.c-types alien.syntax byte-arrays
4 destructors generalizations hints kernel libc locals math math.order
5 sequences sequences.private classes.struct accessors alien.data ;
6 IN: benchmark.yuv-to-rgb
19 :: fake-data ( -- rgb yuv )
22 yuv_buffer <struct> :> buffer
23 w h * 3 * <byte-array> :> rgb
30 w h * [ dup * ] B{ } map-as malloc-byte-array &free >>y
31 w h * 2/ [ dup dup * * ] B{ } map-as malloc-byte-array &free >>u
32 w h * 2/ [ dup * dup * ] B{ } map-as malloc-byte-array &free >>v ;
35 255 min 0 max ; inline
37 : stride ( line yuv -- uvy yy )
38 [ uv_stride>> swap 2/ * ] [ y_stride>> * ] 2bi ; inline
40 : compute-y ( yuv uvy yy x -- y )
41 + >fixnum nip swap y>> swap alien-unsigned-1 16 - ; inline
43 : compute-v ( yuv uvy yy x -- v )
44 nip 2/ + >fixnum swap u>> swap alien-unsigned-1 128 - ; inline
46 : compute-u ( yuv uvy yy x -- v )
47 nip 2/ + >fixnum swap v>> swap alien-unsigned-1 128 - ; inline
49 :: compute-yuv ( yuv uvy yy x -- y u v )
50 yuv uvy yy x compute-y
51 yuv uvy yy x compute-u
52 yuv uvy yy x compute-v ; inline
54 : compute-blue ( y u v -- b )
55 drop 516 * 128 + swap 298 * + -8 shift clamp ; inline
57 : compute-green ( y u v -- g )
58 [ [ 298 * ] dip 100 * - ] dip 208 * - 128 + -8 shift clamp ;
61 : compute-red ( y u v -- g )
62 nip 409 * swap 298 * + 128 + -8 shift clamp ; inline
64 : compute-rgb ( y u v -- b g r )
65 [ compute-blue ] [ compute-green ] [ compute-red ] 3tri ;
68 : store-rgb ( index rgb b g r -- index )
69 [ pick 0 + pick set-nth-unsafe ]
70 [ pick 1 + pick set-nth-unsafe ]
71 [ pick 2 + pick set-nth-unsafe ] tri*
74 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
75 compute-yuv compute-rgb store-rgb 3 + ; inline
77 : yuv>rgb-row ( index rgb yuv y -- index )
80 [ yuv>rgb-pixel ] with with with with each ; inline
82 : yuv>rgb ( rgb yuv -- )
85 [ yuv>rgb-row ] with with each
88 HINTS: yuv>rgb byte-array yuv_buffer ;
90 : yuv>rgb-benchmark ( -- )
91 [ fake-data yuv>rgb ] with-destructors ;
93 MAIN: yuv>rgb-benchmark