2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Vector Graphics Metafiles. %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
63 static MagickBooleanType
64 WriteMVGImage(const ImageInfo *,Image *,ExceptionInfo *);
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % IsMVG() returns MagickTrue if the image format type, identified by the
78 % magick string, is MVG.
80 % The format of the IsMVG method is:
82 % MagickBooleanType IsMVG(const unsigned char *magick,const size_t length)
84 % A description of each parameter follows:
86 % o magick: compare image format pattern against these bytes.
88 % o length: Specifies the length of the magick string.
91 static MagickBooleanType IsMVG(const unsigned char *magick,const size_t length)
95 if (LocaleNCompare((const char *) magick,"push graphic-context",20) == 0)
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % R e a d M V G I m a g e %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
116 % The format of the ReadMVGImage method is:
118 % Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image_info: the image info.
124 % o exception: return any errors or warnings in this structure.
127 static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 #define BoundingBox "viewbox"
143 assert(image_info != (const ImageInfo *) NULL);
144 assert(image_info->signature == MagickCoreSignature);
145 if (image_info->debug != MagickFalse)
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
147 image_info->filename);
148 assert(exception != (ExceptionInfo *) NULL);
149 assert(exception->signature == MagickCoreSignature);
150 image=AcquireImage(image_info,exception);
151 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
152 if (status == MagickFalse)
154 image=DestroyImageList(image);
155 return((Image *) NULL);
157 if ((image->columns == 0) || (image->rows == 0))
160 primitive[MagickPathExtent];
169 Determine size of image canvas.
171 while (ReadBlobString(image,primitive) != (char *) NULL)
173 for (p=primitive; (*p == ' ') || (*p == '\t'); p++) ;
174 if (LocaleNCompare(BoundingBox,p,strlen(BoundingBox)) != 0)
176 (void) sscanf(p,"viewbox %lf %lf %lf %lf",&bounds.x1,&bounds.y1,
177 &bounds.x2,&bounds.y2);
178 image->columns=(size_t) floor((bounds.x2-bounds.x1)+0.5);
179 image->rows=(size_t) floor((bounds.y2-bounds.y1)+0.5);
183 if ((image->columns == 0) || (image->rows == 0))
184 ThrowReaderException(OptionError,"MustSpecifyImageSize");
185 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
186 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
188 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
190 image->columns=(size_t) (draw_info->affine.sx*image->columns);
191 image->rows=(size_t) (draw_info->affine.sy*image->rows);
192 status=SetImageExtent(image,image->columns,image->rows,exception);
193 if (status == MagickFalse)
195 draw_info=DestroyDrawInfo(draw_info);
196 return(DestroyImageList(image));
198 if (SetImageBackgroundColor(image,exception) == MagickFalse)
200 draw_info=DestroyDrawInfo(draw_info);
201 image=DestroyImageList(image);
202 return((Image *) NULL);
207 if (GetBlobStreamData(image) == (unsigned char *) NULL)
208 draw_info->primitive=FileToString(image->filename,~0UL,exception);
211 draw_info->primitive=(char *) AcquireMagickMemory(GetBlobSize(image)+1);
212 if (draw_info->primitive != (char *) NULL)
214 CopyMagickMemory(draw_info->primitive,GetBlobStreamData(image),
216 draw_info->primitive[GetBlobSize(image)]='\0';
219 if (draw_info->primitive == (char *) NULL)
220 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
221 (void) DrawImage(image,draw_info,exception);
222 (void) SetImageArtifact(image,"MVG",draw_info->primitive);
223 draw_info=DestroyDrawInfo(draw_info);
224 (void) CloseBlob(image);
225 return(GetFirstImageInList(image));
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 % R e g i s t e r M V G I m a g e %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % RegisterMVGImage() adds properties for the MVG image format
240 % to the list of supported formats. The properties include the image format
241 % tag, a method to read and/or write the format, whether the format
242 % supports the saving of more than one frame to the same file or blob,
243 % whether the format supports native in-memory I/O, and a brief
244 % description of the format.
246 % The format of the RegisterMVGImage method is:
248 % size_t RegisterMVGImage(void)
251 ModuleExport size_t RegisterMVGImage(void)
256 entry=AcquireMagickInfo("MVG","MVG","Magick Vector Graphics");
257 entry->decoder=(DecodeImageHandler *) ReadMVGImage;
258 entry->encoder=(EncodeImageHandler *) WriteMVGImage;
259 entry->magick=(IsImageFormatHandler *) IsMVG;
260 entry->format_type=ImplicitFormatType;
261 entry->flags^=CoderAdjoinFlag;
262 (void) RegisterMagickInfo(entry);
263 return(MagickImageCoderSignature);
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % U n r e g i s t e r M V G I m a g e %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % UnregisterMVGImage() removes format registrations made by the
278 % MVG module from the list of supported formats.
280 % The format of the UnregisterMVGImage method is:
282 % UnregisterMVGImage(void)
285 ModuleExport void UnregisterMVGImage(void)
287 (void) UnregisterMagickInfo("MVG");
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % W r i t e M V G I m a g e %
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % WriteMVGImage() writes an image to a file in MVG image format.
303 % The format of the WriteMVGImage method is:
305 % MagickBooleanType WriteMVGImage(const ImageInfo *image_info,
306 % Image *image,ExceptionInfo *exception)
308 % A description of each parameter follows.
310 % o image_info: the image info.
312 % o image: The image.
314 % o exception: return any errors or warnings in this structure.
317 static MagickBooleanType WriteMVGImage(const ImageInfo *image_info,Image *image,
318 ExceptionInfo *exception)
327 Open output image file.
329 assert(image_info != (const ImageInfo *) NULL);
330 assert(image_info->signature == MagickCoreSignature);
331 assert(image != (Image *) NULL);
332 assert(image->signature == MagickCoreSignature);
333 if (image->debug != MagickFalse)
334 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
335 value=GetImageArtifact(image,"MVG");
336 if (value == (const char *) NULL)
337 ThrowWriterException(OptionError,"NoImageVectorGraphics");
338 status=OpenBlob(image_info,image,WriteBlobMode,exception);
339 if (status == MagickFalse)
341 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
342 (void) CloseBlob(image);