]> granicus.if.org Git - imagemagick/blob - coders/xwd.c
(no commit message)
[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             SetRedPixelComponent(q,image->colormap[(ssize_t) index].red);
482             SetGreenPixelComponent(q,image->colormap[(ssize_t) index].green);
483             SetBluePixelComponent(q,image->colormap[(ssize_t) index].blue);
484             q++;
485           }
486           if (SyncAuthenticPixels(image,exception) == MagickFalse)
487             break;
488           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
489             image->rows);
490           if (status == MagickFalse)
491             break;
492         }
493         break;
494       }
495     }
496   /*
497     Free image and colormap.
498   */
499   if (header.ncolors != 0)
500     colors=(XColor *) RelinquishMagickMemory(colors);
501   ximage->data=DestroyString(ximage->data);
502   ximage=(XImage *) RelinquishMagickMemory(ximage);
503   if (EOFBlob(image) != MagickFalse)
504     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
505       image->filename);
506   (void) CloseBlob(image);
507   return(GetFirstImageInList(image));
508 }
509 #endif
510 \f
511 /*
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 %                                                                             %
514 %                                                                             %
515 %                                                                             %
516 %   R e g i s t e r X W D I m a g e                                           %
517 %                                                                             %
518 %                                                                             %
519 %                                                                             %
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 %
522 %  RegisterXWDImage() adds properties for the XWD image format to
523 %  the list of supported formats.  The properties include the image format
524 %  tag, a method to read and/or write the format, whether the format
525 %  supports the saving of more than one frame to the same file or blob,
526 %  whether the format supports native in-memory I/O, and a brief
527 %  description of the format.
528 %
529 %  The format of the RegisterXWDImage method is:
530 %
531 %      size_t RegisterXWDImage(void)
532 %
533 */
534 ModuleExport size_t RegisterXWDImage(void)
535 {
536   MagickInfo
537     *entry;
538
539   entry=SetMagickInfo("XWD");
540 #if defined(MAGICKCORE_X11_DELEGATE)
541   entry->decoder=(DecodeImageHandler *) ReadXWDImage;
542   entry->encoder=(EncodeImageHandler *) WriteXWDImage;
543 #endif
544   entry->magick=(IsImageFormatHandler *) IsXWD;
545   entry->adjoin=MagickFalse;
546   entry->description=ConstantString("X Windows system window dump (color)");
547   entry->module=ConstantString("XWD");
548   (void) RegisterMagickInfo(entry);
549   return(MagickImageCoderSignature);
550 }
551 \f
552 /*
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 %                                                                             %
555 %                                                                             %
556 %                                                                             %
557 %   U n r e g i s t e r X W D I m a g e                                       %
558 %                                                                             %
559 %                                                                             %
560 %                                                                             %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 %
563 %  UnregisterXWDImage() removes format registrations made by the
564 %  XWD module from the list of supported formats.
565 %
566 %  The format of the UnregisterXWDImage method is:
567 %
568 %      UnregisterXWDImage(void)
569 %
570 */
571 ModuleExport void UnregisterXWDImage(void)
572 {
573   (void) UnregisterMagickInfo("XWD");
574 }
575 \f
576 #if defined(MAGICKCORE_X11_DELEGATE)
577 /*
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 %                                                                             %
580 %                                                                             %
581 %                                                                             %
582 %   W r i t e X W D I m a g e                                                 %
583 %                                                                             %
584 %                                                                             %
585 %                                                                             %
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 %
588 %  WriteXWDImage() writes an image to a file in X window dump
589 %  rasterfile format.
590 %
591 %  The format of the WriteXWDImage method is:
592 %
593 %      MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
594 %
595 %  A description of each parameter follows.
596 %
597 %    o image_info: the image info.
598 %
599 %    o image:  The image.
600 %
601 */
602 static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
603 {
604   const char
605     *value;
606
607   MagickBooleanType
608     status;
609
610   register const IndexPacket
611     *indexes;
612
613   register const PixelPacket
614     *p;
615
616   register ssize_t
617     x;
618
619   register ssize_t
620     i;
621
622   register unsigned char
623     *q;
624
625   size_t
626     bits_per_pixel,
627     bytes_per_line,
628     length,
629     lsb_first,
630     scanline_pad;
631
632   ssize_t
633     y;
634
635   unsigned char
636     *pixels;
637
638   XWDFileHeader
639     xwd_info;
640
641   /*
642     Open output image file.
643   */
644   assert(image_info != (const ImageInfo *) NULL);
645   assert(image_info->signature == MagickSignature);
646   assert(image != (Image *) NULL);
647   assert(image->signature == MagickSignature);
648   if (image->debug != MagickFalse)
649     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
650   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
651   if (status == MagickFalse)
652     return(status);
653   if (image->colorspace != RGBColorspace)
654     (void) TransformImageColorspace(image,RGBColorspace);
655   /*
656     Initialize XWD file header.
657   */
658   (void) ResetMagickMemory(&xwd_info,0,sizeof(xwd_info));
659   xwd_info.header_size=(CARD32) sz_XWDheader;
660   value=GetImageProperty(image,"comment");
661   if (value != (const char *) NULL)
662     xwd_info.header_size+=(CARD32) strlen(value);
663   xwd_info.header_size++;
664   xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
665   xwd_info.pixmap_format=(CARD32) ZPixmap;
666   xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
667   xwd_info.pixmap_width=(CARD32) image->columns;
668   xwd_info.pixmap_height=(CARD32) image->rows;
669   xwd_info.xoffset=(CARD32) 0;
670   xwd_info.byte_order=(CARD32) MSBFirst;
671   xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
672   xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
673   xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
674   bits_per_pixel=(size_t) (image->storage_class == DirectClass ? 24 : 8);
675   xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
676   bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
677     xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
678     (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
679   xwd_info.bytes_per_line=(CARD32) bytes_per_line;
680   xwd_info.visual_class=(CARD32)
681     (image->storage_class == DirectClass ? DirectColor : PseudoColor);
682   xwd_info.red_mask=(CARD32)
683     (image->storage_class == DirectClass ? 0xff0000 : 0);
684   xwd_info.green_mask=(CARD32)
685     (image->storage_class == DirectClass ? 0xff00 : 0);
686   xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
687   xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
688   xwd_info.colormap_entries=(CARD32)
689     (image->storage_class == DirectClass ? 256 : image->colors);
690   xwd_info.ncolors=(unsigned int)
691     (image->storage_class == DirectClass ? 0 : image->colors);
692   xwd_info.window_width=(CARD32) image->columns;
693   xwd_info.window_height=(CARD32) image->rows;
694   xwd_info.window_x=0;
695   xwd_info.window_y=0;
696   xwd_info.window_bdrwidth=(CARD32) 0;
697   /*
698     Write XWD header.
699   */
700   lsb_first=1;
701   if ((int) (*(char *) &lsb_first) != 0)
702     MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
703   (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
704   if (value != (const char *) NULL)
705     (void) WriteBlob(image,strlen(value),(unsigned char *) value);
706   (void) WriteBlob(image,1,(const unsigned char *) "\0");
707   if (image->storage_class == PseudoClass)
708     {
709       XColor
710         *colors;
711
712       XWDColor
713         color;
714
715       /*
716         Dump colormap to file.
717       */
718       (void) ResetMagickMemory(&color,0,sizeof(color));
719       colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
720         sizeof(*colors));
721       if (colors == (XColor *) NULL)
722         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
723       for (i=0; i < (ssize_t) image->colors; i++)
724       {
725         colors[i].pixel=(size_t) i;
726         colors[i].red=ScaleQuantumToShort(image->colormap[i].red);
727         colors[i].green=ScaleQuantumToShort(image->colormap[i].green);
728         colors[i].blue=ScaleQuantumToShort(image->colormap[i].blue);
729         colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
730         colors[i].pad='\0';
731         if ((int) (*(char *) &lsb_first) != 0)
732           {
733             MSBOrderLong((unsigned char *) &colors[i].pixel,
734               sizeof(colors[i].pixel));
735             MSBOrderShort((unsigned char *) &colors[i].red,
736               3*sizeof(colors[i].red));
737           }
738       }
739       for (i=0; i < (ssize_t) image->colors; i++)
740       {
741         color.pixel=(CARD32) colors[i].pixel;
742         color.red=colors[i].red;
743         color.green=colors[i].green;
744         color.blue=colors[i].blue;
745         color.flags=(CARD8) colors[i].flags;
746         (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
747       }
748       colors=(XColor *) RelinquishMagickMemory(colors);
749     }
750   /*
751     Allocate memory for pixels.
752   */
753   length=3*bytes_per_line;
754   if (image->storage_class == PseudoClass)
755     length=bytes_per_line;
756   pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
757   if (pixels == (unsigned char *) NULL)
758     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
759   (void) ResetMagickMemory(pixels,0,length);
760   /*
761     Convert MIFF to XWD raster pixels.
762   */
763   scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
764   for (y=0; y < (ssize_t) image->rows; y++)
765   {
766     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
767     if (p == (const PixelPacket *) NULL)
768       break;
769     q=pixels;
770     if (image->storage_class == PseudoClass)
771       {
772         indexes=GetVirtualIndexQueue(image);
773         for (x=0; x < (ssize_t) image->columns; x++)
774           *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
775       }
776     else
777       for (x=0; x < (ssize_t) image->columns; x++)
778       {
779         *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
780         *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
781         *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
782         p++;
783       }
784     for (x=0; x < (ssize_t) scanline_pad; x++)
785       *q++='\0';
786     (void) WriteBlob(image,(size_t) (q-pixels),pixels);
787     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
788       image->rows);
789     if (status == MagickFalse)
790       break;
791   }
792   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
793   (void) CloseBlob(image);
794   return(MagickTrue);
795 }
796 #endif