view src/laser/rasterize.clj @ 6:4ae2497acf7d

saving progress
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 03:18:05 -0400
parents 8cc418e04b87
children 5e167f275a93
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)
42 (defn preserve-meta [f]
43 (fn [& x] (with-meta
44 (apply f x)
45 (meta (last x)))))
49 (defn frame-hash
50 "yields a convienent representation for the pixles in an image.
51 Because of the size of the structvre generated, this must only be used
52 in a transient way so that java can do it's garbage collection."
53 [#^java.lang.String image-name]
54 (let [image+ (ImagePlus. image-name)]
55 (with-meta
56 (let [buf (.. image+ getBufferedImage)
57 color (.getColorModel buf)]
58 (apply hash-map
59 (interleave
60 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
61 (vector x y)))
62 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
63 (let [data (.getRGB buf x y)]
64 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
65 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
66 :b (bit-and 0x0000ff data))))))))
67 {:width (.getWidth image+) :height (.getHeight image+)})))
75 (def white {:r 255, :g 255, :b 255})
76 (def black {:r 0, :g 0, :b 0})
78 (def expt #(Math/pow %1 %2))
80 (defn rgb-euclidian
81 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]
82 (expt (+ (expt (- r1 r2) 2)
83 (expt (- g1 g2) 2)
84 (expt (- b1 b2) 2)) 0.5))
86 (defn b&w
87 "turn everything strictly black or white"
88 [window]
89 (with-meta
90 (zipmap
91 (keys window)
92 (map (fn [rgb]
93 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))
94 black white))
95 (vals window))) (meta window)))
99 (defn raster-preamble []
100 (str-join \newline
101 ["M63 P0\nG61"
102 (str \F feed)
103 "M101"
104 "M3 S1"]))
106 (defn raster-epilogue []
107 (str-join \newline
108 ["M63 PO"
109 "M5"
110 "M2"]))
113 (defn raster-comment [string]
114 (str "(" (re-gsub #"[()]" "" string) ")"))
116 (defn filter-keys [fun m]
117 (select-keys m (filter fun (keys m))))
119 (def filter-keys (preserve-meta filter-keys))
121 (defn filter-vals [fun m]
122 (into {} (filter (comp fun val) m)))
124 (def filter-vals (preserve-meta filter-vals))
126 (defn frame-hash->bufferedImage
127 [frame-hash]
128 (let [data (meta frame-hash)
129 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
131 (doall (for [element frame-hash]
132 (let [coord (key element)
133 rgb (val element)
134 packed-RGB
135 (+ (bit-shift-left (:r rgb) 16)
136 (bit-shift-left (:g rgb) 8)
137 (:b rgb))]
138 (.setRGB image (first coord) (last coord) packed-RGB))))
139 image))
141 (defmulti display "Creates a JFrame and displays a buffered image" class)
143 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))
145 (defn select-row [x window]
146 (filter-keys (comp (partial = x) first) window))
150 (defmethod display
151 BufferedImage [image]
152 (let [panel (makePanel image)
153 frame (JFrame. "Oh Yeah!")]
154 (.add frame panel)
155 (.pack frame)
156 (.setVisible frame true )
157 (.setSize frame(.getWidth image) (.getHeight image))))
159 (defmethod display
160 ImagePlus [image]
161 (display (.getBufferedImage image)))
163 (defmethod display
164 clojure.lang.PersistentHashMap [frame-hash]
165 (display (frame-hash->bufferedImage frame-hash)))
167 (defmethod display
168 clojure.lang.PersistentArrayMap [frame-hash]
169 (display (frame-hash->bufferedImage frame-hash)))