2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write X Windows System Window Dump Format %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
69 #include <X11/XWDFile.h>
78 #if defined(MAGICKCORE_X11_DELEGATE)
79 static MagickBooleanType
80 WriteXWDImage(const ImageInfo *,Image *,ExceptionInfo *);
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % IsXWD() returns MagickTrue if the image format type, identified by the
95 % magick string, is XWD.
97 % The format of the IsXWD method is:
99 % MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
101 % A description of each parameter follows:
103 % o magick: compare image format pattern against these bytes.
105 % o length: Specifies the length of the magick string.
108 static MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
112 if (memcmp(magick+1,"\000\000",2) == 0)
114 if (memcmp(magick+4,"\007\000\000",3) == 0)
116 if (memcmp(magick+5,"\000\000\007",3) == 0)
122 #if defined(MAGICKCORE_X11_DELEGATE)
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 % R e a d X W D I m a g e %
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
138 % The format of the ReadXWDImage method is:
140 % Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
142 % A description of each parameter follows:
144 % o image_info: the image info.
146 % o exception: return any errors or warnings in this structure.
150 static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
152 #define CheckOverflowException(length,width,height) \
153 (((height) != 0) && ((length)/((size_t) height) != ((size_t) width)))
207 assert(image_info != (const ImageInfo *) NULL);
208 assert(image_info->signature == MagickCoreSignature);
209 if (image_info->debug != MagickFalse)
210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
211 image_info->filename);
212 assert(exception != (ExceptionInfo *) NULL);
213 assert(exception->signature == MagickCoreSignature);
214 image=AcquireImage(image_info,exception);
215 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
216 if (status == MagickFalse)
218 image=DestroyImageList(image);
219 return((Image *) NULL);
222 Read in header information.
224 count=ReadBlob(image,sz_XWDheader,(unsigned char *) &header);
225 if (count != sz_XWDheader)
226 ThrowReaderException(CorruptImageError,"UnableToReadImageHeader");
228 Ensure the header byte-order is most-significant byte first.
231 if ((int) (*(char *) &lsb_first) != 0)
232 MSBOrderLong((unsigned char *) &header,sz_XWDheader);
234 Check to see if the dump file is in the proper format.
236 if (header.file_version != XWD_FILE_VERSION)
237 ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
238 if (header.header_size < sz_XWDheader)
239 ThrowReaderException(CorruptImageError,"CorruptImage");
240 switch (header.visual_class) {
249 ThrowReaderException(CorruptImageError,"CorruptImage");
251 switch (header.pixmap_format) {
257 ThrowReaderException(CorruptImageError,"CorruptImage");
259 length=(size_t) header.header_size-sz_XWDheader;
260 comment=(char *) AcquireQuantumMemory(length+1,sizeof(*comment));
261 if (comment == (char *) NULL)
262 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
263 count=ReadBlob(image,length,(unsigned char *) comment);
264 comment[length]='\0';
265 (void) SetImageProperty(image,"comment",comment,exception);
266 comment=DestroyString(comment);
267 if (count != (ssize_t) length)
268 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
270 Initialize the X image.
272 ximage=(XImage *) AcquireMagickMemory(sizeof(*ximage));
273 if (ximage == (XImage *) NULL)
274 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
275 ximage->depth=(int) header.pixmap_depth;
276 ximage->format=(int) header.pixmap_format;
277 ximage->xoffset=(int) header.xoffset;
278 ximage->data=(char *) NULL;
279 ximage->width=(int) header.pixmap_width;
280 ximage->height=(int) header.pixmap_height;
281 ximage->bitmap_pad=(int) header.bitmap_pad;
282 ximage->bytes_per_line=(int) header.bytes_per_line;
283 ximage->byte_order=(int) header.byte_order;
284 ximage->bitmap_unit=(int) header.bitmap_unit;
285 ximage->bitmap_bit_order=(int) header.bitmap_bit_order;
286 ximage->bits_per_pixel=(int) header.bits_per_pixel;
287 ximage->red_mask=header.red_mask;
288 ximage->green_mask=header.green_mask;
289 ximage->blue_mask=header.blue_mask;
290 if ((ximage->width < 0) || (ximage->height < 0) || (ximage->depth < 0) ||
291 (ximage->format < 0) || (ximage->byte_order < 0) ||
292 (ximage->bitmap_bit_order < 0) || (ximage->bitmap_pad < 0) ||
293 (ximage->bytes_per_line < 0))
295 ximage=(XImage *) RelinquishMagickMemory(ximage);
296 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
298 if ((ximage->width > 65535) || (ximage->height > 65535))
300 ximage=(XImage *) RelinquishMagickMemory(ximage);
301 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
303 if ((ximage->bits_per_pixel > 32) || (ximage->bitmap_unit > 32))
305 ximage=(XImage *) RelinquishMagickMemory(ximage);
306 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
308 x_status=XInitImage(ximage);
311 ximage=(XImage *) RelinquishMagickMemory(ximage);
312 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
317 authentic_colormap=MagickFalse;
318 colors=(XColor *) NULL;
319 if (header.ncolors != 0)
324 length=(size_t) header.ncolors;
325 if (length > ((~0UL)/sizeof(*colors)))
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327 colors=(XColor *) AcquireQuantumMemory(length,sizeof(*colors));
328 if (colors == (XColor *) NULL)
330 ximage=(XImage *) RelinquishMagickMemory(ximage);
331 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
333 for (i=0; i < (ssize_t) header.ncolors; i++)
335 count=ReadBlob(image,sz_XWDColor,(unsigned char *) &color);
336 if (count != sz_XWDColor)
338 colors=(XColor *) RelinquishMagickMemory(colors);
339 ximage=(XImage *) RelinquishMagickMemory(ximage);
340 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
342 colors[i].pixel=color.pixel;
343 colors[i].red=color.red;
344 colors[i].green=color.green;
345 colors[i].blue=color.blue;
346 colors[i].flags=(char) color.flags;
347 if (color.flags != 0)
348 authentic_colormap=MagickTrue;
351 Ensure the header byte-order is most-significant byte first.
354 if ((int) (*(char *) &lsb_first) != 0)
355 for (i=0; i < (ssize_t) header.ncolors; i++)
357 MSBOrderLong((unsigned char *) &colors[i].pixel,
358 sizeof(colors[i].pixel));
359 MSBOrderShort((unsigned char *) &colors[i].red,3*
360 sizeof(colors[i].red));
364 Allocate the pixel buffer.
366 length=(size_t) ximage->bytes_per_line*ximage->height;
367 if (CheckOverflowException(length,ximage->bytes_per_line,ximage->height))
369 if (header.ncolors != 0)
370 colors=(XColor *) RelinquishMagickMemory(colors);
371 ximage=(XImage *) RelinquishMagickMemory(ximage);
372 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
374 if (ximage->format != ZPixmap)
380 length*=ximage->depth;
381 if (CheckOverflowException(length,extent,ximage->depth))
383 if (header.ncolors != 0)
384 colors=(XColor *) RelinquishMagickMemory(colors);
385 ximage=(XImage *) RelinquishMagickMemory(ximage);
386 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
389 ximage->data=(char *) AcquireQuantumMemory(length,sizeof(*ximage->data));
390 if (ximage->data == (char *) NULL)
392 if (header.ncolors != 0)
393 colors=(XColor *) RelinquishMagickMemory(colors);
394 ximage=(XImage *) RelinquishMagickMemory(ximage);
395 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
397 count=ReadBlob(image,length,(unsigned char *) ximage->data);
398 if (count != (ssize_t) length)
400 if (header.ncolors != 0)
401 colors=(XColor *) RelinquishMagickMemory(colors);
402 ximage->data=DestroyString(ximage->data);
403 ximage=(XImage *) RelinquishMagickMemory(ximage);
404 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
407 Convert image to MIFF format.
409 image->columns=(size_t) ximage->width;
410 image->rows=(size_t) ximage->height;
412 status=SetImageExtent(image,image->columns,image->rows,exception);
413 if (status == MagickFalse)
415 if (header.ncolors != 0)
416 colors=(XColor *) RelinquishMagickMemory(colors);
417 ximage->data=DestroyString(ximage->data);
418 ximage=(XImage *) RelinquishMagickMemory(ximage);
419 return(DestroyImageList(image));
421 if ((header.ncolors == 0U) || (ximage->red_mask != 0) ||
422 (ximage->green_mask != 0) || (ximage->blue_mask != 0))
423 image->storage_class=DirectClass;
425 image->storage_class=PseudoClass;
426 image->colors=header.ncolors;
427 if (image_info->ping == MagickFalse)
428 switch (image->storage_class)
445 Determine shift and mask for red, green, and blue.
447 red_mask=ximage->red_mask;
449 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
454 green_mask=ximage->green_mask;
456 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
461 blue_mask=ximage->blue_mask;
463 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
469 Convert X image to DirectClass packets.
471 if ((image->colors != 0) && (authentic_colormap != MagickFalse))
472 for (y=0; y < (ssize_t) image->rows; y++)
474 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
475 if (q == (Quantum *) NULL)
477 for (x=0; x < (ssize_t) image->columns; x++)
479 pixel=XGetPixel(ximage,(int) x,(int) y);
480 index=(Quantum) ((pixel >> red_shift) & red_mask);
481 if (index < header.ncolors)
482 SetPixelRed(image,ScaleShortToQuantum(
483 colors[(ssize_t) index].red),q);
484 index=(Quantum) ((pixel >> green_shift) & green_mask);
485 if (index < header.ncolors)
486 SetPixelGreen(image,ScaleShortToQuantum(
487 colors[(ssize_t) index].green),q);
488 index=(Quantum) ((pixel >> blue_shift) & blue_mask);
489 if (index < header.ncolors)
490 SetPixelBlue(image,ScaleShortToQuantum(
491 colors[(ssize_t) index].blue),q);
492 q+=GetPixelChannels(image);
494 if (SyncAuthenticPixels(image,exception) == MagickFalse)
496 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
498 if (status == MagickFalse)
502 for (y=0; y < (ssize_t) image->rows; y++)
504 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
505 if (q == (Quantum *) NULL)
507 for (x=0; x < (ssize_t) image->columns; x++)
509 pixel=XGetPixel(ximage,(int) x,(int) y);
510 color=(pixel >> red_shift) & red_mask;
512 color=(color*65535UL)/red_mask;
513 SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q);
514 color=(pixel >> green_shift) & green_mask;
516 color=(color*65535UL)/green_mask;
517 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),
519 color=(pixel >> blue_shift) & blue_mask;
521 color=(color*65535UL)/blue_mask;
522 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q);
523 q+=GetPixelChannels(image);
525 if (SyncAuthenticPixels(image,exception) == MagickFalse)
527 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
529 if (status == MagickFalse)
537 Convert X image to PseudoClass packets.
539 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
541 if (header.ncolors != 0)
542 colors=(XColor *) RelinquishMagickMemory(colors);
543 ximage->data=DestroyString(ximage->data);
544 ximage=(XImage *) RelinquishMagickMemory(ximage);
545 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
547 for (i=0; i < (ssize_t) image->colors; i++)
549 image->colormap[i].red=(MagickRealType) ScaleShortToQuantum(
551 image->colormap[i].green=(MagickRealType) ScaleShortToQuantum(
553 image->colormap[i].blue=(MagickRealType) ScaleShortToQuantum(
556 for (y=0; y < (ssize_t) image->rows; y++)
558 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
559 if (q == (Quantum *) NULL)
561 for (x=0; x < (ssize_t) image->columns; x++)
563 index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
565 SetPixelIndex(image,index,q);
566 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
567 q+=GetPixelChannels(image);
569 if (SyncAuthenticPixels(image,exception) == MagickFalse)
571 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
573 if (status == MagickFalse)
580 Free image and colormap.
582 if (header.ncolors != 0)
583 colors=(XColor *) RelinquishMagickMemory(colors);
584 ximage->data=DestroyString(ximage->data);
585 ximage=(XImage *) RelinquishMagickMemory(ximage);
586 if (EOFBlob(image) != MagickFalse)
587 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
589 (void) CloseBlob(image);
590 return(GetFirstImageInList(image));
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599 % R e g i s t e r X W D I m a g e %
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 % RegisterXWDImage() adds properties for the XWD image format to
606 % the list of supported formats. The properties include the image format
607 % tag, a method to read and/or write the format, whether the format
608 % supports the saving of more than one frame to the same file or blob,
609 % whether the format supports native in-memory I/O, and a brief
610 % description of the format.
612 % The format of the RegisterXWDImage method is:
614 % size_t RegisterXWDImage(void)
617 ModuleExport size_t RegisterXWDImage(void)
622 entry=AcquireMagickInfo("XWD","XWD","X Windows system window dump (color)");
623 #if defined(MAGICKCORE_X11_DELEGATE)
624 entry->decoder=(DecodeImageHandler *) ReadXWDImage;
625 entry->encoder=(EncodeImageHandler *) WriteXWDImage;
627 entry->magick=(IsImageFormatHandler *) IsXWD;
628 entry->flags^=CoderAdjoinFlag;
629 (void) RegisterMagickInfo(entry);
630 return(MagickImageCoderSignature);
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % U n r e g i s t e r X W D I m a g e %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % UnregisterXWDImage() removes format registrations made by the
645 % XWD module from the list of supported formats.
647 % The format of the UnregisterXWDImage method is:
649 % UnregisterXWDImage(void)
652 ModuleExport void UnregisterXWDImage(void)
654 (void) UnregisterMagickInfo("XWD");
657 #if defined(MAGICKCORE_X11_DELEGATE)
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 % W r i t e X W D I m a g e %
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 % WriteXWDImage() writes an image to a file in X window dump
672 % The format of the WriteXWDImage method is:
674 % MagickBooleanType WriteXWDImage(const ImageInfo *image_info,
675 % Image *image,ExceptionInfo *exception)
677 % A description of each parameter follows.
679 % o image_info: the image info.
681 % o image: The image.
683 % o exception: return any errors or warnings in this structure.
686 static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image,
687 ExceptionInfo *exception)
695 register const Quantum
701 register unsigned char
723 Open output image file.
725 assert(image_info != (const ImageInfo *) NULL);
726 assert(image_info->signature == MagickCoreSignature);
727 assert(image != (Image *) NULL);
728 assert(image->signature == MagickCoreSignature);
729 if (image->debug != MagickFalse)
730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
731 assert(exception != (ExceptionInfo *) NULL);
732 assert(exception->signature == MagickCoreSignature);
733 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
734 if (status == MagickFalse)
736 (void) TransformImageColorspace(image,sRGBColorspace,exception);
738 Initialize XWD file header.
740 (void) memset(&xwd_info,0,sizeof(xwd_info));
741 xwd_info.header_size=(CARD32) sz_XWDheader;
742 value=GetImageProperty(image,"comment",exception);
743 if (value != (const char *) NULL)
744 xwd_info.header_size+=(CARD32) strlen(value);
745 xwd_info.header_size++;
746 xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
747 xwd_info.pixmap_format=(CARD32) ZPixmap;
748 xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
749 xwd_info.pixmap_width=(CARD32) image->columns;
750 xwd_info.pixmap_height=(CARD32) image->rows;
751 xwd_info.xoffset=(CARD32) 0;
752 xwd_info.byte_order=(CARD32) MSBFirst;
753 xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
754 xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
755 xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
756 bits_per_pixel=(size_t) (image->storage_class == DirectClass ? 24 : 8);
757 xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
758 bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
759 xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
760 (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
761 xwd_info.bytes_per_line=(CARD32) bytes_per_line;
762 xwd_info.visual_class=(CARD32)
763 (image->storage_class == DirectClass ? DirectColor : PseudoColor);
764 xwd_info.red_mask=(CARD32)
765 (image->storage_class == DirectClass ? 0xff0000 : 0);
766 xwd_info.green_mask=(CARD32)
767 (image->storage_class == DirectClass ? 0xff00 : 0);
768 xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
769 xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
770 xwd_info.colormap_entries=(CARD32)
771 (image->storage_class == DirectClass ? 256 : image->colors);
772 xwd_info.ncolors=(unsigned int)
773 (image->storage_class == DirectClass ? 0 : image->colors);
774 xwd_info.window_width=(CARD32) image->columns;
775 xwd_info.window_height=(CARD32) image->rows;
778 xwd_info.window_bdrwidth=(CARD32) 0;
783 if ((int) (*(char *) &lsb_first) != 0)
784 MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
785 (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
786 if (value != (const char *) NULL)
787 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
788 (void) WriteBlob(image,1,(const unsigned char *) "\0");
789 if (image->storage_class == PseudoClass)
801 Dump colormap to file.
803 (void) memset(&color,0,sizeof(color));
804 colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
806 if (colors == (XColor *) NULL)
807 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
808 for (i=0; i < (ssize_t) image->colors; i++)
810 colors[i].pixel=(unsigned long) i;
811 colors[i].red=ScaleQuantumToShort(ClampToQuantum(
812 image->colormap[i].red));
813 colors[i].green=ScaleQuantumToShort(ClampToQuantum(
814 image->colormap[i].green));
815 colors[i].blue=ScaleQuantumToShort(ClampToQuantum(
816 image->colormap[i].blue));
817 colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
819 if ((int) (*(char *) &lsb_first) != 0)
821 MSBOrderLong((unsigned char *) &colors[i].pixel,
822 sizeof(colors[i].pixel));
823 MSBOrderShort((unsigned char *) &colors[i].red,
824 3*sizeof(colors[i].red));
827 for (i=0; i < (ssize_t) image->colors; i++)
829 color.pixel=(CARD32) colors[i].pixel;
830 color.red=colors[i].red;
831 color.green=colors[i].green;
832 color.blue=colors[i].blue;
833 color.flags=(CARD8) colors[i].flags;
834 (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
836 colors=(XColor *) RelinquishMagickMemory(colors);
839 Allocate memory for pixels.
841 length=3*bytes_per_line;
842 if (image->storage_class == PseudoClass)
843 length=bytes_per_line;
844 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
845 if (pixels == (unsigned char *) NULL)
846 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
847 (void) memset(pixels,0,length);
849 Convert MIFF to XWD raster pixels.
851 scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
852 for (y=0; y < (ssize_t) image->rows; y++)
854 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
855 if (p == (const Quantum *) NULL)
858 if (image->storage_class == PseudoClass)
860 for (x=0; x < (ssize_t) image->columns; x++)
862 *q++=(unsigned char) GetPixelIndex(image,p);
863 p+=GetPixelChannels(image);
867 for (x=0; x < (ssize_t) image->columns; x++)
869 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
870 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
871 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
872 p+=GetPixelChannels(image);
874 for (x=0; x < (ssize_t) scanline_pad; x++)
876 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
877 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
879 if (status == MagickFalse)
882 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
883 (void) CloseBlob(image);