2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write X Windows System Window Dump Format %
20 % Copyright 1999-2010 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 % http://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 "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"
67 #include <X11/XWDFile.h>
76 #if defined(MAGICKCORE_X11_DELEGATE)
77 static MagickBooleanType
78 WriteXWDImage(const ImageInfo *,Image *);
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % IsXWD() returns MagickTrue if the image format type, identified by the
93 % magick string, is XWD.
95 % The format of the IsXWD method is:
97 % MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
99 % A description of each parameter follows:
101 % o magick: compare image format pattern against these bytes.
103 % o length: Specifies the length of the magick string.
106 static MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
110 if (memcmp(magick+1,"\000\000",2) == 0)
112 if (memcmp(magick+4,"\007\000\000",3) == 0)
114 if (memcmp(magick+5,"\000\000\007",3) == 0)
120 #if defined(MAGICKCORE_X11_DELEGATE)
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % R e a d X W D I m a g e %
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
136 % The format of the ReadXWDImage method is:
138 % Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
140 % A description of each parameter follows:
142 % o image_info: the image info.
144 % o exception: return any errors or warnings in this structure.
148 static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
150 #define CheckOverflowException(length,width,height) \
151 (((height) != 0) && ((length)/((size_t) height) != ((size_t) width)))
210 assert(image_info != (const ImageInfo *) NULL);
211 assert(image_info->signature == MagickSignature);
212 if (image_info->debug != MagickFalse)
213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
214 image_info->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 image=AcquireImage(image_info);
218 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
219 if (status == MagickFalse)
221 image=DestroyImageList(image);
222 return((Image *) NULL);
225 Read in header information.
227 count=ReadBlob(image,sz_XWDheader,(unsigned char *) &header);
229 ThrowReaderException(CorruptImageError,"UnableToReadImageHeader");
230 image->columns=header.pixmap_width;
231 image->rows=header.pixmap_height;
234 Ensure the header byte-order is most-significant byte first.
237 if ((int) (*(char *) &lsb_first) != 0)
238 MSBOrderLong((unsigned char *) &header,sz_XWDheader);
240 Check to see if the dump file is in the proper format.
242 if (header.file_version != XWD_FILE_VERSION)
243 ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
244 if (header.header_size < sz_XWDheader)
245 ThrowReaderException(CorruptImageError,"CorruptImage");
246 length=(size_t) header.header_size-sz_XWDheader;
247 comment=(char *) AcquireQuantumMemory(length+1,sizeof(*comment));
248 if (comment == (char *) NULL)
249 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
250 count=ReadBlob(image,length,(unsigned char *) comment);
251 comment[length]='\0';
252 (void) SetImageProperty(image,"comment",comment);
253 comment=DestroyString(comment);
254 if (count != (ssize_t) length)
255 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
257 Initialize the X image.
259 ximage=(XImage *) AcquireAlignedMemory(1,sizeof(*ximage));
260 if (ximage == (XImage *) NULL)
261 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
262 ximage->depth=(int) header.pixmap_depth;
263 ximage->format=(int) header.pixmap_format;
264 ximage->xoffset=(int) header.xoffset;
265 ximage->data=(char *) NULL;
266 ximage->width=(int) header.pixmap_width;
267 ximage->height=(int) header.pixmap_height;
268 ximage->bitmap_pad=(int) header.bitmap_pad;
269 ximage->bytes_per_line=(int) header.bytes_per_line;
270 ximage->byte_order=(int) header.byte_order;
271 ximage->bitmap_unit=(int) header.bitmap_unit;
272 ximage->bitmap_bit_order=(int) header.bitmap_bit_order;
273 ximage->bits_per_pixel=(int) header.bits_per_pixel;
274 ximage->red_mask=header.red_mask;
275 ximage->green_mask=header.green_mask;
276 ximage->blue_mask=header.blue_mask;
277 if ((ximage->depth < 0) || (ximage->width < 0) || (ximage->height < 0) ||
278 (ximage->bitmap_pad < 0) || (ximage->bytes_per_line < 0))
279 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
280 if ((ximage->bits_per_pixel > 32) || (ximage->bitmap_unit > 32))
281 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
282 x_status=XInitImage(ximage);
284 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
288 authentic_colormap=MagickFalse;
289 colors=(XColor *) NULL;
290 if (header.ncolors != 0)
295 length=(size_t) header.ncolors;
296 colors=(XColor *) AcquireQuantumMemory(length,sizeof(*colors));
297 if (colors == (XColor *) NULL)
298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
299 for (i=0; i < (ssize_t) header.ncolors; i++)
301 count=ReadBlob(image,sz_XWDColor,(unsigned char *) &color);
303 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
304 colors[i].pixel=color.pixel;
305 colors[i].red=color.red;
306 colors[i].green=color.green;
307 colors[i].blue=color.blue;
308 colors[i].flags=(char) color.flags;
309 if (color.flags != 0)
310 authentic_colormap=MagickTrue;
313 Ensure the header byte-order is most-significant byte first.
316 if ((int) (*(char *) &lsb_first) != 0)
317 for (i=0; i < (ssize_t) header.ncolors; i++)
319 MSBOrderLong((unsigned char *) &colors[i].pixel,
320 sizeof(colors[i].pixel));
321 MSBOrderShort((unsigned char *) &colors[i].red,3*
322 sizeof(colors[i].red));
326 Allocate the pixel buffer.
328 length=(size_t) ximage->bytes_per_line*ximage->height;
329 if (CheckOverflowException(length,ximage->bytes_per_line,ximage->height))
330 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
331 if (ximage->format != ZPixmap)
337 length*=ximage->depth;
338 if (CheckOverflowException(length,extent,ximage->depth))
339 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
341 ximage->data=(char *) AcquireQuantumMemory(length,sizeof(*ximage->data));
342 if (ximage->data == (char *) NULL)
343 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
344 count=ReadBlob(image,length,(unsigned char *) ximage->data);
346 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
348 Convert image to MIFF format.
350 image->columns=(size_t) ximage->width;
351 image->rows=(size_t) ximage->height;
352 if ((colors == (XColor *) NULL) || (ximage->red_mask != 0) ||
353 (ximage->green_mask != 0) || (ximage->blue_mask != 0))
354 image->storage_class=DirectClass;
356 image->storage_class=PseudoClass;
357 image->colors=header.ncolors;
358 if (image_info->ping == MagickFalse)
359 switch (image->storage_class)
376 Determine shift and mask for red, green, and blue.
378 red_mask=ximage->red_mask;
380 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
385 green_mask=ximage->green_mask;
387 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
392 blue_mask=ximage->blue_mask;
394 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
400 Convert X image to DirectClass packets.
402 if ((image->colors != 0) && (authentic_colormap != MagickFalse))
403 for (y=0; y < (ssize_t) image->rows; y++)
405 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
406 if (q == (PixelPacket *) NULL)
408 for (x=0; x < (ssize_t) image->columns; x++)
410 pixel=XGetPixel(ximage,(int) x,(int) y);
411 index=(IndexPacket) ((pixel >> red_shift) & red_mask);
412 q->red=ScaleShortToQuantum(colors[(ssize_t) index].red);
413 index=(IndexPacket) ((pixel >> green_shift) & green_mask);
414 q->green=ScaleShortToQuantum(colors[(ssize_t) index].green);
415 index=(IndexPacket) ((pixel >> blue_shift) & blue_mask);
416 q->blue=ScaleShortToQuantum(colors[(ssize_t) index].blue);
419 if (SyncAuthenticPixels(image,exception) == MagickFalse)
421 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
423 if (status == MagickFalse)
427 for (y=0; y < (ssize_t) image->rows; y++)
429 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
430 if (q == (PixelPacket *) NULL)
432 for (x=0; x < (ssize_t) image->columns; x++)
434 pixel=XGetPixel(ximage,(int) x,(int) y);
435 color=(pixel >> red_shift) & red_mask;
436 color=(color*65535UL)/red_mask;
437 q->red=ScaleShortToQuantum((unsigned short) color);
438 color=(pixel >> green_shift) & green_mask;
439 color=(color*65535UL)/green_mask;
440 q->green=ScaleShortToQuantum((unsigned short) color);
441 color=(pixel >> blue_shift) & blue_mask;
442 color=(color*65535UL)/blue_mask;
443 q->blue=ScaleShortToQuantum((unsigned short) color);
446 if (SyncAuthenticPixels(image,exception) == MagickFalse)
448 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
450 if (status == MagickFalse)
458 Convert X image to PseudoClass packets.
460 if (AcquireImageColormap(image,image->colors) == MagickFalse)
461 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
462 for (i=0; i < (ssize_t) image->colors; i++)
464 image->colormap[i].red=ScaleShortToQuantum(colors[i].red);
465 image->colormap[i].green=ScaleShortToQuantum(colors[i].green);
466 image->colormap[i].blue=ScaleShortToQuantum(colors[i].blue);
468 for (y=0; y < (ssize_t) image->rows; y++)
470 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
471 if (q == (PixelPacket *) NULL)
473 indexes=GetAuthenticIndexQueue(image);
474 for (x=0; x < (ssize_t) image->columns; x++)
476 index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
479 *q++=image->colormap[(ssize_t) index];
481 if (SyncAuthenticPixels(image,exception) == MagickFalse)
483 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
485 if (status == MagickFalse)
492 Free image and colormap.
494 if (header.ncolors != 0)
495 colors=(XColor *) RelinquishMagickMemory(colors);
496 ximage->data=DestroyString(ximage->data);
497 ximage=(XImage *) RelinquishMagickMemory(ximage);
498 if (EOFBlob(image) != MagickFalse)
499 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
501 (void) CloseBlob(image);
502 return(GetFirstImageInList(image));
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511 % R e g i s t e r X W D I m a g e %
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 % RegisterXWDImage() adds properties for the XWD image format to
518 % the list of supported formats. The properties include the image format
519 % tag, a method to read and/or write the format, whether the format
520 % supports the saving of more than one frame to the same file or blob,
521 % whether the format supports native in-memory I/O, and a brief
522 % description of the format.
524 % The format of the RegisterXWDImage method is:
526 % size_t RegisterXWDImage(void)
529 ModuleExport size_t RegisterXWDImage(void)
534 entry=SetMagickInfo("XWD");
535 #if defined(MAGICKCORE_X11_DELEGATE)
536 entry->decoder=(DecodeImageHandler *) ReadXWDImage;
537 entry->encoder=(EncodeImageHandler *) WriteXWDImage;
539 entry->magick=(IsImageFormatHandler *) IsXWD;
540 entry->adjoin=MagickFalse;
541 entry->description=ConstantString("X Windows system window dump (color)");
542 entry->module=ConstantString("XWD");
543 (void) RegisterMagickInfo(entry);
544 return(MagickImageCoderSignature);
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 % U n r e g i s t e r X W D I m a g e %
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % UnregisterXWDImage() removes format registrations made by the
559 % XWD module from the list of supported formats.
561 % The format of the UnregisterXWDImage method is:
563 % UnregisterXWDImage(void)
566 ModuleExport void UnregisterXWDImage(void)
568 (void) UnregisterMagickInfo("XWD");
571 #if defined(MAGICKCORE_X11_DELEGATE)
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % W r i t e X W D I m a g e %
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 % WriteXWDImage() writes an image to a file in X window dump
586 % The format of the WriteXWDImage method is:
588 % MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
590 % A description of each parameter follows.
592 % o image_info: the image info.
594 % o image: The image.
597 static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
608 register const IndexPacket
611 register const PixelPacket
620 register unsigned char
639 Open output image file.
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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
648 if (status == MagickFalse)
650 if (image->colorspace != RGBColorspace)
651 (void) TransformImageColorspace(image,RGBColorspace);
653 Initialize XWD file header.
655 (void) ResetMagickMemory(&xwd_info,0,sizeof(xwd_info));
656 xwd_info.header_size=(CARD32) sz_XWDheader;
657 value=GetImageProperty(image,"comment");
658 if (value != (const char *) NULL)
659 xwd_info.header_size+=(CARD32) strlen(value);
660 xwd_info.header_size++;
661 xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
662 xwd_info.pixmap_format=(CARD32) ZPixmap;
663 xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
664 xwd_info.pixmap_width=(CARD32) image->columns;
665 xwd_info.pixmap_height=(CARD32) image->rows;
666 xwd_info.xoffset=(CARD32) 0;
667 xwd_info.byte_order=(CARD32) MSBFirst;
668 xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
669 xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
670 xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
671 bits_per_pixel=(size_t) (image->storage_class == DirectClass ? 24 : 8);
672 xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
673 bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
674 xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
675 (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
676 xwd_info.bytes_per_line=(CARD32) bytes_per_line;
677 xwd_info.visual_class=(CARD32)
678 (image->storage_class == DirectClass ? DirectColor : PseudoColor);
679 xwd_info.red_mask=(CARD32)
680 (image->storage_class == DirectClass ? 0xff0000 : 0);
681 xwd_info.green_mask=(CARD32)
682 (image->storage_class == DirectClass ? 0xff00 : 0);
683 xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
684 xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
685 xwd_info.colormap_entries=(CARD32)
686 (image->storage_class == DirectClass ? 256 : image->colors);
687 xwd_info.ncolors=(unsigned int)
688 (image->storage_class == DirectClass ? 0 : image->colors);
689 xwd_info.window_width=(CARD32) image->columns;
690 xwd_info.window_height=(CARD32) image->rows;
693 xwd_info.window_bdrwidth=(CARD32) 0;
698 if ((int) (*(char *) &lsb_first) != 0)
699 MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
700 (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
701 if (value != (const char *) NULL)
702 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
703 (void) WriteBlob(image,1,(const unsigned char *) "\0");
704 if (image->storage_class == PseudoClass)
713 Dump colormap to file.
715 (void) ResetMagickMemory(&color,0,sizeof(color));
716 colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
718 if (colors == (XColor *) NULL)
719 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
720 for (i=0; i < (ssize_t) image->colors; i++)
722 colors[i].pixel=(size_t) i;
723 colors[i].red=ScaleQuantumToShort(image->colormap[i].red);
724 colors[i].green=ScaleQuantumToShort(image->colormap[i].green);
725 colors[i].blue=ScaleQuantumToShort(image->colormap[i].blue);
726 colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
728 if ((int) (*(char *) &lsb_first) != 0)
730 MSBOrderLong((unsigned char *) &colors[i].pixel,
731 sizeof(colors[i].pixel));
732 MSBOrderShort((unsigned char *) &colors[i].red,
733 3*sizeof(colors[i].red));
736 for (i=0; i < (ssize_t) image->colors; i++)
738 color.pixel=(CARD32) colors[i].pixel;
739 color.red=colors[i].red;
740 color.green=colors[i].green;
741 color.blue=colors[i].blue;
742 color.flags=(CARD8) colors[i].flags;
743 (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
745 colors=(XColor *) RelinquishMagickMemory(colors);
748 Allocate memory for pixels.
750 length=3*bytes_per_line;
751 if (image->storage_class == PseudoClass)
752 length=bytes_per_line;
753 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
754 if (pixels == (unsigned char *) NULL)
755 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
756 (void) ResetMagickMemory(pixels,0,length);
758 Convert MIFF to XWD raster pixels.
760 scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
761 for (y=0; y < (ssize_t) image->rows; y++)
763 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
764 if (p == (const PixelPacket *) NULL)
767 if (image->storage_class == PseudoClass)
769 indexes=GetVirtualIndexQueue(image);
770 for (x=0; x < (ssize_t) image->columns; x++)
771 *q++=(unsigned char) indexes[x];
774 for (x=0; x < (ssize_t) image->columns; x++)
776 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
777 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
778 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
781 for (x=0; x < (ssize_t) scanline_pad; x++)
783 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
784 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
786 if (status == MagickFalse)
789 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
790 (void) CloseBlob(image);