2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % W W EEEEE BBBB PPPP %
8 % W W W EEE BBBB PPPP %
13 % Read/Write WebP Image Format %
20 % Copyright 1999-2013 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 % http://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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/client.h"
46 #include "MagickCore/display.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/monitor.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/option.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/string-private.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/utility.h"
64 #include "MagickCore/xwindow.h"
65 #include "MagickCore/xwindow-private.h"
66 #if defined(MAGICKCORE_WEBP_DELEGATE)
67 #include <webp/decode.h>
68 #include <webp/encode.h>
74 #if defined(MAGICKCORE_WEBP_DELEGATE)
75 static MagickBooleanType
76 WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
79 #if defined(MAGICKCORE_WEBP_DELEGATE)
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % R e a d W E B P I m a g e %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % ReadWEBPImage() reads an image in the WebP image format.
93 % The format of the ReadWEBPImage method is:
95 % Image *ReadWEBPImage(const ImageInfo *image_info,
96 % ExceptionInfo *exception)
98 % A description of each parameter follows:
100 % o image_info: the image info.
102 % o exception: return any errors or warnings in this structure.
105 static Image *ReadWEBPImage(const ImageInfo *image_info,
106 ExceptionInfo *exception)
114 register unsigned char
131 *const webp_image = &configure.output;
133 WebPBitstreamFeatures
134 *const features = &configure.input;
139 assert(image_info != (const ImageInfo *) NULL);
140 assert(image_info->signature == MagickSignature);
141 if (image_info->debug != MagickFalse)
142 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
143 image_info->filename);
144 assert(exception != (ExceptionInfo *) NULL);
145 assert(exception->signature == MagickSignature);
146 image=AcquireImage(image_info,exception);
147 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
148 if (status == MagickFalse)
150 image=DestroyImageList(image);
151 return((Image *) NULL);
153 if (WebPInitDecoderConfig(&configure) == 0)
154 ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
155 length=(size_t) GetBlobSize(image);
156 stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
157 if (stream == (unsigned char *) NULL)
158 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
159 count=ReadBlob(image,length,stream);
160 if (count != (ssize_t) length)
161 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
162 if (WebPGetFeatures(stream,length,features) != 0)
164 stream=(unsigned char*) RelinquishMagickMemory(stream);
165 ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
167 webp_image->colorspace=MODE_RGBA;
168 if (WebPDecode(stream,length,&configure) != 0)
170 stream=(unsigned char*) RelinquishMagickMemory(stream);
171 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
173 image->columns=(size_t) webp_image->width;
174 image->rows=(size_t) webp_image->height;
175 image->alpha_trait=features->has_alpha != 0 ? BlendPixelTrait :
177 if ((stream[15] == 'L') || (stream[15] == ' '))
179 p=webp_image->u.RGBA.rgba;
180 for (y=0; y < (ssize_t) image->rows; y++)
188 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
189 if (q == (Quantum *) NULL)
191 for (x=0; x < (ssize_t) image->columns; x++)
193 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
194 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
195 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
196 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
197 q+=GetPixelChannels(image);
199 if (SyncAuthenticPixels(image,exception) == MagickFalse)
201 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
203 if (status == MagickFalse)
206 WebPFreeDecBuffer(webp_image);
207 stream=(unsigned char*) RelinquishMagickMemory(stream);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % R e g i s t e r W E B P I m a g e %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % RegisterWEBPImage() adds attributes for the WebP image format to
224 % the list of supported formats. The attributes include the image format
225 % tag, a method to read and/or write the format, whether the format
226 % supports the saving of more than one frame to the same file or blob,
227 % whether the format supports native in-memory I/O, and a brief
228 % description of the format.
230 % The format of the RegisterWEBPImage method is:
232 % size_t RegisterWEBPImage(void)
235 ModuleExport size_t RegisterWEBPImage(void)
238 version[MaxTextExtent];
244 entry=SetMagickInfo("WEBP");
245 #if defined(MAGICKCORE_WEBP_DELEGATE)
246 entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
247 entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
248 (void) FormatLocaleString(version,MaxTextExtent,"libwebp %d.%d.%d",
249 (WebPGetDecoderVersion() >> 16) & 0xff,
250 (WebPGetDecoderVersion() >> 8) & 0xff,
251 (WebPGetDecoderVersion() >> 0) & 0xff);
253 entry->description=ConstantString("WebP Image Format");
254 entry->adjoin=MagickFalse;
255 entry->module=ConstantString("WEBP");
256 if (*version != '\0')
257 entry->version=ConstantString(version);
258 (void) RegisterMagickInfo(entry);
259 return(MagickImageCoderSignature);
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % U n r e g i s t e r W E B P I m a g e %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % UnregisterWEBPImage() removes format registrations made by the WebP module
274 % from the list of supported formats.
276 % The format of the UnregisterWEBPImage method is:
278 % UnregisterWEBPImage(void)
281 ModuleExport void UnregisterWEBPImage(void)
283 (void) UnregisterMagickInfo("WEBP");
285 #if defined(MAGICKCORE_WEBP_DELEGATE)
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % W r i t e W E B P I m a g e %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % WriteWEBPImage() writes an image in the WebP image format.
300 % The format of the WriteWEBPImage method is:
302 % MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
305 % A description of each parameter follows.
307 % o image_info: the image info.
309 % o image: The image.
313 static int WebPWriter(const unsigned char *stream,size_t length,
314 const WebPPicture *const picture)
319 image=(Image *) picture->custom_ptr;
320 return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
323 static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
324 Image *image,ExceptionInfo *exception)
351 Open output image file.
353 assert(image_info != (const ImageInfo *) NULL);
354 assert(image_info->signature == MagickSignature);
355 assert(image != (Image *) NULL);
356 assert(image->signature == MagickSignature);
357 if (image->debug != MagickFalse)
358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
359 if ((image->columns > 16383UL) || (image->rows > 16383UL))
360 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
361 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
362 if (status == MagickFalse)
364 if ((WebPPictureInit(&picture) == 0) || (WebPConfigInit(&configure) == 0))
365 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
366 picture.writer=WebPWriter;
367 picture.custom_ptr=(void *) image;
368 picture.stats=(&statistics);
369 picture.width=(int) image->columns;
370 picture.height=(int) image->rows;
371 picture.argb_stride=(int) image->columns;
373 if (image->quality != UndefinedCompressionQuality)
374 configure.quality=(float) image->quality;
376 if (image->quality >= 100)
377 configure.lossless=1;
378 value=GetImageOption(image_info,"webp:lossless");
379 if (value != (char *) NULL)
380 configure.lossless=ParseCommandOption(MagickBooleanOptions,MagickFalse,
382 value=GetImageOption(image_info,"webp:method");
383 if (value != (char *) NULL)
384 configure.method=StringToInteger(value);
385 value=GetImageOption(image_info,"webp:image-hint");
386 if (value != (char *) NULL)
388 if (LocaleCompare(value,"graph") == 0)
389 configure.image_hint=WEBP_HINT_GRAPH;
390 if (LocaleCompare(value,"photo") == 0)
391 configure.image_hint=WEBP_HINT_PHOTO;
392 if (LocaleCompare(value,"picture") == 0)
393 configure.image_hint=WEBP_HINT_PICTURE;
395 value=GetImageOption(image_info,"webp:target-size");
396 if (value != (char *) NULL)
397 configure.target_size=StringToInteger(value);
398 value=GetImageOption(image_info,"webp:target-psnr");
399 if (value != (char *) NULL)
400 configure.target_PSNR=(float) StringToDouble(value,(char **) NULL);
401 value=GetImageOption(image_info,"webp:segments");
402 if (value != (char *) NULL)
403 configure.segments=StringToInteger(value);
404 value=GetImageOption(image_info,"webp:sns-strength");
405 if (value != (char *) NULL)
406 configure.sns_strength=StringToInteger(value);
407 value=GetImageOption(image_info,"webp:filter-strength");
408 if (value != (char *) NULL)
409 configure.filter_strength=StringToInteger(value);
410 value=GetImageOption(image_info,"webp:filter-sharpness");
411 if (value != (char *) NULL)
412 configure.filter_sharpness=StringToInteger(value);
413 value=GetImageOption(image_info,"webp:filter-type");
414 if (value != (char *) NULL)
415 configure.filter_type=StringToInteger(value);
416 value=GetImageOption(image_info,"webp:auto-filter");
417 if (value != (char *) NULL)
418 configure.autofilter=ParseCommandOption(MagickBooleanOptions,MagickFalse,
420 value=GetImageOption(image_info,"webp:alpha-compression");
421 if (value != (char *) NULL)
422 configure.alpha_compression=StringToInteger(value);
423 value=GetImageOption(image_info,"webp:alpha-filtering");
424 if (value != (char *) NULL)
425 configure.alpha_filtering=StringToInteger(value);
426 value=GetImageOption(image_info,"webp:alpha-quality");
427 if (value != (char *) NULL)
428 configure.alpha_quality=StringToInteger(value);
429 value=GetImageOption(image_info,"webp:pass");
430 if (value != (char *) NULL)
431 configure.pass=StringToInteger(value);
432 value=GetImageOption(image_info,"webp:show-compressed");
433 if (value != (char *) NULL)
434 configure.show_compressed=StringToInteger(value);
435 value=GetImageOption(image_info,"webp:preprocessing");
436 if (value != (char *) NULL)
437 configure.preprocessing=StringToInteger(value);
438 value=GetImageOption(image_info,"webp:partitions");
439 if (value != (char *) NULL)
440 configure.partitions=StringToInteger(value);
441 value=GetImageOption(image_info,"webp:partition-limit");
442 if (value != (char *) NULL)
443 configure.partition_limit=StringToInteger(value);
444 if (WebPValidateConfig(&configure) == 0)
445 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
447 Allocate memory for pixels.
449 picture.argb=(uint32_t *) AcquireQuantumMemory(image->columns,image->rows*
450 sizeof(*picture.argb));
451 if (picture.argb == (uint32_t *) NULL)
452 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
454 Convert image to WebP raster pixels.
457 for (y=0; y < (ssize_t) image->rows; y++)
459 register const Quantum
465 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
466 if (p == (const Quantum *) NULL)
468 for (x=0; x < (ssize_t) image->columns; x++)
470 *q++=(uint32_t) (image->alpha_trait == BlendPixelTrait ?
471 ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000u) |
472 (ScaleQuantumToChar(GetPixelRed(image,p)) << 16) |
473 (ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) |
474 (ScaleQuantumToChar(GetPixelBlue(image,p)));
475 p+=GetPixelChannels(image);
477 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
479 if (status == MagickFalse)
482 webp_status=WebPEncode(&configure,&picture);
483 WebPPictureFree(&picture);
484 picture.argb=(uint32_t *) RelinquishMagickMemory(picture.argb);
485 (void) CloseBlob(image);
486 return(webp_status == 0 ? MagickFalse : MagickTrue);