check in kobie's image rotation code

db4
Doug Coleman 2009-05-26 21:05:46 -05:00
parent 38a8e80ba9
commit 1d721a32c1
8 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Kobie Lurie
Doug Coleman

View File

@ -0,0 +1,77 @@
! Copyright (C) 2009 Kobie Lurie, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry images.loader images.normalization
images.processing.rotation kernel literals math sequences
tools.test images.processing.rotation.private ;
IN: images.processing.rotation.tests
: first-row ( seq^2 -- seq ) first ;
: first-col ( seq^2 -- item ) harvest [ first ] map ;
: last-row ( seq^2 -- item ) last ;
: last-col ( seq^2 -- item ) harvest [ last ] map ;
: end-of-first-row ( seq^2 -- item ) first-row last ;
: first-of-first-row ( seq^2 -- item ) first-row first ;
: end-of-last-row ( seq^2 -- item ) last-row last ;
: first-of-last-row ( seq^2 -- item ) last-row first ;
<<
: clone-image ( image -- new-image )
clone [ clone ] change-bitmap ;
>>
CONSTANT: pasted-image
$[
"vocab:images/processing/rotation/test-bitmaps/PastedImage.bmp"
load-image normalize-image clone-image
]
CONSTANT: pasted-image90
$[
"vocab:images/processing/rotation/test-bitmaps/PastedImage90.bmp"
load-image normalize-image clone-image
]
CONSTANT: lake-image
$[
"vocab:images/processing/rotation/test-bitmaps/lake.bmp"
load-image preprocess
]
[ t ] [ pasted-image dup clone-image 4 [ 90 rotate ] times = ] unit-test
[ t ] [ pasted-image dup clone-image 2 [ 180 rotate ] times = ] unit-test
[ t ] [ pasted-image dup clone-image 270 rotate 90 rotate = ] unit-test
[ t ] [
pasted-image dup clone-image dup { 90 180 90 } [ rotate drop ] with each =
] unit-test
[ t ] [
pasted-image 90 rotate
pasted-image90 =
] unit-test
[ t ] [
"vocab:images/processing/rotation/test-bitmaps/small.bmp"
load-image 90 rotate
"vocab:images/processing/rotation/test-bitmaps/small-rotated.bmp"
load-image normalize-image =
] unit-test
[ t ] [
lake-image
[ first-of-first-row ]
[ 90 (rotate) end-of-first-row ] bi =
] unit-test
[ t ]
[ lake-image [ first-row ] [ 90 (rotate) last-col ] bi = ] unit-test
[ t ]
[ lake-image [ last-col ] [ 90 (rotate) last-row reverse ] bi = ] unit-test
[ t ]
[ lake-image [ last-row ] [ 90 (rotate) first-col ] bi = ] unit-test
[ t ]
[ lake-image [ first-col ] [ 90 (rotate) first-row reverse ] bi = ] unit-test

View File

@ -0,0 +1,71 @@
! Copyright (C) 2009 Kobie Lurie.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays colors combinators
combinators.short-circuit fry grouping images images.bitmap
images.loader images.normalization kernel locals math sequences ;
IN: images.processing.rotation
ERROR: unsupported-rotation degrees ;
<PRIVATE
: rotate-90 ( seq^3 -- seq^3 ) flip [ reverse ] map ;
: rotate-180 ( seq^3 -- seq^3 ) reverse [ reverse ] map ;
: rotate-270 ( seq^3 -- seq^3 ) flip reverse ;
: (rotate) ( seq n -- seq' )
{
{ 0 [ ] }
{ 90 [ rotate-90 ] }
{ 180 [ rotate-180 ] }
{ 270 [ rotate-270 ] }
[ unsupported-rotation ]
} case ;
: rows-remove-pad ( byte-rows -- pixels' )
[ dup length 4 mod head* ] map ;
: row-length ( image -- n )
[ bitmap>> length ] [ dim>> second ] bi /i ;
: image>byte-rows ( image -- byte-rows )
[ bitmap>> ] [ row-length ] bi group rows-remove-pad ;
: (seperate-to-pixels) ( byte-rows image -- pixel-rows )
component-order>> bytes-per-pixel '[ _ group ] map ;
: image>pixel-rows ( image -- pixel-rows )
[ image>byte-rows ] keep (seperate-to-pixels) ;
: flatten-table ( seq^3 -- seq )
[ concat ] map concat ;
: preprocess ( image -- pixelrows )
normalize-image image>pixel-rows ;
: ?reverse-dimensions ( image n -- )
{ 270 90 } member? [ [ reverse ] change-dim ] when drop ;
: normalize-degree ( n -- n' ) 360 rem ;
: processing-effect ( image quot -- image' )
'[ preprocess @ flatten-table ] [ (>>bitmap) ] [ ] tri ; inline
:: rotate' ( image n -- image )
n normalize-degree :> n'
image preprocess :> pixel-table
image n' ?reverse-dimensions
pixel-table n' (rotate) :> table-rotated
image table-rotated flatten-table >>bitmap ;
PRIVATE>
: rotate ( image n -- image' )
normalize-degree
[ '[ _ (rotate) ] processing-effect ] [ ?reverse-dimensions ] 2bi ;
: reflect-y-axis ( image -- image )
[ [ reverse ] map ] processing-effect ;
: reflect-x-axis ( image -- image )
[ reverse ] processing-effect ;

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 B