]> granicus.if.org Git - imagemagick/blob - coders/wmf.c
(no commit message)
[imagemagick] / coders / wmf.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             W   W   M   M  FFFFF                            %
7 %                             W   W   MM MM  F                                %
8 %                             W W W   M M M  FFF                              %
9 %                             WW WW   M   M  F                                %
10 %                             W   W   M   M  F                                %
11 %                                                                             %
12 %                                                                             %
13 %                        Read Windows Metafile Format                         %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                               December 2000                                 %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 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 \f
37 /*
38   Include declarations.
39 */
40 #include "magick/studio.h"
41 #include "magick/property.h"
42 #include "magick/blob.h"
43 #include "magick/blob-private.h"
44 #include "magick/color.h"
45 #include "magick/color-private.h"
46 #include "magick/constitute.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/log.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/paint.h"
58 #include "magick/quantum-private.h"
59 #include "magick/static.h"
60 #include "magick/string_.h"
61 #include "magick/module.h"
62 #include "magick/type.h"
63 #include "magick/module.h"
64 #include "wand/MagickWand.h"
65
66 #if defined(MAGICKCORE_WMF_DELEGATE)
67 #include "libwmf/api.h"
68 #include "libwmf/gd.h"
69 \f
70 /*
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %   R e a d W M F I m a g e                                                   %
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %
81 %  ReadWMFImage() reads an Windows Metafile image file and returns it.  It
82 %  allocates the memory necessary for the new Image structure and returns a
83 %  pointer to the new image.
84 %
85 %  The format of the ReadWMFImage method is:
86 %
87 %      Image *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o image_info: the image info.
92 %
93 %    o exception: return any errors or warnings in this structure.
94 %
95 */
96
97 static int WMFReadBlob(void *image)
98 {
99   return(ReadBlobByte((Image *) image));
100 }
101
102 static int WMFSeekBlob(void *image,long offset)
103 {
104   return((int) SeekBlob((Image *) image,(MagickOffsetType) offset,SEEK_SET));
105 }
106
107 static long WMFTellBlob(void *image)
108 {
109   return((long) TellBlob((Image*) image));
110 }
111
112 static Image *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
113 {
114   char
115     filename[MaxTextExtent];
116
117   int
118     unique_file;
119
120   FILE
121     *file;
122
123   Image
124     *image;
125
126   ImageInfo
127     *read_info;
128
129   unsigned long
130     flags;
131
132   wmfAPI
133     *wmf_info;
134
135   wmfAPI_Options
136     options;
137
138   wmfD_Rect
139     bounding_box;
140
141   wmf_gd_t
142    *gd_info;
143
144   wmf_error_t
145     status;
146
147   /*
148     Read WMF image.
149   */
150   image=AcquireImage(image_info);
151   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
152   if (status == MagickFalse)
153     {
154       image=DestroyImageList(image);
155       return((Image *) NULL);
156     }
157   wmf_info=(wmfAPI *) NULL;
158   flags=0;
159   flags|=WMF_OPT_IGNORE_NONFATAL;
160   flags|=WMF_OPT_FUNCTION;
161   options.function=wmf_gd_function;
162   status=wmf_api_create(&wmf_info,flags,&options);
163   if (status != wmf_E_None)
164     {
165       if (wmf_info != (wmfAPI *) NULL)
166         wmf_api_destroy(wmf_info);
167       ThrowReaderException(DelegateError,"UnableToInitializeWMFLibrary");
168     }
169   status=wmf_bbuf_input(wmf_info,WMFReadBlob,WMFSeekBlob,WMFTellBlob,
170     (void *) image);
171   if (status != wmf_E_None)
172     {
173       wmf_api_destroy(wmf_info);
174       ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
175         image->filename);
176       image=DestroyImageList(image);
177       return((Image *) NULL);
178     }
179   status=wmf_scan(wmf_info,0,&bounding_box);
180   if (status != wmf_E_None)
181     {
182       wmf_api_destroy(wmf_info);
183       ThrowReaderException(DelegateError,"FailedToScanFile");
184     }
185   gd_info=WMF_GD_GetData(wmf_info);
186   file=(FILE *) NULL;
187   unique_file=AcquireUniqueFileResource(filename);
188   if (unique_file != -1)
189     file=fdopen(unique_file,"wb");
190   if ((unique_file == -1) || (file == (FILE *) NULL))
191     {
192       wmf_api_destroy(wmf_info);
193       ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
194     }
195   gd_info->flags|=WMF_GD_OUTPUT_FILE;
196   gd_info->file=file;
197   gd_info->bbox=bounding_box;
198   wmf_display_size(wmf_info,&gd_info->width,&gd_info->height,
199     ceil(image->x_resolution-0.5),ceil(image->y_resolution-0.5));
200   status=wmf_play(wmf_info,0,&bounding_box);
201   if (status != wmf_E_None)
202     {
203       wmf_api_destroy(wmf_info);
204       ThrowReaderException(DelegateError,"FailedToRenderFile");
205     }
206   (void) fclose(file);
207   wmf_api_destroy(wmf_info);
208   (void) CloseBlob(image);
209   image=DestroyImage(image);
210   /*
211     Read GD image.
212   */
213   read_info=CloneImageInfo(image_info);
214   (void) FormatMagickString(read_info->filename,MaxTextExtent,"png:%.1024s",
215     filename);
216   image=ReadImage(read_info,exception);
217   read_info=DestroyImageInfo(read_info);
218   if (image != (Image *) NULL)
219     {
220       (void) CopyMagickString(image->filename,image_info->filename,
221         MaxTextExtent);
222       (void) CopyMagickString(image->magick_filename,image_info->filename,
223         MaxTextExtent);
224       (void) CopyMagickString(image->magick,"WMF",MaxTextExtent);
225     }
226   (void) RelinquishUniqueFileResource(filename);
227   return(GetFirstImageInList(image));
228 }
229 #endif
230 \f
231 /*
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 %                                                                             %
234 %                                                                             %
235 %                                                                             %
236 %   R e g i s t e r W M F I m a g e                                           %
237 %                                                                             %
238 %                                                                             %
239 %                                                                             %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 %
242 %  RegisterWMFImage() adds attributes for the WMF image format to
243 %  the list of supported formats.  The attributes include the image format
244 %  tag, a method to read and/or write the format, whether the format
245 %  supports the saving of more than one frame to the same file or blob,
246 %  whether the format supports native in-memory I/O, and a brief
247 %  description of the format.
248 %
249 %  The format of the RegisterWMFImage method is:
250 %
251 %      unsigned long RegisterWMFImage(void)
252 %
253 */
254 ModuleExport unsigned long RegisterWMFImage(void)
255 {
256   MagickInfo
257     *entry;
258
259   entry = SetMagickInfo("WMZ");
260 #if defined(MAGICKCORE_WMF_DELEGATE)
261   entry->decoder=ReadWMFImage;
262 #endif
263   entry->description=ConstantString("Compressed Windows Meta File");
264   entry->module=ConstantString("WMZ");
265   (void) RegisterMagickInfo(entry);
266   entry=SetMagickInfo("WMF");
267 #if defined(MAGICKCORE_WMF_DELEGATE)
268   entry->decoder=ReadWMFImage;
269 #endif
270   entry->description=ConstantString("Windows Meta File");
271   entry->module=ConstantString("WMF");
272   (void) RegisterMagickInfo(entry);
273   return(MagickImageCoderSignature);
274 }
275 \f
276 /*
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %   U n r e g i s t e r W M F I m a g e                                       %
282 %                                                                             %
283 %                                                                             %
284 %                                                                             %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %
287 %  UnregisterWMFImage() removes format registrations made by the
288 %  WMF module from the list of supported formats.
289 %
290 %  The format of the UnregisterWMFImage method is:
291 %
292 %      UnregisterWMFImage(void)
293 %
294 */
295 ModuleExport void UnregisterWMFImage(void)
296 {
297   (void) UnregisterMagickInfo("WMZ");
298   (void) UnregisterMagickInfo("WMF");
299 }