]> granicus.if.org Git - imagemagick/blob - coders/webp.c
(no commit message)
[imagemagick] / coders / webp.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                         W   W  EEEEE  BBBB   PPPP                           %
7 %                         W   W  E      B   B  P   P                          %
8 %                         W W W  EEE    BBBB   PPPP                           %
9 %                         WW WW  E      B   B  P                              %
10 %                         W   W  EEEEE  BBBB   P                              %
11 %                                                                             %
12 %                                                                             %
13 %                         Read/Write WebP Image Format                        %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 March 2011                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/artifact.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/client.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/display.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/option.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/string-private.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/utility.h"
66 #include "MagickCore/xwindow.h"
67 #include "MagickCore/xwindow-private.h"
68 #if defined(MAGICKCORE_WEBP_DELEGATE)
69 #include <webp/decode.h>
70 #include <webp/encode.h>
71 #endif
72 \f
73 /*
74   Forward declarations.
75 */
76 #if defined(MAGICKCORE_WEBP_DELEGATE)
77 static MagickBooleanType
78   WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
79 #endif
80 \f
81 /*
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %                                                                             %
84 %                                                                             %
85 %                                                                             %
86 %   I s W E B P                                                               %
87 %                                                                             %
88 %                                                                             %
89 %                                                                             %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %
92 %  IsWEBP() returns MagickTrue if the image format type, identified by the
93 %  magick string, is WebP.
94 %
95 %  The format of the IsWEBP method is:
96 %
97 %      MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length)
98 %
99 %  A description of each parameter follows:
100 %
101 %    o magick: compare image format pattern against these bytes.
102 %
103 %    o length: Specifies the length of the magick string.
104 %
105 */
106 static MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length)
107 {
108   if (length < 12)
109     return(MagickFalse);
110   if (LocaleNCompare((const char *) magick+8,"WEBP",4) == 0)
111     return(MagickTrue);
112   return(MagickFalse);
113 }
114 \f
115 #if defined(MAGICKCORE_WEBP_DELEGATE)
116 /*
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %                                                                             %
119 %                                                                             %
120 %                                                                             %
121 %   R e a d W E B P I m a g e                                                 %
122 %                                                                             %
123 %                                                                             %
124 %                                                                             %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %
127 %  ReadWEBPImage() reads an image in the WebP image format.
128 %
129 %  The format of the ReadWEBPImage method is:
130 %
131 %      Image *ReadWEBPImage(const ImageInfo *image_info,
132 %        ExceptionInfo *exception)
133 %
134 %  A description of each parameter follows:
135 %
136 %    o image_info: the image info.
137 %
138 %    o exception: return any errors or warnings in this structure.
139 %
140 */
141
142 static inline uint32_t ReadWebPLSBWord(const unsigned char *restrict data)
143 {
144   register const unsigned char
145     *p;
146
147   register uint32_t
148     value;
149
150   p=data;
151   value=(uint32_t) (*p++);
152   value|=((uint32_t) (*p++)) << 8;
153   value|=((uint32_t) (*p++)) << 16;
154   value|=((uint32_t) (*p++)) << 24;
155   return(value);
156 }
157
158 static MagickBooleanType IsWEBPImageLossless(const unsigned char *stream,
159   const size_t length)
160 {
161 #define VP8_CHUNK_INDEX  15
162 #define LOSSLESS_FLAG  'L'
163 #define EXTENDED_HEADER  'X'
164 #define VP8_CHUNK_HEADER  "VP8"
165 #define VP8_CHUNK_HEADER_SIZE  3
166 #define RIFF_HEADER_SIZE  12
167 #define VP8X_CHUNK_SIZE  10
168 #define TAG_SIZE  4
169 #define CHUNK_SIZE_BYTES  4
170 #define CHUNK_HEADER_SIZE  8
171 #define MAX_CHUNK_PAYLOAD  (~0U-CHUNK_HEADER_SIZE-1)
172
173   ssize_t
174     offset;
175
176   /*
177     Read simple header.
178   */
179   if (stream[VP8_CHUNK_INDEX] != EXTENDED_HEADER)
180     return(stream[VP8_CHUNK_INDEX] == LOSSLESS_FLAG ? MagickTrue : MagickFalse);
181   /*
182     Read extended header.
183   */
184   offset=RIFF_HEADER_SIZE+TAG_SIZE+CHUNK_SIZE_BYTES+VP8X_CHUNK_SIZE;
185   while (offset <= (ssize_t) length)
186   {
187     uint32_t
188       chunk_size,
189       chunk_size_pad;
190
191     chunk_size=ReadWebPLSBWord(stream+offset+TAG_SIZE);
192     if (chunk_size > MAX_CHUNK_PAYLOAD)
193       break;
194     chunk_size_pad=(CHUNK_HEADER_SIZE+chunk_size+1) & ~1;
195     if (memcmp(stream+offset,VP8_CHUNK_HEADER,VP8_CHUNK_HEADER_SIZE) == 0)
196       return(*(stream+offset+VP8_CHUNK_HEADER_SIZE) == LOSSLESS_FLAG ?
197         MagickTrue : MagickFalse);
198     offset+=chunk_size_pad;
199   }
200   return(MagickFalse);
201 }
202
203 static Image *ReadWEBPImage(const ImageInfo *image_info,
204   ExceptionInfo *exception)
205 {
206   Image
207     *image;
208
209   int
210     webp_status;
211
212   MagickBooleanType
213     status;
214
215   register unsigned char
216     *p;
217
218   size_t
219     length;
220
221   ssize_t
222     count,
223     y;
224
225   unsigned char
226     header[12],
227     *stream;
228
229   WebPDecoderConfig
230     configure;
231
232   WebPDecBuffer
233     *restrict webp_image = &configure.output;
234
235   WebPBitstreamFeatures
236     *restrict features = &configure.input;
237
238   /*
239     Open image file.
240   */
241   assert(image_info != (const ImageInfo *) NULL);
242   assert(image_info->signature == MagickSignature);
243   if (image_info->debug != MagickFalse)
244     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
245       image_info->filename);
246   assert(exception != (ExceptionInfo *) NULL);
247   assert(exception->signature == MagickSignature);
248   image=AcquireImage(image_info,exception);
249   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
250   if (status == MagickFalse)
251     {
252       image=DestroyImageList(image);
253       return((Image *) NULL);
254     }
255   if (WebPInitDecoderConfig(&configure) == 0)
256     ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
257   webp_image->colorspace=MODE_RGBA;
258   count=ReadBlob(image,12,header);
259   if (count != 12)
260     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
261   status=IsWEBP(header,count);
262   if (status == MagickFalse)
263     ThrowReaderException(CorruptImageError,"CorruptImage");
264   length=(size_t) (ReadWebPLSBWord(header+4)+8);
265   if (length < 12)
266     ThrowReaderException(CorruptImageError,"CorruptImage");
267   stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
268   if (stream == (unsigned char *) NULL)
269     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
270   memcpy(stream,header,12);
271   count=ReadBlob(image,length-12,stream+12);
272   if (count != (ssize_t) (length-12))
273     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
274   webp_status=WebPGetFeatures(stream,length,features);
275   if (webp_status == VP8_STATUS_OK)
276     {
277       image->columns=(size_t) features->width;
278       image->rows=(size_t) features->height;
279       image->depth=8;
280       image->alpha_trait=features->has_alpha != 0 ? BlendPixelTrait :
281         UndefinedPixelTrait;
282       if (image_info->ping != MagickFalse)
283         {
284           stream=(unsigned char*) RelinquishMagickMemory(stream);
285           (void) CloseBlob(image);
286           return(GetFirstImageInList(image));
287         }
288       webp_status=WebPDecode(stream,length,&configure);
289     }
290   if (webp_status != VP8_STATUS_OK)
291     {
292       stream=(unsigned char*) RelinquishMagickMemory(stream);
293       switch (webp_status)
294       {
295         case VP8_STATUS_OUT_OF_MEMORY:
296         {
297           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
298           break;
299         }
300         case VP8_STATUS_INVALID_PARAM:
301         {
302           ThrowReaderException(CorruptImageError,"invalid parameter");
303           break;
304         }
305         case VP8_STATUS_BITSTREAM_ERROR:
306         {
307           ThrowReaderException(CorruptImageError,"CorruptImage");
308           break;
309         }
310         case VP8_STATUS_UNSUPPORTED_FEATURE:
311         {
312           ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
313           break;
314         }
315         case VP8_STATUS_SUSPENDED:
316         {
317           ThrowReaderException(CorruptImageError,"decoder suspended");
318           break;
319         }
320         case VP8_STATUS_USER_ABORT:
321         {
322           ThrowReaderException(CorruptImageError,"user abort");
323           break;
324         }
325         case VP8_STATUS_NOT_ENOUGH_DATA:
326         {
327           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
328           break;
329         }
330         default:
331           ThrowReaderException(CorruptImageError,"CorruptImage");
332       }
333     }
334   if (IsWEBPImageLossless(stream,length) != MagickFalse)
335     image->quality=100;
336   p=(unsigned char *) webp_image->u.RGBA.rgba;
337   for (y=0; y < (ssize_t) image->rows; y++)
338   {
339     register Quantum
340       *q;
341
342     register ssize_t
343       x;
344
345     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
346     if (q == (Quantum *) NULL)
347       break;
348     for (x=0; x < (ssize_t) image->columns; x++)
349     {
350       SetPixelRed(image,ScaleCharToQuantum(*p++),q);
351       SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
352       SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
353       SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
354       q+=GetPixelChannels(image);
355     }
356     if (SyncAuthenticPixels(image,exception) == MagickFalse)
357       break;
358     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
359       image->rows);
360     if (status == MagickFalse)
361       break;
362   }
363   WebPFreeDecBuffer(webp_image);
364   stream=(unsigned char*) RelinquishMagickMemory(stream);
365   return(image);
366 }
367 #endif
368 \f
369 /*
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 %                                                                             %
372 %                                                                             %
373 %                                                                             %
374 %   R e g i s t e r W E B P I m a g e                                         %
375 %                                                                             %
376 %                                                                             %
377 %                                                                             %
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 %
380 %  RegisterWEBPImage() adds attributes for the WebP image format to
381 %  the list of supported formats.  The attributes include the image format
382 %  tag, a method to read and/or write the format, whether the format
383 %  supports the saving of more than one frame to the same file or blob,
384 %  whether the format supports native in-memory I/O, and a brief
385 %  description of the format.
386 %
387 %  The format of the RegisterWEBPImage method is:
388 %
389 %      size_t RegisterWEBPImage(void)
390 %
391 */
392 ModuleExport size_t RegisterWEBPImage(void)
393 {
394   char
395     version[MaxTextExtent];
396
397   MagickInfo
398     *entry;
399
400   *version='\0';
401   entry=SetMagickInfo("WEBP");
402 #if defined(MAGICKCORE_WEBP_DELEGATE)
403   entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
404   entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
405   (void) FormatLocaleString(version,MaxTextExtent,"libwebp %d.%d.%d [%04X]",
406     (WebPGetDecoderVersion() >> 16) & 0xff,
407     (WebPGetDecoderVersion() >> 8) & 0xff,
408     (WebPGetDecoderVersion() >> 0) & 0xff,WEBP_DECODER_ABI_VERSION);
409 #endif
410   entry->description=ConstantString("WebP Image Format");
411   entry->mime_type=ConstantString("image/x-webp");
412   entry->adjoin=MagickFalse;
413   entry->module=ConstantString("WEBP");
414   entry->magick=(IsImageFormatHandler *) IsWEBP;
415   if (*version != '\0')
416     entry->version=ConstantString(version);
417   (void) RegisterMagickInfo(entry);
418   return(MagickImageCoderSignature);
419 }
420 \f
421 /*
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 %                                                                             %
424 %                                                                             %
425 %                                                                             %
426 %   U n r e g i s t e r W E B P I m a g e                                     %
427 %                                                                             %
428 %                                                                             %
429 %                                                                             %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 %
432 %  UnregisterWEBPImage() removes format registrations made by the WebP module
433 %  from the list of supported formats.
434 %
435 %  The format of the UnregisterWEBPImage method is:
436 %
437 %      UnregisterWEBPImage(void)
438 %
439 */
440 ModuleExport void UnregisterWEBPImage(void)
441 {
442   (void) UnregisterMagickInfo("WEBP");
443 }
444 #if defined(MAGICKCORE_WEBP_DELEGATE)
445 \f
446 /*
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 %                                                                             %
449 %                                                                             %
450 %                                                                             %
451 %   W r i t e W E B P I m a g e                                               %
452 %                                                                             %
453 %                                                                             %
454 %                                                                             %
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 %
457 %  WriteWEBPImage() writes an image in the WebP image format.
458 %
459 %  The format of the WriteWEBPImage method is:
460 %
461 %      MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
462 %        Image *image)
463 %
464 %  A description of each parameter follows.
465 %
466 %    o image_info: the image info.
467 %
468 %    o image:  The image.
469 %
470 */
471
472 #if WEBP_DECODER_ABI_VERSION >= 0x0100
473 static int WebPEncodeProgress(int percent,const WebPPicture* picture)
474 {
475 #define EncodeImageTag  "Encode/Image"
476
477   Image
478     *image;
479
480   MagickBooleanType
481     status;
482
483   image=(Image *) picture->custom_ptr;
484   status=SetImageProgress(image,EncodeImageTag,percent-1,100);
485   return(status == MagickFalse ? 0 : 1);
486 }
487 #endif
488
489 static int WebPEncodeWriter(const unsigned char *stream,size_t length,
490   const WebPPicture *const picture)
491 {
492   Image
493     *image;
494
495   image=(Image *) picture->custom_ptr;
496   return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
497 }
498
499 static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
500   Image *image,ExceptionInfo *exception)
501 {
502   const char
503     *value;
504
505   int
506     webp_status;
507
508   MagickBooleanType
509     status;
510
511   MemoryInfo
512     *pixel_info;
513
514   register uint32_t
515     *restrict q;
516
517   ssize_t
518     y;
519
520   WebPConfig
521     configure;
522
523   WebPPicture
524     picture;
525
526   WebPAuxStats
527     statistics;
528
529   /*
530     Open output image file.
531   */
532   assert(image_info != (const ImageInfo *) NULL);
533   assert(image_info->signature == MagickSignature);
534   assert(image != (Image *) NULL);
535   assert(image->signature == MagickSignature);
536   if (image->debug != MagickFalse)
537     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
538   if ((image->columns > 16383UL) || (image->rows > 16383UL))
539     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
540   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
541   if (status == MagickFalse)
542     return(status);
543   if ((WebPPictureInit(&picture) == 0) || (WebPConfigInit(&configure) == 0))
544     ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
545   picture.writer=WebPEncodeWriter;
546   picture.custom_ptr=(void *) image;
547 #if WEBP_DECODER_ABI_VERSION >= 0x0100
548   picture.progress_hook=WebPEncodeProgress;
549 #endif
550   picture.stats=(&statistics);
551   picture.width=(int) image->columns;
552   picture.height=(int) image->rows;
553   picture.argb_stride=(int) image->columns;
554   picture.use_argb=1;
555   if (image->quality != UndefinedCompressionQuality)
556     configure.quality=(float) image->quality;
557   else
558     if (image->quality >= 100)
559       configure.lossless=1;
560   value=GetImageOption(image_info,"webp:lossless");
561   if (value != (char *) NULL)
562     configure.lossless=(int) ParseCommandOption(MagickBooleanOptions,
563       MagickFalse,value);
564   value=GetImageOption(image_info,"webp:method");
565   if (value != (char *) NULL)
566     configure.method=StringToInteger(value);
567   value=GetImageOption(image_info,"webp:image-hint");
568   if (value != (char *) NULL)
569     {
570       if (LocaleCompare(value,"default") == 0)
571         configure.image_hint=WEBP_HINT_DEFAULT;
572       if (LocaleCompare(value,"photo") == 0)
573         configure.image_hint=WEBP_HINT_PHOTO;
574       if (LocaleCompare(value,"picture") == 0)
575         configure.image_hint=WEBP_HINT_PICTURE;
576 #if WEBP_DECODER_ABI_VERSION >= 0x0200
577       if (LocaleCompare(value,"graph") == 0)
578         configure.image_hint=WEBP_HINT_GRAPH;
579 #endif
580     }
581   value=GetImageOption(image_info,"webp:target-size");
582   if (value != (char *) NULL)
583     configure.target_size=StringToInteger(value);
584   value=GetImageOption(image_info,"webp:target-psnr");
585   if (value != (char *) NULL)
586     configure.target_PSNR=(float) StringToDouble(value,(char **) NULL);
587   value=GetImageOption(image_info,"webp:segments");
588   if (value != (char *) NULL)
589     configure.segments=StringToInteger(value);
590   value=GetImageOption(image_info,"webp:sns-strength");
591   if (value != (char *) NULL)
592     configure.sns_strength=StringToInteger(value);
593   value=GetImageOption(image_info,"webp:filter-strength");
594   if (value != (char *) NULL)
595     configure.filter_strength=StringToInteger(value);
596   value=GetImageOption(image_info,"webp:filter-sharpness");
597   if (value != (char *) NULL)
598     configure.filter_sharpness=StringToInteger(value);
599   value=GetImageOption(image_info,"webp:filter-type");
600   if (value != (char *) NULL)
601     configure.filter_type=StringToInteger(value);
602   value=GetImageOption(image_info,"webp:auto-filter");
603   if (value != (char *) NULL)
604     configure.autofilter=(int) ParseCommandOption(MagickBooleanOptions,
605       MagickFalse,value);
606   value=GetImageOption(image_info,"webp:alpha-compression");
607   if (value != (char *) NULL)
608     configure.alpha_compression=StringToInteger(value);
609   value=GetImageOption(image_info,"webp:alpha-filtering");
610   if (value != (char *) NULL)
611     configure.alpha_filtering=StringToInteger(value);
612   value=GetImageOption(image_info,"webp:alpha-quality");
613   if (value != (char *) NULL)
614     configure.alpha_quality=StringToInteger(value);
615   value=GetImageOption(image_info,"webp:pass");
616   if (value != (char *) NULL)
617     configure.pass=StringToInteger(value);
618   value=GetImageOption(image_info,"webp:show-compressed");
619   if (value != (char *) NULL)
620     configure.show_compressed=StringToInteger(value);
621   value=GetImageOption(image_info,"webp:preprocessing");
622   if (value != (char *) NULL)
623     configure.preprocessing=StringToInteger(value);
624   value=GetImageOption(image_info,"webp:partitions");
625   if (value != (char *) NULL)
626     configure.partitions=StringToInteger(value);
627   value=GetImageOption(image_info,"webp:partition-limit");
628   if (value != (char *) NULL)
629     configure.partition_limit=StringToInteger(value);
630 #if WEBP_DECODER_ABI_VERSION >= 0x0201
631   value=GetImageOption(image_info,"webp:low-memory");
632   if (value != (char *) NULL)
633     configure.low_memory=(int) ParseCommandOption(MagickBooleanOptions,
634       MagickFalse,value);
635   value=GetImageOption(image_info,"webp:emulate-jpeg-size");
636   if (value != (char *) NULL)
637     configure.emulate_jpeg_size=(int) ParseCommandOption(MagickBooleanOptions,
638       MagickFalse,value);
639   value=GetImageOption(image_info,"webp:thread-level");
640   if (value != (char *) NULL)
641     configure.thread_level=StringToInteger(value);
642 #endif
643   if (WebPValidateConfig(&configure) == 0)
644     ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
645   /*
646     Allocate memory for pixels.
647   */
648   (void) TransformImageColorspace(image,sRGBColorspace,exception);
649   pixel_info=AcquireVirtualMemory(image->columns,image->rows*
650     sizeof(*picture.argb));
651   if (pixel_info == (MemoryInfo *) NULL)
652     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
653   picture.argb=(uint32_t *) GetVirtualMemoryBlob(pixel_info);
654   /*
655     Convert image to WebP raster pixels.
656   */
657   q=picture.argb;
658   for (y=0; y < (ssize_t) image->rows; y++)
659   {
660     register const Quantum
661       *restrict p;
662
663     register ssize_t
664       x;
665
666     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
667     if (p == (const Quantum *) NULL)
668       break;
669     for (x=0; x < (ssize_t) image->columns; x++)
670     {
671       *q++=(uint32_t) (image->alpha_trait != UndefinedPixelTrait ?
672         ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000) |
673         (ScaleQuantumToChar(GetPixelRed(image,p)) << 16) |
674         (ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) |
675         (ScaleQuantumToChar(GetPixelBlue(image,p)));
676       p+=GetPixelChannels(image);
677     }
678     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
679       image->rows);
680     if (status == MagickFalse)
681       break;
682   }
683   webp_status=WebPEncode(&configure,&picture);
684   if (webp_status == 0)
685     {
686       const char
687         *message;
688
689       switch (picture.error_code)
690       {
691         case VP8_ENC_ERROR_OUT_OF_MEMORY:
692         {
693           message="out of memory";
694           break;
695         }
696         case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
697         {
698           message="bitstream out of memory";
699           break;
700         }
701         case VP8_ENC_ERROR_NULL_PARAMETER:
702         {
703           message="NULL parameter";
704           break;
705         }
706         case VP8_ENC_ERROR_INVALID_CONFIGURATION:
707         {
708           message="invalid configuration";
709           break;
710         }
711         case VP8_ENC_ERROR_BAD_DIMENSION:
712         {
713           message="bad dimension";
714           break;
715         }
716         case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
717         {
718           message="partition 0 overflow (> 512K)";
719           break;
720         }
721         case VP8_ENC_ERROR_PARTITION_OVERFLOW:
722         {
723           message="partition overflow (> 16M)";
724           break;
725         }
726         case VP8_ENC_ERROR_BAD_WRITE:
727         {
728           message="bad write";
729           break;
730         }
731         case VP8_ENC_ERROR_FILE_TOO_BIG:
732         {
733           message="file too big (> 4GB)";
734           break;
735         }
736 #if WEBP_DECODER_ABI_VERSION >= 0x0100
737         case VP8_ENC_ERROR_USER_ABORT:
738         {
739           message="user abort";
740           break;
741         }
742 #endif
743         default:
744         {
745           message="unknown exception";
746           break;
747         }
748       }
749       (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
750         (char *) message,"`%s'",image->filename);
751     }
752   WebPPictureFree(&picture);
753   pixel_info=RelinquishVirtualMemory(pixel_info);
754   (void) CloseBlob(image);
755   return(webp_status == 0 ? MagickFalse : MagickTrue);
756 }
757 #endif