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 math.ranges math.vectors sequences sequences.deep fry ;
8 : coord-matrix ( dim -- m )
9 [ iota ] map first2 cartesian-product ;
11 : map^2 ( m quot -- m' ) '[ _ map ] map ; inline
12 : each^2 ( m quot -- m' ) '[ _ each ] each ; inline
14 : matrix-dim ( m -- dim ) [ length ] [ first length ] bi 2array ;
16 : matrix>image ( m -- image )
17 <image> over matrix-dim >>dim
19 [ 128 * 128 + 0 255 clamp >fixnum ] map
20 >byte-array >>bitmap L >>component-order ubyte-components >>component-type ;
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 ;
26 :: image-offset ( x,y image -- xy )
28 x,y second * x,y first + ;
30 :: draw-grey ( value x,y image -- )
31 x,y image image-offset 3 * { 0 1 2 }
33 + value 128 + >fixnum 0 255 clamp swap image bitmap>> set-nth
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 ;
40 ! : matrix. ( m -- ) 10 matrix-zoom matrix>image image. ;