1 ! Copyright (C) 2009 Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors grouping sequences combinators math
4 byte-arrays fry specialized-arrays.uint specialized-arrays.ushort
5 specialized-arrays.float images half-floats ;
6 IN: images.normalization
10 : add-dummy-alpha ( seq -- seq' )
11 3 <groups> [ 255 suffix ] map concat ;
13 : normalize-floats ( float-array -- byte-array )
14 [ 255.0 * >integer ] B{ } map-as ;
16 GENERIC: normalize-component-type* ( image component-type -- image )
17 GENERIC: normalize-component-order* ( image component-order -- image )
19 : normalize-component-order ( image -- image )
20 dup component-type>> '[ _ normalize-component-type* ] change-bitmap
21 dup component-order>> '[ _ normalize-component-order* ] change-bitmap ;
23 M: float-components normalize-component-type*
24 drop byte-array>float-array normalize-floats ;
25 M: half-components normalize-component-type*
26 drop byte-array>half-array normalize-floats ;
28 : ushorts>ubytes ( bitmap -- bitmap' )
29 byte-array>ushort-array [ -8 shift ] B{ } map-as ; inline
31 M: ushort-components normalize-component-type*
34 M: ubyte-components normalize-component-type*
37 M: RGBA normalize-component-order* drop ;
39 : BGR>RGB ( bitmap -- pixels )
40 3 <sliced-groups> [ <reversed> ] map B{ } join ; inline
42 : BGRA>RGBA ( bitmap -- pixels )
44 [ unclip-last-slice [ <reversed> ] dip suffix ] map concat ; inline
46 M: BGRA normalize-component-order*
49 M: RGB normalize-component-order*
50 drop add-dummy-alpha ;
52 M: BGR normalize-component-order*
53 drop BGR>RGB add-dummy-alpha ;
55 : ARGB>RGBA ( bitmap -- bitmap' )
56 4 <groups> [ unclip suffix ] map B{ } join ; inline
58 M: ARGB normalize-component-order*
61 M: ABGR normalize-component-order*
62 drop ARGB>RGBA BGRA>RGBA ;
64 : fix-XBGR ( bitmap -- bitmap' )
65 dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
67 M: XBGR normalize-component-order*
68 drop fix-XBGR ABGR normalize-component-order* ;
70 : fix-BGRX ( bitmap -- bitmap' )
71 dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
73 M: BGRX normalize-component-order*
74 drop fix-BGRX BGRA normalize-component-order* ;
76 : normalize-scan-line-order ( image -- image )
78 dup dim>> first 4 * '[
79 _ <groups> reverse concat
86 : normalize-image ( image -- image )
87 [ >byte-array ] change-bitmap
88 normalize-component-order
89 normalize-scan-line-order
90 RGBA >>component-order ;