]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/yuv-to-rgb/yuv-to-rgb.factor
Switch to https urls
[factor.git] / extra / benchmark / yuv-to-rgb / yuv-to-rgb.factor
1 ! Copyright (C) Chris Double.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.accessors alien.c-types alien.data
4 byte-arrays classes.struct destructors generalizations kernel
5 libc math math.order sequences sequences.private typed ;
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 * <iota> [ dup * ] B{ } map-as malloc-byte-array &free >>y
31         w h * 2/ <iota> [ dup dup * * ] B{ } map-as malloc-byte-array &free >>u
32         w h * 2/ <iota> [ 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 ; inline
59
60 : compute-red ( y u v -- g )
61     nip 409 * swap 298 * + 128 + -8 shift clamp ; inline
62
63 : compute-rgb ( y u v -- b g r )
64     [ compute-blue ] [ compute-green ] [ compute-red ] 3tri ; inline
65
66 : store-rgb ( index rgb b g r -- index )
67     [ pick 0 + pick set-nth-unsafe ]
68     [ pick 1 + pick set-nth-unsafe ]
69     [ pick 2 + pick set-nth-unsafe ] tri*
70     drop ; inline
71
72 : yuv>rgb-pixel ( index rgb yuv uvy yy x -- index )
73     compute-yuv compute-rgb store-rgb 3 + ; inline
74
75 : yuv>rgb-row ( index rgb yuv y -- index )
76     over stride
77     pick y_width>> <iota>
78     [ yuv>rgb-pixel ] 4 nwith each ; inline
79
80 TYPED: yuv>rgb ( rgb: byte-array yuv: yuv-buffer -- )
81     [ 0 ] 2dip
82     dup y_height>> <iota>
83     [ yuv>rgb-row ] 2with each
84     drop ;
85
86 : yuv-to-rgb-benchmark ( -- )
87     [ fake-data yuv>rgb ] with-destructors ;
88
89 MAIN: yuv-to-rgb-benchmark