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 % |
Cristy | cb63560 | 2017-08-02 06:59:15 -0400 | [diff] [blame] | 16 | % Cristy % |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 17 | % March 2011 % |
| 18 | % % |
| 19 | % % |
Cristy | d842011 | 2021-01-01 14:52:00 -0500 | [diff] [blame] | 20 | % Copyright 1999-2021 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 | % % |
Cristy | 83d74de | 2018-10-13 10:17:25 -0400 | [diff] [blame] | 26 | % https://imagemagick.org/script/license.php % |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 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" |
cristy | 76ce6e1 | 2013-04-05 14:33:38 +0000 | [diff] [blame] | 43 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/client.h" |
cristy | e65bb19 | 2013-08-01 11:22:06 +0000 | [diff] [blame] | 47 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #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" |
Cristy | 75f4730 | 2020-08-02 10:08:22 -0400 | [diff] [blame] | 53 | #include "MagickCore/layer.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 54 | #include "MagickCore/list.h" |
| 55 | #include "MagickCore/magick.h" |
| 56 | #include "MagickCore/monitor.h" |
| 57 | #include "MagickCore/monitor-private.h" |
| 58 | #include "MagickCore/memory_.h" |
| 59 | #include "MagickCore/option.h" |
| 60 | #include "MagickCore/pixel-accessor.h" |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 61 | #include "MagickCore/profile.h" |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 62 | #include "MagickCore/property.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 63 | #include "MagickCore/quantum-private.h" |
| 64 | #include "MagickCore/static.h" |
| 65 | #include "MagickCore/string_.h" |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 66 | #include "MagickCore/string-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 67 | #include "MagickCore/module.h" |
| 68 | #include "MagickCore/utility.h" |
| 69 | #include "MagickCore/xwindow.h" |
| 70 | #include "MagickCore/xwindow-private.h" |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 71 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 72 | #include <webp/decode.h> |
| 73 | #include <webp/encode.h> |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 74 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
| 75 | #include <webp/mux.h> |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 76 | #include <webp/demux.h> |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 77 | #endif |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 78 | #endif |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | Forward declarations. |
| 82 | */ |
| 83 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 84 | static MagickBooleanType |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 85 | WriteWEBPImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
cristy | dffa79e | 2013-02-20 00:57:26 +0000 | [diff] [blame] | 88 | /* |
| 89 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | % I s W E B P % |
| 94 | % % |
| 95 | % % |
| 96 | % % |
| 97 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 98 | % |
| 99 | % IsWEBP() returns MagickTrue if the image format type, identified by the |
| 100 | % magick string, is WebP. |
| 101 | % |
| 102 | % The format of the IsWEBP method is: |
| 103 | % |
| 104 | % MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length) |
| 105 | % |
| 106 | % A description of each parameter follows: |
| 107 | % |
| 108 | % o magick: compare image format pattern against these bytes. |
| 109 | % |
| 110 | % o length: Specifies the length of the magick string. |
| 111 | % |
| 112 | */ |
| 113 | static MagickBooleanType IsWEBP(const unsigned char *magick,const size_t length) |
| 114 | { |
| 115 | if (length < 12) |
| 116 | return(MagickFalse); |
| 117 | if (LocaleNCompare((const char *) magick+8,"WEBP",4) == 0) |
| 118 | return(MagickTrue); |
| 119 | return(MagickFalse); |
| 120 | } |
| 121 | |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 122 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 123 | /* |
| 124 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 125 | % % |
| 126 | % % |
| 127 | % % |
| 128 | % R e a d W E B P I m a g e % |
| 129 | % % |
| 130 | % % |
| 131 | % % |
| 132 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 133 | % |
| 134 | % ReadWEBPImage() reads an image in the WebP image format. |
| 135 | % |
| 136 | % The format of the ReadWEBPImage method is: |
| 137 | % |
| 138 | % Image *ReadWEBPImage(const ImageInfo *image_info, |
| 139 | % ExceptionInfo *exception) |
| 140 | % |
| 141 | % A description of each parameter follows: |
| 142 | % |
| 143 | % o image_info: the image info. |
| 144 | % |
| 145 | % o exception: return any errors or warnings in this structure. |
| 146 | % |
| 147 | */ |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 148 | |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 149 | static inline uint32_t ReadWebPLSBWord( |
| 150 | const unsigned char *magick_restrict data) |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 151 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 152 | const unsigned char |
cristy | 0b7a033 | 2013-02-23 02:28:08 +0000 | [diff] [blame] | 153 | *p; |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 154 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 155 | uint32_t |
cristy | 0b7a033 | 2013-02-23 02:28:08 +0000 | [diff] [blame] | 156 | value; |
| 157 | |
| 158 | p=data; |
| 159 | value=(uint32_t) (*p++); |
| 160 | value|=((uint32_t) (*p++)) << 8; |
| 161 | value|=((uint32_t) (*p++)) << 16; |
| 162 | value|=((uint32_t) (*p++)) << 24; |
| 163 | return(value); |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static MagickBooleanType IsWEBPImageLossless(const unsigned char *stream, |
| 167 | const size_t length) |
| 168 | { |
cristy | 0b7a033 | 2013-02-23 02:28:08 +0000 | [diff] [blame] | 169 | #define VP8_CHUNK_INDEX 15 |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 170 | #define LOSSLESS_FLAG 'L' |
| 171 | #define EXTENDED_HEADER 'X' |
| 172 | #define VP8_CHUNK_HEADER "VP8" |
| 173 | #define VP8_CHUNK_HEADER_SIZE 3 |
cristy | 0b7a033 | 2013-02-23 02:28:08 +0000 | [diff] [blame] | 174 | #define RIFF_HEADER_SIZE 12 |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 175 | #define VP8X_CHUNK_SIZE 10 |
| 176 | #define TAG_SIZE 4 |
| 177 | #define CHUNK_SIZE_BYTES 4 |
| 178 | #define CHUNK_HEADER_SIZE 8 |
cristy | a230a61 | 2014-01-05 20:51:47 +0000 | [diff] [blame] | 179 | #define MAX_CHUNK_PAYLOAD (~0U-CHUNK_HEADER_SIZE-1) |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 180 | |
Cristy | 27e15b7 | 2019-11-17 16:30:19 -0500 | [diff] [blame] | 181 | size_t |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 182 | offset; |
| 183 | |
| 184 | /* |
| 185 | Read simple header. |
| 186 | */ |
Cristy | 361ed68 | 2018-03-27 20:39:27 -0400 | [diff] [blame] | 187 | if (length <= VP8_CHUNK_INDEX) |
| 188 | return(MagickFalse); |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 189 | if (stream[VP8_CHUNK_INDEX] != EXTENDED_HEADER) |
cristy | ef632a5 | 2013-12-03 11:06:33 +0000 | [diff] [blame] | 190 | return(stream[VP8_CHUNK_INDEX] == LOSSLESS_FLAG ? MagickTrue : MagickFalse); |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 191 | /* |
| 192 | Read extended header. |
| 193 | */ |
| 194 | offset=RIFF_HEADER_SIZE+TAG_SIZE+CHUNK_SIZE_BYTES+VP8X_CHUNK_SIZE; |
Alex Gaynor | e0ed80f | 2019-11-17 10:29:04 -0500 | [diff] [blame] | 195 | while (offset <= (length-TAG_SIZE-TAG_SIZE-4)) |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 196 | { |
| 197 | uint32_t |
| 198 | chunk_size, |
| 199 | chunk_size_pad; |
| 200 | |
cristy | ef97829 | 2013-02-23 02:32:06 +0000 | [diff] [blame] | 201 | chunk_size=ReadWebPLSBWord(stream+offset+TAG_SIZE); |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 202 | if (chunk_size > MAX_CHUNK_PAYLOAD) |
| 203 | break; |
| 204 | chunk_size_pad=(CHUNK_HEADER_SIZE+chunk_size+1) & ~1; |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 205 | if (memcmp(stream+offset,VP8_CHUNK_HEADER,VP8_CHUNK_HEADER_SIZE) == 0) |
cristy | ef97829 | 2013-02-23 02:32:06 +0000 | [diff] [blame] | 206 | return(*(stream+offset+VP8_CHUNK_HEADER_SIZE) == LOSSLESS_FLAG ? |
cristy | 8f25aa8 | 2013-02-23 02:14:37 +0000 | [diff] [blame] | 207 | MagickTrue : MagickFalse); |
| 208 | offset+=chunk_size_pad; |
| 209 | } |
| 210 | return(MagickFalse); |
| 211 | } |
| 212 | |
Cristy | 9dd0587 | 2019-10-12 10:31:46 -0400 | [diff] [blame] | 213 | static int FillBasicWEBPInfo(Image *image,const uint8_t *stream,size_t length, |
| 214 | WebPDecoderConfig *configure) |
| 215 | { |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 216 | WebPBitstreamFeatures |
| 217 | *magick_restrict features = &configure->input; |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 218 | |
cristy | ef632a5 | 2013-12-03 11:06:33 +0000 | [diff] [blame] | 219 | int |
| 220 | webp_status; |
| 221 | |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 222 | webp_status=WebPGetFeatures(stream,length,features); |
| 223 | |
| 224 | if (webp_status != VP8_STATUS_OK) |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 225 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 226 | |
| 227 | image->columns=(size_t) features->width; |
| 228 | image->rows=(size_t) features->height; |
| 229 | image->depth=8; |
| 230 | image->alpha_trait=features->has_alpha != 0 ? BlendPixelTrait : |
| 231 | UndefinedPixelTrait; |
| 232 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 233 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Cristy | 9dd0587 | 2019-10-12 10:31:46 -0400 | [diff] [blame] | 236 | static int ReadSingleWEBPImage(Image *image,const uint8_t *stream, |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 237 | size_t length,WebPDecoderConfig *configure,ExceptionInfo *exception, |
| 238 | MagickBooleanType is_first) |
Cristy | 9dd0587 | 2019-10-12 10:31:46 -0400 | [diff] [blame] | 239 | { |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 240 | int |
| 241 | webp_status; |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 242 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 243 | unsigned char |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 244 | *p; |
| 245 | |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 246 | size_t |
| 247 | canvas_width, |
| 248 | canvas_height, |
| 249 | image_width, |
| 250 | image_height; |
| 251 | |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 252 | ssize_t |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 253 | x_offset, |
| 254 | y_offset, |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 255 | y; |
| 256 | |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 257 | WebPDecBuffer |
Dirk Lemstra | 89d15bb | 2020-12-28 09:56:41 +0100 | [diff] [blame] | 258 | *magick_restrict webp_image; |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 259 | |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 260 | MagickBooleanType |
| 261 | status; |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 262 | |
Dirk Lemstra | 89d15bb | 2020-12-28 09:56:41 +0100 | [diff] [blame] | 263 | webp_image=&configure->output; |
jace.k (김준성) | c835fda | 2019-12-18 08:38:52 +0900 | [diff] [blame] | 264 | if (is_first) |
| 265 | { |
| 266 | canvas_width=image->columns; |
| 267 | canvas_height=image->rows; |
| 268 | x_offset=image->page.x; |
| 269 | y_offset=image->page.y; |
| 270 | image->page.x=0; |
| 271 | image->page.y=0; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | x_offset=0; |
| 276 | y_offset=0; |
| 277 | } |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 278 | webp_status=FillBasicWEBPInfo(image,stream,length,configure); |
| 279 | image_width=image->columns; |
| 280 | image_height=image->rows; |
| 281 | if (is_first) |
| 282 | { |
| 283 | image->columns=canvas_width; |
| 284 | image->rows=canvas_height; |
| 285 | } |
| 286 | |
| 287 | if (webp_status != VP8_STATUS_OK) |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 288 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 289 | |
| 290 | if (IsWEBPImageLossless(stream,length) != MagickFalse) |
| 291 | image->quality=100; |
| 292 | |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 293 | webp_status=WebPDecode(stream,length,configure); |
| 294 | if (webp_status != VP8_STATUS_OK) |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 295 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 296 | |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 297 | p=(unsigned char *) webp_image->u.RGBA.rgba; |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 298 | for (y=0; y < (ssize_t) image->rows; y++) |
| 299 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 300 | Quantum |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 301 | *q; |
| 302 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 303 | ssize_t |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 304 | x; |
| 305 | |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 306 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 307 | if (q == (Quantum *) NULL) |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 308 | break; |
| 309 | for (x=0; x < (ssize_t) image->columns; x++) |
| 310 | { |
Cristy | fcaccf4 | 2020-12-22 16:20:58 -0500 | [diff] [blame] | 311 | if ((x >= x_offset && x < (ssize_t) (x_offset+image_width)) && |
| 312 | (y >= y_offset && y < (ssize_t) (y_offset+image_height))) |
jace.k (김준성) | c835fda | 2019-12-18 08:38:52 +0900 | [diff] [blame] | 313 | { |
| 314 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| 315 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 316 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 317 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | SetPixelRed(image,0,q); |
| 322 | SetPixelGreen(image,0,q); |
| 323 | SetPixelBlue(image,0,q); |
| 324 | SetPixelAlpha(image,0,q); |
| 325 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 326 | q+=GetPixelChannels(image); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 327 | } |
| 328 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 329 | break; |
| 330 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 331 | image->rows); |
| 332 | if (status == MagickFalse) |
| 333 | break; |
| 334 | } |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 335 | WebPFreeDecBuffer(webp_image); |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 336 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
| 337 | { |
| 338 | StringInfo |
| 339 | *profile; |
| 340 | |
| 341 | uint32_t |
| 342 | webp_flags = 0; |
| 343 | |
| 344 | WebPData |
Cristy | 5e87ab2 | 2018-06-24 16:26:06 -0400 | [diff] [blame] | 345 | chunk, |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 346 | content; |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 347 | |
| 348 | WebPMux |
| 349 | *mux; |
| 350 | |
| 351 | /* |
Cristy | 5fa0d44 | 2020-09-22 14:50:00 -0400 | [diff] [blame] | 352 | Extract any profiles: |
| 353 | https://developers.google.com/speed/webp/docs/container-api. |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 354 | */ |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 355 | content.bytes=stream; |
| 356 | content.size=length; |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 357 | mux=WebPMuxCreate(&content,0); |
Cristy | 5e87ab2 | 2018-06-24 16:26:06 -0400 | [diff] [blame] | 358 | (void) memset(&chunk,0,sizeof(chunk)); |
Cristy | 692b678 | 2017-11-19 12:35:18 -0500 | [diff] [blame] | 359 | WebPMuxGetFeatures(mux,&webp_flags); |
| 360 | if (webp_flags & ICCP_FLAG) |
| 361 | { |
| 362 | WebPMuxGetChunk(mux,"ICCP",&chunk); |
| 363 | profile=BlobToStringInfo(chunk.bytes,chunk.size); |
| 364 | if (profile != (StringInfo *) NULL) |
| 365 | { |
| 366 | SetImageProfile(image,"ICC",profile,exception); |
| 367 | profile=DestroyStringInfo(profile); |
| 368 | } |
| 369 | } |
| 370 | if (webp_flags & EXIF_FLAG) |
| 371 | { |
| 372 | WebPMuxGetChunk(mux,"EXIF",&chunk); |
| 373 | profile=BlobToStringInfo(chunk.bytes,chunk.size); |
| 374 | if (profile != (StringInfo *) NULL) |
| 375 | { |
| 376 | SetImageProfile(image,"EXIF",profile,exception); |
| 377 | profile=DestroyStringInfo(profile); |
| 378 | } |
| 379 | } |
| 380 | if (webp_flags & XMP_FLAG) |
| 381 | { |
| 382 | WebPMuxGetChunk(mux,"XMP",&chunk); |
| 383 | profile=BlobToStringInfo(chunk.bytes,chunk.size); |
| 384 | if (profile != (StringInfo *) NULL) |
| 385 | { |
| 386 | SetImageProfile(image,"XMP",profile,exception); |
| 387 | profile=DestroyStringInfo(profile); |
| 388 | } |
| 389 | } |
| 390 | WebPMuxDelete(mux); |
| 391 | } |
| 392 | #endif |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 393 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
Cristy | 7caee07 | 2019-11-17 10:54:58 -0500 | [diff] [blame] | 397 | static int ReadAnimatedWEBPImage(const ImageInfo *image_info,Image *image, |
| 398 | uint8_t *stream,size_t length,WebPDecoderConfig *configure, |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 399 | ExceptionInfo *exception) |
| 400 | { |
| 401 | Image |
| 402 | *original_image; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 403 | |
| 404 | int |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 405 | image_count, |
| 406 | webp_status; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 407 | |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 408 | size_t |
| 409 | canvas_width, |
| 410 | canvas_height; |
| 411 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 412 | WebPData |
| 413 | data; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 414 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 415 | WebPDemuxer |
| 416 | *demux; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 417 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 418 | WebPIterator |
| 419 | iter; |
| 420 | |
| 421 | image_count=0; |
| 422 | webp_status=0; |
| 423 | original_image=image; |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 424 | webp_status=FillBasicWEBPInfo(image,stream,length,configure); |
| 425 | canvas_width=image->columns; |
| 426 | canvas_height=image->rows; |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 427 | data.bytes=stream; |
| 428 | data.size=length; |
Cristy | 0e421ed | 2020-12-01 00:58:17 +0000 | [diff] [blame] | 429 | { |
| 430 | WebPMux |
| 431 | *mux; |
| 432 | |
| 433 | WebPMuxAnimParams |
| 434 | params; |
| 435 | |
| 436 | WebPMuxError |
| 437 | status; |
| 438 | |
| 439 | mux=WebPMuxCreate(&data,0); |
| 440 | status=WebPMuxGetAnimationParams(mux,¶ms); |
| 441 | if (status >= 0) |
| 442 | image->iterations=params.loop_count; |
| 443 | WebPMuxDelete(mux); |
| 444 | } |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 445 | demux=WebPDemux(&data); |
Cristy | fab704b | 2020-08-03 18:46:38 -0400 | [diff] [blame] | 446 | if (WebPDemuxGetFrame(demux,1,&iter)) |
| 447 | { |
| 448 | do |
| 449 | { |
| 450 | if (image_count != 0) |
| 451 | { |
| 452 | AcquireNextImage(image_info,image,exception); |
| 453 | if (GetNextImageInList(image) == (Image *) NULL) |
| 454 | break; |
| 455 | image=SyncNextImageInList(image); |
| 456 | CloneImageProperties(image,original_image); |
| 457 | image->page.x=iter.x_offset; |
| 458 | image->page.y=iter.y_offset; |
| 459 | webp_status=ReadSingleWEBPImage(image,iter.fragment.bytes, |
| 460 | iter.fragment.size,configure,exception,MagickFalse); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | image->page.x=iter.x_offset; |
| 465 | image->page.y=iter.y_offset; |
| 466 | webp_status=ReadSingleWEBPImage(image,iter.fragment.bytes, |
| 467 | iter.fragment.size,configure,exception,MagickTrue); |
| 468 | } |
| 469 | if (webp_status != VP8_STATUS_OK) |
| 470 | break; |
| 471 | image->page.width=canvas_width; |
| 472 | image->page.height=canvas_height; |
| 473 | image->ticks_per_second=100; |
| 474 | image->delay=iter.duration/10; |
| 475 | image->dispose=NoneDispose; |
| 476 | if (iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) |
| 477 | image->dispose=BackgroundDispose; |
| 478 | (void) SetImageProperty(image,"webp:mux-blend", |
| 479 | "AtopPreviousAlphaBlend",exception); |
| 480 | if (iter.blend_method == WEBP_MUX_BLEND) |
| 481 | (void) SetImageProperty(image,"webp:mux-blend", |
| 482 | "AtopBackgroundAlphaBlend",exception); |
| 483 | image_count++; |
| 484 | } while (WebPDemuxNextFrame(&iter)); |
| 485 | WebPDemuxReleaseIterator(&iter); |
| 486 | } |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 487 | WebPDemuxDelete(demux); |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 488 | return(webp_status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 489 | } |
| 490 | #endif |
| 491 | |
| 492 | static Image *ReadWEBPImage(const ImageInfo *image_info, |
| 493 | ExceptionInfo *exception) |
| 494 | { |
| 495 | #define ThrowWEBPException(severity,tag) \ |
| 496 | { \ |
| 497 | if (stream != (unsigned char *) NULL) \ |
| 498 | stream=(unsigned char*) RelinquishMagickMemory(stream); \ |
| 499 | if (webp_image != (WebPDecBuffer *) NULL) \ |
| 500 | WebPFreeDecBuffer(webp_image); \ |
| 501 | ThrowReaderException(severity,tag); \ |
| 502 | } |
| 503 | |
| 504 | Image |
| 505 | *image; |
| 506 | |
| 507 | int |
| 508 | webp_status; |
| 509 | |
| 510 | MagickBooleanType |
| 511 | status; |
| 512 | |
| 513 | size_t |
| 514 | length; |
| 515 | |
| 516 | ssize_t |
| 517 | count; |
| 518 | |
| 519 | unsigned char |
| 520 | header[12], |
| 521 | *stream; |
| 522 | |
| 523 | WebPDecoderConfig |
| 524 | configure; |
| 525 | |
| 526 | WebPDecBuffer |
| 527 | *magick_restrict webp_image = &configure.output; |
| 528 | |
| 529 | /* |
| 530 | Open image file. |
| 531 | */ |
| 532 | assert(image_info != (const ImageInfo *) NULL); |
| 533 | assert(image_info->signature == MagickCoreSignature); |
| 534 | if (image_info->debug != MagickFalse) |
| 535 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 536 | image_info->filename); |
| 537 | assert(exception != (ExceptionInfo *) NULL); |
| 538 | assert(exception->signature == MagickCoreSignature); |
| 539 | image=AcquireImage(image_info,exception); |
| 540 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 541 | if (status == MagickFalse) |
| 542 | { |
| 543 | image=DestroyImageList(image); |
| 544 | return((Image *) NULL); |
| 545 | } |
| 546 | stream=(unsigned char *) NULL; |
| 547 | if (WebPInitDecoderConfig(&configure) == 0) |
| 548 | ThrowReaderException(ResourceLimitError,"UnableToDecodeImageFile"); |
| 549 | webp_image->colorspace=MODE_RGBA; |
| 550 | count=ReadBlob(image,12,header); |
| 551 | if (count != 12) |
| 552 | ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile"); |
| 553 | status=IsWEBP(header,count); |
| 554 | if (status == MagickFalse) |
| 555 | ThrowWEBPException(CorruptImageError,"CorruptImage"); |
| 556 | length=(size_t) (ReadWebPLSBWord(header+4)+8); |
| 557 | if (length < 12) |
| 558 | ThrowWEBPException(CorruptImageError,"CorruptImage"); |
| 559 | if (length > GetBlobSize(image)) |
| 560 | ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile"); |
| 561 | stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream)); |
| 562 | if (stream == (unsigned char *) NULL) |
| 563 | ThrowWEBPException(ResourceLimitError,"MemoryAllocationFailed"); |
| 564 | (void) memcpy(stream,header,12); |
| 565 | count=ReadBlob(image,length-12,stream+12); |
| 566 | if (count != (ssize_t) (length-12)) |
| 567 | ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile"); |
| 568 | |
| 569 | webp_status=FillBasicWEBPInfo(image,stream,length,&configure); |
| 570 | if (webp_status == VP8_STATUS_OK) { |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 571 | if (configure.input.has_animation) { |
| 572 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
Cristy | 7caee07 | 2019-11-17 10:54:58 -0500 | [diff] [blame] | 573 | webp_status=ReadAnimatedWEBPImage(image_info,image,stream,length, |
| 574 | &configure,exception); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 575 | #else |
Alex Gaynor | d58edd3 | 2019-09-30 07:41:01 -0400 | [diff] [blame] | 576 | webp_status=VP8_STATUS_UNSUPPORTED_FEATURE; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 577 | #endif |
| 578 | } else { |
jace.k (김준성) | 7b743df | 2019-12-17 14:57:46 +0900 | [diff] [blame] | 579 | webp_status=ReadSingleWEBPImage(image,stream,length,&configure,exception,MagickFalse); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | if (webp_status != VP8_STATUS_OK) |
| 584 | switch (webp_status) |
| 585 | { |
| 586 | case VP8_STATUS_OUT_OF_MEMORY: |
| 587 | { |
| 588 | ThrowWEBPException(ResourceLimitError,"MemoryAllocationFailed"); |
| 589 | break; |
| 590 | } |
| 591 | case VP8_STATUS_INVALID_PARAM: |
| 592 | { |
| 593 | ThrowWEBPException(CorruptImageError,"invalid parameter"); |
| 594 | break; |
| 595 | } |
| 596 | case VP8_STATUS_BITSTREAM_ERROR: |
| 597 | { |
| 598 | ThrowWEBPException(CorruptImageError,"CorruptImage"); |
| 599 | break; |
| 600 | } |
| 601 | case VP8_STATUS_UNSUPPORTED_FEATURE: |
| 602 | { |
| 603 | ThrowWEBPException(CoderError,"DataEncodingSchemeIsNotSupported"); |
| 604 | break; |
| 605 | } |
| 606 | case VP8_STATUS_SUSPENDED: |
| 607 | { |
| 608 | ThrowWEBPException(CorruptImageError,"decoder suspended"); |
| 609 | break; |
| 610 | } |
| 611 | case VP8_STATUS_USER_ABORT: |
| 612 | { |
| 613 | ThrowWEBPException(CorruptImageError,"user abort"); |
| 614 | break; |
| 615 | } |
| 616 | case VP8_STATUS_NOT_ENOUGH_DATA: |
| 617 | { |
| 618 | ThrowWEBPException(CorruptImageError,"InsufficientImageDataInFile"); |
| 619 | break; |
| 620 | } |
| 621 | default: |
| 622 | ThrowWEBPException(CorruptImageError,"CorruptImage"); |
| 623 | } |
| 624 | |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 625 | stream=(unsigned char*) RelinquishMagickMemory(stream); |
zhangji | fe757a2 | 2017-02-17 18:34:45 +0800 | [diff] [blame] | 626 | (void) CloseBlob(image); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 627 | return(image); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 628 | } |
| 629 | #endif |
| 630 | |
| 631 | /* |
| 632 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 633 | % % |
| 634 | % % |
| 635 | % % |
| 636 | % R e g i s t e r W E B P I m a g e % |
| 637 | % % |
| 638 | % % |
| 639 | % % |
| 640 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 641 | % |
| 642 | % RegisterWEBPImage() adds attributes for the WebP image format to |
| 643 | % the list of supported formats. The attributes include the image format |
| 644 | % tag, a method to read and/or write the format, whether the format |
| 645 | % supports the saving of more than one frame to the same file or blob, |
| 646 | % whether the format supports native in-memory I/O, and a brief |
| 647 | % description of the format. |
| 648 | % |
| 649 | % The format of the RegisterWEBPImage method is: |
| 650 | % |
| 651 | % size_t RegisterWEBPImage(void) |
| 652 | % |
| 653 | */ |
| 654 | ModuleExport size_t RegisterWEBPImage(void) |
| 655 | { |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 656 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 657 | version[MagickPathExtent]; |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 658 | |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 659 | MagickInfo |
| 660 | *entry; |
| 661 | |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 662 | *version='\0'; |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 663 | entry=AcquireMagickInfo("WEBP","WEBP","WebP Image Format"); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 664 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 665 | entry->decoder=(DecodeImageHandler *) ReadWEBPImage; |
| 666 | entry->encoder=(EncodeImageHandler *) WriteWEBPImage; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 667 | (void) FormatLocaleString(version,MagickPathExtent,"libwebp %d.%d.%d [%04X]", |
よや | 5ce163f | 2019-02-10 22:42:15 +0900 | [diff] [blame] | 668 | (WebPGetEncoderVersion() >> 16) & 0xff, |
| 669 | (WebPGetEncoderVersion() >> 8) & 0xff, |
| 670 | (WebPGetEncoderVersion() >> 0) & 0xff,WEBP_ENCODER_ABI_VERSION); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 671 | #endif |
dirk | 8648daf | 2015-09-14 22:26:43 +0200 | [diff] [blame] | 672 | entry->mime_type=ConstantString("image/webp"); |
Cristy | cb63560 | 2017-08-02 06:59:15 -0400 | [diff] [blame] | 673 | entry->flags|=CoderDecoderSeekableStreamFlag; |
Dirk Lemstra | 47f757a | 2020-01-10 14:58:40 +0100 | [diff] [blame] | 674 | #if !defined(MAGICKCORE_WEBPMUX_DELEGATE) |
| 675 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 2424fb2 | 2020-01-10 14:55:54 +0100 | [diff] [blame] | 676 | #endif |
cristy | dffa79e | 2013-02-20 00:57:26 +0000 | [diff] [blame] | 677 | entry->magick=(IsImageFormatHandler *) IsWEBP; |
cristy | af29b62 | 2013-02-18 15:06:48 +0000 | [diff] [blame] | 678 | if (*version != '\0') |
| 679 | entry->version=ConstantString(version); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 680 | (void) RegisterMagickInfo(entry); |
| 681 | return(MagickImageCoderSignature); |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 686 | % % |
| 687 | % % |
| 688 | % % |
| 689 | % U n r e g i s t e r W E B P I m a g e % |
| 690 | % % |
| 691 | % % |
| 692 | % % |
| 693 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 694 | % |
| 695 | % UnregisterWEBPImage() removes format registrations made by the WebP module |
| 696 | % from the list of supported formats. |
| 697 | % |
| 698 | % The format of the UnregisterWEBPImage method is: |
| 699 | % |
| 700 | % UnregisterWEBPImage(void) |
| 701 | % |
| 702 | */ |
| 703 | ModuleExport void UnregisterWEBPImage(void) |
| 704 | { |
| 705 | (void) UnregisterMagickInfo("WEBP"); |
| 706 | } |
| 707 | #if defined(MAGICKCORE_WEBP_DELEGATE) |
| 708 | |
| 709 | /* |
| 710 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 711 | % % |
| 712 | % % |
| 713 | % % |
| 714 | % W r i t e W E B P I m a g e % |
| 715 | % % |
| 716 | % % |
| 717 | % % |
| 718 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 719 | % |
| 720 | % WriteWEBPImage() writes an image in the WebP image format. |
| 721 | % |
| 722 | % The format of the WriteWEBPImage method is: |
| 723 | % |
| 724 | % MagickBooleanType WriteWEBPImage(const ImageInfo *image_info, |
| 725 | % Image *image) |
| 726 | % |
| 727 | % A description of each parameter follows. |
| 728 | % |
| 729 | % o image_info: the image info. |
| 730 | % |
| 731 | % o image: The image. |
| 732 | % |
| 733 | */ |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 734 | |
よや | 5ce163f | 2019-02-10 22:42:15 +0900 | [diff] [blame] | 735 | #if WEBP_ENCODER_ABI_VERSION >= 0x0100 |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 736 | static int WebPEncodeProgress(int percent,const WebPPicture* picture) |
| 737 | { |
cristy | 88bf7c1 | 2014-04-06 15:21:42 +0000 | [diff] [blame] | 738 | #define EncodeImageTag "Encode/Image" |
| 739 | |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 740 | Image |
| 741 | *image; |
| 742 | |
| 743 | MagickBooleanType |
| 744 | status; |
| 745 | |
Cristy | cf4ad30 | 2017-12-23 08:32:30 -0500 | [diff] [blame] | 746 | image=(Image *) picture->user_data; |
cristy | 88bf7c1 | 2014-04-06 15:21:42 +0000 | [diff] [blame] | 747 | status=SetImageProgress(image,EncodeImageTag,percent-1,100); |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 748 | return(status == MagickFalse ? 0 : 1); |
| 749 | } |
| 750 | #endif |
| 751 | |
Cristy | 5642ab1 | 2017-11-19 14:37:48 -0500 | [diff] [blame] | 752 | #if !defined(MAGICKCORE_WEBPMUX_DELEGATE) |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 753 | static int WebPEncodeWriter(const unsigned char *stream,size_t length, |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 754 | const WebPPicture *const picture) |
| 755 | { |
| 756 | Image |
| 757 | *image; |
| 758 | |
| 759 | image=(Image *) picture->custom_ptr; |
Cristy | 6ba049c | 2018-01-20 15:08:44 -0500 | [diff] [blame] | 760 | return(length != 0 ? (WriteBlob(image,length,stream) == (ssize_t) length) : 1); |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 761 | } |
Cristy | 5642ab1 | 2017-11-19 14:37:48 -0500 | [diff] [blame] | 762 | #endif |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 763 | |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 764 | static const char * WebPErrorCodeMessage(WebPEncodingError error_code) |
| 765 | { |
| 766 | switch (error_code) |
| 767 | { |
| 768 | case VP8_ENC_OK: |
| 769 | return NULL; |
| 770 | case VP8_ENC_ERROR_OUT_OF_MEMORY: |
| 771 | return "out of memory"; |
| 772 | case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY: |
| 773 | return "bitstream out of memory"; |
| 774 | case VP8_ENC_ERROR_NULL_PARAMETER: |
| 775 | return "NULL parameter"; |
| 776 | case VP8_ENC_ERROR_INVALID_CONFIGURATION: |
| 777 | return "invalid configuration"; |
| 778 | case VP8_ENC_ERROR_BAD_DIMENSION: |
| 779 | return "bad dimension"; |
| 780 | case VP8_ENC_ERROR_PARTITION0_OVERFLOW: |
| 781 | return "partition 0 overflow (> 512K)"; |
| 782 | case VP8_ENC_ERROR_PARTITION_OVERFLOW: |
| 783 | return "partition overflow (> 16M)"; |
| 784 | case VP8_ENC_ERROR_BAD_WRITE: |
| 785 | return "bad write"; |
| 786 | case VP8_ENC_ERROR_FILE_TOO_BIG: |
| 787 | return "file too big (> 4GB)"; |
| 788 | #if WEBP_ENCODER_ABI_VERSION >= 0x0100 |
| 789 | case VP8_ENC_ERROR_USER_ABORT: |
| 790 | return "user abort"; |
| 791 | case VP8_ENC_ERROR_LAST: |
| 792 | return "error last"; |
| 793 | #endif |
| 794 | } |
| 795 | return "unknown exception"; |
| 796 | } |
| 797 | |
| 798 | static MagickBooleanType WriteSingleWEBPPicture(const ImageInfo *image_info, |
| 799 | Image *image,WebPConfig *configure,WebPPicture *picture,ExceptionInfo *exception) |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 800 | { |
| 801 | MagickBooleanType |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 802 | status; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 803 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 804 | uint32_t |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 805 | *magick_restrict q; |
| 806 | |
| 807 | ssize_t |
| 808 | y; |
| 809 | |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 810 | MemoryInfo |
| 811 | *pixel_info; |
| 812 | |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 813 | #if WEBP_ENCODER_ABI_VERSION >= 0x0100 |
| 814 | picture->progress_hook=WebPEncodeProgress; |
| 815 | picture->user_data=(void *) image; |
| 816 | #endif |
| 817 | picture->width=(int) image->columns; |
| 818 | picture->height=(int) image->rows; |
| 819 | picture->argb_stride=(int) image->columns; |
| 820 | picture->use_argb=1; |
| 821 | |
| 822 | /* |
| 823 | Allocate memory for pixels. |
| 824 | */ |
| 825 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 826 | pixel_info=AcquireVirtualMemory(image->columns,image->rows* |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 827 | sizeof(*(picture->argb))); |
| 828 | |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 829 | if (pixel_info == (MemoryInfo *) NULL) |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 830 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 831 | picture->argb=(uint32_t *) GetVirtualMemoryBlob(pixel_info); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 832 | /* |
| 833 | Convert image to WebP raster pixels. |
| 834 | */ |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 835 | status=MagickFalse; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 836 | q=picture->argb; |
| 837 | for (y=0; y < (ssize_t) image->rows; y++) |
| 838 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 839 | const Quantum |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 840 | *magick_restrict p; |
| 841 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 842 | ssize_t |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 843 | x; |
| 844 | |
| 845 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 846 | if (p == (const Quantum *) NULL) |
| 847 | break; |
| 848 | for (x=0; x < (ssize_t) image->columns; x++) |
| 849 | { |
| 850 | *q++=(uint32_t) (image->alpha_trait != UndefinedPixelTrait ? (uint32_t) |
| 851 | ScaleQuantumToChar(GetPixelAlpha(image,p)) << 24 : 0xff000000) | |
| 852 | ((uint32_t) ScaleQuantumToChar(GetPixelRed(image,p)) << 16) | |
| 853 | ((uint32_t) ScaleQuantumToChar(GetPixelGreen(image,p)) << 8) | |
| 854 | ((uint32_t) ScaleQuantumToChar(GetPixelBlue(image,p))); |
| 855 | p+=GetPixelChannels(image); |
| 856 | } |
| 857 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 858 | image->rows); |
| 859 | if (status == MagickFalse) |
| 860 | break; |
| 861 | } |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 862 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 863 | if (status != MagickFalse) |
| 864 | status=(MagickBooleanType) WebPEncode(configure, picture); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 865 | |
| 866 | RelinquishVirtualMemory(pixel_info); |
| 867 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 868 | if (status == MagickFalse) |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 869 | (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError, |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 870 | WebPErrorCodeMessage(picture->error_code),"`%s'", |
| 871 | image->filename); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 872 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 873 | return(status); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static MagickBooleanType WriteSingleWEBPImage(const ImageInfo *image_info, |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 877 | Image *image,WebPConfig *configure,WebPMemoryWriter *writer, |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 878 | ExceptionInfo *exception) |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 879 | { |
| 880 | MagickBooleanType |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 881 | status; |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 882 | |
| 883 | WebPPicture |
| 884 | picture; |
| 885 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 886 | if (WebPPictureInit(&picture) == 0) |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 887 | ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile"); |
| 888 | |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 889 | picture.writer=WebPMemoryWrite; |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 890 | picture.custom_ptr=writer; |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 891 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 892 | status=WriteSingleWEBPPicture(image_info,image,configure,&picture,exception); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 893 | WebPPictureFree(&picture); |
| 894 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 895 | return(status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
| 899 | static MagickBooleanType WriteAnimatedWEBPImage(const ImageInfo *image_info, |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 900 | Image *image,WebPConfig *configure,WebPData *webp_data, |
| 901 | ExceptionInfo *exception) |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 902 | { |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 903 | Image |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 904 | *first_image; |
| 905 | |
Cristy | 7caee07 | 2019-11-17 10:54:58 -0500 | [diff] [blame] | 906 | size_t |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 907 | effective_delta, |
| 908 | frame_timestamp; |
Cristy | 7caee07 | 2019-11-17 10:54:58 -0500 | [diff] [blame] | 909 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 910 | WebPAnimEncoder |
| 911 | *enc; |
| 912 | |
| 913 | WebPAnimEncoderOptions |
| 914 | enc_options; |
| 915 | |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 916 | WebPPicture |
| 917 | picture; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 918 | |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 919 | MagickBooleanType |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 920 | status; |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 921 | |
| 922 | first_image=CoalesceImages(image,exception); |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 923 | if (first_image == (Image *) NULL) |
| 924 | return(MagickFalse); |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 925 | image=first_image; |
| 926 | effective_delta=0; |
| 927 | frame_timestamp=0; |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 928 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 929 | (void) WebPAnimEncoderOptionsInit(&enc_options); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 930 | if (image_info->verbose) |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 931 | enc_options.verbose=1; |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 932 | enc=WebPAnimEncoderNew((int) image->page.width,(int) image->page.height, |
| 933 | &enc_options); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 934 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 935 | status=MagickTrue; |
Cristy | 6f84d89 | 2019-10-29 21:32:11 -0400 | [diff] [blame] | 936 | while (image != NULL) |
Dirk Lemstra | abb9179 | 2019-10-26 13:23:52 +0200 | [diff] [blame] | 937 | { |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 938 | status=(MagickBooleanType) WebPPictureInit(&picture); |
| 939 | if (status == MagickFalse) |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 940 | { |
| 941 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 942 | ResourceLimitError,"UnableToEncodeImageFile","`%s'",image->filename); |
| 943 | break; |
| 944 | } |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 945 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 946 | status=WriteSingleWEBPPicture(image_info,image,configure,&picture, |
| 947 | exception); |
| 948 | if (status == MagickFalse) |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 949 | break; |
| 950 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 951 | status=(MagickBooleanType) WebPAnimEncoderAdd(enc,&picture, |
| 952 | (int) frame_timestamp,configure); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 953 | WebPPictureFree(&picture); |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 954 | if (status == MagickFalse) |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 955 | { |
| 956 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 957 | CoderError,WebPAnimEncoderGetError(enc),"`%s'",image->filename); |
| 958 | break; |
| 959 | } |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 960 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 961 | effective_delta=image->delay*1000/image->ticks_per_second; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 962 | if (effective_delta < 10) |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 963 | effective_delta=100; /* Consistent with gif2webp */ |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 964 | frame_timestamp+=effective_delta; |
| 965 | |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 966 | image=GetNextImageInList(image); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 967 | } |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 968 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 969 | if (status != MagickFalse) |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 970 | { |
| 971 | // add last null frame and assemble picture. |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 972 | status=(MagickBooleanType) WebPAnimEncoderAdd(enc,(WebPPicture *) NULL, |
| 973 | (int) frame_timestamp,configure); |
| 974 | if (status != MagickFalse) |
| 975 | status=(MagickBooleanType) WebPAnimEncoderAssemble(enc,webp_data); |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 976 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 977 | if (status == MagickFalse) |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 978 | (void) ThrowMagickException(exception,GetMagickModule(), |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 979 | CoderError,WebPAnimEncoderGetError(enc),"`%s'", |
| 980 | first_image->filename); |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 981 | } |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 982 | |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 983 | WebPAnimEncoderDelete(enc); |
| 984 | DestroyImageList(first_image); |
tomwei7 | 2d2e014 | 2020-12-27 23:52:46 +0800 | [diff] [blame] | 985 | return(status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 986 | } |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 987 | |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 988 | static MagickBooleanType WriteWEBPImageProfile(Image *image, |
| 989 | WebPData *webp_data,ExceptionInfo *exception) |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 990 | { |
| 991 | const StringInfo |
| 992 | *icc_profile, |
| 993 | *exif_profile, |
| 994 | *xmp_profile; |
| 995 | |
| 996 | WebPData |
| 997 | chunk; |
| 998 | |
| 999 | WebPMux |
| 1000 | *mux; |
| 1001 | |
| 1002 | WebPMuxError |
| 1003 | mux_error=WEBP_MUX_OK; |
| 1004 | |
| 1005 | icc_profile=GetImageProfile(image,"ICC"); |
| 1006 | exif_profile=GetImageProfile(image,"EXIF"); |
| 1007 | xmp_profile=GetImageProfile(image,"XMP"); |
| 1008 | |
| 1009 | if (icc_profile == (StringInfo *) NULL |
| 1010 | && exif_profile == (StringInfo *) NULL |
| 1011 | && xmp_profile == (StringInfo *) NULL) |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 1012 | return(MagickTrue); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 1013 | |
| 1014 | mux=WebPMuxCreate(webp_data, 1); |
| 1015 | WebPDataClear(webp_data); |
| 1016 | |
| 1017 | if (mux == NULL) |
| 1018 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 1019 | ResourceLimitError,"UnableToEncodeImageFile","`%s'",image->filename); |
| 1020 | |
| 1021 | // Clean up returned data |
| 1022 | memset(webp_data, 0, sizeof(*webp_data)); |
| 1023 | if (icc_profile != (StringInfo *) NULL && (mux_error == WEBP_MUX_OK)) |
| 1024 | { |
| 1025 | chunk.bytes=GetStringInfoDatum(icc_profile); |
| 1026 | chunk.size=GetStringInfoLength(icc_profile); |
| 1027 | mux_error=WebPMuxSetChunk(mux,"ICCP",&chunk,0); |
| 1028 | } |
| 1029 | if (exif_profile != (StringInfo *) NULL && (mux_error == WEBP_MUX_OK)) |
| 1030 | { |
| 1031 | chunk.bytes=GetStringInfoDatum(exif_profile); |
| 1032 | chunk.size=GetStringInfoLength(exif_profile); |
| 1033 | mux_error=WebPMuxSetChunk(mux,"EXIF",&chunk,0); |
| 1034 | } |
| 1035 | if (xmp_profile != (StringInfo *) NULL && (mux_error == WEBP_MUX_OK)) |
| 1036 | { |
| 1037 | chunk.bytes=GetStringInfoDatum(xmp_profile); |
| 1038 | chunk.size=GetStringInfoLength(xmp_profile); |
| 1039 | mux_error=WebPMuxSetChunk(mux,"XMP",&chunk,0); |
| 1040 | } |
| 1041 | if (mux_error == WEBP_MUX_OK) |
| 1042 | mux_error=WebPMuxAssemble(mux,webp_data); |
| 1043 | WebPMuxDelete(mux); |
| 1044 | |
| 1045 | if (mux_error != WEBP_MUX_OK) |
| 1046 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 1047 | ResourceLimitError,"UnableToEncodeImageFile","`%s'",image->filename); |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 1048 | return(MagickTrue); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 1049 | } |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1050 | #endif |
| 1051 | |
Dirk Lemstra | 4e056d6 | 2020-12-30 11:09:32 +0100 | [diff] [blame] | 1052 | static inline void SetIntegerOption(const ImageInfo *image_info, |
| 1053 | const char *option,int *setting) |
| 1054 | { |
| 1055 | const char |
| 1056 | *value; |
| 1057 | |
| 1058 | value=GetImageOption(image_info,option); |
| 1059 | if (value != (const char *) NULL) |
| 1060 | *setting=StringToInteger(value); |
| 1061 | } |
| 1062 | |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 1063 | static MagickBooleanType WriteWEBPImage(const ImageInfo *image_info, |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1064 | Image *image,ExceptionInfo * exception) |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 1065 | { |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 1066 | const char |
| 1067 | *value; |
| 1068 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 1069 | MagickBooleanType |
| 1070 | status; |
| 1071 | |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 1072 | WebPConfig |
| 1073 | configure; |
| 1074 | |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 1075 | WebPMemoryWriter |
| 1076 | writer; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1077 | |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 1078 | /* |
| 1079 | Open output image file. |
| 1080 | */ |
| 1081 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1082 | assert(image_info->signature == MagickCoreSignature); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 1083 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1084 | assert(image->signature == MagickCoreSignature); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 1085 | if (image->debug != MagickFalse) |
| 1086 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 5779030 | 2013-02-18 18:19:08 +0000 | [diff] [blame] | 1087 | if ((image->columns > 16383UL) || (image->rows > 16383UL)) |
cristy | e4d08fd | 2012-04-26 17:29:08 +0000 | [diff] [blame] | 1088 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1089 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | ffa663d | 2011-03-14 00:58:51 +0000 | [diff] [blame] | 1090 | if (status == MagickFalse) |
| 1091 | return(status); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1092 | if (WebPConfigInit(&configure) == 0) |
| 1093 | ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile"); |
cristy | 35c63c7 | 2012-10-23 14:40:54 +0000 | [diff] [blame] | 1094 | if (image->quality != UndefinedCompressionQuality) |
| 1095 | configure.quality=(float) image->quality; |
cristy | 7ab1ba8 | 2014-11-13 21:37:21 +0000 | [diff] [blame] | 1096 | if (image->quality >= 100) |
| 1097 | configure.lossless=1; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1098 | value=GetImageOption(image_info,"webp:lossless"); |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 1099 | if (value != (char *) NULL) |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 1100 | configure.lossless=(int) ParseCommandOption(MagickBooleanOptions, |
| 1101 | MagickFalse,value); |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1102 | value=GetImageOption(image_info,"webp:image-hint"); |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 1103 | if (value != (char *) NULL) |
cristy | d6f2cc4 | 2013-02-18 17:24:19 +0000 | [diff] [blame] | 1104 | { |
cristy | 9bb4d7a | 2014-04-20 01:41:32 +0000 | [diff] [blame] | 1105 | if (LocaleCompare(value,"default") == 0) |
| 1106 | configure.image_hint=WEBP_HINT_DEFAULT; |
cristy | 547fe7f | 2013-02-18 17:27:13 +0000 | [diff] [blame] | 1107 | if (LocaleCompare(value,"photo") == 0) |
cristy | d6f2cc4 | 2013-02-18 17:24:19 +0000 | [diff] [blame] | 1108 | configure.image_hint=WEBP_HINT_PHOTO; |
cristy | 547fe7f | 2013-02-18 17:27:13 +0000 | [diff] [blame] | 1109 | if (LocaleCompare(value,"picture") == 0) |
cristy | d6f2cc4 | 2013-02-18 17:24:19 +0000 | [diff] [blame] | 1110 | configure.image_hint=WEBP_HINT_PICTURE; |
よや | 5ce163f | 2019-02-10 22:42:15 +0900 | [diff] [blame] | 1111 | #if WEBP_ENCODER_ABI_VERSION >= 0x0200 |
cristy | 9bb4d7a | 2014-04-20 01:41:32 +0000 | [diff] [blame] | 1112 | if (LocaleCompare(value,"graph") == 0) |
| 1113 | configure.image_hint=WEBP_HINT_GRAPH; |
| 1114 | #endif |
cristy | d6f2cc4 | 2013-02-18 17:24:19 +0000 | [diff] [blame] | 1115 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1116 | value=GetImageOption(image_info,"webp:auto-filter"); |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 1117 | if (value != (char *) NULL) |
cristy | b22ab41 | 2014-04-06 15:09:41 +0000 | [diff] [blame] | 1118 | configure.autofilter=(int) ParseCommandOption(MagickBooleanOptions, |
| 1119 | MagickFalse,value); |
Dirk Lemstra | 4e056d6 | 2020-12-30 11:09:32 +0100 | [diff] [blame] | 1120 | value=GetImageOption(image_info,"webp:target-psnr"); |
cristy | e71e089 | 2013-02-18 13:13:53 +0000 | [diff] [blame] | 1121 | if (value != (char *) NULL) |
Dirk Lemstra | 4e056d6 | 2020-12-30 11:09:32 +0100 | [diff] [blame] | 1122 | configure.target_PSNR=(float) StringToDouble(value,(char **) NULL); |
| 1123 | SetIntegerOption(image_info,"webp:alpha-compression", |
| 1124 | &configure.alpha_compression); |
| 1125 | SetIntegerOption(image_info,"webp:alpha-filtering", |
| 1126 | &configure.alpha_filtering); |
| 1127 | SetIntegerOption(image_info,"webp:alpha-quality", |
| 1128 | &configure.alpha_quality); |
| 1129 | SetIntegerOption(image_info,"webp:filter-strength", |
| 1130 | &configure.filter_strength); |
| 1131 | SetIntegerOption(image_info,"webp:filter-sharpness", |
| 1132 | &configure.filter_sharpness); |
| 1133 | SetIntegerOption(image_info,"webp:filter-type",&configure.filter_type); |
| 1134 | SetIntegerOption(image_info,"webp:method",&configure.method); |
| 1135 | SetIntegerOption(image_info,"webp:partitions",&configure.partitions); |
| 1136 | SetIntegerOption(image_info,"webp:partition-limit", |
| 1137 | &configure.partition_limit); |
| 1138 | SetIntegerOption(image_info,"webp:pass",&configure.pass); |
| 1139 | SetIntegerOption(image_info,"webp:preprocessing",&configure.preprocessing); |
| 1140 | SetIntegerOption(image_info,"webp:segments",&configure.segments); |
| 1141 | SetIntegerOption(image_info,"webp:show-compressed", |
| 1142 | &configure.show_compressed); |
| 1143 | SetIntegerOption(image_info,"webp:sns-strength",&configure.sns_strength); |
| 1144 | SetIntegerOption(image_info,"webp:target-size",&configure.target_size); |
よや | 5ce163f | 2019-02-10 22:42:15 +0900 | [diff] [blame] | 1145 | #if WEBP_ENCODER_ABI_VERSION >= 0x0201 |
cristy | 17e26ea | 2014-08-10 23:30:23 +0000 | [diff] [blame] | 1146 | value=GetImageOption(image_info,"webp:emulate-jpeg-size"); |
cristy | 9bb4d7a | 2014-04-20 01:41:32 +0000 | [diff] [blame] | 1147 | if (value != (char *) NULL) |
| 1148 | configure.emulate_jpeg_size=(int) ParseCommandOption(MagickBooleanOptions, |
| 1149 | MagickFalse,value); |
cristy | 7ab1ba8 | 2014-11-13 21:37:21 +0000 | [diff] [blame] | 1150 | value=GetImageOption(image_info,"webp:low-memory"); |
| 1151 | if (value != (char *) NULL) |
| 1152 | configure.low_memory=(int) ParseCommandOption(MagickBooleanOptions, |
| 1153 | MagickFalse,value); |
Dirk Lemstra | 4e056d6 | 2020-12-30 11:09:32 +0100 | [diff] [blame] | 1154 | SetIntegerOption(image_info,"webp:thread-level",&configure.thread_level); |
cristy | 9bb4d7a | 2014-04-20 01:41:32 +0000 | [diff] [blame] | 1155 | #endif |
よや | 5ce163f | 2019-02-10 22:42:15 +0900 | [diff] [blame] | 1156 | #if WEBP_ENCODER_ABI_VERSION >= 0x020e |
Dirk Lemstra | 0b770a9 | 2020-12-30 11:14:36 +0100 | [diff] [blame] | 1157 | SetIntegerOption(image_info,"webp:near-lossless",&configure.near_lossless); |
Dirk Lemstra | 4e056d6 | 2020-12-30 11:09:32 +0100 | [diff] [blame] | 1158 | SetIntegerOption(image_info,"webp:use-sharp-yuv",&configure.use_sharp_yuv); |
Cristy | aa5bcab | 2019-01-31 07:38:50 -0500 | [diff] [blame] | 1159 | #endif |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 1160 | if (WebPValidateConfig(&configure) == 0) |
| 1161 | ThrowWriterException(ResourceLimitError,"UnableToEncodeImageFile"); |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1162 | #if defined(MAGICKCORE_WEBPMUX_DELEGATE) |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 1163 | { |
| 1164 | WebPData |
| 1165 | webp_data; |
Cristy | 46d4fb5 | 2019-09-29 20:32:20 -0400 | [diff] [blame] | 1166 | |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 1167 | memset(&webp_data,0,sizeof(webp_data)); |
| 1168 | if ((image_info->adjoin != MagickFalse) && |
| 1169 | (GetPreviousImageInList(image) == (Image *) NULL) && |
| 1170 | (GetNextImageInList(image) != (Image *) NULL)) |
| 1171 | status=WriteAnimatedWEBPImage(image_info,image,&configure,&webp_data, |
| 1172 | exception); |
| 1173 | else |
| 1174 | { |
| 1175 | WebPMemoryWriterInit(&writer); |
| 1176 | status=WriteSingleWEBPImage(image_info,image,&configure,&writer, |
| 1177 | exception); |
| 1178 | if (status == MagickFalse) |
| 1179 | WebPMemoryWriterClear(&writer); |
| 1180 | else |
| 1181 | { |
| 1182 | webp_data.bytes=writer.mem; |
| 1183 | webp_data.size=writer.size; |
| 1184 | } |
| 1185 | } |
| 1186 | if (status != MagickFalse) |
| 1187 | status=WriteWEBPImageProfile(image,&webp_data,exception); |
| 1188 | if (status != MagickFalse) |
| 1189 | (void) WriteBlob(image,webp_data.size,webp_data.bytes); |
| 1190 | WebPDataClear(&webp_data); |
| 1191 | } |
| 1192 | #else |
| 1193 | WebPMemoryWriterInit(&writer); |
| 1194 | status=WriteSingleWEBPImage(image_info,image,&configure,&writer, |
| 1195 | exception); |
Dirk Lemstra | acc0682 | 2020-12-28 10:26:17 +0100 | [diff] [blame] | 1196 | if (status != MagickFalse) |
Dirk Lemstra | d219762 | 2020-12-28 15:57:43 +0100 | [diff] [blame] | 1197 | (void) WriteBlob(image,writer.size,writer.mem); |
| 1198 | WebPMemoryWriterClear(&writer); |
| 1199 | #endif |
cristy | 644040e | 2011-03-14 17:31:59 +0000 | [diff] [blame] | 1200 | (void) CloseBlob(image); |
tomwei7 | 17e7643 | 2020-11-26 23:34:47 +0800 | [diff] [blame] | 1201 | return(status); |
cristy | b186075 | 2011-03-14 00:27:46 +0000 | [diff] [blame] | 1202 | } |
| 1203 | #endif |