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