1 ! Copyright (C) 2009 Doug Coleman, Keith Lazuka
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data byte-arrays grouping
4 images kernel math math.floats.half sequences specialized-arrays ;
5 FROM: alien.c-types => float ;
6 IN: images.normalization
7 SPECIALIZED-ARRAY: half
8 SPECIALIZED-ARRAY: float
9 SPECIALIZED-ARRAY: ushort
13 CONSTANT: don't-care 127
14 CONSTANT: fill-value 255
16 : permutation ( src dst -- seq )
17 swap '[ _ index [ don't-care ] unless* ] { } map-as
18 4 don't-care pad-tail ;
20 : pad4 ( seq -- newseq ) 4 fill-value pad-tail ;
22 : shuffle ( seq permutation -- newseq )
24 dup 4 >= [ drop fill-value ] [ _ nth ] if
27 :: permute ( bytes width stride src-order dst-order -- new-bytes )
28 src-order name>> :> src
29 dst-order name>> :> dst
32 src length group width head
33 [ pad4 src dst permutation shuffle dst length head ] map concat
36 : stride ( image -- n )
37 [ bitmap>> length ] [ dim>> second ] bi / ;
39 : (reorder-components) ( image src-order dest-order -- image )
40 [ [ ] [ dim>> first ] [ stride ] tri ] 2dip
41 '[ _ _ _ _ permute ] change-bitmap ;
43 GENERIC: normalize-component-type* ( image component-type -- image )
45 : normalize-floats ( float-array -- byte-array )
46 [ 255.0 * >integer ] B{ } map-as ;
48 M: float-components normalize-component-type*
49 drop float cast-array normalize-floats ;
51 M: half-components normalize-component-type*
52 drop half cast-array normalize-floats ;
54 : ushorts>ubytes ( bitmap -- bitmap' )
55 ushort cast-array [ -8 shift ] B{ } map-as ; inline
57 M: ushort-components normalize-component-type*
60 M: ubyte-components normalize-component-type*
63 : normalize-scan-line-order ( image -- image' )
65 dup dim>> first 4 * '[
66 _ <groups> reverse concat
71 : validate-request ( src-order dst-order -- src-order dst-order )
73 [ { DEPTH DEPTH-STENCIL INTENSITY } member? ] bi@
74 or [ "Invalid component-order" throw ] when
79 : reorder-components ( image component-order -- image' )
81 dup component-type>> '[ _ normalize-component-type* ] change-bitmap
84 validate-request [ (reorder-components) ] keep >>component-order ;
86 : normalize-image ( image -- image' )
87 [ >byte-array ] change-bitmap
88 RGBA reorder-components
89 normalize-scan-line-order ;