unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Lars Ingebrigtsen <larsi@gnus.org>
To: Jean Louis <bugs@gnu.support>
Cc: 54242@debbugs.gnu.org
Subject: bug#54242: Emacs can't display animated webp images
Date: Thu, 07 Apr 2022 16:42:44 +0200	[thread overview]
Message-ID: <87v8vl9cpn.fsf_-_@gnus.org> (raw)
In-Reply-To: <87pmmw782g.fsf@gnus.org> (Lars Ingebrigtsen's message of "Tue, 08 Mar 2022 16:55:19 +0100")

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

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Here's an example animated webp image, and indeed, Emacs fails at
> displaying it:
>
> https://mathiasbynens.be/demo/animated-webp-supported.webp

I've started poking at this just to see what's necessary.  As a first
attempt, I thought we could at least display the first frame in the
animate image, so I did the following according to the er documentation
from Google.


[-- Attachment #2: Type: image/png, Size: 10716 bytes --]

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


And indeed, I end up with something that looks like a mangled version of
the nyan cat in the test URL above -- but with wrong width and height or
something?  

The first image in the animation is very simple, though -- frame
width/height is the same as the canvas width height...  so I must be
doing something obviously wrong:

Data: 400 400 400 400 0 0

Anybody spot the obvious error?

There's also the "animation" functions from libwebp, but they are even
less clear as to how they're supposed to be used.

Amusingly, imagemagick also renders the animated webp image wrong, but
in a different way.

diff --git a/configure.ac b/configure.ac
index cda2a04be9..fc409ce8f1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2692,6 +2692,7 @@ AC_DEFUN
 
       EMACS_CHECK_MODULES([WEBP], [$WEBP_MODULE])
       AC_SUBST(WEBP_CFLAGS)
+      WEBP_LIBS="-lwebp -lwebpdemux"
       AC_SUBST(WEBP_LIBS)
    fi
    if test $HAVE_WEBP = yes; then
diff --git a/src/image.c b/src/image.c
index 519eafb904..f175be8923 100644
--- a/src/image.c
+++ b/src/image.c
@@ -9053,6 +9053,7 @@ gif_load (struct frame *f, struct image *img)
  ***********************************************************************/
 
 #include "webp/decode.h"
+#include "webp/demux.h"
 
 /* Indices of image specification fields in webp_format, below.  */
 
@@ -9224,19 +9225,61 @@ webp_load (struct frame *f, struct image *img)
       goto webp_error1;
     }
 
-  /* Decode WebP data.  */
-  uint8_t *decoded;
-  int width, height;
-  if (features.has_alpha)
-    /* Linear [r0, g0, b0, a0, r1, g1, b1, a1, ...] order.  */
-    decoded = WebPDecodeRGBA (contents, size, &width, &height);
+  uint8_t *decoded = NULL;
+  int width, height, x_offset = 0, y_offset = 0, fwidth, fheight;
+
+  if (features.has_animation)
+    {
+      /* Animated image.  */
+      WebPData webp_data;
+      webp_data.bytes = WebPMalloc (size);
+      webp_data.size = size;
+      memcpy ((void*) webp_data.bytes, contents, size);
+      
+      WebPDemuxer* demux = WebPDemux(&webp_data);
+      width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
+      height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
+
+      WebPIterator iter;
+      if (WebPDemuxGetFrame (demux, 1, &iter)) {
+	do {
+	  fprintf(stderr, "Complete is %d %d\n", iter.complete, iter.has_alpha);
+	  /* Decode WebP data.  */
+	  if (iter.has_alpha)
+	    /* Linear [r0, g0, b0, a0, r1, g1, b1, a1, ...] order.  */
+	    decoded = WebPDecodeRGBA (iter.fragment.bytes, iter.fragment.size,
+				      &fwidth, &fheight);
+	  else
+	    /* Linear [r0, g0, b0, r1, g1, b1, ...] order.  */
+	    decoded = WebPDecodeRGB (iter.fragment.bytes, iter.fragment.size,
+				     &fwidth, &fheight);
+	  x_offset = iter.x_offset;
+	  y_offset = iter.y_offset;
+	  fwidth = iter.width;
+	  fheight = iter.height;
+	  break;
+	} while (WebPDemuxNextFrame (&iter));
+	WebPDemuxReleaseIterator (&iter);
+      }
+      WebPDataClear (&webp_data);
+      WebPDemuxDelete (demux);
+    }
   else
-    /* Linear [r0, g0, b0, r1, g1, b1, ...] order.  */
-    decoded = WebPDecodeRGB (contents, size, &width, &height);
+    {
+      /* Non-animated image.  */
+      if (features.has_alpha)
+	/* Linear [r0, g0, b0, a0, r1, g1, b1, a1, ...] order.  */
+	decoded = WebPDecodeRGBA (contents, size, &width, &height);
+      else
+	/* Linear [r0, g0, b0, r1, g1, b1, ...] order.  */
+	decoded = WebPDecodeRGB (contents, size, &width, &height);
+      fwidth = width;
+      fheight = height;
+    }
 
   if (!decoded)
     {
-      image_error ("Error when interpreting WebP image data");
+      image_error ("Error when decoding WebP image data");
       goto webp_error1;
     }
 
@@ -9266,9 +9309,11 @@ webp_load (struct frame *f, struct image *img)
   init_color_table ();
 
   uint8_t *p = decoded;
-  for (int y = 0; y < height; ++y)
+  fprintf(stderr, "Data: %d %d %d %d %d %d\n", width, height, fwidth, fheight,
+	  x_offset, y_offset);
+  for (int y = y_offset; y < fheight; ++y)
     {
-      for (int x = 0; x < width; ++x)
+      for (int x = x_offset; x < fwidth; ++x)
 	{
 	  int r = *p++ << 8;
 	  int g = *p++ << 8;

  reply	other threads:[~2022-04-07 14:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03  9:29 bug#54242: 29.0.50; Cannot see WebP imags in Lucid build Jean Louis
2022-03-04  8:32 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-03-06  0:45 ` Lars Ingebrigtsen
2022-03-07 22:51   ` Jean Louis
2022-03-08 15:55     ` Lars Ingebrigtsen
2022-04-07 14:42       ` Lars Ingebrigtsen [this message]
2022-04-07 14:55         ` bug#54242: Emacs can't display animated webp images Lars Ingebrigtsen
2022-04-09 16:45           ` Lars Ingebrigtsen
2022-04-10 11:12             ` Lars Ingebrigtsen
2022-04-10 13:48               ` Eli Zaretskii
2022-04-10 13:54                 ` Lars Ingebrigtsen
2022-04-10 13:56                   ` Eli Zaretskii
2022-04-10 13:58                     ` Lars Ingebrigtsen
2022-04-10 14:13                       ` Eli Zaretskii
2022-04-10 14:24                         ` Lars Ingebrigtsen
2022-04-10 14:26                           ` Eli Zaretskii
2022-04-10 14:32                             ` Lars Ingebrigtsen
2022-04-10 14:45                               ` Eli Zaretskii
2022-04-10 14:49                                 ` Lars Ingebrigtsen

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=87v8vl9cpn.fsf_-_@gnus.org \
    --to=larsi@gnus.org \
    --cc=54242@debbugs.gnu.org \
    --cc=bugs@gnu.support \
    /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).