From 3b84441664e296cbe3cebf570146e857d318655d Mon Sep 17 00:00:00 2001 From: lordnik Date: Sat, 8 Sep 2018 19:42:31 +0200 Subject: [PATCH] Function to merge images verticaly and horizontaly toghter Images with the same width or height can be joined toghter either vertically(same width) or horizontally(same height). --- src/main/clojure/mikera/image/core.clj | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/clojure/mikera/image/core.clj b/src/main/clojure/mikera/image/core.clj index 0473e53..b3007fe 100644 --- a/src/main/clojure/mikera/image/core.clj +++ b/src/main/clojure/mikera/image/core.clj @@ -172,6 +172,36 @@ [^BufferedImage image ^long x ^long y ^long rgb] (.setRGB image (int x) (int y) (unchecked-int rgb))) +(defn merge-vert + "Merge images vertically together to one. Second image is merged +under first image, third image is merged under second image, etc. +All images must have the same width else return nil. +Return merged image." + ([image1 image2] + (merge-vert (list image1 image2))) + ([images] + (let [merged-image (new-image (width (first images)) + (reduce + (map height images)))] + (set-pixels merged-image (int-array (apply concat (map get-pixels images)))) + merged-image))) + +(defn merge-horiz + "Merge images horizontal together to one. Second image is merged on + the right side of first image, third image is merged on right side + of second image, etc. All images must have the same height else + return nil. + Return merged image." + ([image1 image2] + (merge-horiz (list image1 image2))) + ([images] + (let [merged-image (new-image (reduce + (map width images)) + (height (first images)))] + (set-pixels merged-image (int-array (flatten + (apply map concat + (map (fn [image] (partition (height (first images)) (get-pixels image))) + images))))) + merged-image))) + (defn width "Gets the width of an image as a long value" (^long [^BufferedImage image]