summaryrefslogtreecommitdiff
path: root/src/guile/skribilo/utils/images.in
blob: e8b581f6c568beda66562e43766c7562e88e9a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
;;; images.scm  --  Images handling utilities.
;;;
;;; Copyright 2005, 2006, 2007, 2008  Ludovic Court�s <ludo@gnu.org>
;;; Copyright 2003, 2004  Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo 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 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Skribilo 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 Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo utils images)
  :export (convert-image
	   *fig-convert-program* *generic-convert-program*)

  :autoload (skribilo utils files) (file-suffix file-prefix)
  :autoload (skribilo parameters)  (*image-path* *verbose*)
  :autoload   (skribilo condition) (&file-search-error)
  :autoload   (srfi srfi-34) (raise)
  :use-module (srfi srfi-35)
  :use-module (srfi srfi-39))

;;; Author:  Erick Gallesio, Ludovic Court�s
;;;
;;; Commentary:
;;;
;;; This module provides convenience functions to handle image files, notably
;;; for format conversion via ImageMagick's `convert'.
;;;
;;; Code:

(define *fig-convert-program*     (make-parameter "@FIG2DEV@ -L"))
(define *generic-convert-program* (make-parameter "@CONVERT@"))

(define (builtin-convert-image from fmt dir)
  (let* ((s  (file-suffix from))
	 (f  (string-append (file-prefix (basename from)) "." fmt))
	 (to (string-append dir "/" f)))   ;; FIXME:
    (cond
      ((string=? s fmt)
       to)
      ((file-exists? to)
       to)
      (else
       (let ((c (if (string=? s "fig")
		    (string-append (*fig-convert-program*) " "
				   fmt " " from " > " to)
		    (string-append (*generic-convert-program*) " "
				   from " " to))))
	 (cond
	   ((> (*verbose*) 1)
	    (format (current-error-port) "  [converting image: ~S (~S)]~%" from c))
	   ((> (*verbose*) 0)
	    (format (current-error-port) "  [converting image: ~S]~%" from)))
	 (and (zero? (system c))
	      to))))))

(define (convert-image file formats)
  (let ((path (search-path (*image-path*) file)))
    (if (not path)
	(raise (condition (&file-search-error (file-name file)
					      (path (*image-path*)))))
	(let ((suf (file-suffix file)))
	  (if (member suf formats)
	      (let* ((dir (if (string? (*destination-file*))
			      (dirname (*destination-file*))
			      #f)))
		(if dir
		    (let* ((dest (basename path))
			   (dest-path (string-append dir "/" dest)))
		      (if (and (not (string=? path dest-path))
                               (not (file-exists? dest-path)))
                          (copy-file path dest-path))
		      dest)
		    path))
	      (let loop ((fmts formats))
		(if (null? fmts)
		    #f
		     (let* ((dir (if (string? (*destination-file*))
				     (dirname (*destination-file*))
				     "."))
			    (p (builtin-convert-image path (car fmts) dir)))
		       (if (string? p)
			   p
			   (loop (cdr fmts)))))))))))


;;; Local Variables:
;;; coding: latin-1
;;; End:

;;; images.scm ends here