image.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. ;;; image.el --- image API
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  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][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
  26. ("\\`GIF8[79]a" . gif)
  27. ("\\`\x89PNG\r\n\x1a\n" . png)
  28. ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
  29. #define \\1_height [0-9]+\n\\(\
  30. #define \\1_x_hot [0-9]+\n\
  31. #define \\1_y_hot [0-9]+\n\\)?\
  32. static \\(unsigned \\)?char \\1_bits" . xbm)
  33. ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
  34. ("\\`[\t\n\r ]*%!PS" . postscript)
  35. ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg)
  36. (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
  37. (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
  38. (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
  39. comment-re "*"
  40. "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
  41. "[Ss][Vv][Gg]"))
  42. . svg)
  43. )
  44. "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
  45. When the first bytes of an image file match REGEXP, it is assumed to
  46. be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
  47. IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
  48. with one argument, a string containing the image data. If PREDICATE returns
  49. a non-nil value, TYPE is the image's type.")
  50. (defvar image-type-file-name-regexps
  51. '(("\\.png\\'" . png)
  52. ("\\.gif\\'" . gif)
  53. ("\\.jpe?g\\'" . jpeg)
  54. ("\\.bmp\\'" . bmp)
  55. ("\\.xpm\\'" . xpm)
  56. ("\\.pbm\\'" . pbm)
  57. ("\\.xbm\\'" . xbm)
  58. ("\\.ps\\'" . postscript)
  59. ("\\.tiff?\\'" . tiff)
  60. ("\\.svgz?\\'" . svg)
  61. )
  62. "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
  63. When the name of an image file match REGEXP, it is assumed to
  64. be of image type IMAGE-TYPE.")
  65. ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
  66. ;; of content autodetection. Their contents are just C code, so it is
  67. ;; easy to generate false matches.
  68. (defvar image-type-auto-detectable
  69. '((pbm . t)
  70. (xbm . nil)
  71. (bmp . maybe)
  72. (gif . maybe)
  73. (png . maybe)
  74. (xpm . nil)
  75. (jpeg . maybe)
  76. (tiff . maybe)
  77. (svg . maybe)
  78. (postscript . nil))
  79. "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
  80. \(See `image-type-auto-detected-p').
  81. AUTODETECT can be
  82. - t always auto-detect.
  83. - nil never auto-detect.
  84. - maybe auto-detect only if the image type is available
  85. (see `image-type-available-p').")
  86. (defcustom image-load-path
  87. (list (file-name-as-directory (expand-file-name "images" data-directory))
  88. 'data-directory 'load-path)
  89. "List of locations in which to search for image files.
  90. If an element is a string, it defines a directory to search.
  91. If an element is a variable symbol whose value is a string, that
  92. value defines a directory to search.
  93. If an element is a variable symbol whose value is a list, the
  94. value is used as a list of directories to search."
  95. :type '(repeat (choice directory variable))
  96. :initialize 'custom-initialize-delay)
  97. (defun image-load-path-for-library (library image &optional path no-error)
  98. "Return a suitable search path for images used by LIBRARY.
  99. It searches for IMAGE in `image-load-path' (excluding
  100. \"`data-directory'/images\") and `load-path', followed by a path
  101. suitable for LIBRARY, which includes \"../../etc/images\" and
  102. \"../etc/images\" relative to the library file itself, and then
  103. in \"`data-directory'/images\".
  104. Then this function returns a list of directories which contains
  105. first the directory in which IMAGE was found, followed by the
  106. value of `load-path'. If PATH is given, it is used instead of
  107. `load-path'.
  108. If NO-ERROR is non-nil and a suitable path can't be found, don't
  109. signal an error. Instead, return a list of directories as before,
  110. except that nil appears in place of the image directory.
  111. Here is an example that uses a common idiom to provide
  112. compatibility with versions of Emacs that lack the variable
  113. `image-load-path':
  114. ;; Shush compiler.
  115. (defvar image-load-path)
  116. (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
  117. (image-load-path (cons (car load-path)
  118. (when (boundp 'image-load-path)
  119. image-load-path))))
  120. (mh-tool-bar-folder-buttons-init))"
  121. (unless library (error "No library specified"))
  122. (unless image (error "No image specified"))
  123. (let (image-directory image-directory-load-path)
  124. ;; Check for images in image-load-path or load-path.
  125. (let ((img image)
  126. (dir (or
  127. ;; Images in image-load-path.
  128. (image-search-load-path image)
  129. ;; Images in load-path.
  130. (locate-library image)))
  131. parent)
  132. ;; Since the image might be in a nested directory (for
  133. ;; example, mail/attach.pbm), adjust `image-directory'
  134. ;; accordingly.
  135. (when dir
  136. (setq dir (file-name-directory dir))
  137. (while (setq parent (file-name-directory img))
  138. (setq img (directory-file-name parent)
  139. dir (expand-file-name "../" dir))))
  140. (setq image-directory-load-path dir))
  141. ;; If `image-directory-load-path' isn't Emacs's image directory,
  142. ;; it's probably a user preference, so use it. Then use a
  143. ;; relative setting if possible; otherwise, use
  144. ;; `image-directory-load-path'.
  145. (cond
  146. ;; User-modified image-load-path?
  147. ((and image-directory-load-path
  148. (not (equal image-directory-load-path
  149. (file-name-as-directory
  150. (expand-file-name "images" data-directory)))))
  151. (setq image-directory image-directory-load-path))
  152. ;; Try relative setting.
  153. ((let (library-name d1ei d2ei)
  154. ;; First, find library in the load-path.
  155. (setq library-name (locate-library library))
  156. (if (not library-name)
  157. (error "Cannot find library %s in load-path" library))
  158. ;; And then set image-directory relative to that.
  159. (setq
  160. ;; Go down 2 levels.
  161. d2ei (file-name-as-directory
  162. (expand-file-name
  163. (concat (file-name-directory library-name) "../../etc/images")))
  164. ;; Go down 1 level.
  165. d1ei (file-name-as-directory
  166. (expand-file-name
  167. (concat (file-name-directory library-name) "../etc/images"))))
  168. (setq image-directory
  169. ;; Set it to nil if image is not found.
  170. (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
  171. ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
  172. ;; Use Emacs's image directory.
  173. (image-directory-load-path
  174. (setq image-directory image-directory-load-path))
  175. (no-error
  176. (message "Could not find image %s for library %s" image library))
  177. (t
  178. (error "Could not find image %s for library %s" image library)))
  179. ;; Return an augmented `path' or `load-path'.
  180. (nconc (list image-directory)
  181. (delete image-directory (copy-sequence (or path load-path))))))
  182. ;; Used to be in image-type-header-regexps, but now not used anywhere
  183. ;; (since 2009-08-28).
  184. (defun image-jpeg-p (data)
  185. "Value is non-nil if DATA, a string, consists of JFIF image data.
  186. We accept the tag Exif because that is the same format."
  187. (setq data (ignore-errors (string-to-unibyte data)))
  188. (when (and data (string-match-p "\\`\xff\xd8" data))
  189. (catch 'jfif
  190. (let ((len (length data)) (i 2))
  191. (while (< i len)
  192. (when (/= (aref data i) #xff)
  193. (throw 'jfif nil))
  194. (setq i (1+ i))
  195. (when (>= (+ i 2) len)
  196. (throw 'jfif nil))
  197. (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
  198. (aref data (+ i 2))))
  199. (code (aref data i)))
  200. (when (and (>= code #xe0) (<= code #xef))
  201. ;; APP0 LEN1 LEN2 "JFIF\0"
  202. (throw 'jfif
  203. (string-match-p "JFIF\\|Exif"
  204. (substring data i (min (+ i nbytes) len)))))
  205. (setq i (+ i 1 nbytes))))))))
  206. ;;;###autoload
  207. (defun image-type-from-data (data)
  208. "Determine the image type from image data DATA.
  209. Value is a symbol specifying the image type or nil if type cannot
  210. be determined."
  211. (let ((types image-type-header-regexps)
  212. type)
  213. (while types
  214. (let ((regexp (car (car types)))
  215. (image-type (cdr (car types))))
  216. (if (or (and (symbolp image-type)
  217. (string-match-p regexp data))
  218. (and (consp image-type)
  219. (funcall (car image-type) data)
  220. (setq image-type (cdr image-type))))
  221. (setq type image-type
  222. types nil)
  223. (setq types (cdr types)))))
  224. type))
  225. ;;;###autoload
  226. (defun image-type-from-buffer ()
  227. "Determine the image type from data in the current buffer.
  228. Value is a symbol specifying the image type or nil if type cannot
  229. be determined."
  230. (let ((types image-type-header-regexps)
  231. type
  232. (opoint (point)))
  233. (goto-char (point-min))
  234. (while types
  235. (let ((regexp (car (car types)))
  236. (image-type (cdr (car types)))
  237. data)
  238. (if (or (and (symbolp image-type)
  239. (looking-at-p regexp))
  240. (and (consp image-type)
  241. (funcall (car image-type)
  242. (or data
  243. (setq data
  244. (buffer-substring
  245. (point-min)
  246. (min (point-max)
  247. (+ (point-min) 256))))))
  248. (setq image-type (cdr image-type))))
  249. (setq type image-type
  250. types nil)
  251. (setq types (cdr types)))))
  252. (goto-char opoint)
  253. type))
  254. ;;;###autoload
  255. (defun image-type-from-file-header (file)
  256. "Determine the type of image file FILE from its first few bytes.
  257. Value is a symbol specifying the image type, or nil if type cannot
  258. be determined."
  259. (unless (or (file-readable-p file)
  260. (file-name-absolute-p file))
  261. (setq file (image-search-load-path file)))
  262. (and file
  263. (file-readable-p file)
  264. (with-temp-buffer
  265. (set-buffer-multibyte nil)
  266. (insert-file-contents-literally file nil 0 256)
  267. (image-type-from-buffer))))
  268. ;;;###autoload
  269. (defun image-type-from-file-name (file)
  270. "Determine the type of image file FILE from its name.
  271. Value is a symbol specifying the image type, or nil if type cannot
  272. be determined."
  273. (assoc-default file image-type-file-name-regexps 'string-match-p))
  274. ;;;###autoload
  275. (defun image-type (source &optional type data-p)
  276. "Determine and return image type.
  277. SOURCE is an image file name or image data.
  278. Optional TYPE is a symbol describing the image type. If TYPE is omitted
  279. or nil, try to determine the image type from its first few bytes
  280. of image data. If that doesn't work, and SOURCE is a file name,
  281. use its file extension as image type.
  282. Optional DATA-P non-nil means SOURCE is a string containing image data."
  283. (when (and (not data-p) (not (stringp source)))
  284. (error "Invalid image file name `%s'" source))
  285. (unless type
  286. (setq type (if data-p
  287. (image-type-from-data source)
  288. (or (image-type-from-file-header source)
  289. (image-type-from-file-name source))))
  290. (or type (error "Cannot determine image type")))
  291. (or (memq type (and (boundp 'image-types) image-types))
  292. (error "Invalid image type `%s'" type))
  293. type)
  294. (if (fboundp 'image-metadata) ; eg not --without-x
  295. (define-obsolete-function-alias 'image-extension-data
  296. 'image-metadata' "24.1"))
  297. (define-obsolete-variable-alias
  298. 'image-library-alist
  299. 'dynamic-library-alist "24.1")
  300. ;;;###autoload
  301. (defun image-type-available-p (type)
  302. "Return non-nil if image type TYPE is available.
  303. Image types are symbols like `xbm' or `jpeg'."
  304. (and (fboundp 'init-image-library)
  305. (init-image-library type dynamic-library-alist)))
  306. ;;;###autoload
  307. (defun image-type-auto-detected-p ()
  308. "Return t if the current buffer contains an auto-detectable image.
  309. This function is intended to be used from `magic-fallback-mode-alist'.
  310. The buffer is considered to contain an auto-detectable image if
  311. its beginning matches an image type in `image-type-header-regexps',
  312. and that image type is present in `image-type-auto-detectable' with a
  313. non-nil value. If that value is non-nil, but not t, then the image type
  314. must be available."
  315. (let* ((type (image-type-from-buffer))
  316. (auto (and type (cdr (assq type image-type-auto-detectable)))))
  317. (and auto
  318. (or (eq auto t) (image-type-available-p type)))))
  319. ;;;###autoload
  320. (defun create-image (file-or-data &optional type data-p &rest props)
  321. "Create an image.
  322. FILE-OR-DATA is an image file name or image data.
  323. Optional TYPE is a symbol describing the image type. If TYPE is omitted
  324. or nil, try to determine the image type from its first few bytes
  325. of image data. If that doesn't work, and FILE-OR-DATA is a file name,
  326. use its file extension as image type.
  327. Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
  328. Optional PROPS are additional image attributes to assign to the image,
  329. like, e.g. `:mask MASK'.
  330. Value is the image created, or nil if images of type TYPE are not supported.
  331. Images should not be larger than specified by `max-image-size'.
  332. Image file names that are not absolute are searched for in the
  333. \"images\" sub-directory of `data-directory' and
  334. `x-bitmap-file-path' (in that order)."
  335. ;; It is x_find_image_file in image.c that sets the search path.
  336. (setq type (image-type file-or-data type data-p))
  337. (when (image-type-available-p type)
  338. (append (list 'image :type type (if data-p :data :file) file-or-data)
  339. props)))
  340. ;;;###autoload
  341. (defun put-image (image pos &optional string area)
  342. "Put image IMAGE in front of POS in the current buffer.
  343. IMAGE must be an image created with `create-image' or `defimage'.
  344. IMAGE is displayed by putting an overlay into the current buffer with a
  345. `before-string' STRING that has a `display' property whose value is the
  346. image. STRING is defaulted if you omit it.
  347. The overlay created will have the `put-image' property set to t.
  348. POS may be an integer or marker.
  349. AREA is where to display the image. AREA nil or omitted means
  350. display it in the text area, a value of `left-margin' means
  351. display it in the left marginal area, a value of `right-margin'
  352. means display it in the right marginal area."
  353. (unless string (setq string "x"))
  354. (let ((buffer (current-buffer)))
  355. (unless (eq (car-safe image) 'image)
  356. (error "Not an image: %s" image))
  357. (unless (or (null area) (memq area '(left-margin right-margin)))
  358. (error "Invalid area %s" area))
  359. (setq string (copy-sequence string))
  360. (let ((overlay (make-overlay pos pos buffer))
  361. (prop (if (null area) image (list (list 'margin area) image))))
  362. (put-text-property 0 (length string) 'display prop string)
  363. (overlay-put overlay 'put-image t)
  364. (overlay-put overlay 'before-string string))))
  365. ;;;###autoload
  366. (defun insert-image (image &optional string area slice)
  367. "Insert IMAGE into current buffer at point.
  368. IMAGE is displayed by inserting STRING into the current buffer
  369. with a `display' property whose value is the image. STRING
  370. defaults to the empty string if you omit it.
  371. AREA is where to display the image. AREA nil or omitted means
  372. display it in the text area, a value of `left-margin' means
  373. display it in the left marginal area, a value of `right-margin'
  374. means display it in the right marginal area.
  375. SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
  376. means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
  377. specifying the X and Y positions and WIDTH and HEIGHT of image area
  378. to insert. A float value 0.0 - 1.0 means relative to the width or
  379. height of the image; integer values are taken as pixel values."
  380. ;; Use a space as least likely to cause trouble when it's a hidden
  381. ;; character in the buffer.
  382. (unless string (setq string " "))
  383. (unless (eq (car-safe image) 'image)
  384. (error "Not an image: %s" image))
  385. (unless (or (null area) (memq area '(left-margin right-margin)))
  386. (error "Invalid area %s" area))
  387. (if area
  388. (setq image (list (list 'margin area) image))
  389. ;; Cons up a new spec equal but not eq to `image' so that
  390. ;; inserting it twice in a row (adjacently) displays two copies of
  391. ;; the image. Don't try to avoid this by looking at the display
  392. ;; properties on either side so that we DTRT more often with
  393. ;; cut-and-paste. (Yanking killed image text next to another copy
  394. ;; of it loses anyway.)
  395. (setq image (cons 'image (cdr image))))
  396. (let ((start (point)))
  397. (insert string)
  398. (add-text-properties start (point)
  399. `(display ,(if slice
  400. (list (cons 'slice slice) image)
  401. image) rear-nonsticky (display)))))
  402. ;;;###autoload
  403. (defun insert-sliced-image (image &optional string area rows cols)
  404. "Insert IMAGE into current buffer at point.
  405. IMAGE is displayed by inserting STRING into the current buffer
  406. with a `display' property whose value is the image. STRING is
  407. defaulted if you omit it.
  408. AREA is where to display the image. AREA nil or omitted means
  409. display it in the text area, a value of `left-margin' means
  410. display it in the left marginal area, a value of `right-margin'
  411. means display it in the right marginal area.
  412. The image is automatically split into ROWS x COLS slices."
  413. (unless string (setq string " "))
  414. (unless (eq (car-safe image) 'image)
  415. (error "Not an image: %s" image))
  416. (unless (or (null area) (memq area '(left-margin right-margin)))
  417. (error "Invalid area %s" area))
  418. (if area
  419. (setq image (list (list 'margin area) image))
  420. ;; Cons up a new spec equal but not eq to `image' so that
  421. ;; inserting it twice in a row (adjacently) displays two copies of
  422. ;; the image. Don't try to avoid this by looking at the display
  423. ;; properties on either side so that we DTRT more often with
  424. ;; cut-and-paste. (Yanking killed image text next to another copy
  425. ;; of it loses anyway.)
  426. (setq image (cons 'image (cdr image))))
  427. (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
  428. (y 0.0) (dy (/ 1.0001 (or rows 1))))
  429. (while (< y 1.0)
  430. (while (< x 1.0)
  431. (let ((start (point)))
  432. (insert string)
  433. (add-text-properties start (point)
  434. `(display ,(list (list 'slice x y dx dy) image)
  435. rear-nonsticky (display)))
  436. (setq x (+ x dx))))
  437. (setq x 0.0
  438. y (+ y dy))
  439. (insert (propertize "\n" 'line-height t)))))
  440. ;;;###autoload
  441. (defun remove-images (start end &optional buffer)
  442. "Remove images between START and END in BUFFER.
  443. Remove only images that were put in BUFFER with calls to `put-image'.
  444. BUFFER nil or omitted means use the current buffer."
  445. (unless buffer
  446. (setq buffer (current-buffer)))
  447. (let ((overlays (overlays-in start end)))
  448. (while overlays
  449. (let ((overlay (car overlays)))
  450. (when (overlay-get overlay 'put-image)
  451. (delete-overlay overlay)))
  452. (setq overlays (cdr overlays)))))
  453. (defun image-search-load-path (file &optional path)
  454. (unless path
  455. (setq path image-load-path))
  456. (let (element found filename)
  457. (while (and (not found) (consp path))
  458. (setq element (car path))
  459. (cond
  460. ((stringp element)
  461. (setq found
  462. (file-readable-p
  463. (setq filename (expand-file-name file element)))))
  464. ((and (symbolp element) (boundp element))
  465. (setq element (symbol-value element))
  466. (cond
  467. ((stringp element)
  468. (setq found
  469. (file-readable-p
  470. (setq filename (expand-file-name file element)))))
  471. ((consp element)
  472. (if (setq filename (image-search-load-path file element))
  473. (setq found t))))))
  474. (setq path (cdr path)))
  475. (if found filename)))
  476. ;;;###autoload
  477. (defun find-image (specs)
  478. "Find an image, choosing one of a list of image specifications.
  479. SPECS is a list of image specifications.
  480. Each image specification in SPECS is a property list. The contents of
  481. a specification are image type dependent. All specifications must at
  482. least contain the properties `:type TYPE' and either `:file FILE' or
  483. `:data DATA', where TYPE is a symbol specifying the image type,
  484. e.g. `xbm', FILE is the file to load the image from, and DATA is a
  485. string containing the actual image data. The specification whose TYPE
  486. is supported, and FILE exists, is used to construct the image
  487. specification to be returned. Return nil if no specification is
  488. satisfied.
  489. The image is looked for in `image-load-path'.
  490. Image files should not be larger than specified by `max-image-size'."
  491. (let (image)
  492. (while (and specs (null image))
  493. (let* ((spec (car specs))
  494. (type (plist-get spec :type))
  495. (data (plist-get spec :data))
  496. (file (plist-get spec :file))
  497. found)
  498. (when (image-type-available-p type)
  499. (cond ((stringp file)
  500. (if (setq found (image-search-load-path file))
  501. (setq image
  502. (cons 'image (plist-put (copy-sequence spec)
  503. :file found)))))
  504. ((not (null data))
  505. (setq image (cons 'image spec)))))
  506. (setq specs (cdr specs))))
  507. image))
  508. ;;;###autoload
  509. (defmacro defimage (symbol specs &optional doc)
  510. "Define SYMBOL as an image.
  511. SPECS is a list of image specifications. DOC is an optional
  512. documentation string.
  513. Each image specification in SPECS is a property list. The contents of
  514. a specification are image type dependent. All specifications must at
  515. least contain the properties `:type TYPE' and either `:file FILE' or
  516. `:data DATA', where TYPE is a symbol specifying the image type,
  517. e.g. `xbm', FILE is the file to load the image from, and DATA is a
  518. string containing the actual image data. The first image
  519. specification whose TYPE is supported, and FILE exists, is used to
  520. define SYMBOL.
  521. Example:
  522. (defimage test-image ((:type xpm :file \"~/test1.xpm\")
  523. (:type xbm :file \"~/test1.xbm\")))"
  524. (declare (doc-string 3))
  525. `(defvar ,symbol (find-image ',specs) ,doc))
  526. ;;; Animated image API
  527. (defconst image-animated-types '(gif)
  528. "List of supported animated image types.")
  529. (defun image-animated-p (image)
  530. "Return non-nil if IMAGE can be animated.
  531. To be capable of being animated, an image must be of a type
  532. listed in `image-animated-types', and contain more than one
  533. sub-image, with a specified animation delay. The actual return
  534. value is a cons (NIMAGES . DELAY), where NIMAGES is the number
  535. of sub-images in the animated image and DELAY is the delay in
  536. seconds until the next sub-image should be displayed."
  537. (cond
  538. ((memq (plist-get (cdr image) :type) image-animated-types)
  539. (let* ((metadata (image-metadata image))
  540. (images (plist-get metadata 'count))
  541. (delay (plist-get metadata 'delay)))
  542. (when (and images (> images 1) (numberp delay))
  543. (if (< delay 0) (setq delay 0.1))
  544. (cons images delay))))))
  545. ;; "Destructively"?
  546. (defun image-animate (image &optional index limit)
  547. "Start animating IMAGE.
  548. Animation occurs by destructively altering the IMAGE spec list.
  549. With optional INDEX, begin animating from that animation frame.
  550. LIMIT specifies how long to animate the image. If omitted or
  551. nil, play the animation until the end. If t, loop forever. If a
  552. number, play until that number of seconds has elapsed."
  553. (let ((animation (image-animated-p image))
  554. timer)
  555. (when animation
  556. (if (setq timer (image-animate-timer image))
  557. (cancel-timer timer))
  558. (run-with-timer 0.2 nil 'image-animate-timeout
  559. image (or index 0) (car animation)
  560. 0 limit))))
  561. (defun image-animate-timer (image)
  562. "Return the animation timer for image IMAGE."
  563. ;; See cancel-function-timers
  564. (let ((tail timer-list) timer)
  565. (while tail
  566. (setq timer (car tail)
  567. tail (cdr tail))
  568. (if (and (eq (aref timer 5) 'image-animate-timeout)
  569. (eq (car-safe (aref timer 6)) image))
  570. (setq tail nil)
  571. (setq timer nil)))
  572. timer))
  573. ;; FIXME? The delay may not be the same for different sub-images,
  574. ;; hence we need to call image-animated-p to return it.
  575. ;; But it also returns count, so why do we bother passing that as an
  576. ;; argument?
  577. (defun image-animate-timeout (image n count time-elapsed limit)
  578. "Display animation frame N of IMAGE.
  579. N=0 refers to the initial animation frame.
  580. COUNT is the total number of frames in the animation.
  581. TIME-ELAPSED is the total time that has elapsed since
  582. `image-animate-start' was called.
  583. LIMIT determines when to stop. If t, loop forever. If nil, stop
  584. after displaying the last animation frame. Otherwise, stop
  585. after LIMIT seconds have elapsed.
  586. The minimum delay between successive frames is 0.01s."
  587. (plist-put (cdr image) :index n)
  588. (force-window-update)
  589. (setq n (1+ n))
  590. (let* ((time (float-time))
  591. (animation (image-animated-p image))
  592. ;; Subtract off the time we took to load the image from the
  593. ;; stated delay time.
  594. (delay (max (+ (cdr animation) time (- (float-time)))
  595. 0.01))
  596. done)
  597. (if (>= n count)
  598. (if limit
  599. (setq n 0)
  600. (setq done t)))
  601. (setq time-elapsed (+ delay time-elapsed))
  602. (if (numberp limit)
  603. (setq done (>= time-elapsed limit)))
  604. (unless done
  605. (run-with-timer delay nil 'image-animate-timeout
  606. image n count time-elapsed limit))))
  607. (defcustom imagemagick-types-inhibit
  608. '(C HTML HTM TXT PDF)
  609. "ImageMagick types that should not be visited in Image mode.
  610. This should be a list of symbols, each of which should be one of
  611. the ImageMagick types listed in `imagemagick-types'. These image
  612. types are not registered by `imagemagick-register-types'.
  613. If Emacs is compiled without ImageMagick support, this variable
  614. has no effect."
  615. :type '(choice (const :tag "Let ImageMagick handle all types it can" nil)
  616. (repeat symbol))
  617. ;; Ideally, would have a :set function that checks if we already did
  618. ;; imagemagick-register-types, and if so undoes it, then redoes it.
  619. :version "24.1"
  620. :group 'image)
  621. ;;;###autoload
  622. (defun imagemagick-register-types ()
  623. "Register file types that can be handled by ImageMagick.
  624. This registers the ImageMagick types listed in `imagemagick-types',
  625. excluding those listed in `imagemagick-types-inhibit'.
  626. Registered image types are added to `auto-mode-alist', so that
  627. Emacs visits them in Image mode. They are also added to
  628. `image-type-file-name-regexps', so that the `image-type' function
  629. recognizes these files as having image type `imagemagick'.
  630. If Emacs is compiled without ImageMagick support, do nothing."
  631. (when (fboundp 'imagemagick-types)
  632. (let ((im-types '()))
  633. (dolist (im-type (imagemagick-types))
  634. (unless (memq im-type imagemagick-types-inhibit)
  635. (push (downcase (symbol-name im-type)) im-types)))
  636. (let ((extension (concat "\\." (regexp-opt im-types) "\\'")))
  637. (push (cons extension 'image-mode) auto-mode-alist)
  638. (push (cons extension 'imagemagick)
  639. image-type-file-name-regexps)))))
  640. (provide 'image)
  641. ;;; image.el ends here