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-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/property.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/string_.h"
62 #include "magick/module.h"
63 #if defined(MAGICKCORE_X11_DELEGATE)
64 #include "magick/xwindow-private.h"
66 #include <X11/XWDFile.h>
75 #if defined(MAGICKCORE_X11_DELEGATE)
76 static MagickBooleanType
77 WriteXWDImage(const ImageInfo *,Image *);
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % IsXWD() returns MagickTrue if the image format type, identified by the
92 % magick string, is XWD.
94 % The format of the IsXWD method is:
96 % MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
98 % A description of each parameter follows:
100 % o magick: compare image format pattern against these bytes.
102 % o length: Specifies the length of the magick string.
105 static MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
109 if (memcmp(magick+1,"\000\000",2) == 0)
111 if (memcmp(magick+4,"\007\000\000",3) == 0)
113 if (memcmp(magick+5,"\000\000\007",3) == 0)
119 #if defined(MAGICKCORE_X11_DELEGATE)
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % R e a d X W D I m a g e %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 % ReadXWDImage() reads an X Window System window dump image file and
132 % returns it. It allocates the memory necessary for the new Image structure
133 % and returns a pointer to the new image.
135 % The format of the ReadXWDImage method is:
137 % Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
139 % A description of each parameter follows:
141 % o image_info: the image info.
143 % o exception: return any errors or warnings in this structure.
147 static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
149 #define CheckOverflowException(length,width,height) \
150 (((height) != 0) && ((length)/((size_t) height) != ((size_t) width)))
185 register unsigned long
209 assert(image_info != (const ImageInfo *) NULL);
210 assert(image_info->signature == MagickSignature);
211 if (image_info->debug != MagickFalse)
212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
213 image_info->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 image=AcquireImage(image_info);
217 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
218 if (status == MagickFalse)
220 image=DestroyImageList(image);
221 return((Image *) NULL);
224 Read in header information.
226 count=ReadBlob(image,sz_XWDheader,(unsigned char *) &header);
228 ThrowReaderException(CorruptImageError,"UnableToReadImageHeader");
229 image->columns=header.pixmap_width;
230 image->rows=header.pixmap_height;
233 Ensure the header byte-order is most-significant byte first.
236 if ((int) (*(char *) &lsb_first) != 0)
237 MSBOrderLong((unsigned char *) &header,sz_XWDheader);
239 Check to see if the dump file is in the proper format.
241 if (header.file_version != XWD_FILE_VERSION)
242 ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
243 if (header.header_size < sz_XWDheader)
244 ThrowReaderException(CorruptImageError,"CorruptImage");
245 length=(size_t) header.header_size-sz_XWDheader;
246 comment=(char *) AcquireQuantumMemory(length+1,sizeof(*comment));
247 if (comment == (char *) NULL)
248 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
249 count=ReadBlob(image,length,(unsigned char *) comment);
250 comment[length]='\0';
251 (void) SetImageProperty(image,"comment",comment);
252 comment=DestroyString(comment);
253 if (count != (ssize_t) length)
254 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
256 Initialize the X image.
258 ximage=(XImage *) AcquireAlignedMemory(1,sizeof(*ximage));
259 if (ximage == (XImage *) NULL)
260 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
261 ximage->depth=(int) header.pixmap_depth;
262 ximage->format=(int) header.pixmap_format;
263 ximage->xoffset=(int) header.xoffset;
264 ximage->data=(char *) NULL;
265 ximage->width=(int) header.pixmap_width;
266 ximage->height=(int) header.pixmap_height;
267 ximage->bitmap_pad=(int) header.bitmap_pad;
268 ximage->bytes_per_line=(int) header.bytes_per_line;
269 ximage->byte_order=(int) header.byte_order;
270 ximage->bitmap_unit=(int) header.bitmap_unit;
271 ximage->bitmap_bit_order=(int) header.bitmap_bit_order;
272 ximage->bits_per_pixel=(int) header.bits_per_pixel;
273 ximage->red_mask=header.red_mask;
274 ximage->green_mask=header.green_mask;
275 ximage->blue_mask=header.blue_mask;
276 if ((ximage->depth < 0) || (ximage->width < 0) || (ximage->height < 0) ||
277 (ximage->bitmap_pad < 0) || (ximage->bytes_per_line < 0))
278 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
279 if ((ximage->bits_per_pixel > 32) || (ximage->bitmap_unit > 32))
280 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
281 x_status=XInitImage(ximage);
283 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
287 authentic_colormap=MagickFalse;
288 colors=(XColor *) NULL;
289 if (header.ncolors != 0)
294 length=(size_t) header.ncolors;
295 colors=(XColor *) AcquireQuantumMemory(length,sizeof(*colors));
296 if (colors == (XColor *) NULL)
297 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
298 for (i=0; i < (long) header.ncolors; i++)
300 count=ReadBlob(image,sz_XWDColor,(unsigned char *) &color);
302 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
303 colors[i].pixel=color.pixel;
304 colors[i].red=color.red;
305 colors[i].green=color.green;
306 colors[i].blue=color.blue;
307 colors[i].flags=(char) color.flags;
308 if (color.flags != 0)
309 authentic_colormap=MagickTrue;
312 Ensure the header byte-order is most-significant byte first.
315 if ((int) (*(char *) &lsb_first) != 0)
316 for (i=0; i < (long) header.ncolors; i++)
318 MSBOrderLong((unsigned char *) &colors[i].pixel,
319 sizeof(colors[i].pixel));
320 MSBOrderShort((unsigned char *) &colors[i].red,3*
321 sizeof(colors[i].red));
325 Allocate the pixel buffer.
327 length=(size_t) ximage->bytes_per_line*ximage->height;
328 if (CheckOverflowException(length,ximage->bytes_per_line,ximage->height))
329 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
330 if (ximage->format != ZPixmap)
336 length*=ximage->depth;
337 if (CheckOverflowException(length,extent,ximage->depth))
338 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
340 ximage->data=(char *) AcquireQuantumMemory(length,sizeof(*ximage->data));
341 if (ximage->data == (char *) NULL)
342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
343 count=ReadBlob(image,length,(unsigned char *) ximage->data);
345 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
347 Convert image to MIFF format.
349 image->columns=(unsigned long) ximage->width;
350 image->rows=(unsigned long) ximage->height;
351 if ((colors == (XColor *) NULL) || (ximage->red_mask != 0) ||
352 (ximage->green_mask != 0) || (ximage->blue_mask != 0))
353 image->storage_class=DirectClass;
355 image->storage_class=PseudoClass;
356 image->colors=header.ncolors;
357 if (image_info->ping == MagickFalse)
358 switch (image->storage_class)
363 register unsigned long
375 Determine shift and mask for red, green, and blue.
377 red_mask=ximage->red_mask;
379 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
384 green_mask=ximage->green_mask;
386 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
391 blue_mask=ximage->blue_mask;
393 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
399 Convert X image to DirectClass packets.
401 if ((image->colors != 0) && (authentic_colormap != MagickFalse))
402 for (y=0; y < (long) image->rows; y++)
404 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
405 if (q == (PixelPacket *) NULL)
407 for (x=0; x < (long) image->columns; x++)
409 pixel=XGetPixel(ximage,(int) x,(int) y);
410 index=(IndexPacket) ((pixel >> red_shift) & red_mask);
411 q->red=ScaleShortToQuantum(colors[(long) index].red);
412 index=(IndexPacket) ((pixel >> green_shift) & green_mask);
413 q->green=ScaleShortToQuantum(colors[(long) index].green);
414 index=(IndexPacket) ((pixel >> blue_shift) & blue_mask);
415 q->blue=ScaleShortToQuantum(colors[(long) index].blue);
418 if (SyncAuthenticPixels(image,exception) == MagickFalse)
420 status=SetImageProgress(image,LoadImageTag,y,image->rows);
421 if (status == MagickFalse)
425 for (y=0; y < (long) image->rows; y++)
427 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
428 if (q == (PixelPacket *) NULL)
430 for (x=0; x < (long) image->columns; x++)
432 pixel=XGetPixel(ximage,(int) x,(int) y);
433 color=(pixel >> red_shift) & red_mask;
434 color=(color*65535UL)/red_mask;
435 q->red=ScaleShortToQuantum((unsigned short) color);
436 color=(pixel >> green_shift) & green_mask;
437 color=(color*65535UL)/green_mask;
438 q->green=ScaleShortToQuantum((unsigned short) color);
439 color=(pixel >> blue_shift) & blue_mask;
440 color=(color*65535UL)/blue_mask;
441 q->blue=ScaleShortToQuantum((unsigned short) color);
444 if (SyncAuthenticPixels(image,exception) == MagickFalse)
446 status=SetImageProgress(image,LoadImageTag,y,image->rows);
447 if (status == MagickFalse)
455 Convert X image to PseudoClass packets.
457 if (AcquireImageColormap(image,image->colors) == MagickFalse)
458 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
459 for (i=0; i < (long) image->colors; i++)
461 image->colormap[i].red=ScaleShortToQuantum(colors[i].red);
462 image->colormap[i].green=ScaleShortToQuantum(colors[i].green);
463 image->colormap[i].blue=ScaleShortToQuantum(colors[i].blue);
465 for (y=0; y < (long) image->rows; y++)
467 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
468 if (q == (PixelPacket *) NULL)
470 indexes=GetAuthenticIndexQueue(image);
471 for (x=0; x < (long) image->columns; x++)
473 index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
476 *q++=image->colormap[(long) index];
478 if (SyncAuthenticPixels(image,exception) == MagickFalse)
480 status=SetImageProgress(image,LoadImageTag,y,image->rows);
481 if (status == MagickFalse)
488 Free image and colormap.
490 if (header.ncolors != 0)
491 colors=(XColor *) RelinquishMagickMemory(colors);
492 ximage->data=DestroyString(ximage->data);
493 ximage=(XImage *) RelinquishMagickMemory(ximage);
494 if (EOFBlob(image) != MagickFalse)
495 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
497 (void) CloseBlob(image);
498 return(GetFirstImageInList(image));
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507 % R e g i s t e r X W D I m a g e %
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 % RegisterXWDImage() adds properties for the XWD image format to
514 % the list of supported formats. The properties include the image format
515 % tag, a method to read and/or write the format, whether the format
516 % supports the saving of more than one frame to the same file or blob,
517 % whether the format supports native in-memory I/O, and a brief
518 % description of the format.
520 % The format of the RegisterXWDImage method is:
522 % unsigned long RegisterXWDImage(void)
525 ModuleExport unsigned long RegisterXWDImage(void)
530 entry=SetMagickInfo("XWD");
531 #if defined(MAGICKCORE_X11_DELEGATE)
532 entry->decoder=(DecodeImageHandler *) ReadXWDImage;
533 entry->encoder=(EncodeImageHandler *) WriteXWDImage;
535 entry->magick=(IsImageFormatHandler *) IsXWD;
536 entry->adjoin=MagickFalse;
537 entry->description=ConstantString("X Windows system window dump (color)");
538 entry->module=ConstantString("XWD");
539 (void) RegisterMagickInfo(entry);
540 return(MagickImageCoderSignature);
544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % U n r e g i s t e r X W D I m a g e %
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % UnregisterXWDImage() removes format registrations made by the
555 % XWD module from the list of supported formats.
557 % The format of the UnregisterXWDImage method is:
559 % UnregisterXWDImage(void)
562 ModuleExport void UnregisterXWDImage(void)
564 (void) UnregisterMagickInfo("XWD");
567 #if defined(MAGICKCORE_X11_DELEGATE)
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % W r i t e X W D I m a g e %
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % WriteXWDImage() writes an image to a file in X window dump
582 % The format of the WriteXWDImage method is:
584 % MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
586 % A description of each parameter follows.
588 % o image_info: the image info.
590 % o image: The image.
593 static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
604 register const IndexPacket
607 register const PixelPacket
616 register unsigned char
635 Open output image file.
637 assert(image_info != (const ImageInfo *) NULL);
638 assert(image_info->signature == MagickSignature);
639 assert(image != (Image *) NULL);
640 assert(image->signature == MagickSignature);
641 if (image->debug != MagickFalse)
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
643 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
644 if (status == MagickFalse)
646 if (image->colorspace != RGBColorspace)
647 (void) TransformImageColorspace(image,RGBColorspace);
649 Initialize XWD file header.
651 (void) ResetMagickMemory(&xwd_info,0,sizeof(xwd_info));
652 xwd_info.header_size=(CARD32) sz_XWDheader;
653 value=GetImageProperty(image,"comment");
654 if (value != (const char *) NULL)
655 xwd_info.header_size+=(CARD32) strlen(value);
656 xwd_info.header_size++;
657 xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
658 xwd_info.pixmap_format=(CARD32) ZPixmap;
659 xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
660 xwd_info.pixmap_width=(CARD32) image->columns;
661 xwd_info.pixmap_height=(CARD32) image->rows;
662 xwd_info.xoffset=(CARD32) 0;
663 xwd_info.byte_order=(CARD32) MSBFirst;
664 xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
665 xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
666 xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
667 bits_per_pixel=(unsigned long) (image->storage_class == DirectClass ? 24 : 8);
668 xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
669 bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
670 xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
671 (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
672 xwd_info.bytes_per_line=(CARD32) bytes_per_line;
673 xwd_info.visual_class=(CARD32)
674 (image->storage_class == DirectClass ? DirectColor : PseudoColor);
675 xwd_info.red_mask=(CARD32)
676 (image->storage_class == DirectClass ? 0xff0000 : 0);
677 xwd_info.green_mask=(CARD32)
678 (image->storage_class == DirectClass ? 0xff00 : 0);
679 xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
680 xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
681 xwd_info.colormap_entries=(CARD32)
682 (image->storage_class == DirectClass ? 256 : image->colors);
683 xwd_info.ncolors=(unsigned int)
684 (image->storage_class == DirectClass ? 0 : image->colors);
685 xwd_info.window_width=(CARD32) image->columns;
686 xwd_info.window_height=(CARD32) image->rows;
689 xwd_info.window_bdrwidth=(CARD32) 0;
694 if ((int) (*(char *) &lsb_first) != 0)
695 MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
696 (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
697 if (value != (const char *) NULL)
698 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
699 (void) WriteBlob(image,1,(const unsigned char *) "\0");
700 if (image->storage_class == PseudoClass)
709 Dump colormap to file.
711 (void) ResetMagickMemory(&color,0,sizeof(color));
712 colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
714 if (colors == (XColor *) NULL)
715 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
716 for (i=0; i < (long) image->colors; i++)
718 colors[i].pixel=(unsigned long) i;
719 colors[i].red=ScaleQuantumToShort(image->colormap[i].red);
720 colors[i].green=ScaleQuantumToShort(image->colormap[i].green);
721 colors[i].blue=ScaleQuantumToShort(image->colormap[i].blue);
722 colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
724 if ((int) (*(char *) &lsb_first) != 0)
726 MSBOrderLong((unsigned char *) &colors[i].pixel,
727 sizeof(colors[i].pixel));
728 MSBOrderShort((unsigned char *) &colors[i].red,
729 3*sizeof(colors[i].red));
732 for (i=0; i < (long) image->colors; i++)
734 color.pixel=(CARD32) colors[i].pixel;
735 color.red=colors[i].red;
736 color.green=colors[i].green;
737 color.blue=colors[i].blue;
738 color.flags=(CARD8) colors[i].flags;
739 (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
741 colors=(XColor *) RelinquishMagickMemory(colors);
744 Allocate memory for pixels.
746 length=3*bytes_per_line;
747 if (image->storage_class == PseudoClass)
748 length=bytes_per_line;
749 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
750 if (pixels == (unsigned char *) NULL)
751 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
752 (void) ResetMagickMemory(pixels,0,length);
754 Convert MIFF to XWD raster pixels.
756 scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
757 for (y=0; y < (long) image->rows; y++)
759 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
760 if (p == (const PixelPacket *) NULL)
763 if (image->storage_class == PseudoClass)
765 indexes=GetVirtualIndexQueue(image);
766 for (x=0; x < (long) image->columns; x++)
767 *q++=(unsigned char) indexes[x];
770 for (x=0; x < (long) image->columns; x++)
772 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
773 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
774 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
777 for (x=0; x < (long) scanline_pad; x++)
779 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
780 status=SetImageProgress(image,SaveImageTag,y,image->rows);
781 if (status == MagickFalse)
784 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
785 (void) CloseBlob(image);