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 buffer [ yuv_buffer <struct> ]
23 rgb [ w h * 3 * <byte-array> ] |
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
36 255 min 0 max ; inline
38 : stride ( line yuv -- uvy yy )
39 [ uv_stride>> swap 2/ * ] [ y_stride>> * ] 2bi ; inline
41 : compute-y ( yuv uvy yy x -- y )
42 + >fixnum nip swap y>> swap alien-unsigned-1 16 - ; inline
44 : compute-v ( yuv uvy yy x -- v )
45 nip 2/ + >fixnum swap u>> swap alien-unsigned-1 128 - ; inline
47 : compute-u ( yuv uvy yy x -- v )
48 nip 2/ + >fixnum swap v>> swap alien-unsigned-1 128 - ; inline
50 :: compute-yuv ( yuv uvy yy x -- y u v )
51 yuv uvy yy x compute-y
52 yuv uvy yy x compute-u
53 yuv uvy yy x compute-v ; inline
55 : compute-blue ( y u v -- b )
56 drop 516 * 128 + swap 298 * + -8 shift clamp ; inline
58 : compute-green ( y u v -- g )
59 [ [ 298 * ] dip 100 * - ] dip 208 * - 128 + -8 shift clamp ;
62 : compute-red ( y u v -- g )
63 nip 409 * swap 298 * + 128 + -8 shift clamp ; inline
65 : compute-rgb ( y u v -- b g r )
66 [ compute-blue ] [ compute-green ] [ compute-red ] 3tri ;
69 : store-rgb ( index rgb b g r -- index )
70 [ pick 0 + pick set-nth-unsafe ]
71 [ pick 1 + pick set-nth-unsafe ]
72 [ pick 2 + pick set-nth-unsafe ] tri*
75 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
76 compute-yuv compute-rgb store-rgb 3 + ; inline
78 : yuv>rgb-row ( index rgb yuv y -- index )
81 [ yuv>rgb-pixel ] with with with with each ; inline
83 : yuv>rgb ( rgb yuv -- )
86 [ yuv>rgb-row ] with with each
89 HINTS: yuv>rgb byte-array yuv_buffer ;
91 : yuv>rgb-benchmark ( -- )
92 [ fake-data yuv>rgb ] with-destructors ;
94 MAIN: yuv>rgb-benchmark