1 ! Copyright (C) 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel accessors sequences math arrays ;
7 A L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
8 INTENSITY DEPTH DEPTH-STENCIL R RG
9 ubyte-components ushort-components uint-components
10 half-components float-components
11 byte-integer-components ubyte-integer-components
12 short-integer-components ushort-integer-components
13 int-integer-components uint-integer-components
14 u-5-5-5-1-components u-5-6-5-components
15 u-10-10-10-2-components
16 u-24-components u-24-8-components
17 float-32-u-8-components
19 float-11-11-10-components ;
21 UNION: component-order
22 A L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
23 INTENSITY DEPTH DEPTH-STENCIL R RG ;
26 ubyte-components ushort-components uint-components
27 half-components float-components
28 byte-integer-components ubyte-integer-components
29 short-integer-components ushort-integer-components
30 int-integer-components uint-integer-components
31 u-5-5-5-1-components u-5-6-5-components
32 u-10-10-10-2-components
33 u-24-components u-24-8-components
34 float-32-u-8-components
36 float-11-11-10-components ;
38 UNION: unnormalized-integer-components
39 byte-integer-components ubyte-integer-components
40 short-integer-components ushort-integer-components
41 int-integer-components uint-integer-components ;
43 UNION: signed-unnormalized-integer-components
44 byte-integer-components
45 short-integer-components
46 int-integer-components ;
48 UNION: unsigned-unnormalized-integer-components
49 ubyte-integer-components
50 ushort-integer-components
51 uint-integer-components ;
53 UNION: packed-components
54 u-5-5-5-1-components u-5-6-5-components
55 u-10-10-10-2-components
56 u-24-components u-24-8-components
57 float-32-u-8-components
59 float-11-11-10-components ;
61 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
63 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
65 TUPLE: image dim component-order component-type upside-down? bitmap ;
67 : <image> ( -- image ) image new ; inline
69 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
71 : bytes-per-component ( component-type -- n )
73 { ubyte-components [ 1 ] }
74 { ushort-components [ 2 ] }
75 { uint-components [ 4 ] }
76 { half-components [ 2 ] }
77 { float-components [ 4 ] }
78 { byte-integer-components [ 1 ] }
79 { ubyte-integer-components [ 1 ] }
80 { short-integer-components [ 2 ] }
81 { ushort-integer-components [ 2 ] }
82 { int-integer-components [ 4 ] }
83 { uint-integer-components [ 4 ] }
86 : bytes-per-packed-pixel ( component-type -- n )
88 { u-5-5-5-1-components [ 2 ] }
89 { u-5-6-5-components [ 2 ] }
90 { u-10-10-10-2-components [ 4 ] }
91 { u-24-components [ 4 ] }
92 { u-24-8-components [ 4 ] }
93 { u-9-9-9-e5-components [ 4 ] }
94 { float-11-11-10-components [ 4 ] }
95 { float-32-u-8-components [ 8 ] }
98 : component-count ( component-order -- n )
115 { DEPTH-STENCIL [ 1 ] }
120 : (bytes-per-pixel) ( component-order component-type -- n )
121 dup packed-components?
122 [ nip bytes-per-packed-pixel ] [
123 [ component-count ] [ bytes-per-component ] bi* *
126 : bytes-per-pixel ( image -- n )
127 [ component-order>> ] [ component-type>> ] bi (bytes-per-pixel) ;
131 : pixel@ ( x y image -- start end bitmap )
133 [ bytes-per-pixel [ * dup ] keep + ]
136 : set-subseq ( new-value from to victim -- )
137 <slice> 0 swap copy ; inline
141 : pixel-at ( x y image -- pixel )
144 : set-pixel-at ( pixel x y image -- )