Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(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]"
(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)]
(fn [sections]
(doseq [s sections] (.put queue (render! s)))))
(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
You can’t perform that action at this time.