2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Postscript Using the Display Postscript System. %
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/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/client.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/image.h"
49 #include "magick/image-private.h"
50 #include "magick/list.h"
51 #include "magick/magick.h"
52 #include "magick/memory_.h"
53 #include "magick/monitor.h"
54 #include "magick/monitor-private.h"
55 #include "magick/quantum-private.h"
56 #include "magick/static.h"
57 #include "magick/string_.h"
58 #include "magick/module.h"
59 #include "magick/utility.h"
60 #include "magick/xwindow-private.h"
61 #if defined(MAGICKCORE_DPS_DELEGATE)
62 #include <DPS/dpsXclient.h>
63 #include <DPS/dpsXpreview.h>
66 #if defined(MAGICKCORE_DPS_DELEGATE)
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d D P S I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadDPSImage() reads a Adobe Postscript image file and returns it. It
79 % allocates the memory necessary for the new Image structure and returns a
80 % pointer to the new image.
82 % The format of the ReadDPSImage method is:
84 % Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
86 % A description of each parameter follows:
88 % o image_info: the image info.
90 % o exception: return any errors or warnings in this structure.
94 static inline double MagickMin(const double x,const double y)
101 static Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
135 register unsigned long
164 Open X server connection.
166 assert(image_info != (const ImageInfo *) NULL);
167 assert(image_info->signature == MagickSignature);
168 if (image_info->debug != MagickFalse)
169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
170 image_info->filename);
171 assert(exception != (ExceptionInfo *) NULL);
172 assert(exception->signature == MagickSignature);
173 display=XOpenDisplay(image_info->server_name);
174 if (display == (Display *) NULL)
175 return((Image *) NULL);
177 Set our forgiving exception handler.
179 (void) XSetErrorHandler(XError);
183 image=AcquireImage(image_info);
184 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
185 if (status == MagickFalse)
186 return((Image *) NULL);
188 Get user defaults from X resource database.
190 client_name=GetClientName();
191 resource_database=XGetResourceDatabase(display,client_name);
192 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
194 Allocate standard colormap.
196 map_info=XAllocStandardColormap();
197 visual_info=(XVisualInfo *) NULL;
198 if (map_info == (XStandardColormap *) NULL)
199 ThrowReaderException(ResourceLimitError,"UnableToCreateStandardColormap")
203 Initialize visual info.
205 (void) CloneString(&resource_info.visual_type,"default");
206 visual_info=XBestVisualInfo(display,map_info,&resource_info);
207 map_info->colormap=(Colormap) NULL;
209 if ((map_info == (XStandardColormap *) NULL) ||
210 (visual_info == (XVisualInfo *) NULL))
212 image=DestroyImage(image);
213 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
214 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
215 return((Image *) NULL);
218 Create a pixmap the appropriate size for the image.
220 screen=ScreenOfDisplay(display,visual_info->screen);
221 pixels_per_point=XDPSPixelsPerPoint(screen);
222 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
223 pixels_per_point=MagickMin(image->x_resolution,image->y_resolution)/
225 status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
226 GetBlobFileHandle(image),visual_info->depth,pixels_per_point,&pixmap,
227 &bits_per_pixel,&page);
228 if ((status == dps_status_failure) || (status == dps_status_no_extension))
230 image=DestroyImage(image);
231 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
232 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
233 return((Image *) NULL);
236 Rasterize the file into the pixmap.
238 status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
239 GetBlobFileHandle(image),(int) bits_per_pixel.height,visual_info->depth,
240 &page,-page.x,-page.y,pixels_per_point,MagickTrue,MagickFalse,MagickTrue,
242 if (status != dps_status_success)
244 image=DestroyImage(image);
245 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
246 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
247 return((Image *) NULL);
250 Initialize DPS X image.
252 dps_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
253 bits_per_pixel.height,AllPlanes,ZPixmap);
254 (void) XFreePixmap(display,pixmap);
255 if (dps_image == (XImage *) NULL)
257 image=DestroyImage(image);
258 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
259 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
260 return((Image *) NULL);
263 Get the colormap colors.
265 colors=(XColor *) AcquireQuantumMemory(visual_info->colormap_size,
267 if (colors == (XColor *) NULL)
269 image=DestroyImage(image);
270 XDestroyImage(dps_image);
271 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
272 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
273 return((Image *) NULL);
275 if ((visual_info->klass != DirectColor) && (visual_info->klass != TrueColor))
276 for (i=0; i < visual_info->colormap_size; i++)
278 colors[i].pixel=(unsigned long) i;
292 DirectColor or TrueColor visual.
297 red_bit=visual_info->red_mask & (~(visual_info->red_mask)+1);
298 green_bit=visual_info->green_mask & (~(visual_info->green_mask)+1);
299 blue_bit=visual_info->blue_mask & (~(visual_info->blue_mask)+1);
300 for (i=0; i < visual_info->colormap_size; i++)
302 colors[i].pixel=red | green | blue;
305 if (red > visual_info->red_mask)
308 if (green > visual_info->green_mask)
311 if (blue > visual_info->blue_mask)
315 (void) XQueryColors(display,XDefaultColormap(display,visual_info->screen),
316 colors,visual_info->colormap_size);
318 Convert X image to MIFF format.
320 if ((visual_info->klass != TrueColor) && (visual_info->klass != DirectColor))
321 image->storage_class=PseudoClass;
322 image->columns=(unsigned long) dps_image->width;
323 image->rows=(unsigned long) dps_image->height;
324 if (image_info->ping != MagickFalse)
326 (void) CloseBlob(image);
327 return(GetFirstImageInList(image));
329 switch (image->storage_class)
334 register unsigned long
347 Determine shift and mask for red, green, and blue.
349 red_mask=visual_info->red_mask;
351 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
356 green_mask=visual_info->green_mask;
358 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
363 blue_mask=visual_info->blue_mask;
365 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
371 Convert X image to DirectClass packets.
373 if ((visual_info->colormap_size > 0) &&
374 (visual_info->klass == DirectColor))
375 for (y=0; y < (long) image->rows; y++)
377 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
378 if (q == (PixelPacket *) NULL)
380 for (x=0; x < (long) image->columns; x++)
382 pixel=XGetPixel(dps_image,x,y);
383 index=(pixel >> red_shift) & red_mask;
384 q->red=ScaleShortToQuantum(colors[index].red);
385 index=(pixel >> green_shift) & green_mask;
386 q->green=ScaleShortToQuantum(colors[index].green);
387 index=(pixel >> blue_shift) & blue_mask;
388 q->blue=ScaleShortToQuantum(colors[index].blue);
391 if (SyncAuthenticPixels(image,exception) == MagickFalse)
393 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
397 for (y=0; y < (long) image->rows; y++)
399 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
400 if (q == (PixelPacket *) NULL)
402 for (x=0; x < (long) image->columns; x++)
404 pixel=XGetPixel(dps_image,x,y);
405 color=(pixel >> red_shift) & red_mask;
406 color=(color*65535L)/red_mask;
407 q->red=ScaleShortToQuantum((unsigned short) color);
408 color=(pixel >> green_shift) & green_mask;
409 color=(color*65535L)/green_mask;
410 q->green=ScaleShortToQuantum((unsigned short) color);
411 color=(pixel >> blue_shift) & blue_mask;
412 color=(color*65535L)/blue_mask;
413 q->blue=ScaleShortToQuantum((unsigned short) color);
416 if (SyncAuthenticPixels(image,exception) == MagickFalse)
418 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
428 if (AcquireImageColormap(image,(unsigned long) visual_info->colormap_size) == MagickFalse)
430 image=DestroyImage(image);
431 colors=(XColor *) RelinquishMagickMemory(colors);
432 XDestroyImage(dps_image);
433 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
434 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
435 return((Image *) NULL);
437 for (i=0; i < (long) image->colors; i++)
439 image->colormap[colors[i].pixel].red=ScaleShortToQuantum(colors[i].red);
440 image->colormap[colors[i].pixel].green=
441 ScaleShortToQuantum(colors[i].green);
442 image->colormap[colors[i].pixel].blue=
443 ScaleShortToQuantum(colors[i].blue);
446 Convert X image to PseudoClass packets.
448 for (y=0; y < (long) image->rows; y++)
450 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
451 if (q == (PixelPacket *) NULL)
453 indexes=GetAuthenticIndexQueue(image);
454 for (x=0; x < (long) image->columns; x++)
455 indexes[x]=(unsigned short) XGetPixel(dps_image,x,y);
456 if (SyncAuthenticPixels(image,exception) == MagickFalse)
458 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
464 colors=(XColor *) RelinquishMagickMemory(colors);
465 XDestroyImage(dps_image);
466 if (image->storage_class == PseudoClass)
467 (void) SyncImage(image);
469 Rasterize matte image.
471 status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
472 GetBlobFileHandle(image),1,pixels_per_point,&pixmap,&bits_per_pixel,&page);
473 if ((status != dps_status_failure) && (status != dps_status_no_extension))
475 status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
476 GetBlobFileHandle(image),(int) bits_per_pixel.height,1,&page,-page.x,
477 -page.y,pixels_per_point,MagickTrue,MagickTrue,MagickTrue,&sans);
478 if (status == dps_status_success)
484 Initialize image matte.
486 matte_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
487 bits_per_pixel.height,AllPlanes,ZPixmap);
488 (void) XFreePixmap(display,pixmap);
489 if (matte_image != (XImage *) NULL)
491 image->storage_class=DirectClass;
492 image->matte=MagickTrue;
493 for (y=0; y < (long) image->rows; y++)
495 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
496 if (q == (PixelPacket *) NULL)
498 for (x=0; x < (long) image->columns; x++)
500 SetOpacityPixelComponent(q,OpaqueOpacity);
501 if (XGetPixel(matte_image,x,y) == 0)
502 q->opacity=TransparentOpacity;
505 if (SyncAuthenticPixels(image,exception) == MagickFalse)
508 XDestroyImage(matte_image);
513 Relinquish resources.
515 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
516 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
517 (void) CloseBlob(image);
518 return(GetFirstImageInList(image));
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527 % R e g i s t e r D P S I m a g e %
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 % RegisterDPSImage() adds attributes for the Display Postscript image
534 % format to the list of supported formats. The attributes include the image
535 % format tag, a method to read and/or write the format, whether the format
536 % supports the saving of more than one frame to the same file or blob,
537 % whether the format supports native in-memory I/O, and a brief
538 % description of the format.
540 % The format of the RegisterDPSImage method is:
542 % unsigned long RegisterDPSImage(void)
545 ModuleExport unsigned long RegisterDPSImage(void)
550 entry=SetMagickInfo("DPS");
551 #if defined(MAGICKCORE_DPS_DELEGATE)
552 entry->decoder=(DecodeImageHandler *) ReadDPSImage;
554 entry->blob_support=MagickFalse;
555 entry->description=ConstantString("Display Postscript Interpreter");
556 entry->module=ConstantString("DPS");
557 (void) RegisterMagickInfo(entry);
558 return(MagickImageCoderSignature);
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % U n r e g i s t e r D P S I m a g e %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % UnregisterDPSImage() removes format registrations made by the
573 % DPS module from the list of supported formats.
575 % The format of the UnregisterDPSImage method is:
577 % UnregisterDPSImage(void)
580 ModuleExport void UnregisterDPSImage(void)
582 (void) UnregisterMagickInfo("DPS");