unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Lars Magne Ingebrigtsen <larsi@gnus.org>
To: emacs-devel@gnu.org
Subject: Re: image-size
Date: Thu, 20 Jun 2013 14:18:34 +0200	[thread overview]
Message-ID: <m3mwql0ycl.fsf@stories.gnus.org> (raw)
In-Reply-To: m3ip19drnf.fsf@stories.gnus.org

[-- Attachment #1: Type: text/plain, Size: 127 bytes --]

Er, whaddayou know.  I implemented this (patch below), and reading 9gag
via ssh is now a lot faster.

Does the patch look ok?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: size.patch --]
[-- Type: text/x-diff, Size: 4048 bytes --]

=== modified file 'src/image.c'
--- src/image.c	2013-05-12 19:17:04 +0000
+++ src/image.c	2013-06-20 12:17:20 +0000
@@ -876,6 +876,33 @@
 }
 
 
+#if defined (HAVE_IMAGEMAGICK)
+void
+imagemagick_image_size (unsigned char *contents, size_t size,
+			char *filename,
+			size_t *width, size_t *height);
+
+static void
+fast_image_size (Lisp_Object spec, size_t *width, size_t *height)
+{
+  Lisp_Object filename, data;
+
+  *width = -1;
+  *height = -1;
+
+  filename = Fplist_get (Fcdr (spec), QCfile);
+  if (! NILP (filename)) {
+    imagemagick_image_size (NULL, 0, SSDATA (x_find_image_file (filename)),
+			    width, height);
+    return;
+  }
+
+  data = Fplist_get (Fcdr (spec), QCdata);
+  if (! NILP (data))
+    imagemagick_image_size (SDATA (data), SBYTES (data), NULL, width, height);
+}
+#endif
+
 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
        doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
 PIXELS non-nil means return the size in pixels, otherwise return the
@@ -884,26 +911,33 @@
 or omitted means use the selected frame.  */)
   (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame)
 {
-  Lisp_Object size;
+  Lisp_Object size = Qnil;
 
-  size = Qnil;
-  if (valid_image_p (spec))
-    {
-      struct frame *f = decode_window_system_frame (frame);
-      ptrdiff_t id = lookup_image (f, spec);
-      struct image *img = IMAGE_FROM_ID (f, id);
-      int width = img->width + 2 * img->hmargin;
-      int height = img->height + 2 * img->vmargin;
-
-      if (NILP (pixels))
-	size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
-		      make_float ((double) height / FRAME_LINE_HEIGHT (f)));
-      else
-	size = Fcons (make_number (width), make_number (height));
-    }
-  else
+  if (! valid_image_p (spec))
     error ("Invalid image specification");
 
+#if defined (HAVE_IMAGEMAGICK)
+  if (! NILP (pixels)) {
+    size_t width, height;
+    fast_image_size (spec, &width, &height);
+    return Fcons (make_number (width), make_number (height));
+  }
+#endif
+
+  {
+    struct frame *f = decode_window_system_frame (frame);
+    ptrdiff_t id = lookup_image (f, spec);
+    struct image *img = IMAGE_FROM_ID (f, id);
+    size_t width = img->width + 2 * img->hmargin;
+    size_t height = img->height + 2 * img->vmargin;
+
+    if (NILP (pixels))
+      size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
+		    make_float ((double) height / FRAME_LINE_HEIGHT (f)));
+    else
+      size = Fcons (make_number (width), make_number (height));
+  }
+
   return size;
 }
 
@@ -7616,6 +7650,52 @@
   description = (char *) MagickRelinquishMemory (description);
 }
 
+/* Return the size of an image that's either contained in contents or
+   filename.  If we couldn't read the image, return -1 as the
+   width/height. */
+
+void
+imagemagick_image_size (unsigned char *contents, size_t size,
+			char *filename,
+			size_t *width, size_t *height)
+{
+  MagickWand *image_wand, *ping_wand;
+
+  *width = -1;
+  *height = -1;
+
+  /* Initialize the imagemagick environment.  */
+  MagickWandGenesis ();
+  ping_wand = NewMagickWand ();
+
+  if ((filename
+       ? MagickPingImage (ping_wand, filename)
+       : MagickPingImageBlob (ping_wand, contents, size))
+      == MagickFalse)
+    {
+      imagemagick_error (ping_wand);
+      DestroyMagickWand (ping_wand);
+      return;
+    }
+
+  DestroyMagickWand (ping_wand);
+  image_wand = NewMagickWand ();
+
+  if ((filename
+       ? MagickReadImage (image_wand, filename)
+       : MagickReadImageBlob (image_wand, contents, size))
+      == MagickFalse)
+    {
+      imagemagick_error (image_wand);
+    } else {
+      *height = MagickGetImageHeight (image_wand);
+      *width = MagickGetImageWidth (image_wand);
+    }
+
+  DestroyMagickWand (image_wand);
+  MagickWandTerminus ();
+}
+
 /* Helper function for imagemagick_load, which does the actual loading
    given contents and size, apart from frame and image structures,
    passed from imagemagick_load.  Uses librimagemagick to do most of


[-- Attachment #3: Type: text/plain, Size: 104 bytes --]



-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/

  reply	other threads:[~2013-06-20 12:18 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-20 10:04 image-size Lars Magne Ingebrigtsen
2013-06-20 12:18 ` Lars Magne Ingebrigtsen [this message]
2013-06-20 16:52   ` image-size Eli Zaretskii
2013-06-20 16:01 ` image-size Eli Zaretskii
2013-06-20 16:50   ` image-size Lars Magne Ingebrigtsen
2013-06-20 16:55     ` image-size Lars Magne Ingebrigtsen
2013-06-20 17:16       ` image-size Eli Zaretskii
2013-06-20 17:54         ` image-size Lars Magne Ingebrigtsen
2013-06-20 18:15           ` image-size Eli Zaretskii
2013-06-20 18:31             ` image-size Eli Zaretskii
2013-06-20 18:46               ` image-size Lars Magne Ingebrigtsen
2013-06-20 19:11                 ` image-size Eli Zaretskii
2013-06-20 19:18                   ` image-size Lars Magne Ingebrigtsen
2013-06-20 19:37                     ` image-size Eli Zaretskii
2013-06-20 19:52                       ` image-size David Engster
2013-06-20 20:50                         ` image-size Eli Zaretskii
2013-06-20 19:34                   ` image-size Lars Magne Ingebrigtsen
2013-06-20 19:42                     ` image-size Eli Zaretskii
2013-06-20 19:52                       ` image-size Lars Magne Ingebrigtsen
2013-06-20 20:53                         ` image-size Eli Zaretskii
2013-06-20 20:57                           ` image-size Lars Magne Ingebrigtsen
2013-06-20 21:06                             ` image-size Lars Magne Ingebrigtsen
2013-06-21  6:12                               ` image-size Eli Zaretskii
2013-06-20 21:42                             ` image-size Jan Djärv
2013-06-21  0:46                               ` image-size YAMAMOTO Mitsuharu
2013-06-21  6:27                                 ` image-size Lars Magne Ingebrigtsen
2013-06-26  8:25                                   ` image-size YAMAMOTO Mitsuharu
2013-06-26 10:39                                     ` image-size YAMAMOTO Mitsuharu
2013-06-26 12:04                                     ` image-size Lars Magne Ingebrigtsen
2013-06-27  0:46                                       ` image-size Lars Magne Ingebrigtsen
2013-06-27  2:49                                         ` image-size Stefan Monnier
2013-06-28  2:46                                       ` image-size YAMAMOTO Mitsuharu
2013-06-28  3:12                                         ` image-size Juanma Barranquero
2013-06-28  3:45                                           ` image-size YAMAMOTO Mitsuharu
2013-06-21  6:15                               ` image-size Eli Zaretskii
2013-06-21  7:34                                 ` image-size Jan Djärv
2013-06-21  8:27                                   ` image-size Eli Zaretskii
2013-06-21  9:03                                     ` image-size Jan Djärv
2013-06-21  9:07                                       ` image-size Lars Magne Ingebrigtsen
2013-06-21  9:58                                         ` image-size Eli Zaretskii
2013-06-21  9:57                                       ` image-size Eli Zaretskii
2013-06-21  6:13                             ` image-size Eli Zaretskii
2013-06-21 15:54                               ` image-size Willem Rein Oudshoorn

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=m3mwql0ycl.fsf@stories.gnus.org \
    --to=larsi@gnus.org \
    --cc=emacs-devel@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.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).