1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            X   X  W   W  DDDD                               %
7 %                             X X   W   W  D   D                              %
8 %                              X    W   W  D   D                              %
9 %                             X X   W W W  D   D                              %
10 %                            X   X   W W   DDDD                               %
11 %                                                                             %
12 %                                                                             %
13 %                Read/Write X Windows System Window Dump Format               %
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/cache.h"
46 #include "MagickCore/color-private.h"
47 #include "MagickCore/colormap.h"
48 #include "MagickCore/colormap-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/property.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
66 #if defined(MAGICKCORE_X11_DELEGATE)
67 #include "MagickCore/xwindow-private.h"
68 #if !defined(vms)
69 #include <X11/XWDFile.h>
70 #else
71 #include "XWDFile.h"
72 #endif
73 #endif
74 
75 /*
76   Forward declarations.
77 */
78 #if defined(MAGICKCORE_X11_DELEGATE)
79 static MagickBooleanType
80   WriteXWDImage(const ImageInfo *,Image *,ExceptionInfo *);
81 #endif
82 
83 /*
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %                                                                             %
86 %                                                                             %
87 %                                                                             %
88 %   I s X W D                                                                 %
89 %                                                                             %
90 %                                                                             %
91 %                                                                             %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 %
94 %  IsXWD() returns MagickTrue if the image format type, identified by the
95 %  magick string, is XWD.
96 %
97 %  The format of the IsXWD method is:
98 %
99 %      MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
100 %
101 %  A description of each parameter follows:
102 %
103 %    o magick: compare image format pattern against these bytes.
104 %
105 %    o length: Specifies the length of the magick string.
106 %
107 */
IsXWD(const unsigned char * magick,const size_t length)108 static MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
109 {
110   if (length < 8)
111     return(MagickFalse);
112   if (memcmp(magick+1,"\000\000",2) == 0)
113     {
114       if (memcmp(magick+4,"\007\000\000",3) == 0)
115         return(MagickTrue);
116       if (memcmp(magick+5,"\000\000\007",3) == 0)
117         return(MagickTrue);
118     }
119   return(MagickFalse);
120 }
121 
122 #if defined(MAGICKCORE_X11_DELEGATE)
123 /*
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 %                                                                             %
126 %                                                                             %
127 %                                                                             %
128 %   R e a d X W D I m a g e                                                   %
129 %                                                                             %
130 %                                                                             %
131 %                                                                             %
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 %
134 %  ReadXWDImage() reads an X Window System window dump image file and
135 %  returns it.  It allocates the memory necessary for the new Image structure
136 %  and returns a pointer to the new image.
137 %
138 %  The format of the ReadXWDImage method is:
139 %
140 %      Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
141 %
142 %  A description of each parameter follows:
143 %
144 %    o image_info: the image info.
145 %
146 %    o exception: return any errors or warnings in this structure.
147 %
148 */
149 
ReadXWDImage(const ImageInfo * image_info,ExceptionInfo * exception)150 static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
151 {
152 #define CheckOverflowException(length,width,height) \
153   (((height) != 0) && ((length)/((size_t) height) != ((size_t) width)))
154 
155   char
156     *comment;
157 
158   Image
159     *image;
160 
161   int
162     x_status;
163 
164   MagickBooleanType
165     authentic_colormap;
166 
167   MagickStatusType
168     status;
169 
170   Quantum
171     index;
172 
173   register ssize_t
174     x;
175 
176   register Quantum
177     *q;
178 
179   register ssize_t
180     i;
181 
182   register size_t
183     pixel;
184 
185   size_t
186     length;
187 
188   ssize_t
189     count,
190     y;
191 
192   unsigned long
193     lsb_first;
194 
195   XColor
196     *colors;
197 
198   XImage
199     *ximage;
200 
201   XWDFileHeader
202     header;
203 
204   /*
205     Open image file.
206   */
207   assert(image_info != (const ImageInfo *) NULL);
208   assert(image_info->signature == MagickCoreSignature);
209   if (image_info->debug != MagickFalse)
210     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
211       image_info->filename);
212   assert(exception != (ExceptionInfo *) NULL);
213   assert(exception->signature == MagickCoreSignature);
214   image=AcquireImage(image_info,exception);
215   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
216   if (status == MagickFalse)
217     {
218       image=DestroyImageList(image);
219       return((Image *) NULL);
220     }
221   /*
222      Read in header information.
223   */
224   count=ReadBlob(image,sz_XWDheader,(unsigned char *) &header);
225   if (count != sz_XWDheader)
226     ThrowReaderException(CorruptImageError,"UnableToReadImageHeader");
227   /*
228     Ensure the header byte-order is most-significant byte first.
229   */
230   lsb_first=1;
231   if ((int) (*(char *) &lsb_first) != 0)
232     MSBOrderLong((unsigned char *) &header,sz_XWDheader);
233   /*
234     Check to see if the dump file is in the proper format.
235   */
236   if (header.file_version != XWD_FILE_VERSION)
237     ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
238   if (header.header_size < sz_XWDheader)
239     ThrowReaderException(CorruptImageError,"CorruptImage");
240   switch (header.visual_class) {
241     case StaticGray:
242     case GrayScale:
243     case StaticColor:
244     case PseudoColor:
245     case TrueColor:
246     case DirectColor:
247       break;
248     default:
249       ThrowReaderException(CorruptImageError,"CorruptImage");
250     }
251   switch (header.pixmap_format) {
252     case XYBitmap:
253     case XYPixmap:
254     case ZPixmap:
255       break;
256     default:
257       ThrowReaderException(CorruptImageError,"CorruptImage");
258   }
259   length=(size_t) header.header_size-sz_XWDheader;
260   comment=(char *) AcquireQuantumMemory(length+1,sizeof(*comment));
261   if (comment == (char *) NULL)
262     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
263   count=ReadBlob(image,length,(unsigned char *) comment);
264   comment[length]='\0';
265   (void) SetImageProperty(image,"comment",comment,exception);
266   comment=DestroyString(comment);
267   if (count != (ssize_t) length)
268     ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
269   /*
270     Initialize the X image.
271   */
272   ximage=(XImage *) AcquireMagickMemory(sizeof(*ximage));
273   if (ximage == (XImage *) NULL)
274     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
275   ximage->depth=(int) header.pixmap_depth;
276   ximage->format=(int) header.pixmap_format;
277   ximage->xoffset=(int) header.xoffset;
278   ximage->data=(char *) NULL;
279   ximage->width=(int) header.pixmap_width;
280   ximage->height=(int) header.pixmap_height;
281   ximage->bitmap_pad=(int) header.bitmap_pad;
282   ximage->bytes_per_line=(int) header.bytes_per_line;
283   ximage->byte_order=(int) header.byte_order;
284   ximage->bitmap_unit=(int) header.bitmap_unit;
285   ximage->bitmap_bit_order=(int) header.bitmap_bit_order;
286   ximage->bits_per_pixel=(int) header.bits_per_pixel;
287   ximage->red_mask=header.red_mask;
288   ximage->green_mask=header.green_mask;
289   ximage->blue_mask=header.blue_mask;
290   if ((ximage->width < 0) || (ximage->height < 0) || (ximage->depth < 0) ||
291       (ximage->format < 0) || (ximage->byte_order < 0) ||
292       (ximage->bitmap_bit_order < 0) || (ximage->bitmap_pad < 0) ||
293       (ximage->bytes_per_line < 0))
294     {
295       ximage=(XImage *) RelinquishMagickMemory(ximage);
296       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
297     }
298   if ((ximage->width > 65535) || (ximage->height > 65535))
299     {
300       ximage=(XImage *) RelinquishMagickMemory(ximage);
301       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
302     }
303   if ((ximage->bits_per_pixel > 32) || (ximage->bitmap_unit > 32))
304     {
305       ximage=(XImage *) RelinquishMagickMemory(ximage);
306       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
307     }
308   x_status=XInitImage(ximage);
309   if (x_status == 0)
310     {
311       ximage=(XImage *) RelinquishMagickMemory(ximage);
312       ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
313     }
314   /*
315     Read colormap.
316   */
317   authentic_colormap=MagickFalse;
318   colors=(XColor *) NULL;
319   if (header.ncolors != 0)
320     {
321       XWDColor
322         color;
323 
324       length=(size_t) header.ncolors;
325       if (length > ((~0UL)/sizeof(*colors)))
326         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327       colors=(XColor *) AcquireQuantumMemory(length,sizeof(*colors));
328       if (colors == (XColor *) NULL)
329         {
330           ximage=(XImage *) RelinquishMagickMemory(ximage);
331           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
332         }
333       for (i=0; i < (ssize_t) header.ncolors; i++)
334       {
335         count=ReadBlob(image,sz_XWDColor,(unsigned char *) &color);
336         if (count != sz_XWDColor)
337           {
338             colors=(XColor *) RelinquishMagickMemory(colors);
339             ximage=(XImage *) RelinquishMagickMemory(ximage);
340             ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
341           }
342         colors[i].pixel=color.pixel;
343         colors[i].red=color.red;
344         colors[i].green=color.green;
345         colors[i].blue=color.blue;
346         colors[i].flags=(char) color.flags;
347         if (color.flags != 0)
348           authentic_colormap=MagickTrue;
349       }
350       /*
351         Ensure the header byte-order is most-significant byte first.
352       */
353       lsb_first=1;
354       if ((int) (*(char *) &lsb_first) != 0)
355         for (i=0; i < (ssize_t) header.ncolors; i++)
356         {
357           MSBOrderLong((unsigned char *) &colors[i].pixel,
358             sizeof(colors[i].pixel));
359           MSBOrderShort((unsigned char *) &colors[i].red,3*
360             sizeof(colors[i].red));
361         }
362     }
363   /*
364     Allocate the pixel buffer.
365   */
366   length=(size_t) ximage->bytes_per_line*ximage->height;
367   if (CheckOverflowException(length,ximage->bytes_per_line,ximage->height))
368     {
369       if (header.ncolors != 0)
370         colors=(XColor *) RelinquishMagickMemory(colors);
371       ximage=(XImage *) RelinquishMagickMemory(ximage);
372       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
373     }
374   if (ximage->format != ZPixmap)
375     {
376       size_t
377         extent;
378 
379       extent=length;
380       length*=ximage->depth;
381       if (CheckOverflowException(length,extent,ximage->depth))
382         {
383           if (header.ncolors != 0)
384             colors=(XColor *) RelinquishMagickMemory(colors);
385           ximage=(XImage *) RelinquishMagickMemory(ximage);
386           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
387         }
388     }
389   ximage->data=(char *) AcquireQuantumMemory(length,sizeof(*ximage->data));
390   if (ximage->data == (char *) NULL)
391     {
392       if (header.ncolors != 0)
393         colors=(XColor *) RelinquishMagickMemory(colors);
394       ximage=(XImage *) RelinquishMagickMemory(ximage);
395       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
396     }
397   count=ReadBlob(image,length,(unsigned char *) ximage->data);
398   if (count != (ssize_t) length)
399     {
400       if (header.ncolors != 0)
401         colors=(XColor *) RelinquishMagickMemory(colors);
402       ximage->data=DestroyString(ximage->data);
403       ximage=(XImage *) RelinquishMagickMemory(ximage);
404       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
405     }
406   /*
407     Convert image to MIFF format.
408   */
409   image->columns=(size_t) ximage->width;
410   image->rows=(size_t) ximage->height;
411   image->depth=8;
412   status=SetImageExtent(image,image->columns,image->rows,exception);
413   if (status == MagickFalse)
414     {
415       if (header.ncolors != 0)
416         colors=(XColor *) RelinquishMagickMemory(colors);
417       ximage->data=DestroyString(ximage->data);
418       ximage=(XImage *) RelinquishMagickMemory(ximage);
419       return(DestroyImageList(image));
420     }
421   if ((header.ncolors == 0U) || (ximage->red_mask != 0) ||
422       (ximage->green_mask != 0) || (ximage->blue_mask != 0))
423     image->storage_class=DirectClass;
424   else
425     image->storage_class=PseudoClass;
426   image->colors=header.ncolors;
427   if (image_info->ping == MagickFalse)
428     switch (image->storage_class)
429     {
430       case DirectClass:
431       default:
432       {
433         register size_t
434           color;
435 
436         size_t
437           blue_mask,
438           blue_shift,
439           green_mask,
440           green_shift,
441           red_mask,
442           red_shift;
443 
444         /*
445           Determine shift and mask for red, green, and blue.
446         */
447         red_mask=ximage->red_mask;
448         red_shift=0;
449         while ((red_mask != 0) && ((red_mask & 0x01) == 0))
450         {
451           red_mask>>=1;
452           red_shift++;
453         }
454         green_mask=ximage->green_mask;
455         green_shift=0;
456         while ((green_mask != 0) && ((green_mask & 0x01) == 0))
457         {
458           green_mask>>=1;
459           green_shift++;
460         }
461         blue_mask=ximage->blue_mask;
462         blue_shift=0;
463         while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
464         {
465           blue_mask>>=1;
466           blue_shift++;
467         }
468         /*
469           Convert X image to DirectClass packets.
470         */
471         if ((image->colors != 0) && (authentic_colormap != MagickFalse))
472           for (y=0; y < (ssize_t) image->rows; y++)
473           {
474             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
475             if (q == (Quantum *) NULL)
476               break;
477             for (x=0; x < (ssize_t) image->columns; x++)
478             {
479               pixel=XGetPixel(ximage,(int) x,(int) y);
480               index=(Quantum) ((pixel >> red_shift) & red_mask);
481               if (index < header.ncolors)
482                 SetPixelRed(image,ScaleShortToQuantum(
483                   colors[(ssize_t) index].red),q);
484               index=(Quantum) ((pixel >> green_shift) & green_mask);
485               if (index < header.ncolors)
486                 SetPixelGreen(image,ScaleShortToQuantum(
487                   colors[(ssize_t) index].green),q);
488               index=(Quantum) ((pixel >> blue_shift) & blue_mask);
489               if (index < header.ncolors)
490                 SetPixelBlue(image,ScaleShortToQuantum(
491                   colors[(ssize_t) index].blue),q);
492               q+=GetPixelChannels(image);
493             }
494             if (SyncAuthenticPixels(image,exception) == MagickFalse)
495               break;
496             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
497               image->rows);
498             if (status == MagickFalse)
499               break;
500           }
501         else
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               pixel=XGetPixel(ximage,(int) x,(int) y);
510               color=(pixel >> red_shift) & red_mask;
511               if (red_mask != 0)
512                 color=(color*65535UL)/red_mask;
513               SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q);
514               color=(pixel >> green_shift) & green_mask;
515               if (green_mask != 0)
516                 color=(color*65535UL)/green_mask;
517               SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),
518                 q);
519               color=(pixel >> blue_shift) & blue_mask;
520               if (blue_mask != 0)
521                 color=(color*65535UL)/blue_mask;
522               SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q);
523               q+=GetPixelChannels(image);
524             }
525             if (SyncAuthenticPixels(image,exception) == MagickFalse)
526               break;
527             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
528               image->rows);
529             if (status == MagickFalse)
530               break;
531           }
532         break;
533       }
534       case PseudoClass:
535       {
536         /*
537           Convert X image to PseudoClass packets.
538         */
539         if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
540           {
541             if (header.ncolors != 0)
542               colors=(XColor *) RelinquishMagickMemory(colors);
543             ximage->data=DestroyString(ximage->data);
544             ximage=(XImage *) RelinquishMagickMemory(ximage);
545             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
546           }
547         for (i=0; i < (ssize_t) image->colors; i++)
548         {
549           image->colormap[i].red=(MagickRealType) ScaleShortToQuantum(
550             colors[i].red);
551           image->colormap[i].green=(MagickRealType) ScaleShortToQuantum(
552             colors[i].green);
553           image->colormap[i].blue=(MagickRealType) ScaleShortToQuantum(
554             colors[i].blue);
555         }
556         for (y=0; y < (ssize_t) image->rows; y++)
557         {
558           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
559           if (q == (Quantum *) NULL)
560             break;
561           for (x=0; x < (ssize_t) image->columns; x++)
562           {
563             index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
564               (int) y),exception);
565             SetPixelIndex(image,index,q);
566             SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
567             q+=GetPixelChannels(image);
568           }
569           if (SyncAuthenticPixels(image,exception) == MagickFalse)
570             break;
571           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
572             image->rows);
573           if (status == MagickFalse)
574             break;
575         }
576         break;
577       }
578     }
579   /*
580     Free image and colormap.
581   */
582   if (header.ncolors != 0)
583     colors=(XColor *) RelinquishMagickMemory(colors);
584   ximage->data=DestroyString(ximage->data);
585   ximage=(XImage *) RelinquishMagickMemory(ximage);
586   if (EOFBlob(image) != MagickFalse)
587     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
588       image->filename);
589   (void) CloseBlob(image);
590   return(GetFirstImageInList(image));
591 }
592 #endif
593 
594 /*
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 %                                                                             %
597 %                                                                             %
598 %                                                                             %
599 %   R e g i s t e r X W D I m a g e                                           %
600 %                                                                             %
601 %                                                                             %
602 %                                                                             %
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604 %
605 %  RegisterXWDImage() adds properties for the XWD image format to
606 %  the list of supported formats.  The properties include the image format
607 %  tag, a method to read and/or write the format, whether the format
608 %  supports the saving of more than one frame to the same file or blob,
609 %  whether the format supports native in-memory I/O, and a brief
610 %  description of the format.
611 %
612 %  The format of the RegisterXWDImage method is:
613 %
614 %      size_t RegisterXWDImage(void)
615 %
616 */
RegisterXWDImage(void)617 ModuleExport size_t RegisterXWDImage(void)
618 {
619   MagickInfo
620     *entry;
621 
622   entry=AcquireMagickInfo("XWD","XWD","X Windows system window dump (color)");
623 #if defined(MAGICKCORE_X11_DELEGATE)
624   entry->decoder=(DecodeImageHandler *) ReadXWDImage;
625   entry->encoder=(EncodeImageHandler *) WriteXWDImage;
626 #endif
627   entry->magick=(IsImageFormatHandler *) IsXWD;
628   entry->flags^=CoderAdjoinFlag;
629   (void) RegisterMagickInfo(entry);
630   return(MagickImageCoderSignature);
631 }
632 
633 /*
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 %                                                                             %
636 %                                                                             %
637 %                                                                             %
638 %   U n r e g i s t e r X W D I m a g e                                       %
639 %                                                                             %
640 %                                                                             %
641 %                                                                             %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 %
644 %  UnregisterXWDImage() removes format registrations made by the
645 %  XWD module from the list of supported formats.
646 %
647 %  The format of the UnregisterXWDImage method is:
648 %
649 %      UnregisterXWDImage(void)
650 %
651 */
UnregisterXWDImage(void)652 ModuleExport void UnregisterXWDImage(void)
653 {
654   (void) UnregisterMagickInfo("XWD");
655 }
656 
657 #if defined(MAGICKCORE_X11_DELEGATE)
658 /*
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 %                                                                             %
661 %                                                                             %
662 %                                                                             %
663 %   W r i t e X W D I m a g e                                                 %
664 %                                                                             %
665 %                                                                             %
666 %                                                                             %
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 %
669 %  WriteXWDImage() writes an image to a file in X window dump
670 %  rasterfile format.
671 %
672 %  The format of the WriteXWDImage method is:
673 %
674 %      MagickBooleanType WriteXWDImage(const ImageInfo *image_info,
675 %        Image *image,ExceptionInfo *exception)
676 %
677 %  A description of each parameter follows.
678 %
679 %    o image_info: the image info.
680 %
681 %    o image:  The image.
682 %
683 %    o exception: return any errors or warnings in this structure.
684 %
685 */
WriteXWDImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)686 static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image,
687   ExceptionInfo *exception)
688 {
689   const char
690     *value;
691 
692   MagickBooleanType
693     status;
694 
695   register const Quantum
696     *p;
697 
698   register ssize_t
699     x;
700 
701   register unsigned char
702     *q;
703 
704   size_t
705     bits_per_pixel,
706     bytes_per_line,
707     length,
708     scanline_pad;
709 
710   ssize_t
711     y;
712 
713   unsigned char
714     *pixels;
715 
716   unsigned long
717     lsb_first;
718 
719   XWDFileHeader
720     xwd_info;
721 
722   /*
723     Open output image file.
724   */
725   assert(image_info != (const ImageInfo *) NULL);
726   assert(image_info->signature == MagickCoreSignature);
727   assert(image != (Image *) NULL);
728   assert(image->signature == MagickCoreSignature);
729   if (image->debug != MagickFalse)
730     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
731   assert(exception != (ExceptionInfo *) NULL);
732   assert(exception->signature == MagickCoreSignature);
733   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
734   if (status == MagickFalse)
735     return(status);
736   (void) TransformImageColorspace(image,sRGBColorspace,exception);
737   /*
738     Initialize XWD file header.
739   */
740   (void) memset(&xwd_info,0,sizeof(xwd_info));
741   xwd_info.header_size=(CARD32) sz_XWDheader;
742   value=GetImageProperty(image,"comment",exception);
743   if (value != (const char *) NULL)
744     xwd_info.header_size+=(CARD32) strlen(value);
745   xwd_info.header_size++;
746   xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
747   xwd_info.pixmap_format=(CARD32) ZPixmap;
748   xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
749   xwd_info.pixmap_width=(CARD32) image->columns;
750   xwd_info.pixmap_height=(CARD32) image->rows;
751   xwd_info.xoffset=(CARD32) 0;
752   xwd_info.byte_order=(CARD32) MSBFirst;
753   xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
754   xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
755   xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
756   bits_per_pixel=(size_t) (image->storage_class == DirectClass ? 24 : 8);
757   xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
758   bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
759     xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
760     (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
761   xwd_info.bytes_per_line=(CARD32) bytes_per_line;
762   xwd_info.visual_class=(CARD32)
763     (image->storage_class == DirectClass ? DirectColor : PseudoColor);
764   xwd_info.red_mask=(CARD32)
765     (image->storage_class == DirectClass ? 0xff0000 : 0);
766   xwd_info.green_mask=(CARD32)
767     (image->storage_class == DirectClass ? 0xff00 : 0);
768   xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
769   xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
770   xwd_info.colormap_entries=(CARD32)
771     (image->storage_class == DirectClass ? 256 : image->colors);
772   xwd_info.ncolors=(unsigned int)
773     (image->storage_class == DirectClass ? 0 : image->colors);
774   xwd_info.window_width=(CARD32) image->columns;
775   xwd_info.window_height=(CARD32) image->rows;
776   xwd_info.window_x=0;
777   xwd_info.window_y=0;
778   xwd_info.window_bdrwidth=(CARD32) 0;
779   /*
780     Write XWD header.
781   */
782   lsb_first=1;
783   if ((int) (*(char *) &lsb_first) != 0)
784     MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
785   (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
786   if (value != (const char *) NULL)
787     (void) WriteBlob(image,strlen(value),(unsigned char *) value);
788   (void) WriteBlob(image,1,(const unsigned char *) "\0");
789   if (image->storage_class == PseudoClass)
790     {
791       register ssize_t
792         i;
793 
794       XColor
795         *colors;
796 
797       XWDColor
798         color;
799 
800       /*
801         Dump colormap to file.
802       */
803       (void) memset(&color,0,sizeof(color));
804       colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
805         sizeof(*colors));
806       if (colors == (XColor *) NULL)
807         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
808       for (i=0; i < (ssize_t) image->colors; i++)
809       {
810         colors[i].pixel=(unsigned long) i;
811         colors[i].red=ScaleQuantumToShort(ClampToQuantum(
812           image->colormap[i].red));
813         colors[i].green=ScaleQuantumToShort(ClampToQuantum(
814           image->colormap[i].green));
815         colors[i].blue=ScaleQuantumToShort(ClampToQuantum(
816           image->colormap[i].blue));
817         colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
818         colors[i].pad='\0';
819         if ((int) (*(char *) &lsb_first) != 0)
820           {
821             MSBOrderLong((unsigned char *) &colors[i].pixel,
822               sizeof(colors[i].pixel));
823             MSBOrderShort((unsigned char *) &colors[i].red,
824               3*sizeof(colors[i].red));
825           }
826       }
827       for (i=0; i < (ssize_t) image->colors; i++)
828       {
829         color.pixel=(CARD32) colors[i].pixel;
830         color.red=colors[i].red;
831         color.green=colors[i].green;
832         color.blue=colors[i].blue;
833         color.flags=(CARD8) colors[i].flags;
834         (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
835       }
836       colors=(XColor *) RelinquishMagickMemory(colors);
837     }
838   /*
839     Allocate memory for pixels.
840   */
841   length=3*bytes_per_line;
842   if (image->storage_class == PseudoClass)
843     length=bytes_per_line;
844   pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
845   if (pixels == (unsigned char *) NULL)
846     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
847   (void) memset(pixels,0,length);
848   /*
849     Convert MIFF to XWD raster pixels.
850   */
851   scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
852   for (y=0; y < (ssize_t) image->rows; y++)
853   {
854     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
855     if (p == (const Quantum *) NULL)
856       break;
857     q=pixels;
858     if (image->storage_class == PseudoClass)
859       {
860         for (x=0; x < (ssize_t) image->columns; x++)
861         {
862           *q++=(unsigned char) GetPixelIndex(image,p);
863           p+=GetPixelChannels(image);
864         }
865       }
866     else
867       for (x=0; x < (ssize_t) image->columns; x++)
868       {
869         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
870         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
871         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
872         p+=GetPixelChannels(image);
873       }
874     for (x=0; x < (ssize_t) scanline_pad; x++)
875       *q++='\0';
876     (void) WriteBlob(image,(size_t) (q-pixels),pixels);
877     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
878       image->rows);
879     if (status == MagickFalse)
880       break;
881   }
882   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
883   (void) CloseBlob(image);
884   return(MagickTrue);
885 }
886 #endif
887