]> gitweb.factorcode.org Git - factor.git/blob - extra/images/processing/rotation/rotation.factor
Switch to https urls
[factor.git] / extra / images / processing / rotation / rotation.factor
1 ! Copyright (C) 2009 Kobi Lurie.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators grouping images kernel math
4 sequences ;
5 IN: images.processing.rotation
6
7 ERROR: unsupported-rotation degrees ;
8
9 <PRIVATE
10
11 : rotate-90 ( seq^3 -- seq^3 ) flip [ reverse ] map ;
12 : rotate-180 ( seq^3 -- seq^3 ) reverse [ reverse ] map ;
13 : rotate-270 ( seq^3 -- seq^3 ) flip reverse ;
14
15 : (rotate) ( seq n -- seq' )
16     {
17         { 0 [ ] }
18         { 90 [ rotate-90 ] }
19         { 180 [ rotate-180 ] }
20         { 270 [ rotate-270 ] }
21         [ unsupported-rotation ]
22     } case ;
23
24 : rows-remove-pad ( byte-rows -- pixels' )
25     [ dup length 4 mod head* ] map ; 
26
27 : row-length ( image -- n ) 
28     [ bitmap>> length ] [ dim>> second ] bi /i ;
29
30 : image>byte-rows ( image -- byte-rows )
31     [ bitmap>> ] [ row-length ] bi group rows-remove-pad ;
32
33 : (separate-to-pixels) ( byte-rows image -- pixel-rows )
34     bytes-per-pixel '[ _ group ] map ;
35
36 : image>pixel-rows ( image -- pixel-rows )
37     [ image>byte-rows ] keep (separate-to-pixels) ;
38  
39 : flatten-table ( seq^3 -- seq )
40     [ concat ] map concat ;
41
42 : ?reverse-dimensions ( image n -- )
43     { 270 90 } member? [ [ reverse ] change-dim ] when drop ;
44
45 :  normalize-degree ( n -- n' ) 360 rem ;
46
47 : processing-effect ( image quot -- image' )
48     '[ image>pixel-rows @ flatten-table ] [ bitmap<< ] [ ] tri ; inline
49
50 :: rotate' ( image n -- image )
51     n normalize-degree :> n'
52     image image>pixel-rows :> pixel-table
53     image n' ?reverse-dimensions
54     pixel-table n' (rotate) :> table-rotated
55     image table-rotated flatten-table >>bitmap ;
56
57 PRIVATE>
58
59 : rotate ( image n -- image' )
60     normalize-degree
61     [ '[ _ (rotate) ] processing-effect ] [ ?reverse-dimensions ] 2bi ;
62
63 : reflect-y-axis ( image -- image ) 
64     [ [ reverse ] map ] processing-effect ;
65
66 : reflect-x-axis ( image -- image ) 
67     [ reverse ] processing-effect ;