emacs-devel
[Top][All Lists]
Advanced

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

Re: image-size


From: Lars Magne Ingebrigtsen
Subject: Re: image-size
Date: Thu, 20 Jun 2013 14:18:34 +0200
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3.50 (gnu/linux)

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

Does the patch look ok?

=== 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


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

reply via email to

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