]> gitweb.factorcode.org Git - factor.git/blob - basis/images/images.factor
Merge branch 'master' of git://factorcode.org/git/factor into llvm
[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
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: 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
48     u-9-9-9-e5-components
49     float-11-11-10-components ;
50
51 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
52
53 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
54
55 TUPLE: image dim component-order component-type upside-down? bitmap ;
56
57 : <image> ( -- image ) image new ; inline
58
59 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
60
61 GENERIC: load-image* ( path class -- image )
62
63 : bytes-per-component ( component-type -- n )
64     {
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 ] }
76     } case ;
77
78 : bytes-per-packed-pixel ( component-type -- n )
79     {
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 ] }
88     } case ;
89
90 : component-count ( component-order -- n )
91     {
92         { A [ 1 ] }
93         { L [ 1 ] }
94         { LA [ 2 ] }
95         { BGR [ 3 ] }
96         { RGB [ 3 ] }
97         { BGRA [ 4 ] }
98         { RGBA [ 4 ] }
99         { ABGR [ 4 ] }
100         { ARGB [ 4 ] }
101         { RGBX [ 4 ] }
102         { XRGB [ 4 ] }
103         { BGRX [ 4 ] }
104         { XBGR [ 4 ] }
105         { INTENSITY [ 1 ] }
106         { DEPTH [ 1 ] }
107         { DEPTH-STENCIL [ 1 ] }
108         { R [ 1 ] }
109         { RG [ 2 ] }
110     } case ;
111
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 *
117     ] if ;
118
119 <PRIVATE
120
121 : pixel@ ( x y image -- start end bitmap )
122     [ dim>> first * + ]
123     [ bytes-per-pixel [ * dup ] keep + ]
124     [ bitmap>> ] tri ;
125
126 : set-subseq ( new-value from to victim -- )
127     <slice> 0 swap copy ; inline
128
129 PRIVATE>
130
131 : pixel-at ( x y image -- pixel )
132     pixel@ subseq ;
133
134 : set-pixel-at ( pixel x y image -- )
135     pixel@ set-subseq ;