2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % W W EEEEE BBBB PPPP %
8 % W W W EEE BBBB PPPP %
13 % Read/Write WebP Image Format %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/profile.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
65 #include "MagickCore/module.h"
66 #include "MagickCore/utility.h"
67 #include "MagickCore/xwindow.h"
68 #include "MagickCore/xwindow-private.h"
69 #if defined(MAGICKCORE_WEBP_DELEGATE)
70 #include <webp/decode.h>
71 #include <webp/encode.h>
72 #if defined(MAGICKCORE_WEBPMUX_DELEGATE)
80 #if defined(MAGICKCORE_WEBP_DELEGATE)
81 static MagickBooleanType
82 WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % IsWEBP() returns MagickTrue if the image format type, identified by the
97 % magick string, is WebP.
99 % The format of the IsWEBP method is:
101 % MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length)
103 % A description of each parameter follows:
105 % o magick: compare image format pattern against these bytes.
107 % o length: Specifies the length of the magick string.
110 static MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length)
114 if (LocaleNCompare((const char *) magick+8,"WEBP",4) == 0)
119 #if defined(MAGICKCORE_WEBP_DELEGATE)
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % R e a d W E B P I m a g e %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 % ReadWEBPImage() reads an image in the WebP image format.
133 % The format of the ReadWEBPImage method is:
135 % Image *ReadWEBPImage(const ImageInfo *image_info,
136 % ExceptionInfo *exception)
138 % A description of each parameter follows:
140 % o image_info: the image info.
142 % o exception: return any errors or warnings in this structure.
146 static inline uint32_t ReadWebPLSBWord(
147 const unsigned char *magick_restrict data)
149 register const unsigned char
156 value=(uint32_t) (*p++);
157 value|=((uint32_t) (*p++)) << 8;
158 value|=((uint32_t) (*p++)) << 16;
159 value|=((uint32_t) (*p++)) << 24;
163 static MagickBooleanType IsWEBPImageLossless(const unsigned char *stream,
166 #define VP8_CHUNK_INDEX 15
167 #define LOSSLESS_FLAG 'L'
168 #define EXTENDED_HEADER 'X'
169 #define VP8_CHUNK_HEADER "VP8"
170 #define VP8_CHUNK_HEADER_SIZE 3
171 #define RIFF_HEADER_SIZE 12
172 #define VP8X_CHUNK_SIZE 10
174 #define CHUNK_SIZE_BYTES 4
175 #define CHUNK_HEADER_SIZE 8
176 #define MAX_CHUNK_PAYLOAD (~0U-CHUNK_HEADER_SIZE-1)
184 if (length <= VP8_CHUNK_INDEX)
186 if (stream[VP8_CHUNK_INDEX] != EXTENDED_HEADER)
187 return(stream[VP8_CHUNK_INDEX] == LOSSLESS_FLAG ? MagickTrue : MagickFalse);
189 Read extended header.
191 offset=RIFF_HEADER_SIZE+TAG_SIZE+CHUNK_SIZE_BYTES+VP8X_CHUNK_SIZE;
192 while (offset+TAG_SIZE <= (ssize_t) (length-TAG_SIZE))
198 chunk_size=ReadWebPLSBWord(stream+offset+TAG_SIZE);
199 if (chunk_size > MAX_CHUNK_PAYLOAD)
201 chunk_size_pad=(CHUNK_HEADER_SIZE+chunk_size+1) & ~1;
202 if (memcmp(stream+offset,VP8_CHUNK_HEADER,VP8_CHUNK_HEADER_SIZE) == 0)
203 return(*(stream+offset+VP8_CHUNK_HEADER_SIZE) == LOSSLESS_FLAG ?
204 MagickTrue : MagickFalse);
205 offset+=chunk_size_pad;
210 static Image *ReadWEBPImage(const ImageInfo *image_info,
211 ExceptionInfo *exception)
213 #define ThrowWEBPException(severity,tag) \
215 if (stream != (unsigned char *) NULL) \
216 stream=(unsigned char*) RelinquishMagickMemory(stream); \
217 if (webp_image != (WebPDecBuffer *) NULL) \
218 WebPFreeDecBuffer(webp_image); \
219 ThrowReaderException(severity,tag); \
231 register unsigned char
249 *magick_restrict webp_image = &configure.output;
251 WebPBitstreamFeatures
252 *magick_restrict features = &configure.input;
257 assert(image_info != (const ImageInfo *) NULL);
258 assert(image_info->signature == MagickCoreSignature);
259 if (image_info->debug != MagickFalse)
260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
261 image_info->filename);
262 assert(exception != (ExceptionInfo *) NULL);
263 assert(exception->signature == MagickCoreSignature);
264 image=AcquireImage(image_info,exception);
265 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
266 if (status == MagickFalse)
268 image=DestroyImageList(image);
269 return((Image *) NULL);
271 stream=(unsigned char *) NULL;
272 if (WebPInitDecoderConfig(&configure) == 0)
273 ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
274 webp_image->colorspace=MODE_RGBA;
275 count=ReadBlob(image,12,header);
277 ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile");
278 status=IsWEBP(header,count);
279 if (status == MagickFalse)
280 ThrowWEBPException(CorruptImageError,"CorruptImage");
281 length=(size_t) (ReadWebPLSBWord(header+4)+8);
283 ThrowWEBPException(CorruptImageError,"CorruptImage");
284 if (length > GetBlobSize(image))
285 ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile");
286 stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
287 if (stream == (unsigned char *) NULL)
288 ThrowWEBPException(ResourceLimitError,"MemoryAllocationFailed");
289 (void) memcpy(stream,header,12);
290 count=ReadBlob(image,length-12,stream+12);
291 if (count != (ssize_t) (length-12))
292 ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile");
293 webp_status=WebPGetFeatures(stream,length,features);
294 if (webp_status == VP8_STATUS_OK)
296 image->columns=(size_t) features->width;
297 image->rows=(size_t) features->height;
299 image->alpha_trait=features->has_alpha != 0 ? BlendPixelTrait :
301 if (image_info->ping != MagickFalse)
303 stream=(unsigned char*) RelinquishMagickMemory(stream);
304 (void) CloseBlob(image);
305 return(GetFirstImageInList(image));
307 status=SetImageExtent(image,image->columns,image->rows,exception);
308 if (status == MagickFalse)
310 stream=(unsigned char*) RelinquishMagickMemory(stream);
311 (void) CloseBlob(image);
312 return(DestroyImageList(image));
314 if (IsWEBPImageLossless(stream,length) != MagickFalse)
316 webp_status=WebPDecode(stream,length,&configure);
318 if (webp_status != VP8_STATUS_OK)
321 case VP8_STATUS_OUT_OF_MEMORY:
323 ThrowWEBPException(ResourceLimitError,"MemoryAllocationFailed");
326 case VP8_STATUS_INVALID_PARAM:
328 ThrowWEBPException(CorruptImageError,"invalid parameter");
331 case VP8_STATUS_BITSTREAM_ERROR:
333 ThrowWEBPException(CorruptImageError,"CorruptImage");
336 case VP8_STATUS_UNSUPPORTED_FEATURE:
338 ThrowWEBPException(CoderError,"DataEncodingSchemeIsNotSupported");
341 case VP8_STATUS_SUSPENDED:
343 ThrowWEBPException(CorruptImageError,"decoder suspended");
346 case VP8_STATUS_USER_ABORT:
348 ThrowWEBPException(CorruptImageError,"user abort");
351 case VP8_STATUS_NOT_ENOUGH_DATA:
353 ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile");
357 ThrowWEBPException(CorruptImageError,"CorruptImage");
359 p=(unsigned char *) webp_image->u.RGBA.rgba;
360 for (y=0; y < (ssize_t) image->rows; y++)
368 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
369 if (q == (Quantum *) NULL)
371 for (x=0; x < (ssize_t) image->columns; x++)
373 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
374 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
375 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
376 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
377 q+=GetPixelChannels(image);
379 if (SyncAuthenticPixels(image,exception) == MagickFalse)
381 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
383 if (status == MagickFalse)
386 WebPFreeDecBuffer(webp_image);
387 #if defined(MAGICKCORE_WEBPMUX_DELEGATE)
397 content = { stream, length };
403 Extract any profiles.
405 mux=WebPMuxCreate(&content,0);
406 (void) memset(&chunk,0,sizeof(chunk));
407 WebPMuxGetFeatures(mux,&webp_flags);
408 if (webp_flags & ICCP_FLAG)
410 WebPMuxGetChunk(mux,"ICCP",&chunk);
411 profile=BlobToStringInfo(chunk.bytes,chunk.size);
412 if (profile != (StringInfo *) NULL)
414 SetImageProfile(image,"ICC",profile,exception);
415 profile=DestroyStringInfo(profile);
418 if (webp_flags & EXIF_FLAG)
420 WebPMuxGetChunk(mux,"EXIF",&chunk);
421 profile=BlobToStringInfo(chunk.bytes,chunk.size);
422 if (profile != (StringInfo *) NULL)
424 SetImageProfile(image,"EXIF",profile,exception);
425 profile=DestroyStringInfo(profile);
428 if (webp_flags & XMP_FLAG)
430 WebPMuxGetChunk(mux,"XMP",&chunk);
431 profile=BlobToStringInfo(chunk.bytes,chunk.size);
432 if (profile != (StringInfo *) NULL)
434 SetImageProfile(image,"XMP",profile,exception);
435 profile=DestroyStringInfo(profile);
441 stream=(unsigned char*) RelinquishMagickMemory(stream);
442 (void) CloseBlob(image);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % R e g i s t e r W E B P I m a g e %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % RegisterWEBPImage() adds attributes for the WebP image format to
459 % the list of supported formats. The attributes include the image format
460 % tag, a method to read and/or write the format, whether the format
461 % supports the saving of more than one frame to the same file or blob,
462 % whether the format supports native in-memory I/O, and a brief
463 % description of the format.
465 % The format of the RegisterWEBPImage method is:
467 % size_t RegisterWEBPImage(void)
470 ModuleExport size_t RegisterWEBPImage(void)
473 version[MagickPathExtent];
479 entry=AcquireMagickInfo("WEBP","WEBP","WebP Image Format");
480 #if defined(MAGICKCORE_WEBP_DELEGATE)
481 entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
482 entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
483 (void) FormatLocaleString(version,MagickPathExtent,"libwebp %d.%d.%d [%04X]",
484 (WebPGetDecoderVersion() >> 16) & 0xff,
485 (WebPGetDecoderVersion() >> 8) & 0xff,
486 (WebPGetDecoderVersion() >> 0) & 0xff,WEBP_DECODER_ABI_VERSION);
488 entry->mime_type=ConstantString("image/webp");
489 entry->flags|=CoderDecoderSeekableStreamFlag;
490 entry->flags^=CoderAdjoinFlag;
491 entry->magick=(IsImageFormatHandler *) IsWEBP;
492 if (*version != '\0')
493 entry->version=ConstantString(version);
494 (void) RegisterMagickInfo(entry);
495 return(MagickImageCoderSignature);
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 % U n r e g i s t e r W E B P I m a g e %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 % UnregisterWEBPImage() removes format registrations made by the WebP module
510 % from the list of supported formats.
512 % The format of the UnregisterWEBPImage method is:
514 % UnregisterWEBPImage(void)
517 ModuleExport void UnregisterWEBPImage(void)
519 (void) UnregisterMagickInfo("WEBP");
521 #if defined(MAGICKCORE_WEBP_DELEGATE)
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % W r i t e W E B P I m a g e %
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % WriteWEBPImage() writes an image in the WebP image format.
536 % The format of the WriteWEBPImage method is:
538 % MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
541 % A description of each parameter follows.
543 % o image_info: the image info.
545 % o image: The image.
549 #if WEBP_DECODER_ABI_VERSION >= 0x0100
550 static int WebPEncodeProgress(int percent,const WebPPicture* picture)
552 #define EncodeImageTag "Encode/Image"
560 image=(Image *) picture->user_data;
561 status=SetImageProgress(image,EncodeImageTag,percent-1,100);
562 return(status == MagickFalse ? 0 : 1);
566 #if !defined(MAGICKCORE_WEBPMUX_DELEGATE)
567 static int WebPEncodeWriter(const unsigned char *stream,size_t length,
568 const WebPPicture *const picture)
573 image=(Image *) picture->custom_ptr;
574 return(length != 0 ? (WriteBlob(image,length,stream) == (ssize_t) length) : 1);
578 static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
579 Image *image,ExceptionInfo *exception)
605 #if defined(MAGICKCORE_WEBPMUX_DELEGATE)
614 Open output image file.
616 assert(image_info != (const ImageInfo *) NULL);
617 assert(image_info->signature == MagickCoreSignature);
618 assert(image != (Image *) NULL);
619 assert(image->signature == MagickCoreSignature);
620 if (image->debug != MagickFalse)
621 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
622 if ((image->columns > 16383UL) || (image->rows > 16383UL))
623 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
624 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
625 if (status == MagickFalse)
627 if ((WebPPictureInit(&picture) == 0) || (WebPConfigInit(&configure) == 0))
628 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
629 #if !defined(MAGICKCORE_WEBPMUX_DELEGATE)
630 picture.writer=WebPEncodeWriter;
631 picture.custom_ptr=(void *) image;
633 WebPMemoryWriterInit(&writer_info);
634 picture.writer=WebPMemoryWrite;
635 picture.custom_ptr=(&writer_info);
637 #if WEBP_DECODER_ABI_VERSION >= 0x0100
638 picture.progress_hook=WebPEncodeProgress;
639 picture.user_data=(void *) image;
641 picture.stats=(&statistics);
642 picture.width=(int) image->columns;
643 picture.height=(int) image->rows;
644 picture.argb_stride=(int) image->columns;
646 if (image->quality != UndefinedCompressionQuality)
647 configure.quality=(float) image->quality;
648 if (image->quality >= 100)
649 configure.lossless=1;
650 value=GetImageOption(image_info,"webp:lossless");
651 if (value != (char *) NULL)
652 configure.lossless=(int) ParseCommandOption(MagickBooleanOptions,
654 value=GetImageOption(image_info,"webp:method");
655 if (value != (char *) NULL)
656 configure.method=StringToInteger(value);
657 value=GetImageOption(image_info,"webp:image-hint");
658 if (value != (char *) NULL)
660 if (LocaleCompare(value,"default") == 0)
661 configure.image_hint=WEBP_HINT_DEFAULT;
662 if (LocaleCompare(value,"photo") == 0)
663 configure.image_hint=WEBP_HINT_PHOTO;
664 if (LocaleCompare(value,"picture") == 0)
665 configure.image_hint=WEBP_HINT_PICTURE;
666 #if WEBP_DECODER_ABI_VERSION >= 0x0200
667 if (LocaleCompare(value,"graph") == 0)
668 configure.image_hint=WEBP_HINT_GRAPH;
671 value=GetImageOption(image_info,"webp:target-size");
672 if (value != (char *) NULL)
673 configure.target_size=StringToInteger(value);
674 value=GetImageOption(image_info,"webp:target-psnr");
675 if (value != (char *) NULL)
676 configure.target_PSNR=(float) StringToDouble(value,(char **) NULL);
677 value=GetImageOption(image_info,"webp:segments");
678 if (value != (char *) NULL)
679 configure.segments=StringToInteger(value);
680 value=GetImageOption(image_info,"webp:sns-strength");
681 if (value != (char *) NULL)
682 configure.sns_strength=StringToInteger(value);
683 value=GetImageOption(image_info,"webp:filter-strength");
684 if (value != (char *) NULL)
685 configure.filter_strength=StringToInteger(value);
686 value=GetImageOption(image_info,"webp:filter-sharpness");
687 if (value != (char *) NULL)
688 configure.filter_sharpness=StringToInteger(value);
689 value=GetImageOption(image_info,"webp:filter-type");
690 if (value != (char *) NULL)
691 configure.filter_type=StringToInteger(value);
692 value=GetImageOption(image_info,"webp:auto-filter");
693 if (value != (char *) NULL)
694 configure.autofilter=(int) ParseCommandOption(MagickBooleanOptions,
696 value=GetImageOption(image_info,"webp:alpha-compression");
697 if (value != (char *) NULL)
698 configure.alpha_compression=StringToInteger(value);
699 value=GetImageOption(image_info,"webp:alpha-filtering");
700 if (value != (char *) NULL)
701 configure.alpha_filtering=StringToInteger(value);
702 value=GetImageOption(image_info,"webp:alpha-quality");
703 if (value != (char *) NULL)
704 configure.alpha_quality=StringToInteger(value);
705 value=GetImageOption(image_info,"webp:pass");
706 if (value != (char *) NULL)
707 configure.pass=StringToInteger(value);
708 value=GetImageOption(image_info,"webp:show-compressed");
709 if (value != (char *) NULL)
710 configure.show_compressed=StringToInteger(value);
711 value=GetImageOption(image_info,"webp:preprocessing");
712 if (value != (char *) NULL)
713 configure.preprocessing=StringToInteger(value);
714 value=GetImageOption(image_info,"webp:partitions");
715 if (value != (char *) NULL)
716 configure.partitions=StringToInteger(value);
717 value=GetImageOption(image_info,"webp:partition-limit");
718 if (value != (char *) NULL)
719 configure.partition_limit=StringToInteger(value);
720 #if WEBP_DECODER_ABI_VERSION >= 0x0201
721 value=GetImageOption(image_info,"webp:emulate-jpeg-size");
722 if (value != (char *) NULL)
723 configure.emulate_jpeg_size=(int) ParseCommandOption(MagickBooleanOptions,
725 value=GetImageOption(image_info,"webp:low-memory");
726 if (value != (char *) NULL)
727 configure.low_memory=(int) ParseCommandOption(MagickBooleanOptions,
729 value=GetImageOption(image_info,"webp:thread-level");
730 if (value != (char *) NULL)
731 configure.thread_level=StringToInteger(value);
733 if (WebPValidateConfig(&configure) == 0)
734 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
736 Allocate memory for pixels.
738 (void) TransformImageColorspace(image,sRGBColorspace,exception);
739 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
740 sizeof(*picture.argb));
741 if (pixel_info == (MemoryInfo *) NULL)
742 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
743 picture.argb=(uint32_t *) GetVirtualMemoryBlob(pixel_info);
745 Convert image to WebP raster pixels.
748 for (y=0; y < (ssize_t) image->rows; y++)
750 register const Quantum
756 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
757 if (p == (const Quantum *) NULL)
759 for (x=0; x < (ssize_t) image->columns; x++)
761 *q++=(uint32_t) (image->alpha_trait != UndefinedPixelTrait ? (uint32_t)
762 ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000) |
763 ((uint32_t) ScaleQuantumToChar(GetPixelRed(image,p)) << 16) |
764 ((uint32_t) ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) |
765 ((uint32_t) ScaleQuantumToChar(GetPixelBlue(image,p)));
766 p+=GetPixelChannels(image);
768 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
770 if (status == MagickFalse)
773 webp_status=WebPEncode(&configure,&picture);
774 if (webp_status == 0)
779 switch (picture.error_code)
781 case VP8_ENC_ERROR_OUT_OF_MEMORY:
783 message="out of memory";
786 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
788 message="bitstream out of memory";
791 case VP8_ENC_ERROR_NULL_PARAMETER:
793 message="NULL parameter";
796 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
798 message="invalid configuration";
801 case VP8_ENC_ERROR_BAD_DIMENSION:
803 message="bad dimension";
806 case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
808 message="partition 0 overflow (> 512K)";
811 case VP8_ENC_ERROR_PARTITION_OVERFLOW:
813 message="partition overflow (> 16M)";
816 case VP8_ENC_ERROR_BAD_WRITE:
821 case VP8_ENC_ERROR_FILE_TOO_BIG:
823 message="file too big (> 4GB)";
826 #if WEBP_DECODER_ABI_VERSION >= 0x0100
827 case VP8_ENC_ERROR_USER_ABORT:
829 message="user abort";
835 message="unknown exception";
839 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
840 (char *) message,"`%s'",image->filename);
842 #if defined(MAGICKCORE_WEBPMUX_DELEGATE)
849 image_chunk = { writer_info.mem, writer_info.size };
858 Set image profiles (if any).
860 mux_error=WEBP_MUX_OK;
861 (void) memset(&chunk,0,sizeof(chunk));
863 profile=GetImageProfile(image,"ICC");
864 if ((profile != (StringInfo *) NULL) && (mux_error == WEBP_MUX_OK))
866 chunk.bytes=GetStringInfoDatum(profile);
867 chunk.size=GetStringInfoLength(profile);
868 mux_error=WebPMuxSetChunk(mux,"ICCP",&chunk,0);
870 profile=GetImageProfile(image,"EXIF");
871 if ((profile != (StringInfo *) NULL) && (mux_error == WEBP_MUX_OK))
873 chunk.bytes=GetStringInfoDatum(profile);
874 chunk.size=GetStringInfoLength(profile);
875 mux_error=WebPMuxSetChunk(mux,"EXIF",&chunk,0);
877 profile=GetImageProfile(image,"XMP");
878 if ((profile != (StringInfo *) NULL) && (mux_error == WEBP_MUX_OK))
880 chunk.bytes=GetStringInfoDatum(profile);
881 chunk.size=GetStringInfoLength(profile);
882 mux_error=WebPMuxSetChunk(mux,"XMP",&chunk,0);
884 if (mux_error != WEBP_MUX_OK)
885 (void) ThrowMagickException(exception,GetMagickModule(),
886 ResourceLimitError,"UnableToEncodeImageFile","`%s'",image->filename);
890 picture_profiles = { writer_info.mem, writer_info.size };
893 Replace original container with image profile (if any).
895 WebPMuxSetImage(mux,&image_chunk,1);
896 mux_error=WebPMuxAssemble(mux,&picture_profiles);
897 WebPMemoryWriterClear(&writer_info);
898 writer_info.size=picture_profiles.size;
899 writer_info.mem=(unsigned char *) picture_profiles.bytes;
903 (void) WriteBlob(image,writer_info.size,writer_info.mem);
905 picture.argb=(uint32_t *) NULL;
906 WebPPictureFree(&picture);
907 #if defined(MAGICKCORE_WEBPMUX_DELEGATE)
908 WebPMemoryWriterClear(&writer_info);
910 pixel_info=RelinquishVirtualMemory(pixel_info);
911 (void) CloseBlob(image);
912 return(webp_status == 0 ? MagickFalse : MagickTrue);