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
9 ubyte-components ushort-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 ;
15 UNION: component-order
16 A L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
17 INTENSITY DEPTH R RG ;
20 ubyte-components ushort-components
21 half-components float-components
22 byte-integer-components ubyte-integer-components
23 short-integer-components ushort-integer-components
24 int-integer-components uint-integer-components ;
26 UNION: unnormalized-integer-components
27 byte-integer-components ubyte-integer-components
28 short-integer-components ushort-integer-components
29 int-integer-components uint-integer-components ;
31 UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
33 TUPLE: image dim component-order component-type upside-down? bitmap ;
35 : <image> ( -- image ) image new ; inline
37 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
39 GENERIC: load-image* ( path class -- image )
41 DEFER: bytes-per-pixel
45 : bytes-per-component ( component-type -- n )
47 { ubyte-components [ 1 ] }
48 { ushort-components [ 2 ] }
49 { half-components [ 2 ] }
50 { float-components [ 4 ] }
51 { byte-integer-components [ 1 ] }
52 { ubyte-integer-components [ 1 ] }
53 { short-integer-components [ 2 ] }
54 { ushort-integer-components [ 2 ] }
55 { int-integer-components [ 4 ] }
56 { uint-integer-components [ 4 ] }
59 : component-count ( component-order -- n )
80 : pixel@ ( x y image -- start end bitmap )
82 [ bytes-per-pixel [ * dup ] keep + ]
85 : set-subseq ( new-value from to victim -- )
86 <slice> 0 swap copy ; inline
90 : bytes-per-pixel ( image -- n )
91 [ component-order>> component-count ]
92 [ component-type>> bytes-per-component ] bi * ;
94 : pixel-at ( x y image -- pixel )
97 : set-pixel-at ( pixel x y image -- )