]> gitweb.factorcode.org Git - factor.git/blob - extra/images/normalization/normalization.factor
90341fed9262655105551045e552f7403e2e59a6
[factor.git] / extra / images / normalization / normalization.factor
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
7
8 <PRIVATE
9
10 : add-dummy-alpha ( seq -- seq' )
11     3 <groups> [ 255 suffix ] map concat ;
12
13 : normalize-floats ( float-array -- byte-array )
14     [ 255.0 * >integer ] B{ } map-as ;
15
16 GENERIC: normalize-component-type* ( image component-type -- image )
17 GENERIC: normalize-component-order* ( image component-order -- image )
18
19 : normalize-component-order ( image -- image )
20     dup component-type>> '[ _ normalize-component-type* ] change-bitmap
21     dup component-order>> '[ _ normalize-component-order* ] change-bitmap ;
22
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 ;
27
28 : ushorts>ubytes ( bitmap -- bitmap' )
29     byte-array>ushort-array [ -8 shift ] B{ } map-as ; inline
30
31 M: ushort-components normalize-component-type*
32     drop ushorts>ubytes ;
33
34 M: ubyte-components normalize-component-type*
35     drop ;
36
37 M: RGBA normalize-component-order* drop ;
38
39 : BGR>RGB ( bitmap -- pixels )
40     3 <sliced-groups> [ <reversed> ] map B{ } join ; inline
41
42 : BGRA>RGBA ( bitmap -- pixels )
43     4 <sliced-groups>
44     [ unclip-last-slice [ <reversed> ] dip suffix ] map concat ; inline
45
46 M: BGRA normalize-component-order*
47     drop BGRA>RGBA ;
48
49 M: RGB normalize-component-order*
50     drop add-dummy-alpha ;
51
52 M: BGR normalize-component-order*
53     drop BGR>RGB add-dummy-alpha ;
54
55 : ARGB>RGBA ( bitmap -- bitmap' )
56     4 <groups> [ unclip suffix ] map B{ } join ; inline
57
58 M: ARGB normalize-component-order*
59     drop ARGB>RGBA ;
60
61 M: ABGR normalize-component-order*
62     drop ARGB>RGBA BGRA>RGBA ;
63
64 : fix-XBGR ( bitmap -- bitmap' )
65     dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
66
67 M: XBGR normalize-component-order*
68     drop fix-XBGR ABGR normalize-component-order* ;
69
70 : fix-BGRX ( bitmap -- bitmap' )
71     dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
72
73 M: BGRX normalize-component-order*
74     drop fix-BGRX BGRA normalize-component-order* ;
75
76 : normalize-scan-line-order ( image -- image )
77     dup upside-down?>> [
78         dup dim>> first 4 * '[
79             _ <groups> reverse concat
80         ] change-bitmap
81         f >>upside-down?
82     ] when ;
83
84 PRIVATE>
85
86 : normalize-image ( image -- image )
87     [ >byte-array ] change-bitmap
88     normalize-component-order
89     normalize-scan-line-order
90     RGBA >>component-order ;