]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/yuv-to-rgb/yuv-to-rgb.factor
move some allocation words that don't really have much to do with c types out of...
[factor.git] / extra / benchmark / yuv-to-rgb / yuv-to-rgb.factor
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
7
8 STRUCT: yuv_buffer
9     { y_width int }
10     { y_height int }
11     { y_stride int }
12     { uv_width int }
13     { uv_height int }
14     { uv_stride int }
15     { y void* }
16     { u void* }
17     { v void* } ;
18
19 :: fake-data ( -- rgb yuv )
20     [let* | w [ 1600 ]
21             h [ 1200 ]
22             buffer [ yuv_buffer <struct> ]
23             rgb [ w h * 3 * <byte-array> ] |
24         rgb buffer
25             w >>y_width
26             h >>y_height
27             h >>uv_height
28             w >>y_stride
29             w >>uv_stride
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
33     ] ;
34
35 : clamp ( n -- n )
36     255 min 0 max ; inline
37
38 : stride ( line yuv  -- uvy yy )
39     [ uv_stride>> swap 2/ * ] [ y_stride>> * ] 2bi ; inline
40
41 : compute-y ( yuv uvy yy x -- y )
42     + >fixnum nip swap y>> swap alien-unsigned-1 16 - ; inline
43
44 : compute-v ( yuv uvy yy x -- v )
45     nip 2/ + >fixnum swap u>> swap alien-unsigned-1 128 - ; inline
46
47 : compute-u ( yuv uvy yy x -- v )
48     nip 2/ + >fixnum swap v>> swap alien-unsigned-1 128 - ; inline
49
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
54
55 : compute-blue ( y u v -- b )
56     drop 516 * 128 + swap 298 * + -8 shift clamp ; inline
57
58 : compute-green ( y u v -- g )
59     [ [ 298 * ] dip 100 * - ] dip 208 * - 128 + -8 shift clamp ;
60     inline
61
62 : compute-red ( y u v -- g )
63     nip 409 * swap 298 * + 128 + -8 shift clamp ; inline
64
65 : compute-rgb ( y u v -- b g r )
66     [ compute-blue ] [ compute-green ] [ compute-red ] 3tri ;
67     inline
68
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*
73     drop ; inline
74
75 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
76     compute-yuv compute-rgb store-rgb 3 + ; inline
77
78 : yuv>rgb-row ( index rgb yuv y -- index )
79     over stride
80     pick y_width>>
81     [ yuv>rgb-pixel ] with with with with each ; inline
82
83 : yuv>rgb ( rgb yuv -- )
84     [ 0 ] 2dip
85     dup y_height>>
86     [ yuv>rgb-row ] with with each
87     drop ;
88
89 HINTS: yuv>rgb byte-array yuv_buffer ;
90
91 : yuv>rgb-benchmark ( -- )
92     [ fake-data yuv>rgb ] with-destructors ;
93
94 MAIN: yuv>rgb-benchmark