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 L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
8 ubyte-components ushort-components
9 half-components float-components
10 byte-integer-components ubyte-integer-components
11 short-integer-components ushort-integer-components
12 int-integer-components uint-integer-components ;
14 UNION: component-order
15 L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR ;
18 ubyte-components ushort-components
19 half-components float-components
20 byte-integer-components ubyte-integer-components
21 short-integer-components ushort-integer-components
22 int-integer-components uint-integer-components ;
24 UNION: unnormalized-integer-components
25 byte-integer-components ubyte-integer-components
26 short-integer-components ushort-integer-components
27 int-integer-components uint-integer-components ;
29 UNION: alpha-channel BGRA RGBA ABGR ARGB ;
31 TUPLE: image dim component-order component-type upside-down? bitmap ;
33 : <image> ( -- image ) image new ; inline
35 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
37 GENERIC: load-image* ( path class -- image )
39 DEFER: bytes-per-pixel
43 : bytes-per-component ( component-type -- n )
45 { ubyte-components [ 1 ] }
46 { ushort-components [ 2 ] }
47 { half-components [ 2 ] }
48 { float-components [ 4 ] }
49 { byte-integer-components [ 1 ] }
50 { ubyte-integer-components [ 1 ] }
51 { short-integer-components [ 2 ] }
52 { ushort-integer-components [ 2 ] }
53 { int-integer-components [ 4 ] }
54 { uint-integer-components [ 4 ] }
57 : component-count ( component-order -- n )
73 : pixel@ ( x y image -- start end bitmap )
75 [ bytes-per-pixel [ * dup ] keep + ]
78 : set-subseq ( new-value from to victim -- )
79 <slice> 0 swap copy ; inline
83 : bytes-per-pixel ( image -- n )
84 [ component-order>> component-count ]
85 [ component-type>> bytes-per-component ] bi * ;
87 : pixel-at ( x y image -- pixel )
90 : set-pixel-at ( pixel x y image -- )