2
|
1 ;;; images.el --- Automatic image converters
|
0
|
2 ;; Author: wmperry
|
22
|
3 ;; Created: 1997/02/13 15:01:57
|
|
4 ;; Version: 1.8
|
0
|
5 ;; Keywords: images
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1995 - 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
16
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation Inc.
|
0
|
10 ;;;
|
14
|
11 ;;; This file is part of GNU Emacs.
|
0
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
14
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
0
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 ;;; The emacsen compatibility package - load it up before anything else
|
|
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
32 (eval-and-compile
|
20
|
33 (if (not (and (string-match "XEmacs" emacs-version)
|
|
34 (or (> emacs-major-version 19)
|
|
35 (>= emacs-minor-version 14))))
|
|
36 (require 'w3-sysdp)))
|
0
|
37
|
|
38 (defvar image-temp-stack nil "Do no touch - internal storage.")
|
|
39 (defvar image-converters nil "Storage for the image converters.")
|
|
40 (defvar image-native-formats
|
|
41 (delq nil (cons (if (featurep 'x) 'xbm)
|
|
42 (mapcar (function (lambda (x) (if (featurep x) x)))
|
|
43 '(xpm gif jpeg tiff png))))
|
|
44 "A list of image formats that this version of emacs supports natively.")
|
|
45
|
|
46 (defun image-register-converter (from to converter)
|
|
47 "Register the image converter for FROM to TO. CONVERTER is the actual
|
|
48 command used to convert the image. If this is a string, it will be executed
|
|
49 in a subprocess. If a symbol, it is assumed to be a function. It will be
|
|
50 called with two arguments, the start and end of the data to be converted.
|
|
51 The function should replace that data with the new image data. The return
|
|
52 value is not significant."
|
|
53 (let* ((node (assq from image-converters))
|
|
54 (replace (assq to (cdr-safe node))))
|
|
55 (cond
|
|
56 (replace ; Replace existing converter
|
|
57 (setcdr replace converter)
|
|
58 (display-warning 'image (format "Replacing image converter %s->%s"
|
|
59 from to)))
|
|
60 (node ; Add to existing node
|
|
61 (setcdr node (cons (cons to converter) (cdr node))))
|
|
62 (t ; New toplevel converter
|
|
63 (setq image-converters (cons (cons from (list (cons to converter)))
|
|
64 image-converters))))))
|
|
65
|
|
66 (defun image-unregister-converter (from to)
|
|
67 "Unregister the image converter for FROM to TO"
|
|
68 (let* ((node (assq from image-converters))
|
|
69 (tos (cdr-safe node))
|
|
70 (new nil))
|
|
71 (while tos
|
|
72 (if (eq to (car (car tos)))
|
|
73 nil
|
|
74 (setq new (cons (car tos) new)))
|
|
75 (setq tos (cdr tos)))
|
|
76 (setcdr node new)))
|
|
77
|
|
78 (defun image-converter-registered-p (from to)
|
|
79 (cdr-safe (assq to (cdr-safe (assq from image-converters)))))
|
|
80
|
|
81 (defun image-converter-chain (from to)
|
|
82 "Return the shortest converter chain for image format FROM to TO"
|
|
83 (setq image-temp-stack (cons from image-temp-stack))
|
|
84 (let ((converters (cdr-safe (assq from image-converters)))
|
|
85 (thisone nil)
|
|
86 (possibles nil)
|
|
87 (done nil))
|
|
88 (while (and (not done) converters)
|
|
89 (setq thisone (car converters))
|
|
90 (cond
|
|
91 ((eq (car thisone) to)
|
|
92 (setq done t))
|
|
93 ((memq (car thisone) image-temp-stack)
|
|
94 nil)
|
|
95 (t
|
|
96 (setq possibles (cons (image-converter-chain (car thisone) to)
|
|
97 possibles))))
|
|
98 (setq converters (cdr converters)))
|
|
99 (setq image-temp-stack (cdr image-temp-stack)
|
|
100 possibles (sort (delq nil possibles)
|
|
101 (function
|
|
102 (lambda (x y)
|
|
103 (< (length (delete 'ignore x))
|
|
104 (length (delete 'ignore y)))))))
|
|
105 (if (not done)
|
|
106 (setq done (car possibles)))
|
|
107 (cond
|
|
108 ((eq done t) (list (cdr thisone)))
|
|
109 (done (setq done (cons (cdr thisone) done)))
|
|
110 (t nil))))
|
|
111
|
|
112 (defun image-normalize (format data)
|
|
113 "Return an image specification for XEmacs 19.13 and later. FORMAT specifies
|
|
114 the image format, DATA is the image data as a string. Any conversions to get
|
|
115 to a suitable internal image format will be carried out."
|
|
116 (setq image-temp-stack nil)
|
|
117 (if (stringp format) (setq format (intern format)))
|
|
118 (if (not (memq format image-native-formats))
|
|
119 (let* ((winner (car-safe
|
|
120 (sort (mapcar
|
|
121 (function
|
|
122 (lambda (x)
|
|
123 (cons x
|
|
124 (delete 'ignore
|
|
125 (image-converter-chain format
|
|
126 x)))))
|
|
127 image-native-formats)
|
|
128 (function
|
|
129 (lambda (x y)
|
|
130 (cond
|
|
131 ((null (cdr x)) nil)
|
|
132 ((= (length (cdr x))
|
|
133 (length (cdr y)))
|
|
134 (< (length (memq (car x)
|
|
135 image-native-formats))
|
|
136 (length (memq (car y)
|
|
137 image-native-formats))))
|
|
138 (t
|
|
139 (< (length (cdr x))
|
|
140 (length (cdr y))))))))))
|
|
141 (type (car-safe winner))
|
|
142 (chain (cdr-safe winner))
|
|
143 )
|
|
144 (if chain
|
|
145 (save-excursion
|
|
146 (set-buffer (generate-new-buffer " *image-conversion*"))
|
|
147 (erase-buffer)
|
|
148 (insert data)
|
|
149 (while chain
|
|
150 (cond
|
|
151 ((stringp (car chain))
|
|
152 (shell-command-on-region (point-min) (point-max)
|
|
153 (concat
|
|
154 "/bin/sh -c '"
|
|
155 (car chain)
|
|
156 " 2> /dev/null"
|
|
157 "'") t))
|
|
158 ((and (symbolp (car chain)) (fboundp (car chain)))
|
|
159 (funcall (car chain) (point-min) (point-max))))
|
|
160 (setq chain (cdr chain)))
|
|
161 (setq data (buffer-string))
|
|
162 (kill-buffer (current-buffer)))
|
|
163 (setq type format))
|
|
164 (vector type ':data data))
|
|
165 (vector format ':data data)))
|
|
166
|
|
167 (defun image-register-netpbm-utilities ()
|
|
168 "Register all the netpbm utility packages converters."
|
|
169 (interactive)
|
22
|
170 (if (image-converter-registered-p 'pgm 'pbm)
|
0
|
171 nil
|
|
172 (image-register-converter 'pgm 'pbm "pgmtopbm")
|
|
173 (image-register-converter 'ppm 'pgm "ppmtopgm")
|
|
174 (image-register-converter 'pnm 'xpm "(ppmquant 256 | ppmtoxpm)")
|
|
175 (image-register-converter 'ppm 'xpm "(ppmquant 256 | ppmtoxpm)")
|
|
176 (image-register-converter 'xpm 'ppm "xpmtoppm")
|
|
177 (image-register-converter 'gif 'ppm "giftopnm")
|
|
178 (image-register-converter 'pnm 'gif "(ppmquant 256 | ppmtogif)")
|
|
179 (image-register-converter 'ppm 'gif "(ppmquant 256 | ppmtogif)")
|
|
180 (image-register-converter 'bmp 'ppm "bmptoppm")
|
|
181 (image-register-converter 'ppm 'bmp "ppmtobmp")
|
|
182 (image-register-converter 'ppm 'ps "pnmtops")
|
|
183 (image-register-converter 'pnm 'ps "pnmtops")
|
|
184 (image-register-converter 'ps 'pnm "pstopnm")
|
|
185 (image-register-converter 'g3 'pbm "g3topbm")
|
|
186 (image-register-converter 'macpt 'pbm "macptopbm")
|
|
187 (image-register-converter 'pbm 'macpt "pbmtomacp")
|
|
188 (image-register-converter 'pcx 'ppm "pcxtoppm")
|
|
189 (image-register-converter 'ppm 'pcx "ppmtopcx")
|
|
190 (image-register-converter 'pict 'ppm "picttoppm")
|
|
191 (image-register-converter 'ppm 'pict "ppmtopict")
|
|
192 (image-register-converter 'pnm 'sgi "pnmtosgi")
|
|
193 (image-register-converter 'tga 'ppm "tgatoppm")
|
|
194 (image-register-converter 'ppm 'tga "ppmtotga")
|
|
195 (image-register-converter 'sgi 'pnm "sgitopnm")
|
|
196 (image-register-converter 'tiff 'pnm "tifftopnm")
|
|
197 (image-register-converter 'pnm 'tiff "pnmtotiff")
|
|
198 (image-register-converter 'xbm 'pbm "xbmtopbm")
|
|
199 (image-register-converter 'pbm 'xbm "pbmtoxbm")
|
|
200 (image-register-converter 'png 'pnm "pngtopnm")
|
|
201 (image-register-converter 'pnm 'png "pnmtopng")
|
|
202 (image-register-converter 'pnm 'jbg "pbmtojbg")
|
|
203 (image-register-converter 'jbg 'pnm "jbgtopbm")
|
|
204 (image-register-converter 'jpeg 'ppm "djpeg")))
|
|
205
|
|
206 (provide 'images)
|