1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors grouping sequences combinators
4 math specialized-arrays.direct.uint byte-arrays
5 specialized-arrays.direct.ushort specialized-arrays.uint
6 specialized-arrays.ushort specialized-arrays.float ;
9 SINGLETONS: BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
10 R16G16B16 R32G32B32 R16G16B16A16 R32G32B32A32 ;
12 : bytes-per-pixel ( component-order -- n )
26 { R16G16B16A16 [ 8 ] }
27 { R32G32B32A32 [ 16 ] }
30 TUPLE: image dim component-order bitmap ;
32 : <image> ( -- image ) image new ; inline
34 GENERIC: load-image* ( path tuple -- image )
36 : add-dummy-alpha ( seq -- seq' )
38 [ 255 suffix ] map concat ;
40 : normalize-floats ( byte-array -- byte-array )
41 byte-array>float-array [ 255.0 * >integer ] B{ } map-as ;
43 : normalize-component-order ( image -- image )
48 [ normalize-floats ] change-bitmap
51 [ normalize-floats add-dummy-alpha ] change-bitmap
54 [ byte-array>ushort-array [ -8 shift ] B{ } map-as ] change-bitmap
58 byte-array>ushort-array [ -8 shift ] B{ } map-as add-dummy-alpha
63 4 <sliced-groups> dup [ 3 head-slice reverse-here ] each
66 { RGB [ [ add-dummy-alpha ] change-bitmap ] }
70 [ [ 3 head-slice reverse-here ] each ]
71 [ [ 255 suffix ] map ] bi concat
75 RGBA >>component-order ;
77 GENERIC: normalize-scan-line-order ( image -- image )
79 M: image normalize-scan-line-order ;
81 : normalize-image ( image -- image )
82 [ >byte-array ] change-bitmap
83 normalize-component-order
84 normalize-scan-line-order ;