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
8 : bits>pixels ( bits -- bits' pixels )
9 [ -1 shift ] [ 1 bitand ] bi 255 * ; inline
11 : ?generate-more-bits ( a bits -- a bits' )
12 over 32 mod zero? [ drop random-32 ] when ; inline
14 : <random-images-bytes> ( dim -- bytes )
17 [ 1 + ] [ bits>pixels ] bi*
18 ] B{ } replicate-as 2nip ;
20 : <random-bw-image> ( -- image )
22 { 320 240 } [ >>dim ] [ <random-images-bytes> >>bitmap ] bi
24 ubyte-components >>component-type ;
26 TUPLE: bw-noise-gadget < image-control timers cnt old-cnt ;
28 : animate-image ( control -- )
30 model>> <random-bw-image> swap set-model ;
32 : update-cnt ( gadget -- )
33 [ cnt>> ] [ old-cnt<< ] bi ;
35 : fps ( gadget -- fps )
36 [ cnt>> ] [ old-cnt>> ] bi -
37 number>string "FPS: " prepend ;
39 : fps-monitor ( gadget -- )
40 [ fps ] [ update-cnt ] [ show-status ] tri ;
42 : start-animation ( gadget -- )
43 [ [ animate-image ] curry 1 nanoseconds every ] [ timers>> push ] bi ;
45 : start-fps ( gadget -- )
46 [ [ fps-monitor ] curry 1 seconds every ] [ timers>> push ] bi ;
48 : setup-timers ( gadget -- )
49 [ start-animation ] [ start-fps ] bi ;
51 : stop-animation ( gadget -- )
52 timers>> [ [ stop-timer ] each ] [ delete-all ] bi ;
54 M: bw-noise-gadget graft* [ call-next-method ] [ setup-timers ] bi ;
56 M: bw-noise-gadget ungraft* [ stop-animation ] [ call-next-method ] bi ;
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 ;
62 : open-noise-window ( -- )
63 [ <bw-noise-gadget> "Black and White noise" open-status-window ] with-ui ;
65 MAIN: open-noise-window