# HG changeset patch # User Robert McIntyre # Date 1282375282 14400 # Node ID 0f48db8d2a05abc5d18128fd3a47c3d98bd93493 # Parent 5e167f275a9315b2b0f7ad43b962d11794b7ae12 going to upgrade to clojure 1.2 diff -r 5e167f275a93 -r 0f48db8d2a05 src/laser/rasterize.clj --- a/src/laser/rasterize.clj Fri Aug 20 06:53:41 2010 -0400 +++ b/src/laser/rasterize.clj Sat Aug 21 03:21:22 2010 -0400 @@ -14,29 +14,21 @@ (import '(java.awt.image BufferedImage)) (use 'clojure.contrib.str-utils) - +(use 'clojure.contrib.seq-utils) (use 'clojure.contrib.combinatorics) - +(use 'clojure.contrib.duck-streams) (use 'clojure.contrib.repl-utils) (set! *print-length* 20) -(def img "/home/r/graster/test.png") (def feed 120) (def dpi [500, 500]) -(def on_range [0.0, 0.5]) -(def overshoot 0.5) -(def offset [1.0, 1.0]) -(def tiles [1, 1]) -(def tile_size [false, false]) -(def tile_spacing [0.125, 0.125]) -(def feed 120) -(def cut_feed 20) -(def corner_radius 0) + + (defn preserve-meta [f] @@ -44,14 +36,11 @@ (apply f x) (meta (last x))))) +(defmulti frame-hash-multi class) -(defn frame-hash - "yields a convienent representation for the pixles in an image. - Because of the size of the structvre generated, this must only be used - in a transient way so that java can do it's garbage collection." - [#^java.lang.String image-name] - (let [image+ (ImagePlus. image-name)] +(defmethod frame-hash-multi ImagePlus + [image+] (with-meta (let [buf (.. image+ getBufferedImage) color (.getColorModel buf)] @@ -64,11 +53,24 @@ (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16) :g (bit-shift-right (bit-and 0x00ff00 data) 8) :b (bit-and 0x0000ff data)))))))) - {:width (.getWidth image+) :height (.getHeight image+)}))) + {:width (.getWidth image+) :height (.getHeight image+)})) +(defmethod frame-hash-multi String + [image-name] + (let [image+ (ImagePlus. image-name)] + (frame-hash-multi image+))) +(defn frame-hash + "yields a convienent representation for the pixles in an image. + Because of the size of the structvre generated, this must only be used + in a transient way so that java can do it's garbage collection." + [something] + (frame-hash-multi something)) + +;(def frame-hash (preserve-meta frame-hash)) + @@ -101,13 +103,13 @@ ["M63 P0\nG61" (str \F feed) "M101" - "M3 S1"])) + "M3 S1\n"])) (defn raster-epilogue [] (str-join \newline - ["M63 PO" + ["M63 P0" "M5" - "M2"])) + "M2\n"])) (defn raster-comment [string] @@ -182,7 +184,7 @@ (defn row->gcode [[x-dpi y-dpi] row] (let [[x1 y1] (first row) [x2 y2] (last row)] - (str (format "GO X%.3f Y%.3f\n" + (str (format "G0 X%.3f Y%.3f\n" (float (* x1 (/ x-dpi))) (float (* y1 (/ y-dpi)))) @@ -245,28 +247,98 @@ - -;sequence of numbers indicating width -(def mega - - ;sequence of rows - - (map (partial sort #(< (first %1) (first %2))) - (partition-by last - (sort (fn [[x1 y1][x2 y2]] (> y2 y1)) - (map first (filter-vals (partial = black) pic)))))) - - - - (defn generate-gmask [pic] (str "1 0 0 0\n" - (str-join "" (map (fn [[index row]] (row->gmask dpi (even? index) row)) (indexed (make-rows pic)))))) + (str-join "" (map (fn [[index row]] + (row->gmask dpi (odd? index) row)) + (indexed (make-rows pic)))))) + +;; 1 0 0 0 +;; 0 0 1 2.881 +;; 0 0 0 2.881 +;; 0 1 0 2.863 +;; 0 0 0 2.769 +;; 0 1 0 2.751 +;; 0 0 0 2.729 +;; 0 1 0 2.617 +;; 0 0 0 2.593 +;; 0 1 0 2.561 +;; 0 0 0 2.463 +;; 0 1 0 2.445 +;; 0 0 0 2.385 +;; 0 1 0 2.317 +;; 0 0 0 2.253 +;; 0 1 0 2.233 +;; 0 0 0 2.177 + (defn generate-gcode [pic] - (str-join "" (map (partial row->gcode dpi) (make-rows pic)))) + (str (raster-preamble) + + + (str-join "" (map (partial row->gcode dpi) (make-rows pic))) + (raster-epilogue))) + +(defn rotate [degrees #^ImagePlus image] + (.rotate (.getChannelProcessor image) degrees) + image) +(defn map-keys [f m] + (into {} (map (fn [[key val]] [(f key) val]) m))) + + + +(defn invert-frame-hash [pic] + (map-keys (fn [[x y]] [x (- (:height (meta pic)) y 1)]) pic )) + + +(defn generate-files [pic] + (let [image (invert-frame-hash (b&w (frame-hash (rotate 180 (ImagePlus. pic)))))] + (spit "/home/r/kevin/out.ngc" (generate-gcode image)) + (spit "/home/r/kevin/out.gmask" (generate-gmask image)) + image)) + + + +;;;; testing + +(defn init [] + (let [stuff + + (bound-fn [] + + (do + (println "hi everyone") + (def img "/home/r/kevin/sing.png") + (def pic (frame-hash (let [image (ImagePlus. img)] + (.rotate (.getChannelProcessor image) 180) + image))) + + + (def test-image + (invert-frame-hash (b&w (frame-hash (rotate 180 (ImagePlus. img)))))) + + (defn test-gmask [] + (println (str-join "" (take 170 (generate-gmask test-image))))) + + (println "ALL variables initialized!") + + ))] + (.start + (Thread. stuff)))) + + + +(defn thread-test [] + + (let [temp *out*] + (.start + (Thread. + (fn [] + (with-bindings {#'clojure.core/*out* temp} + (Thread/sleep 5000) + (println "hi"))))))) diff -r 5e167f275a93 -r 0f48db8d2a05 src/laser/rubyisms.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/laser/rubyisms.clj Sat Aug 21 03:21:22 2010 -0400 @@ -0,0 +1,9 @@ +(ns laser.rubyisms + (:use [clojure.contrib repl-utils str-utils seq-utils])) + + +(defmacro unless [boolean & forms] + `(if (not ~boolean) ~@forms)) + +(defn exit [] + ) diff -r 5e167f275a93 -r 0f48db8d2a05 swank-laser --- a/swank-laser Fri Aug 20 06:53:41 2010 -0400 +++ b/swank-laser Sat Aug 21 03:21:22 2010 -0400 @@ -1,4 +1,4 @@ -":";exec java -verbose:gc -Xmn700M -Xms1g -Xmx1g -cp $HOME/lasercutter/src:$HOME/lasercutter/lib/* clojure.main $0 $*; +":";exec java -verbose:gc -Xmn100M -Xms1500M -Xmx1500M -cp $HOME/roBin/src:$HOME/lasercutter/src:$HOME/lasercutter/lib/* clojure.main $0 $*; (do (require 'swank.swank)