comparison src/laser/rasterize.clj @ 11:f952052e37b7

trying a fix.
author Robert McIntyre <rlm@mit.edu>
date Tue, 24 Aug 2010 19:06:45 -0400
parents 0f48db8d2a05
children 397ab24b4952
comparison
equal deleted inserted replaced
10:ef7dbbd6452c 11:f952052e37b7
11 (import '(ij Macro)) 11 (import '(ij Macro))
12 12
13 (import '(java.io BufferedReader InputStreamReader)) 13 (import '(java.io BufferedReader InputStreamReader))
14 (import '(java.awt.image BufferedImage)) 14 (import '(java.awt.image BufferedImage))
15 15
16 (use 'clojure.contrib.str-utils) 16 ;(use 'clojure.contrib.str-utils)
17 (use 'clojure.contrib.seq-utils) 17 ;(use 'clojure.contrib.seq-utils)
18 (use 'clojure.contrib.combinatorics) 18 ;(use 'clojure.contrib.combinatorics)
19 (use 'clojure.contrib.duck-streams) 19 ;(use 'clojure.contrib.duck-streams)
20 20
21 (use 'clojure.contrib.repl-utils) 21 ;(use 'clojure.contrib.repl-utils)
22 22
23 (set! *print-length* 20) 23 ;(set! *print-length* 20)
24 24
25 25
26 26
27 27
28 (def feed 120) 28 (def feed 120)
75 75
76 76
77 (def white {:r 255, :g 255, :b 255}) 77 (def white {:r 255, :g 255, :b 255})
78 (def black {:r 0, :g 0, :b 0}) 78 (def black {:r 0, :g 0, :b 0})
79 79
80 (def expt #(Math/pow %1 %2)) 80
81 81
82 (defn rgb-euclidian 82 (defn rgb-euclidian
83 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ] 83 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]
84 (expt (+ (expt (- r1 r2) 2) 84 (expt (+ (expt (- r1 r2) 2)
85 (expt (- g1 g2) 2) 85 (expt (- g1 g2) 2)
203 range-start (first (last collection))] 203 range-start (first (last collection))]
204 ; (println new-n) 204 ; (println new-n)
205 ; (println prevoius) 205 ; (println prevoius)
206 ; (println range-start) 206 ; (println range-start)
207 (if (<= new-n (+ prevoius 1)) 207 (if (<= new-n (+ prevoius 1))
208 (do ;(println "join") 208 (do ;(println "join")
209 ;(println (butlast collection)) 209 ;(println (butlast collection))
210 (conj (apply vector (butlast collection)) (vector range-start new-n))) 210 (conj (apply vector (butlast collection))
211 (conj collection (vector new-n new-n))))) 211 (vector range-start new-n)))
212 (conj collection (vector new-n new-n)))))
212 213
213 base 214 base
214 (map first row)))) 215 (map first row))))
216
215 217
216 218
217 219
218 (defn row->gmask [[x-dpi y-dpi] forward? row] 220 (defn row->gmask [[x-dpi y-dpi] forward? row]
219 221 ; (println forward?)
220 (let [start (float (* (/ x-dpi) (first (first row))))] 222 (let [start (float (* (/ x-dpi) (first (first
221 223 (if forward?
222 (let [preamble (if forward? 224 (reverse row) row)))))]
223 (str 225
226 (let [preamble (if-not forward?
224 (format "0 0 0 %.3f\n" start) 227 (format "0 0 0 %.3f\n" start)
225 (format "0 0 1 %.3f\n" start))) 228 (format "0 0 1 %.3f\n" start))
226 body 229 body
227 (for [[x y] (gather-row row)] 230 (for [[x y]
231 (if forward?
232 (reverse (gather-row row))
233 (gather-row row))]
228 (let [x (float (* x (/ x-dpi))) 234 (let [x (float (* x (/ x-dpi)))
229 y (float (* y (/ x-dpi)))] 235 y (float (* y (/ x-dpi)))]
230 (if forward? 236 (if-not forward?
231 (str (format "0 0 1 %.3f\n" x) 237 (str (format "0 0 1 %.3f\n" x)
232 (format "0 1 1 %.3f\n" y)) 238 (format "0 1 1 %.3f\n" y))
233 239
234 (str (format "0 0 0 %.3f\n" x) 240 (str (format "0 0 0 %.3f\n" y)
235 (format "0 1 0 %.3f\n" y)))))] 241 (format "0 1 0 %.3f\n" x)))))]
236 242
237 (str preamble (str-join "" body))))) 243 (str preamble (str-join "" body)))))
238 244
239 245
240 246
249 255
250 (defn generate-gmask [pic] 256 (defn generate-gmask [pic]
251 257
252 (str "1 0 0 0\n" 258 (str "1 0 0 0\n"
253 (str-join "" (map (fn [[index row]] 259 (str-join "" (map (fn [[index row]]
254 (row->gmask dpi (odd? index) row)) 260 (row->gmask dpi (even? index) row))
255 (indexed (make-rows pic)))))) 261 (indexed (make-rows pic))))))
262
256 263
257 ;; 1 0 0 0 264 ;; 1 0 0 0
258 ;; 0 0 1 2.881 265 ;; 0 0 1 2.881
259 ;; 0 0 0 2.881 266 ;; 0 0 0 2.881
260 ;; 0 1 0 2.863 267 ;; 0 1 0 2.863
300 (let [image (invert-frame-hash (b&w (frame-hash (rotate 180 (ImagePlus. pic)))))] 307 (let [image (invert-frame-hash (b&w (frame-hash (rotate 180 (ImagePlus. pic)))))]
301 (spit "/home/r/kevin/out.ngc" (generate-gcode image)) 308 (spit "/home/r/kevin/out.ngc" (generate-gcode image))
302 (spit "/home/r/kevin/out.gmask" (generate-gmask image)) 309 (spit "/home/r/kevin/out.gmask" (generate-gmask image))
303 image)) 310 image))
304 311
305 312
313
314 (defn update-state []
315 (def sing "/home/r/lasercutter/graster/signer4laser2x1.png")
316
317 (def pic (frame-hash (let [image (ImagePlus. sing)]
318 (.rotate (.getChannelProcessor image) 180)
319 image)))
320
321 (def pic (b&w pic)))
322
323
324
325
326
327
328
329
330
331
306 332
307 ;;;; testing 333 ;;;; testing
308 334
309 (defn init [] 335 (defn init []
310 (let [stuff 336 (let [stuff
337 363
338 (let [temp *out*] 364 (let [temp *out*]
339 (.start 365 (.start
340 (Thread. 366 (Thread.
341 (fn [] 367 (fn []
342 (with-bindings {#'clojure.core/*out* temp} 368 (with-bindings {#'*out* temp}
343 (Thread/sleep 5000) 369 (Thread/sleep 5000)
344 (println "hi"))))))) 370 (println "hi")))))))