Skip to content

Instantly share code, notes, and snippets.

@beoliver
Created July 26, 2017 22:27
Show Gist options
  • Save beoliver/36bb8f7cd186af13be3c899b035e77a6 to your computer and use it in GitHub Desktop.
Save beoliver/36bb8f7cd186af13be3c899b035e77a6 to your computer and use it in GitHub Desktop.
(ns bitmap
(:require [clojure.core.matrix :as matrix]
[clojure.java.io :as io])
(:import
java.awt.image.BufferedImage
javax.imageio.ImageIO
java.awt.Color
java.io.File))
(matrix/set-current-implementation :vectorz)
(def ^:const rgb-max -1)
(def ^:const rgb-min -16777216)
(defn to-rgb-int
([{:keys [r g b]}] (to-rgb-int r g b))
([r g b] (.intValue (bit-or
(long 0xff000000)
(bit-shift-left (int r) 16)
(bit-shift-left (int g) 8)
(int b)))))
(defn as-rgb [rgb-int]
{:r (bit-shift-right (bit-and 0xff0000 rgb-int) 16)
:g (bit-shift-right (bit-and 0xff00 rgb-int) 8)
:b (bit-and 0xff rgb-int)})
(defn open-img [path]
(ImageIO/read (io/file path)))
(defn img-matrix [img]
(matrix/matrix (for [x (range (.getWidth img))]
(for [y (range (.getHeight img))]
(.getRGB img x y)))))
(defn red [x] (bit-shift-right (bit-and 0xff0000 (long x)) 16))
(defn green [x] (bit-shift-right (bit-and 0xff00 (long x)) 8))
(defn blue [x] (bit-and 0xff (long x)))
(defn red-channel [matrix]
(matrix/emap red matrix))
(defn green-channel [matrix]
(matrix/emap green matrix))
(defn blue-channel [matrix]
(matrix/emap blue matrix))
(defn make-img
([m] (make-img m identity))
([m f] (let [[width height] (matrix/shape m)
i (BufferedImage. height width BufferedImage/TYPE_INT_ARGB)]
(doseq [[x row] (map-indexed vector m)]
(doseq [[y colour] (map-indexed vector row)]
(.setRGB i x y (f colour))))
i)))
;; (defn random-rgb-channel-matrix [height width]
;; (let [random-row-fn #(vec (repeatedly width (fn [] (rand-int 256))))]
;; (vec (repeatedly height random-row-fn))))
;; (defn random-matrix [height width min max]
;; (let [r (Math/abs (- min max))
;; random-row-fn #(vec (repeatedly width (fn [] (+ min (rand-int (+ r 1))))))]
;; (matrix/matrix (vec (repeatedly height random-row-fn)))))
;; (defn random-rgb-matrix [height width]
;; (random-matrix height width rgb-min rgb-max))
;; (defn random-img [height width]
;; (let [m (random-rgb-matrix height width)
;; i (BufferedImage. height width BufferedImage/TYPE_INT_ARGB)]
;; (doseq [[x row] (map-indexed vector m)]
;; (doseq [[y rgb] (map-indexed vector row)]
;; (.setRGB i x y rgb)))
;; i))
;; (defn make-img [m height width]
;; (let [i (BufferedImage. height width BufferedImage/TYPE_INT_ARGB)]
;; (doseq [[x row] (map-indexed vector m)]
;; (doseq [[y rgb] (map-indexed vector row)]
;; (.setRGB i x y rgb)))
;; i))
;; (defn matrix-map [f m]
;; (matrix/matrix (vec (map (fn [row] (vec (map f row))) m))))
;; (defn normalize [m]
;; (matrix/emap (fn [x] (let [v (mod x rgb-min)]
;; (if (zero? v) rgb-max v))) m))
;; ;; (defn normalize [m]
;; ;; (matrix/emap (fn [x] (let [v (/ x (Math/abs rgb-min))]
;; ;; (if (zero? v) rgb-max v))) m))
;; (defn mmul-image [infile1 infile2 outfile]
;; (let [img1 (open-img infile1)
;; img2 (open-img infile2)
;; rgb-matrix1 (matrix/mul 1 (get-rgb-matrix img1))
;; rgb-matrix2 (matrix/mul 1 (get-rgb-matrix img2))
;; n (normalize (matrix/mmul rgb-matrix2 rgb-matrix1))
;; img-out (make-img n (.getHeight img1) (.getWidth img1))]
;; (ImageIO/write img-out "png" (File. outfile))))
;; (defn scalar-image [infile1 outfile]
;; (let [img1 (open-img infile1)
;; rgb-matrix1 (matrix/emap #(/ % 2048) (get-rgb-matrix img1))
;; n (normalize rgb-matrix1)
;; img-out (make-img n (.getHeight img1) (.getWidth img1))]
;; (ImageIO/write img-out "png" (File. outfile))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment