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