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-09-27 16:11:21 -04:00
|
|
|
USING: alien.c-types kernel accessors grouping sequences
|
|
|
|
combinators math byte-arrays fry images half-floats
|
2009-10-06 16:31:49 -04:00
|
|
|
specialized-arrays words ;
|
2009-09-27 16:11:21 -04:00
|
|
|
FROM: alien.c-types => float ;
|
2009-09-09 23:33:34 -04:00
|
|
|
SPECIALIZED-ARRAY: uint
|
|
|
|
SPECIALIZED-ARRAY: ushort
|
|
|
|
SPECIALIZED-ARRAY: float
|
|
|
|
SPECIALIZED-ARRAY: half
|
2009-03-26 22:28:57 -04:00
|
|
|
IN: images.normalization
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
! Helpers
|
2009-03-26 22:28:57 -04:00
|
|
|
: add-dummy-alpha ( seq -- seq' )
|
|
|
|
3 <groups> [ 255 suffix ] map concat ;
|
|
|
|
|
2009-06-22 12:20:54 -04:00
|
|
|
: normalize-floats ( float-array -- byte-array )
|
|
|
|
[ 255.0 * >integer ] B{ } map-as ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: fix-XBGR ( bitmap -- bitmap' )
|
|
|
|
dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: fix-BGRX ( bitmap -- bitmap' )
|
|
|
|
dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
! Bitmap Conversions
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
! TODO RGBX, XRGB, BGRX, XBGR conversions
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: BGR>BGR ( bitmap -- bitmap' ) ;
|
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGR>RGB ( bitmap -- bitmap' )
|
|
|
|
3 <sliced-groups> [ <reversed> ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGR>BGRA ( bitmap -- bitmap' ) add-dummy-alpha ; inline
|
|
|
|
|
|
|
|
: BGR>RGBA ( bitmap -- bitmap' ) BGR>RGB add-dummy-alpha ; inline
|
2009-06-22 12:20:54 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGR>ARGB ( bitmap -- bitmap' )
|
|
|
|
3 <sliced-groups> [ 255 suffix <reversed> ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: BGRA>BGRA ( bitmap -- bitmap' ) ;
|
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGRA>BGR ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ but-last-slice ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGRA>RGBA ( bitmap -- bitmap' )
|
2009-03-26 22:28:57 -04:00
|
|
|
4 <sliced-groups>
|
|
|
|
[ unclip-last-slice [ <reversed> ] dip suffix ] map concat ; inline
|
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: BGRA>RGB ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ but-last-slice <reversed> ] map concat ; inline
|
|
|
|
|
|
|
|
: BGRA>ARGB ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ <reversed> ] map concat ; inline
|
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: RGB>RGB ( bitmap -- bitmap' ) ;
|
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: RGB>BGR ( bitmap -- bitmap' ) BGR>RGB ; inline
|
|
|
|
|
|
|
|
: RGB>RGBA ( bitmap -- bitmap' ) add-dummy-alpha ; inline
|
|
|
|
|
|
|
|
: RGB>BGRA ( bitmap -- bitmap' )
|
|
|
|
3 <sliced-groups> [ <reversed> add-dummy-alpha ] map concat ; inline
|
|
|
|
|
|
|
|
: RGB>ARGB ( bitmap -- bitmap' )
|
|
|
|
3 <sliced-groups> [ 255 prefix ] map concat ; inline
|
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: RGBA>RGBA ( bitmap -- bitmap' ) ;
|
2009-10-06 15:36:58 -04:00
|
|
|
|
|
|
|
: RGBA>BGR ( bitmap -- bitmap' ) BGRA>RGB ; inline
|
|
|
|
|
|
|
|
: RGBA>BGRA ( bitmap -- bitmap' ) BGRA>RGBA ; inline
|
|
|
|
|
|
|
|
: RGBA>RGB ( bitmap -- bitmap' ) BGRA>BGR ; inline
|
|
|
|
|
|
|
|
: RGBA>ARGB ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ unclip-last-slice prefix ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: ARGB>ARGB ( bitmap -- bitmap' ) ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: ARGB>RGB ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ rest-slice ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
|
|
|
: ARGB>RGBA ( bitmap -- bitmap' )
|
2009-10-06 15:36:58 -04:00
|
|
|
4 <sliced-groups> [ unclip-slice suffix ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: ARGB>BGR ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups> [ rest-slice <reversed> ] map concat ; inline
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: ARGB>BGRA ( bitmap -- bitmap' )
|
|
|
|
4 <sliced-groups>
|
|
|
|
[ unclip-slice [ <reversed> ] dip suffix ] map concat ; inline
|
|
|
|
|
2009-10-06 16:31:49 -04:00
|
|
|
: (reorder-colors) ( image src-order des-order -- image )
|
|
|
|
[ name>> ] bi@ ">" glue "images.normalization.private" lookup
|
|
|
|
[ '[ _ execute( image -- image' ) ] change-bitmap ]
|
|
|
|
[ "No component-order conversion found." throw ]
|
|
|
|
if* ;
|
2009-03-26 22:28:57 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
PRIVATE>
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
: reorder-colors ( image component-order -- image )
|
|
|
|
[
|
|
|
|
[ component-type>> ubyte-components assert= ]
|
|
|
|
[ dup component-order>> ] bi
|
2009-10-06 16:31:49 -04:00
|
|
|
] dip (reorder-colors) ;
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
<PRIVATE
|
2009-04-02 14:05:26 -04:00
|
|
|
|
2009-10-06 15:36:58 -04:00
|
|
|
GENERIC: normalize-component-type* ( image component-type -- image )
|
|
|
|
|
|
|
|
: normalize-component-order ( image -- image )
|
|
|
|
dup component-type>> '[ _ normalize-component-type* ] change-bitmap
|
|
|
|
RGBA reorder-colors ;
|
|
|
|
|
|
|
|
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 ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: normalize-image ( image -- image )
|
|
|
|
[ >byte-array ] change-bitmap
|
|
|
|
normalize-component-order
|
|
|
|
normalize-scan-line-order
|
|
|
|
RGBA >>component-order ;
|
2009-10-06 15:36:58 -04:00
|
|
|
|