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