diff --git a/src/toascii/models/image.clj b/src/toascii/models/image.clj index a657a64..35d9385 100644 --- a/src/toascii/models/image.clj +++ b/src/toascii/models/image.clj @@ -1,8 +1,10 @@ (ns toascii.models.image (:import (java.awt.image BufferedImage) + (java.io Writer) (javax.imageio.stream ImageInputStream)) (:require [clojure.string :as str] [clojure.java.io :as io] + [ring.util.io :refer [piped-input-stream]] [clj-image2ascii.core :as i2a] [toascii.util :refer [query-param-url->java-url]]) (:use hiccup.core)) @@ -19,7 +21,7 @@ (let [java-url (query-param-url->java-url url)] (i2a/get-image-stream-by-url java-url))) -(defn wrap-pre-tag [s & {:keys [hidden? delay]}] +(defn- wrap-pre-tag [s & {:keys [hidden? delay]}] (str "
" s "")) +(defn- get-open-pre-tag [hidden? delay] + (str + "
")) + (defn image->ascii [^BufferedImage image scale-to-width color? html?] (let [converted (i2a/convert-image image scale-to-width color?) ascii (:image converted)] @@ -37,14 +50,21 @@ (wrap-pre-tag ascii) ascii))) +(defn- write-out [^Writer w s] + (.write w (str s))) + (defn gif->ascii [^ImageInputStream image-stream scale-to-width color?] - (as-> image-stream x - (i2a/convert-animated-gif-frames x scale-to-width color?) - (:frames x) - (map - (fn [frame] - (wrap-pre-tag (:image frame) :delay (:delay frame) :hidden? true)) - x) - (str/join x) - (str "" x "") - (str x ""))) \ No newline at end of file + (piped-input-stream + (fn [output-stream] + (with-open [^Writer w (io/writer output-stream :append true)] + (write-out w "") + (i2a/stream-animated-gif-frames! + image-stream scale-to-width color? + (fn [{:keys [image delay]}] + (write-out w (get-open-pre-tag true delay)) + (write-out w image) + (write-out w ""))) + (write-out w "") + (write-out w ""))))) \ No newline at end of file