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
18 float-11-11-10-components ;
20 UNION: component-order
21 A L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
22 INTENSITY DEPTH DEPTH-STENCIL R RG ;
25 ubyte-components ushort-components
26 half-components float-components
27 byte-integer-components ubyte-integer-components
28 short-integer-components ushort-integer-components
29 int-integer-components uint-integer-components
30 u-5-5-5-1-components u-5-6-5-components
31 u-10-10-10-2-components
32 u-24-components u-24-8-components
34 float-11-11-10-components ;
36 UNION: unnormalized-integer-components
37 byte-integer-components ubyte-integer-components
38 short-integer-components ushort-integer-components
39 int-integer-components uint-integer-components ;
41 UNION: packed-components
42 u-5-5-5-1-components u-5-6-5-components
43 u-10-10-10-2-components
44 u-24-components u-24-8-components
46 float-11-11-10-components ;
48 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
50 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
52 TUPLE: image dim component-order component-type upside-down? bitmap ;
54 : <image> ( -- image ) image new ; inline
56 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
58 GENERIC: load-image* ( path class -- image )
60 : bytes-per-component ( component-type -- n )
62 { ubyte-components [ 1 ] }
63 { ushort-components [ 2 ] }
64 { uint-components [ 4 ] }
65 { half-components [ 2 ] }
66 { float-components [ 4 ] }
67 { byte-integer-components [ 1 ] }
68 { ubyte-integer-components [ 1 ] }
69 { short-integer-components [ 2 ] }
70 { ushort-integer-components [ 2 ] }
71 { int-integer-components [ 4 ] }
72 { uint-integer-components [ 4 ] }
75 : bytes-per-packed-pixel ( component-type -- n )
77 { u-5-5-5-1-components [ 2 ] }
78 { u-5-6-5-components [ 2 ] }
79 { u-10-10-10-2-components [ 4 ] }
80 { u-24-components [ 4 ] }
81 { u-24-8-components [ 4 ] }
82 { u-9-9-9-e5-components [ 4 ] }
83 { float-11-11-10-components [ 4 ] }
86 : component-count ( component-order -- n )
103 { DEPTH-STENCIL [ 1 ] }
108 : bytes-per-pixel ( image -- n )
109 dup component-type>> packed-components?
110 [ component-type>> bytes-per-packed-pixel ] [
111 [ component-order>> component-count ]
112 [ component-type>> bytes-per-component ] bi *
117 : pixel@ ( x y image -- start end bitmap )
119 [ bytes-per-pixel [ * dup ] keep + ]
122 : set-subseq ( new-value from to victim -- )
123 <slice> 0 swap copy ; inline
127 : pixel-at ( x y image -- pixel )
130 : set-pixel-at ( pixel x y image -- )