cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % M M V V GGGG % |
| 7 | % MM MM V V G % |
| 8 | % M M M V V G GG % |
| 9 | % M M V V G G % |
| 10 | % M M V GGG % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Magick Vector Graphics Metafiles. % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % April 2000 % |
| 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 | 3ed852e | 2009-09-05 21:47:34 +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 | 3ed852e | 2009-09-05 21:47:34 +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" |
| 43 | #include "MagickCore/artifact.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/draw.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/memory_.h" |
| 54 | #include "MagickCore/module.h" |
| 55 | #include "MagickCore/property.h" |
| 56 | #include "MagickCore/quantum-private.h" |
| 57 | #include "MagickCore/static.h" |
| 58 | #include "MagickCore/string_.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | Forward declarations. |
| 62 | */ |
| 63 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 64 | WriteMVGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 68 | % % |
| 69 | % % |
| 70 | % % |
| 71 | % I s M V G % |
| 72 | % % |
| 73 | % % |
| 74 | % % |
| 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 76 | % |
| 77 | % IsMVG() returns MagickTrue if the image format type, identified by the |
| 78 | % magick string, is MVG. |
| 79 | % |
| 80 | % The format of the IsMVG method is: |
| 81 | % |
| 82 | % MagickBooleanType IsMVG(const unsigned char *magick,const size_t length) |
| 83 | % |
| 84 | % A description of each parameter follows: |
| 85 | % |
| 86 | % o magick: compare image format pattern against these bytes. |
| 87 | % |
| 88 | % o length: Specifies the length of the magick string. |
| 89 | % |
| 90 | */ |
| 91 | static MagickBooleanType IsMVG(const unsigned char *magick,const size_t length) |
| 92 | { |
| 93 | if (length < 20) |
| 94 | return(MagickFalse); |
| 95 | if (LocaleNCompare((const char *) magick,"push graphic-context",20) == 0) |
| 96 | return(MagickTrue); |
| 97 | return(MagickFalse); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 102 | % % |
| 103 | % % |
| 104 | % % |
| 105 | % R e a d M V G I m a g e % |
| 106 | % % |
| 107 | % % |
| 108 | % % |
| 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 110 | % |
| 111 | % ReadMVGImage creates a gradient image and initializes it to |
| 112 | % the X server color range as specified by the filename. It allocates the |
| 113 | % memory necessary for the new Image structure and returns a pointer to the |
| 114 | % new image. |
| 115 | % |
| 116 | % The format of the ReadMVGImage method is: |
| 117 | % |
| 118 | % Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 119 | % |
| 120 | % A description of each parameter follows: |
| 121 | % |
| 122 | % o image_info: the image info. |
| 123 | % |
| 124 | % o exception: return any errors or warnings in this structure. |
| 125 | % |
| 126 | */ |
| 127 | static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 128 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | DrawInfo |
| 130 | *draw_info; |
| 131 | |
| 132 | Image |
| 133 | *image; |
| 134 | |
| 135 | MagickBooleanType |
| 136 | status; |
| 137 | |
| 138 | /* |
| 139 | Open image. |
| 140 | */ |
| 141 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 142 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | if (image_info->debug != MagickFalse) |
| 144 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 145 | image_info->filename); |
| 146 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 147 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 148 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 149 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 150 | if (status == MagickFalse) |
| 151 | { |
| 152 | image=DestroyImageList(image); |
| 153 | return((Image *) NULL); |
| 154 | } |
| 155 | if ((image->columns == 0) || (image->rows == 0)) |
| 156 | { |
| 157 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 158 | primitive[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 160 | char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | *p; |
| 162 | |
| 163 | SegmentInfo |
| 164 | bounds; |
| 165 | |
| 166 | /* |
| 167 | Determine size of image canvas. |
| 168 | */ |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 169 | (void) memset(&bounds,0,sizeof(bounds)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 170 | while (ReadBlobString(image,primitive) != (char *) NULL) |
| 171 | { |
Cristy | 198399d | 2018-04-22 18:34:25 -0400 | [diff] [blame] | 172 | int |
| 173 | count; |
| 174 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | for (p=primitive; (*p == ' ') || (*p == '\t'); p++) ; |
Cristy | 198399d | 2018-04-22 18:34:25 -0400 | [diff] [blame] | 176 | count=sscanf(p,"viewbox %lf %lf %lf %lf",&bounds.x1,&bounds.y1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | &bounds.x2,&bounds.y2); |
Cristy | 198399d | 2018-04-22 18:34:25 -0400 | [diff] [blame] | 178 | if (count != 4) |
| 179 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 180 | image->columns=(size_t) floor((bounds.x2-bounds.x1)+0.5); |
| 181 | image->rows=(size_t) floor((bounds.y2-bounds.y1)+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if ((image->columns == 0) || (image->rows == 0)) |
| 186 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 187 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
Cristy | 2667e84 | 2018-07-07 09:00:08 -0400 | [diff] [blame] | 188 | if (draw_info->density != (char *) NULL) |
| 189 | draw_info->density=DestroyString(draw_info->density); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 190 | draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/ |
Cristy | a98e9df | 2017-04-18 20:51:00 -0400 | [diff] [blame] | 191 | 96.0; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 192 | draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/ |
Cristy | a98e9df | 2017-04-18 20:51:00 -0400 | [diff] [blame] | 193 | 96.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 194 | image->columns=(size_t) (draw_info->affine.sx*image->columns); |
| 195 | image->rows=(size_t) (draw_info->affine.sy*image->rows); |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 196 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 197 | if (status == MagickFalse) |
Cristy | 2477eac | 2017-07-23 10:29:39 -0400 | [diff] [blame] | 198 | { |
| 199 | draw_info=DestroyDrawInfo(draw_info); |
| 200 | return(DestroyImageList(image)); |
| 201 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 202 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 203 | { |
Cristy | 2477eac | 2017-07-23 10:29:39 -0400 | [diff] [blame] | 204 | draw_info=DestroyDrawInfo(draw_info); |
Dirk Lemstra | 17a9864 | 2018-03-04 12:32:25 +0100 | [diff] [blame] | 205 | return(DestroyImageList(image)); |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 206 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | /* |
| 208 | Render drawing. |
| 209 | */ |
| 210 | if (GetBlobStreamData(image) == (unsigned char *) NULL) |
| 211 | draw_info->primitive=FileToString(image->filename,~0UL,exception); |
| 212 | else |
| 213 | { |
Cristy | d27726f | 2018-07-19 16:05:57 -0400 | [diff] [blame] | 214 | MagickSizeType |
| 215 | length; |
| 216 | |
| 217 | length=GetBlobSize(image); |
| 218 | if (length == (MagickSizeType) ((size_t) length)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 219 | { |
Cristy | 566eaf1 | 2020-11-15 17:46:43 +0000 | [diff] [blame] | 220 | draw_info->primitive=(char *) AcquireQuantumMemory(1,(size_t) length+1); |
Cristy | d27726f | 2018-07-19 16:05:57 -0400 | [diff] [blame] | 221 | if (draw_info->primitive != (char *) NULL) |
| 222 | { |
| 223 | memcpy(draw_info->primitive,GetBlobStreamData(image),(size_t) |
| 224 | length); |
| 225 | draw_info->primitive[length]='\0'; |
| 226 | } |
| 227 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | } |
Cristy | 6aa4376 | 2016-05-24 08:00:42 -0400 | [diff] [blame] | 229 | if (draw_info->primitive == (char *) NULL) |
Cristy | c7d011f | 2018-06-18 18:50:04 -0400 | [diff] [blame] | 230 | { |
| 231 | draw_info=DestroyDrawInfo(draw_info); |
| 232 | return(DestroyImageList(image)); |
| 233 | } |
Cristy | 568da17 | 2018-07-21 16:46:40 -0400 | [diff] [blame] | 234 | if (*draw_info->primitive == '@') |
| 235 | { |
| 236 | draw_info=DestroyDrawInfo(draw_info); |
| 237 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 238 | } |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 239 | (void) DrawImage(image,draw_info,exception); |
Cristy | fdb31cf | 2019-02-12 21:26:35 -0500 | [diff] [blame] | 240 | (void) SetImageArtifact(image,"mvg:vector-graphics",draw_info->primitive); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | draw_info=DestroyDrawInfo(draw_info); |
| 242 | (void) CloseBlob(image); |
| 243 | return(GetFirstImageInList(image)); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 248 | % % |
| 249 | % % |
| 250 | % % |
| 251 | % R e g i s t e r M V G I m a g e % |
| 252 | % % |
| 253 | % % |
| 254 | % % |
| 255 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 256 | % |
| 257 | % RegisterMVGImage() adds properties for the MVG image format |
| 258 | % to the list of supported formats. The properties include the image format |
| 259 | % tag, a method to read and/or write the format, whether the format |
| 260 | % supports the saving of more than one frame to the same file or blob, |
| 261 | % whether the format supports native in-memory I/O, and a brief |
| 262 | % description of the format. |
| 263 | % |
| 264 | % The format of the RegisterMVGImage method is: |
| 265 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 266 | % size_t RegisterMVGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | % |
| 268 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 269 | ModuleExport size_t RegisterMVGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 270 | { |
| 271 | MagickInfo |
| 272 | *entry; |
| 273 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 274 | entry=AcquireMagickInfo("MVG","MVG","Magick Vector Graphics"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 275 | entry->decoder=(DecodeImageHandler *) ReadMVGImage; |
| 276 | entry->encoder=(EncodeImageHandler *) WriteMVGImage; |
| 277 | entry->magick=(IsImageFormatHandler *) IsMVG; |
Cristy | a58ba05 | 2016-05-07 16:43:55 -0400 | [diff] [blame] | 278 | entry->format_type=ImplicitFormatType; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 279 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | (void) RegisterMagickInfo(entry); |
| 281 | return(MagickImageCoderSignature); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 286 | % % |
| 287 | % % |
| 288 | % % |
| 289 | % U n r e g i s t e r M V G I m a g e % |
| 290 | % % |
| 291 | % % |
| 292 | % % |
| 293 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 294 | % |
| 295 | % UnregisterMVGImage() removes format registrations made by the |
| 296 | % MVG module from the list of supported formats. |
| 297 | % |
| 298 | % The format of the UnregisterMVGImage method is: |
| 299 | % |
| 300 | % UnregisterMVGImage(void) |
| 301 | % |
| 302 | */ |
| 303 | ModuleExport void UnregisterMVGImage(void) |
| 304 | { |
| 305 | (void) UnregisterMagickInfo("MVG"); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 310 | % % |
| 311 | % % |
| 312 | % % |
| 313 | % W r i t e M V G I m a g e % |
| 314 | % % |
| 315 | % % |
| 316 | % % |
| 317 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 318 | % |
| 319 | % WriteMVGImage() writes an image to a file in MVG image format. |
| 320 | % |
| 321 | % The format of the WriteMVGImage method is: |
| 322 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 323 | % MagickBooleanType WriteMVGImage(const ImageInfo *image_info, |
| 324 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 325 | % |
| 326 | % A description of each parameter follows. |
| 327 | % |
| 328 | % o image_info: the image info. |
| 329 | % |
| 330 | % o image: The image. |
| 331 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 332 | % o exception: return any errors or warnings in this structure. |
| 333 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 334 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 335 | static MagickBooleanType WriteMVGImage(const ImageInfo *image_info,Image *image, |
| 336 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 337 | { |
| 338 | const char |
| 339 | *value; |
| 340 | |
| 341 | MagickBooleanType |
| 342 | status; |
| 343 | |
| 344 | /* |
| 345 | Open output image file. |
| 346 | */ |
| 347 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 348 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 350 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | if (image->debug != MagickFalse) |
| 352 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
Cristy | fdb31cf | 2019-02-12 21:26:35 -0500 | [diff] [blame] | 353 | value=GetImageArtifact(image,"mvg:vector-graphics"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 354 | if (value == (const char *) NULL) |
| 355 | ThrowWriterException(OptionError,"NoImageVectorGraphics"); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 356 | status=OpenBlob(image_info,image,WriteBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | if (status == MagickFalse) |
| 358 | return(status); |
| 359 | (void) WriteBlob(image,strlen(value),(const unsigned char *) value); |
| 360 | (void) CloseBlob(image); |
| 361 | return(MagickTrue); |
| 362 | } |