emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Orgmode] [PATCH] org-display-inline-images to reduce image size


From: Vladimir Alexiev
Subject: [Orgmode] [PATCH] org-display-inline-images to reduce image size
Date: Fri, 7 Jan 2011 16:31:10 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Here's a patch that introduces two custom options
org-display-inline-image-width, org-display-inline-image-height
and patches org-display-inline-images to respect them as
max width, height settings for inline images.

(Someone please defcustom them for me, I only know defvar syntax).

org-display-inline-images uses overlay-put.
(iimage.el uses text-properties, which are recommended in emacs).
(The refs below are (elisp) info nodes.)
Both overlays and text-properties use "Display Property", 
"Other Display Specifications" to setup an image. 
Of all "Image" formats, only "ImageMagick Images" (maybe XBM and PS) 
support scaling. 
That is, IF you build Emacs with ImageMagick (a big if indeed!)

So the patch uses scaling if (fboundp 'imagemagick-types)
  and in this case you must call (imagemagick-register-types) in your .emacs;
else it uses a slice the top left corner (cropping the rest).

diff --git a/doc/org.texi b/doc/org.texi
index 96ea986..9e1c5cf 100755
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -3266,13 +3266,31 @@ variable @code{org-display-internal-link-with-indirect-
buffer}}.
 @vindex org-startup-with-inline-images
 @cindex @code{inlineimages}, STARTUP keyword
 @cindex @code{noinlineimages}, STARTUP keyword
-Toggle the inline display of linked images.  Normally this will only inline
+Toggle the inline display of linked images. 
+Works only for links that start with @code{file:},
+"." (current dir), "/" (root dir) or "~" (home dir). 
+Normally this will only inline
 images that have no description part in the link, i.e. images that will also
 be inlined during export.  When called with a prefix argument, also display
 images that do have a link description.  You can ask for inline images to be
 displayed at startup by configuring the variable
 @address@hidden corresponding
 @code{#+STARTUP} keywords @code{inlineimages} and @code{inlineimages}}.
+
address@hidden org-display-inline-image-width
address@hidden org-display-inline-image-height
+Maximum image size that Orgmode will display inline. Images are reduced by 
EITHER:
address@hidden @bullet
address@hidden Scaling down: supported when Emacs is compiled with ImageMagic, 
in 
which case
+  the function @code{imagemagick-types} is bound. You should invoke
+  @code{(imagemagick-register-types)} in your @code{.emacs}, OR
address@hidden Slicing the top left corner and cropping the rest
address@hidden itemize
+Integer specifies maximum number of pixels.
+Floating number specifies maximum ratio to the frame width/height respectively.
+nil places no restriction on the respective dimension. 
+If both are set, scaling can distort the aspect ratio
+
 @orgcmd{C-c %,org-mark-ring-push}
 @cindex mark ring
 Push the current position onto the mark ring, to be able to return
        Modified lisp/org.el
diff --git a/lisp/org.el b/lisp/org.el
index 53039e4..836286c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16156,6 +16156,55 @@ INCLUDE-LINKED is passed to `org-display-inline-
images'."
                 (length org-inline-image-overlays))
       (message "No images to display inline"))))
 
+;; TODO: defcustom
+(defvar org-display-inline-image-width nil
+"*Maximum image width that Orgmode will display inline.
+Images are reduced by:
+- Scaling down: supported when Emacs is compiled with ImageMagic, in which case
+  (fboundp 'imagemagick-types). You should invoke (imagemagick-register-types) 
in .emacs. 
+- Otherwise: slicing the top left corner and cropping the rest
+Integer specifies maximum number of pixels.
+Floating point specifies maximum ratio to the frame width.
+nil places no restriction.
+If both org-display-inline-image-width and org-display-inline-image-height are 
set, 
+scaling can distort the aspect ratio")
+
+;; TODO: defcustom
+(defvar org-display-inline-image-height nil
+"*Maximum image height that Orgmode will display inline.
+Works similarly to org-display-inline-image-width, which see.")
+
+;; TODO tested only in GNU Emacs 24.0.50.1
+;; TODO scaling not tested (don't have ImageMagick)!
+(defun org-display-inline-images-scale-or-slice (img)
+  ;; Return eventually scaled or sliced down version of image IMG.
+  ;; Scaling is an image property (after 'image)
+  ;; Slicing is a display property (for overlay or text-property) (before 
'image).
+  (when (or org-display-inline-image-width org-display-inline-image-height)
+    (let (c w h width height width height)
+      (setq c (image-size img 'pixels))
+      (setq w (car c) h (cdr c))
+      (setq width (cond
+                   ((integerp org-display-inline-image-width)
+                    org-display-inline-image-width)
+                   ((floatp org-display-inline-image-width)
+                    (truncate (* org-display-inline-image-width (frame-pixel-
width))))))
+      (setq height (cond
+                    ((integerp org-display-inline-image-height)
+                     org-display-inline-image-height)
+                    ((floatp org-display-inline-image-height) 
+                     (truncate (* org-display-inline-image-height (frame-pixel-
height))))))
+      (setq width (and width (> w width) width)) ; if set and w>width then 
width
+      (setq height (and height (> h height) height)) ; if height and h>height 
then height
+      (cond ((fboundp 'imagemagick-types) ; do scaling (it's preferred)
+             ;; FIXME: here we assume that :width :height are given to 
ImageMagick in pixels, 
+             ;; but they could be ratio of original size
+             (if width (setq img (append img (list :width width))))
+             (if height (setq img (append img (list :height height)))))
+            ((or width height)         ; do slicing
+             (setq img (list (list 'slice 0 0 width height) img))))))
+  img)
+
 (defun org-display-inline-images (&optional include-linked refresh beg end)
   "Display inline images.
 Normally only links without a description part are inlined, because this
@@ -16190,7 +16239,7 @@ BEG and END default to the buffer boundaries."
              (setq img (save-match-data (create-image file)))
              (when img
                (setq ov (make-overlay (match-beginning 0) (match-end 0)))
-               (overlay-put ov 'display img)
+               (overlay-put ov 'display (org-display-inline-images-scale-or-
slice img))
                (overlay-put ov 'face 'default)
                (overlay-put ov 'org-image-overlay t)
                (overlay-put ov 'modification-hooks

TINYCHANGE




reply via email to

[Prev in Thread] Current Thread [Next in Thread]