blob: 939d61667093d47672e867e4b635882c981783cd [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
16% John Cristy %
17% April 2000 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/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"
cristy3ed852e2009-09-05 21:47:34 +000059
60/*
61 Forward declarations.
62*/
63static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000064 WriteMVGImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000065
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*/
91static 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*/
127static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
128{
129#define BoundingBox "viewbox"
130
131 DrawInfo
132 *draw_info;
133
134 Image
135 *image;
136
137 MagickBooleanType
138 status;
139
140 /*
141 Open image.
142 */
143 assert(image_info != (const ImageInfo *) NULL);
144 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000150 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000151 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
152 if (status == MagickFalse)
153 {
154 image=DestroyImageList(image);
155 return((Image *) NULL);
156 }
157 if ((image->columns == 0) || (image->rows == 0))
158 {
159 char
160 primitive[MaxTextExtent];
161
162 register char
163 *p;
164
165 SegmentInfo
166 bounds;
167
168 /*
169 Determine size of image canvas.
170 */
171 while (ReadBlobString(image,primitive) != (char *) NULL)
172 {
173 for (p=primitive; (*p == ' ') || (*p == '\t'); p++) ;
174 if (LocaleNCompare(BoundingBox,p,strlen(BoundingBox)) != 0)
175 continue;
176 (void) sscanf(p,"viewbox %lf %lf %lf %lf",&bounds.x1,&bounds.y1,
177 &bounds.x2,&bounds.y2);
cristybb503372010-05-27 20:51:26 +0000178 image->columns=(size_t) floor((bounds.x2-bounds.x1)+0.5);
179 image->rows=(size_t) floor((bounds.y2-bounds.y1)+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000180 break;
181 }
182 }
183 if ((image->columns == 0) || (image->rows == 0))
184 ThrowReaderException(OptionError,"MustSpecifyImageSize");
185 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
cristy2a11bef2011-10-28 18:33:11 +0000186 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
cristy3ed852e2009-09-05 21:47:34 +0000187 DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +0000188 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
cristy3ed852e2009-09-05 21:47:34 +0000189 DefaultResolution;
cristybb503372010-05-27 20:51:26 +0000190 image->columns=(size_t) (draw_info->affine.sx*image->columns);
191 image->rows=(size_t) (draw_info->affine.sy*image->rows);
cristyea1a8aa2011-10-20 13:24:06 +0000192 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000193 {
cristy95524f92010-02-16 18:44:34 +0000194 image=DestroyImageList(image);
195 return((Image *) NULL);
196 }
cristy3ed852e2009-09-05 21:47:34 +0000197 /*
198 Render drawing.
199 */
200 if (GetBlobStreamData(image) == (unsigned char *) NULL)
201 draw_info->primitive=FileToString(image->filename,~0UL,exception);
202 else
203 {
204 draw_info->primitive=(char *) AcquireMagickMemory(GetBlobSize(image)+1);
205 if (draw_info->primitive != (char *) NULL)
206 {
207 CopyMagickMemory(draw_info->primitive,GetBlobStreamData(image),
208 GetBlobSize(image));
209 draw_info->primitive[GetBlobSize(image)]='\0';
210 }
211 }
cristy018f07f2011-09-04 21:15:19 +0000212 (void) DrawImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000213 draw_info=DestroyDrawInfo(draw_info);
214 (void) CloseBlob(image);
215 return(GetFirstImageInList(image));
216}
217
218/*
219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220% %
221% %
222% %
223% R e g i s t e r M V G I m a g e %
224% %
225% %
226% %
227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228%
229% RegisterMVGImage() adds properties for the MVG image format
230% to the list of supported formats. The properties include the image format
231% tag, a method to read and/or write the format, whether the format
232% supports the saving of more than one frame to the same file or blob,
233% whether the format supports native in-memory I/O, and a brief
234% description of the format.
235%
236% The format of the RegisterMVGImage method is:
237%
cristybb503372010-05-27 20:51:26 +0000238% size_t RegisterMVGImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000239%
240*/
cristybb503372010-05-27 20:51:26 +0000241ModuleExport size_t RegisterMVGImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000242{
243 MagickInfo
244 *entry;
245
246 entry=SetMagickInfo("MVG");
247 entry->decoder=(DecodeImageHandler *) ReadMVGImage;
248 entry->encoder=(EncodeImageHandler *) WriteMVGImage;
249 entry->magick=(IsImageFormatHandler *) IsMVG;
250 entry->adjoin=MagickFalse;
251 entry->seekable_stream=MagickTrue;
252 entry->description=ConstantString("Magick Vector Graphics");
253 entry->module=ConstantString("MVG");
254 (void) RegisterMagickInfo(entry);
255 return(MagickImageCoderSignature);
256}
257
258/*
259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260% %
261% %
262% %
263% U n r e g i s t e r M V G I m a g e %
264% %
265% %
266% %
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268%
269% UnregisterMVGImage() removes format registrations made by the
270% MVG module from the list of supported formats.
271%
272% The format of the UnregisterMVGImage method is:
273%
274% UnregisterMVGImage(void)
275%
276*/
277ModuleExport void UnregisterMVGImage(void)
278{
279 (void) UnregisterMagickInfo("MVG");
280}
281
282/*
283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284% %
285% %
286% %
287% W r i t e M V G I m a g e %
288% %
289% %
290% %
291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292%
293% WriteMVGImage() writes an image to a file in MVG image format.
294%
295% The format of the WriteMVGImage method is:
296%
cristy1e178e72011-08-28 19:44:34 +0000297% MagickBooleanType WriteMVGImage(const ImageInfo *image_info,
298% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000299%
300% A description of each parameter follows.
301%
302% o image_info: the image info.
303%
304% o image: The image.
305%
cristy1e178e72011-08-28 19:44:34 +0000306% o exception: return any errors or warnings in this structure.
307%
cristy3ed852e2009-09-05 21:47:34 +0000308*/
cristy1e178e72011-08-28 19:44:34 +0000309static MagickBooleanType WriteMVGImage(const ImageInfo *image_info,Image *image,
310 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000311{
312 const char
313 *value;
314
315 MagickBooleanType
316 status;
317
318 /*
319 Open output image file.
320 */
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickSignature);
323 assert(image != (Image *) NULL);
324 assert(image->signature == MagickSignature);
325 if (image->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
327 value=GetImageArtifact(image,"MVG");
328 if (value == (const char *) NULL)
329 ThrowWriterException(OptionError,"NoImageVectorGraphics");
cristy1e178e72011-08-28 19:44:34 +0000330 status=OpenBlob(image_info,image,WriteBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000331 if (status == MagickFalse)
332 return(status);
333 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
334 (void) CloseBlob(image);
335 return(MagickTrue);
336}