65918ae90e16f13cedf4ce6902046b91f8080434
[uxul-world.git] / files.lisp
1 ;;; Copyright 2009 Christoph Senjak
2
3 ;; This file declares the constants for loading different files and
4 ;; file-formats.
5
6 (in-package :uxul-world)
7
8 (defun si (var val)
9   (setf (symbol-value (intern var)) val))
10
11
12 (defun stretch-image (x y img)
13   "Call ImageMagick to resize that file to 64x64."
14   (lisp-magick:with-magick-wand (mywand)
15     (lisp-magick::magick-read-image-blob mywand img)
16     (lisp-magick::magick-resize-image mywand x y #x00000000 1d0)
17     (lisp-magick::magick-set-format mywand "png")
18     (lisp-magick::magick-get-image-blob mywand)))
19
20 (defun ash-sized-image (img a)
21   "Calculate an image of half of the size."
22   (lisp-magick:with-magick-wand (mywand)
23     (lisp-magick::magick-read-image-blob mywand img)
24     (let
25         ((w (lisp-magick::magick-get-image-width mywand))
26          (h (lisp-magick::magick-get-image-height mywand)))
27       (lisp-magick::magick-resize-image mywand
28                                         (max 1 (floor (/ w a))) (max 1 (floor (/ h a))) ;; no ash here ...
29                                         #x00000000 1d0)
30       (lisp-magick::magick-set-format mywand "png")
31       (lisp-magick::magick-get-image-blob mywand))))
32
33 (defun all-sizes (img)
34   (list img
35         (ash-sized-image img 2)
36         (ash-sized-image img 4)
37         (ash-sized-image img 8)))
38
39
40 ;; (defun init-file (file)
41 ;;   "Load a file into a Variable. Access with |filename| (without .png
42 ;; and path)."
43 ;;   (si (pathname-name file)
44 ;;       (stretched-image 
45 ;;       (with-open-file (in file :element-type '(unsigned-byte 8)) 
46 ;;      (let* ((length (file-length in))
47 ;;             (content (make-array (list length)
48 ;;                                  :element-type '(unsigned-byte 8)
49 ;;                                  :adjustable nil)))
50 ;;        (read-sequence content in)
51 ;;        content)))))
52
53 (defun init-png-file (file)
54   "Load an image file into a Variable. Set |filename| (without .png
55 and path) to a list with all sizes of that image."
56   (si (pathname-name file) 
57       (all-sizes
58        (with-open-file (in file :element-type '(unsigned-byte 8)) 
59          (let* ((length (file-length in))
60                 (content (make-array (list length)
61                                      :element-type '(unsigned-byte 8)
62                                      :adjustable nil)))
63            (read-sequence content in)
64            content)))))
65
66 (defun png-p (file)
67   "Is the file relevant for initialization? So far only .png-files are
68 relevant."
69   (string= (pathname-type file) "png"))
70
71 (defun init-png-files ()
72   (cl-fad:walk-directory
73    (asdf:component-pathname (asdf:find-system :uxul-world))
74    #'init-png-file :test #'png-p))
75
76 (defun init-files ()
77   "Load the relevant files into variables"
78   (init-png-files))
79
80 (init-files)