change convert-image to use the java image->ascii function
This commit is contained in:
parent
54dc8b0543
commit
7c29069559
|
@ -1,55 +1,15 @@
|
||||||
(ns toascii.models.image
|
(ns toascii.models.image
|
||||||
"Largely based on the Claskii library: https://github.com/LauJensen/Claskii"
|
|
||||||
(:import (java.awt RenderingHints Graphics2D Image)
|
(:import (java.awt RenderingHints Graphics2D Image)
|
||||||
(java.awt.image BufferedImage Raster)
|
(java.awt.image BufferedImage Raster)
|
||||||
(javax.imageio ImageIO)
|
(javax.imageio ImageIO)
|
||||||
(java.io File)
|
(java.io File)
|
||||||
(java.net URL))
|
(java.net URL)
|
||||||
|
(toascii.images ImageToAscii))
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
[cemerick.url :as url]
|
[cemerick.url :as url]
|
||||||
[toascii.util :refer [query-param-url->java-url]])
|
[toascii.util :refer [query-param-url->java-url]])
|
||||||
(:use hiccup.core))
|
(:use hiccup.core))
|
||||||
|
|
||||||
(def ascii-chars [\# \A \@ \% \$ \+ \= \* \: \, \. \space])
|
|
||||||
(def num-ascii-chars (count ascii-chars))
|
|
||||||
|
|
||||||
(defn- add-pixel [^Integer argb ^StringBuilder sb color?]
|
|
||||||
(let [r (bit-shift-right (bit-and 0x00ff0000 argb) 16)
|
|
||||||
g (bit-shift-right (bit-and 0x0000ff00 argb) 8)
|
|
||||||
b (bit-and 0x000000ff argb)
|
|
||||||
peak (int
|
|
||||||
(Math/sqrt
|
|
||||||
(+ (* r r 0.241)
|
|
||||||
(* g g 0.691)
|
|
||||||
(* b b 0.068))))
|
|
||||||
char-index (if (zero? peak)
|
|
||||||
(dec num-ascii-chars)
|
|
||||||
(dec (int (* num-ascii-chars (/ peak 255)))))
|
|
||||||
pixel-char (nth ascii-chars (if (pos? char-index) char-index 0))]
|
|
||||||
(if color?
|
|
||||||
(doto sb
|
|
||||||
(.append "<span style=\"color: rgb(")
|
|
||||||
(.append r)
|
|
||||||
(.append ",")
|
|
||||||
(.append g)
|
|
||||||
(.append ",")
|
|
||||||
(.append b)
|
|
||||||
(.append ");\">")
|
|
||||||
(.append pixel-char)
|
|
||||||
(.append "</span>"))
|
|
||||||
pixel-char)))
|
|
||||||
|
|
||||||
(defn- pixels->ascii [^BufferedImage image color?]
|
|
||||||
(let [width (.getWidth image)
|
|
||||||
height (.getHeight image)
|
|
||||||
sb (StringBuilder. (+ (* width height 47) (* height 4)))
|
|
||||||
^ints pixels (.getRGB image 0 0 width height nil 0 width)]
|
|
||||||
(dotimes [y height]
|
|
||||||
(dotimes [x width]
|
|
||||||
(add-pixel (aget pixels (+ x (* y width))) sb color?))
|
|
||||||
(.append sb (if color? "<br>" \newline)))
|
|
||||||
(.toString sb)))
|
|
||||||
|
|
||||||
(defn get-image-by-url
|
(defn get-image-by-url
|
||||||
(^BufferedImage [^String url]
|
(^BufferedImage [^String url]
|
||||||
(try
|
(try
|
||||||
|
@ -91,24 +51,7 @@
|
||||||
final-image (if-not (= new-width current-width)
|
final-image (if-not (= new-width current-width)
|
||||||
(scale-image image new-width)
|
(scale-image image new-width)
|
||||||
image)]
|
image)]
|
||||||
(pixels->ascii final-image color?))))
|
(ImageToAscii/convert final-image color?))))
|
||||||
|
|
||||||
(defn wrap-pre-tag [s]
|
(defn wrap-pre-tag [s]
|
||||||
(str "<pre style=\"font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;\">" s "</pre>"))
|
(str "<pre style=\"font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;\">" s "</pre>"))
|
||||||
|
|
||||||
|
|
||||||
#_(set! *warn-on-reflection* true)
|
|
||||||
|
|
||||||
|
|
||||||
#_(require '[criterium.core :as c])
|
|
||||||
|
|
||||||
#_(c/bench
|
|
||||||
(let [f (File. "./test/images/test.png")
|
|
||||||
image (get-image-by-file f)
|
|
||||||
ascii (convert-image image true)]
|
|
||||||
(count ascii)))
|
|
||||||
|
|
||||||
#_(let [f (File. "./test/images/test.png")
|
|
||||||
image (get-image-by-file f)
|
|
||||||
ascii (convert-image image true)]
|
|
||||||
(count ascii))
|
|
Reference in a new issue