Mercurial > lasercutter
view 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 |
line wrap: on
line source
1 (ns laser.rasterize)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))11 (import '(ij Macro))13 (import '(java.io BufferedReader InputStreamReader))14 (import '(java.awt.image BufferedImage))16 (use 'clojure.contrib.str-utils)18 (use 'clojure.contrib.combinatorics)21 (use 'clojure.contrib.repl-utils)23 (set! *print-length* 20)26 (def img "/home/r/graster/test.png")29 (def feed 120)30 (def dpi [500, 500])31 (def on_range [0.0, 0.5])32 (def overshoot 0.5)33 (def offset [1.0, 1.0])34 (def tiles [1, 1])35 (def tile_size [false, false])36 (def tile_spacing [0.125, 0.125])37 (def feed 120)38 (def cut_feed 20)39 (def corner_radius 0)44 (defn raster-preamble []45 (str-join \newline46 ["M63 P0\nG61"47 (str \F feed)48 "M101"49 "M3 S1"]))53 (defn frame-hash54 "yields a convienent representation for the pixles in an image.55 Because of the size of the structvre generated, this must only be used56 in a transient way so that java can do it's garbage collection."57 [#^java.lang.String image-name]58 (let [image+ (ImagePlus. image-name)]59 (with-meta60 (let [buf (.. image+ getBufferedImage)61 color (.getColorModel buf)]62 (apply hash-map63 (interleave64 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]65 (vector x y)))66 (doall (for [x (range (.getWidth image+)) y (range (.getHeight image+))]67 (let [data (.getRGB buf x y)]68 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)69 :g (bit-shift-right (bit-and 0x00ff00 data) 8)70 :b (bit-and 0x0000ff data))))))))71 {:width (.getWidth image+) :height (.getHeight image+)})))74 (def white {:r 255, :g 255, :b 255})75 (def black {:r 0, :g 0, :b 0})77 (def expt #(Math/pow %1 %2))79 (defn rgb-euclidian80 [{r1 :r g1 :g b1 :b} {r2 :r g2 :g b2 :b} ]81 (expt (+ (expt (- r1 r2) 2)82 (expt (- g1 g2) 2)83 (expt (- b1 b2) 2)) 0.5))85 (defn b&w86 "turn everything strictly black or white"87 [window]88 (with-meta89 (zipmap90 (keys window)91 (map (fn [rgb]92 (if (> (rgb-euclidian rgb white) (rgb-euclidian rgb black))93 black white))94 (vals window))) (meta window)))110 (defn frame-hash->bufferedImage111 [frame-hash]112 (let [data (meta frame-hash)113 image (BufferedImage. (:width data) (:height data) BufferedImage/TYPE_INT_BGR)]115 (doall (for [element frame-hash]116 (let [coord (key element)117 rgb (val element)118 packed-RGB119 (+ (bit-shift-left (:r rgb) 16)120 (bit-shift-left (:g rgb) 8)121 (:b rgb))]122 (.setRGB image (first coord) (last coord) packed-RGB))))123 image))