2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % W W EEEEE BBBB PPPP %
8 % W W W EEE BBBB PPPP %
13 % Read/Write WebP Image Format %
20 % Copyright 1999-2011 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/module.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/xwindow.h"
64 #include "MagickCore/xwindow-private.h"
65 #if defined(MAGICKCORE_WEBP_DELEGATE)
66 #include <webp/decode.h>
67 #include <webp/encode.h>
73 #if defined(MAGICKCORE_WEBP_DELEGATE)
74 static MagickBooleanType
75 WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
78 #if defined(MAGICKCORE_WEBP_DELEGATE)
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % R e a d W E B P I m a g e %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 % ReadWEBPImage() reads an image in the WebP image format.
92 % The format of the ReadWEBPImage method is:
94 % Image *ReadWEBPImage(const ImageInfo *image_info,
95 % ExceptionInfo *exception)
97 % A description of each parameter follows:
99 % o image_info: the image info.
101 % o exception: return any errors or warnings in this structure.
104 static Image *ReadWEBPImage(const ImageInfo *image_info,
105 ExceptionInfo *exception)
123 register unsigned char
140 assert(image_info != (const ImageInfo *) NULL);
141 assert(image_info->signature == MagickSignature);
142 if (image_info->debug != MagickFalse)
143 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
144 image_info->filename);
145 assert(exception != (ExceptionInfo *) NULL);
146 assert(exception->signature == MagickSignature);
147 image=AcquireImage(image_info,exception);
148 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
149 if (status == MagickFalse)
151 image=DestroyImageList(image);
152 return((Image *) NULL);
154 length=(size_t) GetBlobSize(image);
155 stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
156 if (stream == (unsigned char *) NULL)
157 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
158 count=ReadBlob(image,length,stream);
159 if (count != (ssize_t) length)
160 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
161 pixels=(unsigned char *) WebPDecodeRGBA(stream,length,&width,&height);
162 if (pixels == (unsigned char *) NULL)
163 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
164 image->columns=(size_t) width;
165 image->rows=(size_t) height;
167 for (y=0; y < (ssize_t) image->rows; y++)
169 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
170 if (q == (Quantum *) NULL)
172 for (x=0; x < (ssize_t) image->columns; x++)
174 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
175 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
176 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
177 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
178 if (GetPixelAlpha(image,q) != OpaqueAlpha)
179 image->matte=MagickTrue;
180 q+=GetPixelChannels(image);
182 if (SyncAuthenticPixels(image,exception) == MagickFalse)
184 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
186 if (status == MagickFalse)
190 pixels=(unsigned char *) NULL;
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 % R e g i s t e r W E B P I m a g e %
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % RegisterWEBPImage() adds attributes for the WebP image format to
207 % the list of supported formats. The attributes include the image format
208 % tag, a method to read and/or write the format, whether the format
209 % supports the saving of more than one frame to the same file or blob,
210 % whether the format supports native in-memory I/O, and a brief
211 % description of the format.
213 % The format of the RegisterWEBPImage method is:
215 % size_t RegisterWEBPImage(void)
218 ModuleExport size_t RegisterWEBPImage(void)
223 entry=SetMagickInfo("WEBP");
224 #if defined(MAGICKCORE_WEBP_DELEGATE)
225 entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
226 entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
228 entry->description=ConstantString("WebP Image Format");
229 entry->adjoin=MagickFalse;
230 entry->module=ConstantString("WEBP");
231 (void) RegisterMagickInfo(entry);
232 return(MagickImageCoderSignature);
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % U n r e g i s t e r W E B P I m a g e %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % UnregisterWEBPImage() removes format registrations made by the WebP module
247 % from the list of supported formats.
249 % The format of the UnregisterWEBPImage method is:
251 % UnregisterWEBPImage(void)
254 ModuleExport void UnregisterWEBPImage(void)
256 (void) UnregisterMagickInfo("WEBP");
258 #if defined(MAGICKCORE_WEBP_DELEGATE)
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % W r i t e W E B P I m a g e %
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % WriteWEBPImage() writes an image in the WebP image format.
273 % The format of the WriteWEBPImage method is:
275 % MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
278 % A description of each parameter follows.
280 % o image_info: the image info.
282 % o image: The image.
286 static int WebPWriter(const unsigned char *stream,size_t length,
287 const WebPPicture *const picture)
292 image=(Image *) picture->custom_ptr;
293 return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
296 static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
297 Image *image,ExceptionInfo *exception)
305 register const Quantum
311 register unsigned char
330 Open output image file.
332 assert(image_info != (const ImageInfo *) NULL);
333 assert(image_info->signature == MagickSignature);
334 assert(image != (Image *) NULL);
335 assert(image->signature == MagickSignature);
336 if (image->debug != MagickFalse)
337 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
338 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
339 if (status == MagickFalse)
341 if (WebPPictureInit(&picture) == 0)
342 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
343 picture.writer=WebPWriter;
344 picture.custom_ptr=(void *) image;
345 picture.stats=(&statistics);
346 picture.width=(int) image->columns;
347 picture.height=(int) image->rows;
348 if (image->quality != UndefinedCompressionQuality)
349 configure.quality=(float) image->quality;
350 if (WebPConfigInit(&configure) == 0)
351 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
353 Future: set custom configuration parameters here.
355 if (WebPValidateConfig(&configure) == 0)
356 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
358 Allocate memory for pixels.
360 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
361 (image->matte != MagickFalse ? 4 : 3)*image->rows*sizeof(*pixels));
362 if (pixels == (unsigned char *) NULL)
363 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
365 Convert image to WebP raster pixels.
368 for (y=0; y < (ssize_t) image->rows; y++)
370 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
371 if (p == (const Quantum *) NULL)
373 for (x=0; x < (ssize_t) image->columns; x++)
375 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
376 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
377 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
378 if (image->matte != MagickFalse)
379 *q++=ScaleQuantumToChar((Quantum) (image->matte != MagickFalse ?
380 GetPixelAlpha(image,p) : OpaqueAlpha));
381 p+=GetPixelChannels(image);
383 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
385 if (status == MagickFalse)
388 if (image->matte == MagickFalse)
389 webp_status=WebPPictureImportRGB(&picture,pixels,3*picture.width);
391 webp_status=WebPPictureImportRGBA(&picture,pixels,4*picture.width);
392 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
393 webp_status=WebPEncode(&configure,&picture);
394 (void) CloseBlob(image);
395 return(webp_status == 0 ? MagickFalse : MagickTrue);