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
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: packed-components
44 u-5-5-5-1-components u-5-6-5-components
45 u-10-10-10-2-components
46 u-24-components u-24-8-components
47 float-32-u-8-components
49 float-11-11-10-components ;
51 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
53 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
55 TUPLE: image dim component-order component-type upside-down? bitmap ;
57 : <image> ( -- image ) image new ; inline
59 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
61 GENERIC: load-image* ( path class -- image )
63 : bytes-per-component ( component-type -- n )
65 { ubyte-components [ 1 ] }
66 { ushort-components [ 2 ] }
67 { uint-components [ 4 ] }
68 { half-components [ 2 ] }
69 { float-components [ 4 ] }
70 { byte-integer-components [ 1 ] }
71 { ubyte-integer-components [ 1 ] }
72 { short-integer-components [ 2 ] }
73 { ushort-integer-components [ 2 ] }
74 { int-integer-components [ 4 ] }
75 { uint-integer-components [ 4 ] }
78 : bytes-per-packed-pixel ( component-type -- n )
80 { u-5-5-5-1-components [ 2 ] }
81 { u-5-6-5-components [ 2 ] }
82 { u-10-10-10-2-components [ 4 ] }
83 { u-24-components [ 4 ] }
84 { u-24-8-components [ 4 ] }
85 { u-9-9-9-e5-components [ 4 ] }
86 { float-11-11-10-components [ 4 ] }
87 { float-32-u-8-components [ 8 ] }
90 : component-count ( component-order -- n )
107 { DEPTH-STENCIL [ 1 ] }
112 : bytes-per-pixel ( image -- n )
113 dup component-type>> packed-components?
114 [ component-type>> bytes-per-packed-pixel ] [
115 [ component-order>> component-count ]
116 [ component-type>> bytes-per-component ] bi *
121 : pixel@ ( x y image -- start end bitmap )
123 [ bytes-per-pixel [ * dup ] keep + ]
126 : set-subseq ( new-value from to victim -- )
127 <slice> 0 swap copy ; inline
131 : pixel-at ( x y image -- pixel )
134 : set-pixel-at ( pixel x y image -- )