2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Postscript Using the Display Postscript System. %
20 % Copyright 1999-2017 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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/client.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/xwindow-private.h"
63 #if defined(MAGICKCORE_DPS_DELEGATE)
64 #include <DPS/dpsXclient.h>
65 #include <DPS/dpsXpreview.h>
68 #if defined(MAGICKCORE_DPS_DELEGATE)
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d D P S I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % ReadDPSImage() reads a Adobe Postscript image file and returns it. It
81 % allocates the memory necessary for the new Image structure and returns a
82 % pointer to the new image.
84 % The format of the ReadDPSImage method is:
86 % Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
88 % A description of each parameter follows:
90 % o image_info: the image info.
92 % o exception: return any errors or warnings in this structure.
95 static Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
155 Open X server connection.
157 assert(image_info != (const ImageInfo *) NULL);
158 assert(image_info->signature == MagickCoreSignature);
159 if (image_info->debug != MagickFalse)
160 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
161 image_info->filename);
162 assert(exception != (ExceptionInfo *) NULL);
163 assert(exception->signature == MagickCoreSignature);
164 display=XOpenDisplay(image_info->server_name);
165 if (display == (Display *) NULL)
166 return((Image *) NULL);
168 Set our forgiving exception handler.
170 (void) XSetErrorHandler(XError);
174 image=AcquireImage(image_info,exception);
175 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
176 if (status == MagickFalse)
177 return((Image *) NULL);
179 Get user defaults from X resource database.
181 client_name=GetClientName();
182 resource_database=XGetResourceDatabase(display,client_name);
183 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
185 Allocate standard colormap.
187 map_info=XAllocStandardColormap();
188 visual_info=(XVisualInfo *) NULL;
189 if (map_info == (XStandardColormap *) NULL)
190 ThrowReaderException(ResourceLimitError,"UnableToCreateStandardColormap")
194 Initialize visual info.
196 (void) CloneString(&resource_info.visual_type,"default");
197 visual_info=XBestVisualInfo(display,map_info,&resource_info);
198 map_info->colormap=(Colormap) NULL;
200 if ((map_info == (XStandardColormap *) NULL) ||
201 (visual_info == (XVisualInfo *) NULL))
203 image=DestroyImage(image);
204 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
205 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
206 return((Image *) NULL);
209 Create a pixmap the appropriate size for the image.
211 screen=ScreenOfDisplay(display,visual_info->screen);
212 pixels_per_point=XDPSPixelsPerPoint(screen);
213 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
214 pixels_per_point=MagickMin(image->resolution.x,image->resolution.y)/
216 status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
217 GetBlobFileHandle(image),visual_info->depth,pixels_per_point,&pixmap,
218 &bits_per_pixel,&page);
219 if ((status == dps_status_failure) || (status == dps_status_no_extension))
221 image=DestroyImage(image);
222 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
223 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
224 return((Image *) NULL);
227 Rasterize the file into the pixmap.
229 status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
230 GetBlobFileHandle(image),(int) bits_per_pixel.height,visual_info->depth,
231 &page,-page.x,-page.y,pixels_per_point,MagickTrue,MagickFalse,MagickTrue,
233 if (status != dps_status_success)
235 image=DestroyImage(image);
236 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
237 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
238 return((Image *) NULL);
241 Initialize DPS X image.
243 dps_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
244 bits_per_pixel.height,AllPlanes,ZPixmap);
245 (void) XFreePixmap(display,pixmap);
246 if (dps_image == (XImage *) NULL)
248 image=DestroyImage(image);
249 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
250 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
251 return((Image *) NULL);
254 Get the colormap colors.
256 colors=(XColor *) AcquireQuantumMemory(visual_info->colormap_size,
258 if (colors == (XColor *) NULL)
260 image=DestroyImage(image);
261 XDestroyImage(dps_image);
262 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
263 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
264 return((Image *) NULL);
266 if ((visual_info->klass != DirectColor) && (visual_info->klass != TrueColor))
267 for (i=0; i < visual_info->colormap_size; i++)
269 colors[i].pixel=(size_t) i;
283 DirectColor or TrueColor visual.
288 red_bit=visual_info->red_mask & (~(visual_info->red_mask)+1);
289 green_bit=visual_info->green_mask & (~(visual_info->green_mask)+1);
290 blue_bit=visual_info->blue_mask & (~(visual_info->blue_mask)+1);
291 for (i=0; i < visual_info->colormap_size; i++)
293 colors[i].pixel=red | green | blue;
296 if (red > visual_info->red_mask)
299 if (green > visual_info->green_mask)
302 if (blue > visual_info->blue_mask)
306 (void) XQueryColors(display,XDefaultColormap(display,visual_info->screen),
307 colors,visual_info->colormap_size);
309 Convert X image to MIFF format.
311 if ((visual_info->klass != TrueColor) && (visual_info->klass != DirectColor))
312 image->storage_class=PseudoClass;
313 image->columns=(size_t) dps_image->width;
314 image->rows=(size_t) dps_image->height;
315 if (image_info->ping != MagickFalse)
317 (void) CloseBlob(image);
318 return(GetFirstImageInList(image));
320 status=SetImageExtent(image,image->columns,image->rows,exception);
321 if (status == MagickFalse)
322 return(DestroyImageList(image));
323 switch (image->storage_class)
341 Determine shift and mask for red, green, and blue.
343 red_mask=visual_info->red_mask;
345 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
350 green_mask=visual_info->green_mask;
352 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
357 blue_mask=visual_info->blue_mask;
359 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
365 Convert X image to DirectClass packets.
367 if ((visual_info->colormap_size > 0) &&
368 (visual_info->klass == DirectColor))
369 for (y=0; y < (ssize_t) image->rows; y++)
371 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
372 if (q == (Quantum *) NULL)
374 for (x=0; x < (ssize_t) image->columns; x++)
376 pixel=XGetPixel(dps_image,x,y);
377 index=(pixel >> red_shift) & red_mask;
378 SetPixelRed(image,ScaleShortToQuantum(colors[index].red),q);
379 index=(pixel >> green_shift) & green_mask;
380 SetPixelGreen(image,ScaleShortToQuantum(colors[index].green),q);
381 index=(pixel >> blue_shift) & blue_mask;
382 SetPixelBlue(image,ScaleShortToQuantum(colors[index].blue),q);
383 q+=GetPixelChannels(image);
385 if (SyncAuthenticPixels(image,exception) == MagickFalse)
387 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
391 for (y=0; y < (ssize_t) image->rows; y++)
393 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
394 if (q == (Quantum *) NULL)
396 for (x=0; x < (ssize_t) image->columns; x++)
398 pixel=XGetPixel(dps_image,x,y);
399 color=(pixel >> red_shift) & red_mask;
400 color=(color*65535L)/red_mask;
401 SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q);
402 color=(pixel >> green_shift) & green_mask;
403 color=(color*65535L)/green_mask;
404 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),q);
405 color=(pixel >> blue_shift) & blue_mask;
406 color=(color*65535L)/blue_mask;
407 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q);
408 q+=GetPixelChannels(image);
410 if (SyncAuthenticPixels(image,exception) == MagickFalse)
412 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
422 if (AcquireImageColormap(image,(size_t) visual_info->colormap_size,exception) == MagickFalse)
424 image=DestroyImage(image);
425 colors=(XColor *) RelinquishMagickMemory(colors);
426 XDestroyImage(dps_image);
427 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
428 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
429 return((Image *) NULL);
431 for (i=0; i < (ssize_t) image->colors; i++)
433 image->colormap[colors[i].pixel].red=ScaleShortToQuantum(colors[i].red);
434 image->colormap[colors[i].pixel].green=
435 ScaleShortToQuantum(colors[i].green);
436 image->colormap[colors[i].pixel].blue=
437 ScaleShortToQuantum(colors[i].blue);
440 Convert X image to PseudoClass packets.
442 for (y=0; y < (ssize_t) image->rows; y++)
444 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
445 if (q == (Quantum *) NULL)
447 for (x=0; x < (ssize_t) image->columns; x++)
449 SetPixelIndex(image,(unsigned short) XGetPixel(dps_image,x,y),q);
450 q+=GetPixelChannels(image);
452 if (SyncAuthenticPixels(image,exception) == MagickFalse)
454 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
460 colors=(XColor *) RelinquishMagickMemory(colors);
461 XDestroyImage(dps_image);
462 if (image->storage_class == PseudoClass)
463 (void) SyncImage(image,exception);
465 Rasterize matte image.
467 status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
468 GetBlobFileHandle(image),1,pixels_per_point,&pixmap,&bits_per_pixel,&page);
469 if ((status != dps_status_failure) && (status != dps_status_no_extension))
471 status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
472 GetBlobFileHandle(image),(int) bits_per_pixel.height,1,&page,-page.x,
473 -page.y,pixels_per_point,MagickTrue,MagickTrue,MagickTrue,&sans);
474 if (status == dps_status_success)
480 Initialize image matte.
482 matte_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
483 bits_per_pixel.height,AllPlanes,ZPixmap);
484 (void) XFreePixmap(display,pixmap);
485 if (matte_image != (XImage *) NULL)
487 image->storage_class=DirectClass;
488 image->alpha_trait=BlendPixelTrait;
489 for (y=0; y < (ssize_t) image->rows; y++)
491 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
492 if (q == (Quantum *) NULL)
494 for (x=0; x < (ssize_t) image->columns; x++)
496 SetPixelAlpha(image,OpaqueAlpha,q);
497 if (XGetPixel(matte_image,x,y) == 0)
498 SetPixelAlpha(image,TransparentAlpha,q);
499 q+=GetPixelChannels(image);
501 if (SyncAuthenticPixels(image,exception) == MagickFalse)
504 XDestroyImage(matte_image);
509 Relinquish resources.
511 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
512 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
513 (void) CloseBlob(image);
514 return(GetFirstImageInList(image));
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 % R e g i s t e r D P S I m a g e %
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % RegisterDPSImage() adds attributes for the Display Postscript image
530 % format to the list of supported formats. The attributes include the image
531 % format tag, a method to read and/or write the format, whether the format
532 % supports the saving of more than one frame to the same file or blob,
533 % whether the format supports native in-memory I/O, and a brief
534 % description of the format.
536 % The format of the RegisterDPSImage method is:
538 % size_t RegisterDPSImage(void)
541 ModuleExport size_t RegisterDPSImage(void)
546 entry=AcquireMagickInfo("DPS","DPS","Display Postscript Interpreter");
547 #if defined(MAGICKCORE_DPS_DELEGATE)
548 entry->decoder=(DecodeImageHandler *) ReadDPSImage;
550 entry->flags^=CoderBlobSupportFlag;
551 (void) RegisterMagickInfo(entry);
552 return(MagickImageCoderSignature);
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % U n r e g i s t e r D P S I m a g e %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % UnregisterDPSImage() removes format registrations made by the
567 % DPS module from the list of supported formats.
569 % The format of the UnregisterDPSImage method is:
571 % UnregisterDPSImage(void)
574 ModuleExport void UnregisterDPSImage(void)
576 (void) UnregisterMagickInfo("DPS");