rlm@0: (ns laser.rasterize) rlm@0: rlm@0: (import '(java.io File)) rlm@0: (import '(org.apache.commons.io FileUtils)) rlm@0: (import '(javax.imageio ImageIO) ) rlm@0: (import '(javax.swing JFrame)) rlm@0: (import '(java.awt Color BorderLayout)) rlm@0: (import '(ij ImagePlus IJ)) rlm@0: (import '(java.lang Math)) rlm@0: rlm@0: (import '(ij Macro)) rlm@0: rlm@0: (import '(java.io BufferedReader InputStreamReader)) rlm@0: (import '(java.awt.image BufferedImage)) rlm@0: rlm@0: (use 'clojure.contrib.str-utils) rlm@0: rlm@0: (use 'clojure.contrib.combinatorics) rlm@0: rlm@0: rlm@0: (use 'clojure.contrib.repl-utils) rlm@0: rlm@0: (set! *print-length* 20) rlm@0: rlm@0: rlm@0: (def img "/home/r/graster/test.png") rlm@0: rlm@0: (defn frame-hash rlm@0: "yields a convienent representation for the pixles in an image. rlm@0: Because of the size of the structvre generated, this must only be used rlm@0: in a transient way so that java can do it's garbage collection." rlm@0: [#^java.lang.String image-name] rlm@0: (let [image+ (ImagePlus. image-name)] rlm@0: (with-meta rlm@0: (let [buf (.. image+ getBufferedImage) rlm@0: color (.getColorModel buf)] rlm@0: (apply hash-map rlm@0: (interleave rlm@0: (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))] rlm@0: (vector x y))) rlm@0: (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))] rlm@0: (let [data (.getRGB buf x y)] rlm@0: (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16) rlm@0: :g (bit-shift-right (bit-and 0x00ff00 data) 8) rlm@0: :b (bit-and 0x0000ff data)))))))) rlm@0: {:width (.getWidth image+) :height (.getHeight image+)}))) rlm@0: rlm@0: rlm@0: (defn frame-hash->bufferedImage rlm@0: [frame-hash] rlm@0: (let [data (meta frame-hash) rlm@0: image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)] rlm@0: rlm@0: (doall (for [element frame-hash] rlm@0: (let [coord (key element) rlm@0: rgb (val element) rlm@0: packed-RGB rlm@0: (+ (bit-shift-left (:r rgb) 16) rlm@0: (bit-shift-left (:g rgb) 8) rlm@0: (:b rgb))] rlm@0: (.setRGB image (first coord) (last coord) packed-RGB)))) rlm@0: image)) rlm@0: rlm@0: rlm@0: