Mercurial > lasercutter
view src/laser/rasterize.clj @ 2:5ed873917c34
enabled display
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 20 Aug 2010 00:39:50 -0400 |
parents | 6d9bdaf919f7 |
children | e6254010c95a |
line wrap: on
line source
1 (ns laser.rasterize)3 (import '(java.io File))4 (import '(org.apache.commons.io FileUtils))5 (import '(javax.imageio ImageIO) )6 (import '(javax.swing JFrame))7 (import '(java.awt Color BorderLayout))8 (import '(ij ImagePlus IJ))9 (import '(java.lang Math))10 (import '(java.awt Graphics2D Panel))11 (import '(ij Macro))13 (import '(java.io BufferedReader InputStreamReader))14 (import '(java.awt.image BufferedImage))16 (use 'clojure.contrib.str-utils)18 (use 'clojure.contrib.combinatorics)21 (use 'clojure.contrib.repl-utils)23 (set! *print-length* 20)26 (def img "/home/r/graster/test.png")29 (def feed 120)30 (def dpi [500, 500])31 (def on_range [0.0, 0.5])32 (def overshoot 0.5)33 (def offset [1.0, 1.0])34 (def tiles [1, 1])35 (def tile_size [false, false])36 (def tile_spacing [0.125, 0.125])37 (def feed 120)38 (def cut_feed 20)39 (def corner_radius 0)41 (defmulti display "Creates a JFrame and displays a buffered image" class)43 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))45 (defmethod display46 BufferedImage [image]47 (let [panel (makePanel image)48 frame (JFrame. "Oh Yeah!")]49 (.add frame panel)50 (.pack frame)51 (.setVisible frame true )52 (.setSize frame(.getWidth image) (.getHeight image))))54 (defmethod display55 ImagePlus [image]56 (display (.getBufferedImage image)))58 (defmethod display59 clojure.lang.PersistentHashMap [frame-hash]60 (display (frame-hash->bufferedImage frame-hash)))62 (defmethod display63 clojure.lang.PersistentArrayMap [frame-hash]64 (display (frame-hash->bufferedImage frame-hash)))67 (defn raster-preamble []68 (str-join \newline69 ["M63 P0\nG61"70 (str \F feed)71 "M101"72 "M3 S1"]))74 (defn frame-hash75 "yields a convienent representation for the pixles in an image.76 Because of the size of the structvre generated, this must only be used77 in a transient way so that java can do it's garbage collection."78 [#^java.lang.String image-name]79 (let [image+ (ImagePlus. image-name)]80 (with-meta81 (let [buf (.. image+ getBufferedImage)82 color (.getColorModel buf)]83 (apply hash-map84 (interleave85 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]86 (vector x y)))87 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]88 (let [data (.getRGB buf x y)]89 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)90 :g (bit-shift-right (bit-and 0x00ff00 data) 8)91 :b (bit-and 0x0000ff data))))))))92 {:width (.getWidth image+) :height (.getHeight image+)})))95 (def white {:r 255, :g 255, :b 255})96 (def black {:r 0, :g 0, :b 0})98 (def expt #(Math/pow %1 %2))100 (defn rgb-euclidian101 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]102 (expt (+ (expt (- r1 r2) 2)103 (expt (- g1 g2) 2)104 (expt (- b1 b2) 2)) 0.5))106 (defn b&w107 "turn everything strictly black or white"108 [window]109 (with-meta110 (zipmap111 (keys window)112 (map (fn [rgb]113 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))114 black white))115 (vals window))) (meta window)))139 (defn frame-hash->bufferedImage140 [frame-hash]141 (let [data (meta frame-hash)142 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]144 (doall (for [element frame-hash]145 (let [coord (key element)146 rgb (val element)147 packed-RGB148 (+ (bit-shift-left (:r rgb) 16)149 (bit-shift-left (:g rgb) 8)150 (:b rgb))]151 (.setRGB image (first coord) (last coord) packed-RGB))))152 image))