diff src/laser/rasterize.clj @ 8:0f48db8d2a05

going to upgrade to clojure 1.2
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 03:21:22 -0400
parents 5e167f275a93
children f952052e37b7
line wrap: on
line diff
     1.1 --- a/src/laser/rasterize.clj	Fri Aug 20 06:53:41 2010 -0400
     1.2 +++ b/src/laser/rasterize.clj	Sat Aug 21 03:21:22 2010 -0400
     1.3 @@ -14,29 +14,21 @@
     1.4  (import '(java.awt.image BufferedImage))
     1.5  
     1.6  (use 'clojure.contrib.str-utils)
     1.7 -
     1.8 +(use 'clojure.contrib.seq-utils)
     1.9  (use 'clojure.contrib.combinatorics)
    1.10 -
    1.11 +(use 'clojure.contrib.duck-streams)
    1.12  
    1.13  (use 'clojure.contrib.repl-utils)
    1.14  
    1.15  (set! *print-length* 20)
    1.16  
    1.17  
    1.18 -(def img "/home/r/graster/test.png")
    1.19  
    1.20  
    1.21  (def feed 120)
    1.22  (def  dpi [500, 500])
    1.23 -(def on_range [0.0, 0.5])
    1.24 -(def overshoot 0.5)
    1.25 -(def offset [1.0, 1.0])
    1.26 -(def tiles [1, 1])
    1.27 -(def tile_size [false, false])
    1.28 -(def tile_spacing [0.125, 0.125])
    1.29 -(def feed 120)
    1.30 -(def cut_feed 20)
    1.31 -(def corner_radius 0)
    1.32 +
    1.33 +
    1.34  
    1.35  
    1.36  (defn preserve-meta [f]
    1.37 @@ -44,14 +36,11 @@
    1.38  	      (apply f x)
    1.39  	      (meta (last x)))))
    1.40  
    1.41 +(defmulti frame-hash-multi  class)
    1.42  
    1.43  
    1.44 -(defn frame-hash
    1.45 -  "yields a convienent representation for the pixles in an image.
    1.46 -   Because of the size of the structvre generated, this must only be used
    1.47 -   in a transient way so that java can do it's garbage collection."
    1.48 -  [#^java.lang.String image-name]
    1.49 -  (let [image+ (ImagePlus. image-name)]
    1.50 +(defmethod frame-hash-multi ImagePlus
    1.51 +  [image+]
    1.52      (with-meta
    1.53        (let [buf (.. image+ getBufferedImage)
    1.54  	    color (.getColorModel buf)]
    1.55 @@ -64,11 +53,24 @@
    1.56  			   (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
    1.57  				     :g (bit-shift-right (bit-and 0x00ff00 data) 8)
    1.58  				     :b (bit-and 0x0000ff data))))))))
    1.59 -      {:width  (.getWidth image+) :height (.getHeight image+)})))
    1.60 +      {:width  (.getWidth image+) :height (.getHeight image+)}))
    1.61  
    1.62  
    1.63 +(defmethod frame-hash-multi String
    1.64 +  [image-name]
    1.65 +  (let [image+ (ImagePlus. image-name)]
    1.66 +    (frame-hash-multi image+)))
    1.67  
    1.68  
    1.69 +(defn frame-hash
    1.70 +  "yields a convienent representation for the pixles in an image.
    1.71 +   Because of the size of the structvre generated, this must only be used
    1.72 +   in a transient way so that java can do it's garbage collection."
    1.73 +  [something]
    1.74 +  (frame-hash-multi something))
    1.75 +
    1.76 +;(def frame-hash (preserve-meta frame-hash))
    1.77 + 
    1.78  
    1.79  
    1.80  
    1.81 @@ -101,13 +103,13 @@
    1.82  	    ["M63 P0\nG61"
    1.83  	    (str \F feed)
    1.84  	    "M101"
    1.85 -	    "M3 S1"]))
    1.86 +	    "M3 S1\n"]))
    1.87  
    1.88  (defn raster-epilogue []
    1.89    (str-join \newline
    1.90 -	    ["M63 PO"
    1.91 +	    ["M63 P0"
    1.92  	     "M5"
    1.93 -	     "M2"]))
    1.94 +	     "M2\n"]))
    1.95  
    1.96  
    1.97  (defn raster-comment [string]
    1.98 @@ -182,7 +184,7 @@
    1.99  (defn row->gcode [[x-dpi y-dpi] row]
   1.100    (let [[x1 y1] (first row)
   1.101  	[x2 y2] (last row)]
   1.102 -    (str (format "GO X%.3f Y%.3f\n"
   1.103 +    (str (format "G0 X%.3f Y%.3f\n"
   1.104  		 (float (* x1 (/ x-dpi)))
   1.105  		 (float (* y1 (/ y-dpi))))
   1.106  	 
   1.107 @@ -245,28 +247,98 @@
   1.108  
   1.109  
   1.110  
   1.111 -
   1.112 -;sequence of numbers indicating width
   1.113 -(def mega 
   1.114 - 
   1.115 -     ;sequence of rows
   1.116 -      
   1.117 -      (map (partial sort #(< (first %1) (first %2)))
   1.118 -	   (partition-by last
   1.119 -			 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
   1.120 -			       (map first (filter-vals (partial = black) pic))))))
   1.121 -     
   1.122 -
   1.123 -
   1.124 -
   1.125  (defn generate-gmask [pic]
   1.126  
   1.127    (str "1 0 0 0\n"
   1.128 -       (str-join "" (map (fn [[index row]]  (row->gmask dpi (even? index) row)) (indexed (make-rows pic))))))
   1.129 +       (str-join "" (map (fn [[index row]]
   1.130 +			   (row->gmask dpi (odd? index) row))
   1.131 +			 (indexed (make-rows pic))))))
   1.132 +
   1.133 +;; 1 0 0 0
   1.134 +;; 0 0 1 2.881
   1.135 +;; 0 0 0 2.881
   1.136 +;; 0 1 0 2.863
   1.137 +;; 0 0 0 2.769
   1.138 +;; 0 1 0 2.751
   1.139 +;; 0 0 0 2.729
   1.140 +;; 0 1 0 2.617
   1.141 +;; 0 0 0 2.593
   1.142 +;; 0 1 0 2.561
   1.143 +;; 0 0 0 2.463
   1.144 +;; 0 1 0 2.445
   1.145 +;; 0 0 0 2.385
   1.146 +;; 0 1 0 2.317
   1.147 +;; 0 0 0 2.253
   1.148 +;; 0 1 0 2.233
   1.149 +;; 0 0 0 2.177
   1.150 +
   1.151  
   1.152  
   1.153  (defn generate-gcode [pic]
   1.154 -  (str-join "" (map (partial row->gcode dpi) (make-rows pic))))
   1.155 +  (str (raster-preamble)
   1.156 +       
   1.157 +       
   1.158 +       (str-join "" (map (partial row->gcode dpi) (make-rows pic)))
   1.159 +       (raster-epilogue)))
   1.160 +  
   1.161  
   1.162  
   1.163 +(defn rotate [degrees #^ImagePlus image]
   1.164 +  (.rotate (.getChannelProcessor image) degrees)
   1.165 +  image)
   1.166  
   1.167 +(defn map-keys [f m]
   1.168 +  (into {} (map (fn [[key val]] [(f key) val]) m)))
   1.169 +
   1.170 +
   1.171 +
   1.172 +(defn invert-frame-hash [pic]
   1.173 +  (map-keys (fn [[x y]] [x (- (:height (meta pic)) y 1)]) pic ))
   1.174 +
   1.175 +
   1.176 +(defn generate-files [pic]
   1.177 +  (let [image (invert-frame-hash (b&w (frame-hash  (rotate 180 (ImagePlus. pic)))))]
   1.178 +    (spit "/home/r/kevin/out.ngc" (generate-gcode image))
   1.179 +    (spit "/home/r/kevin/out.gmask" (generate-gmask image))
   1.180 +    image))
   1.181 +
   1.182 +  
   1.183 +
   1.184 +;;;; testing
   1.185 +
   1.186 +(defn init []
   1.187 +  (let [stuff
   1.188 +    
   1.189 +      (bound-fn []
   1.190 +
   1.191 +	(do 
   1.192 +	  (println "hi everyone")
   1.193 +	  (def img "/home/r/kevin/sing.png")
   1.194 +	  (def pic (frame-hash (let [image  (ImagePlus. img)]
   1.195 +				 (.rotate (.getChannelProcessor image) 180)
   1.196 +				 image)))
   1.197 +
   1.198 +
   1.199 +	  (def test-image
   1.200 +	       (invert-frame-hash (b&w (frame-hash  (rotate 180 (ImagePlus. img))))))
   1.201 +	  
   1.202 +	  (defn test-gmask []
   1.203 +	    (println (str-join "" (take 170 (generate-gmask test-image)))))
   1.204 +
   1.205 +	  (println "ALL variables initialized!")
   1.206 +
   1.207 +	  ))]
   1.208 +  (.start
   1.209 +     (Thread. stuff))))
   1.210 +
   1.211 +
   1.212 +
   1.213 +(defn thread-test []
   1.214 +
   1.215 +  (let [temp *out*]
   1.216 +    (.start
   1.217 +     (Thread.
   1.218 +      (fn []
   1.219 +	(with-bindings  {#'clojure.core/*out* temp}
   1.220 +	  (Thread/sleep 5000)
   1.221 +	  (println "hi")))))))