Mercurial > lasercutter
view src/laser/rasterize.clj @ 3:e6254010c95a
added raster-comment
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 20 Aug 2010 00:49:00 -0400 |
parents | 5ed873917c34 |
children | 6533b4ef83ad |
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 frame-hash68 "yields a convienent representation for the pixles in an image.69 Because of the size of the structvre generated, this must only be used70 in a transient way so that java can do it's garbage collection."71 [#^java.lang.String image-name]72 (let [image+ (ImagePlus. image-name)]73 (with-meta74 (let [buf (.. image+ getBufferedImage)75 color (.getColorModel buf)]76 (apply hash-map77 (interleave78 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]79 (vector x y)))80 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]81 (let [data (.getRGB buf x y)]82 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)83 :g (bit-shift-right (bit-and 0x00ff00 data) 8)84 :b (bit-and 0x0000ff data))))))))85 {:width (.getWidth image+) :height (.getHeight image+)})))88 (def white {:r 255, :g 255, :b 255})89 (def black {:r 0, :g 0, :b 0})91 (def expt #(Math/pow %1 %2))93 (defn rgb-euclidian94 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]95 (expt (+ (expt (- r1 r2) 2)96 (expt (- g1 g2) 2)97 (expt (- b1 b2) 2)) 0.5))99 (defn b&w100 "turn everything strictly black or white"101 [window]102 (with-meta103 (zipmap104 (keys window)105 (map (fn [rgb]106 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))107 black white))108 (vals window))) (meta window)))112 (defn raster-preamble []113 (str-join \newline114 ["M63 P0\nG61"115 (str \F feed)116 "M101"117 "M3 S1"]))140 (defn frame-hash->bufferedImage141 [frame-hash]142 (let [data (meta frame-hash)143 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]145 (doall (for [element frame-hash]146 (let [coord (key element)147 rgb (val element)148 packed-RGB149 (+ (bit-shift-left (:r rgb) 16)150 (bit-shift-left (:g rgb) 8)151 (:b rgb))]152 (.setRGB image (first coord) (last coord) packed-RGB))))153 image))