Rename www-image.el to album.el; rename prefix `www-image' to `album'.
[elisp/album.git] / album.el
1 ;;; album.el --- Photo album utility
2
3 ;; Copyright (C) 2004 MORIOKA Tomohiko
4
5 ;; Keywords: Photo, image, album, HTML, WWW
6
7 ;; This file is part of Album.
8
9 ;; Album is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; Album is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This facility is documented in the Emacs Manual.
27
28 ;;; Code:
29
30 (defun album-make-spec-by-width (width limit spec-name)
31   (when (> width limit)
32     (let ((percent (floor (/ (* limit 100.0) width))))
33       (vector percent spec-name
34               (/ (* width percent) 100.0)
35               (/ (* height percent) 100.0)))))
36
37 (defun album-make-spec-by-height (height limit spec-name)
38   (when (> height limit)
39     (let ((percent (floor (/ (* limit 100.0) height))))
40       (vector percent spec-name
41               (/ (* width percent) 100.0)
42               (/ (* height percent) 100.0)))))
43
44 (defun album-write-html (dest-dir
45                          prev-file file next-file
46                          prev-grade grade next-grade)
47   (with-temp-buffer
48     (insert
49      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
50             \"http://www.w3.org/TR/html4/loose.dtd\">\n")
51     (insert "<head>\n")
52     (insert (format "<title>%s</title>\n" file))
53     (insert "</head>\n")
54     (insert "<body>\n")
55     (insert (format "<h1>%s</h1>\n" file))
56
57     (if prev-file
58         (insert (format "<a href=\"%s.html\">" prev-file)))
59     (insert "[Previous]")
60     (if prev-file
61         (insert "</a>"))
62     (insert "\n")
63
64     (if next-file
65         (insert (format "<a href=\"%s.html\">" next-file)))
66     (insert "[Next]")
67     (if next-file
68         (insert "</a>"))
69     (insert "\n")
70
71     (if prev-grade
72         (insert (format "<a href=\"../%s/%s.html\">"
73                         prev-grade
74                         file)))
75     (insert "[Smaller]")
76     (if prev-grade
77         (insert "</a>"))
78     (insert "\n")
79
80     (if next-grade
81         (insert (format "<a href=\"../%s/%s.html\">"
82                         next-grade
83                         file)))
84     (insert "[Larger]")
85     (if next-grade
86         (insert "</a>"))
87     (insert "\n")
88
89     (insert "
90 <hr>
91 ")
92     (insert "<a href=\"")
93     (insert
94      (if next-grade
95           (format "../%s/%s.html" next-grade file)
96        (concat "../fullsize/" file ".jpg")))
97     (insert "\">")
98     (insert (format "<img alt=\"%s\" src=\"%s.jpg\">" file file))
99     (insert "</a>
100
101 <hr>
102
103 </body>
104 </html>
105 ")
106     (unless (file-exists-p
107              (expand-file-name (symbol-name grade) dest-dir))
108       (make-directory
109        (expand-file-name (symbol-name grade) dest-dir)))
110     (write-region (point-min)(point-max)
111                   (expand-file-name
112                    (concat file ".html")
113                    (expand-file-name (symbol-name grade)
114                                      dest-dir)))))
115
116 (defun album-convert-images (dest-dir prev-file file next-file)
117   (let* ((ret
118           (with-temp-buffer
119             (call-process "identify" nil t t file)
120             (goto-char (point-min))
121             (and (re-search-forward " \\([0-9]+\\)x\\([0-9]+\\) " nil t)
122                  (cons (string-to-number (match-string 1))
123                        (string-to-number (match-string 2))))))
124          (width (car ret))
125          (height (cdr ret))
126          prev-grade
127          rest dest)
128     (cond ((>= width height)
129            (when (setq ret (album-make-spec-by-width width 2048 'QXGA))
130              (setq dest (cons ret dest)))
131            (when (setq ret (album-make-spec-by-width width 1600 'UXGA))
132              (setq dest (cons ret dest)))
133            (when (setq ret (album-make-spec-by-width width 1400 'SXGA+))
134              (setq dest (cons ret dest)))
135            (when (setq ret (album-make-spec-by-width width 1280 'SXGA))
136              (setq dest (cons ret dest)))
137            (when (setq ret (album-make-spec-by-width width 1024 'XGA))
138              (setq dest (cons ret dest)))
139            (when (setq ret (album-make-spec-by-width width 800 'SVGA))
140              (setq dest (cons ret dest)))
141            (when (setq ret (album-make-spec-by-width width 640 'VGA))
142              (setq dest (cons ret dest)))
143            )
144           (t
145            (when (setq ret (album-make-spec-by-height height 1536 'QXGA))
146              (setq dest (cons ret dest)))
147            (when (setq ret (album-make-spec-by-height height 1200 'UXGA))
148              (setq dest (cons ret dest)))
149            (when (setq ret (album-make-spec-by-height height 1050 'SXGA+))
150              (setq dest (cons ret dest)))
151            (when (setq ret (album-make-spec-by-height height 960 'SXGA))
152              (setq dest (cons ret dest)))
153            (when (setq ret (album-make-spec-by-height height 768 'XGA))
154              (setq dest (cons ret dest)))
155            (when (setq ret (album-make-spec-by-height height 600 'SVGA))
156              (setq dest (cons ret dest)))
157            (when (setq ret (album-make-spec-by-height height 480 'VGA))
158              (setq dest (cons ret dest)))
159            ))
160     (setq rest dest)
161     (while rest
162       (setq spec (car rest))
163       (album-write-html dest-dir
164                             (if prev-file
165                                 (file-name-sans-extension
166                                  (file-name-nondirectory prev-file)))
167                             (file-name-sans-extension
168                              (file-name-nondirectory file))
169                             (if next-file
170                                 (file-name-sans-extension
171                                  (file-name-nondirectory next-file)))
172                             prev-grade
173                             (aref spec 1)
174                             (if (nth 1 rest)
175                                 (aref (nth 1 rest) 1)))
176       (call-process "convert" nil nil nil
177                     "-resize" (format "%d%%" (aref spec 0))
178                     file
179                     (expand-file-name
180                      (concat
181                       (file-name-sans-extension
182                        (file-name-nondirectory file)) ".jpg")
183                      (expand-file-name
184                       (symbol-name (aref spec 1))
185                       dest-dir)))
186       (setq prev-grade (aref spec 1))
187       (setq rest (cdr rest)))
188     (unless (file-exists-p
189              (expand-file-name "fullsize" dest-dir))
190       (make-directory
191        (expand-file-name "fullsize" dest-dir)))
192     (call-process "convert" nil nil nil
193                   file
194                   (expand-file-name
195                    (concat
196                     (file-name-sans-extension
197                      (file-name-nondirectory file)) ".jpg")
198                    (expand-file-name "fullsize" dest-dir)))
199     dest))
200
201 ;;; album.el ends here