]> granicus.if.org Git - imagemagick/blob - coders/webp.c
f9afc252f3db0fa51348c80382cb250a43893a94
[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     *stream;
227
228   WebPDecoderConfig
229     configure;
230
231   WebPDecBuffer
232     *restrict webp_image = &configure.output;
233
234   WebPBitstreamFeatures
235     *restrict features = &configure.input;
236
237   /*
238     Open image file.
239   */
240   assert(image_info != (const ImageInfo *) NULL);
241   assert(image_info->signature == MagickSignature);
242   if (image_info->debug != MagickFalse)
243     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
244       image_info->filename);
245   assert(exception != (ExceptionInfo *) NULL);
246   assert(exception->signature == MagickSignature);
247   image=AcquireImage(image_info,exception);
248   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
249   if (status == MagickFalse)
250     {
251       image=DestroyImageList(image);
252       return((Image *) NULL);
253     }
254   if (WebPInitDecoderConfig(&configure) == 0)
255     ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
256   webp_image->colorspace=MODE_RGBA;
257   length=(size_t) GetBlobSize(image);
258   stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
259   if (stream == (unsigned char *) NULL)
260     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
261   count=ReadBlob(image,length,stream);
262   if (count != (ssize_t) length)
263     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
264   webp_status=WebPGetFeatures(stream,length,features);
265   if (webp_status == VP8_STATUS_OK)
266     {
267       image->columns=(size_t) features->width;
268       image->rows=(size_t) features->height;
269       image->depth=8;
270       image->alpha_trait=features->has_alpha != 0 ? BlendPixelTrait :
271         UndefinedPixelTrait;
272       if (image_info->ping != MagickFalse)
273         {
274           stream=(unsigned char*) RelinquishMagickMemory(stream);
275           (void) CloseBlob(image);
276           return(GetFirstImageInList(image));
277         }
278       webp_status=WebPDecode(stream,length,&configure);
279     }
280   if (webp_status != VP8_STATUS_OK)
281     {
282       stream=(unsigned char*) RelinquishMagickMemory(stream);
283       switch (webp_status)
284       {
285         case VP8_STATUS_OUT_OF_MEMORY:
286         {
287           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
288           break;
289         }
290         case VP8_STATUS_INVALID_PARAM:
291         {
292           ThrowReaderException(CorruptImageError,"invalid parameter");
293           break;
294         }
295         case VP8_STATUS_BITSTREAM_ERROR:
296         {
297           ThrowReaderException(CorruptImageError,"CorruptImage");
298           break;
299         }
300         case VP8_STATUS_UNSUPPORTED_FEATURE:
301         {
302           ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
303           break;
304         }
305         case VP8_STATUS_SUSPENDED:
306         {
307           ThrowReaderException(CorruptImageError,"decoder suspended");
308           break;
309         }
310         case VP8_STATUS_USER_ABORT:
311         {
312           ThrowReaderException(CorruptImageError,"user abort");
313           break;
314         }
315         case VP8_STATUS_NOT_ENOUGH_DATA:
316         {
317           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
318           break;
319         }
320         default:
321           ThrowReaderException(CorruptImageError,"CorruptImage");
322       }
323     }
324   if (IsWEBPImageLossless(stream,length) != MagickFalse)
325     image->quality=100;
326   p=webp_image->u.RGBA.rgba;
327   for (y=0; y < (ssize_t) image->rows; y++)
328   {
329     register Quantum
330       *q;
331
332     register ssize_t
333       x;
334
335     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
336     if (q == (Quantum *) NULL)
337       break;
338     for (x=0; x < (ssize_t) image->columns; x++)
339     {
340       SetPixelRed(image,ScaleCharToQuantum(*p++),q);
341       SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
342       SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
343       SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
344       q+=GetPixelChannels(image);
345     }
346     if (SyncAuthenticPixels(image,exception) == MagickFalse)
347       break;
348     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
349       image->rows);
350     if (status == MagickFalse)
351       break;
352   }
353   WebPFreeDecBuffer(webp_image);
354   stream=(unsigned char*) RelinquishMagickMemory(stream);
355   return(image);
356 }
357 #endif
358 \f
359 /*
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 %                                                                             %
362 %                                                                             %
363 %                                                                             %
364 %   R e g i s t e r W E B P I m a g e                                         %
365 %                                                                             %
366 %                                                                             %
367 %                                                                             %
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 %
370 %  RegisterWEBPImage() adds attributes for the WebP image format to
371 %  the list of supported formats.  The attributes include the image format
372 %  tag, a method to read and/or write the format, whether the format
373 %  supports the saving of more than one frame to the same file or blob,
374 %  whether the format supports native in-memory I/O, and a brief
375 %  description of the format.
376 %
377 %  The format of the RegisterWEBPImage method is:
378 %
379 %      size_t RegisterWEBPImage(void)
380 %
381 */
382 ModuleExport size_t RegisterWEBPImage(void)
383 {
384   char
385     version[MaxTextExtent];
386
387   MagickInfo
388     *entry;
389
390   *version='\0';
391   entry=SetMagickInfo("WEBP");
392 #if defined(MAGICKCORE_WEBP_DELEGATE)
393   entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
394   entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
395   (void) FormatLocaleString(version,MaxTextExtent,"libwebp %d.%d.%d (%04X)",
396     (WebPGetDecoderVersion() >> 16) & 0xff,
397     (WebPGetDecoderVersion() >> 8) & 0xff,
398     (WebPGetDecoderVersion() >> 0) & 0xff,WEBP_ENCODER_ABI_VERSION);
399 #endif
400   entry->description=ConstantString("WebP Image Format");
401   entry->mime_type=ConstantString("image/x-webp");
402   entry->adjoin=MagickFalse;
403   entry->module=ConstantString("WEBP");
404   entry->magick=(IsImageFormatHandler *) IsWEBP;
405   if (*version != '\0')
406     entry->version=ConstantString(version);
407   (void) RegisterMagickInfo(entry);
408   return(MagickImageCoderSignature);
409 }
410 \f
411 /*
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 %                                                                             %
414 %                                                                             %
415 %                                                                             %
416 %   U n r e g i s t e r W E B P I m a g e                                     %
417 %                                                                             %
418 %                                                                             %
419 %                                                                             %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 %
422 %  UnregisterWEBPImage() removes format registrations made by the WebP module
423 %  from the list of supported formats.
424 %
425 %  The format of the UnregisterWEBPImage method is:
426 %
427 %      UnregisterWEBPImage(void)
428 %
429 */
430 ModuleExport void UnregisterWEBPImage(void)
431 {
432   (void) UnregisterMagickInfo("WEBP");
433 }
434 #if defined(MAGICKCORE_WEBP_DELEGATE)
435 \f
436 /*
437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 %                                                                             %
439 %                                                                             %
440 %                                                                             %
441 %   W r i t e W E B P I m a g e                                               %
442 %                                                                             %
443 %                                                                             %
444 %                                                                             %
445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 %
447 %  WriteWEBPImage() writes an image in the WebP image format.
448 %
449 %  The format of the WriteWEBPImage method is:
450 %
451 %      MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
452 %        Image *image)
453 %
454 %  A description of each parameter follows.
455 %
456 %    o image_info: the image info.
457 %
458 %    o image:  The image.
459 %
460 */
461
462 static int WebPWriter(const unsigned char *stream,size_t length,
463   const WebPPicture *const picture)
464 {
465   Image
466     *image;
467
468   image=(Image *) picture->custom_ptr;
469   return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
470 }
471
472 static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
473   Image *image,ExceptionInfo *exception)
474 {
475   const char
476     *value;
477
478   int
479     webp_status;
480
481   MagickBooleanType
482     status;
483
484   MemoryInfo
485     *pixel_info;
486
487   register uint32_t
488     *restrict q;
489
490   ssize_t
491     y;
492
493   WebPConfig
494     configure;
495
496   WebPPicture
497     picture;
498
499   WebPAuxStats
500     statistics;
501
502   /*
503     Open output image file.
504   */
505   assert(image_info != (const ImageInfo *) NULL);
506   assert(image_info->signature == MagickSignature);
507   assert(image != (Image *) NULL);
508   assert(image->signature == MagickSignature);
509   if (image->debug != MagickFalse)
510     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
511   if ((image->columns > 16383UL) || (image->rows > 16383UL))
512     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
513   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
514   if (status == MagickFalse)
515     return(status);
516   if ((WebPPictureInit(&picture) == 0) || (WebPConfigInit(&configure) == 0))
517     ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
518   picture.writer=WebPWriter;
519   picture.custom_ptr=(void *) image;
520   picture.stats=(&statistics);
521   picture.width=(int) image->columns;
522   picture.height=(int) image->rows;
523   picture.argb_stride=(int) image->columns;
524   picture.use_argb=1;
525   if (image->quality != UndefinedCompressionQuality)
526     configure.quality=(float) image->quality;
527   else
528     if (image->quality >= 100)
529       configure.lossless=1;
530   value=GetImageOption(image_info,"webp:lossless");
531   if (value != (char *) NULL)
532     configure.lossless=ParseCommandOption(MagickBooleanOptions,MagickFalse,
533       value);
534   value=GetImageOption(image_info,"webp:method");
535   if (value != (char *) NULL)
536     configure.method=StringToInteger(value);
537   value=GetImageOption(image_info,"webp:image-hint");
538   if (value != (char *) NULL)
539     {
540       if (LocaleCompare(value,"graph") == 0)
541         configure.image_hint=WEBP_HINT_GRAPH;
542       if (LocaleCompare(value,"photo") == 0)
543         configure.image_hint=WEBP_HINT_PHOTO;
544       if (LocaleCompare(value,"picture") == 0)
545         configure.image_hint=WEBP_HINT_PICTURE;
546     }
547   value=GetImageOption(image_info,"webp:target-size");
548   if (value != (char *) NULL)
549     configure.target_size=StringToInteger(value);
550   value=GetImageOption(image_info,"webp:target-psnr");
551   if (value != (char *) NULL)
552     configure.target_PSNR=(float) StringToDouble(value,(char **) NULL);
553   value=GetImageOption(image_info,"webp:segments");
554   if (value != (char *) NULL)
555     configure.segments=StringToInteger(value);
556   value=GetImageOption(image_info,"webp:sns-strength");
557   if (value != (char *) NULL)
558     configure.sns_strength=StringToInteger(value);
559   value=GetImageOption(image_info,"webp:filter-strength");
560   if (value != (char *) NULL)
561     configure.filter_strength=StringToInteger(value);
562   value=GetImageOption(image_info,"webp:filter-sharpness");
563   if (value != (char *) NULL)
564     configure.filter_sharpness=StringToInteger(value);
565   value=GetImageOption(image_info,"webp:filter-type");
566   if (value != (char *) NULL)
567     configure.filter_type=StringToInteger(value);
568   value=GetImageOption(image_info,"webp:auto-filter");
569   if (value != (char *) NULL)
570     configure.autofilter=ParseCommandOption(MagickBooleanOptions,MagickFalse,
571       value);
572   value=GetImageOption(image_info,"webp:alpha-compression");
573   if (value != (char *) NULL)
574     configure.alpha_compression=StringToInteger(value);
575   value=GetImageOption(image_info,"webp:alpha-filtering");
576   if (value != (char *) NULL)
577     configure.alpha_filtering=StringToInteger(value);
578   value=GetImageOption(image_info,"webp:alpha-quality");
579   if (value != (char *) NULL)
580     configure.alpha_quality=StringToInteger(value);
581   value=GetImageOption(image_info,"webp:pass");
582   if (value != (char *) NULL)
583     configure.pass=StringToInteger(value);
584   value=GetImageOption(image_info,"webp:show-compressed");
585   if (value != (char *) NULL)
586     configure.show_compressed=StringToInteger(value);
587   value=GetImageOption(image_info,"webp:preprocessing");
588   if (value != (char *) NULL)
589     configure.preprocessing=StringToInteger(value);
590   value=GetImageOption(image_info,"webp:partitions");
591   if (value != (char *) NULL)
592     configure.partitions=StringToInteger(value);
593   value=GetImageOption(image_info,"webp:partition-limit");
594   if (value != (char *) NULL)
595     configure.partition_limit=StringToInteger(value);
596   if (WebPValidateConfig(&configure) == 0)
597     ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
598   /*
599     Allocate memory for pixels.
600   */
601   (void) TransformImageColorspace(image,sRGBColorspace,exception);
602   pixel_info=AcquireVirtualMemory(image->columns,image->rows*
603     sizeof(*picture.argb));
604   if (pixel_info == (MemoryInfo *) NULL)
605     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
606   picture.argb=(uint32_t *) GetVirtualMemoryBlob(pixel_info);
607   /*
608     Convert image to WebP raster pixels.
609   */
610   q=picture.argb;
611   for (y=0; y < (ssize_t) image->rows; y++)
612   {
613     register const Quantum
614       *restrict p;
615
616     register ssize_t
617       x;
618
619     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
620     if (p == (const Quantum *) NULL)
621       break;
622     for (x=0; x < (ssize_t) image->columns; x++)
623     {
624       *q++=(uint32_t) (image->alpha_trait == BlendPixelTrait ?
625         ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000) |
626         (ScaleQuantumToChar(GetPixelRed(image,p)) << 16) |
627         (ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) |
628         (ScaleQuantumToChar(GetPixelBlue(image,p)));
629       p+=GetPixelChannels(image);
630     }
631     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
632       image->rows);
633     if (status == MagickFalse)
634       break;
635   }
636   webp_status=WebPEncode(&configure,&picture);
637   if (webp_status != VP8_ENC_OK)
638     {
639       const char
640         *message;
641
642       switch (picture.error_code)
643       {
644         case VP8_ENC_ERROR_OUT_OF_MEMORY:
645         {
646           message="out of memory";
647           break;
648         }
649         case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
650         {
651           message="bitstream out of memory";
652           break;
653         }
654         case VP8_ENC_ERROR_NULL_PARAMETER:
655         {
656           message="NULL parameter";
657           break;
658         }
659         case VP8_ENC_ERROR_INVALID_CONFIGURATION:
660         {
661           message="invalid configuration";
662           break;
663         }
664         case VP8_ENC_ERROR_BAD_DIMENSION:
665         {
666           message="bad dimension";
667           break;
668         }
669         case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
670         {
671           message="partition 0 overflow (> 512K)";
672           break;
673         }
674         case VP8_ENC_ERROR_PARTITION_OVERFLOW:
675         {
676           message="partition overflow (> 16M)";
677           break;
678         }
679         case VP8_ENC_ERROR_BAD_WRITE:
680         {
681           message="bad write";
682           break;
683         }
684         case VP8_ENC_ERROR_FILE_TOO_BIG:
685         {
686           message="file too big (> 4GB)";
687           break;
688         }
689 #if WEBP_ENCODER_ABI_VERSION >= 0x0100
690         case VP8_ENC_ERROR_USER_ABORT:
691         {
692           message="user abort";
693           break;
694         }
695 #endif
696         default:
697         {
698           message="unknown exception";
699           break;
700         }
701       }
702       (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
703         (char *) message,"`%s'",image->filename);
704     }
705   WebPPictureFree(&picture);
706   pixel_info=RelinquishVirtualMemory(pixel_info);
707   (void) CloseBlob(image);
708   return(webp_status == 0 ? MagickFalse : MagickTrue);
709 }
710 #endif