Page 1 of 1

WEBP images from STDIN fail when format specified [patch]

Posted: 2014-07-08T12:46:14-07:00
by njdoyle
I'm trying to read a WEBP image from the command line over STDIN. This works in most cases except when I specifically tell convert that the incoming data is WEBP format.

Code: Select all

$ convert Example.webp info:-
Example.webp WEBP 2448x2512 2448x2512+0+0 8-bit sRGB 58KB 0.100u 0:00.099

$ convert WEBP:Example.webp info:-
WEBP: Example.webp WEBP 2448x2512 2448x2512+0+0 8-bit sRGB 58KB 0.090u 0:00.090

$ cat Example.webp | convert - info:-
- WEBP 2448x2512 2448x2512+0+0 8-bit sRGB 58KB 0.110u 0:00.110

$ cat Example.webp | convert WEBP:- info:-
convert: memory allocation failed `-' @ error/webp.c/ReadWEBPImage/260.
convert: no images defined `info:-' @ error/convert.c/ConvertImageCommand/3187.

Re: Reading WEBP image from STDIN fails when format specifie

Posted: 2014-07-08T18:19:57-07:00
by njdoyle
What seems to be happening is that, when reading directly from a file, we always get a FileStream image blob type. This makes sense.

For the case where we don't explicitly specify the input format, the STDIN stream gets written to a temporary file and ends up as a FileStream image blob type by the time it gets to the WebP reader.

For the case where we do explicitly specify the input format, the STDIN stream gets passed to the WebP reader directly as a StandardStream image blob type.

GetBlobSize returns the appropriate size for FileStream image blob types, it returns 0 for StandardStream image blob types because it doesn't know how much data it's going to get.

WebP reads the entire image data in to memory and allocates this memory using the size given by GetBlobSize (which for the StandardStream case returns 0). The allocation fails because it was asked to allocate nothing.

I've written a patch that reads the file size from the WebP header instead of depending on the reported blob size. Here it is:

Code: Select all

diff -rupN ImageMagick-6.8.9-5.old/coders/webp.c ImageMagick-6.8.9-5/coders/webp.c
--- ImageMagick-6.8.9-5.old/coders/webp.c	2014-04-19 21:41:29.000000000 -0400
+++ ImageMagick-6.8.9-5/coders/webp.c	2014-07-08 22:33:29.000000000 -0400
@@ -223,6 +223,7 @@ static Image *ReadWEBPImage(const ImageI
     y;
 
   unsigned char
+    header[12],
     *stream;
 
   WebPDecoderConfig
@@ -254,12 +255,21 @@ static Image *ReadWEBPImage(const ImageI
   if (WebPInitDecoderConfig(&configure) == 0)
     ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
   webp_image->colorspace=MODE_RGBA;
-  length=(size_t) GetBlobSize(image);
+  count=ReadBlob(image,12,header);
+  if (count != 12)
+    ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
+  status=IsWEBP(header,count);
+  if (status == MagickFalse)
+    ThrowReaderException(CorruptImageError,"CorruptImage");
+  length=(size_t) (ReadWebPLSBWord(header+4)+8);
+  if (length < 12)
+    ThrowReaderException(CorruptImageError,"CorruptImage");
   stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
   if (stream == (unsigned char *) NULL)
     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-  count=ReadBlob(image,length,stream);
-  if (count != (ssize_t) length)
+  memcpy(stream,header,12);
+  count=ReadBlob(image,length-12,stream+12);
+  if (count != (ssize_t) (length-12))
     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
   webp_status=WebPGetFeatures(stream,length,features);
   if (webp_status == VP8_STATUS_OK)

Re: WEBP images from STDIN fail when format specified [patch

Posted: 2014-07-19T14:25:08-07:00
by magick
We can reproduce the problem you posted and have a patch in ImageMagick 6.8.9-6 Beta, available by sometime tomorrow. Thanks.