blob: 95ac4c9065da11c57c421ca8e6a41a8437779537 [file] [log] [blame]
cristyb1860752011-03-14 00:27:46 +00001/*
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 %
cristyef632a52013-12-03 11:06:33 +000016% John Cristy %
cristyb1860752011-03-14 00:27:46 +000017% March 2011 %
18% %
19% %
cristyef632a52013-12-03 11:06:33 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristyb1860752011-03-14 00:27:46 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristy76ce6e12013-04-05 14:33:38 +000043#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/client.h"
cristye65bb192013-08-01 11:22:06 +000047#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristye71e0892013-02-18 13:13:53 +000063#include "MagickCore/string-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/module.h"
65#include "MagickCore/utility.h"
66#include "MagickCore/xwindow.h"
67#include "MagickCore/xwindow-private.h"
cristy644040e2011-03-14 17:31:59 +000068#if defined(MAGICKCORE_WEBP_DELEGATE)
69#include <webp/decode.h>
70#include <webp/encode.h>
71#endif
cristyb1860752011-03-14 00:27:46 +000072
73/*
74 Forward declarations.
75*/
76#if defined(MAGICKCORE_WEBP_DELEGATE)
77static MagickBooleanType
cristy018f07f2011-09-04 21:15:19 +000078 WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
cristyb1860752011-03-14 00:27:46 +000079#endif
80
cristydffa79e2013-02-20 00:57:26 +000081/*
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*/
106static 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
cristyb1860752011-03-14 00:27:46 +0000115#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*/
cristy8f25aa82013-02-23 02:14:37 +0000141
cristy0b7a0332013-02-23 02:28:08 +0000142static inline uint32_t ReadWebPLSBWord(const unsigned char *restrict data)
cristy8f25aa82013-02-23 02:14:37 +0000143{
cristy0b7a0332013-02-23 02:28:08 +0000144 register const unsigned char
145 *p;
cristy8f25aa82013-02-23 02:14:37 +0000146
cristy0b7a0332013-02-23 02:28:08 +0000147 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);
cristy8f25aa82013-02-23 02:14:37 +0000156}
157
158static MagickBooleanType IsWEBPImageLossless(const unsigned char *stream,
159 const size_t length)
160{
cristy0b7a0332013-02-23 02:28:08 +0000161#define VP8_CHUNK_INDEX 15
cristy8f25aa82013-02-23 02:14:37 +0000162#define LOSSLESS_FLAG 'L'
163#define EXTENDED_HEADER 'X'
164#define VP8_CHUNK_HEADER "VP8"
165#define VP8_CHUNK_HEADER_SIZE 3
cristy0b7a0332013-02-23 02:28:08 +0000166#define RIFF_HEADER_SIZE 12
cristy8f25aa82013-02-23 02:14:37 +0000167#define VP8X_CHUNK_SIZE 10
168#define TAG_SIZE 4
169#define CHUNK_SIZE_BYTES 4
170#define CHUNK_HEADER_SIZE 8
cristya230a612014-01-05 20:51:47 +0000171#define MAX_CHUNK_PAYLOAD (~0U-CHUNK_HEADER_SIZE-1)
cristy8f25aa82013-02-23 02:14:37 +0000172
173 ssize_t
174 offset;
175
176 /*
177 Read simple header.
178 */
179 if (stream[VP8_CHUNK_INDEX] != EXTENDED_HEADER)
cristyef632a52013-12-03 11:06:33 +0000180 return(stream[VP8_CHUNK_INDEX] == LOSSLESS_FLAG ? MagickTrue : MagickFalse);
cristy8f25aa82013-02-23 02:14:37 +0000181 /*
182 Read extended header.
183 */
184 offset=RIFF_HEADER_SIZE+TAG_SIZE+CHUNK_SIZE_BYTES+VP8X_CHUNK_SIZE;
cristyb0de93f2013-05-03 13:39:25 +0000185 while (offset <= (ssize_t) length)
cristy8f25aa82013-02-23 02:14:37 +0000186 {
187 uint32_t
188 chunk_size,
189 chunk_size_pad;
190
cristyef978292013-02-23 02:32:06 +0000191 chunk_size=ReadWebPLSBWord(stream+offset+TAG_SIZE);
cristy8f25aa82013-02-23 02:14:37 +0000192 if (chunk_size > MAX_CHUNK_PAYLOAD)
193 break;
194 chunk_size_pad=(CHUNK_HEADER_SIZE+chunk_size+1) & ~1;
cristy5def2032013-06-30 17:44:08 +0000195 if (memcmp(stream+offset,VP8_CHUNK_HEADER,VP8_CHUNK_HEADER_SIZE) == 0)
cristyef978292013-02-23 02:32:06 +0000196 return(*(stream+offset+VP8_CHUNK_HEADER_SIZE) == LOSSLESS_FLAG ?
cristy8f25aa82013-02-23 02:14:37 +0000197 MagickTrue : MagickFalse);
198 offset+=chunk_size_pad;
199 }
200 return(MagickFalse);
201}
202
cristyb1860752011-03-14 00:27:46 +0000203static Image *ReadWEBPImage(const ImageInfo *image_info,
204 ExceptionInfo *exception)
205{
cristyffa663d2011-03-14 00:58:51 +0000206 Image
207 *image;
208
cristyef632a52013-12-03 11:06:33 +0000209 int
210 webp_status;
211
cristyffa663d2011-03-14 00:58:51 +0000212 MagickBooleanType
213 status;
214
cristy644040e2011-03-14 17:31:59 +0000215 register unsigned char
216 *p;
217
218 size_t
219 length;
220
221 ssize_t
222 count,
223 y;
224
225 unsigned char
cristyaf29b622013-02-18 15:06:48 +0000226 *stream;
227
228 WebPDecoderConfig
229 configure;
230
231 WebPDecBuffer
cristy0b7a0332013-02-23 02:28:08 +0000232 *restrict webp_image = &configure.output;
cristyaf29b622013-02-18 15:06:48 +0000233
234 WebPBitstreamFeatures
cristy0b7a0332013-02-23 02:28:08 +0000235 *restrict features = &configure.input;
cristy644040e2011-03-14 17:31:59 +0000236
cristyffa663d2011-03-14 00:58:51 +0000237 /*
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);
cristy9950d572011-10-01 18:22:35 +0000247 image=AcquireImage(image_info,exception);
cristyffa663d2011-03-14 00:58:51 +0000248 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
249 if (status == MagickFalse)
250 {
251 image=DestroyImageList(image);
252 return((Image *) NULL);
253 }
cristyaf29b622013-02-18 15:06:48 +0000254 if (WebPInitDecoderConfig(&configure) == 0)
255 ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile");
cristyef632a52013-12-03 11:06:33 +0000256 webp_image->colorspace=MODE_RGBA;
cristy644040e2011-03-14 17:31:59 +0000257 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");
cristyef632a52013-12-03 11:06:33 +0000264 webp_status=WebPGetFeatures(stream,length,features);
265 if (webp_status == VP8_STATUS_OK)
266 {
cristy106ce7d2013-12-12 13:02:12 +0000267 image->columns=(size_t) features->width;
268 image->rows=(size_t) features->height;
cristyef632a52013-12-03 11:06:33 +0000269 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)
cristyaf29b622013-02-18 15:06:48 +0000281 {
282 stream=(unsigned char*) RelinquishMagickMemory(stream);
cristyef632a52013-12-03 11:06:33 +0000283 switch (webp_status)
284 {
285 case VP8_STATUS_OUT_OF_MEMORY:
286 {
287 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
288 break;
289 }
cristyeb043f22014-04-06 00:30:25 +0000290 case VP8_STATUS_INVALID_PARAM:
291 {
292 ThrowReaderException(CorruptImageError,"invalid parameter");
293 break;
294 }
cristyef632a52013-12-03 11:06:33 +0000295 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 }
cristyeb043f22014-04-06 00:30:25 +0000305 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 }
cristyef632a52013-12-03 11:06:33 +0000315 case VP8_STATUS_NOT_ENOUGH_DATA:
316 {
317 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
318 break;
319 }
320 default:
321 ThrowReaderException(CorruptImageError,"CorruptImage");
322 }
cristyaf29b622013-02-18 15:06:48 +0000323 }
cristy8f25aa82013-02-23 02:14:37 +0000324 if (IsWEBPImageLossless(stream,length) != MagickFalse)
cristye71e0892013-02-18 13:13:53 +0000325 image->quality=100;
cristyb22ab412014-04-06 15:09:41 +0000326 p=(unsigned char *) webp_image->u.RGBA.rgba;
cristy644040e2011-03-14 17:31:59 +0000327 for (y=0; y < (ssize_t) image->rows; y++)
328 {
cristyaf29b622013-02-18 15:06:48 +0000329 register Quantum
330 *q;
331
332 register ssize_t
333 x;
334
cristy644040e2011-03-14 17:31:59 +0000335 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000336 if (q == (Quantum *) NULL)
cristy644040e2011-03-14 17:31:59 +0000337 break;
338 for (x=0; x < (ssize_t) image->columns; x++)
339 {
cristy4c08aed2011-07-01 19:47:50 +0000340 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
341 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
342 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
343 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
cristyed231572011-07-14 02:18:59 +0000344 q+=GetPixelChannels(image);
cristy644040e2011-03-14 17:31:59 +0000345 }
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 }
cristyaf29b622013-02-18 15:06:48 +0000353 WebPFreeDecBuffer(webp_image);
cristye71e0892013-02-18 13:13:53 +0000354 stream=(unsigned char*) RelinquishMagickMemory(stream);
cristyffa663d2011-03-14 00:58:51 +0000355 return(image);
cristyb1860752011-03-14 00:27:46 +0000356}
357#endif
358
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*/
382ModuleExport size_t RegisterWEBPImage(void)
383{
cristyaf29b622013-02-18 15:06:48 +0000384 char
385 version[MaxTextExtent];
386
cristyb1860752011-03-14 00:27:46 +0000387 MagickInfo
388 *entry;
389
cristyaf29b622013-02-18 15:06:48 +0000390 *version='\0';
cristy25ba0c02011-03-14 00:54:32 +0000391 entry=SetMagickInfo("WEBP");
cristyb1860752011-03-14 00:27:46 +0000392#if defined(MAGICKCORE_WEBP_DELEGATE)
393 entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
394 entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
cristyeb043f22014-04-06 00:30:25 +0000395 (void) FormatLocaleString(version,MaxTextExtent,"libwebp %d.%d.%d (%04X)",
cristyaf29b622013-02-18 15:06:48 +0000396 (WebPGetDecoderVersion() >> 16) & 0xff,
397 (WebPGetDecoderVersion() >> 8) & 0xff,
cristyeb043f22014-04-06 00:30:25 +0000398 (WebPGetDecoderVersion() >> 0) & 0xff,WEBP_ENCODER_ABI_VERSION);
cristyb1860752011-03-14 00:27:46 +0000399#endif
cristyb1860752011-03-14 00:27:46 +0000400 entry->description=ConstantString("WebP Image Format");
cristy99872d02014-02-05 12:26:14 +0000401 entry->mime_type=ConstantString("image/x-webp");
cristy644040e2011-03-14 17:31:59 +0000402 entry->adjoin=MagickFalse;
cristyb1860752011-03-14 00:27:46 +0000403 entry->module=ConstantString("WEBP");
cristydffa79e2013-02-20 00:57:26 +0000404 entry->magick=(IsImageFormatHandler *) IsWEBP;
cristyaf29b622013-02-18 15:06:48 +0000405 if (*version != '\0')
406 entry->version=ConstantString(version);
cristyb1860752011-03-14 00:27:46 +0000407 (void) RegisterMagickInfo(entry);
408 return(MagickImageCoderSignature);
409}
410
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*/
430ModuleExport void UnregisterWEBPImage(void)
431{
432 (void) UnregisterMagickInfo("WEBP");
433}
434#if defined(MAGICKCORE_WEBP_DELEGATE)
435
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*/
cristy644040e2011-03-14 17:31:59 +0000461
cristyb22ab412014-04-06 15:09:41 +0000462#if WEBP_ENCODER_ABI_VERSION >= 0x0100
463static int WebPEncodeProgress(int percent,const WebPPicture* picture)
464{
465 Image
466 *image;
467
468 MagickBooleanType
469 status;
470
471 image=(Image *) picture->custom_ptr;
472 status=SetImageProgress(image,SaveImageTag,percent-1,100);
473 return(status == MagickFalse ? 0 : 1);
474}
475#endif
476
477static int WebPEncodeWriter(const unsigned char *stream,size_t length,
cristy644040e2011-03-14 17:31:59 +0000478 const WebPPicture *const picture)
479{
480 Image
481 *image;
482
483 image=(Image *) picture->custom_ptr;
484 return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
485}
486
cristyb1860752011-03-14 00:27:46 +0000487static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000488 Image *image,ExceptionInfo *exception)
cristyb1860752011-03-14 00:27:46 +0000489{
cristye71e0892013-02-18 13:13:53 +0000490 const char
491 *value;
492
cristy644040e2011-03-14 17:31:59 +0000493 int
494 webp_status;
495
cristyffa663d2011-03-14 00:58:51 +0000496 MagickBooleanType
497 status;
498
cristy5def2032013-06-30 17:44:08 +0000499 MemoryInfo
500 *pixel_info;
501
cristye71e0892013-02-18 13:13:53 +0000502 register uint32_t
cristy644040e2011-03-14 17:31:59 +0000503 *restrict q;
504
505 ssize_t
506 y;
507
cristy644040e2011-03-14 17:31:59 +0000508 WebPConfig
509 configure;
510
511 WebPPicture
512 picture;
513
514 WebPAuxStats
515 statistics;
516
cristyffa663d2011-03-14 00:58:51 +0000517 /*
518 Open output image file.
519 */
520 assert(image_info != (const ImageInfo *) NULL);
521 assert(image_info->signature == MagickSignature);
522 assert(image != (Image *) NULL);
523 assert(image->signature == MagickSignature);
524 if (image->debug != MagickFalse)
525 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy57790302013-02-18 18:19:08 +0000526 if ((image->columns > 16383UL) || (image->rows > 16383UL))
cristye4d08fd2012-04-26 17:29:08 +0000527 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristyc82a27b2011-10-21 01:07:16 +0000528 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristyffa663d2011-03-14 00:58:51 +0000529 if (status == MagickFalse)
530 return(status);
cristyaf29b622013-02-18 15:06:48 +0000531 if ((WebPPictureInit(&picture) == 0) || (WebPConfigInit(&configure) == 0))
532 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
cristyb22ab412014-04-06 15:09:41 +0000533 picture.writer=WebPEncodeWriter;
cristy644040e2011-03-14 17:31:59 +0000534 picture.custom_ptr=(void *) image;
cristyb22ab412014-04-06 15:09:41 +0000535#if WEBP_ENCODER_ABI_VERSION >= 0x0100
536 picture.progress_hook=WebPEncodeProgress;
537#endif
cristy644040e2011-03-14 17:31:59 +0000538 picture.stats=(&statistics);
539 picture.width=(int) image->columns;
540 picture.height=(int) image->rows;
cristye71e0892013-02-18 13:13:53 +0000541 picture.argb_stride=(int) image->columns;
542 picture.use_argb=1;
cristy35c63c72012-10-23 14:40:54 +0000543 if (image->quality != UndefinedCompressionQuality)
544 configure.quality=(float) image->quality;
cristye71e0892013-02-18 13:13:53 +0000545 else
546 if (image->quality >= 100)
547 configure.lossless=1;
cristy092ec8d2013-04-26 13:46:22 +0000548 value=GetImageOption(image_info,"webp:lossless");
cristye71e0892013-02-18 13:13:53 +0000549 if (value != (char *) NULL)
cristyb22ab412014-04-06 15:09:41 +0000550 configure.lossless=(int) ParseCommandOption(MagickBooleanOptions,
551 MagickFalse,value);
cristy092ec8d2013-04-26 13:46:22 +0000552 value=GetImageOption(image_info,"webp:method");
cristye71e0892013-02-18 13:13:53 +0000553 if (value != (char *) NULL)
554 configure.method=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000555 value=GetImageOption(image_info,"webp:image-hint");
cristye71e0892013-02-18 13:13:53 +0000556 if (value != (char *) NULL)
cristyd6f2cc42013-02-18 17:24:19 +0000557 {
cristy547fe7f2013-02-18 17:27:13 +0000558 if (LocaleCompare(value,"graph") == 0)
cristyd6f2cc42013-02-18 17:24:19 +0000559 configure.image_hint=WEBP_HINT_GRAPH;
cristy547fe7f2013-02-18 17:27:13 +0000560 if (LocaleCompare(value,"photo") == 0)
cristyd6f2cc42013-02-18 17:24:19 +0000561 configure.image_hint=WEBP_HINT_PHOTO;
cristy547fe7f2013-02-18 17:27:13 +0000562 if (LocaleCompare(value,"picture") == 0)
cristyd6f2cc42013-02-18 17:24:19 +0000563 configure.image_hint=WEBP_HINT_PICTURE;
564 }
cristy092ec8d2013-04-26 13:46:22 +0000565 value=GetImageOption(image_info,"webp:target-size");
cristye71e0892013-02-18 13:13:53 +0000566 if (value != (char *) NULL)
567 configure.target_size=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000568 value=GetImageOption(image_info,"webp:target-psnr");
cristye71e0892013-02-18 13:13:53 +0000569 if (value != (char *) NULL)
570 configure.target_PSNR=(float) StringToDouble(value,(char **) NULL);
cristy092ec8d2013-04-26 13:46:22 +0000571 value=GetImageOption(image_info,"webp:segments");
cristye71e0892013-02-18 13:13:53 +0000572 if (value != (char *) NULL)
573 configure.segments=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000574 value=GetImageOption(image_info,"webp:sns-strength");
cristye71e0892013-02-18 13:13:53 +0000575 if (value != (char *) NULL)
576 configure.sns_strength=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000577 value=GetImageOption(image_info,"webp:filter-strength");
cristye71e0892013-02-18 13:13:53 +0000578 if (value != (char *) NULL)
579 configure.filter_strength=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000580 value=GetImageOption(image_info,"webp:filter-sharpness");
cristye71e0892013-02-18 13:13:53 +0000581 if (value != (char *) NULL)
582 configure.filter_sharpness=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000583 value=GetImageOption(image_info,"webp:filter-type");
cristye71e0892013-02-18 13:13:53 +0000584 if (value != (char *) NULL)
585 configure.filter_type=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000586 value=GetImageOption(image_info,"webp:auto-filter");
cristye71e0892013-02-18 13:13:53 +0000587 if (value != (char *) NULL)
cristyb22ab412014-04-06 15:09:41 +0000588 configure.autofilter=(int) ParseCommandOption(MagickBooleanOptions,
589 MagickFalse,value);
cristy092ec8d2013-04-26 13:46:22 +0000590 value=GetImageOption(image_info,"webp:alpha-compression");
cristye71e0892013-02-18 13:13:53 +0000591 if (value != (char *) NULL)
592 configure.alpha_compression=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000593 value=GetImageOption(image_info,"webp:alpha-filtering");
cristye71e0892013-02-18 13:13:53 +0000594 if (value != (char *) NULL)
595 configure.alpha_filtering=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000596 value=GetImageOption(image_info,"webp:alpha-quality");
cristye71e0892013-02-18 13:13:53 +0000597 if (value != (char *) NULL)
598 configure.alpha_quality=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000599 value=GetImageOption(image_info,"webp:pass");
cristye71e0892013-02-18 13:13:53 +0000600 if (value != (char *) NULL)
601 configure.pass=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000602 value=GetImageOption(image_info,"webp:show-compressed");
cristye71e0892013-02-18 13:13:53 +0000603 if (value != (char *) NULL)
604 configure.show_compressed=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000605 value=GetImageOption(image_info,"webp:preprocessing");
cristye71e0892013-02-18 13:13:53 +0000606 if (value != (char *) NULL)
607 configure.preprocessing=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000608 value=GetImageOption(image_info,"webp:partitions");
cristye71e0892013-02-18 13:13:53 +0000609 if (value != (char *) NULL)
610 configure.partitions=StringToInteger(value);
cristy092ec8d2013-04-26 13:46:22 +0000611 value=GetImageOption(image_info,"webp:partition-limit");
cristye71e0892013-02-18 13:13:53 +0000612 if (value != (char *) NULL)
613 configure.partition_limit=StringToInteger(value);
cristy644040e2011-03-14 17:31:59 +0000614 if (WebPValidateConfig(&configure) == 0)
615 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
616 /*
617 Allocate memory for pixels.
618 */
cristyaf8d3912014-02-21 14:50:33 +0000619 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy5def2032013-06-30 17:44:08 +0000620 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
cristye71e0892013-02-18 13:13:53 +0000621 sizeof(*picture.argb));
cristy5def2032013-06-30 17:44:08 +0000622 if (pixel_info == (MemoryInfo *) NULL)
cristy644040e2011-03-14 17:31:59 +0000623 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy5def2032013-06-30 17:44:08 +0000624 picture.argb=(uint32_t *) GetVirtualMemoryBlob(pixel_info);
cristy644040e2011-03-14 17:31:59 +0000625 /*
626 Convert image to WebP raster pixels.
627 */
cristye71e0892013-02-18 13:13:53 +0000628 q=picture.argb;
cristy644040e2011-03-14 17:31:59 +0000629 for (y=0; y < (ssize_t) image->rows; y++)
630 {
cristyaf29b622013-02-18 15:06:48 +0000631 register const Quantum
632 *restrict p;
633
634 register ssize_t
635 x;
636
cristyc82a27b2011-10-21 01:07:16 +0000637 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000638 if (p == (const Quantum *) NULL)
cristy644040e2011-03-14 17:31:59 +0000639 break;
640 for (x=0; x < (ssize_t) image->columns; x++)
641 {
cristyaf29b622013-02-18 15:06:48 +0000642 *q++=(uint32_t) (image->alpha_trait == BlendPixelTrait ?
cristyb0de93f2013-05-03 13:39:25 +0000643 ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000) |
cristyaf29b622013-02-18 15:06:48 +0000644 (ScaleQuantumToChar(GetPixelRed(image,p)) << 16) |
645 (ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) |
646 (ScaleQuantumToChar(GetPixelBlue(image,p)));
cristyed231572011-07-14 02:18:59 +0000647 p+=GetPixelChannels(image);
cristy644040e2011-03-14 17:31:59 +0000648 }
649 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
650 image->rows);
651 if (status == MagickFalse)
652 break;
653 }
cristy644040e2011-03-14 17:31:59 +0000654 webp_status=WebPEncode(&configure,&picture);
cristyeb043f22014-04-06 00:30:25 +0000655 if (webp_status != VP8_ENC_OK)
cristyf13926d2014-03-13 12:38:55 +0000656 {
657 const char
658 *message;
659
660 switch (picture.error_code)
661 {
662 case VP8_ENC_ERROR_OUT_OF_MEMORY:
663 {
664 message="out of memory";
665 break;
666 }
667 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
668 {
669 message="bitstream out of memory";
670 break;
671 }
672 case VP8_ENC_ERROR_NULL_PARAMETER:
673 {
674 message="NULL parameter";
675 break;
676 }
677 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
678 {
679 message="invalid configuration";
680 break;
681 }
682 case VP8_ENC_ERROR_BAD_DIMENSION:
683 {
684 message="bad dimension";
685 break;
686 }
687 case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
688 {
689 message="partition 0 overflow (> 512K)";
690 break;
691 }
692 case VP8_ENC_ERROR_PARTITION_OVERFLOW:
693 {
694 message="partition overflow (> 16M)";
695 break;
696 }
697 case VP8_ENC_ERROR_BAD_WRITE:
698 {
699 message="bad write";
700 break;
701 }
702 case VP8_ENC_ERROR_FILE_TOO_BIG:
703 {
704 message="file too big (> 4GB)";
705 break;
706 }
707#if WEBP_ENCODER_ABI_VERSION >= 0x0100
708 case VP8_ENC_ERROR_USER_ABORT:
709 {
710 message="user abort";
711 break;
712 }
713#endif
714 default:
715 {
716 message="unknown exception";
717 break;
718 }
719 }
720 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
721 (char *) message,"`%s'",image->filename);
722 }
cristy05df1122012-09-26 09:43:12 +0000723 WebPPictureFree(&picture);
cristy5def2032013-06-30 17:44:08 +0000724 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy644040e2011-03-14 17:31:59 +0000725 (void) CloseBlob(image);
726 return(webp_status == 0 ? MagickFalse : MagickTrue);
cristyb1860752011-03-14 00:27:46 +0000727}
728#endif