emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Vladimir Alexiev <vladimir@sirma.bg>
To: emacs-orgmode@gnu.org
Subject: [PATCH] org-display-inline-images to reduce image size
Date: Fri, 7 Jan 2011 16:31:10 +0000 (UTC)	[thread overview]
Message-ID: <loom.20110107T173057-183@post.gmane.org> (raw)
In-Reply-To: loom.20110107T090451-105@post.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
 @code{org-startup-with-inline-images}@footnote{with corresponding
 @code{#+STARTUP} keywords @code{inlineimages} and @code{inlineimages}}.
+
+@vindex org-display-inline-image-width
+@vindex org-display-inline-image-height
+Maximum image size that Orgmode will display inline. Images are reduced by 
EITHER:
+@itemize @bullet
+@itemize 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
+@itemize Slicing the top left corner and cropping the rest
+@end 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	other threads:[~2011-01-07 16:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-01 15:33 inline images Richard Riley
2008-10-02  0:47 ` Lindsay Todd
2008-10-02 19:56   ` Felipe Csaszar
2008-10-03  3:34     ` Sebastian Rose
2008-10-03 16:51       ` Felipe Csaszar
2008-10-07  2:50         ` gregory mitchell
2011-01-07  8:06           ` Vladimir Alexiev
2011-01-07 16:31             ` Vladimir Alexiev [this message]
2011-01-10  8:31               ` [PATCH] org-display-inline-images to reduce image size Vladimir Alexiev
2011-02-07  8:33                 ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=loom.20110107T173057-183@post.gmane.org \
    --to=vladimir@sirma.bg \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).