diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/laser/rasterize.clj	Thu Aug 19 22:24:41 2010 -0400
     1.3 @@ -0,0 +1,65 @@
     1.4 +(ns laser.rasterize)
     1.5 +
     1.6 +(import '(java.io File))
     1.7 +(import '(org.apache.commons.io FileUtils))
     1.8 +(import '(javax.imageio ImageIO) )
     1.9 +(import '(javax.swing JFrame))
    1.10 +(import '(java.awt Color BorderLayout))
    1.11 +(import '(ij ImagePlus IJ))
    1.12 +(import '(java.lang Math))
    1.13 +
    1.14 +(import '(ij Macro))
    1.15 +
    1.16 +(import '(java.io BufferedReader InputStreamReader))
    1.17 +(import '(java.awt.image BufferedImage))
    1.18 +
    1.19 +(use 'clojure.contrib.str-utils)
    1.20 +
    1.21 +(use 'clojure.contrib.combinatorics)
    1.22 +
    1.23 +
    1.24 +(use 'clojure.contrib.repl-utils)
    1.25 +
    1.26 +(set! *print-length* 20)
    1.27 +
    1.28 +
    1.29 +(def img "/home/r/graster/test.png")
    1.30 +
    1.31 +(defn frame-hash
    1.32 +  "yields a convienent representation for the pixles in an image.
    1.33 +   Because of the size of the structvre generated, this must only be used
    1.34 +   in a transient way so that java can do it's garbage collection."
    1.35 +  [#^java.lang.String image-name]
    1.36 +  (let [image+ (ImagePlus. image-name)]
    1.37 +    (with-meta
    1.38 +      (let [buf (.. image+ getBufferedImage)
    1.39 +	    color (.getColorModel buf)]
    1.40 +	(apply hash-map 
    1.41 +	       (interleave 
    1.42 +		(doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
    1.43 +			 (vector x y)))
    1.44 +		(doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
    1.45 +			 (let [data (.getRGB buf x y)] 
    1.46 +			   (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
    1.47 +				     :g (bit-shift-right (bit-and 0x00ff00 data) 8)
    1.48 +				     :b (bit-and 0x0000ff data))))))))
    1.49 +      {:width  (.getWidth image+) :height (.getHeight image+)})))
    1.50 +
    1.51 +
    1.52 +(defn frame-hash->bufferedImage
    1.53 +  [frame-hash]
    1.54 +    (let [data  (meta frame-hash)
    1.55 +	image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
    1.56 +	
    1.57 +	(doall (for [element frame-hash] 
    1.58 +	  (let [coord  (key element) 
    1.59 +		rgb   (val element)
    1.60 +		packed-RGB 
    1.61 +		(+ (bit-shift-left (:r rgb) 16)
    1.62 +		   (bit-shift-left (:g rgb) 8)
    1.63 +		                   (:b rgb))]
    1.64 +	    (.setRGB image (first coord) (last coord) packed-RGB))))
    1.65 +	image))
    1.66 +
    1.67 +
    1.68 +