]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/yuv-to-rgb/yuv-to-rgb.factor
update existing code for [let change
[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     1600 :> w
21     1200 :> h
22     yuv_buffer <struct> :> buffer
23     w h * 3 * <byte-array> :> rgb
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 : clamp ( n -- n )
35     255 min 0 max ; inline
36
37 : stride ( line yuv  -- uvy yy )
38     [ uv_stride>> swap 2/ * ] [ y_stride>> * ] 2bi ; inline
39
40 : compute-y ( yuv uvy yy x -- y )
41     + >fixnum nip swap y>> swap alien-unsigned-1 16 - ; inline
42
43 : compute-v ( yuv uvy yy x -- v )
44     nip 2/ + >fixnum swap u>> swap alien-unsigned-1 128 - ; inline
45
46 : compute-u ( yuv uvy yy x -- v )
47     nip 2/ + >fixnum swap v>> swap alien-unsigned-1 128 - ; inline
48
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
53
54 : compute-blue ( y u v -- b )
55     drop 516 * 128 + swap 298 * + -8 shift clamp ; inline
56
57 : compute-green ( y u v -- g )
58     [ [ 298 * ] dip 100 * - ] dip 208 * - 128 + -8 shift clamp ;
59     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 ;
66     inline
67
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*
72     drop ; inline
73
74 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
75     compute-yuv compute-rgb store-rgb 3 + ; inline
76
77 : yuv>rgb-row ( index rgb yuv y -- index )
78     over stride
79     pick y_width>>
80     [ yuv>rgb-pixel ] with with with with each ; inline
81
82 : yuv>rgb ( rgb yuv -- )
83     [ 0 ] 2dip
84     dup y_height>>
85     [ yuv>rgb-row ] with with each
86     drop ;
87
88 HINTS: yuv>rgb byte-array yuv_buffer ;
89
90 : yuv>rgb-benchmark ( -- )
91     [ fake-data yuv>rgb ] with-destructors ;
92
93 MAIN: yuv>rgb-benchmark