]> gitweb.factorcode.org Git - factor.git/blob - extra/rosetta-code/image-noise/image-noise.factor
factor: trim using lists
[factor.git] / extra / rosetta-code / image-noise / image-noise.factor
1 ! Copyright (C) 2012 Anonymous.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar images images.viewer kernel math
4 math.parser models random sequences timers ui ui.gadgets
5 ui.gadgets.status-bar ui.gadgets.worlds ;
6 IN: rosetta-code.image-noise
7
8 : bits>pixels ( bits -- bits' pixels )
9     [ -1 shift ] [ 1 bitand ] bi 255 * ; inline
10
11 : ?generate-more-bits ( a bits -- a bits' )
12     over 32 mod zero? [ drop random-32 ] when ; inline
13
14 : <random-images-bytes> ( dim -- bytes )
15     [ 0 0 ] dip product  [
16         ?generate-more-bits
17         [ 1 + ] [ bits>pixels ] bi*
18     ] B{ } replicate-as 2nip ;
19
20 : <random-bw-image> ( -- image )
21     <image>
22         { 320 240 } [ >>dim ] [ <random-images-bytes> >>bitmap ] bi
23         L >>component-order
24         ubyte-components >>component-type ;
25
26 TUPLE: bw-noise-gadget < image-control timers cnt old-cnt ;
27
28 : animate-image ( control -- )
29     [ 1 + ] change-cnt
30     model>> <random-bw-image> swap set-model ;
31
32 : update-cnt ( gadget -- )
33     [ cnt>> ] [ old-cnt<< ] bi ;
34
35 : fps ( gadget -- fps )
36     [ cnt>> ] [ old-cnt>> ] bi -
37     number>string "FPS: " prepend ;
38
39 : fps-monitor ( gadget -- )
40     [ fps ] [ update-cnt ] [ show-status ] tri ;
41
42 : start-animation ( gadget -- )
43     [ [ animate-image ] curry 1 nanoseconds every ] [ timers>> push ] bi ;
44
45 : start-fps ( gadget -- )
46     [ [ fps-monitor ] curry 1 seconds every ] [ timers>> push ] bi ;
47
48 : setup-timers ( gadget -- )
49     [ start-animation ] [ start-fps ] bi ;
50
51 : stop-animation ( gadget -- )
52     timers>> [ [ stop-timer ] each ] [ delete-all ] bi ;
53
54 M: bw-noise-gadget graft* [ call-next-method ] [ setup-timers ] bi ;
55
56 M: bw-noise-gadget ungraft* [ stop-animation ] [ call-next-method ] bi ;
57
58 : <bw-noise-gadget> ( -- gadget )
59     <random-bw-image> <model> bw-noise-gadget new-image-gadget*
60     0 >>cnt 0 >>old-cnt V{ } clone >>timers ;
61
62 : open-noise-window ( -- )
63     [ <bw-noise-gadget> "Black and White noise" open-status-window ] with-ui ;
64
65 MAIN: open-noise-window