summaryrefslogtreecommitdiff
path: root/coders/webp.c
blob: 54eb608208b64a77df9506309aea5e94ae58d7c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/*
% Copyright (C) 2013-2020 GraphicsMagick Group
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                         W   W  EEEEE  BBBB   PPPP                           %
%                         W   W  E      B   B  P   P                          %
%                         W W W  EEE    BBBB   PPPP                           %
%                         WW WW  E      B   B  P                              %
%                         W   W  EEEEE  BBBB   P                              %
%                                                                             %
%                                                                             %
%                     Read/Write Google WEBP Image Format.                    %
%                                                                             %
%                                                                             %
%                              Software Design                                %
%                                  TIMEBUG                                    %
%                                January 2013                                 %
%                                                                             %
%                          Subsequent Development By                          %
%                               Bob Friesenhahn                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/

/*
  Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/colormap.h"
#include "magick/log.h"
#include "magick/constitute.h"
#include "magick/magick.h"
#include "magick/monitor.h"
#include "magick/pixel_cache.h"
#include "magick/profile.h"
#include "magick/tsd.h"
#include "magick/utility.h"

/*
  Forward declarations.
*/
#if defined(HasWEBP)
static unsigned int WriteWEBPImage(const ImageInfo *,Image *);
#else
 #define WebPGetEncoderVersion()  (0)
 #define WEBP_ENCODER_ABI_VERSION 0
#endif

#if defined(HasWEBP)
#include <webp/decode.h>
#include <webp/encode.h>

/*
  Release versions vs ABI versions (found in src/webp/encode.h)

    0.1.3  - 0x0002
    0.1.99 - 0x0100 <-- earliest version we support
    0.2.0  - 0x0200
    0.2.1  - 0x0200
    0.3.0  - 0x0201
    0.4.0  - 0x0202
    0.4.1  - 0x0202
    0.4.2  - 0x0202
    0.4.3  - 0x0202
    0.4.4  - 0x0202
    0.5.0  - 0x0209
    0.5.1  - 0x0209
    0.5.2  - 0x0209
    0.6.0  - 0x020e
*/

/*
  Progress indication support not added until v0.1.99
*/
#if WEBP_ENCODER_ABI_VERSION >= 0x0100 /* >= v0.1.99 */
#  define SUPPORT_PROGRESS 1
#  define SUPPORT_USER_ABORT
#endif
#if WEBP_ENCODER_ABI_VERSION >= 0x0200 /* >= 0.2.0 */
#  define SUPPORT_CONFIG_WEBP_HINT_GRAPH
#endif
/* These relate to 'struct WebPConfig' parameters */
#if WEBP_ENCODER_ABI_VERSION >= 0x0201 /* >= 0.3.0 */
#  define SUPPORT_CONFIG_EMULATE_JPEG_SIZE
#  define SUPPORT_CONFIG_THREAD_LEVEL
#  define SUPPORT_CONFIG_LOW_MEMORY
#endif
#if WEBP_ENCODER_ABI_VERSION >= 0x0202 /* >= 0.4.0 */
#endif
#if WEBP_ENCODER_ABI_VERSION >= 0x0209 /* >= 0.5.0 */
#  define SUPPORT_WEBP_MUX
#include <webp/mux.h>
#endif
#if WEBP_ENCODER_ABI_VERSION >= 0x020e /* >= 0.6.0 */
#  define SUPPORT_CONFIG_USE_SHARP_YUV
#endif

/*
  Global declarations.
*/
static MagickTsdKey_t tsd_key = (MagickTsdKey_t) 0;


/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   R e a d W E B P I m a g e                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  ReadWEBPImage() reads an image in the WebP image format.
%
%  The format of the ReadWEBPImage method is:
%
%      Image *ReadWEBPImage(const ImageInfo *image_info,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image_info: the image info.
%
%    o exception: return any errors or warnings in this structure.
%
*/
static Image *ReadWEBPImage(const ImageInfo *image_info,
                            ExceptionInfo *exception)
{
  Image
    *image;

  unsigned long
    count,
    y;

  register PixelPacket
    *q;

  size_t
    length;

  register size_t
    x;

  register unsigned char
    *p;

  unsigned char
    *stream,
    *pixels;

  WebPBitstreamFeatures
    stream_features;

  int
    webp_status;

  /*
    Open image file.
  */
  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickSignature);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickSignature);
  image=AllocateImage(image_info);
  if (OpenBlob(image_info,image,ReadBinaryBlobMode,exception) == MagickFail)
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
  /*
    Read WEBP file.
  */
  length = (size_t) GetBlobSize(image); /* FIXME, does not work with stream */
  stream=MagickAllocateResourceLimitedArray(unsigned char *,
                                            length,sizeof(*stream));
  if (stream == (unsigned char *) NULL)
    ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);

  count=(long) ReadBlob(image,length,(char *) stream);
  if (count != (size_t) length)
    {
      MagickFreeResourceLimitedMemory(stream);
      ThrowReaderException(CorruptImageError,InsufficientImageDataInFile,image);
    }
  if ((webp_status=WebPGetFeatures(stream,length,&stream_features)) != VP8_STATUS_OK)
    {
      MagickFreeResourceLimitedMemory(stream);

      switch (webp_status)
        {
#if !defined(__COVERITY__)
        case VP8_STATUS_OK:
          break;
#else
        default:
          break;
#endif
        case VP8_STATUS_OUT_OF_MEMORY:
          ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
          break;
        case VP8_STATUS_INVALID_PARAM:
          ThrowReaderException(CoderError,WebPInvalidParameter,image);
          break;
        case VP8_STATUS_BITSTREAM_ERROR:
          ThrowReaderException(CorruptImageError,CorruptImage,image);
          break;
        case VP8_STATUS_UNSUPPORTED_FEATURE:
          ThrowReaderException(CoderError,DataEncodingSchemeIsNotSupported,image);
          break;
        case VP8_STATUS_SUSPENDED:
          /*
            Incremental decoder object may be left in SUSPENDED state
            if the picture is only partially decoded, pending
            additional input.  We are not doing incremental decoding
            at this time.
          */
          break;
        case VP8_STATUS_USER_ABORT:
          /*
            This is what is returned if the user terminates the
            decoding.
          */
          ThrowReaderException(CoderError,WebPDecodingFailedUserAbort,image);
          break;
        case VP8_STATUS_NOT_ENOUGH_DATA:
          ThrowReaderException(CorruptImageError,InsufficientImageDataInFile,image);
          break;
        }
      /*
        Catch-all if not handled above.
      */
      ThrowReaderException(CorruptImageError,CorruptImage,image);
    }
  image->depth=8;
  image->columns=(size_t) stream_features.width;
  image->rows=(size_t) stream_features.height;
  image->matte=(stream_features.has_alpha ? MagickTrue : MagickFalse);
  if (image->ping)
    {
      MagickFreeResourceLimitedMemory(stream);
      CloseBlob(image);
      StopTimer(&image->timer);
      return(image);
    }
  if (CheckImagePixelLimits(image, exception) != MagickPass)
    {
      MagickFreeResourceLimitedMemory(stream);
      ThrowReaderException(ResourceLimitError,ImagePixelLimitExceeded,image);
    }
  if (image->matte)
    pixels=(unsigned char *) WebPDecodeRGBA(stream,length,
                                            &stream_features.width,
                                            &stream_features.height);
  else
    pixels=(unsigned char *) WebPDecodeRGB(stream,length,
                                           &stream_features.width,
                                           &stream_features.height);
  if (pixels == (unsigned char *) NULL)
    {
      MagickFreeResourceLimitedMemory(stream);
      ThrowReaderException(CoderError,NoDataReturned,image);
    }

  p=pixels;

  for (y=0; y < (size_t) image->rows; y++)
    {
      q=GetImagePixelsEx(image,0,y,image->columns,1,exception);
      if (q == (PixelPacket *) NULL)
        break;

      for (x=0; x < (size_t) image->columns; x++)
        {
          SetRedSample(q,ScaleCharToQuantum(*p++));
          SetGreenSample(q,ScaleCharToQuantum(*p++));
          SetBlueSample(q,ScaleCharToQuantum(*p++));
          if (image->matte)
            SetOpacitySample(q,MaxRGB-ScaleCharToQuantum(*p++));
          else
            SetOpacitySample(q,OpaqueOpacity);
          q++;
        }

      if (!SyncImagePixels(image))
        break;
    }

#if defined(SUPPORT_WEBP_MUX)
  /*
    Read features out of the WebP container
    https://developers.google.com/speed/webp/docs/container-api
  */
  {
    uint32_t webp_flags=0;
    WebPData flag_data;
    WebPData content={stream,length};

    WebPMux *mux=WebPMuxCreate(&content,0);
    (void) memset(&flag_data,0,sizeof(flag_data));
    WebPMuxGetFeatures(mux,&webp_flags);

    if (webp_flags & ICCP_FLAG)
      {
        WebPMuxGetChunk(mux,"ICCP",&flag_data);
        SetImageProfile(image,"ICC",flag_data.bytes,flag_data.size);
      }

    if (webp_flags & EXIF_FLAG)
      {
        WebPMuxGetChunk(mux,"EXIF",&flag_data);
        SetImageProfile(image,"EXIF",flag_data.bytes,flag_data.size);
      }

    if (webp_flags & XMP_FLAG)
      {
        WebPMuxGetChunk(mux,"XMP",&flag_data);
        SetImageProfile(image,"XMP",flag_data.bytes,flag_data.size);
      }

    WebPMuxDelete(mux);
  }
#endif /* defined(SUPPORT_WEBP_MUX) */

  /*
    Free scale resource.
  */
  free(pixels);
  pixels=(unsigned char *) NULL;
  MagickFreeResourceLimitedMemory(stream);
  CloseBlob(image);
  StopTimer(&image->timer);
  return(image);
}
#endif

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   R e g i s t e r W E B P I m a g e                                         %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method RegisterWEBPImage adds attributes for the WEBP image format to
%  the list of supported formats.  The attributes include the image format
%  tag, a method to read and/or write the format, whether the format
%  supports the saving of more than one frame to the same file or blob,
%  whether the format supports native in-memory I/O, and a brief
%  description of the format.
%
%  The format of the RegisterWEBPImage method is:
%
%      RegisterWEBPImage(void)
%
*/
ModuleExport void RegisterWEBPImage(void)
{
  static const char
    description[] = "WebP Image Format";

  static char
    version[41];

  MagickInfo
    *entry;

  int
    web_encoder_version;

  unsigned int
    webp_major,
    webp_minor,
    webp_revision;

  *version='\0';

  /*
    Initialize thread specific data key.
  */
  if (tsd_key == (MagickTsdKey_t) 0)
    (void) MagickTsdKeyCreate(&tsd_key);

  /*
    Obtain the encoder's version number from the library, packed in
    hexadecimal using 8bits for each of major/minor/revision. E.g:
    v2.5.7 is 0x020507.

    Also capture the encoder ABI version from <webp/encode.h> which is
    in the form MAJOR(8b) + MINOR(8b) where ABI is related to the
    library ABI and not the package release version.
  */
  web_encoder_version=(WebPGetEncoderVersion());
  webp_major=(web_encoder_version >> 16) & 0xff;
  webp_minor=(web_encoder_version >> 8) & 0xff;
  webp_revision=web_encoder_version & 0xff;
  (void) sprintf(version,
                  "libwepb v%u.%u.%u, ENCODER ABI 0x%04X", webp_major,
                  webp_minor, webp_revision, WEBP_ENCODER_ABI_VERSION);

  entry=SetMagickInfo("WEBP");
#if defined(HasWEBP)
  entry->decoder=(DecoderHandler) ReadWEBPImage;
  entry->encoder=(EncoderHandler) WriteWEBPImage;
#endif
  entry->description=description;
  entry->adjoin=False;
  entry->seekable_stream=MagickTrue; /* FIXME */
  if (*version != '\0')
    entry->version=version;
  entry->module="WEBP";
  entry->coder_class=PrimaryCoderClass;
  (void) RegisterMagickInfo(entry);
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U n r e g i s t e r W E B P I m a g e                                     %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method UnregisterWEBPImage removes format registrations made by the
%  WEBP module from the list of supported formats.
%
%  The format of the UnregisterWEBPImage method is:
%
%      UnregisterWEBPImage(void)
%
*/
ModuleExport void UnregisterWEBPImage(void)
{
  (void) UnregisterMagickInfo("WEBP");

  /*
    Destroy thread specific data key.
  */
  if (tsd_key != (MagickTsdKey_t) 0)
    {
      (void) MagickTsdKeyDelete(tsd_key);
      tsd_key = (MagickTsdKey_t) 0;
    }
}

#if defined(HasWEBP)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e W E B P I m a g e                                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  WriteWEBPImage() writes an image in the WebP image format.
%
%  The format of the WriteWEBPImage method is:
%
%      MagickPassFail WriteWEBPImage(const ImageInfo *image_info, Image *image)
%
%  A description of each parameter follows.
%
%    o image_info: the image info.
%
%    o image:  The image.
%
*/

#if !defined(SUPPORT_WEBP_MUX)
 /*
  Called to write data to blob ("Should return true if writing was
  successful")
*/
static int WriterCallback(const unsigned char *stream,size_t length,
                          const WebPPicture *const picture)
{
  Image
    *image;

  image=(Image *) picture->custom_ptr;
  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  return (length != 0U ? (WriteBlob(image,length,stream) == length) :
          MagickTrue);
}
#endif /* !defined(SUPPORT_WEBP_MUX) */

/*
  Called to provide progress indication ("It can return false to
  request an abort of the encoding process, or true otherwise if
  everything is OK.")
*/
#if defined(SUPPORT_PROGRESS)
static int ProgressCallback(int percent, const WebPPicture* picture)
{
  Image
    *image;

  /*
    When the WebPMemoryWriter is used, it commandeers custom_ptr
    and the ProgressCallback no longer has access to image.

    We use thread specific data instead.
   */
  (void) picture;
  image=(Image *) MagickTsdGetSpecific(tsd_key);
#if 0
  image=(Image *) picture->custom_ptr;
#endif
  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  return MagickMonitorFormatted(percent, 101, &image->exception,
                                SaveImageText, image->filename,
                                image->columns, image->rows);
}
#endif /* defined(SUPPORT_PROGRESS) */

static unsigned int WriteWEBPImage(const ImageInfo *image_info,Image *image)
{
  const char
    *value;

  int
    webp_status;

  unsigned int
    status;

  register PixelPacket
    *p;

  register size_t
    x;

  register unsigned char
    *q;

  unsigned long
    y;

  size_t
    per_column;

  unsigned char
    *pixels;

  WebPConfig
    configure;

  WebPPicture
    picture;

#if defined(SUPPORT_WEBP_MUX)
  WebPMemoryWriter
    writer;
#endif /* defined(SUPPORT_WEBP_MUX) */

  WebPAuxStats
    statistics;

  /*
    Open output image file.
  */
  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickSignature);
  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  if ((image->columns > 16383) || (image->rows > 16383))
    ThrowWriterException(ImageError,WidthOrHeightExceedsLimit,image);
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
  if (status == MagickFail)
    ThrowWriterException(FileOpenError,UnableToOpenFile,image);
  /*
    Initialize WebP picture. This function is declared as 'static
    inline'.  It returns false if there is a mismatch between the
    libwebp headers and library.
  */
  if (WebPPictureInit(&picture) == 0)
    ThrowWriterException(DelegateError, WebPABIMismatch, image);
  /*
    Make sure that image is in an RGB type space and DirectClass.
  */
  (void) TransformColorspace(image,RGBColorspace);
  image->storage_class=DirectClass;
  picture.use_argb = 1;

  (void) MagickTsdSetSpecific(tsd_key,(void *) image);

#if !defined(SUPPORT_WEBP_MUX)
  picture.writer=WriterCallback;
  picture.custom_ptr=(void *) image;
#else
  WebPMemoryWriterInit(&writer);
  picture.writer=WebPMemoryWrite;
  picture.custom_ptr=&writer;
#endif
#if defined(SUPPORT_PROGRESS)
  picture.progress_hook=ProgressCallback;
#endif
  picture.stats=(&statistics);
  picture.width=(int) image->columns;
  picture.height=(int) image->rows;
  if (WebPConfigInit(&configure) == 0)
    ThrowWriterException(DelegateError, WebPABIMismatch, image);
  if (image_info->quality != DefaultCompressionQuality)
    configure.quality = (float) image_info->quality;

  if ((value=AccessDefinition(image_info,"webp","lossless")))
    configure.lossless=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
  if ((value=AccessDefinition(image_info,"webp","method")))
    configure.method=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","image-hint")))
    {
      if (LocaleCompare(value,"default") == 0)
        configure.image_hint=WEBP_HINT_DEFAULT;
      if (LocaleCompare(value,"picture") == 0)
        configure.image_hint=WEBP_HINT_PICTURE;
      if (LocaleCompare(value,"photo") == 0)
        configure.image_hint=WEBP_HINT_PHOTO;
#if defined(SUPPORT_CONFIG_WEBP_HINT_GRAPH)
      if (LocaleCompare(value,"graph") == 0)
        configure.image_hint=WEBP_HINT_GRAPH;
#endif
    }
  if ((value=AccessDefinition(image_info,"webp","target-size")))
    configure.target_size=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","target-psnr")))
    configure.target_PSNR=(float) MagickAtoF(value);
  if ((value=AccessDefinition(image_info,"webp","segments")))
    configure.segments=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","sns-strength")))
    configure.sns_strength=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","filter-strength")))
    configure.filter_strength=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","filter-sharpness")))
    configure.filter_sharpness=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","filter-type")))
    configure.filter_type=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","auto-filter")))
    configure.autofilter=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
  if ((value=AccessDefinition(image_info,"webp","alpha-compression")))
    configure.alpha_compression=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","alpha-filtering")))
    configure.alpha_filtering=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","alpha-quality")))
    configure.alpha_quality=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","pass")))
    configure.pass=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","show-compressed")))
    configure.show_compressed=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
  if ((value=AccessDefinition(image_info,"webp","preprocessing")))
    configure.preprocessing=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","partitions")))
    configure.partitions=MagickAtoI(value);
  if ((value=AccessDefinition(image_info,"webp","partition-limit")))
    configure.partition_limit=MagickAtoI(value);
#if defined(SUPPORT_CONFIG_EMULATE_JPEG_SIZE)
  if ((value=AccessDefinition(image_info,"webp","emulate-jpeg-size")))
    configure.emulate_jpeg_size=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
#endif
#if defined(SUPPORT_CONFIG_THREAD_LEVEL)
  if ((value=AccessDefinition(image_info,"webp","thread-level")))
    configure.thread_level=MagickAtoI(value);
#endif
#if defined(SUPPORT_CONFIG_LOW_MEMORY)
  if ((value=AccessDefinition(image_info,"webp","low-memory")))
    configure.low_memory=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
#endif
#if defined(SUPPORT_CONFIG_USE_SHARP_YUV)
  if ((value=AccessDefinition(image_info,"webp","use-sharp-yuv")))
    configure.use_sharp_yuv=(LocaleCompare(value,"TRUE") == 0 ? 1 : 0);
#endif
  if (WebPValidateConfig(&configure) != 1)
    ThrowWriterException(CoderError,WebPInvalidConfiguration,image);

  if (configure.lossless == 1)
    {
      /*
        Use ARGB input for lossless (YUVA input is lossy).
      */
      webp_status = WebPPictureAlloc(&picture);

      for (y = 0; y < image->rows; y++)
        {
          magick_uint32_t *s = picture.argb + (size_t)y * picture.argb_stride;
          p=GetImagePixelsEx(image,0,y,image->columns,1,&image->exception);
          if (p == (const PixelPacket *) NULL)
            break;
          for (x = 0; x < image->columns; x++)
            {
              *s = ((!image->matte ? 0xff000000u : (magick_uint32_t)
                     ScaleQuantumToChar(MaxRGB-GetOpacitySample(p)) << 24) |
                    (ScaleQuantumToChar(GetRedSample(p)) << 16) |
                    (ScaleQuantumToChar(GetGreenSample(p)) << 8) |
                    (ScaleQuantumToChar(GetBlueSample(p))));
              s++;
              p++;
            }
        }

    }
  else
    {
      /*
        Allocate memory for pixels.
      */
      per_column = MagickArraySize(MagickArraySize(4,image->rows),sizeof(*pixels));
      pixels=MagickAllocateResourceLimitedArray(unsigned char *,image->columns,per_column);
      if (pixels == (unsigned char *) NULL)
        ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);

      /*
        Convert image to WebP raster pixels.
      */
      q=pixels;
      for (y=0; y < image->rows; y++)
        {
          p=GetImagePixelsEx(image,0,y,image->columns,1,&image->exception);
          if (p == (const PixelPacket *) NULL)
            break;
          for (x=0; x < image->columns; x++)
            {
              *q++=ScaleQuantumToChar(GetRedSample(p));
              *q++=ScaleQuantumToChar(GetGreenSample(p));
              *q++=ScaleQuantumToChar(GetBlueSample(p));
              if (image->matte == MagickTrue)
                *q++=ScaleQuantumToChar(MaxRGB-GetOpacitySample(p));
              p++;
            }
        }

      /*
        "Returns false in case of memory error."
      */
      if (image->matte != MagickTrue)
        webp_status=WebPPictureImportRGB(&picture,pixels,3*picture.width);
      else
        webp_status=WebPPictureImportRGBA(&picture,pixels,4*picture.width);
      MagickFreeResourceLimitedMemory(pixels);
    }

  if (webp_status)
    {
      /*
        "Returns false in case of error, true otherwise.
        In case of error, picture->error_code is updated accordingly."
      */
      webp_status=WebPEncode(&configure, &picture);
      if (! webp_status)
        {
          int
            picture_error_code;

          picture_error_code=picture.error_code;
          WebPPictureFree(&picture);

          switch (picture_error_code)
            {
            case VP8_ENC_OK:
              break;
            case VP8_ENC_ERROR_OUT_OF_MEMORY:
              ThrowWriterException(CoderError,WebPEncodingFailedOutOfMemory,image);
              break;
            case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
              ThrowWriterException(CoderError,WebPEncodingFailedBitstreamOutOfMemory,image);
              break;
            case VP8_ENC_ERROR_NULL_PARAMETER:
              ThrowWriterException(CoderError,WebPEncodingFailedNULLParameter,image);
              break;
            case VP8_ENC_ERROR_INVALID_CONFIGURATION:
              ThrowWriterException(CoderError,WebPEncodingFailedInvalidConfiguration,image);
              break;
            case VP8_ENC_ERROR_BAD_DIMENSION:
              ThrowWriterException(CoderError,WebPEncodingFailedBadDimension,image);
              break;
            case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
              ThrowWriterException(CoderError,WebPEncodingFailedPartition0Overflow,image);
              break;
            case VP8_ENC_ERROR_PARTITION_OVERFLOW:
              ThrowWriterException(CoderError,WebPEncodingFailedPartitionOverflow,image);
              break;
            case VP8_ENC_ERROR_BAD_WRITE:
              ThrowWriterException(CoderError,WebPEncodingFailedBadWrite,image);
              break;
            case VP8_ENC_ERROR_FILE_TOO_BIG:
              ThrowWriterException(CoderError,WebPEncodingFailedFileTooBig,image);
              break;
#if defined(SUPPORT_USER_ABORT)
            case VP8_ENC_ERROR_USER_ABORT:     /* Added in v0.1.99 */
              ThrowWriterException(CoderError,WebPEncodingFailedUserAbort,image);
              break;
            case VP8_ENC_ERROR_LAST:           /* Added in v0.1.99 */
              break;
#endif
            } /* switch (picture_error_code) */
          /*
            Catch-all in case a code is added that we are not
            explicitly prepared for.
          */
          ThrowWriterException(CoderError,WebPEncodingFailed,image);
        } /* if (! webp_status) */
    } /* if (webp_status) */

#if defined(SUPPORT_WEBP_MUX)
  /* Write profiles */
  if (image->profiles)
    {
      size_t idx;

      /* Mapping of GraphicsMagick->libwebp feature/profile names */
      char data_features[][3][6]={{"ICC", "ICCP"},{"EXIF", "EXIF"},{"XMP", "XMP"}};

      /* Prepare the WebP muxer */
      WebPMuxError mux_error;
      WebPMux *mux=WebPMuxNew();
      WebPData encoded_image={writer.mem,writer.size};

      WebPMuxSetImage(mux,&encoded_image,1);

      /* Iterate over all available features and try to push them into the WebP container */
      for (idx=0;idx<sizeof(data_features)/sizeof(data_features[0]);idx++)
        {
          /* Get feature data */
          WebPData chunk;

          (void) memset(&chunk,0,sizeof(chunk));
          chunk.bytes=GetImageProfile(image,data_features[idx][0],&chunk.size);

          if (!chunk.bytes)
            continue;

          /* Write feature data */
          mux_error=WebPMuxSetChunk(mux,data_features[idx][1],&chunk,0);

          switch(mux_error) {
          case WEBP_MUX_OK:
            break;
            ;;
          case WEBP_MUX_BAD_DATA:
          case WEBP_MUX_NOT_ENOUGH_DATA:
          case WEBP_MUX_NOT_FOUND:
            ThrowWriterException(CoderError,WebPInvalidParameter,image);
            break;
            ;;
          case WEBP_MUX_INVALID_ARGUMENT:
            ThrowWriterException(CoderError,WebPEncodingFailedNULLParameter,image);
            break;
            ;;
          case WEBP_MUX_MEMORY_ERROR:
            ThrowWriterException(CoderError,WebPEncodingFailedOutOfMemory,image);
            break;
            ;;
          }
        }

      /* Create the new container */
      {
        WebPData picture_profiles={writer.mem, writer.size};
        mux_error=WebPMuxAssemble(mux,&picture_profiles);

        switch (mux_error) {
        case WEBP_MUX_OK:
          break;
          ;;
        case WEBP_MUX_BAD_DATA:
        case WEBP_MUX_NOT_ENOUGH_DATA:
        case WEBP_MUX_NOT_FOUND:
          ThrowWriterException(CoderError,WebPInvalidParameter,image);
        case WEBP_MUX_INVALID_ARGUMENT:
          ThrowWriterException(CoderError,WebPEncodingFailedNULLParameter,image);
          break;
          ;;
        case WEBP_MUX_MEMORY_ERROR:
          ThrowWriterException(CoderError,WebPEncodingFailedOutOfMemory,image);
          break;
          ;;
        }

        /* Replace the original data with the container data */
        WebPMemoryWriterClear(&writer);
        writer.size=picture_profiles.size;
        writer.mem=(unsigned char *)picture_profiles.bytes;

        /* Cleanup the muxer */
        WebPMuxDelete(mux);
      }
    } /* if (webp_status) */

  /* Write out the data to the blob and cleanup*/
  WriteBlob(image, writer.size, writer.mem);
#endif /* defined(SUPPORT_WEBP_MUX) */
  WebPPictureFree(&picture);
#if defined(SUPPORT_WEBP_MUX)
  WebPMemoryWriterClear(&writer);
#endif
  CloseBlob(image);

  return (webp_status ? MagickPass : MagickFail);
}
#endif