lisp/image-mode.el (image-transform-check-size): Use assertions only
[emacs.git] / lisp / image-mode.el
blob0e91567a29a005537016b6dbb6769a5bb924f203
1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Defines a major mode for visiting image files
27 ;; that allows conversion between viewing the text of the file
28 ;; and viewing the file as an image. Viewing the image
29 ;; works by putting a `display' text-property on the
30 ;; image data, with the image-data still present underneath; if the
31 ;; resulting buffer file is saved to another name it will correctly save
32 ;; the image data to the new file.
34 ;; Todo:
36 ;; Consolidate with doc-view to make them work on directories of images or on
37 ;; image files containing various "pages".
39 ;;; Code:
41 (require 'image)
42 (eval-when-compile (require 'cl-lib))
44 ;;; Image mode window-info management.
46 (defvar-local image-mode-winprops-alist t)
48 (defvar image-mode-new-window-functions nil
49 "Special hook run when image data is requested in a new window.
50 It is called with one argument, the initial WINPROPS.")
52 (defun image-mode-winprops (&optional window cleanup)
53 "Return winprops of WINDOW.
54 A winprops object has the shape (WINDOW . ALIST).
55 WINDOW defaults to `selected-window' if it displays the current buffer, and
56 otherwise it defaults to t, used for times when the buffer is not displayed."
57 (cond ((null window)
58 (setq window
59 (if (eq (current-buffer) (window-buffer)) (selected-window) t)))
60 ((eq window t))
61 ((not (windowp window))
62 (error "Not a window: %s" window)))
63 (when cleanup
64 (setq image-mode-winprops-alist
65 (delq nil (mapcar (lambda (winprop)
66 (if (window-live-p (car-safe winprop))
67 winprop))
68 image-mode-winprops-alist))))
69 (let ((winprops (assq window image-mode-winprops-alist)))
70 ;; For new windows, set defaults from the latest.
71 (unless winprops
72 (setq winprops (cons window
73 (copy-alist (cdar image-mode-winprops-alist))))
74 (run-hook-with-args 'image-mode-new-window-functions winprops))
75 ;; Move window to front.
76 (setq image-mode-winprops-alist
77 (cons winprops (delq winprops image-mode-winprops-alist)))
78 winprops))
80 (defun image-mode-window-get (prop &optional winprops)
81 (declare (gv-setter (lambda (val)
82 `(image-mode-window-put ,prop ,val ,winprops))))
83 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
84 (cdr (assq prop (cdr winprops))))
86 (defun image-mode-window-put (prop val &optional winprops)
87 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
88 (setcdr winprops (cons (cons prop val)
89 (delq (assq prop (cdr winprops)) (cdr winprops)))))
91 (defun image-set-window-vscroll (vscroll)
92 (setf (image-mode-window-get 'vscroll) vscroll)
93 (set-window-vscroll (selected-window) vscroll))
95 (defun image-set-window-hscroll (ncol)
96 (setf (image-mode-window-get 'hscroll) ncol)
97 (set-window-hscroll (selected-window) ncol))
99 (defun image-mode-reapply-winprops ()
100 ;; When set-window-buffer, set hscroll and vscroll to what they were
101 ;; last time the image was displayed in this window.
102 (when (and (image-get-display-property)
103 (listp image-mode-winprops-alist))
104 (let* ((winprops (image-mode-winprops nil t))
105 (hscroll (image-mode-window-get 'hscroll winprops))
106 (vscroll (image-mode-window-get 'vscroll winprops)))
107 (if hscroll (set-window-hscroll (selected-window) hscroll))
108 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
110 (defun image-mode-setup-winprops ()
111 ;; Record current scroll settings.
112 (unless (listp image-mode-winprops-alist)
113 (setq image-mode-winprops-alist nil))
114 (add-hook 'window-configuration-change-hook
115 'image-mode-reapply-winprops nil t))
117 ;;; Image scrolling functions
119 (defun image-get-display-property ()
120 (get-char-property (point-min) 'display
121 ;; There might be different images for different displays.
122 (if (eq (window-buffer) (current-buffer))
123 (selected-window))))
125 (declare-function image-size "image.c" (spec &optional pixels frame))
127 (defun image-display-size (spec &optional pixels frame)
128 "Wrapper around `image-size', handling slice display properties.
129 Like `image-size', the return value is (WIDTH . HEIGHT).
130 WIDTH and HEIGHT are in canonical character units if PIXELS is
131 nil, and in pixel units if PIXELS is non-nil.
133 If SPEC is an image display property, this function is equivalent
134 to `image-size'. If SPEC is a list of properties containing
135 `image' and `slice' properties, return the display size taking
136 the slice property into account. If the list contains `image'
137 but not `slice', return the `image-size' of the specified image."
138 (if (eq (car spec) 'image)
139 (image-size spec pixels frame)
140 (let ((image (assoc 'image spec))
141 (slice (assoc 'slice spec)))
142 (cond ((and image slice)
143 (if pixels
144 (cons (nth 3 slice) (nth 4 slice))
145 (cons (/ (float (nth 3 slice)) (frame-char-width frame))
146 (/ (float (nth 4 slice)) (frame-char-height frame)))))
147 (image
148 (image-size image pixels frame))
150 (error "Invalid image specification: %s" spec))))))
152 (defun image-forward-hscroll (&optional n)
153 "Scroll image in current window to the left by N character widths.
154 Stop if the right edge of the image is reached."
155 (interactive "p")
156 (cond ((= n 0) nil)
157 ((< n 0)
158 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
160 (let* ((image (image-get-display-property))
161 (edges (window-inside-edges))
162 (win-width (- (nth 2 edges) (nth 0 edges)))
163 (img-width (ceiling (car (image-display-size image)))))
164 (image-set-window-hscroll (min (max 0 (- img-width win-width))
165 (+ n (window-hscroll))))))))
167 (defun image-backward-hscroll (&optional n)
168 "Scroll image in current window to the right by N character widths.
169 Stop if the left edge of the image is reached."
170 (interactive "p")
171 (image-forward-hscroll (- n)))
173 (defun image-next-line (n)
174 "Scroll image in current window upward by N lines.
175 Stop if the bottom edge of the image is reached."
176 (interactive "p")
177 (cond ((= n 0) nil)
178 ((< n 0)
179 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
181 (let* ((image (image-get-display-property))
182 (edges (window-inside-edges))
183 (win-height (- (nth 3 edges) (nth 1 edges)))
184 (img-height (ceiling (cdr (image-display-size image)))))
185 (image-set-window-vscroll (min (max 0 (- img-height win-height))
186 (+ n (window-vscroll))))))))
188 (defun image-previous-line (&optional n)
189 "Scroll image in current window downward by N lines.
190 Stop if the top edge of the image is reached."
191 (interactive "p")
192 (image-next-line (- n)))
194 (defun image-scroll-up (&optional n)
195 "Scroll image in current window upward by N lines.
196 Stop if the bottom edge of the image is reached.
197 If ARG is omitted or nil, scroll upward by a near full screen.
198 A near full screen is `next-screen-context-lines' less than a full screen.
199 Negative ARG means scroll downward.
200 If ARG is the atom `-', scroll downward by nearly full screen.
201 When calling from a program, supply as argument a number, nil, or `-'."
202 (interactive "P")
203 (cond ((null n)
204 (let* ((edges (window-inside-edges))
205 (win-height (- (nth 3 edges) (nth 1 edges))))
206 (image-next-line
207 (max 0 (- win-height next-screen-context-lines)))))
208 ((eq n '-)
209 (let* ((edges (window-inside-edges))
210 (win-height (- (nth 3 edges) (nth 1 edges))))
211 (image-next-line
212 (min 0 (- next-screen-context-lines win-height)))))
213 (t (image-next-line (prefix-numeric-value n)))))
215 (defun image-scroll-down (&optional n)
216 "Scroll image in current window downward by N lines.
217 Stop if the top edge of the image is reached.
218 If ARG is omitted or nil, scroll downward by a near full screen.
219 A near full screen is `next-screen-context-lines' less than a full screen.
220 Negative ARG means scroll upward.
221 If ARG is the atom `-', scroll upward by nearly full screen.
222 When calling from a program, supply as argument a number, nil, or `-'."
223 (interactive "P")
224 (cond ((null n)
225 (let* ((edges (window-inside-edges))
226 (win-height (- (nth 3 edges) (nth 1 edges))))
227 (image-next-line
228 (min 0 (- next-screen-context-lines win-height)))))
229 ((eq n '-)
230 (let* ((edges (window-inside-edges))
231 (win-height (- (nth 3 edges) (nth 1 edges))))
232 (image-next-line
233 (max 0 (- win-height next-screen-context-lines)))))
234 (t (image-next-line (- (prefix-numeric-value n))))))
236 (defun image-bol (arg)
237 "Scroll horizontally to the left edge of the image in the current window.
238 With argument ARG not nil or 1, move forward ARG - 1 lines first,
239 stopping if the top or bottom edge of the image is reached."
240 (interactive "p")
241 (and arg
242 (/= (setq arg (prefix-numeric-value arg)) 1)
243 (image-next-line (- arg 1)))
244 (image-set-window-hscroll 0))
246 (defun image-eol (arg)
247 "Scroll horizontally to the right edge of the image in the current window.
248 With argument ARG not nil or 1, move forward ARG - 1 lines first,
249 stopping if the top or bottom edge of the image is reached."
250 (interactive "p")
251 (and arg
252 (/= (setq arg (prefix-numeric-value arg)) 1)
253 (image-next-line (- arg 1)))
254 (let* ((image (image-get-display-property))
255 (edges (window-inside-edges))
256 (win-width (- (nth 2 edges) (nth 0 edges)))
257 (img-width (ceiling (car (image-display-size image)))))
258 (image-set-window-hscroll (max 0 (- img-width win-width)))))
260 (defun image-bob ()
261 "Scroll to the top-left corner of the image in the current window."
262 (interactive)
263 (image-set-window-hscroll 0)
264 (image-set-window-vscroll 0))
266 (defun image-eob ()
267 "Scroll to the bottom-right corner of the image in the current window."
268 (interactive)
269 (let* ((image (image-get-display-property))
270 (edges (window-inside-edges))
271 (win-width (- (nth 2 edges) (nth 0 edges)))
272 (img-width (ceiling (car (image-display-size image))))
273 (win-height (- (nth 3 edges) (nth 1 edges)))
274 (img-height (ceiling (cdr (image-display-size image)))))
275 (image-set-window-hscroll (max 0 (- img-width win-width)))
276 (image-set-window-vscroll (max 0 (- img-height win-height)))))
278 ;; Adjust frame and image size.
280 (defun image-mode-fit-frame ()
281 "Toggle whether to fit the frame to the current image.
282 This function assumes the current frame has only one window."
283 ;; FIXME: This does not take into account decorations like mode-line,
284 ;; minibuffer, header-line, ...
285 (interactive)
286 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
287 (display (image-get-display-property))
288 (size (image-display-size display)))
289 (if (and saved
290 (eq (caar saved) (frame-width))
291 (eq (cdar saved) (frame-height)))
292 (progn ;; Toggle back to previous non-fitted size.
293 (set-frame-parameter nil 'image-mode-saved-size nil)
294 (setq size (cdr saved)))
295 ;; Round up size, and save current size so we can toggle back to it.
296 (setcar size (ceiling (car size)))
297 (setcdr size (ceiling (cdr size)))
298 (set-frame-parameter nil 'image-mode-saved-size
299 (cons size (cons (frame-width) (frame-height)))))
300 (set-frame-width (selected-frame) (car size))
301 (set-frame-height (selected-frame) (cdr size))))
303 ;;; Image Mode setup
305 (defvar image-type nil
306 "The image type for the current Image mode buffer.")
307 (make-variable-buffer-local 'image-type)
309 (defvar image-mode-previous-major-mode nil
310 "Internal variable to keep the previous non-image major mode.")
312 (defvar image-mode-map
313 (let ((map (make-sparse-keymap)))
314 (set-keymap-parent map special-mode-map)
315 (define-key map "\C-c\C-c" 'image-toggle-display)
316 (define-key map (kbd "SPC") 'image-scroll-up)
317 (define-key map (kbd "DEL") 'image-scroll-down)
318 (define-key map (kbd "RET") 'image-toggle-animation)
319 (define-key map [remap forward-char] 'image-forward-hscroll)
320 (define-key map [remap backward-char] 'image-backward-hscroll)
321 (define-key map [remap right-char] 'image-forward-hscroll)
322 (define-key map [remap left-char] 'image-backward-hscroll)
323 (define-key map [remap previous-line] 'image-previous-line)
324 (define-key map [remap next-line] 'image-next-line)
325 (define-key map [remap scroll-up] 'image-scroll-up)
326 (define-key map [remap scroll-down] 'image-scroll-down)
327 (define-key map [remap scroll-up-command] 'image-scroll-up)
328 (define-key map [remap scroll-down-command] 'image-scroll-down)
329 (define-key map [remap move-beginning-of-line] 'image-bol)
330 (define-key map [remap move-end-of-line] 'image-eol)
331 (define-key map [remap beginning-of-buffer] 'image-bob)
332 (define-key map [remap end-of-buffer] 'image-eob)
333 map)
334 "Mode keymap for `image-mode'.")
336 (defvar image-minor-mode-map
337 (let ((map (make-sparse-keymap)))
338 (define-key map "\C-c\C-c" 'image-toggle-display)
339 map)
340 "Mode keymap for `image-minor-mode'.")
342 (defvar bookmark-make-record-function)
344 (put 'image-mode 'mode-class 'special)
346 ;;;###autoload
347 (defun image-mode ()
348 "Major mode for image files.
349 You can use \\<image-mode-map>\\[image-toggle-display]
350 to toggle between display as an image and display as text."
351 (interactive)
352 (condition-case err
353 (progn
354 (unless (display-images-p)
355 (error "Display does not support images"))
357 (kill-all-local-variables)
358 (setq major-mode 'image-mode)
360 (if (not (image-get-display-property))
361 (progn
362 (image-toggle-display-image)
363 ;; If attempt to display the image fails.
364 (if (not (image-get-display-property))
365 (error "Invalid image")))
366 ;; Set next vars when image is already displayed but local
367 ;; variables were cleared by kill-all-local-variables
368 (setq cursor-type nil truncate-lines t
369 image-type (plist-get (cdr (image-get-display-property)) :type)))
371 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
372 (use-local-map image-mode-map)
374 ;; Use our own bookmarking function for images.
375 (set (make-local-variable 'bookmark-make-record-function)
376 'image-bookmark-make-record)
378 ;; Keep track of [vh]scroll when switching buffers
379 (image-mode-setup-winprops)
381 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
382 (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
383 (run-mode-hooks 'image-mode-hook)
384 (let ((image (image-get-display-property))
385 (msg1 (substitute-command-keys
386 "Type \\[image-toggle-display] to view the image as ")))
387 (cond
388 ((null image)
389 (message "%s" (concat msg1 "an image.")))
390 ((image-animated-p image)
391 (message "%s"
392 (concat msg1 "text, or "
393 (substitute-command-keys
394 "\\[image-toggle-animation] to animate."))))
396 (message "%s" (concat msg1 "text."))))))
398 (error
399 (image-mode-as-text)
400 (funcall
401 (if (called-interactively-p 'any) 'error 'message)
402 "Cannot display image: %s" (cdr err)))))
404 ;;;###autoload
405 (define-minor-mode image-minor-mode
406 "Toggle Image minor mode in this buffer.
407 With a prefix argument ARG, enable Image minor mode if ARG is
408 positive, and disable it otherwise. If called from Lisp, enable
409 the mode if ARG is omitted or nil.
411 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
412 to switch back to `image-mode' and display an image file as the
413 actual image."
414 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
415 image-minor-mode-map
416 :group 'image
417 :version "22.1"
418 (if image-minor-mode
419 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
421 ;;;###autoload
422 (defun image-mode-as-text ()
423 "Set a non-image mode as major mode in combination with image minor mode.
424 A non-image major mode found from `auto-mode-alist' or Fundamental mode
425 displays an image file as text. `image-minor-mode' provides the key
426 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
427 to display an image file as the actual image.
429 You can use `image-mode-as-text' in `auto-mode-alist' when you want
430 to display an image file as text initially.
432 See commands `image-mode' and `image-minor-mode' for more information
433 on these modes."
434 (interactive)
435 ;; image-mode-as-text = normal-mode + image-minor-mode
436 (let ((previous-image-type image-type)) ; preserve `image-type'
437 (if image-mode-previous-major-mode
438 ;; Restore previous major mode that was already found by this
439 ;; function and cached in `image-mode-previous-major-mode'
440 (funcall image-mode-previous-major-mode)
441 (let ((auto-mode-alist
442 (delq nil (mapcar
443 (lambda (elt)
444 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
445 '(image-mode image-mode-maybe image-mode-as-text))
446 elt))
447 auto-mode-alist)))
448 (magic-fallback-mode-alist
449 (delq nil (mapcar
450 (lambda (elt)
451 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
452 '(image-mode image-mode-maybe image-mode-as-text))
453 elt))
454 magic-fallback-mode-alist))))
455 (normal-mode)
456 (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
457 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
458 (setq image-type previous-image-type)
459 ;; Enable image minor mode with `C-c C-c'.
460 (image-minor-mode 1)
461 ;; Show the image file as text.
462 (image-toggle-display-text)
463 (message "%s" (concat
464 (substitute-command-keys
465 "Type \\[image-toggle-display] to view the image as ")
466 (if (image-get-display-property)
467 "text" "an image") "."))))
469 (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
471 (defun image-toggle-display-text ()
472 "Show the image file as text.
473 Remove text properties that display the image."
474 (let ((inhibit-read-only t)
475 (buffer-undo-list t)
476 (modified (buffer-modified-p)))
477 (remove-list-of-text-properties (point-min) (point-max)
478 '(display read-nonsticky ;; intangible
479 read-only front-sticky))
480 (set-buffer-modified-p modified)
481 (if (called-interactively-p 'any)
482 (message "Repeat this command to go back to displaying the image"))))
484 (defvar archive-superior-buffer)
485 (defvar tar-superior-buffer)
486 (declare-function image-flush "image.c" (spec &optional frame))
488 (defun image-toggle-display-image ()
489 "Show the image of the image file.
490 Turn the image data into a real image, but only if the whole file
491 was inserted."
492 (unless (derived-mode-p 'image-mode)
493 (error "The buffer is not in Image mode"))
494 (let* ((filename (buffer-file-name))
495 (data-p (not (and filename
496 (file-readable-p filename)
497 (not (file-remote-p filename))
498 (not (buffer-modified-p))
499 (not (and (boundp 'archive-superior-buffer)
500 archive-superior-buffer))
501 (not (and (boundp 'tar-superior-buffer)
502 tar-superior-buffer)))))
503 (file-or-data (if data-p
504 (string-make-unibyte
505 (buffer-substring-no-properties (point-min) (point-max)))
506 filename))
507 (type (image-type file-or-data nil data-p))
508 (image (create-image file-or-data type data-p))
509 (inhibit-read-only t)
510 (buffer-undo-list t)
511 (modified (buffer-modified-p))
512 props)
514 ;; Discard any stale image data before looking it up again.
515 (image-flush image)
516 (setq image (append image (image-transform-properties image)))
517 (setq props
518 `(display ,image
519 ;; intangible ,image
520 rear-nonsticky (display) ;; intangible
521 read-only t front-sticky (read-only)))
523 (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
524 (add-text-properties (point-min) (point-max) props)
525 (restore-buffer-modified-p modified))
526 ;; Inhibit the cursor when the buffer contains only an image,
527 ;; because cursors look very strange on top of images.
528 (setq cursor-type nil)
529 ;; This just makes the arrow displayed in the right fringe
530 ;; area look correct when the image is wider than the window.
531 (setq truncate-lines t)
532 ;; Disable adding a newline at the end of the image file when it
533 ;; is written with, e.g., C-x C-w.
534 (if (coding-system-equal (coding-system-base buffer-file-coding-system)
535 'no-conversion)
536 (set (make-local-variable 'find-file-literally) t))
537 ;; Allow navigation of large images
538 (set (make-local-variable 'auto-hscroll-mode) nil)
539 (setq image-type type)
540 (if (eq major-mode 'image-mode)
541 (setq mode-name (format "Image[%s]" type)))
542 (image-transform-check-size)
543 (if (called-interactively-p 'any)
544 (message "Repeat this command to go back to displaying the file as text"))))
546 (defun image-toggle-display ()
547 "Toggle between image and text display.
548 If the current buffer is displaying an image file as an image,
549 call `image-mode-as-text' to switch to text. Otherwise, display
550 the image by calling `image-mode'."
551 (interactive)
552 (if (image-get-display-property)
553 (image-mode-as-text)
554 (image-mode)))
556 (defun image-after-revert-hook ()
557 (when (image-get-display-property)
558 (image-toggle-display-text)
559 ;; Update image display.
560 (mapc (lambda (window) (redraw-frame (window-frame window)))
561 (get-buffer-window-list (current-buffer) 'nomini 'visible))
562 (image-toggle-display-image)))
565 ;;; Animated images
567 (defcustom image-animate-loop nil
568 "Non-nil means animated images loop forever, rather than playing once."
569 :type 'boolean
570 :version "24.1"
571 :group 'image)
573 (defun image-toggle-animation ()
574 "Start or stop animating the current image.
575 If `image-animate-loop' is non-nil, animation loops forever.
576 Otherwise it plays once, then stops."
577 (interactive)
578 (let ((image (image-get-display-property))
579 animation)
580 (cond
581 ((null image)
582 (error "No image is present"))
583 ((null (setq animation (image-animated-p image)))
584 (message "No image animation."))
586 (let ((timer (image-animate-timer image)))
587 (if timer
588 (cancel-timer timer)
589 (let ((index (plist-get (cdr image) :index)))
590 ;; If we're at the end, restart.
591 (and index
592 (>= index (1- (car animation)))
593 (setq index nil))
594 (image-animate image index
595 (if image-animate-loop t)))))))))
598 ;;; Support for bookmark.el
599 (declare-function bookmark-make-record-default
600 "bookmark" (&optional no-file no-context posn))
601 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
602 (declare-function bookmark-default-handler "bookmark" (bmk))
604 (defun image-bookmark-make-record ()
605 `(,@(bookmark-make-record-default nil 'no-context 0)
606 (image-type . ,image-type)
607 (handler . image-bookmark-jump)))
609 ;;;###autoload
610 (defun image-bookmark-jump (bmk)
611 ;; This implements the `handler' function interface for record type
612 ;; returned by `bookmark-make-record-function', which see.
613 (prog1 (bookmark-default-handler bmk)
614 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
615 (image-toggle-display))))
618 ;; Not yet implemented.
619 ;; (defvar image-transform-minor-mode-map
620 ;; (let ((map (make-sparse-keymap)))
621 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
622 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
623 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
624 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
625 ;; ;; (define-key map "c ]" 'image-rotate-right)
626 ;; map)
627 ;; "Minor mode keymap `image-transform-mode'.")
629 ;; (define-minor-mode image-transform-mode
630 ;; "Minor mode for scaling and rotating images.
631 ;; With a prefix argument ARG, enable the mode if ARG is positive,
632 ;; and disable it otherwise. If called from Lisp, enable the mode
633 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
634 ;; been compiled with ImageMagick support."
635 ;; nil "image-transform" image-transform-minor-mode-map)
638 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
639 (defvar image-transform-resize nil
640 "The image resize operation.
641 Its value should be one of the following:
642 - nil, meaning no resizing.
643 - `fit-height', meaning to fit the image to the window height.
644 - `fit-width', meaning to fit the image to the window width.
645 - A number, which is a scale factor (the default size is 1).")
647 (defvar image-transform-scale 1.0
648 "The scale factor of the image being displayed.")
650 (defvar image-transform-rotation 0.0
651 "Rotation angle for the image in the current Image mode buffer.")
653 (defvar image-transform-right-angle-fudge 0.0001
654 "Snap distance to a multiple of a right angle.
655 There's no deep theory behind the default value, it should just
656 be somewhat larger than ImageMagick's MagickEpsilon.")
658 (defsubst image-transform-width (width height)
659 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
660 The rotation angle is the value of `image-transform-rotation' in degrees."
661 (let ((angle (degrees-to-radians image-transform-rotation)))
662 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
663 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
664 ;; rotation by the angle A. The projections onto the first axis
665 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
666 ;; (h/2) sin A, and the difference between the largest and the
667 ;; smallest of the four values is the expression below.
668 (+ (* width (abs (cos angle))) (* height (abs (sin angle))))))
670 ;; The following comment and code snippet are from
671 ;; ImageMagick-6.7.4-4/magick/distort.c
673 ;; /* Set the output image geometry to calculated 'best fit'.
674 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
675 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
676 ;; */
677 ;; if ( fix_bounds ) {
678 ;; geometry.x = (ssize_t) floor(min.x-0.5);
679 ;; geometry.y = (ssize_t) floor(min.y-0.5);
680 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
681 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
682 ;; }
684 ;; Other parts of the same file show that here the origin is in the
685 ;; left lower corner of the image rectangle, the center of the
686 ;; rotation is the center of the rectangle and min.x and max.x
687 ;; (resp. min.y and max.y) are the smallest and the largest of the
688 ;; projections of the vertices onto the first (resp. second) axis.
690 (defun image-transform-fit-width (width height length)
691 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
692 The rotation angle is the value of `image-transform-rotation'.
693 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
694 an \"approximately uniformly\" scaled W x H rectangle, which
695 currently means that w is one of floor(s W) + {0, 1, -1} and h is
696 floor(s H), where s can be recovered as the value of `image-transform-scale'.
697 The value of `image-transform-rotation' may be replaced by
698 a slightly different angle. Currently this is done for values
699 close to a multiple of 90, see `image-transform-right-angle-fudge'."
700 (cond ((< (abs (- (mod (+ image-transform-rotation 90) 180) 90))
701 image-transform-right-angle-fudge)
702 (cl-assert (not (zerop width)) t)
703 (setq image-transform-rotation
704 (float (round image-transform-rotation))
705 image-transform-scale (/ (float length) width))
706 (cons length nil))
707 ((< (abs (- (mod (+ image-transform-rotation 45) 90) 45))
708 image-transform-right-angle-fudge)
709 (cl-assert (not (zerop height)) t)
710 (setq image-transform-rotation
711 (float (round image-transform-rotation))
712 image-transform-scale (/ (float length) height))
713 (cons nil length))
715 (cl-assert (not (and (zerop width) (zerop height))) t)
716 (setq image-transform-scale
717 (/ (float (1- length)) (image-transform-width width height)))
718 ;; Assume we have a w x h image and an angle A, and let l =
719 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
720 ;; of the bounding box of the rotated image, as calculated by
721 ;; `image-transform-width'. The code snippet quoted above
722 ;; means that ImageMagick puts the rotated image in
723 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
724 ;; Elementary considerations show that this is equivalent to
725 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
726 ;; the given `length' parameter and our job is to determine
727 ;; reasonable values for w and h which satisfy these
728 ;; conditions.
729 (let ((w (floor (* image-transform-scale width)))
730 (h (floor (* image-transform-scale height))))
731 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
732 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
733 ;; hence l(w, h) > (L-1) - 2 = L-3.
734 (cons
735 (cond ((= (mod w 2) (mod length 2))
737 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
738 ;; L-1 hold?
739 ((<= (image-transform-width (1+ w) h) (1- length))
740 (1+ w))
741 ;; No, it doesn't, but this implies that l(w-1, h) =
742 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
743 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
745 (1- w)))
746 h)))))
748 (defun image-transform-check-size ()
749 "Check that the image exactly fits the width/height of the window.
751 Do this for an image of type `imagemagick' to make sure that the
752 elisp code matches the way ImageMagick computes the bounding box
753 of a rotated image."
754 (when (and (not (numberp image-transform-resize))
755 (boundp 'image-type)
756 (eq image-type 'imagemagick))
757 (let ((size (image-display-size (image-get-display-property) t)))
758 (cond ((eq image-transform-resize 'fit-width)
759 (cl-assert (= (car size)
760 (- (nth 2 (window-inside-pixel-edges))
761 (nth 0 (window-inside-pixel-edges))))
763 ((eq image-transform-resize 'fit-height)
764 (cl-assert (= (cdr size)
765 (- (nth 3 (window-inside-pixel-edges))
766 (nth 1 (window-inside-pixel-edges))))
767 t))))))
769 (defun image-transform-properties (spec)
770 "Return rescaling/rotation properties for image SPEC.
771 These properties are determined by the Image mode variables
772 `image-transform-resize' and `image-transform-rotation'. The
773 return value is suitable for appending to an image spec.
775 Rescaling and rotation properties only take effect if Emacs is
776 compiled with ImageMagick support."
777 (setq image-transform-scale 1.0)
778 (when (or image-transform-resize
779 (/= image-transform-rotation 0.0))
780 ;; Note: `image-size' looks up and thus caches the untransformed
781 ;; image. There's no easy way to prevent that.
782 (let* ((size (image-size spec t))
783 (resized
784 (cond
785 ((numberp image-transform-resize)
786 (unless (= image-transform-resize 1)
787 (setq image-transform-scale image-transform-resize)
788 (cons nil (floor (* image-transform-resize (cdr size))))))
789 ((eq image-transform-resize 'fit-width)
790 (image-transform-fit-width
791 (car size) (cdr size)
792 (- (nth 2 (window-inside-pixel-edges))
793 (nth 0 (window-inside-pixel-edges)))))
794 ((eq image-transform-resize 'fit-height)
795 (let ((res (image-transform-fit-width
796 (cdr size) (car size)
797 (- (nth 3 (window-inside-pixel-edges))
798 (nth 1 (window-inside-pixel-edges))))))
799 (cons (cdr res) (car res)))))))
800 `(,@(when (car resized)
801 (list :width (car resized)))
802 ,@(when (cdr resized)
803 (list :height (cdr resized)))
804 ,@(unless (= 0.0 image-transform-rotation)
805 (list :rotation image-transform-rotation))))))
807 (defun image-transform-set-scale (scale)
808 "Prompt for a number, and resize the current image by that amount.
809 This command has no effect unless Emacs is compiled with
810 ImageMagick support."
811 (interactive "nScale: ")
812 (setq image-transform-resize scale)
813 (image-toggle-display-image))
815 (defun image-transform-fit-to-height ()
816 "Fit the current image to the height of the current window.
817 This command has no effect unless Emacs is compiled with
818 ImageMagick support."
819 (interactive)
820 (setq image-transform-resize 'fit-height)
821 (image-toggle-display-image))
823 (defun image-transform-fit-to-width ()
824 "Fit the current image to the width of the current window.
825 This command has no effect unless Emacs is compiled with
826 ImageMagick support."
827 (interactive)
828 (setq image-transform-resize 'fit-width)
829 (image-toggle-display-image))
831 (defun image-transform-set-rotation (rotation)
832 "Prompt for an angle ROTATION, and rotate the image by that amount.
833 ROTATION should be in degrees. This command has no effect unless
834 Emacs is compiled with ImageMagick support."
835 (interactive "nRotation angle (in degrees): ")
836 (setq image-transform-rotation (float (mod rotation 360)))
837 (image-toggle-display-image))
839 (provide 'image-mode)
841 ;;; image-mode.el ends here