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-2019 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 register ssize_t
117 i;
118
119 register Quantum
120 *q;
121
122 register 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 return((Image *) NULL);
178 /*
179 Get user defaults from X resource database.
180 */
181 client_name=GetClientName();
182 resource_database=XGetResourceDatabase(display,client_name);
183 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
184 /*
185 Allocate standard colormap.
186 */
187 map_info=XAllocStandardColormap();
188 visual_info=(XVisualInfo *) NULL;
189 if (map_info == (XStandardColormap *) NULL)
190 ThrowReaderException(ResourceLimitError,"UnableToCreateStandardColormap")
191 else
192 {
193 /*
194 Initialize visual info.
195 */
196 (void) CloneString(&resource_info.visual_type,"default");
197 visual_info=XBestVisualInfo(display,map_info,&resource_info);
198 map_info->colormap=(Colormap) NULL;
199 }
200 if ((map_info == (XStandardColormap *) NULL) ||
201 (visual_info == (XVisualInfo *) NULL))
202 {
203 image=DestroyImage(image);
204 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
205 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
206 return((Image *) NULL);
207 }
208 /*
209 Create a pixmap the appropriate size for the image.
210 */
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)/
215 DefaultResolution;
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))
220 {
221 image=DestroyImage(image);
222 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
223 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
224 return((Image *) NULL);
225 }
226 /*
227 Rasterize the file into the pixmap.
228 */
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,
232 &sans);
233 if (status != dps_status_success)
234 {
235 image=DestroyImage(image);
236 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
237 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
238 return((Image *) NULL);
239 }
240 /*
241 Initialize DPS X image.
242 */
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)
247 {
248 image=DestroyImage(image);
249 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
250 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
251 return((Image *) NULL);
252 }
253 /*
254 Get the colormap colors.
255 */
256 colors=(XColor *) AcquireQuantumMemory(visual_info->colormap_size,
257 sizeof(*colors));
258 if (colors == (XColor *) NULL)
259 {
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);
265 }
266 if ((visual_info->klass != DirectColor) && (visual_info->klass != TrueColor))
267 for (i=0; i < visual_info->colormap_size; i++)
268 {
269 colors[i].pixel=(size_t) i;
270 colors[i].pad=0;
271 }
272 else
273 {
274 size_t
275 blue,
276 blue_bit,
277 green,
278 green_bit,
279 red,
280 red_bit;
281
282 /*
283 DirectColor or TrueColor visual.
284 */
285 red=0;
286 green=0;
287 blue=0;
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++)
292 {
293 colors[i].pixel=red | green | blue;
294 colors[i].pad=0;
295 red+=red_bit;
296 if (red > visual_info->red_mask)
297 red=0;
298 green+=green_bit;
299 if (green > visual_info->green_mask)
300 green=0;
301 blue+=blue_bit;
302 if (blue > visual_info->blue_mask)
303 blue=0;
304 }
305 }
306 (void) XQueryColors(display,XDefaultColormap(display,visual_info->screen),
307 colors,visual_info->colormap_size);
308 /*
309 Convert X image to MIFF format.
310 */
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)
316 {
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));
323 }
324 status=SetImageExtent(image,image->columns,image->rows,exception);
325 if (status == MagickFalse)
326 {
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));
332 }
333 switch (image->storage_class)
334 {
335 case DirectClass:
336 default:
337 {
338 register size_t
339 color,
340 index;
341
342 size_t
343 blue_mask,
344 blue_shift,
345 green_mask,
346 green_shift,
347 red_mask,
348 red_shift;
349
350 /*
351 Determine shift and mask for red, green, and blue.
352 */
353 red_mask=visual_info->red_mask;
354 red_shift=0;
355 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
356 {
357 red_mask>>=1;
358 red_shift++;
359 }
360 green_mask=visual_info->green_mask;
361 green_shift=0;
362 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
363 {
364 green_mask>>=1;
365 green_shift++;
366 }
367 blue_mask=visual_info->blue_mask;
368 blue_shift=0;
369 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
370 {
371 blue_mask>>=1;
372 blue_shift++;
373 }
374 /*
375 Convert X image to DirectClass packets.
376 */
377 if ((visual_info->colormap_size > 0) &&
378 (visual_info->klass == DirectColor))
379 for (y=0; y < (ssize_t) image->rows; y++)
380 {
381 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
382 if (q == (Quantum *) NULL)
383 break;
384 for (x=0; x < (ssize_t) image->columns; x++)
385 {
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);
394 }
395 if (SyncAuthenticPixels(image,exception) == MagickFalse)
396 break;
397 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
398 break;
399 }
400 else
401 for (y=0; y < (ssize_t) image->rows; y++)
402 {
403 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
404 if (q == (Quantum *) NULL)
405 break;
406 for (x=0; x < (ssize_t) image->columns; x++)
407 {
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);
419 }
420 if (SyncAuthenticPixels(image,exception) == MagickFalse)
421 break;
422 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
423 break;
424 }
425 break;
426 }
427 case PseudoClass:
428 {
429 /*
430 Create colormap.
431 */
432 if (AcquireImageColormap(image,(size_t) visual_info->colormap_size,exception) == MagickFalse)
433 {
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);
440 }
441 for (i=0; i < (ssize_t) image->colors; i++)
442 {
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);
448 }
449 /*
450 Convert X image to PseudoClass packets.
451 */
452 for (y=0; y < (ssize_t) image->rows; y++)
453 {
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
456 break;
457 for (x=0; x < (ssize_t) image->columns; x++)
458 {
459 SetPixelIndex(image,(unsigned short) XGetPixel(dps_image,x,y),q);
460 q+=GetPixelChannels(image);
461 }
462 if (SyncAuthenticPixels(image,exception) == MagickFalse)
463 break;
464 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
465 break;
466 }
467 break;
468 }
469 }
470 colors=(XColor *) RelinquishMagickMemory(colors);
471 XDestroyImage(dps_image);
472 if (image->storage_class == PseudoClass)
473 (void) SyncImage(image,exception);
474 /*
475 Rasterize matte image.
476 */
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))
480 {
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)
485 {
486 XImage
487 *matte_image;
488
489 /*
490 Initialize image matte.
491 */
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)
496 {
497 image->storage_class=DirectClass;
498 image->alpha_trait=BlendPixelTrait;
499 for (y=0; y < (ssize_t) image->rows; y++)
500 {
501 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
502 if (q == (Quantum *) NULL)
503 break;
504 for (x=0; x < (ssize_t) image->columns; x++)
505 {
506 SetPixelAlpha(image,OpaqueAlpha,q);
507 if (XGetPixel(matte_image,x,y) == 0)
508 SetPixelAlpha(image,TransparentAlpha,q);
509 q+=GetPixelChannels(image);
510 }
511 if (SyncAuthenticPixels(image,exception) == MagickFalse)
512 break;
513 }
514 XDestroyImage(matte_image);
515 }
516 }
517 }
518 /*
519 Relinquish resources.
520 */
521 XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL,
522 (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL);
523 (void) CloseBlob(image);
524 return(GetFirstImageInList(image));
525 }
526 #endif
527
528 /*
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 % %
531 % %
532 % %
533 % R e g i s t e r D P S I m a g e %
534 % %
535 % %
536 % %
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 %
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.
545 %
546 % The format of the RegisterDPSImage method is:
547 %
548 % size_t RegisterDPSImage(void)
549 %
550 */
RegisterDPSImage(void)551 ModuleExport size_t RegisterDPSImage(void)
552 {
553 MagickInfo
554 *entry;
555
556 entry=AcquireMagickInfo("DPS","DPS","Display Postscript Interpreter");
557 #if defined(MAGICKCORE_DPS_DELEGATE)
558 entry->decoder=(DecodeImageHandler *) ReadDPSImage;
559 #endif
560 entry->flags^=CoderBlobSupportFlag;
561 (void) RegisterMagickInfo(entry);
562 return(MagickImageCoderSignature);
563 }
564
565 /*
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % %
568 % %
569 % %
570 % U n r e g i s t e r D P S I m a g e %
571 % %
572 % %
573 % %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 %
576 % UnregisterDPSImage() removes format registrations made by the
577 % DPS module from the list of supported formats.
578 %
579 % The format of the UnregisterDPSImage method is:
580 %
581 % UnregisterDPSImage(void)
582 %
583 */
UnregisterDPSImage(void)584 ModuleExport void UnregisterDPSImage(void)
585 {
586 (void) UnregisterMagickInfo("DPS");
587 }
588