所以我一直在 clojure 中进行基本的图像处理(只是将 rgb 转换为灰度),并且我在使用 amap 时遇到了严重的问题;也就是说,我不能让它快。
我已经能够通过类型提示将其从 21000 毫秒降低到大约 8000 毫秒,但仅此而已。相比之下,法线贴图的运行时间约为 400 毫秒……
我还能做些什么来让它像这样的简单操作一样快吗?
(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 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 "D:/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 (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)
)
)