--- /dev/null
+! BSD License. Copyright 2009 Keith Lazuka
+USING: images.normalization images.normalization.private
+sequences tools.test ;
+IN: images.normalization.tests
+
+! RGB
+
+[ B{ 0 1 2 255 3 4 5 255 } ]
+[ B{ 0 1 2 3 4 5 } RGB>RGBA ] unit-test
+
+[ B{ 2 1 0 5 4 3 } ]
+[ B{ 0 1 2 3 4 5 } RGB>BGR ] unit-test
+
+[ B{ 2 1 0 255 5 4 3 255 } ]
+[ B{ 0 1 2 3 4 5 } RGB>BGRA ] unit-test
+
+[ B{ 255 0 1 2 255 3 4 5 } ]
+[ B{ 0 1 2 3 4 5 } RGB>ARGB ] unit-test
+
+! RGBA
+
+[ B{ 0 1 2 4 5 6 } ]
+[ B{ 0 1 2 3 4 5 6 7 } RGBA>RGB ] unit-test
+
+[ B{ 2 1 0 6 5 4 } ]
+[ B{ 0 1 2 3 4 5 6 7 } RGBA>BGR ] unit-test
+
+[ B{ 2 1 0 3 6 5 4 7 } ]
+[ B{ 0 1 2 3 4 5 6 7 } RGBA>BGRA ] unit-test
+
+[ B{ 3 0 1 2 7 4 5 6 } ]
+[ B{ 0 1 2 3 4 5 6 7 } RGBA>ARGB ] unit-test
+
+! BGR
+
+[ B{ 2 1 0 5 4 3 } ]
+[ B{ 0 1 2 3 4 5 } BGR>RGB ] unit-test
+
+[ B{ 2 1 0 255 5 4 3 255 } ]
+[ B{ 0 1 2 3 4 5 } BGR>RGBA ] unit-test
+
+[ B{ 0 1 2 255 3 4 5 255 } ]
+[ B{ 0 1 2 3 4 5 } BGR>BGRA ] unit-test
+
+[ B{ 255 2 1 0 255 5 4 3 } ]
+[ B{ 0 1 2 3 4 5 } BGR>ARGB ] unit-test
+
+! BGRA
+
+[ B{ 2 1 0 6 5 4 } ]
+[ B{ 0 1 2 3 4 5 6 7 } BGRA>RGB ] unit-test
+
+[ B{ 0 1 2 4 5 6 } ]
+[ B{ 0 1 2 3 4 5 6 7 } BGRA>BGR ] unit-test
+
+[ B{ 2 1 0 3 6 5 4 7 } ]
+[ B{ 0 1 2 3 4 5 6 7 } BGRA>RGBA ] unit-test
+
+[ B{ 3 2 1 0 7 6 5 4 } ]
+[ B{ 0 1 2 3 4 5 6 7 } BGRA>ARGB ] unit-test
+
+! ARGB
+
+[ B{ 1 2 3 5 6 7 } ]
+[ B{ 0 1 2 3 4 5 6 7 } ARGB>RGB ] unit-test
+
+[ B{ 3 2 1 7 6 5 } ]
+[ B{ 0 1 2 3 4 5 6 7 } ARGB>BGR ] unit-test
+
+[ B{ 3 2 1 0 7 6 5 4 } ]
+[ B{ 0 1 2 3 4 5 6 7 } ARGB>BGRA ] unit-test
+
+[ B{ 1 2 3 0 5 6 7 4 } ]
+[ B{ 0 1 2 3 4 5 6 7 } ARGB>RGBA ] unit-test
+
+
-! Copyright (C) 2009 Doug Coleman
+! Copyright (C) 2009 Doug Coleman, Keith Lazuka
! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types kernel accessors grouping sequences
combinators math byte-arrays fry images half-floats
<PRIVATE
+! Helpers
: add-dummy-alpha ( seq -- seq' )
3 <groups> [ 255 suffix ] map concat ;
: normalize-floats ( float-array -- byte-array )
[ 255.0 * >integer ] B{ } map-as ;
-GENERIC: normalize-component-type* ( image component-type -- image )
-GENERIC: normalize-component-order* ( image component-order -- image )
+: fix-XBGR ( bitmap -- bitmap' )
+ dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
-: normalize-component-order ( image -- image )
- dup component-type>> '[ _ normalize-component-type* ] change-bitmap
- dup component-order>> '[ _ normalize-component-order* ] change-bitmap ;
+: fix-BGRX ( bitmap -- bitmap' )
+ dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
-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 ;
+! Bitmap Conversions
-: ushorts>ubytes ( bitmap -- bitmap' )
- byte-array>ushort-array [ -8 shift ] B{ } map-as ; inline
+! TODO RGBX, XRGB, BGRX, XBGR conversions
-M: ushort-components normalize-component-type*
- drop ushorts>ubytes ;
+! BGR>
+: BGR>RGB ( bitmap -- bitmap' )
+ 3 <sliced-groups> [ <reversed> ] map concat ; inline
-M: ubyte-components normalize-component-type*
- drop ;
+: BGR>BGRA ( bitmap -- bitmap' ) add-dummy-alpha ; inline
+
+: BGR>RGBA ( bitmap -- bitmap' ) BGR>RGB add-dummy-alpha ; inline
-M: RGBA normalize-component-order* drop ;
+: BGR>ARGB ( bitmap -- bitmap' )
+ 3 <sliced-groups> [ 255 suffix <reversed> ] map concat ; inline
-: BGR>RGB ( bitmap -- pixels )
- 3 <sliced-groups> [ <reversed> ] map B{ } join ; inline
+! BGRA>
+: BGRA>BGR ( bitmap -- bitmap' )
+ 4 <sliced-groups> [ but-last-slice ] map concat ; inline
-: BGRA>RGBA ( bitmap -- pixels )
+: BGRA>RGBA ( bitmap -- bitmap' )
4 <sliced-groups>
[ unclip-last-slice [ <reversed> ] dip suffix ] map concat ; inline
-M: BGRA normalize-component-order*
- drop BGRA>RGBA ;
+: 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
+
+! RGB>
+: 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
+
+! RGBA>
+
+: 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
-M: RGB normalize-component-order*
- drop add-dummy-alpha ;
+! ARGB>
-M: BGR normalize-component-order*
- drop BGR>RGB add-dummy-alpha ;
+: ARGB>RGB ( bitmap -- bitmap' )
+ 4 <sliced-groups> [ rest-slice ] map concat ; inline
: ARGB>RGBA ( bitmap -- bitmap' )
- 4 <groups> [ unclip suffix ] map B{ } join ; inline
+ 4 <sliced-groups> [ unclip-slice suffix ] map concat ; inline
-M: ARGB normalize-component-order*
- drop ARGB>RGBA ;
+: ARGB>BGR ( bitmap -- bitmap' )
+ 4 <sliced-groups> [ rest-slice <reversed> ] map concat ; inline
-M: ABGR normalize-component-order*
- drop ARGB>RGBA BGRA>RGBA ;
+: ARGB>BGRA ( bitmap -- bitmap' )
+ 4 <sliced-groups>
+ [ unclip-slice [ <reversed> ] dip suffix ] map concat ; inline
+
+! Dispatch
+GENERIC# convert-component-order 1 ( image src-order dest-order -- image )
+
+M: RGB convert-component-order
+ nip [ >>component-order ] keep {
+ { RGB [ ] }
+ { RGBA [ [ RGB>RGBA ] change-bitmap ] }
+ { BGRA [ [ BGR>BGRA ] change-bitmap ] }
+ { ARGB [ [ RGB>RGBA RGBA>ARGB ] change-bitmap ] }
+ { BGR [ [ RGB>BGR ] change-bitmap ] }
+ [ "Cannot convert from RGB to desired component order!" throw ]
+ } case ;
+
+M: RGBA convert-component-order
+ nip [ >>component-order ] keep {
+ { RGBA [ ] }
+ { BGRA [ [ RGBA>BGRA ] change-bitmap ] }
+ { BGR [ [ RGBA>BGR ] change-bitmap ] }
+ { RGB [ [ RGBA>RGB ] change-bitmap ] }
+ { ARGB [ [ RGBA>ARGB ] change-bitmap ] }
+ [ "Cannot convert from RGBA to desired component order!" throw ]
+ } case ;
+
+M: BGR convert-component-order
+ nip [ >>component-order ] keep {
+ { BGR [ ] }
+ { BGRA [ [ BGR>BGRA ] change-bitmap ] }
+ { RGB [ [ BGR>RGB ] change-bitmap ] }
+ { RGBA [ [ BGR>RGBA ] change-bitmap ] }
+ { ARGB [ [ BGR>ARGB ] change-bitmap ] }
+ [ "Cannot convert from BGR to desired component order!" throw ]
+ } case ;
+
+M: BGRA convert-component-order
+ nip [ >>component-order ] keep {
+ { BGRA [ ] }
+ { BGR [ [ BGRA>BGR ] change-bitmap ] }
+ { RGB [ [ BGRA>RGB ] change-bitmap ] }
+ { RGBA [ [ BGRA>RGBA ] change-bitmap ] }
+ { ARGB [ [ BGRA>ARGB ] change-bitmap ] }
+ [ "Cannot convert from BGRA to desired component order!" throw ]
+ } case ;
+
+M: ARGB convert-component-order
+ nip [ >>component-order ] keep {
+ { ARGB [ ] }
+ { BGR [ [ ARGB>BGR ] change-bitmap ] }
+ { RGB [ [ ARGB>RGB ] change-bitmap ] }
+ { RGBA [ [ ARGB>RGBA ] change-bitmap ] }
+ { BGRA [ [ ARGB>BGRA ] change-bitmap ] }
+ [ "Cannot convert from ARGB to desired component order!" throw ]
+ } case ;
-: fix-XBGR ( bitmap -- bitmap' )
- dup 4 <sliced-groups> [ [ 255 0 ] dip set-nth ] each ;
+PRIVATE>
-M: XBGR normalize-component-order*
- drop fix-XBGR ABGR normalize-component-order* ;
+! asserts that component-type must be ubyte-components
+: reorder-colors ( image component-order -- image )
+ [
+ [ component-type>> ubyte-components assert= ]
+ [ dup component-order>> ] bi
+ ] dip convert-component-order ;
-: fix-BGRX ( bitmap -- bitmap' )
- dup 4 <sliced-groups> [ [ 255 3 ] dip set-nth ] each ;
+<PRIVATE
-M: BGRX normalize-component-order*
- drop fix-BGRX BGRA normalize-component-order* ;
+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 ;
: normalize-scan-line-order ( image -- image )
dup upside-down?>> [
normalize-component-order
normalize-scan-line-order
RGBA >>component-order ;
+