;;; gif-skippy.lisp
;;;
;;; Author: Eric Marsden <eric.marsden@free.fr>
;;; Time-stamp: <2006-12-28 emarsden>


(eval-when (:compile-toplevel :load-toplevel :execute)
  (asdf:oos 'asdf:load-op :skippy)
  (asdf:oos 'asdf:load-op :flexi-streams))


(in-package :renderer)

(defun gstream->flexi-stream (gstream)
  (let ((data (make-array 1024 :element-type '(unsigned-byte 8)
                          :fill-pointer 0
                          :adjustable t)))
    (loop :for b = (g/read-byte gstream nil nil)
          :while b
          :do (vector-push-extend b data))
    (g/close gstream)
    (flexi-streams:make-in-memory-input-stream data)))


(defun gif-stream->aimage (gstream)
  (let* ((data-stream (skippy::read-data-stream (gstream->flexi-stream gstream)))
         (image (skippy:last-image data-stream))
         (gif-color-table (skippy:color-table data-stream))
         (aimage (make-aimage (skippy:width image)
                              (skippy:height image) :alpha-p nil))
         (aimage-data (aimage-data aimage)))
    (dotimes (x (skippy:width image))
      (dotimes (y (skippy:height image))
        (multiple-value-bind (r g b)
            (skippy:color-rgb
             (skippy:color-table-entry gif-color-table (skippy:pixel-ref image x y)))
          (setf (aref aimage-data y x)
                (dpb r (byte 8 0)
                     (dpb g (byte 8 8)
                          (dpb b (byte 8 16)
                               (dpb (- 255 0) (byte 8 24) 0))))))))
    aimage))


;; EOF
