image.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. ;;; image.el --- image API
  2. ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  4. ;; Keywords: multimedia
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (defgroup image ()
  20. "Image support."
  21. :group 'multimedia)
  22. (defalias 'image-refresh 'image-flush)
  23. (defconst image-type-header-regexps
  24. `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
  25. ("\\`P[1-6]\\\(?:\
  26. \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[[:space:]]\\)+\
  27. \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[0-9]\\)+\
  28. \\)\\{2\\}" . pbm)
  29. ("\\`GIF8[79]a" . gif)
  30. ("\\`\x89PNG\r\n\x1a\n" . png)
  31. ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
  32. #define \\1_height [0-9]+\n\\(\
  33. #define \\1_x_hot [0-9]+\n\
  34. #define \\1_y_hot [0-9]+\n\\)?\
  35. static \\(unsigned \\)?char \\1_bits" . xbm)
  36. ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
  37. ("\\`[\t\n\r ]*%!PS" . postscript)
  38. ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg)
  39. (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
  40. (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
  41. (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
  42. comment-re "*"
  43. "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
  44. "[Ss][Vv][Gg]"))
  45. . svg)
  46. )
  47. "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
  48. When the first bytes of an image file match REGEXP, it is assumed to
  49. be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
  50. IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
  51. with one argument, a string containing the image data. If PREDICATE returns
  52. a non-nil value, TYPE is the image's type.")
  53. (defvar image-type-file-name-regexps
  54. '(("\\.png\\'" . png)
  55. ("\\.gif\\'" . gif)
  56. ("\\.jpe?g\\'" . jpeg)
  57. ("\\.bmp\\'" . bmp)
  58. ("\\.xpm\\'" . xpm)
  59. ("\\.pbm\\'" . pbm)
  60. ("\\.xbm\\'" . xbm)
  61. ("\\.ps\\'" . postscript)
  62. ("\\.tiff?\\'" . tiff)
  63. ("\\.svgz?\\'" . svg)
  64. )
  65. "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
  66. When the name of an image file match REGEXP, it is assumed to
  67. be of image type IMAGE-TYPE.")
  68. ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
  69. ;; of content autodetection. Their contents are just C code, so it is
  70. ;; easy to generate false matches.
  71. (defvar image-type-auto-detectable
  72. '((pbm . t)
  73. (xbm . nil)
  74. (bmp . maybe)
  75. (gif . maybe)
  76. (png . maybe)
  77. (xpm . nil)
  78. (jpeg . maybe)
  79. (tiff . maybe)
  80. (svg . maybe)
  81. (postscript . nil))
  82. "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
  83. \(See `image-type-auto-detected-p').
  84. AUTODETECT can be
  85. - t always auto-detect.
  86. - nil never auto-detect.
  87. - maybe auto-detect only if the image type is available
  88. (see `image-type-available-p').")
  89. (defvar image-format-suffixes
  90. '((image/x-icon "ico"))
  91. "An alist associating image types with file name suffixes.
  92. This is used as a hint by the ImageMagick library when detecting
  93. the type of image data (that does not have an associated file name).
  94. Each element has the form (MIME-CONTENT-TYPE EXTENSION).
  95. If `create-image' is called with a :format attribute whose value
  96. equals a content-type found in this list, the ImageMagick library is
  97. told that the data would have the associated suffix if saved to a file.")
  98. (defcustom image-load-path
  99. (list (file-name-as-directory (expand-file-name "images" data-directory))
  100. 'data-directory 'load-path)
  101. "List of locations in which to search for image files.
  102. If an element is a string, it defines a directory to search.
  103. If an element is a variable symbol whose value is a string, that
  104. value defines a directory to search.
  105. If an element is a variable symbol whose value is a list, the
  106. value is used as a list of directories to search.
  107. Subdirectories are not automatically included in the search."
  108. :type '(repeat (choice directory variable))
  109. :initialize 'custom-initialize-delay)
  110. (defun image-load-path-for-library (library image &optional path no-error)
  111. "Return a suitable search path for images used by LIBRARY.
  112. It searches for IMAGE in `image-load-path' (excluding
  113. \"`data-directory'/images\") and `load-path', followed by a path
  114. suitable for LIBRARY, which includes \"../../etc/images\" and
  115. \"../etc/images\" relative to the library file itself, and then
  116. in \"`data-directory'/images\".
  117. Then this function returns a list of directories which contains
  118. first the directory in which IMAGE was found, followed by the
  119. value of `load-path'. If PATH is given, it is used instead of
  120. `load-path'.
  121. If NO-ERROR is non-nil and a suitable path can't be found, don't
  122. signal an error. Instead, return a list of directories as before,
  123. except that nil appears in place of the image directory.
  124. Here is an example that uses a common idiom to provide
  125. compatibility with versions of Emacs that lack the variable
  126. `image-load-path':
  127. ;; Shush compiler.
  128. (defvar image-load-path)
  129. (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
  130. (image-load-path (cons (car load-path)
  131. (when (boundp 'image-load-path)
  132. image-load-path))))
  133. (mh-tool-bar-folder-buttons-init))"
  134. (unless library (error "No library specified"))
  135. (unless image (error "No image specified"))
  136. (let (image-directory image-directory-load-path)
  137. ;; Check for images in image-load-path or load-path.
  138. (let ((img image)
  139. (dir (or
  140. ;; Images in image-load-path.
  141. (image-search-load-path image)
  142. ;; Images in load-path.
  143. (locate-library image)))
  144. parent)
  145. ;; Since the image might be in a nested directory (for
  146. ;; example, mail/attach.pbm), adjust `image-directory'
  147. ;; accordingly.
  148. (when dir
  149. (setq dir (file-name-directory dir))
  150. (while (setq parent (file-name-directory img))
  151. (setq img (directory-file-name parent)
  152. dir (expand-file-name "../" dir))))
  153. (setq image-directory-load-path dir))
  154. ;; If `image-directory-load-path' isn't Emacs's image directory,
  155. ;; it's probably a user preference, so use it. Then use a
  156. ;; relative setting if possible; otherwise, use
  157. ;; `image-directory-load-path'.
  158. (cond
  159. ;; User-modified image-load-path?
  160. ((and image-directory-load-path
  161. (not (equal image-directory-load-path
  162. (file-name-as-directory
  163. (expand-file-name "images" data-directory)))))
  164. (setq image-directory image-directory-load-path))
  165. ;; Try relative setting.
  166. ((let (library-name d1ei d2ei)
  167. ;; First, find library in the load-path.
  168. (setq library-name (locate-library library))
  169. (if (not library-name)
  170. (error "Cannot find library %s in load-path" library))
  171. ;; And then set image-directory relative to that.
  172. (setq
  173. ;; Go down 2 levels.
  174. d2ei (file-name-as-directory
  175. (expand-file-name
  176. (concat (file-name-directory library-name) "../../etc/images")))
  177. ;; Go down 1 level.
  178. d1ei (file-name-as-directory
  179. (expand-file-name
  180. (concat (file-name-directory library-name) "../etc/images"))))
  181. (setq image-directory
  182. ;; Set it to nil if image is not found.
  183. (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
  184. ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
  185. ;; Use Emacs's image directory.
  186. (image-directory-load-path
  187. (setq image-directory image-directory-load-path))
  188. (no-error
  189. (message "Could not find image %s for library %s" image library))
  190. (t
  191. (error "Could not find image %s for library %s" image library)))
  192. ;; Return an augmented `path' or `load-path'.
  193. (nconc (list image-directory)
  194. (delete image-directory (copy-sequence (or path load-path))))))
  195. ;; Used to be in image-type-header-regexps, but now not used anywhere
  196. ;; (since 2009-08-28).
  197. (defun image-jpeg-p (data)
  198. "Value is non-nil if DATA, a string, consists of JFIF image data.
  199. We accept the tag Exif because that is the same format."
  200. (setq data (ignore-errors (string-to-unibyte data)))
  201. (when (and data (string-match-p "\\`\xff\xd8" data))
  202. (catch 'jfif
  203. (let ((len (length data)) (i 2))
  204. (while (< i len)
  205. (when (/= (aref data i) #xff)
  206. (throw 'jfif nil))
  207. (setq i (1+ i))
  208. (when (>= (+ i 2) len)
  209. (throw 'jfif nil))
  210. (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
  211. (aref data (+ i 2))))
  212. (code (aref data i)))
  213. (when (and (>= code #xe0) (<= code #xef))
  214. ;; APP0 LEN1 LEN2 "JFIF\0"
  215. (throw 'jfif
  216. (string-match-p "JFIF\\|Exif"
  217. (substring data i (min (+ i nbytes) len)))))
  218. (setq i (+ i 1 nbytes))))))))
  219. ;;;###autoload
  220. (defun image-type-from-data (data)
  221. "Determine the image type from image data DATA.
  222. Value is a symbol specifying the image type or nil if type cannot
  223. be determined."
  224. (let ((types image-type-header-regexps)
  225. type)
  226. (while types
  227. (let ((regexp (car (car types)))
  228. (image-type (cdr (car types))))
  229. (if (or (and (symbolp image-type)
  230. (string-match-p regexp data))
  231. (and (consp image-type)
  232. (funcall (car image-type) data)
  233. (setq image-type (cdr image-type))))
  234. (setq type image-type
  235. types nil)
  236. (setq types (cdr types)))))
  237. type))
  238. ;;;###autoload
  239. (defun image-type-from-buffer ()
  240. "Determine the image type from data in the current buffer.
  241. Value is a symbol specifying the image type or nil if type cannot
  242. be determined."
  243. (let ((types image-type-header-regexps)
  244. type
  245. (opoint (point)))
  246. (goto-char (point-min))
  247. (while types
  248. (let ((regexp (car (car types)))
  249. (image-type (cdr (car types)))
  250. data)
  251. (if (or (and (symbolp image-type)
  252. (looking-at-p regexp))
  253. (and (consp image-type)
  254. (funcall (car image-type)
  255. (or data
  256. (setq data
  257. (buffer-substring
  258. (point-min)
  259. (min (point-max)
  260. (+ (point-min) 256))))))
  261. (setq image-type (cdr image-type))))
  262. (setq type image-type
  263. types nil)
  264. (setq types (cdr types)))))
  265. (goto-char opoint)
  266. (and type
  267. (boundp 'image-types)
  268. (memq type image-types)
  269. type)))
  270. ;;;###autoload
  271. (defun image-type-from-file-header (file)
  272. "Determine the type of image file FILE from its first few bytes.
  273. Value is a symbol specifying the image type, or nil if type cannot
  274. be determined."
  275. (unless (or (file-readable-p file)
  276. (file-name-absolute-p file))
  277. (setq file (image-search-load-path file)))
  278. (and file
  279. (file-readable-p file)
  280. (with-temp-buffer
  281. (set-buffer-multibyte nil)
  282. (insert-file-contents-literally file nil 0 256)
  283. (image-type-from-buffer))))
  284. ;;;###autoload
  285. (defun image-type-from-file-name (file)
  286. "Determine the type of image file FILE from its name.
  287. Value is a symbol specifying the image type, or nil if type cannot
  288. be determined."
  289. (let (type first)
  290. (catch 'found
  291. (dolist (elem image-type-file-name-regexps first)
  292. (when (string-match-p (car elem) file)
  293. (if (image-type-available-p (setq type (cdr elem)))
  294. (throw 'found type)
  295. ;; If nothing seems to be supported, return first type that matched.
  296. (or first (setq first type))))))))
  297. ;;;###autoload
  298. (defun image-type (source &optional type data-p)
  299. "Determine and return image type.
  300. SOURCE is an image file name or image data.
  301. Optional TYPE is a symbol describing the image type. If TYPE is omitted
  302. or nil, try to determine the image type from its first few bytes
  303. of image data. If that doesn't work, and SOURCE is a file name,
  304. use its file extension as image type.
  305. Optional DATA-P non-nil means SOURCE is a string containing image data."
  306. (when (and (not data-p) (not (stringp source)))
  307. (error "Invalid image file name ‘%s’" source))
  308. (unless type
  309. (setq type (if data-p
  310. (image-type-from-data source)
  311. (or (image-type-from-file-header source)
  312. (image-type-from-file-name source))))
  313. (or type (error "Cannot determine image type")))
  314. (or (memq type (and (boundp 'image-types) image-types))
  315. (error "Invalid image type ‘%s’" type))
  316. type)
  317. (if (fboundp 'image-metadata) ; eg not --without-x
  318. (define-obsolete-function-alias 'image-extension-data
  319. 'image-metadata "24.1"))
  320. (define-obsolete-variable-alias
  321. 'image-library-alist
  322. 'dynamic-library-alist "24.1")
  323. ;;;###autoload
  324. (defun image-type-available-p (type)
  325. "Return non-nil if image type TYPE is available.
  326. Image types are symbols like `xbm' or `jpeg'."
  327. (and (fboundp 'init-image-library)
  328. (init-image-library type)))
  329. ;;;###autoload
  330. (defun image-type-auto-detected-p ()
  331. "Return t if the current buffer contains an auto-detectable image.
  332. This function is intended to be used from `magic-fallback-mode-alist'.
  333. The buffer is considered to contain an auto-detectable image if
  334. its beginning matches an image type in `image-type-header-regexps',
  335. and that image type is present in `image-type-auto-detectable' with a
  336. non-nil value. If that value is non-nil, but not t, then the image type
  337. must be available."
  338. (let* ((type (image-type-from-buffer))
  339. (auto (and type (cdr (assq type image-type-auto-detectable)))))
  340. (and auto
  341. (or (eq auto t) (image-type-available-p type)))))
  342. ;;;###autoload
  343. (defun create-image (file-or-data &optional type data-p &rest props)
  344. "Create an image.
  345. FILE-OR-DATA is an image file name or image data.
  346. Optional TYPE is a symbol describing the image type. If TYPE is omitted
  347. or nil, try to determine the image type from its first few bytes
  348. of image data. If that doesn't work, and FILE-OR-DATA is a file name,
  349. use its file extension as image type.
  350. Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
  351. Optional PROPS are additional image attributes to assign to the image,
  352. like, e.g. `:mask MASK'.
  353. Value is the image created, or nil if images of type TYPE are not supported.
  354. Images should not be larger than specified by `max-image-size'.
  355. Image file names that are not absolute are searched for in the
  356. \"images\" sub-directory of `data-directory' and
  357. `x-bitmap-file-path' (in that order)."
  358. ;; It is x_find_image_file in image.c that sets the search path.
  359. (setq type (image-type file-or-data type data-p))
  360. (when (image-type-available-p type)
  361. (append (list 'image :type type (if data-p :data :file) file-or-data)
  362. props)))
  363. ;;;###autoload
  364. (defun put-image (image pos &optional string area)
  365. "Put image IMAGE in front of POS in the current buffer.
  366. IMAGE must be an image created with `create-image' or `defimage'.
  367. IMAGE is displayed by putting an overlay into the current buffer with a
  368. `before-string' STRING that has a `display' property whose value is the
  369. image. STRING is defaulted if you omit it.
  370. The overlay created will have the `put-image' property set to t.
  371. POS may be an integer or marker.
  372. AREA is where to display the image. AREA nil or omitted means
  373. display it in the text area, a value of `left-margin' means
  374. display it in the left marginal area, a value of `right-margin'
  375. means display it in the right marginal area."
  376. (unless string (setq string "x"))
  377. (let ((buffer (current-buffer)))
  378. (unless (eq (car-safe image) 'image)
  379. (error "Not an image: %s" image))
  380. (unless (or (null area) (memq area '(left-margin right-margin)))
  381. (error "Invalid area %s" area))
  382. (setq string (copy-sequence string))
  383. (let ((overlay (make-overlay pos pos buffer))
  384. (prop (if (null area) image (list (list 'margin area) image))))
  385. (put-text-property 0 (length string) 'display prop string)
  386. (overlay-put overlay 'put-image t)
  387. (overlay-put overlay 'before-string string)
  388. overlay)))
  389. ;;;###autoload
  390. (defun insert-image (image &optional string area slice)
  391. "Insert IMAGE into current buffer at point.
  392. IMAGE is displayed by inserting STRING into the current buffer
  393. with a `display' property whose value is the image. STRING
  394. defaults to a single space if you omit it.
  395. AREA is where to display the image. AREA nil or omitted means
  396. display it in the text area, a value of `left-margin' means
  397. display it in the left marginal area, a value of `right-margin'
  398. means display it in the right marginal area.
  399. SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
  400. means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
  401. specifying the X and Y positions and WIDTH and HEIGHT of image area
  402. to insert. A float value 0.0 - 1.0 means relative to the width or
  403. height of the image; integer values are taken as pixel values."
  404. ;; Use a space as least likely to cause trouble when it's a hidden
  405. ;; character in the buffer.
  406. (unless string (setq string " "))
  407. (unless (eq (car-safe image) 'image)
  408. (error "Not an image: %s" image))
  409. (unless (or (null area) (memq area '(left-margin right-margin)))
  410. (error "Invalid area %s" area))
  411. (if area
  412. (setq image (list (list 'margin area) image))
  413. ;; Cons up a new spec equal but not eq to `image' so that
  414. ;; inserting it twice in a row (adjacently) displays two copies of
  415. ;; the image. Don't try to avoid this by looking at the display
  416. ;; properties on either side so that we DTRT more often with
  417. ;; cut-and-paste. (Yanking killed image text next to another copy
  418. ;; of it loses anyway.)
  419. (setq image (cons 'image (cdr image))))
  420. (let ((start (point)))
  421. (insert string)
  422. (add-text-properties start (point)
  423. `(display ,(if slice
  424. (list (cons 'slice slice) image)
  425. image) rear-nonsticky (display)))))
  426. ;;;###autoload
  427. (defun insert-sliced-image (image &optional string area rows cols)
  428. "Insert IMAGE into current buffer at point.
  429. IMAGE is displayed by inserting STRING into the current buffer
  430. with a `display' property whose value is the image. The default
  431. STRING is a single space.
  432. AREA is where to display the image. AREA nil or omitted means
  433. display it in the text area, a value of `left-margin' means
  434. display it in the left marginal area, a value of `right-margin'
  435. means display it in the right marginal area.
  436. The image is automatically split into ROWS x COLS slices."
  437. (unless string (setq string " "))
  438. (unless (eq (car-safe image) 'image)
  439. (error "Not an image: %s" image))
  440. (unless (or (null area) (memq area '(left-margin right-margin)))
  441. (error "Invalid area %s" area))
  442. (if area
  443. (setq image (list (list 'margin area) image))
  444. ;; Cons up a new spec equal but not eq to `image' so that
  445. ;; inserting it twice in a row (adjacently) displays two copies of
  446. ;; the image. Don't try to avoid this by looking at the display
  447. ;; properties on either side so that we DTRT more often with
  448. ;; cut-and-paste. (Yanking killed image text next to another copy
  449. ;; of it loses anyway.)
  450. (setq image (cons 'image (cdr image))))
  451. (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
  452. (y 0.0) (dy (/ 1.0001 (or rows 1))))
  453. (while (< y 1.0)
  454. (while (< x 1.0)
  455. (let ((start (point)))
  456. (insert string)
  457. (add-text-properties start (point)
  458. `(display ,(list (list 'slice x y dx dy) image)
  459. rear-nonsticky (display)))
  460. (setq x (+ x dx))))
  461. (setq x 0.0
  462. y (+ y dy))
  463. (insert (propertize "\n" 'line-height t)))))
  464. ;;;###autoload
  465. (defun remove-images (start end &optional buffer)
  466. "Remove images between START and END in BUFFER.
  467. Remove only images that were put in BUFFER with calls to `put-image'.
  468. BUFFER nil or omitted means use the current buffer."
  469. (unless buffer
  470. (setq buffer (current-buffer)))
  471. (let ((overlays (overlays-in start end)))
  472. (while overlays
  473. (let ((overlay (car overlays)))
  474. (when (overlay-get overlay 'put-image)
  475. (delete-overlay overlay)))
  476. (setq overlays (cdr overlays)))))
  477. (defun image-search-load-path (file &optional path)
  478. (unless path
  479. (setq path image-load-path))
  480. (let (element found filename)
  481. (while (and (not found) (consp path))
  482. (setq element (car path))
  483. (cond
  484. ((stringp element)
  485. (setq found
  486. (file-readable-p
  487. (setq filename (expand-file-name file element)))))
  488. ((and (symbolp element) (boundp element))
  489. (setq element (symbol-value element))
  490. (cond
  491. ((stringp element)
  492. (setq found
  493. (file-readable-p
  494. (setq filename (expand-file-name file element)))))
  495. ((consp element)
  496. (if (setq filename (image-search-load-path file element))
  497. (setq found t))))))
  498. (setq path (cdr path)))
  499. (if found filename)))
  500. ;;;###autoload
  501. (defun find-image (specs)
  502. "Find an image, choosing one of a list of image specifications.
  503. SPECS is a list of image specifications.
  504. Each image specification in SPECS is a property list. The contents of
  505. a specification are image type dependent. All specifications must at
  506. least contain the properties `:type TYPE' and either `:file FILE' or
  507. `:data DATA', where TYPE is a symbol specifying the image type,
  508. e.g. `xbm', FILE is the file to load the image from, and DATA is a
  509. string containing the actual image data. The specification whose TYPE
  510. is supported, and FILE exists, is used to construct the image
  511. specification to be returned. Return nil if no specification is
  512. satisfied.
  513. The image is looked for in `image-load-path'.
  514. Image files should not be larger than specified by `max-image-size'."
  515. (let (image)
  516. (while (and specs (null image))
  517. (let* ((spec (car specs))
  518. (type (plist-get spec :type))
  519. (data (plist-get spec :data))
  520. (file (plist-get spec :file))
  521. found)
  522. (when (image-type-available-p type)
  523. (cond ((stringp file)
  524. (if (setq found (image-search-load-path file))
  525. (setq image
  526. (cons 'image (plist-put (copy-sequence spec)
  527. :file found)))))
  528. ((not (null data))
  529. (setq image (cons 'image spec)))))
  530. (setq specs (cdr specs))))
  531. image))
  532. ;;;###autoload
  533. (defmacro defimage (symbol specs &optional doc)
  534. "Define SYMBOL as an image, and return SYMBOL.
  535. SPECS is a list of image specifications. DOC is an optional
  536. documentation string.
  537. Each image specification in SPECS is a property list. The contents of
  538. a specification are image type dependent. All specifications must at
  539. least contain the properties `:type TYPE' and either `:file FILE' or
  540. `:data DATA', where TYPE is a symbol specifying the image type,
  541. e.g. `xbm', FILE is the file to load the image from, and DATA is a
  542. string containing the actual image data. The first image
  543. specification whose TYPE is supported, and FILE exists, is used to
  544. define SYMBOL.
  545. Example:
  546. (defimage test-image ((:type xpm :file \"~/test1.xpm\")
  547. (:type xbm :file \"~/test1.xbm\")))"
  548. (declare (doc-string 3))
  549. `(defvar ,symbol (find-image ',specs) ,doc))
  550. ;;; Animated image API
  551. (defvar image-default-frame-delay 0.1
  552. "Default interval in seconds between frames of a multi-frame image.
  553. Only used if the image does not specify a value.")
  554. (defun image-multi-frame-p (image)
  555. "Return non-nil if IMAGE contains more than one frame.
  556. The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
  557. the number of frames (or sub-images) in the image and DELAY is the delay
  558. in seconds that the image specifies between each frame. DELAY may be nil,
  559. in which case you might want to use `image-default-frame-delay'."
  560. (when (fboundp 'image-metadata)
  561. (let* ((metadata (image-metadata image))
  562. (images (plist-get metadata 'count))
  563. (delay (plist-get metadata 'delay)))
  564. (when (and images (> images 1))
  565. (and delay (or (not (numberp delay)) (< delay 0))
  566. (setq delay image-default-frame-delay))
  567. (cons images delay)))))
  568. (defun image-animated-p (image)
  569. "Like `image-multi-frame-p', but returns nil if no delay is specified."
  570. (let ((multi (image-multi-frame-p image)))
  571. (and (cdr multi) multi)))
  572. (make-obsolete 'image-animated-p 'image-multi-frame-p "24.4")
  573. ;; "Destructively"?
  574. (defun image-animate (image &optional index limit)
  575. "Start animating IMAGE.
  576. Animation occurs by destructively altering the IMAGE spec list.
  577. With optional INDEX, begin animating from that animation frame.
  578. LIMIT specifies how long to animate the image. If omitted or
  579. nil, play the animation until the end. If t, loop forever. If a
  580. number, play until that number of seconds has elapsed."
  581. (let ((animation (image-multi-frame-p image))
  582. timer)
  583. (when animation
  584. (if (setq timer (image-animate-timer image))
  585. (cancel-timer timer))
  586. (plist-put (cdr image) :animate-buffer (current-buffer))
  587. (run-with-timer 0.2 nil 'image-animate-timeout
  588. image (or index 0) (car animation)
  589. 0 limit))))
  590. (defun image-animate-timer (image)
  591. "Return the animation timer for image IMAGE."
  592. ;; See cancel-function-timers
  593. (let ((tail timer-list) timer)
  594. (while tail
  595. (setq timer (car tail)
  596. tail (cdr tail))
  597. (if (and (eq (timer--function timer) 'image-animate-timeout)
  598. (eq (car-safe (timer--args timer)) image))
  599. (setq tail nil)
  600. (setq timer nil)))
  601. timer))
  602. (defconst image-minimum-frame-delay 0.01
  603. "Minimum interval in seconds between frames of an animated image.")
  604. (defun image-current-frame (image)
  605. "The current frame number of IMAGE, indexed from 0."
  606. (or (plist-get (cdr image) :index) 0))
  607. (defun image-show-frame (image n &optional nocheck)
  608. "Show frame N of IMAGE.
  609. Frames are indexed from 0. Optional argument NOCHECK non-nil means
  610. do not check N is within the range of frames present in the image."
  611. (unless nocheck
  612. (if (< n 0) (setq n 0)
  613. (setq n (min n (1- (car (image-multi-frame-p image)))))))
  614. (plist-put (cdr image) :index n)
  615. (force-window-update))
  616. (defun image-animate-get-speed (image)
  617. "Return the speed factor for animating IMAGE."
  618. (or (plist-get (cdr image) :speed) 1))
  619. (defun image-animate-set-speed (image value &optional multiply)
  620. "Set the speed factor for animating IMAGE to VALUE.
  621. With optional argument MULTIPLY non-nil, treat VALUE as a
  622. multiplication factor for the current value."
  623. (plist-put (cdr image) :speed
  624. (if multiply
  625. (* value (image-animate-get-speed image))
  626. value)))
  627. ;; FIXME? The delay may not be the same for different sub-images,
  628. ;; hence we need to call image-multi-frame-p to return it.
  629. ;; But it also returns count, so why do we bother passing that as an
  630. ;; argument?
  631. (defun image-animate-timeout (image n count time-elapsed limit)
  632. "Display animation frame N of IMAGE.
  633. N=0 refers to the initial animation frame.
  634. COUNT is the total number of frames in the animation.
  635. TIME-ELAPSED is the total time that has elapsed since
  636. `image-animate-start' was called.
  637. LIMIT determines when to stop. If t, loop forever. If nil, stop
  638. after displaying the last animation frame. Otherwise, stop
  639. after LIMIT seconds have elapsed.
  640. The minimum delay between successive frames is `image-minimum-frame-delay'.
  641. If the image has a non-nil :speed property, it acts as a multiplier
  642. for the animation speed. A negative value means to animate in reverse."
  643. (when (buffer-live-p (plist-get (cdr image) :animate-buffer))
  644. (image-show-frame image n t)
  645. (let* ((speed (image-animate-get-speed image))
  646. (time (float-time))
  647. (animation (image-multi-frame-p image))
  648. ;; Subtract off the time we took to load the image from the
  649. ;; stated delay time.
  650. (delay (max (+ (* (or (cdr animation) image-default-frame-delay)
  651. (/ 1 (abs speed)))
  652. time (- (float-time)))
  653. image-minimum-frame-delay))
  654. done)
  655. (setq n (if (< speed 0)
  656. (1- n)
  657. (1+ n)))
  658. (if limit
  659. (cond ((>= n count) (setq n 0))
  660. ((< n 0) (setq n (1- count))))
  661. (and (or (>= n count) (< n 0)) (setq done t)))
  662. (setq time-elapsed (+ delay time-elapsed))
  663. (if (numberp limit)
  664. (setq done (>= time-elapsed limit)))
  665. (unless done
  666. (run-with-timer delay nil 'image-animate-timeout
  667. image n count time-elapsed limit)))))
  668. (defvar imagemagick-types-inhibit)
  669. (defvar imagemagick-enabled-types)
  670. (defun imagemagick-filter-types ()
  671. "Return a list of the ImageMagick types to be treated as images, or nil.
  672. This is the result of `imagemagick-types', including only elements
  673. that match `imagemagick-enabled-types' and do not match
  674. `imagemagick-types-inhibit'."
  675. (when (fboundp 'imagemagick-types)
  676. (cond ((null imagemagick-enabled-types) nil)
  677. ((eq imagemagick-types-inhibit t) nil)
  678. (t
  679. (delq nil
  680. (mapcar
  681. (lambda (type)
  682. (unless (memq type imagemagick-types-inhibit)
  683. (if (eq imagemagick-enabled-types t) type
  684. (catch 'found
  685. (dolist (enable imagemagick-enabled-types nil)
  686. (if (cond ((symbolp enable) (eq enable type))
  687. ((stringp enable)
  688. (string-match enable
  689. (symbol-name type))))
  690. (throw 'found type)))))))
  691. (imagemagick-types)))))))
  692. (defvar imagemagick--file-regexp nil
  693. "File extension regexp for ImageMagick files, if any.
  694. This is the extension installed into `auto-mode-alist' and
  695. `image-type-file-name-regexps' by `imagemagick-register-types'.")
  696. ;;;###autoload
  697. (defun imagemagick-register-types ()
  698. "Register file types that can be handled by ImageMagick.
  699. This function is called at startup, after loading the init file.
  700. It registers the ImageMagick types returned by `imagemagick-filter-types'.
  701. Registered image types are added to `auto-mode-alist', so that
  702. Emacs visits them in Image mode. They are also added to
  703. `image-type-file-name-regexps', so that the `image-type' function
  704. recognizes these files as having image type `imagemagick'.
  705. If Emacs is compiled without ImageMagick support, this does nothing."
  706. (when (fboundp 'imagemagick-types)
  707. (let* ((types (mapcar (lambda (type) (downcase (symbol-name type)))
  708. (imagemagick-filter-types)))
  709. (re (if types (concat "\\." (regexp-opt types) "\\'")))
  710. (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
  711. auto-mode-alist)))
  712. (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
  713. image-type-file-name-regexps))))
  714. (if (not re)
  715. (setq auto-mode-alist (delete ama-elt auto-mode-alist)
  716. image-type-file-name-regexps
  717. (delete itfnr-elt image-type-file-name-regexps))
  718. (if ama-elt
  719. (setcar ama-elt re)
  720. (push (cons re 'image-mode) auto-mode-alist))
  721. (if itfnr-elt
  722. (setcar itfnr-elt re)
  723. ;; Append to `image-type-file-name-regexps', so that we
  724. ;; preferentially use specialized image libraries.
  725. (add-to-list 'image-type-file-name-regexps
  726. (cons re 'imagemagick) t)))
  727. (setq imagemagick--file-regexp re))))
  728. (defcustom imagemagick-types-inhibit
  729. '(C HTML HTM INFO M TXT PDF)
  730. "List of ImageMagick types that should never be treated as images.
  731. This should be a list of symbols, each of which should be one of
  732. the ImageMagick types listed by `imagemagick-types'. The listed
  733. image types are not registered by `imagemagick-register-types'.
  734. If the value is t, inhibit the use of ImageMagick for images.
  735. If you change this without using customize, you must call
  736. `imagemagick-register-types' afterwards.
  737. If Emacs is compiled without ImageMagick support, this variable
  738. has no effect."
  739. :type '(choice (const :tag "Support all ImageMagick types" nil)
  740. (const :tag "Disable all ImageMagick types" t)
  741. (repeat symbol))
  742. :initialize 'custom-initialize-default
  743. :set (lambda (symbol value)
  744. (set-default symbol value)
  745. (imagemagick-register-types))
  746. :version "24.3"
  747. :group 'image)
  748. (defcustom imagemagick-enabled-types
  749. '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
  750. CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
  751. GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
  752. KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
  753. PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
  754. PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
  755. SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
  756. UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
  757. XWD YCbCr YCbCrA YUV)
  758. "List of ImageMagick types to treat as images.
  759. Each list element should be a string or symbol, representing one
  760. of the image types returned by `imagemagick-types'. If the
  761. element is a string, it is handled as a regexp that enables all
  762. matching types.
  763. The value of `imagemagick-enabled-types' may also be t, meaning
  764. to enable all types that ImageMagick supports.
  765. The variable `imagemagick-types-inhibit' overrides this variable.
  766. If you change this without using customize, you must call
  767. `imagemagick-register-types' afterwards.
  768. If Emacs is compiled without ImageMagick support, this variable
  769. has no effect."
  770. :type '(choice (const :tag "Support all ImageMagick types" t)
  771. (const :tag "Disable all ImageMagick types" nil)
  772. (repeat :tag "List of types"
  773. (choice (symbol :tag "type")
  774. (regexp :tag "regexp"))))
  775. :initialize 'custom-initialize-default
  776. :set (lambda (symbol value)
  777. (set-default symbol value)
  778. (imagemagick-register-types))
  779. :version "24.3"
  780. :group 'image)
  781. (imagemagick-register-types)
  782. (provide 'image)
  783. ;;; image.el ends here