diff --git a/src/clojure/toascii/models/image.clj b/src/clojure/toascii/models/image.clj index 713e48e..6c04bcb 100644 --- a/src/clojure/toascii/models/image.clj +++ b/src/clojure/toascii/models/image.clj @@ -1,55 +1,15 @@ (ns toascii.models.image - "Largely based on the Claskii library: https://github.com/LauJensen/Claskii" (:import (java.awt RenderingHints Graphics2D Image) (java.awt.image BufferedImage Raster) (javax.imageio ImageIO) (java.io File) - (java.net URL)) + (java.net URL) + (toascii.images ImageToAscii)) (:require [clojure.string :as str] [cemerick.url :as url] [toascii.util :refer [query-param-url->java-url]]) (: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 "") - (.append pixel-char) - (.append "")) - 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? "
" \newline))) - (.toString sb))) - (defn get-image-by-url (^BufferedImage [^String url] (try @@ -91,24 +51,7 @@ final-image (if-not (= new-width current-width) (scale-image image new-width) image)] - (pixels->ascii final-image color?)))) + (ImageToAscii/convert final-image color?)))) (defn wrap-pre-tag [s] - (str "
" s "
")) - - -#_(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)) \ No newline at end of file + (str "
" s "
")) \ No newline at end of file