]> gitweb.factorcode.org Git - factor.git/blob - basis/images/images.factor
Merge branch 'master' of git://github.com/abeaumont/factor
[factor.git] / basis / images / images.factor
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 ;
4 IN: images
5
6 SINGLETONS:
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
18     u-9-9-9-e5-components
19     float-11-11-10-components ;
20
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 ;
24
25 UNION: component-type
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
35     u-9-9-9-e5-components
36     float-11-11-10-components ;
37
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 ;
42
43 UNION: signed-unnormalized-integer-components
44     byte-integer-components 
45     short-integer-components 
46     int-integer-components ;
47
48 UNION: unsigned-unnormalized-integer-components
49     ubyte-integer-components
50     ushort-integer-components
51     uint-integer-components ;
52
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
58     u-9-9-9-e5-components
59     float-11-11-10-components ;
60
61 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
62
63 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
64
65 TUPLE: image dim component-order component-type upside-down? bitmap ;
66
67 : <image> ( -- image ) image new ; inline
68
69 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
70
71 GENERIC: load-image* ( path class -- image )
72
73 : bytes-per-component ( component-type -- n )
74     {
75         { ubyte-components [ 1 ] }
76         { ushort-components [ 2 ] }
77         { uint-components [ 4 ] }
78         { half-components [ 2 ] }
79         { float-components [ 4 ] }
80         { byte-integer-components [ 1 ] }
81         { ubyte-integer-components [ 1 ] }
82         { short-integer-components [ 2 ] }
83         { ushort-integer-components [ 2 ] }
84         { int-integer-components [ 4 ] }
85         { uint-integer-components [ 4 ] }
86     } case ;
87
88 : bytes-per-packed-pixel ( component-type -- n )
89     {
90         { u-5-5-5-1-components [ 2 ] }
91         { u-5-6-5-components [ 2 ] }
92         { u-10-10-10-2-components [ 4 ] }
93         { u-24-components [ 4 ] }
94         { u-24-8-components [ 4 ] }
95         { u-9-9-9-e5-components [ 4 ] }
96         { float-11-11-10-components [ 4 ] }
97         { float-32-u-8-components [ 8 ] }
98     } case ;
99
100 : component-count ( component-order -- n )
101     {
102         { A [ 1 ] }
103         { L [ 1 ] }
104         { LA [ 2 ] }
105         { BGR [ 3 ] }
106         { RGB [ 3 ] }
107         { BGRA [ 4 ] }
108         { RGBA [ 4 ] }
109         { ABGR [ 4 ] }
110         { ARGB [ 4 ] }
111         { RGBX [ 4 ] }
112         { XRGB [ 4 ] }
113         { BGRX [ 4 ] }
114         { XBGR [ 4 ] }
115         { INTENSITY [ 1 ] }
116         { DEPTH [ 1 ] }
117         { DEPTH-STENCIL [ 1 ] }
118         { R [ 1 ] }
119         { RG [ 2 ] }
120     } case ;
121
122 : (bytes-per-pixel) ( component-order component-type -- n )
123     dup packed-components?
124     [ nip bytes-per-packed-pixel ] [
125         [ component-count ] [ bytes-per-component ] bi* *
126     ] if ;
127
128 : bytes-per-pixel ( image -- n )
129     [ component-order>> ] [ component-type>> ] bi (bytes-per-pixel) ;
130
131 <PRIVATE
132
133 : pixel@ ( x y image -- start end bitmap )
134     [ dim>> first * + ]
135     [ bytes-per-pixel [ * dup ] keep + ]
136     [ bitmap>> ] tri ;
137
138 : set-subseq ( new-value from to victim -- )
139     <slice> 0 swap copy ; inline
140
141 PRIVATE>
142
143 : pixel-at ( x y image -- pixel )
144     pixel@ subseq ;
145
146 : set-pixel-at ( pixel x y image -- )
147     pixel@ set-subseq ;