Skip to content

Instantly share code, notes, and snippets.

@hellonico
Created October 6, 2013 01:12
Show Gist options
  • Save hellonico/6848079 to your computer and use it in GitHub Desktop.
Save hellonico/6848079 to your computer and use it in GitHub Desktop.
(ns imaging.core)
(import 'java.awt.image.BufferedImage)
(use 'clojure.java.io)
(import '[javax.swing JFrame JLabel ImageIcon])
(defn setpxl [^BufferedImage image data]
(let [h (.getHeight image)
w (.getWidth image)]
(.setRGB image 0 0 w h ^ints data 0 w)
) )
(defn getrgb [rgb]
(let [r (bit-shift-right (bit-and rgb (int 0x00FF0000)) 16)
g (bit-shift-right (bit-and rgb (int 0x0000FF00)) 8)
b (bit-and rgb (int 0x000000FF))]
[r g b])
)
(defn getpxl [^BufferedImage img]
(.getRGB img 0 0 (.getWidth img) (.getHeight img) nil 0 (.getWidth img) )
)
(defn graycalc [[r g b]]
(let [gray (int (/ (+ r g b) 3))
r (bit-shift-left gray 16)
g (bit-shift-left gray 8)
b gray
a (bit-shift-left 0x00 24)
]
(int (bit-or a r g b))))
(defn testrgb []
(let [img (time (javax.imageio.ImageIO/read (as-file "resources/cat.jpg")))
h (.getHeight img)
w (.getWidth img)
arr (time (int-array (getpxl img)))
gray (time
;;why is amap so slow?
;;400ms
; (int-array (map #(graycalc (getrgb %1)) arr))
;;8000ms
(amap ^ints arr idx ret ^int (graycalc (getrgb (aget ^ints arr idx))))
)
frame (JFrame. "grayscale image")
label (JLabel. (ImageIcon. img))
panel (.getContentPane frame)]
(-> panel
(.add label))
(.setSize frame w h)
(.setVisible frame true)
(time (setpxl img gray))
(.repaint panel)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment