1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            DDDD   PPPP   SSSSS                              %
7 %                            D   D  P   P  SS                                 %
8 %                            D   D  PPPP    SSS                               %
9 %                            D   D  P         SS                              %
10 %                            DDDD   P      SSSSS                              %
11 %                                                                             %
12 %                                                                             %
13 %            Read Postscript Using the Display Postscript System.             %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 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 %    https://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 
39 /*
40   Include declarations.
41 */
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>
66 #endif
67 
68 #if defined(MAGICKCORE_DPS_DELEGATE)
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e a d D P S I m a g e                                                   %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
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.
83 %
84 %  The format of the ReadDPSImage method is:
85 %
86 %      Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o image_info: the image info.
91 %
92 %    o exception: return any errors or warnings in this structure.
93 %
94 */
ReadDPSImage(const ImageInfo * image_info,ExceptionInfo * exception)95 static Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 {
97   const char
98     *client_name;
99 
100   Display
101     *display;
102 
103   float
104     pixels_per_point;
105 
106   Image
107     *image;
108 
109   int
110     sans,
111     status;
112 
113   Pixmap
114     pixmap;
115 
116   ssize_t
117     i;
118 
119   Quantum
120     *q;
121 
122   size_t
123     pixel;
124 
125   Screen
126     *screen;
127 
128   ssize_t
129     x,
130     y;
131 
132   XColor
133     *colors;
134 
135   XImage
136     *dps_image;
137 
138   XRectangle
139     page,
140     bits_per_pixel;
141 
142   XResourceInfo
143     resource_info;
144 
145   XrmDatabase
146     resource_database;
147 
148   XStandardColormap
149     *map_info;
150 
151   XVisualInfo
152     *visual_info;
153 
154   /*
155     Open X server connection.
156   */
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);
167   /*
168     Set our forgiving exception handler.
169   */
170   (void) XSetErrorHandler(XError);
171   /*
172     Open image file.
173   */
174   image=AcquireImage(image_info,exception);
175   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
176   if (status == MagickFalse)
177     {
178       image=DestroyImageList(image);
179       return((Image *) NULL);
180     }
181   /*
182     Get user defaults from X resource database.
183   */
184   client_name=GetClientName();
185   resource_database=XGetResourceDatabase(display,client_name);
186   XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
187   /*
188     Allocate standard colormap.
189   */
190   map_info=XAllocStandardColormap();
191   visual_info=(XVisualInfo *) NULL;
192   if (map_info == (XStandardColormap *) NULL)
193     ThrowReaderException(ResourceLimitError,"UnableToCreateStandardColormap")
194   else
195     {
196       /*
197         Initialize visual info.
198       */
199       (void) CloneString(&resource_info.visual_type,"default");
200       visual_info=XBestVisualInfo(display,map_info,&resource_info);
201       map_info->colormap=(Colormap) NULL;
202     }
203   if ((map_info == (XStandardColormap *) NULL) ||
204       (visual_info == (XVisualInfo *) NULL))
205     {
206       image=DestroyImage(image);
207       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
208         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
209       return((Image *) NULL);
210     }
211   /*
212     Create a pixmap the appropriate size for the image.
213   */
214   screen=ScreenOfDisplay(display,visual_info->screen);
215   pixels_per_point=XDPSPixelsPerPoint(screen);
216   if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
217     pixels_per_point=MagickMin(image->resolution.x,image->resolution.y)/
218       DefaultResolution;
219   status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
220     GetBlobFileHandle(image),visual_info->depth,pixels_per_point,&pixmap,
221     &bits_per_pixel,&page);
222   if ((status == dps_status_failure) || (status == dps_status_no_extension))
223     {
224       image=DestroyImage(image);
225       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
226         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
227       return((Image *) NULL);
228     }
229   /*
230     Rasterize the file into the pixmap.
231   */
232   status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
233     GetBlobFileHandle(image),(int) bits_per_pixel.height,visual_info->depth,
234     &page,-page.x,-page.y,pixels_per_point,MagickTrue,MagickFalse,MagickTrue,
235     &sans);
236   if (status != dps_status_success)
237     {
238       image=DestroyImage(image);
239       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
240         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
241       return((Image *) NULL);
242     }
243   /*
244     Initialize DPS X image.
245   */
246   dps_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
247     bits_per_pixel.height,AllPlanes,ZPixmap);
248   (void) XFreePixmap(display,pixmap);
249   if (dps_image == (XImage *) NULL)
250     {
251       image=DestroyImage(image);
252       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
253         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
254       return((Image *) NULL);
255     }
256   /*
257     Get the colormap colors.
258   */
259   colors=(XColor *) AcquireQuantumMemory(visual_info->colormap_size,
260     sizeof(*colors));
261   if (colors == (XColor *) NULL)
262     {
263       image=DestroyImage(image);
264       XDestroyImage(dps_image);
265       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
266         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
267       return((Image *) NULL);
268     }
269   if ((visual_info->klass != DirectColor) && (visual_info->klass != TrueColor))
270     for (i=0; i < visual_info->colormap_size; i++)
271     {
272       colors[i].pixel=(size_t) i;
273       colors[i].pad=0;
274     }
275   else
276     {
277       size_t
278         blue,
279         blue_bit,
280         green,
281         green_bit,
282         red,
283         red_bit;
284 
285       /*
286         DirectColor or TrueColor visual.
287       */
288       red=0;
289       green=0;
290       blue=0;
291       red_bit=visual_info->red_mask & (~(visual_info->red_mask)+1);
292       green_bit=visual_info->green_mask & (~(visual_info->green_mask)+1);
293       blue_bit=visual_info->blue_mask & (~(visual_info->blue_mask)+1);
294       for (i=0; i < visual_info->colormap_size; i++)
295       {
296         colors[i].pixel=red | green | blue;
297         colors[i].pad=0;
298         red+=red_bit;
299         if (red > visual_info->red_mask)
300           red=0;
301         green+=green_bit;
302         if (green > visual_info->green_mask)
303           green=0;
304         blue+=blue_bit;
305         if (blue > visual_info->blue_mask)
306           blue=0;
307       }
308     }
309   (void) XQueryColors(display,XDefaultColormap(display,visual_info->screen),
310     colors,visual_info->colormap_size);
311   /*
312     Convert X image to MIFF format.
313   */
314   if ((visual_info->klass != TrueColor) && (visual_info->klass != DirectColor))
315     image->storage_class=PseudoClass;
316   image->columns=(size_t) dps_image->width;
317   image->rows=(size_t) dps_image->height;
318   if (image_info->ping != MagickFalse)
319     {
320       (void) CloseBlob(image);
321       colors=(XColor *) RelinquishMagickMemory(colors);
322       XDestroyImage(dps_image);
323       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
324         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
325       return(GetFirstImageInList(image));
326     }
327   status=SetImageExtent(image,image->columns,image->rows,exception);
328   if (status == MagickFalse)
329     {
330       colors=(XColor *) RelinquishMagickMemory(colors);
331       XDestroyImage(dps_image);
332       XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
333         (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
334       return(DestroyImageList(image));
335     }
336   switch (image->storage_class)
337   {
338     case DirectClass:
339     default:
340     {
341       size_t
342         color,
343         index;
344 
345       size_t
346         blue_mask,
347         blue_shift,
348         green_mask,
349         green_shift,
350         red_mask,
351         red_shift;
352 
353       /*
354         Determine shift and mask for red, green, and blue.
355       */
356       red_mask=visual_info->red_mask;
357       red_shift=0;
358       while ((red_mask != 0) && ((red_mask & 0x01) == 0))
359       {
360         red_mask>>=1;
361         red_shift++;
362       }
363       green_mask=visual_info->green_mask;
364       green_shift=0;
365       while ((green_mask != 0) && ((green_mask & 0x01) == 0))
366       {
367         green_mask>>=1;
368         green_shift++;
369       }
370       blue_mask=visual_info->blue_mask;
371       blue_shift=0;
372       while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
373       {
374         blue_mask>>=1;
375         blue_shift++;
376       }
377       /*
378         Convert X image to DirectClass packets.
379       */
380       if ((visual_info->colormap_size > 0) &&
381           (visual_info->klass == DirectColor))
382         for (y=0; y < (ssize_t) image->rows; y++)
383         {
384           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
385           if (q == (Quantum *) NULL)
386             break;
387           for (x=0; x < (ssize_t) image->columns; x++)
388           {
389             pixel=XGetPixel(dps_image,x,y);
390             index=(pixel >> red_shift) & red_mask;
391             SetPixelRed(image,ScaleShortToQuantum(colors[index].red),q);
392             index=(pixel >> green_shift) & green_mask;
393             SetPixelGreen(image,ScaleShortToQuantum(colors[index].green),q);
394             index=(pixel >> blue_shift) & blue_mask;
395             SetPixelBlue(image,ScaleShortToQuantum(colors[index].blue),q);
396             q+=GetPixelChannels(image);
397           }
398           if (SyncAuthenticPixels(image,exception) == MagickFalse)
399             break;
400           if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
401             break;
402         }
403       else
404         for (y=0; y < (ssize_t) image->rows; y++)
405         {
406           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
407           if (q == (Quantum *) NULL)
408             break;
409           for (x=0; x < (ssize_t) image->columns; x++)
410           {
411             pixel=XGetPixel(dps_image,x,y);
412             color=(pixel >> red_shift) & red_mask;
413             color=(color*65535L)/red_mask;
414             SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q);
415             color=(pixel >> green_shift) & green_mask;
416             color=(color*65535L)/green_mask;
417             SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),q);
418             color=(pixel >> blue_shift) & blue_mask;
419             color=(color*65535L)/blue_mask;
420             SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q);
421             q+=GetPixelChannels(image);
422           }
423           if (SyncAuthenticPixels(image,exception) == MagickFalse)
424             break;
425           if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
426             break;
427         }
428       break;
429     }
430     case PseudoClass:
431     {
432       /*
433         Create colormap.
434       */
435       if (AcquireImageColormap(image,(size_t) visual_info->colormap_size,exception) == MagickFalse)
436         {
437           image=DestroyImage(image);
438           colors=(XColor *) RelinquishMagickMemory(colors);
439           XDestroyImage(dps_image);
440           XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
441             (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
442           return((Image *) NULL);
443         }
444       for (i=0; i < (ssize_t) image->colors; i++)
445       {
446         image->colormap[colors[i].pixel].red=ScaleShortToQuantum(colors[i].red);
447         image->colormap[colors[i].pixel].green=
448           ScaleShortToQuantum(colors[i].green);
449         image->colormap[colors[i].pixel].blue=
450           ScaleShortToQuantum(colors[i].blue);
451       }
452       /*
453         Convert X image to PseudoClass packets.
454       */
455       for (y=0; y < (ssize_t) image->rows; y++)
456       {
457         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
458         if (q == (Quantum *) NULL)
459           break;
460         for (x=0; x < (ssize_t) image->columns; x++)
461         {
462           SetPixelIndex(image,(unsigned short) XGetPixel(dps_image,x,y),q);
463           q+=GetPixelChannels(image);
464         }
465         if (SyncAuthenticPixels(image,exception) == MagickFalse)
466           break;
467         if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
468           break;
469       }
470       break;
471     }
472   }
473   colors=(XColor *) RelinquishMagickMemory(colors);
474   XDestroyImage(dps_image);
475   if (image->storage_class == PseudoClass)
476     (void) SyncImage(image,exception);
477   /*
478     Rasterize matte image.
479   */
480   status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen,
481     GetBlobFileHandle(image),1,pixels_per_point,&pixmap,&bits_per_pixel,&page);
482   if ((status != dps_status_failure) && (status != dps_status_no_extension))
483     {
484       status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap,
485         GetBlobFileHandle(image),(int) bits_per_pixel.height,1,&page,-page.x,
486         -page.y,pixels_per_point,MagickTrue,MagickTrue,MagickTrue,&sans);
487       if (status == dps_status_success)
488         {
489           XImage
490             *matte_image;
491 
492           /*
493             Initialize image matte.
494           */
495           matte_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width,
496             bits_per_pixel.height,AllPlanes,ZPixmap);
497           (void) XFreePixmap(display,pixmap);
498           if (matte_image != (XImage *) NULL)
499             {
500               image->storage_class=DirectClass;
501               image->alpha_trait=BlendPixelTrait;
502               for (y=0; y < (ssize_t) image->rows; y++)
503               {
504                 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
505                 if (q == (Quantum *) NULL)
506                   break;
507                 for (x=0; x < (ssize_t) image->columns; x++)
508                 {
509                   SetPixelAlpha(image,OpaqueAlpha,q);
510                   if (XGetPixel(matte_image,x,y) == 0)
511                     SetPixelAlpha(image,TransparentAlpha,q);
512                   q+=GetPixelChannels(image);
513                 }
514                 if (SyncAuthenticPixels(image,exception) == MagickFalse)
515                   break;
516               }
517               XDestroyImage(matte_image);
518             }
519         }
520     }
521   /*
522     Relinquish resources.
523   */
524   XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
525     (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
526   (void) CloseBlob(image);
527   return(GetFirstImageInList(image));
528 }
529 #endif
530 
531 /*
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 %                                                                             %
534 %                                                                             %
535 %                                                                             %
536 %   R e g i s t e r D P S I m a g e                                           %
537 %                                                                             %
538 %                                                                             %
539 %                                                                             %
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 %
542 %  RegisterDPSImage() adds attributes for the Display Postscript image
543 %  format to the list of supported formats.  The attributes include the image
544 %  format tag, a method to read and/or write the format, whether the format
545 %  supports the saving of more than one frame to the same file or blob,
546 %  whether the format supports native in-memory I/O, and a brief
547 %  description of the format.
548 %
549 %  The format of the RegisterDPSImage method is:
550 %
551 %      size_t RegisterDPSImage(void)
552 %
553 */
RegisterDPSImage(void)554 ModuleExport size_t RegisterDPSImage(void)
555 {
556   MagickInfo
557     *entry;
558 
559   entry=AcquireMagickInfo("DPS","DPS","Display Postscript Interpreter");
560 #if defined(MAGICKCORE_DPS_DELEGATE)
561   entry->decoder=(DecodeImageHandler *) ReadDPSImage;
562 #endif
563   entry->flags^=CoderBlobSupportFlag;
564   (void) RegisterMagickInfo(entry);
565   return(MagickImageCoderSignature);
566 }
567 
568 /*
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 %                                                                             %
571 %                                                                             %
572 %                                                                             %
573 %   U n r e g i s t e r D P S I m a g e                                       %
574 %                                                                             %
575 %                                                                             %
576 %                                                                             %
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 %
579 %  UnregisterDPSImage() removes format registrations made by the
580 %  DPS module from the list of supported formats.
581 %
582 %  The format of the UnregisterDPSImage method is:
583 %
584 %      UnregisterDPSImage(void)
585 %
586 */
UnregisterDPSImage(void)587 ModuleExport void UnregisterDPSImage(void)
588 {
589   (void) UnregisterMagickInfo("DPS");
590 }
591