cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 65 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 66 | #include <webp/decode.h> |
| 67 | #include <webp/encode.h> |
| 68 | #endif |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | Forward declarations. |
| 72 | */ |
| 73 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 74 | static MagickBooleanType |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 75 | WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 76 | #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 | */ |
| 104 | static Image *ReadWEBPImage(const ImageInfo *image_info, |
| 105 | ExceptionInfo *exception) |
| 106 | { |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 107 | int |
| 108 | height, |
| 109 | width; |
| 110 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 111 | Image |
| 112 | *image; |
| 113 | |
| 114 | MagickBooleanType |
| 115 | status; |
| 116 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 117 | register Quantum |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 118 | *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 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 137 | /* |
| 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); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 147 | image=AcquireImage(image_info,exception); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 148 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 149 | if (status == MagickFalse) |
| 150 | { |
| 151 | image=DestroyImageList(image); |
| 152 | return((Image *) NULL); |
| 153 | } |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 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; |
cristy | 3e211bf | 2012-04-24 19:52:52 +0000 | [diff] [blame] | 166 | image->matte=MagickTrue; |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 167 | p=pixels; |
| 168 | for (y=0; y < (ssize_t) image->rows; y++) |
| 169 | { |
| 170 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 171 | if (q == (Quantum *) NULL) |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 172 | break; |
| 173 | for (x=0; x < (ssize_t) image->columns; x++) |
| 174 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 175 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| 176 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 177 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 178 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 179 | q+=GetPixelChannels(image); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 180 | } |
| 181 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 182 | break; |
| 183 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 184 | image->rows); |
| 185 | if (status == MagickFalse) |
| 186 | break; |
| 187 | } |
| 188 | free(pixels); |
| 189 | pixels=(unsigned char *) NULL; |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 190 | return(image); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 191 | } |
| 192 | #endif |
| 193 | |
| 194 | /* |
| 195 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 196 | % % |
| 197 | % % |
| 198 | % % |
| 199 | % R e g i s t e r W E B P I m a g e % |
| 200 | % % |
| 201 | % % |
| 202 | % % |
| 203 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 204 | % |
| 205 | % RegisterWEBPImage() adds attributes for the WebP image format to |
| 206 | % the list of supported formats. The attributes include the image format |
| 207 | % tag, a method to read and/or write the format, whether the format |
| 208 | % supports the saving of more than one frame to the same file or blob, |
| 209 | % whether the format supports native in-memory I/O, and a brief |
| 210 | % description of the format. |
| 211 | % |
| 212 | % The format of the RegisterWEBPImage method is: |
| 213 | % |
| 214 | % size_t RegisterWEBPImage(void) |
| 215 | % |
| 216 | */ |
| 217 | ModuleExport size_t RegisterWEBPImage(void) |
| 218 | { |
| 219 | MagickInfo |
| 220 | *entry; |
| 221 | |
cristy | 25ba0c0 | 2011-03-14 00:54:32 +0000 | [diff] [blame] | 222 | entry=SetMagickInfo("WEBP"); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 223 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 224 | entry->decoder=(DecodeImageHandler *) ReadWEBPImage; |
| 225 | entry->encoder=(EncodeImageHandler *) WriteWEBPImage; |
| 226 | #endif |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 227 | entry->description=ConstantString("WebP Image Format"); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 228 | entry->adjoin=MagickFalse; |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 229 | entry->module=ConstantString("WEBP"); |
| 230 | (void) RegisterMagickInfo(entry); |
| 231 | return(MagickImageCoderSignature); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 236 | % % |
| 237 | % % |
| 238 | % % |
| 239 | % U n r e g i s t e r W E B P I m a g e % |
| 240 | % % |
| 241 | % % |
| 242 | % % |
| 243 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 244 | % |
| 245 | % UnregisterWEBPImage() removes format registrations made by the WebP module |
| 246 | % from the list of supported formats. |
| 247 | % |
| 248 | % The format of the UnregisterWEBPImage method is: |
| 249 | % |
| 250 | % UnregisterWEBPImage(void) |
| 251 | % |
| 252 | */ |
| 253 | ModuleExport void UnregisterWEBPImage(void) |
| 254 | { |
| 255 | (void) UnregisterMagickInfo("WEBP"); |
| 256 | } |
| 257 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 258 | |
| 259 | /* |
| 260 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | % % |
| 262 | % % |
| 263 | % % |
| 264 | % W r i t e W E B P I m a g e % |
| 265 | % % |
| 266 | % % |
| 267 | % % |
| 268 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 269 | % |
| 270 | % WriteWEBPImage() writes an image in the WebP image format. |
| 271 | % |
| 272 | % The format of the WriteWEBPImage method is: |
| 273 | % |
| 274 | % MagickBooleanType WriteWEBPImage(const ImageInfo *image_info, |
| 275 | % Image *image) |
| 276 | % |
| 277 | % A description of each parameter follows. |
| 278 | % |
| 279 | % o image_info: the image info. |
| 280 | % |
| 281 | % o image: The image. |
| 282 | % |
| 283 | */ |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 284 | |
| 285 | static int WebPWriter(const unsigned char *stream,size_t length, |
| 286 | const WebPPicture *const picture) |
| 287 | { |
| 288 | Image |
| 289 | *image; |
| 290 | |
| 291 | image=(Image *) picture->custom_ptr; |
| 292 | return(length != 0 ? (int) WriteBlob(image,length,stream) : 1); |
| 293 | } |
| 294 | |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 295 | static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 296 | Image *image,ExceptionInfo *exception) |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 297 | { |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 298 | int |
| 299 | webp_status; |
| 300 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 301 | MagickBooleanType |
| 302 | status; |
| 303 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 304 | register const Quantum |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 305 | *restrict p; |
| 306 | |
| 307 | register ssize_t |
| 308 | x; |
| 309 | |
| 310 | register unsigned char |
| 311 | *restrict q; |
| 312 | |
| 313 | ssize_t |
| 314 | y; |
| 315 | |
| 316 | unsigned char |
| 317 | *pixels; |
| 318 | |
| 319 | WebPConfig |
| 320 | configure; |
| 321 | |
| 322 | WebPPicture |
| 323 | picture; |
| 324 | |
| 325 | WebPAuxStats |
| 326 | statistics; |
| 327 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 328 | /* |
| 329 | Open output image file. |
| 330 | */ |
| 331 | assert(image_info != (const ImageInfo *) NULL); |
| 332 | assert(image_info->signature == MagickSignature); |
| 333 | assert(image != (Image *) NULL); |
| 334 | assert(image->signature == MagickSignature); |
| 335 | if (image->debug != MagickFalse) |
| 336 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | e4d08fd | 2012-04-26 17:29:08 +0000 | [diff] [blame] | 337 | if ((image->columns > 16383) || (image->rows > 16383)) |
| 338 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 339 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 340 | if (status == MagickFalse) |
| 341 | return(status); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 342 | if (WebPPictureInit(&picture) == 0) |
| 343 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 344 | picture.writer=WebPWriter; |
| 345 | picture.custom_ptr=(void *) image; |
| 346 | picture.stats=(&statistics); |
| 347 | picture.width=(int) image->columns; |
| 348 | picture.height=(int) image->rows; |
| 349 | if (image->quality != UndefinedCompressionQuality) |
| 350 | configure.quality=(float) image->quality; |
| 351 | if (WebPConfigInit(&configure) == 0) |
| 352 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 353 | /* |
| 354 | Future: set custom configuration parameters here. |
| 355 | */ |
| 356 | if (WebPValidateConfig(&configure) == 0) |
| 357 | ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile"); |
| 358 | /* |
| 359 | Allocate memory for pixels. |
| 360 | */ |
cristy | 3e211bf | 2012-04-24 19:52:52 +0000 | [diff] [blame] | 361 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*image->rows* |
| 362 | sizeof(*pixels)); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 363 | if (pixels == (unsigned char *) NULL) |
| 364 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 365 | /* |
| 366 | Convert image to WebP raster pixels. |
| 367 | */ |
| 368 | q=pixels; |
| 369 | for (y=0; y < (ssize_t) image->rows; y++) |
| 370 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 371 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 372 | if (p == (const Quantum *) NULL) |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 373 | break; |
| 374 | for (x=0; x < (ssize_t) image->columns; x++) |
| 375 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 376 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 377 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 378 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | 6d7e3ba | 2012-04-26 01:24:40 +0000 | [diff] [blame] | 379 | if (image->matte != MagickFalse) |
| 380 | *q++=ScaleQuantumToChar(GetPixelAlpha(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 381 | p+=GetPixelChannels(image); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 382 | } |
| 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); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 396 | } |
| 397 | #endif |