2009-10-06 15:36:58 -04:00
|
|
|
! Copyright (C) 2009 Doug Coleman, Keith Lazuka
|
2009-03-26 22:28:57 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-10-07 13:53:32 -04:00
|
|
|
USING: accessors alien.c-types byte-arrays combinators fry
|
2009-10-09 15:08:57 -04:00
|
|
|
grouping images kernel locals math math.vectors
|
|
|
|
sequences specialized-arrays half-floats ;
|
2009-09-27 16:11:21 -04:00
|
|
|
FROM: alien.c-types => float ;
|
2009-10-09 15:08:57 -04:00
|
|
|
SPECIALIZED-ARRAY: half
|
|
|
|
SPECIALIZED-ARRAY: float
|
|
|
|
SPECIALIZED-ARRAY: ushort
|
2009-03-26 22:28:57 -04:00
|
|
|
IN: images.normalization
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2009-10-09 10:45:12 -04:00
|
|
|
CONSTANT: don't-care 127
|
|
|
|
CONSTANT: fill-value 255
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-07 15:11:33 -04:00
|
|
|
: permutation ( src dst -- seq )
|
|
|
|
swap '[ _ index [ don't-care ] unless* ] { } map-as
|
|
|
|
4 don't-care pad-tail ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-09 10:45:12 -04:00
|
|
|
: pad4 ( seq -- newseq ) 4 fill-value pad-tail ;
|
|
|
|
|
|
|
|
: shuffle ( seq permutation -- newseq )
|
|
|
|
swap '[
|
|
|
|
dup 4 >= [ drop fill-value ] [ _ nth ] if
|
|
|
|
] B{ } map-as ;
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-07 15:11:33 -04:00
|
|
|
:: permute ( bytes src-order dst-order -- new-bytes )
|
2009-10-27 22:50:31 -04:00
|
|
|
src-order name>> :> src
|
|
|
|
dst-order name>> :> dst
|
|
|
|
bytes src length group
|
|
|
|
[ pad4 src dst permutation shuffle dst length head ]
|
|
|
|
map concat ;
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-09 15:50:57 -04:00
|
|
|
: (reorder-components) ( image src-order dest-order -- image )
|
2009-10-07 13:53:32 -04:00
|
|
|
[ permute ] 2curry change-bitmap ;
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
GENERIC: normalize-component-type* ( image component-type -- image )
|
|
|
|
|
2009-10-07 13:53:32 -04:00
|
|
|
: normalize-floats ( float-array -- byte-array )
|
|
|
|
[ 255.0 * >integer ] B{ } map-as ;
|
2009-10-06 15:36:58 -04:00
|
|
|
|
|
|
|
M: float-components normalize-component-type*
|
|
|
|
drop byte-array>float-array normalize-floats ;
|
|
|
|
|
|
|
|
M: half-components normalize-component-type*
|
|
|
|
drop byte-array>half-array normalize-floats ;
|
|
|
|
|
|
|
|
: ushorts>ubytes ( bitmap -- bitmap' )
|
|
|
|
byte-array>ushort-array [ -8 shift ] B{ } map-as ; inline
|
|
|
|
|
|
|
|
M: ushort-components normalize-component-type*
|
|
|
|
drop ushorts>ubytes ;
|
|
|
|
|
|
|
|
M: ubyte-components normalize-component-type*
|
|
|
|
drop ;
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-03-26 22:28:57 -04:00
|
|
|
: normalize-scan-line-order ( image -- image )
|
|
|
|
dup upside-down?>> [
|
|
|
|
dup dim>> first 4 * '[
|
|
|
|
_ <groups> reverse concat
|
|
|
|
] change-bitmap
|
|
|
|
f >>upside-down?
|
|
|
|
] when ;
|
|
|
|
|
2009-10-09 15:50:57 -04:00
|
|
|
: validate-request ( src-order dst-order -- src-order dst-order )
|
|
|
|
[
|
|
|
|
[ { DEPTH DEPTH-STENCIL INTENSITY } member? ] bi@
|
|
|
|
or [ "Invalid component-order" throw ] when
|
|
|
|
] 2keep ;
|
|
|
|
|
2009-03-26 22:28:57 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-10-09 15:50:57 -04:00
|
|
|
: reorder-components ( image component-order -- image )
|
2009-10-07 13:53:32 -04:00
|
|
|
[
|
|
|
|
dup component-type>> '[ _ normalize-component-type* ] change-bitmap
|
|
|
|
dup component-order>>
|
|
|
|
] dip
|
2009-10-09 15:50:57 -04:00
|
|
|
validate-request [ (reorder-components) ] keep >>component-order ;
|
2009-10-07 13:53:32 -04:00
|
|
|
|
2009-03-26 22:28:57 -04:00
|
|
|
: normalize-image ( image -- image )
|
|
|
|
[ >byte-array ] change-bitmap
|
2009-10-09 15:50:57 -04:00
|
|
|
RGBA reorder-components
|
2009-10-07 13:53:32 -04:00
|
|
|
normalize-scan-line-order ;
|
2009-10-06 15:36:58 -04:00
|
|
|
|