comparison src/laser/rasterize.clj @ 21:e72220627685 tip

0.002 inch discrepancy with target. going to test anyway
author Robert McIntyre <rlm@mit.edu>
date Mon, 30 Aug 2010 01:19:21 -0400
parents 6bed8ceb51a9
children
comparison
equal deleted inserted replaced
20:6bed8ceb51a9 21:e72220627685
14 14
15 15
16 (set! *print-length* 20) 16 (set! *print-length* 20)
17 (def feed 120) 17 (def feed 120)
18 (def dpi [500, 500]) 18 (def dpi [500, 500])
19 (def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1}) 19 (def paramaters {:x-dpi 500 :y-dpi 500 :margin 0.501 :x-offset 1.001 :y-offset 1})
20 20
21 ;;; this process is divided into two tasks, 21 ;;; this process is divided into two tasks,
22 ;;; creating the raster g-code, which sweeps back and forth 22 ;;; creating the raster g-code, which sweeps back and forth
23 ;;; and creating the gmask, which turns the laser on and off. 23 ;;; and creating the gmask, which turns the laser on and off.
24 24
68 (comment 68 (comment
69 69
70 (import 'org.im4java.core.ConvertCmd) 70 (import 'org.im4java.core.ConvertCmd)
71 (import 'org.im4java.core.IMOperation) 71 (import 'org.im4java.core.IMOperation)
72 (import 'org.im4java.core.Stream2BufferedImage) 72 (import 'org.im4java.core.Stream2BufferedImage)
73 73
74 (def target (.getBufferedImage (ImagePlus. sing))) 74 (def target (.getBufferedImage (ImagePlus. sing)))
75 (def shiv (IMOperation.)) 75 (def shiv (IMOperation.))
76 (.addImage shiv) 76 (.addImage shiv)
77 (.addImage shiv (into-array String ["png:-"])) 77 (.addImage shiv (into-array String ["png:-"]))
78 (def conv-shiv (ConvertCmd.)) 78 (def conv-shiv (ConvertCmd.))
79 (def s2b (Stream2BufferedImage.)) 79 (def s2b (Stream2BufferedImage.))
80 (.setOutputConsumer conv-shiv s2b) 80 (.setOutputConsumer conv-shiv s2b)
81 (.run conv-shiv shiv (into-array Object [target])) 81 (.run conv-shiv shiv (into-array Object [target]))
82 (def result (.getImage s2b )) 82 (def result (.getImage s2b ))
83 83
84 ) 84 )
85 85
86 (defn mirror [#^ImagePlus img] 86 (defn mirror
87 "ImagePlus to ImagePlus via imageMagick through im4java!"
88 [#^ImagePlus img]
87 (let [title (.getTitle img) 89 (let [title (.getTitle img)
88 target (.getBufferedImage img) 90 target (.getBufferedImage img)
89 s2b (Stream2BufferedImage.) 91 s2b (Stream2BufferedImage.)
90 shiv (doto (IMOperation.) 92 shiv (doto (IMOperation.)
91 ;(.blur 2.0) 93 ;(.blur 2.0)
113 (let [x1 (+ x1 x-offset) 115 (let [x1 (+ x1 x-offset)
114 x2 (+ x2 x-offset) 116 x2 (+ x2 x-offset)
115 y1 (+ y1 y-offset) 117 y1 (+ y1 y-offset)
116 y2 (+ y2 y-offset)] 118 y2 (+ y2 y-offset)]
117 119
120 (let [margin (if forward? margin (- margin))]
121
122 (let [x1 (+ x1 margin)
123 x2 (- x2 margin)]
124
118 125
119 126
120 (str (format "G0 X%.3f Y%.3f\n" 127 (str (format "G0 X%.3f Y%.3f\n"
121 (float x1 ) 128 (float x1 )
122 (float y1)) 129 (float y1))
123 130
124 (format "G1 X%.3f Y%.3f\n" 131 (format "G1 X%.3f Y%.3f\n"
125 (float x2) 132 (float x2)
126 (float y2))))))) 133 (float y2)))))))))
127 134
128 (defn generate-gcode [pic] 135 (defn generate-gcode [pic]
129 (str (raster-preamble) 136 (str (raster-preamble)
130 (str-join "" 137 (str-join ""
131 (map 138 (map
175 (map first row)))) 182 (map first row))))
176 183
177 184
178 185
179 186
180 (defn row->gmask [[x-dpi y-dpi] forward? row] 187 (defn row->gmask [{:keys [x-dpi y-dpi margin x-offset y-offset]} forward? row]
181 (let [start (float (* (/ x-dpi) (first (first 188 (let [start (float (* (/ x-dpi) (first (first
182 (if forward? 189 (if forward?
183 (reverse row) row)))))] 190 (reverse row) row)))))
191 start (+ start x-offset)
192 ]
184 (let [preamble (if-not forward? 193 (let [preamble (if-not forward?
185 (format "0 0 0 %.3f\n" start) 194 (format "0 0 0 %.3f\n" start)
186 (format "0 0 1 %.3f\n" start)) 195 (format "0 0 1 %.3f\n" start))
187 body 196 body
188 (for [[x y] 197 (for [[x y]
189 (if forward? 198 (if forward?
190 (reverse (gather-row row)) 199 (reverse (gather-row row))
191 (gather-row row))] 200 (gather-row row))]
192 (let [x (float (* x (/ x-dpi))) 201 (let [x (float (* x (/ x-dpi)))
193 y (float (* y (/ x-dpi)))] 202 y (float (* y (/ x-dpi)))
194 ;; x (+ x 0.159)];; shift by a small margin. 203 x (+ x x-offset)
204 y (+ y y-offset)
205
206
207
208
209 ]
210
211
195 (if-not forward? 212 (if-not forward?
196 (str (format "0 0 1 %.3f\n" x) 213 (str (format "0 0 1 %.3f\n" x)
197 (format "0 1 1 %.3f\n" y)) 214 (format "0 1 1 %.3f\n" y))
198 215
199 (str (format "0 0 0 %.3f\n" y) 216 (str (format "0 0 0 %.3f\n" y)
203 220
204 221
205 (defn generate-gmask [pic] 222 (defn generate-gmask [pic]
206 (str "1 0 0 0\n" 223 (str "1 0 0 0\n"
207 (str-join "" (map (fn [[index row]] 224 (str-join "" (map (fn [[index row]]
208 (row->gmask dpi (even? index) row)) 225 (row->gmask paramaters (even? index) row))
209 (indexed (make-rows pic)))))) 226 (indexed (make-rows pic))))))
210 227
211 228
212 229
213 230