2009/04/26

よしビューア作った

(eval-when (:compile-toplevel :load-toplevel :execute)
(require :cl-opengl)
(require :cl-glu)
(require :cl-glut)
(require :cl-jpeg)
(require :drakma))

(defclass jpeg-viewer (glut:window)
((image :initarg :image :initform nil :accessor image))
(:default-initargs :mode '(:rgb :single)))

(defmethod glut:display-window :before ((w jpeg-viewer))
(gl:clear-color 0 0 0 0)
(%gl:shade-model :flat)
(gl:pixel-store :unpack-alignment 1))

(defmethod glut:display ((window jpeg-viewer))
(with-accessors ((image image)
(width glut:width)
(height glut:height)) window
(gl:clear :color-buffer-bit)
(gl:raster-pos 0 0)
(gl:draw-pixels width height :rgb :unsigned-byte image)
(%gl:flush)))

(defmethod glut:reshape ((window jpeg-viewer) w h)
(%gl:viewport 0 0 w h)
(%gl:matrix-mode :projection)
(%gl:load-identity)
(glu:ortho-2d 0 w 0 h)
(%gl:matrix-mode :modelview)
(%gl:load-identity))

(defmethod glut:keyboard ((window jpeg-viewer) key x y)
(declare (ignore x y))
(case key
((#\Esc #\q) (glut:destroy-current-window))))

(defun view (url)
(multiple-value-bind (image height width)
(jpeg:decode-stream (drakma:http-request url :want-stream t))
(loop for h from 0 below (/ height 2)
do (rotatef (subseq image (* h width 3) (* (1+ h) width 3))
(subseq image (- (* height width 3)
(* (1+ h) width 3))
(- (* height width 3)
(* h width 3)))))
(loop for i from 0 below (* height width 3) by 3
do (rotatef (aref image i) (aref image (+ i 2))))
(glut:display-window
(make-instance 'jpeg-viewer
:image image
:width width
:height height))))

;;(view "http://anime.nifty.com/luckystar/images/ls_wp_tpa.jpg")

0 件のコメント: