blob: c5c51cfe34782e173b630304a24b1c032c0ea5b6 [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 %
16% John Cristy %
17% March 2011 %
18% %
19% %
20% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21% 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"
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"
cristy644040e2011-03-14 17:31:59 +000065#if defined(MAGICKCORE_WEBP_DELEGATE)
66#include <webp/decode.h>
67#include <webp/encode.h>
68#endif
cristyb1860752011-03-14 00:27:46 +000069
70/*
71 Forward declarations.
72*/
73#if defined(MAGICKCORE_WEBP_DELEGATE)
74static MagickBooleanType
cristy018f07f2011-09-04 21:15:19 +000075 WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *);
cristyb1860752011-03-14 00:27:46 +000076#endif
77
78#if defined(MAGICKCORE_WEBP_DELEGATE)
79/*
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81% %
82% %
83% %
84% R e a d W E B P I m a g e %
85% %
86% %
87% %
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89%
90% ReadWEBPImage() reads an image in the WebP image format.
91%
92% The format of the ReadWEBPImage method is:
93%
94% Image *ReadWEBPImage(const ImageInfo *image_info,
95% ExceptionInfo *exception)
96%
97% A description of each parameter follows:
98%
99% o image_info: the image info.
100%
101% o exception: return any errors or warnings in this structure.
102%
103*/
104static Image *ReadWEBPImage(const ImageInfo *image_info,
105 ExceptionInfo *exception)
106{
cristy644040e2011-03-14 17:31:59 +0000107 int
108 height,
109 width;
110
cristyffa663d2011-03-14 00:58:51 +0000111 Image
112 *image;
113
114 MagickBooleanType
115 status;
116
cristy4c08aed2011-07-01 19:47:50 +0000117 register Quantum
cristy644040e2011-03-14 17:31:59 +0000118 *q;
119
120 register ssize_t
121 x;
122
123 register unsigned char
124 *p;
125
126 size_t
127 length;
128
129 ssize_t
130 count,
131 y;
132
133 unsigned char
134 *stream,
135 *pixels;
136
cristyffa663d2011-03-14 00:58:51 +0000137 /*
138 Open image file.
139 */
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);
cristy9950d572011-10-01 18:22:35 +0000147 image=AcquireImage(image_info,exception);
cristyffa663d2011-03-14 00:58:51 +0000148 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
149 if (status == MagickFalse)
150 {
151 image=DestroyImageList(image);
152 return((Image *) NULL);
153 }
cristy644040e2011-03-14 17:31:59 +0000154 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;
166 p=pixels;
167 for (y=0; y < (ssize_t) image->rows; y++)
168 {
169 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000170 if (q == (Quantum *) NULL)
cristy644040e2011-03-14 17:31:59 +0000171 break;
172 for (x=0; x < (ssize_t) image->columns; x++)
173 {
cristy4c08aed2011-07-01 19:47:50 +0000174 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)
cristy644040e2011-03-14 17:31:59 +0000179 image->matte=MagickTrue;
cristyed231572011-07-14 02:18:59 +0000180 q+=GetPixelChannels(image);
cristy644040e2011-03-14 17:31:59 +0000181 }
182 if (SyncAuthenticPixels(image,exception) == MagickFalse)
183 break;
184 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
185 image->rows);
186 if (status == MagickFalse)
187 break;
188 }
189 free(pixels);
190 pixels=(unsigned char *) NULL;
cristyffa663d2011-03-14 00:58:51 +0000191 return(image);
cristyb1860752011-03-14 00:27:46 +0000192}
193#endif
194
195/*
196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197% %
198% %
199% %
200% R e g i s t e r W E B P I m a g e %
201% %
202% %
203% %
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205%
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.
212%
213% The format of the RegisterWEBPImage method is:
214%
215% size_t RegisterWEBPImage(void)
216%
217*/
218ModuleExport size_t RegisterWEBPImage(void)
219{
220 MagickInfo
221 *entry;
222
cristy25ba0c02011-03-14 00:54:32 +0000223 entry=SetMagickInfo("WEBP");
cristyb1860752011-03-14 00:27:46 +0000224#if defined(MAGICKCORE_WEBP_DELEGATE)
225 entry->decoder=(DecodeImageHandler *) ReadWEBPImage;
226 entry->encoder=(EncodeImageHandler *) WriteWEBPImage;
227#endif
cristyb1860752011-03-14 00:27:46 +0000228 entry->description=ConstantString("WebP Image Format");
cristy644040e2011-03-14 17:31:59 +0000229 entry->adjoin=MagickFalse;
cristyb1860752011-03-14 00:27:46 +0000230 entry->module=ConstantString("WEBP");
231 (void) RegisterMagickInfo(entry);
232 return(MagickImageCoderSignature);
233}
234
235/*
236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237% %
238% %
239% %
240% U n r e g i s t e r W E B P I m a g e %
241% %
242% %
243% %
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245%
246% UnregisterWEBPImage() removes format registrations made by the WebP module
247% from the list of supported formats.
248%
249% The format of the UnregisterWEBPImage method is:
250%
251% UnregisterWEBPImage(void)
252%
253*/
254ModuleExport void UnregisterWEBPImage(void)
255{
256 (void) UnregisterMagickInfo("WEBP");
257}
258#if defined(MAGICKCORE_WEBP_DELEGATE)
259
260/*
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262% %
263% %
264% %
265% W r i t e W E B P I m a g e %
266% %
267% %
268% %
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270%
271% WriteWEBPImage() writes an image in the WebP image format.
272%
273% The format of the WriteWEBPImage method is:
274%
275% MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
276% Image *image)
277%
278% A description of each parameter follows.
279%
280% o image_info: the image info.
281%
282% o image: The image.
283%
284*/
cristy644040e2011-03-14 17:31:59 +0000285
286static int WebPWriter(const unsigned char *stream,size_t length,
287 const WebPPicture *const picture)
288{
289 Image
290 *image;
291
292 image=(Image *) picture->custom_ptr;
293 return(length != 0 ? (int) WriteBlob(image,length,stream) : 1);
294}
295
cristyb1860752011-03-14 00:27:46 +0000296static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000297 Image *image,ExceptionInfo *exception)
cristyb1860752011-03-14 00:27:46 +0000298{
cristy644040e2011-03-14 17:31:59 +0000299 int
300 webp_status;
301
cristyffa663d2011-03-14 00:58:51 +0000302 MagickBooleanType
303 status;
304
cristy4c08aed2011-07-01 19:47:50 +0000305 register const Quantum
cristy644040e2011-03-14 17:31:59 +0000306 *restrict p;
307
308 register ssize_t
309 x;
310
311 register unsigned char
312 *restrict q;
313
314 ssize_t
315 y;
316
317 unsigned char
318 *pixels;
319
320 WebPConfig
321 configure;
322
323 WebPPicture
324 picture;
325
326 WebPAuxStats
327 statistics;
328
cristyffa663d2011-03-14 00:58:51 +0000329 /*
330 Open output image file.
331 */
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);
cristyc82a27b2011-10-21 01:07:16 +0000338 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristyffa663d2011-03-14 00:58:51 +0000339 if (status == MagickFalse)
340 return(status);
cristy644040e2011-03-14 17:31:59 +0000341 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");
352 /*
353 Future: set custom configuration parameters here.
354 */
355 if (WebPValidateConfig(&configure) == 0)
356 ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile");
357 /*
358 Allocate memory for pixels.
359 */
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");
364 /*
365 Convert image to WebP raster pixels.
366 */
367 q=pixels;
368 for (y=0; y < (ssize_t) image->rows; y++)
369 {
cristyc82a27b2011-10-21 01:07:16 +0000370 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000371 if (p == (const Quantum *) NULL)
cristy644040e2011-03-14 17:31:59 +0000372 break;
373 for (x=0; x < (ssize_t) image->columns; x++)
374 {
cristy4c08aed2011-07-01 19:47:50 +0000375 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
376 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
377 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy644040e2011-03-14 17:31:59 +0000378 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000379 *q++=ScaleQuantumToChar((Quantum) (image->matte != MagickFalse ?
380 GetPixelAlpha(image,p) : OpaqueAlpha));
cristyed231572011-07-14 02:18:59 +0000381 p+=GetPixelChannels(image);
cristy644040e2011-03-14 17:31:59 +0000382 }
383 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
384 image->rows);
385 if (status == MagickFalse)
386 break;
387 }
388 if (image->matte == MagickFalse)
389 webp_status=WebPPictureImportRGB(&picture,pixels,3*picture.width);
390 else
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);
cristyb1860752011-03-14 00:27:46 +0000396}
397#endif