Skip to content

Instantly share code, notes, and snippets.

@duncanmak
Created October 29, 2010 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncanmak/654059 to your computer and use it in GitHub Desktop.
Save duncanmak/654059 to your computer and use it in GitHub Desktop.
(defn save!
"Takes a BufferedImage and its title and write it out, along with 'n' downsampled versions"
[image title n]
(let [imp (ImagePlus. title image)
ip (.getProcessor imp)]
(IJ/save imp (.getAbsolutePath (file *root* title)))
(loop [i 1, ip ip]
(when (<= i n)
(let [dir (file *root* (str i))
new-ip (.resize ip (/ *width* (Math/pow 3 i)))]
(when-not (.exists dir) (.mkdir dir))
(IJ/save (ImagePlus. title new-ip) (.getAbsolutePath (file dir title)))
(recur (inc i) new-ip))))))
(defn render
"Take the section number, return [transformed-image image-title]"
[section]
(let [image (-> section load-section .getAbsolutePath IJ/openImage)
result (BufferedImage. *width* *height* BufferedImage/TYPE_BYTE_GRAY)
[m00 m01 m02 m10 m11 m12 m20 m21 m22] (get *transforms* section)
graphics (.createGraphics result)
title (.getTitle image)
m02 (+ m02 *tx*)
m12 (+ m12 *ty*)]
(doto graphics
(.setTransform (->affine-transform [m00 m01 m02 m10 m11 m12 m20 m21 m22]))
(.drawImage (.getBufferedImage image) 0 0 nil))
[result title]))
(defn write-from-queue [^BlockingQueue queue]
(doseq [[image title] (repeatedly #(.take queue))]
(save! image title 3)))
(defn render-to-queue [^BlockingQueue queue]
(let [sections (sort *sections*)
number-of-sections (count sections)
chunk (/ number-of-sections 4)
work (partition-all chunk sections)]
(doall
(pmap
(fn [sections]
(future
(doseq [s sections] (.put queue (render! s)))))
work))))
(defn run-with-queue []
(let [queue (ArrayBlockingQueue. 4)
render-jobs (render-to-queue queue)
io-jobs (future (write-from-queue queue))]
[queue render-jobs io-jobs]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment