annotate src/laser/rasterize.clj @ 0:163bf9b2fd13

initial import
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Aug 2010 22:24:41 -0400
parents
children 6d9bdaf919f7
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@0 10
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@0 28 (defn frame-hash
rlm@0 29 "yields a convienent representation for the pixles in an image.
rlm@0 30 Because of the size of the structvre generated, this must only be used
rlm@0 31 in a transient way so that java can do it's garbage collection."
rlm@0 32 [#^java.lang.String image-name]
rlm@0 33 (let [image+ (ImagePlus. image-name)]
rlm@0 34 (with-meta
rlm@0 35 (let [buf (.. image+ getBufferedImage)
rlm@0 36 color (.getColorModel buf)]
rlm@0 37 (apply hash-map
rlm@0 38 (interleave
rlm@0 39 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 40 (vector x y)))
rlm@0 41 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 42 (let [data (.getRGB buf x y)]
rlm@0 43 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@0 44 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@0 45 :b (bit-and 0x0000ff data))))))))
rlm@0 46 {:width (.getWidth image+) :height (.getHeight image+)})))
rlm@0 47
rlm@0 48
rlm@0 49 (defn frame-hash->bufferedImage
rlm@0 50 [frame-hash]
rlm@0 51 (let [data (meta frame-hash)
rlm@0 52 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
rlm@0 53
rlm@0 54 (doall (for [element frame-hash]
rlm@0 55 (let [coord (key element)
rlm@0 56 rgb (val element)
rlm@0 57 packed-RGB
rlm@0 58 (+ (bit-shift-left (:r rgb) 16)
rlm@0 59 (bit-shift-left (:g rgb) 8)
rlm@0 60 (:b rgb))]
rlm@0 61 (.setRGB image (first coord) (last coord) packed-RGB))))
rlm@0 62 image))
rlm@0 63
rlm@0 64
rlm@0 65