annotate src/laser/rasterize.clj @ 1:6d9bdaf919f7

added clojureDemo source
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 00:32:44 -0400
parents 163bf9b2fd13
children 5ed873917c34
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@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@1 41
rlm@1 42
rlm@1 43
rlm@1 44 (defn raster-preamble []
rlm@1 45 (str-join \newline
rlm@1 46 ["M63 P0\nG61"
rlm@1 47 (str \F feed)
rlm@1 48 "M101"
rlm@1 49 "M3 S1"]))
rlm@1 50
rlm@1 51
rlm@1 52
rlm@0 53 (defn frame-hash
rlm@0 54 "yields a convienent representation for the pixles in an image.
rlm@0 55 Because of the size of the structvre generated, this must only be used
rlm@0 56 in a transient way so that java can do it's garbage collection."
rlm@0 57 [#^java.lang.String image-name]
rlm@0 58 (let [image+ (ImagePlus. image-name)]
rlm@0 59 (with-meta
rlm@0 60 (let [buf (.. image+ getBufferedImage)
rlm@0 61 color (.getColorModel buf)]
rlm@0 62 (apply hash-map
rlm@0 63 (interleave
rlm@0 64 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 65 (vector x y)))
rlm@0 66 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]
rlm@0 67 (let [data (.getRGB buf x y)]
rlm@0 68 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@0 69 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@0 70 :b (bit-and 0x0000ff data))))))))
rlm@0 71 {:width (.getWidth image+) :height (.getHeight image+)})))
rlm@0 72
rlm@0 73
rlm@1 74 (def white {:r 255, :g 255, :b 255})
rlm@1 75 (def black {:r 0, :g 0, :b 0})
rlm@1 76
rlm@1 77 (def expt #(Math/pow %1 %2))
rlm@1 78
rlm@1 79 (defn rgb-euclidian
rlm@1 80 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]
rlm@1 81 (expt (+ (expt (- r1 r2) 2)
rlm@1 82 (expt (- g1 g2) 2)
rlm@1 83 (expt (- b1 b2) 2)) 0.5))
rlm@1 84
rlm@1 85 (defn b&w
rlm@1 86 "turn everything strictly black or white"
rlm@1 87 [window]
rlm@1 88 (with-meta
rlm@1 89 (zipmap
rlm@1 90 (keys window)
rlm@1 91 (map (fn [rgb]
rlm@1 92 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))
rlm@1 93 black white))
rlm@1 94 (vals window))) (meta window)))
rlm@1 95
rlm@1 96
rlm@1 97
rlm@1 98
rlm@1 99
rlm@1 100
rlm@1 101
rlm@1 102
rlm@1 103
rlm@1 104
rlm@1 105
rlm@1 106
rlm@1 107
rlm@1 108
rlm@1 109
rlm@0 110 (defn frame-hash->bufferedImage
rlm@0 111 [frame-hash]
rlm@0 112 (let [data (meta frame-hash)
rlm@0 113 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]
rlm@0 114
rlm@0 115 (doall (for [element frame-hash]
rlm@0 116 (let [coord (key element)
rlm@0 117 rgb (val element)
rlm@0 118 packed-RGB
rlm@0 119 (+ (bit-shift-left (:r rgb) 16)
rlm@0 120 (bit-shift-left (:g rgb) 8)
rlm@0 121 (:b rgb))]
rlm@0 122 (.setRGB image (first coord) (last coord) packed-RGB))))
rlm@0 123 image))
rlm@0 124
rlm@0 125
rlm@0 126