root / ImageMagick / trunk / coders / mvg.c

Revision 11742, 12.9 kB (checked in by cristy, 3 weeks ago)
Line 
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                                %
16%                                John Cristy                                  %
17%                                 April 2000                                  %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization      %
21%  dedicated to making software imaging solutions freely available.           %
22%                                                                             %
23%  You may not use this file except in compliance with the License.  You may  %
24%  obtain a copy of the License at                                            %
25%                                                                             %
26%    http://www.imagemagick.org/script/license.php                            %
27%                                                                             %
28%  Unless required by applicable law or agreed to in writing, software        %
29%  distributed under the License is distributed on an "AS IS" BASIS,          %
30%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31%  See the License for the specific language governing permissions and        %
32%  limitations under the License.                                             %
33%                                                                             %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40  Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/artifact.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/draw.h"
47#include "magick/exception.h"
48#include "magick/exception-private.h"
49#include "magick/image.h"
50#include "magick/image-private.h"
51#include "magick/list.h"
52#include "magick/magick.h"
53#include "magick/memory_.h"
54#include "magick/module.h"
55#include "magick/property.h"
56#include "magick/quantum-private.h"
57#include "magick/static.h"
58#include "magick/string_.h"
59
60/*
61  Forward declarations.
62*/
63static MagickBooleanType
64  WriteMVGImage(const ImageInfo *,Image *);
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: This string is generally the first few bytes of an image file
87%      or blob.
88%
89%    o length: Specifies the length of the magick string.
90%
91%
92*/
93static MagickBooleanType IsMVG(const unsigned char *magick,const size_t length)
94{
95  if (length < 20)
96    return(MagickFalse);
97  if (LocaleNCompare((char *) magick,"push graphic-context",20) == 0)
98    return(MagickTrue);
99  return(MagickFalse);
100}
101
102/*
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104%                                                                             %
105%                                                                             %
106%                                                                             %
107%   R e a d M V G I m a g e                                                   %
108%                                                                             %
109%                                                                             %
110%                                                                             %
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112%
113%  ReadMVGImage creates a gradient image and initializes it to
114%  the X server color range as specified by the filename.  It allocates the
115%  memory necessary for the new Image structure and returns a pointer to the
116%  new image.
117%
118%  The format of the ReadMVGImage method is:
119%
120%      Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
121%
122%  A description of each parameter follows:
123%
124%    o image_info: the image info.
125%
126%    o exception: return any errors or warnings in this structure.
127%
128*/
129static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
130{
131#define BoundingBox  "viewbox"
132
133  DrawInfo
134    *draw_info;
135
136  Image
137    *image;
138
139  MagickBooleanType
140    status;
141
142  unsigned char
143    *primitive;
144
145  /*
146    Open image.
147  */
148  assert(image_info != (const ImageInfo *) NULL);
149  assert(image_info->signature == MagickSignature);
150  if (image_info->debug != MagickFalse)
151    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
152      image_info->filename);
153  assert(exception != (ExceptionInfo *) NULL);
154  assert(exception->signature == MagickSignature);
155  image=AcquireImage(image_info);
156  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
157  if (status == MagickFalse)
158    {
159      image=DestroyImageList(image);
160      return((Image *) NULL);
161    }
162  if ((image->columns == 0) || (image->rows == 0))
163    {
164      char
165        primitive[MaxTextExtent];
166
167      register char
168        *p;
169
170      SegmentInfo
171        bounds;
172
173      /*
174        Determine size of image canvas.
175      */
176      while (ReadBlobString(image,primitive) != (char *) NULL)
177      {
178        for (p=primitive; (*p == ' ') || (*p == '\t'); p++) ;
179        if (LocaleNCompare(BoundingBox,p,strlen(BoundingBox)) != 0)
180          continue;
181        (void) sscanf(p,"viewbox %lf %lf %lf %lf",&bounds.x1,&bounds.y1,
182          &bounds.x2,&bounds.y2);
183        image->columns=(unsigned long) ((bounds.x2-bounds.x1)+0.5);
184        image->rows=(unsigned long) ((bounds.y2-bounds.y1)+0.5);
185        break;
186      }
187    }
188  if ((image->columns == 0) || (image->rows == 0))
189    ThrowReaderException(OptionError,"MustSpecifyImageSize");
190  draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
191  draw_info->affine.sx=image->x_resolution == 0.0 ? 1.0 : image->x_resolution/
192    DefaultResolution;
193  draw_info->affine.sy=image->y_resolution == 0.0 ? 1.0 : image->y_resolution/
194    DefaultResolution;
195  image->columns=(unsigned long) (draw_info->affine.sx*image->columns);
196  image->rows=(unsigned long) (draw_info->affine.sy*image->rows);
197  if (SetImageExtent(image,0,0) == MagickFalse)
198    {
199      InheritException(exception,&image->exception);
200      return(DestroyImageList(image));
201    }
202  (void) SetImageBackgroundColor(image);
203  /*
204    Render drawing.
205  */
206  primitive=GetBlobStreamData(image);
207  if (primitive != (unsigned char *) NULL)
208    draw_info->primitive=AcquireString((char *) primitive);
209  else
210    draw_info->primitive=FileToString(image->filename,~0UL,exception);
211  if (draw_info->primitive == (char *) NULL)
212    return((Image *) NULL);
213  (void) DrawImage(image,draw_info);
214  draw_info=DestroyDrawInfo(draw_info);
215  (void) CloseBlob(image);
216  return(GetFirstImageInList(image));
217}
218
219/*
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%                                                                             %
222%                                                                             %
223%                                                                             %
224%   R e g i s t e r M V G I m a g e                                           %
225%                                                                             %
226%                                                                             %
227%                                                                             %
228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229%
230%  RegisterMVGImage() adds properties for the MVG image format
231%  to the list of supported formats.  The properties include the image format
232%  tag, a method to read and/or write the format, whether the format
233%  supports the saving of more than one frame to the same file or blob,
234%  whether the format supports native in-memory I/O, and a brief
235%  description of the format.
236%
237%  The format of the RegisterMVGImage method is:
238%
239%      unsigned long RegisterMVGImage(void)
240%
241*/
242ModuleExport unsigned long RegisterMVGImage(void)
243{
244  MagickInfo
245    *entry;
246
247  entry=SetMagickInfo("MVG");
248  entry->decoder=(DecodeImageHandler *) ReadMVGImage;
249  entry->encoder=(EncodeImageHandler *) WriteMVGImage;
250  entry->magick=(IsImageFormatHandler *) IsMVG;
251  entry->adjoin=MagickFalse;
252  entry->seekable_stream=MagickTrue;
253  entry->description=ConstantString("Magick Vector Graphics");
254  entry->module=ConstantString("MVG");
255  (void) RegisterMagickInfo(entry);
256  return(MagickImageCoderSignature);
257}
258
259/*
260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261%                                                                             %
262%                                                                             %
263%                                                                             %
264%   U n r e g i s t e r M V G I m a g e                                       %
265%                                                                             %
266%                                                                             %
267%                                                                             %
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269%
270%  UnregisterMVGImage() removes format registrations made by the
271%  MVG module from the list of supported formats.
272%
273%  The format of the UnregisterMVGImage method is:
274%
275%      UnregisterMVGImage(void)
276%
277*/
278ModuleExport void UnregisterMVGImage(void)
279{
280  (void) UnregisterMagickInfo("MVG");
281}
282
283/*
284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285%                                                                             %
286%                                                                             %
287%                                                                             %
288%   W r i t e M V G I m a g e                                                 %
289%                                                                             %
290%                                                                             %
291%                                                                             %
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293%
294%  WriteMVGImage() writes an image to a file in MVG image format.
295%
296%  The format of the WriteMVGImage method is:
297%
298%      MagickBooleanType WriteMVGImage(const ImageInfo *image_info,Image *image)
299%
300%  A description of each parameter follows.
301%
302%    o image_info: the image info.
303%
304%    o image:  The image.
305%
306*/
307static MagickBooleanType WriteMVGImage(const ImageInfo *image_info,Image *image)
308{
309  const char
310    *value;
311
312  MagickBooleanType
313    status;
314
315  /*
316    Open output image file.
317  */
318  assert(image_info != (const ImageInfo *) NULL);
319  assert(image_info->signature == MagickSignature);
320  assert(image != (Image *) NULL);
321  assert(image->signature == MagickSignature);
322  if (image->debug != MagickFalse)
323    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
324  value=GetImageArtifact(image,"MVG");
325  if (value == (const char *) NULL)
326    ThrowWriterException(OptionError,"NoImageVectorGraphics");
327  status=OpenBlob(image_info,image,WriteBlobMode,&image->exception);
328  if (status == MagickFalse)
329    return(status);
330  (void) WriteBlob(image,strlen(value),(unsigned char *) value);
331  (void) CloseBlob(image);
332  return(MagickTrue);
333}
Note: See TracBrowser for help on using the browser.