Grayscale image: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 210: Line 210:
tImage.Save("spectrum2.bmp");
tImage.Save("spectrum2.bmp");
</lang>
</lang>

=={{header|Clojure|Clojure}}==
<lang clojure>
(ns rgb-to-gray.clj
(:import [java.io File]
[javax.imageio ImageIO]
[java.awt Color]
[java.awt.image BufferedImage]))


(defn rgb-to-gray [color-image] (map
(fn [y] (
map
(
fn [x]
(
let [rgb (.getRGB color-image x y) rgb-object (new Color rgb) ]
(
let [r (.getRed rgb-object) g (.getGreen rgb-object) b (.getBlue rgb-object) a (.getAlpha rgb-object)]
(
;Compute the grayscale value an return it: L = 0.2126·R + 0.7152·G + 0.0722·B
+ (* r 0.2126) (* g 0.7152) (* b 0.0722)
)

)
)

)

(
range
(
.getWidth color-image
)
)
)
)

(
range
(
.getHeight color-image
)
)


)
)

(defn write-matrix-to-image [matrix filename]
(ImageIO/write (let [height (count matrix) width (count (first matrix)) output-image (new BufferedImage width height BufferedImage/TYPE_BYTE_GRAY)]
(
do
(spit "/dev/null" ( ;This is the only way I could find to force evaluation so that side-effects are applied. spit just writes to null file
map
(
fn [row-index] (


map (fn [column-index]
(
.setRGB output-image column-index row-index (.intValue (nth (nth matrix row-index) column-index))
)
)
(range width)


)
)
(range height)
)
)
output-image
)
)
"png" (new File filename))
)

(println
(write-matrix-to-image
(rgb-to-gray
(ImageIO/read (new File "test.jpg"))
)
"test-gray-cloj.png")
)

</lang>

=={{header|Common Lisp}}==
=={{header|Common Lisp}}==