]> gitweb.factorcode.org Git - factor.git/blob - basis/images/processing/processing.factor
factor: Move math.ranges => ranges.
[factor.git] / basis / images / processing / processing.factor
1 ! Copyright (C) 2009 Marc Fauconneau.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays combinators grouping images
4 kernel locals math math.order
5 ranges math.vectors sequences sequences.deep fry ;
6 IN: images.processing
7
8 : coord-matrix ( dim -- m )
9     [ <iota> ] map first2 cartesian-product ;
10
11 : map^2 ( m quot -- m' ) '[ _ map ] map ; inline
12 : each^2 ( m quot -- m' ) '[ _ each ] each ; inline
13
14 : matrix-dim ( m -- dim ) [ length ] [ first length ] bi 2array ;
15
16 : matrix>image ( m -- image )
17     <image> over matrix-dim >>dim
18     swap flip flatten
19     [ 128 * 128 + 0 255 clamp >fixnum ] map
20     >byte-array >>bitmap L >>component-order ubyte-components >>component-type ;
21
22 :: matrix-zoom ( m f -- m' )
23     m matrix-dim f v*n coord-matrix
24     [ [ f /i ] map first2 swap m nth nth ] map^2 ;
25
26 :: image-offset ( x,y image -- xy )
27     image dim>> first
28     x,y second * x,y first + ;
29
30 :: draw-grey ( value x,y image -- )
31     x,y image image-offset 3 * { 0 1 2 }
32     [
33         + value 128 + >fixnum 0 255 clamp swap image bitmap>> set-nth
34     ] with each ;
35
36 :: draw-color ( value x,y color-id image -- )
37     x,y image image-offset 3 * color-id + value >fixnum
38     swap image bitmap>> set-nth ;
39
40 ! : matrix. ( m -- ) 10 matrix-zoom matrix>image image. ;