comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:163bf9b2fd13
1 (ns laser.rasterize)
2
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
11 (import '(ij Macro))
12
13 (import '(java.io BufferedReader InputStreamReader))
14 (import '(java.awt.image BufferedImage))
15
16 (use 'clojure.contrib.str-utils)
17
18 (use 'clojure.contrib.combinatorics)
19
20
21 (use 'clojure.contrib.repl-utils)
22
23 (set! *print-length* 20)
24
25
26 (def img "/home/r/graster/test.png")
27
28 (defn frame-hash
29 "yields a convienent representation for the pixles in an image.
30 Because of the size of the structvre generated, this must only be used
31 in a transient way so that java can do it's garbage collection."
32 [#^java.lang.String image-name]
33 (let [image+ (ImagePlus. image-name)]
34 (with-meta
35 (let [buf (.. image+ getBufferedImage)
36 color (.getColorModel buf)]
37 (apply hash-map
38 (interleave
39 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
40 (vector x y)))
41 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
42 (let [data (.getRGB buf x y)]
43 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
44 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
45 :b (bit-and 0x0000ff data))))))))
46 {:width (.getWidth image+) :height (.getHeight image+)})))
47
48
49 (defn frame-hash->bufferedImage
50 [frame-hash]
51 (let [data (meta frame-hash)
52 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
53
54 (doall (for [element frame-hash]
55 (let [coord (key element)
56 rgb (val element)
57 packed-RGB
58 (+ (bit-shift-left (:r rgb) 16)
59 (bit-shift-left (:g rgb) 8)
60 (:b rgb))]
61 (.setRGB image (first coord) (last coord) packed-RGB))))
62 image))
63
64
65