rlm@15
|
1 (ns laser.rasterize
|
rlm@15
|
2 (:use [rlm
|
rlm@15
|
3 image-utils
|
rlm@15
|
4 map-utils]
|
rlm@15
|
5 [clojure.contrib
|
rlm@19
|
6 [str-utils :only [str-join re-gsub re-split]]
|
rlm@15
|
7 [seq :only [indexed]]
|
rlm@19
|
8 [math]])
|
rlm@15
|
9 (:import [ij ImagePlus IJ]))
|
rlm@0
|
10
|
rlm@15
|
11 ;(import '(java.io File))
|
rlm@15
|
12 ;(import '(org.apache.commons.io FileUtils))
|
rlm@15
|
13 ;(import '(javax.imageio ImageIO) )
|
rlm@0
|
14
|
rlm@0
|
15
|
rlm@15
|
16 (set! *print-length* 20)
|
rlm@1
|
17 (def feed 120)
|
rlm@1
|
18 (def dpi [500, 500])
|
rlm@19
|
19 (def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1})
|
rlm@8
|
20
|
rlm@15
|
21 ;;; this process is divided into two tasks,
|
rlm@15
|
22 ;;; creating the raster g-code, which sweeps back and forth
|
rlm@15
|
23 ;;; and creating the gmask, which turns the laser on and off.
|
rlm@1
|
24
|
rlm@1
|
25
|
rlm@15
|
26 ;;; we'll be using frame-hashes, which represent picutres as
|
rlm@15
|
27 ;;; a 3D vector field over 2D space, with the vectors representing
|
rlm@19
|
28 ;;; the rgb values at that particulat point.
|
rlm@2
|
29
|
rlm@15
|
30 (defn select-row
|
rlm@15
|
31 "returns a frame hash that is just a single line at the chosen y"
|
rlm@15
|
32 [y window]
|
rlm@19
|
33 (filter-keys (comp (partial = y) last) window))
|
rlm@1
|
34
|
rlm@15
|
35 (defn make-rows [pic]
|
rlm@15
|
36 (map (partial sort #(< (first %1) (first %2)))
|
rlm@15
|
37 (partition-by last
|
rlm@15
|
38 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
|
rlm@15
|
39 (map first (filter-vals (partial = black) pic))))))
|
rlm@1
|
40
|
rlm@19
|
41
|
rlm@15
|
42 ;;; generate rastering g-code
|
rlm@1
|
43
|
rlm@3
|
44 (defn raster-preamble []
|
rlm@19
|
45 (str-join \newline
|
rlm@19
|
46 ["M63 P0\nG61"
|
rlm@19
|
47 (str "F" feed)
|
rlm@19
|
48 "M101"
|
rlm@19
|
49 "M3 S1\n"]))
|
rlm@3
|
50
|
rlm@4
|
51 (defn raster-epilogue []
|
rlm@19
|
52 (str-join \newline
|
rlm@19
|
53 ["M63 P0"
|
rlm@19
|
54 "M5"
|
rlm@19
|
55 "M2\n"]))
|
rlm@3
|
56
|
rlm@19
|
57 (defn raster-comment [string]
|
rlm@4
|
58 (str "(" (re-gsub #"[()]" "" string) ")"))
|
rlm@1
|
59
|
rlm@19
|
60
|
rlm@19
|
61 ;this is a sequence of rows
|
rlm@19
|
62
|
rlm@19
|
63 ;(defn span [row]
|
rlm@19
|
64 ; (let [sorted-row (sort #(< (first %1) (first %2)) row)]
|
rlm@19
|
65 ; (vector (first sorted-row) (last sorted-row))))
|
rlm@19
|
66
|
rlm@19
|
67
|
rlm@20
|
68 (comment
|
rlm@19
|
69
|
rlm@20
|
70 (import 'org.im4java.core.ConvertCmd)
|
rlm@20
|
71 (import 'org.im4java.core.IMOperation)
|
rlm@20
|
72 (import 'org.im4java.core.Stream2BufferedImage)
|
rlm@20
|
73
|
rlm@20
|
74 (def target (.getBufferedImage (ImagePlus. sing)))
|
rlm@20
|
75 (def shiv (IMOperation.))
|
rlm@20
|
76 (.addImage shiv)
|
rlm@20
|
77 (.addImage shiv (into-array String ["png:-"]))
|
rlm@20
|
78 (def conv-shiv (ConvertCmd.))
|
rlm@20
|
79 (def s2b (Stream2BufferedImage.))
|
rlm@20
|
80 (.setOutputConsumer conv-shiv s2b)
|
rlm@20
|
81 (.run conv-shiv shiv (into-array Object [target]))
|
rlm@20
|
82 (def result (.getImage s2b ))
|
rlm@20
|
83
|
rlm@20
|
84 )
|
rlm@20
|
85
|
rlm@20
|
86 (defn mirror [#^ImagePlus img]
|
rlm@20
|
87 (let [title (.getTitle img)
|
rlm@20
|
88 target (.getBufferedImage img)
|
rlm@20
|
89 s2b (Stream2BufferedImage.)
|
rlm@20
|
90 shiv (doto (IMOperation.)
|
rlm@20
|
91 ;(.blur 2.0)
|
rlm@20
|
92 (.flip)
|
rlm@20
|
93 (.addImage)
|
rlm@20
|
94 (.addImage (into-array String ["png:-"])))
|
rlm@20
|
95 conv-shiv (doto (ConvertCmd.)
|
rlm@20
|
96 (.setOutputConsumer s2b))]
|
rlm@20
|
97
|
rlm@20
|
98 (.run conv-shiv shiv (into-array Object [target]))
|
rlm@20
|
99 (ImagePlus. title (.getImage s2b))))
|
rlm@20
|
100
|
rlm@20
|
101
|
rlm@20
|
102
|
rlm@19
|
103 (defn row->gcode [{:keys [x-dpi y-dpi margin x-offset y-offset]} forward? row]
|
rlm@16
|
104
|
rlm@19
|
105 (let [[x1 y1] (if forward? (last row) (first row))
|
rlm@19
|
106 [x2 y2] (if forward? (first row) (last row))]
|
rlm@5
|
107
|
rlm@19
|
108 (let [x1 (* x1 (/ x-dpi))
|
rlm@19
|
109 x2 (* x2 (/ x-dpi))
|
rlm@19
|
110 y1 (* y1 (/ y-dpi))
|
rlm@20
|
111 y2 (* y2 (/ y-dpi))]
|
rlm@7
|
112
|
rlm@19
|
113 (let [x1 (+ x1 x-offset)
|
rlm@19
|
114 x2 (+ x2 x-offset)
|
rlm@19
|
115 y1 (+ y1 y-offset)
|
rlm@19
|
116 y2 (+ y2 y-offset)]
|
rlm@20
|
117
|
rlm@20
|
118
|
rlm@19
|
119
|
rlm@16
|
120 (str (format "G0 X%.3f Y%.3f\n"
|
rlm@19
|
121 (float x1 )
|
rlm@19
|
122 (float y1))
|
rlm@16
|
123
|
rlm@16
|
124 (format "G1 X%.3f Y%.3f\n"
|
rlm@19
|
125 (float x2)
|
rlm@19
|
126 (float y2)))))))
|
rlm@14
|
127
|
rlm@15
|
128 (defn generate-gcode [pic]
|
rlm@15
|
129 (str (raster-preamble)
|
rlm@19
|
130 (str-join ""
|
rlm@19
|
131 (map
|
rlm@19
|
132 (fn [[index row]]
|
rlm@19
|
133 (row->gcode paramaters (even? index) row))
|
rlm@19
|
134 (indexed (make-rows pic))))
|
rlm@19
|
135 (raster-epilogue)))
|
rlm@15
|
136
|
rlm@15
|
137
|
rlm@15
|
138
|
rlm@15
|
139
|
rlm@15
|
140
|
rlm@15
|
141
|
rlm@15
|
142
|
rlm@15
|
143
|
rlm@15
|
144
|
rlm@15
|
145
|
rlm@15
|
146
|
rlm@15
|
147
|
rlm@15
|
148
|
rlm@15
|
149
|
rlm@15
|
150
|
rlm@15
|
151
|
rlm@15
|
152
|
rlm@15
|
153
|
rlm@15
|
154
|
rlm@7
|
155 (defn gather-row [row]
|
rlm@7
|
156 (let [base [[(first (first row)) (first (first row))]]]
|
rlm@7
|
157 ; (println base)
|
rlm@7
|
158 (reduce
|
rlm@7
|
159 (fn colapse [collection new-n]
|
rlm@7
|
160
|
rlm@7
|
161 (let [collection (apply vector collection)
|
rlm@7
|
162 prevoius (last (last collection))
|
rlm@7
|
163 range-start (first (last collection))]
|
rlm@7
|
164 ; (println new-n)
|
rlm@7
|
165 ; (println prevoius)
|
rlm@7
|
166 ; (println range-start)
|
rlm@7
|
167 (if (<= new-n (+ prevoius 1))
|
rlm@11
|
168 (do ;(println "join")
|
rlm@7
|
169 ;(println (butlast collection))
|
rlm@11
|
170 (conj (apply vector (butlast collection))
|
rlm@11
|
171 (vector range-start new-n)))
|
rlm@11
|
172 (conj collection (vector new-n new-n)))))
|
rlm@7
|
173
|
rlm@7
|
174 base
|
rlm@11
|
175 (map first row))))
|
rlm@11
|
176
|
rlm@7
|
177
|
rlm@7
|
178
|
rlm@7
|
179
|
rlm@7
|
180 (defn row->gmask [[x-dpi y-dpi] forward? row]
|
rlm@11
|
181 (let [start (float (* (/ x-dpi) (first (first
|
rlm@11
|
182 (if forward?
|
rlm@11
|
183 (reverse row) row)))))]
|
rlm@11
|
184 (let [preamble (if-not forward?
|
rlm@7
|
185 (format "0 0 0 %.3f\n" start)
|
rlm@11
|
186 (format "0 0 1 %.3f\n" start))
|
rlm@7
|
187 body
|
rlm@11
|
188 (for [[x y]
|
rlm@11
|
189 (if forward?
|
rlm@11
|
190 (reverse (gather-row row))
|
rlm@11
|
191 (gather-row row))]
|
rlm@7
|
192 (let [x (float (* x (/ x-dpi)))
|
rlm@15
|
193 y (float (* y (/ x-dpi)))]
|
rlm@14
|
194 ;; x (+ x 0.159)];; shift by a small margin.
|
rlm@11
|
195 (if-not forward?
|
rlm@7
|
196 (str (format "0 0 1 %.3f\n" x)
|
rlm@7
|
197 (format "0 1 1 %.3f\n" y))
|
rlm@7
|
198
|
rlm@11
|
199 (str (format "0 0 0 %.3f\n" y)
|
rlm@11
|
200 (format "0 1 0 %.3f\n" x)))))]
|
rlm@7
|
201
|
rlm@7
|
202 (str preamble (str-join "" body)))))
|
rlm@7
|
203
|
rlm@7
|
204
|
rlm@7
|
205 (defn generate-gmask [pic]
|
rlm@7
|
206 (str "1 0 0 0\n"
|
rlm@8
|
207 (str-join "" (map (fn [[index row]]
|
rlm@11
|
208 (row->gmask dpi (even? index) row))
|
rlm@8
|
209 (indexed (make-rows pic))))))
|
rlm@8
|
210
|
rlm@11
|
211
|
rlm@11
|
212
|
rlm@8
|
213
|
rlm@8
|
214 ;;;; testing
|
rlm@8
|
215
|
rlm@15
|
216 (defn generate-files [pic]
|
rlm@15
|
217 (println "made-image")
|
rlm@15
|
218 (spit "/home/r/kevin/out.ngc" (generate-gcode pic))
|
rlm@15
|
219 (println "/home/r/kevin/out.ngc")
|
rlm@15
|
220 (spit "/home/r/kevin/out.gmask" (generate-gmask pic))
|
rlm@15
|
221 (println "/home/r/kevin/out.gmask")
|
rlm@15
|
222 pic)
|
rlm@8
|
223
|
rlm@15
|
224 (defn update-state []
|
rlm@15
|
225 (def sing "/home/r/kevin/sing.png")
|
rlm@20
|
226 (def pic (frame-hash (mirror (ImagePlus. sing))))
|
rlm@19
|
227 (def pic (b&w pic)))
|
rlm@8
|
228
|
rlm@19
|
229 (defn compare-gen-fn [n f cmp]
|
rlm@15
|
230 (let [theirs (re-split #"\n" (slurp cmp))
|
rlm@15
|
231 ours (re-split #"\n" (f pic))]
|
rlm@15
|
232 (println (format "%1$-25s%2$s" "OURS" "THEIRS"))
|
rlm@15
|
233 (println "_______________________________________")
|
rlm@15
|
234 (dorun (map (fn [[us them]] (println
|
rlm@15
|
235 (format "%1$-25s%2$s" us them)))
|
rlm@19
|
236 (take n (partition 2 (interleave ours theirs)))))))
|
rlm@8
|
237
|
rlm@19
|
238 (defn compare-gcode [n]
|
rlm@19
|
239 (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))
|
rlm@19
|
240 (defn compare-gmask [n]
|
rlm@19
|
241 (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask"))
|
rlm@15
|
242
|
rlm@8
|
243
|
rlm@8
|
244
|
rlm@8
|
245
|
rlm@8
|
246
|