factor/basis/images/images.factor

67 lines
1.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2009-02-13 10:56:22 -05:00
USING: kernel accessors grouping sequences combinators
2009-02-13 16:56:22 -05:00
math specialized-arrays.direct.uint byte-arrays
2009-02-14 01:30:59 -05:00
specialized-arrays.direct.ushort specialized-arrays.uint
specialized-arrays.ushort specialized-arrays.float ;
IN: images
2009-02-13 10:56:22 -05:00
SINGLETONS: BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
2009-02-14 01:30:59 -05:00
R16G16B16 R32G32B32 R16G16B16A16 R32G32B32A32 ;
2009-02-10 19:42:21 -05:00
TUPLE: image dim component-order bitmap ;
: <image> ( -- image ) image new ; inline
2009-02-10 19:42:21 -05:00
GENERIC: load-image* ( path tuple -- image )
2009-02-10 19:42:21 -05:00
: add-dummy-alpha ( seq -- seq' )
3 <sliced-groups>
[ 255 suffix ] map concat ;
2009-02-14 01:30:59 -05:00
: normalize-floats ( byte-array -- byte-array )
byte-array>float-array [ 255.0 * >integer ] B{ } map-as ;
: normalize-component-order ( image -- image )
dup component-order>>
{
{ RGBA [ ] }
2009-02-14 01:30:59 -05:00
{ R32G32B32A32 [
[ normalize-floats ] change-bitmap
] }
{ R32G32B32 [
2009-02-14 01:30:59 -05:00
[ normalize-floats add-dummy-alpha ] change-bitmap
] }
{ R16G16B16A16 [
[ byte-array>ushort-array [ -8 shift ] B{ } map-as ] change-bitmap
] }
2009-02-13 16:56:22 -05:00
{ R16G16B16 [
[
2009-02-14 01:30:59 -05:00
byte-array>ushort-array [ -8 shift ] B{ } map-as add-dummy-alpha
2009-02-13 16:56:22 -05:00
] change-bitmap
] }
{ BGRA [
[
2009-02-14 01:30:59 -05:00
4 <sliced-groups> dup [ 3 head-slice reverse-here ] each
] change-bitmap
] }
{ RGB [ [ add-dummy-alpha ] change-bitmap ] }
{ BGR [
[
3 <sliced-groups>
2009-02-14 01:30:59 -05:00
[ [ 3 head-slice reverse-here ] each ]
[ add-dummy-alpha ] bi
] change-bitmap
] }
} case
RGBA >>component-order ;
GENERIC: normalize-scan-line-order ( image -- image )
M: image normalize-scan-line-order ;
: normalize-image ( image -- image )
[ >byte-array ] change-bitmap
normalize-component-order
normalize-scan-line-order ;