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 fry
5 specialized-arrays.direct.ushort specialized-arrays.uint
6 specialized-arrays.ushort specialized-arrays.float images
8 IN: images.normalization
12 : add-dummy-alpha ( seq -- seq' )
13 3 <groups> [ 255 suffix ] map concat ;
15 : normalize-floats ( float-array -- byte-array )
16 [ 255.0 * >integer ] B{ } map-as ;
18 GENERIC: normalize-component-type* ( image component-type -- image )
19 GENERIC: normalize-component-order* ( image component-order -- image )
21 : normalize-component-order ( image -- image )
22 dup component-type>> '[ _ normalize-component-type* ] change-bitmap
23 dup component-order>> '[ _ normalize-component-order* ] change-bitmap ;
25 M: float-components normalize-component-type*
26 drop byte-array>float-array normalize-floats ;
27 M: half-components normalize-component-type*
28 drop byte-array>half-array normalize-floats ;
30 : ushorts>ubytes ( bitmap -- bitmap' )
31 byte-array>ushort-array [ -8 shift ] B{ } map-as ; inline
33 M: ushort-components normalize-component-type*
36 M: ubyte-components normalize-component-type*
39 M: RGBA normalize-component-order* drop ;
41 : BGR>RGB ( bitmap -- pixels )
42 3 <sliced-groups> [ <reversed> ] map B{ } join ; inline
44 : BGRA>RGBA ( bitmap -- pixels )
46 [ unclip-last-slice [ <reversed> ] dip suffix ] map concat ; inline
48 M: BGRA normalize-component-order*
51 M: RGB normalize-component-order*
52 drop add-dummy-alpha ;
54 M: BGR normalize-component-order*
55 drop BGR>RGB add-dummy-alpha ;
57 : ARGB>RGBA ( bitmap -- bitmap' )
58 4 <groups> [ unclip suffix ] map B{ } join ; inline
60 M: ARGB normalize-component-order*
63 M: ABGR normalize-component-order*
64 drop ARGB>RGBA BGRA>RGBA ;
66 : fix-XBGR ( bitmap -- bitmap' )
67 dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
69 M: XBGR normalize-component-order*
70 drop fix-XBGR ABGR normalize-component-order* ;
72 : fix-BGRX ( bitmap -- bitmap' )
73 dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
75 M: BGRX normalize-component-order*
76 drop fix-BGRX BGRA normalize-component-order* ;
78 : normalize-scan-line-order ( image -- image )
80 dup dim>> first 4 * '[
81 _ <groups> reverse concat
88 : normalize-image ( image -- image )
89 [ >byte-array ] change-bitmap
90 normalize-component-order
91 normalize-scan-line-order
92 RGBA >>component-order ;