view src/laser/rasterize.clj @ 5:8cc418e04b87

made preserve-meta
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 01:47:28 -0400
parents 6533b4ef83ad
children 4ae2497acf7d
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))))
118 (def filter-keys (preserve-meta filter-keys))
120 (defn filter-vals [fun m]
121 (into {} (filter (comp fun val) m)))
122 (def filter-vals (preserve-meta filter-vals))
124 (defn frame-hash->bufferedImage
125 [frame-hash]
126 (let [data (meta frame-hash)
127 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
129 (doall (for [element frame-hash]
130 (let [coord (key element)
131 rgb (val element)
132 packed-RGB
133 (+ (bit-shift-left (:r rgb) 16)
134 (bit-shift-left (:g rgb) 8)
135 (:b rgb))]
136 (.setRGB image (first coord) (last coord) packed-RGB))))
137 image))
139 (defmulti display "Creates a JFrame and displays a buffered image" class)
141 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))
144 (defmethod display
145 BufferedImage [image]
146 (let [panel (makePanel image)
147 frame (JFrame. "Oh Yeah!")]
148 (.add frame panel)
149 (.pack frame)
150 (.setVisible frame true )
151 (.setSize frame(.getWidth image) (.getHeight image))))
153 (defmethod display
154 ImagePlus [image]
155 (display (.getBufferedImage image)))
157 (defmethod display
158 clojure.lang.PersistentHashMap [frame-hash]
159 (display (frame-hash->bufferedImage frame-hash)))
161 (defmethod display
162 clojure.lang.PersistentArrayMap [frame-hash]
163 (display (frame-hash->bufferedImage frame-hash)))