]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/yuv-to-rgb/yuv-to-rgb.factor
classes.struct: moving to new/boa instead of <struct>/<struct-boa>
[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 kernel libc locals math math.order
5 sequences sequences.private classes.struct accessors alien.data
6 typed ;
7 IN: benchmark.yuv-to-rgb
8
9 STRUCT: yuv-buffer
10     { y_width int }
11     { y_height int }
12     { y_stride int }
13     { uv_width int }
14     { uv_height int }
15     { uv_stride int }
16     { y void* }
17     { u void* }
18     { v void* } ;
19
20 :: fake-data ( -- rgb yuv )
21     1600 :> w
22     1200 :> h
23     yuv-buffer new :> buffer
24     w h * 3 * <byte-array> :> rgb
25     rgb buffer
26         w >>y_width
27         h >>y_height
28         h >>uv_height
29         w >>y_stride
30         w >>uv_stride
31         w h * <iota> [ dup * ] B{ } map-as malloc-byte-array &free >>y
32         w h * 2/ <iota> [ dup dup * * ] B{ } map-as malloc-byte-array &free >>u
33         w h * 2/ <iota> [ dup * dup * ] B{ } map-as malloc-byte-array &free >>v ;
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 ; inline
60
61 : compute-red ( y u v -- g )
62     nip 409 * swap 298 * + 128 + -8 shift clamp ; inline
63
64 : compute-rgb ( y u v -- b g r )
65     [ compute-blue ] [ compute-green ] [ compute-red ] 3tri ; inline
66
67 : store-rgb ( index rgb b g r -- index )
68     [ pick 0 + pick set-nth-unsafe ]
69     [ pick 1 + pick set-nth-unsafe ]
70     [ pick 2 + pick set-nth-unsafe ] tri*
71     drop ; inline
72
73 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
74     compute-yuv compute-rgb store-rgb 3 + ; inline
75
76 : yuv>rgb-row ( index rgb yuv y -- index )
77     over stride
78     pick y_width>> <iota>
79     [ yuv>rgb-pixel ] 4 nwith each ; inline
80
81 TYPED: yuv>rgb ( rgb: byte-array yuv: yuv-buffer -- )
82     [ 0 ] 2dip
83     dup y_height>> <iota>
84     [ yuv>rgb-row ] 2with each
85     drop ;
86
87 : yuv-to-rgb-benchmark ( -- )
88     [ fake-data yuv>rgb ] with-destructors ;
89
90 MAIN: yuv-to-rgb-benchmark