2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Postscript Using the Display Postscript System. %
20 % Copyright 1999-2018 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 % https://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 colors=(XColor *) RelinquishMagickMemory(colors);
319 XDestroyImage(dps_image);
320 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
321 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
322 return(GetFirstImageInList(image));
324 status=SetImageExtent(image,image->columns,image->rows,exception);
325 if (status == MagickFalse)
327 colors=(XColor *) RelinquishMagickMemory(colors);
328 XDestroyImage(dps_image);
329 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
330 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
331 return(DestroyImageList(image));
333 switch (image->storage_class)
351 Determine shift and mask for red, green, and blue.
353 red_mask=visual_info->red_mask;
355 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
360 green_mask=visual_info->green_mask;
362 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
367 blue_mask=visual_info->blue_mask;
369 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
375 Convert X image to DirectClass packets.
377 if ((visual_info->colormap_size > 0) &&
378 (visual_info->klass == DirectColor))
379 for (y=0; y < (ssize_t) image->rows; y++)
381 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
382 if (q == (Quantum *) NULL)
384 for (x=0; x < (ssize_t) image->columns; x++)
386 pixel=XGetPixel(dps_image,x,y);
387 index=(pixel >> red_shift) & red_mask;
388 SetPixelRed(image,ScaleShortToQuantum(colors[index].red),q);
389 index=(pixel >> green_shift) & green_mask;
390 SetPixelGreen(image,ScaleShortToQuantum(colors[index].green),q);
391 index=(pixel >> blue_shift) & blue_mask;
392 SetPixelBlue(image,ScaleShortToQuantum(colors[index].blue),q);
393 q+=GetPixelChannels(image);
395 if (SyncAuthenticPixels(image,exception) == MagickFalse)
397 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
401 for (y=0; y < (ssize_t) image->rows; y++)
403 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
404 if (q == (Quantum *) NULL)
406 for (x=0; x < (ssize_t) image->columns; x++)
408 pixel=XGetPixel(dps_image,x,y);
409 color=(pixel >> red_shift) & red_mask;
410 color=(color*65535L)/red_mask;
411 SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q);
412 color=(pixel >> green_shift) & green_mask;
413 color=(color*65535L)/green_mask;
414 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),q);
415 color=(pixel >> blue_shift) & blue_mask;
416 color=(color*65535L)/blue_mask;
417 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q);
418 q+=GetPixelChannels(image);
420 if (SyncAuthenticPixels(image,exception) == MagickFalse)
422 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
432 if (AcquireImageColormap(image,(size_t) visual_info->colormap_size,exception) == MagickFalse)
434 image=DestroyImage(image);
435 colors=(XColor *) RelinquishMagickMemory(colors);
436 XDestroyImage(dps_image);
437 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
438 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
439 return((Image *) NULL);
441 for (i=0; i < (ssize_t) image->colors; i++)
443 image->colormap[colors[i].pixel].red=ScaleShortToQuantum(colors[i].red);
444 image->colormap[colors[i].pixel].green=
445 ScaleShortToQuantum(colors[i].green);
446 image->colormap[colors[i].pixel].blue=
447 ScaleShortToQuantum(colors[i].blue);
450 Convert X image to PseudoClass packets.
452 for (y=0; y < (ssize_t) image->rows; y++)
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
457 for (x=0; x < (ssize_t) image->columns; x++)
459 SetPixelIndex(image,(unsigned short) XGetPixel(dps_image,x,y),q);
460 q+=GetPixelChannels(image);
462 if (SyncAuthenticPixels(image,exception) == MagickFalse)
464 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
470 colors=(XColor *) RelinquishMagickMemory(colors);
471 XDestroyImage(dps_image);
472 if (image->storage_class == PseudoClass)
473 (void) SyncImage(image,exception);
475 Rasterize matte image.
477 status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
478 GetBlobFileHandle(image),1,pixels_per_point,&pixmap,&bits_per_pixel,&page);
479 if ((status != dps_status_failure) && (status != dps_status_no_extension))
481 status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
482 GetBlobFileHandle(image),(int) bits_per_pixel.height,1,&page,-page.x,
483 -page.y,pixels_per_point,MagickTrue,MagickTrue,MagickTrue,&sans);
484 if (status == dps_status_success)
490 Initialize image matte.
492 matte_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
493 bits_per_pixel.height,AllPlanes,ZPixmap);
494 (void) XFreePixmap(display,pixmap);
495 if (matte_image != (XImage *) NULL)
497 image->storage_class=DirectClass;
498 image->alpha_trait=BlendPixelTrait;
499 for (y=0; y < (ssize_t) image->rows; y++)
501 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
502 if (q == (Quantum *) NULL)
504 for (x=0; x < (ssize_t) image->columns; x++)
506 SetPixelAlpha(image,OpaqueAlpha,q);
507 if (XGetPixel(matte_image,x,y) == 0)
508 SetPixelAlpha(image,TransparentAlpha,q);
509 q+=GetPixelChannels(image);
511 if (SyncAuthenticPixels(image,exception) == MagickFalse)
514 XDestroyImage(matte_image);
519 Relinquish resources.
521 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
522 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
523 (void) CloseBlob(image);
524 return(GetFirstImageInList(image));
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 % R e g i s t e r D P S I m a g e %
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % RegisterDPSImage() adds attributes for the Display Postscript image
540 % format to the list of supported formats. The attributes include the image
541 % format tag, a method to read and/or write the format, whether the format
542 % supports the saving of more than one frame to the same file or blob,
543 % whether the format supports native in-memory I/O, and a brief
544 % description of the format.
546 % The format of the RegisterDPSImage method is:
548 % size_t RegisterDPSImage(void)
551 ModuleExport size_t RegisterDPSImage(void)
556 entry=AcquireMagickInfo("DPS","DPS","Display Postscript Interpreter");
557 #if defined(MAGICKCORE_DPS_DELEGATE)
558 entry->decoder=(DecodeImageHandler *) ReadDPSImage;
560 entry->flags^=CoderBlobSupportFlag;
561 (void) RegisterMagickInfo(entry);
562 return(MagickImageCoderSignature);
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % U n r e g i s t e r D P S I m a g e %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % UnregisterDPSImage() removes format registrations made by the
577 % DPS module from the list of supported formats.
579 % The format of the UnregisterDPSImage method is:
581 % UnregisterDPSImage(void)
584 ModuleExport void UnregisterDPSImage(void)
586 (void) UnregisterMagickInfo("DPS");