# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1283145561 14400
# Node ID e7222062768571109239c24f2726d17069cec69f
# Parent  6bed8ceb51a9110172acdc4761dd03532edcba42
0.002 inch discrepancy with target.  going to test anyway

diff -r 6bed8ceb51a9 -r e72220627685 src/laser/rasterize.clj
--- a/src/laser/rasterize.clj	Mon Aug 30 01:01:35 2010 -0400
+++ b/src/laser/rasterize.clj	Mon Aug 30 01:19:21 2010 -0400
@@ -16,7 +16,7 @@
 (set! *print-length* 20)
 (def feed 120)
 (def  dpi [500, 500])
-(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1})
+(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0.501 :x-offset 1.001 :y-offset 1})
 
 ;;; this process is divided into two tasks,
 ;;; creating the raster g-code, which sweeps back and forth
@@ -70,20 +70,22 @@
   (import 'org.im4java.core.ConvertCmd)
   (import 'org.im4java.core.IMOperation)
   (import 'org.im4java.core.Stream2BufferedImage)			
-
+  
   (def target (.getBufferedImage (ImagePlus. sing)))
- (def shiv (IMOperation.))
-(.addImage shiv)
-(.addImage shiv (into-array String ["png:-"]))
-(def conv-shiv (ConvertCmd.))
-(def s2b (Stream2BufferedImage.))
-(.setOutputConsumer conv-shiv s2b)
-(.run conv-shiv shiv (into-array Object [target]))
-(def result (.getImage s2b ))
+  (def shiv (IMOperation.))
+  (.addImage shiv)
+  (.addImage shiv (into-array String ["png:-"]))
+  (def conv-shiv (ConvertCmd.))
+  (def s2b (Stream2BufferedImage.))
+  (.setOutputConsumer conv-shiv s2b)
+  (.run conv-shiv shiv (into-array Object [target]))
+  (def result (.getImage s2b ))
 
   )
 
-(defn mirror [#^ImagePlus img]
+(defn mirror
+  "ImagePlus to ImagePlus via imageMagick through im4java!" 
+  [#^ImagePlus img]
   (let [title (.getTitle img)
 	target (.getBufferedImage img)
 	s2b (Stream2BufferedImage.)
@@ -115,6 +117,11 @@
 	  y1 (+ y1 y-offset)
 	  y2 (+ y2 y-offset)]
 
+      (let [margin (if forward? margin (- margin))]
+
+	(let [x1 (+ x1 margin)
+	      x2 (- x2 margin)]
+
       
           
       (str (format "G0 X%.3f Y%.3f\n"
@@ -123,7 +130,7 @@
 	   
 	   (format "G1 X%.3f Y%.3f\n"
 		   (float x2)
-		   (float y2)))))))
+		   (float y2)))))))))
   
 (defn generate-gcode [pic]
   (str (raster-preamble)
@@ -177,10 +184,12 @@
 
 
 
-(defn row->gmask [[x-dpi y-dpi] forward? row]
+(defn row->gmask [{:keys [x-dpi y-dpi margin x-offset y-offset]} forward? row]
   (let [start (float (* (/ x-dpi) (first (first
 					  (if forward?
-					  (reverse row) row)))))]
+					    (reverse row) row)))))
+	start (+ start x-offset)
+	]
     (let [preamble (if-not forward?
 		     (format "0 0 0 %.3f\n" start)
 		     (format "0 0 1 %.3f\n" start))
@@ -190,8 +199,16 @@
 		  (reverse (gather-row row))
 		  (gather-row row))]
 	    (let [x (float (* x (/ x-dpi)))
-		  y (float (* y (/ x-dpi)))]
-;;		  x (+ x 0.159)];; shift by a small margin.
+		  y (float (* y (/ x-dpi)))
+		  x (+ x x-offset)
+		  y (+ y y-offset)
+
+
+
+
+		  ]
+	      
+     
 	      (if-not forward?
 		(str (format "0 0 1 %.3f\n" x)
 		     (format "0 1 1 %.3f\n" y))
@@ -205,7 +222,7 @@
 (defn generate-gmask [pic]
   (str "1 0 0 0\n"
        (str-join "" (map (fn [[index row]]
-			   (row->gmask dpi (even? index) row))
+			   (row->gmask paramaters (even? index) row))
 			 (indexed (make-rows pic))))))