]> granicus.if.org Git - imagemagick/blob - coders/xps.c
(no commit message)
[imagemagick] / coders / xps.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            X   X  PPPP   SSSSS                              %
7 %                             X X   P   P  SS                                 %
8 %                              X    PPPP    SSS                               %
9 %                             X X   P         SS                              %
10 %                            X   X  P      SSSSS                              %
11 %                                                                             %
12 %                                                                             %
13 %             Read/Write Microsoft XML Paper Specification Format             %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                               January 2008                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/color.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/delegate.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/profile.h"
64 #include "MagickCore/resource_.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/token.h"
70 #include "MagickCore/transform.h"
71 #include "MagickCore/utility.h"
72 \f
73 /*
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %   R e a d X P S I m a g e                                                   %
79 %                                                                             %
80 %                                                                             %
81 %                                                                             %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %
84 %  ReadXPSImage() reads a Printer Control Language image file and returns it.
85 %  It allocates the memory necessary for the new Image structure and returns a
86 %  pointer to the new image.
87 %
88 %  The format of the ReadXPSImage method is:
89 %
90 %      Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 %
92 %  A description of each parameter follows:
93 %
94 %    o image_info: the image info.
95 %
96 %    o exception: return any errors or warnings in this structure.
97 %
98 */
99 static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
100 {
101 #define CropBox  "CropBox"
102 #define DeviceCMYK  "DeviceCMYK"
103 #define MediaBox  "MediaBox"
104 #define RenderXPSText  "  Rendering XPS...  "
105
106   char
107     command[MaxTextExtent],
108     density[MaxTextExtent],
109     filename[MaxTextExtent],
110     geometry[MaxTextExtent],
111     options[MaxTextExtent],
112     input_filename[MaxTextExtent];
113
114   const char
115     *option;
116
117   const DelegateInfo
118     *delegate_info;
119
120   Image
121     *image,
122     *next_image;
123
124   ImageInfo
125     *read_info;
126
127   MagickBooleanType
128     cmyk,
129     status;
130
131   PointInfo
132     delta;
133
134   RectangleInfo
135     bounding_box,
136     page;
137
138   register char
139     *p;
140
141   register ssize_t
142     c;
143
144   SegmentInfo
145     bounds;
146
147   size_t
148     height,
149     width;
150
151   ssize_t
152     count;
153
154   assert(image_info != (const ImageInfo *) NULL);
155   assert(image_info->signature == MagickSignature);
156   if (image_info->debug != MagickFalse)
157     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
158       image_info->filename);
159   assert(exception != (ExceptionInfo *) NULL);
160   assert(exception->signature == MagickSignature);
161   /*
162     Open image file.
163   */
164   image=AcquireImage(image_info,exception);
165   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
166   if (status == MagickFalse)
167     {
168       image=DestroyImageList(image);
169       return((Image *) NULL);
170     }
171   status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
172   if (status == MagickFalse)
173     {
174       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
175         image_info->filename);
176       image=DestroyImageList(image);
177       return((Image *) NULL);
178     }
179   /*
180     Set the page density.
181   */
182   delta.x=DefaultResolution;
183   delta.y=DefaultResolution;
184   if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
185     {
186       GeometryInfo
187         geometry_info;
188
189       MagickStatusType
190         flags;
191
192       flags=ParseGeometry(PSDensityGeometry,&geometry_info);
193       image->resolution.x=geometry_info.rho;
194       image->resolution.y=geometry_info.sigma;
195       if ((flags & SigmaValue) == 0)
196         image->resolution.y=image->resolution.x;
197     }
198   (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",
199     image->resolution.x,image->resolution.y);
200   /*
201     Determine page geometry from the XPS media box.
202   */
203   cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
204   count=0;
205   (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
206   (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
207   (void) ResetMagickMemory(&page,0,sizeof(page));
208   (void) ResetMagickMemory(command,0,sizeof(command));
209   p=command;
210   for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
211   {
212     if (image_info->page != (char *) NULL)
213       continue;
214     /*
215       Note XPS elements.
216     */
217     *p++=(char) c;
218     if ((c != (int) '/') && (c != '\n') &&
219         ((size_t) (p-command) < (MaxTextExtent-1)))
220       continue;
221     *p='\0';
222     p=command;
223     /*
224       Is this a CMYK document?
225     */
226     if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
227       cmyk=MagickTrue;
228     if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
229       {
230         /*
231           Note region defined by crop box.
232         */
233         count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
234           &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
235         if (count != 4)
236           count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
237             &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
238       }
239     if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
240       {
241         /*
242           Note region defined by media box.
243         */
244         count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
245           &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
246         if (count != 4)
247           count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
248             &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
249       }
250     if (count != 4)
251       continue;
252     /*
253       Set XPS render geometry.
254     */
255     width=(size_t) (floor(bounds.x2+0.5)-ceil(bounds.x1-0.5));
256     height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5));
257     if (width > page.width)
258       page.width=width;
259     if (height > page.height)
260       page.height=height;
261   }
262   (void) CloseBlob(image);
263   /*
264     Render XPS with the GhostXPS delegate.
265   */
266   if ((page.width == 0) || (page.height == 0))
267     (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
268   if (image_info->page != (char *) NULL)
269     (void) ParseAbsoluteGeometry(image_info->page,&page);
270   (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",(double)
271     page.width,(double) page.height);
272   if (image_info->monochrome != MagickFalse)
273     delegate_info=GetDelegateInfo("xps:mono",(char *) NULL,exception);
274   else
275      if (cmyk != MagickFalse)
276        delegate_info=GetDelegateInfo("xps:cmyk",(char *) NULL,exception);
277      else
278        delegate_info=GetDelegateInfo("xps:color",(char *) NULL,exception);
279   if (delegate_info == (const DelegateInfo *) NULL)
280     return((Image *) NULL);
281   *options='\0';
282   if ((page.width == 0) || (page.height == 0))
283     (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
284   if (image_info->page != (char *) NULL)
285     (void) ParseAbsoluteGeometry(image_info->page,&page);
286   page.width=(size_t) floor(page.width*image->resolution.y/delta.x+0.5);
287   page.height=(size_t) floor(page.height*image->resolution.y/delta.y+0.5);
288   (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
289     page.width,(double) page.height);
290   image=DestroyImage(image);
291   read_info=CloneImageInfo(image_info);
292   *read_info->magick='\0';
293   if (read_info->number_scenes != 0)
294     {
295       if (read_info->number_scenes != 1)
296         (void) FormatLocaleString(options,MaxTextExtent,"-dLastPage=%.20g",
297           (double) (read_info->scene+read_info->number_scenes));
298       else
299         (void) FormatLocaleString(options,MaxTextExtent,
300           "-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1,
301           (double) (read_info->scene+read_info->number_scenes));
302       read_info->number_scenes=0;
303       if (read_info->scenes != (char *) NULL)
304         *read_info->scenes='\0';
305     }
306   option=GetImageOption(read_info,"authenticate");
307   if (option != (const char *) NULL)
308     (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
309       " -sPCLPassword=%s",option);
310   (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
311   (void) AcquireUniqueFilename(read_info->filename);
312   (void) FormatLocaleString(command,MaxTextExtent,
313     GetDelegateCommands(delegate_info),
314     read_info->antialias != MagickFalse ? 4 : 1,
315     read_info->antialias != MagickFalse ? 4 : 1,density,options,
316     read_info->filename,input_filename);
317   status=SystemCommand(MagickFalse,read_info->verbose,command,exception) != 0 ?
318     MagickTrue : MagickFalse;
319   image=ReadImage(read_info,exception);
320   (void) RelinquishUniqueFileResource(read_info->filename);
321   (void) RelinquishUniqueFileResource(input_filename);
322   read_info=DestroyImageInfo(read_info);
323   if (image == (Image *) NULL)
324     ThrowReaderException(DelegateError,"XPSDelegateFailed");
325   if (LocaleCompare(image->magick,"BMP") == 0)
326     {
327       Image
328         *cmyk_image;
329
330       cmyk_image=ConsolidateCMYKImages(image,exception);
331       if (cmyk_image != (Image *) NULL)
332         {
333           image=DestroyImageList(image);
334           image=cmyk_image;
335         }
336     }
337   do
338   {
339     (void) CopyMagickString(image->filename,filename,MaxTextExtent);
340     image->page=page;
341     next_image=SyncNextImageInList(image);
342     if (next_image != (Image *) NULL)
343       image=next_image;
344   } while (next_image != (Image *) NULL);
345   return(GetFirstImageInList(image));
346 }
347 \f
348 /*
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 %                                                                             %
351 %                                                                             %
352 %                                                                             %
353 %   R e g i s t e r X P S I m a g e                                           %
354 %                                                                             %
355 %                                                                             %
356 %                                                                             %
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 %
359 %  RegisterXPSImage() adds attributes for the Microsoft XML Paper Specification 
360 %  format to the list of supported formats.  The attributes include the image
361 %  format tag, a method to read and/or write the format, whether the format
362 %  supports the saving of more than one frame to the same file or blob,
363 %  whether the format supports native in-memory I/O, and a brief
364 %  description of the format.
365 %
366 %  The format of the RegisterXPSImage method is:
367 %
368 %      size_t RegisterXPSImage(void)
369 %
370 */
371 ModuleExport size_t RegisterXPSImage(void)
372 {
373   MagickInfo
374     *entry;
375
376   entry=SetMagickInfo("XPS");
377   entry->decoder=(DecodeImageHandler *) ReadXPSImage;
378   entry->adjoin=MagickFalse;
379   entry->blob_support=MagickFalse;
380   entry->seekable_stream=MagickTrue;
381   entry->thread_support=EncoderThreadSupport;
382   entry->description=ConstantString("Microsoft XML Paper Specification");
383   entry->module=ConstantString("XPS");
384   (void) RegisterMagickInfo(entry);
385   return(MagickImageCoderSignature);
386 }
387 \f
388 /*
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 %                                                                             %
391 %                                                                             %
392 %                                                                             %
393 %   U n r e g i s t e r X P S I m a g e                                       %
394 %                                                                             %
395 %                                                                             %
396 %                                                                             %
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 %
399 %  UnregisterXPSImage() removes format registrations made by the XPS module
400 %  from the list of supported formats.
401 %
402 %  The format of the UnregisterXPSImage method is:
403 %
404 %      UnregisterXPSImage(void)
405 %
406 */
407 ModuleExport void UnregisterXPSImage(void)
408 {
409   (void) UnregisterMagickInfo("XPS");
410 }