2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft XML Paper Specification Format %
20 % Copyright 1999-2010 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 % http://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 "magick/studio.h"
43 #include "magick/property.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/constitute.h"
50 #include "magick/delegate.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/profile.h"
63 #include "magick/resource_.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
67 #include "magick/module.h"
68 #include "magick/token.h"
69 #include "magick/transform.h"
70 #include "magick/utility.h"
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d X P S I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % ReadXPSImage() reads a Printer Control Language image file and returns it.
84 % It allocates the memory necessary for the new Image structure and returns a
85 % pointer to the new image.
87 % The format of the ReadXPSImage method is:
89 % Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 % A description of each parameter follows:
93 % o image_info: the image info.
95 % o exception: return any errors or warnings in this structure.
98 static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
100 #define CropBox "CropBox"
101 #define DeviceCMYK "DeviceCMYK"
102 #define MediaBox "MediaBox"
103 #define RenderXPSText " Rendering XPS... "
106 command[MaxTextExtent],
107 density[MaxTextExtent],
108 filename[MaxTextExtent],
109 geometry[MaxTextExtent],
110 options[MaxTextExtent],
111 input_filename[MaxTextExtent];
150 assert(image_info != (const ImageInfo *) NULL);
151 assert(image_info->signature == MagickSignature);
152 if (image_info->debug != MagickFalse)
153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
154 image_info->filename);
155 assert(exception != (ExceptionInfo *) NULL);
156 assert(exception->signature == MagickSignature);
160 image=AcquireImage(image_info);
161 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
162 if (status == MagickFalse)
164 image=DestroyImageList(image);
165 return((Image *) NULL);
167 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
168 if (status == MagickFalse)
170 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
171 image_info->filename);
172 image=DestroyImageList(image);
173 return((Image *) NULL);
176 Set the page density.
178 delta.x=DefaultResolution;
179 delta.y=DefaultResolution;
180 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
188 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
189 image->x_resolution=geometry_info.rho;
190 image->y_resolution=geometry_info.sigma;
191 if ((flags & SigmaValue) == 0)
192 image->y_resolution=image->x_resolution;
194 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
195 image->x_resolution,image->y_resolution);
197 Determine page geometry from the XPS media box.
199 cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
201 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
202 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
203 (void) ResetMagickMemory(&page,0,sizeof(page));
204 (void) ResetMagickMemory(command,0,sizeof(command));
206 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
208 if (image_info->page != (char *) NULL)
214 if ((c != (int) '/') && (c != '\n') &&
215 ((size_t) (p-command) < (MaxTextExtent-1)))
220 Is this a CMYK document?
222 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
224 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
227 Note region defined by crop box.
229 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
230 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
232 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
233 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
235 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
238 Note region defined by media box.
240 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
241 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
243 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
244 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
249 Set XPS render geometry.
251 width=(unsigned long) (bounds.x2-bounds.x1+0.5);
252 height=(unsigned long) (bounds.y2-bounds.y1+0.5);
253 if (width > page.width)
255 if (height > page.height)
258 (void) CloseBlob(image);
260 Render XPS with the GhostXPS delegate.
262 if ((page.width == 0) || (page.height == 0))
263 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
264 if (image_info->page != (char *) NULL)
265 (void) ParseAbsoluteGeometry(image_info->page,&page);
266 (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu",
267 page.width,page.height);
268 if (image_info->monochrome != MagickFalse)
269 delegate_info=GetDelegateInfo("xps:mono",(char *) NULL,exception);
271 if (cmyk != MagickFalse)
272 delegate_info=GetDelegateInfo("xps:cmyk",(char *) NULL,exception);
274 delegate_info=GetDelegateInfo("xps:color",(char *) NULL,exception);
275 if (delegate_info == (const DelegateInfo *) NULL)
276 return((Image *) NULL);
278 if ((page.width == 0) || (page.height == 0))
279 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
280 if (image_info->page != (char *) NULL)
281 (void) ParseAbsoluteGeometry(image_info->page,&page);
282 page.width=(unsigned long) (page.width*image->y_resolution/delta.x+0.5);
283 page.height=(unsigned long) (page.height*image->y_resolution/delta.y+0.5);
284 (void) FormatMagickString(options,MaxTextExtent,"-g%lux%lu ",
285 page.width,page.height);
286 image=DestroyImage(image);
287 read_info=CloneImageInfo(image_info);
288 *read_info->magick='\0';
289 if (read_info->number_scenes != 0)
291 if (read_info->number_scenes != 1)
292 (void) FormatMagickString(options,MaxTextExtent,"-dLastPage=%lu",
293 read_info->scene+read_info->number_scenes);
295 (void) FormatMagickString(options,MaxTextExtent,
296 "-dFirstPage=%lu -dLastPage=%lu",read_info->scene+1,read_info->scene+
297 read_info->number_scenes);
298 read_info->number_scenes=0;
299 if (read_info->scenes != (char *) NULL)
300 *read_info->scenes='\0';
302 if (read_info->authenticate != (char *) NULL)
303 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
304 " -sXPSPassword=%s",read_info->authenticate);
305 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
306 (void) AcquireUniqueFilename(read_info->filename);
307 (void) FormatMagickString(command,MaxTextExtent,
308 GetDelegateCommands(delegate_info),
309 read_info->antialias != MagickFalse ? 4 : 1,
310 read_info->antialias != MagickFalse ? 4 : 1,density,options,
311 read_info->filename,input_filename);
312 status=SystemCommand(MagickFalse,read_info->verbose,command,exception) != 0 ?
313 MagickTrue : MagickFalse;
314 image=ReadImage(read_info,exception);
315 (void) RelinquishUniqueFileResource(read_info->filename);
316 (void) RelinquishUniqueFileResource(input_filename);
317 read_info=DestroyImageInfo(read_info);
318 if (image == (Image *) NULL)
319 ThrowReaderException(DelegateError,"XPSDelegateFailed");
320 if (LocaleCompare(image->magick,"BMP") == 0)
325 cmyk_image=ConsolidateCMYKImages(image,&image->exception);
326 if (cmyk_image != (Image *) NULL)
328 image=DestroyImageList(image);
334 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
336 next_image=SyncNextImageInList(image);
337 if (next_image != (Image *) NULL)
339 } while (next_image != (Image *) NULL);
340 return(GetFirstImageInList(image));
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % R e g i s t e r X P S I m a g e %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % RegisterXPSImage() adds attributes for the Microsoft XML Paper Specification
355 % format to the list of supported formats. The attributes include the image
356 % format tag, a method to read and/or write the format, whether the format
357 % supports the saving of more than one frame to the same file or blob,
358 % whether the format supports native in-memory I/O, and a brief
359 % description of the format.
361 % The format of the RegisterXPSImage method is:
363 % unsigned long RegisterXPSImage(void)
366 ModuleExport unsigned long RegisterXPSImage(void)
371 entry=SetMagickInfo("XPS");
372 entry->decoder=(DecodeImageHandler *) ReadXPSImage;
373 entry->adjoin=MagickFalse;
374 entry->blob_support=MagickFalse;
375 entry->seekable_stream=MagickTrue;
376 entry->thread_support=EncoderThreadSupport;
377 entry->description=ConstantString("Microsoft XML Paper Specification");
378 entry->module=ConstantString("XPS");
379 (void) RegisterMagickInfo(entry);
380 return(MagickImageCoderSignature);
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % U n r e g i s t e r X P S I m a g e %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % UnregisterXPSImage() removes format registrations made by the XPS module
395 % from the list of supported formats.
397 % The format of the UnregisterXPSImage method is:
399 % UnregisterXPSImage(void)
402 ModuleExport void UnregisterXPSImage(void)
404 (void) UnregisterMagickInfo("XPS");