view src/laser/rasterize.clj @ 4:6533b4ef83ad

added hash-map filters :)
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 01:30:12 -0400
parents e6254010c95a
children 8cc418e04b87
line wrap: on
line source
1 (ns laser.rasterize)
3 (import '(java.io File))
4 (import '(org.apache.commons.io FileUtils))
5 (import '(javax.imageio ImageIO) )
6 (import '(javax.swing JFrame))
7 (import '(java.awt Color BorderLayout))
8 (import '(ij ImagePlus IJ))
9 (import '(java.lang Math))
10 (import '(java.awt Graphics2D Panel))
11 (import '(ij Macro))
13 (import '(java.io BufferedReader InputStreamReader))
14 (import '(java.awt.image BufferedImage))
16 (use 'clojure.contrib.str-utils)
18 (use 'clojure.contrib.combinatorics)
21 (use 'clojure.contrib.repl-utils)
23 (set! *print-length* 20)
26 (def img "/home/r/graster/test.png")
29 (def feed 120)
30 (def dpi [500, 500])
31 (def on_range [0.0, 0.5])
32 (def overshoot 0.5)
33 (def offset [1.0, 1.0])
34 (def tiles [1, 1])
35 (def tile_size [false, false])
36 (def tile_spacing [0.125, 0.125])
37 (def feed 120)
38 (def cut_feed 20)
39 (def corner_radius 0)
41 (defmulti display "Creates a JFrame and displays a buffered image" class)
43 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))
45 (defmethod display
46 BufferedImage [image]
47 (let [panel (makePanel image)
48 frame (JFrame. "Oh Yeah!")]
49 (.add frame panel)
50 (.pack frame)
51 (.setVisible frame true )
52 (.setSize frame(.getWidth image) (.getHeight image))))
54 (defmethod display
55 ImagePlus [image]
56 (display (.getBufferedImage image)))
58 (defmethod display
59 clojure.lang.PersistentHashMap [frame-hash]
60 (display (frame-hash->bufferedImage frame-hash)))
62 (defmethod display
63 clojure.lang.PersistentArrayMap [frame-hash]
64 (display (frame-hash->bufferedImage frame-hash)))
67 (defn frame-hash
68 "yields a convienent representation for the pixles in an image.
69 Because of the size of the structvre generated, this must only be used
70 in a transient way so that java can do it's garbage collection."
71 [#^java.lang.String image-name]
72 (let [image+ (ImagePlus. image-name)]
73 (with-meta
74 (let [buf (.. image+ getBufferedImage)
75 color (.getColorModel buf)]
76 (apply hash-map
77 (interleave
78 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
79 (vector x y)))
80 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
81 (let [data (.getRGB buf x y)]
82 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
83 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
84 :b (bit-and 0x0000ff data))))))))
85 {:width (.getWidth image+) :height (.getHeight image+)})))
88 (def white {:r 255, :g 255, :b 255})
89 (def black {:r 0, :g 0, :b 0})
91 (def expt #(Math/pow %1 %2))
93 (defn rgb-euclidian
94 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]
95 (expt (+ (expt (- r1 r2) 2)
96 (expt (- g1 g2) 2)
97 (expt (- b1 b2) 2)) 0.5))
99 (defn b&w
100 "turn everything strictly black or white"
101 [window]
102 (with-meta
103 (zipmap
104 (keys window)
105 (map (fn [rgb]
106 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))
107 black white))
108 (vals window))) (meta window)))
112 (defn raster-preamble []
113 (str-join \newline
114 ["M63 P0\nG61"
115 (str \F feed)
116 "M101"
117 "M3 S1"]))
119 (defn raster-epilogue []
120 (str-join \newline
121 ["M63 PO"
122 "M5"
123 "M2"]))
126 (defn raster-comment [string]
127 (str "(" (re-gsub #"[()]" "" string) ")"))
129 (defn filter-keys [fun m]
130 (select-keys m (filter fun (keys m))))
132 (defn filter-vals [fun m]
133 (into {} (filter (comp fun val) m)))
144 (defn frame-hash->bufferedImage
145 [frame-hash]
146 (let [data (meta frame-hash)
147 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
149 (doall (for [element frame-hash]
150 (let [coord (key element)
151 rgb (val element)
152 packed-RGB
153 (+ (bit-shift-left (:r rgb) 16)
154 (bit-shift-left (:g rgb) 8)
155 (:b rgb))]
156 (.setRGB image (first coord) (last coord) packed-RGB))))
157 image))