annotate 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
rev   line source
rlm@0 1 (ns laser.rasterize)
rlm@0 2
rlm@0 3 (import '(java.io File))
rlm@0 4 (import '(org.apache.commons.io FileUtils))
rlm@0 5 (import '(javax.imageio ImageIO) )
rlm@0 6 (import '(javax.swing JFrame))
rlm@0 7 (import '(java.awt Color BorderLayout))
rlm@0 8 (import '(ij ImagePlus IJ))
rlm@0 9 (import '(java.lang Math))
rlm@2 10 (import '(java.awt Graphics2D Panel))
rlm@0 11 (import '(ij Macro))
rlm@0 12
rlm@0 13 (import '(java.io BufferedReader InputStreamReader))
rlm@0 14 (import '(java.awt.image BufferedImage))
rlm@0 15
rlm@0 16 (use 'clojure.contrib.str-utils)
rlm@0 17
rlm@0 18 (use 'clojure.contrib.combinatorics)
rlm@0 19
rlm@0 20
rlm@0 21 (use 'clojure.contrib.repl-utils)
rlm@0 22
rlm@0 23 (set! *print-length* 20)
rlm@0 24
rlm@0 25
rlm@0 26 (def img "/home/r/graster/test.png")
rlm@0 27
rlm@1 28
rlm@1 29 (def feed 120)
rlm@1 30 (def dpi [500, 500])
rlm@1 31 (def on_range [0.0, 0.5])
rlm@1 32 (def overshoot 0.5)
rlm@1 33 (def offset [1.0, 1.0])
rlm@1 34 (def tiles [1, 1])
rlm@1 35 (def tile_size [false, false])
rlm@1 36 (def tile_spacing [0.125, 0.125])
rlm@1 37 (def feed 120)
rlm@1 38 (def cut_feed 20)
rlm@1 39 (def corner_radius 0)
rlm@1 40
rlm@2 41 (defmulti display "Creates a JFrame and displays a buffered image" class)
rlm@1 42
rlm@2 43 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))
rlm@2 44
rlm@2 45 (defmethod display
rlm@2 46 BufferedImage [image]
rlm@2 47 (let [panel (makePanel image)
rlm@2 48 frame (JFrame. "Oh Yeah!")]
rlm@2 49 (.add frame panel)
rlm@2 50 (.pack frame)
rlm@2 51 (.setVisible frame true )
rlm@2 52 (.setSize frame(.getWidth image) (.getHeight image))))
rlm@2 53
rlm@2 54 (defmethod display
rlm@2 55 ImagePlus [image]
rlm@2 56 (display (.getBufferedImage image)))
rlm@2 57
rlm@2 58 (defmethod display
rlm@2 59 clojure.lang.PersistentHashMap [frame-hash]
rlm@2 60 (display (frame-hash->bufferedImage frame-hash)))
rlm@2 61
rlm@2 62 (defmethod display
rlm@2 63 clojure.lang.PersistentArrayMap [frame-hash]
rlm@2 64 (display (frame-hash->bufferedImage frame-hash)))
rlm@1 65
rlm@1 66
rlm@1 67 (defn raster-preamble []
rlm@1 68 (str-join \newline
rlm@1 69 ["M63 P0\nG61"
rlm@1 70 (str \F feed)
rlm@1 71 "M101"
rlm@1 72 "M3 S1"]))
rlm@1 73
rlm@0 74 (defn frame-hash
rlm@0 75 "yields a convienent representation for the pixles in an image.
rlm@0 76 Because of the size of the structvre generated, this must only be used
rlm@0 77 in a transient way so that java can do it's garbage collection."
rlm@0 78 [#^java.lang.String image-name]
rlm@0 79 (let [image+ (ImagePlus. image-name)]
rlm@0 80 (with-meta
rlm@0 81 (let [buf (.. image+ getBufferedImage)
rlm@0 82 color (.getColorModel buf)]
rlm@0 83 (apply hash-map
rlm@0 84 (interleave
rlm@0 85 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 86 (vector x y)))
rlm@0 87 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 88 (let [data (.getRGB buf x y)]
rlm@0 89 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@0 90 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@0 91 :b (bit-and 0x0000ff data))))))))
rlm@0 92 {:width (.getWidth image+) :height (.getHeight image+)})))
rlm@0 93
rlm@0 94
rlm@1 95 (def white {:r 255, :g 255, :b 255})
rlm@1 96 (def black {:r 0, :g 0, :b 0})
rlm@1 97
rlm@1 98 (def expt #(Math/pow %1 %2))
rlm@1 99
rlm@1 100 (defn rgb-euclidian
rlm@1 101 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]
rlm@1 102 (expt (+ (expt (- r1 r2) 2)
rlm@1 103 (expt (- g1 g2) 2)
rlm@1 104 (expt (- b1 b2) 2)) 0.5))
rlm@1 105
rlm@1 106 (defn b&w
rlm@1 107 "turn everything strictly black or white"
rlm@1 108 [window]
rlm@1 109 (with-meta
rlm@1 110 (zipmap
rlm@1 111 (keys window)
rlm@1 112 (map (fn [rgb]
rlm@1 113 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))
rlm@1 114 black white))
rlm@1 115 (vals window))) (meta window)))
rlm@1 116
rlm@1 117
rlm@1 118
rlm@1 119
rlm@1 120
rlm@2 121
rlm@2 122
rlm@2 123
rlm@2 124
rlm@2 125
rlm@2 126
rlm@2 127
rlm@2 128
rlm@1 129
rlm@1 130
rlm@1 131
rlm@1 132
rlm@1 133
rlm@1 134
rlm@1 135
rlm@1 136
rlm@1 137
rlm@1 138
rlm@0 139 (defn frame-hash->bufferedImage
rlm@0 140 [frame-hash]
rlm@0 141 (let [data (meta frame-hash)
rlm@0 142 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
rlm@0 143
rlm@0 144 (doall (for [element frame-hash]
rlm@0 145 (let [coord (key element)
rlm@0 146 rgb (val element)
rlm@0 147 packed-RGB
rlm@0 148 (+ (bit-shift-left (:r rgb) 16)
rlm@0 149 (bit-shift-left (:g rgb) 8)
rlm@0 150 (:b rgb))]
rlm@0 151 (.setRGB image (first coord) (last coord) packed-RGB))))
rlm@0 152 image))
rlm@0 153
rlm@0 154
rlm@0 155