;;; album.el --- Photo album utility
;; Copyright (C) 2005 MORIOKA Tomohiko
;; Keywords: Photo, image, album, HTML, WWW
;; This file is part of Album.
;; Album is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; Album is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; It requires `convert' and `identify' of ImageMagick.
;;; Code:
(defun album-make-spec-by-width (width limit spec-name)
(when (> width limit)
(let ((percent (floor (/ (* limit 100.0) width))))
(vector percent spec-name
(/ (* width percent) 100.0)
(/ (* height percent) 100.0)))))
(defun album-make-spec-by-height (height limit spec-name)
(when (> height limit)
(let ((percent (floor (/ (* limit 100.0) height))))
(vector percent spec-name
(/ (* width percent) 100.0)
(/ (* height percent) 100.0)))))
(defun album-write-html (dest-dir lang image-url-prefix
prev-file file next-file
prev-grade grade next-grade)
(with-temp-buffer
(insert
"\n")
(insert "
\n")
(insert "\n")
(insert (format "%s\n" file))
(insert "\n")
(insert "\n")
(insert (format "%s
\n" file))
(if prev-file
(insert (format "" prev-file)))
(insert "[Previous]")
(if prev-file
(insert ""))
(insert "\n")
(if next-file
(insert (format "" next-file)))
(insert "[Next]")
(if next-file
(insert ""))
(insert "\n")
(if prev-grade
(insert (format ""
prev-grade
file)))
(insert "[Smaller]")
(if prev-grade
(insert ""))
(insert "\n")
(if next-grade
(insert (format ""
next-grade
file)))
(insert "[Larger]")
(if next-grade
(insert ""))
(insert "\n")
(insert "
")
(insert "")
(insert (format ""
file
(if image-url-prefix
(format "%s/%s/%s"
image-url-prefix grade file)
file)))
(insert "
[index]
")
(unless (file-exists-p
(expand-file-name (symbol-name grade) dest-dir))
(make-directory
(expand-file-name (symbol-name grade) dest-dir)))
(write-region (point-min)(point-max)
(expand-file-name
(concat file ".html")
(expand-file-name (symbol-name grade)
dest-dir)))))
(defun album-convert-image (image-dest-dir html-dest-dir lang
image-url-prefix
prev-file file next-file)
(setq file (expand-file-name file))
(unless html-dest-dir
(setq html-dest-dir image-dest-dir))
(let* ((ret
(with-temp-buffer
(call-process "identify" nil t t file)
(goto-char (point-min))
(and (re-search-forward " \\([0-9]+\\)x\\([0-9]+\\) " nil t)
(cons (string-to-number (match-string 1))
(string-to-number (match-string 2))))))
(width (car ret))
(height (cdr ret))
prev-grade
rest dest)
(cond ((>= width height)
(when (setq ret (album-make-spec-by-width width 2048 'QXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 1600 'UXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 1400 'SXGA+))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 1280 'SXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 1024 'XGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 800 'SVGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 640 'VGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-width width 160 'thumbnail))
(setq dest (cons ret dest)))
)
(t
(when (setq ret (album-make-spec-by-height height 1536 'QXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 1200 'UXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 1050 'SXGA+))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 960 'SXGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 768 'XGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 600 'SVGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 480 'VGA))
(setq dest (cons ret dest)))
(when (setq ret (album-make-spec-by-height height 160 'thumbnail))
(setq dest (cons ret dest)))
))
(unless (file-exists-p
(expand-file-name "thumbnail" image-dest-dir))
(make-directory
(expand-file-name "thumbnail" image-dest-dir)))
(call-process "convert" nil nil nil
"-resize" (format "%d%%" (aref (car dest) 0))
file
(expand-file-name
(concat
(file-name-sans-extension
(file-name-nondirectory file)) ".jpg")
(expand-file-name
"thumbnail"
image-dest-dir)))
(setq rest (cdr dest))
(while rest
(setq spec (car rest))
(album-write-html html-dest-dir lang image-url-prefix
(if prev-file
(file-name-sans-extension
(file-name-nondirectory prev-file)))
(file-name-sans-extension
(file-name-nondirectory file))
(if next-file
(file-name-sans-extension
(file-name-nondirectory next-file)))
prev-grade
(aref spec 1)
(if (nth 1 rest)
(aref (nth 1 rest) 1)))
(call-process "convert" nil nil nil
"-resize" (format "%d%%" (aref spec 0))
file
(expand-file-name
(concat
(file-name-sans-extension
(file-name-nondirectory file)) ".jpg")
(expand-file-name
(symbol-name (aref spec 1))
image-dest-dir)))
(setq prev-grade (aref spec 1))
(setq rest (cdr rest)))
(unless (file-exists-p
(expand-file-name "fullsize" image-dest-dir))
(make-directory
(expand-file-name "fullsize" image-dest-dir)))
(if (string= (downcase (file-name-extension file))
"jpg")
(call-process "ln" nil nil nil
"-f"
file
(expand-file-name "fullsize" image-dest-dir))
(call-process "convert" nil nil nil
file
(expand-file-name
(concat
(file-name-sans-extension
(file-name-nondirectory file)) ".jpg")
(expand-file-name "fullsize" image-dest-dir))))
dest))
(defun album-convert-images (image-dest-dir html-dest-dir lang
image-url-prefix
&rest source-images)
(unless html-dest-dir
(setq html-dest-dir image-dest-dir))
(if (and (consp (car source-images))
(null (cdr source-images)))
(setq source-images (car source-images)))
(album-make-thumbnails html-dest-dir lang image-url-prefix
source-images)
(let (file prev-file)
(while source-images
(setq file (car source-images))
(album-convert-image image-dest-dir html-dest-dir lang
image-url-prefix
prev-file file (nth 1 source-images))
(setq prev-file file
source-images (cdr source-images)))))
(defun album-make-thumbnails (html-dest-dir lang image-url-prefix
source-images)
(let ((album
(file-name-nondirectory
(if (eq (aref html-dest-dir (1- (length html-dest-dir))) ?/)
(substring html-dest-dir 0 (1- (length html-dest-dir)))
html-dest-dir)))
file)
(with-temp-buffer
(insert
"\n")
(insert "\n")
(insert "\n")
(insert (format "%s\n" album))
(insert "\n")
(insert "\n")
(insert (format "%s
\n" album))
(insert "
")
(dolist (image-file source-images)
(setq file (file-name-sans-extension
(file-name-nondirectory image-file)))
(insert "")
(insert (format ""
file
(if image-url-prefix
(format "%s/%s/%s"
image-url-prefix grade file)
file)))
(insert "\n"))
(insert "
")
(write-region (point-min)(point-max)
(expand-file-name "index.html" html-dest-dir)))))
(defun album-convert-directory (image-dest-dir html-dest-dir lang
image-url-prefix
source-dir &rest patterns)
(let (files)
(if patterns
(dolist (pat patterns)
(setq files
(append files
(directory-files source-dir 'full pat))))
(setq files
(let (case-fold-search)
(directory-files
source-dir 'full
".+\\.\\(tiff\\|jpg\\|JPG\\|jpeg\\|gif\\|png\\)$"))))
(album-convert-images image-dest-dir html-dest-dir lang
image-url-prefix files)))
(provide 'album)
;;; album.el ends here