1 ;;; album.el --- Photo album utility
3 ;; Copyright (C) 2005 MORIOKA Tomohiko
5 ;; Keywords: Photo, image, album, HTML, WWW
7 ;; This file is part of Album.
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)
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.
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.
26 ;; It requires `convert' and `identify' of ImageMagick.
30 (defun album-make-spec-by-width (width limit spec-name)
32 (let ((percent (floor (/ (* limit 100.0) width))))
33 (vector percent spec-name
34 (/ (* width percent) 100.0)
35 (/ (* height percent) 100.0)))))
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)))))
44 (defun album-write-html (dest-dir
45 prev-file file next-file
46 prev-grade grade next-grade
47 lang image-url-prefix)
50 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
51 \"http://www.w3.org/TR/html4/loose.dtd\">\n")
54 (insert (format " lang=\"%s\"" lang)))
57 (insert (format "<title>%s</title>\n" file))
60 (insert (format "<h1>%s</h1>\n" file))
63 (insert (format "<a href=\"%s.html\">" prev-file)))
70 (insert (format "<a href=\"%s.html\">" next-file)))
77 (insert (format "<a href=\"../%s/%s.html\">"
86 (insert (format "<a href=\"../%s/%s.html\">"
100 (format "../%s/%s.html" next-grade file)
101 (concat "../fullsize/" file ".jpg")))
103 (insert (format "<img alt=\"%s\" src=\"%s.jpg\">"
107 image-url-prefix grade file)
113 <a href=\"../index.html\">[index]</a>
118 (unless (file-exists-p
119 (expand-file-name (symbol-name grade) dest-dir))
121 (expand-file-name (symbol-name grade) dest-dir)))
122 (write-region (point-min)(point-max)
124 (concat file ".html")
125 (expand-file-name (symbol-name grade)
128 (defun album-convert-image (image-dest-dir
129 prev-file file next-file
130 lang image-url-prefix
132 (setq file (expand-file-name file))
133 (unless html-dest-dir
134 (setq html-dest-dir image-dest-dir))
137 (call-process "identify" nil t t file)
138 (goto-char (point-min))
139 (and (re-search-forward " \\([0-9]+\\)x\\([0-9]+\\) " nil t)
140 (cons (string-to-number (match-string 1))
141 (string-to-number (match-string 2))))))
146 (cond ((>= width height)
147 (when (setq ret (album-make-spec-by-width width 2048 'QXGA))
148 (setq dest (cons ret dest)))
149 (when (setq ret (album-make-spec-by-width width 1600 'UXGA))
150 (setq dest (cons ret dest)))
151 (when (setq ret (album-make-spec-by-width width 1400 'SXGA+))
152 (setq dest (cons ret dest)))
153 (when (setq ret (album-make-spec-by-width width 1280 'SXGA))
154 (setq dest (cons ret dest)))
155 (when (setq ret (album-make-spec-by-width width 1024 'XGA))
156 (setq dest (cons ret dest)))
157 (when (setq ret (album-make-spec-by-width width 800 'SVGA))
158 (setq dest (cons ret dest)))
159 (when (setq ret (album-make-spec-by-width width 640 'VGA))
160 (setq dest (cons ret dest)))
161 (when (setq ret (album-make-spec-by-width width 320 'QVGA))
162 (setq dest (cons ret dest)))
163 (when (setq ret (album-make-spec-by-width width 160 'thumbnail))
164 (setq dest (cons ret dest)))
167 (when (setq ret (album-make-spec-by-height height 1536 'QXGA))
168 (setq dest (cons ret dest)))
169 (when (setq ret (album-make-spec-by-height height 1200 'UXGA))
170 (setq dest (cons ret dest)))
171 (when (setq ret (album-make-spec-by-height height 1050 'SXGA+))
172 (setq dest (cons ret dest)))
173 (when (setq ret (album-make-spec-by-height height 960 'SXGA))
174 (setq dest (cons ret dest)))
175 (when (setq ret (album-make-spec-by-height height 768 'XGA))
176 (setq dest (cons ret dest)))
177 (when (setq ret (album-make-spec-by-height height 600 'SVGA))
178 (setq dest (cons ret dest)))
179 (when (setq ret (album-make-spec-by-height height 480 'VGA))
180 (setq dest (cons ret dest)))
181 (when (setq ret (album-make-spec-by-height height 240 'QVGA))
182 (setq dest (cons ret dest)))
183 (when (setq ret (album-make-spec-by-height height 160 'thumbnail))
184 (setq dest (cons ret dest)))
186 (unless (file-exists-p
187 (expand-file-name "thumbnail" image-dest-dir))
189 (expand-file-name "thumbnail" image-dest-dir)))
190 (call-process "convert" nil nil nil
191 "-resize" (format "%d%%" (aref (car dest) 0))
195 (file-name-sans-extension
196 (file-name-nondirectory file)) ".jpg")
200 (setq rest (cdr dest))
202 (setq spec (car rest))
203 (album-write-html html-dest-dir
205 (file-name-sans-extension
206 (file-name-nondirectory prev-file)))
207 (file-name-sans-extension
208 (file-name-nondirectory file))
210 (file-name-sans-extension
211 (file-name-nondirectory next-file)))
215 (aref (nth 1 rest) 1))
216 lang image-url-prefix)
217 (call-process "convert" nil nil nil
218 "-resize" (format "%d%%" (aref spec 0))
222 (file-name-sans-extension
223 (file-name-nondirectory file)) ".jpg")
225 (symbol-name (aref spec 1))
227 (setq prev-grade (aref spec 1))
228 (setq rest (cdr rest)))
229 (unless (file-exists-p
230 (expand-file-name "fullsize" image-dest-dir))
232 (expand-file-name "fullsize" image-dest-dir)))
233 (if (string= (downcase (file-name-extension file))
235 (call-process "ln" nil nil nil
238 (expand-file-name "fullsize" image-dest-dir))
239 (call-process "convert" nil nil nil
243 (file-name-sans-extension
244 (file-name-nondirectory file)) ".jpg")
245 (expand-file-name "fullsize" image-dest-dir))))
248 (defun album-convert-images (image-dest-dir source-images
249 &optional lang title parent-url
250 image-url-prefix html-dest-dir)
251 (unless html-dest-dir
252 (setq html-dest-dir image-dest-dir))
253 ;; (if (and (consp (car source-images))
254 ;; (null (cdr source-images)))
255 ;; (setq source-images (car source-images)))
256 (album-make-thumbnails html-dest-dir source-images
257 lang title image-url-prefix parent-url)
258 (let (file prev-file)
260 (setq file (car source-images))
261 (album-convert-image image-dest-dir
262 prev-file file (nth 1 source-images)
263 lang image-url-prefix html-dest-dir)
265 source-images (cdr source-images)))))
267 (defun album-make-thumbnails (html-dest-dir
269 lang title image-url-prefix parent-url)
272 (file-name-nondirectory
273 (if (eq (aref html-dest-dir (1- (length html-dest-dir))) ?/)
274 (substring html-dest-dir 0 (1- (length html-dest-dir)))
279 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
280 \"http://www.w3.org/TR/html4/loose.dtd\">\n")
283 (insert (format " lang=\"%s\"" lang)))
286 (insert (format "<title>%s</title>\n" title))
289 (insert (format "<h1>%s</h1>\n" title))
294 (dolist (image-file source-images)
295 (setq file (file-name-sans-extension
296 (file-name-nondirectory image-file)))
297 (insert "<a href=\"VGA/")
300 (insert (format "<img alt=\"%s\" src=\"thumbnail/%s.jpg\">"
304 image-url-prefix grade file)
312 (insert (format "[<a href=\"%s\">Return</a>]\n" parent-url)))
318 (write-region (point-min)(point-max)
319 (expand-file-name "index.html" html-dest-dir)))))
321 (defun album-convert-directory (image-dest-dir source-dir
324 lang title parent-url
325 image-url-prefix html-dest-dir)
328 (dolist (pat patterns)
331 (directory-files source-dir 'full pat))))
333 (let (case-fold-search)
336 ".+\\.\\(tiff\\|TIFF\\|jpg\\|JPG\\|jpeg\\|JPEG\\|gif\\|GIF\\|png\\|PNG\\)$"))))
337 (album-convert-images image-dest-dir files
338 lang title parent-url
339 image-url-prefix html-dest-dir)))
344 ;;; album.el ends here