blob: 690f7ef87879f9dd55a6365da6ee807af6e7dd69 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
46#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000047#include "magick/colormap.h"
cristy316d5172009-09-17 19:31:25 +000048#include "magick/colormap-private.h"
cristy3ed852e2009-09-05 21:47:34 +000049#include "magick/colorspace.h"
50#include "magick/exception.h"
51#include "magick/exception-private.h"
52#include "magick/image.h"
53#include "magick/image-private.h"
54#include "magick/list.h"
55#include "magick/magick.h"
56#include "magick/memory_.h"
57#include "magick/monitor.h"
58#include "magick/monitor-private.h"
59#include "magick/property.h"
60#include "magick/quantum-private.h"
61#include "magick/static.h"
62#include "magick/string_.h"
63#include "magick/module.h"
64#if defined(MAGICKCORE_X11_DELEGATE)
65#include "magick/xwindow-private.h"
66#if !defined(vms)
67#include <X11/XWDFile.h>
68#else
69#include "XWDFile.h"
70#endif
71#endif
72
73/*
74 Forward declarations.
75*/
76#if defined(MAGICKCORE_X11_DELEGATE)
77static MagickBooleanType
78 WriteXWDImage(const ImageInfo *,Image *);
79#endif
80
81/*
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83% %
84% %
85% %
86% I s X W D %
87% %
88% %
89% %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91%
92% IsXWD() returns MagickTrue if the image format type, identified by the
93% magick string, is XWD.
94%
95% The format of the IsXWD method is:
96%
97% MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
98%
99% A description of each parameter follows:
100%
101% o magick: compare image format pattern against these bytes.
102%
103% o length: Specifies the length of the magick string.
104%
105*/
106static MagickBooleanType IsXWD(const unsigned char *magick,const size_t length)
107{
108 if (length < 8)
109 return(MagickFalse);
110 if (memcmp(magick+1,"\000\000",2) == 0)
111 {
112 if (memcmp(magick+4,"\007\000\000",3) == 0)
113 return(MagickTrue);
114 if (memcmp(magick+5,"\000\000\007",3) == 0)
115 return(MagickTrue);
116 }
117 return(MagickFalse);
118}
119
120#if defined(MAGICKCORE_X11_DELEGATE)
121/*
122%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123% %
124% %
125% %
126% R e a d X W D I m a g e %
127% %
128% %
129% %
130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131%
132% ReadXWDImage() reads an X Window System window dump image file and
133% returns it. It allocates the memory necessary for the new Image structure
134% and returns a pointer to the new image.
135%
136% The format of the ReadXWDImage method is:
137%
138% Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
139%
140% A description of each parameter follows:
141%
142% o image_info: the image info.
143%
144% o exception: return any errors or warnings in this structure.
145%
146*/
147
148static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
149{
150#define CheckOverflowException(length,width,height) \
151 (((height) != 0) && ((length)/((size_t) height) != ((size_t) width)))
152
153 char
154 *comment;
155
156 Image
157 *image;
158
159 IndexPacket
160 index;
161
162 int
163 x_status;
164
cristybb503372010-05-27 20:51:26 +0000165 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000166 y;
167
168 MagickBooleanType
169 authentic_colormap;
170
171 MagickStatusType
172 status;
173
174 register IndexPacket
175 *indexes;
176
cristybb503372010-05-27 20:51:26 +0000177 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000178 x;
179
180 register PixelPacket
181 *q;
182
cristybb503372010-05-27 20:51:26 +0000183 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000184 i;
185
cristybb503372010-05-27 20:51:26 +0000186 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000187 pixel;
188
189 size_t
190 length;
191
192 ssize_t
193 count;
194
cristybb503372010-05-27 20:51:26 +0000195 size_t
cristy3ed852e2009-09-05 21:47:34 +0000196 lsb_first;
197
198 XColor
199 *colors;
200
201 XImage
202 *ximage;
203
204 XWDFileHeader
205 header;
206
207 /*
208 Open image file.
209 */
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)
220 {
221 image=DestroyImageList(image);
222 return((Image *) NULL);
223 }
224 /*
225 Read in header information.
226 */
227 count=ReadBlob(image,sz_XWDheader,(unsigned char *) &header);
228 if (count == 0)
229 ThrowReaderException(CorruptImageError,"UnableToReadImageHeader");
230 image->columns=header.pixmap_width;
231 image->rows=header.pixmap_height;
232 image->depth=8;
233 /*
234 Ensure the header byte-order is most-significant byte first.
235 */
236 lsb_first=1;
237 if ((int) (*(char *) &lsb_first) != 0)
238 MSBOrderLong((unsigned char *) &header,sz_XWDheader);
239 /*
240 Check to see if the dump file is in the proper format.
241 */
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");
256 /*
257 Initialize the X image.
258 */
cristy90823212009-12-12 20:48:33 +0000259 ximage=(XImage *) AcquireAlignedMemory(1,sizeof(*ximage));
cristy3ed852e2009-09-05 21:47:34 +0000260 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);
283 if (x_status == 0)
284 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
285 /*
286 Read colormap.
287 */
288 authentic_colormap=MagickFalse;
289 colors=(XColor *) NULL;
290 if (header.ncolors != 0)
291 {
292 XWDColor
293 color;
294
295 length=(size_t) header.ncolors;
296 colors=(XColor *) AcquireQuantumMemory(length,sizeof(*colors));
297 if (colors == (XColor *) NULL)
298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000299 for (i=0; i < (ssize_t) header.ncolors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
301 count=ReadBlob(image,sz_XWDColor,(unsigned char *) &color);
302 if (count == 0)
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;
311 }
312 /*
313 Ensure the header byte-order is most-significant byte first.
314 */
315 lsb_first=1;
316 if ((int) (*(char *) &lsb_first) != 0)
cristybb503372010-05-27 20:51:26 +0000317 for (i=0; i < (ssize_t) header.ncolors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000318 {
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));
323 }
324 }
325 /*
326 Allocate the pixel buffer.
327 */
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)
332 {
333 size_t
334 extent;
335
336 extent=length;
337 length*=ximage->depth;
338 if (CheckOverflowException(length,extent,ximage->depth))
339 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
340 }
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);
345 if (count == 0)
346 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
347 /*
348 Convert image to MIFF format.
349 */
cristybb503372010-05-27 20:51:26 +0000350 image->columns=(size_t) ximage->width;
351 image->rows=(size_t) ximage->height;
cristy3ed852e2009-09-05 21:47:34 +0000352 if ((colors == (XColor *) NULL) || (ximage->red_mask != 0) ||
353 (ximage->green_mask != 0) || (ximage->blue_mask != 0))
354 image->storage_class=DirectClass;
355 else
356 image->storage_class=PseudoClass;
357 image->colors=header.ncolors;
358 if (image_info->ping == MagickFalse)
359 switch (image->storage_class)
360 {
361 case DirectClass:
362 default:
363 {
cristybb503372010-05-27 20:51:26 +0000364 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000365 color;
366
cristybb503372010-05-27 20:51:26 +0000367 size_t
cristy3ed852e2009-09-05 21:47:34 +0000368 blue_mask,
369 blue_shift,
370 green_mask,
371 green_shift,
372 red_mask,
373 red_shift;
374
375 /*
376 Determine shift and mask for red, green, and blue.
377 */
378 red_mask=ximage->red_mask;
379 red_shift=0;
380 while ((red_mask != 0) && ((red_mask & 0x01) == 0))
381 {
382 red_mask>>=1;
383 red_shift++;
384 }
385 green_mask=ximage->green_mask;
386 green_shift=0;
387 while ((green_mask != 0) && ((green_mask & 0x01) == 0))
388 {
389 green_mask>>=1;
390 green_shift++;
391 }
392 blue_mask=ximage->blue_mask;
393 blue_shift=0;
394 while ((blue_mask != 0) && ((blue_mask & 0x01) == 0))
395 {
396 blue_mask>>=1;
397 blue_shift++;
398 }
399 /*
400 Convert X image to DirectClass packets.
401 */
402 if ((image->colors != 0) && (authentic_colormap != MagickFalse))
cristybb503372010-05-27 20:51:26 +0000403 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000404 {
405 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
406 if (q == (PixelPacket *) NULL)
407 break;
cristybb503372010-05-27 20:51:26 +0000408 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000409 {
410 pixel=XGetPixel(ximage,(int) x,(int) y);
411 index=(IndexPacket) ((pixel >> red_shift) & red_mask);
cristybb503372010-05-27 20:51:26 +0000412 q->red=ScaleShortToQuantum(colors[(ssize_t) index].red);
cristy3ed852e2009-09-05 21:47:34 +0000413 index=(IndexPacket) ((pixel >> green_shift) & green_mask);
cristybb503372010-05-27 20:51:26 +0000414 q->green=ScaleShortToQuantum(colors[(ssize_t) index].green);
cristy3ed852e2009-09-05 21:47:34 +0000415 index=(IndexPacket) ((pixel >> blue_shift) & blue_mask);
cristybb503372010-05-27 20:51:26 +0000416 q->blue=ScaleShortToQuantum(colors[(ssize_t) index].blue);
cristy3ed852e2009-09-05 21:47:34 +0000417 q++;
418 }
419 if (SyncAuthenticPixels(image,exception) == MagickFalse)
420 break;
cristycee97112010-05-28 00:44:52 +0000421 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
422 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000423 if (status == MagickFalse)
424 break;
425 }
426 else
cristybb503372010-05-27 20:51:26 +0000427 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000428 {
429 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
430 if (q == (PixelPacket *) NULL)
431 break;
cristybb503372010-05-27 20:51:26 +0000432 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000433 {
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);
444 q++;
445 }
446 if (SyncAuthenticPixels(image,exception) == MagickFalse)
447 break;
cristycee97112010-05-28 00:44:52 +0000448 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
449 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000450 if (status == MagickFalse)
451 break;
452 }
453 break;
454 }
455 case PseudoClass:
456 {
457 /*
458 Convert X image to PseudoClass packets.
459 */
460 if (AcquireImageColormap(image,image->colors) == MagickFalse)
461 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000462 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000463 {
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);
467 }
cristybb503372010-05-27 20:51:26 +0000468 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
470 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
471 if (q == (PixelPacket *) NULL)
472 break;
473 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000474 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000475 {
476 index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
477 (int) y));
478 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +0000479 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +0000480 }
481 if (SyncAuthenticPixels(image,exception) == MagickFalse)
482 break;
cristycee97112010-05-28 00:44:52 +0000483 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
484 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000485 if (status == MagickFalse)
486 break;
487 }
488 break;
489 }
490 }
491 /*
492 Free image and colormap.
493 */
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",
500 image->filename);
501 (void) CloseBlob(image);
502 return(GetFirstImageInList(image));
503}
504#endif
505
506/*
507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508% %
509% %
510% %
511% R e g i s t e r X W D I m a g e %
512% %
513% %
514% %
515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516%
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.
523%
524% The format of the RegisterXWDImage method is:
525%
cristybb503372010-05-27 20:51:26 +0000526% size_t RegisterXWDImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000527%
528*/
cristybb503372010-05-27 20:51:26 +0000529ModuleExport size_t RegisterXWDImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000530{
531 MagickInfo
532 *entry;
533
534 entry=SetMagickInfo("XWD");
535#if defined(MAGICKCORE_X11_DELEGATE)
536 entry->decoder=(DecodeImageHandler *) ReadXWDImage;
537 entry->encoder=(EncodeImageHandler *) WriteXWDImage;
538#endif
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);
545}
546
547/*
548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549% %
550% %
551% %
552% U n r e g i s t e r X W D I m a g e %
553% %
554% %
555% %
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557%
558% UnregisterXWDImage() removes format registrations made by the
559% XWD module from the list of supported formats.
560%
561% The format of the UnregisterXWDImage method is:
562%
563% UnregisterXWDImage(void)
564%
565*/
566ModuleExport void UnregisterXWDImage(void)
567{
568 (void) UnregisterMagickInfo("XWD");
569}
570
571#if defined(MAGICKCORE_X11_DELEGATE)
572/*
573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574% %
575% %
576% %
577% W r i t e X W D I m a g e %
578% %
579% %
580% %
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582%
583% WriteXWDImage() writes an image to a file in X window dump
584% rasterfile format.
585%
586% The format of the WriteXWDImage method is:
587%
588% MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
589%
590% A description of each parameter follows.
591%
592% o image_info: the image info.
593%
594% o image: The image.
595%
596*/
597static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
598{
599 const char
600 *value;
601
cristybb503372010-05-27 20:51:26 +0000602 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000603 y;
604
605 MagickBooleanType
606 status;
607
608 register const IndexPacket
609 *indexes;
610
611 register const PixelPacket
612 *p;
613
cristybb503372010-05-27 20:51:26 +0000614 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000615 x;
616
cristybb503372010-05-27 20:51:26 +0000617 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000618 i;
619
620 register unsigned char
621 *q;
622
623 size_t
624 length;
625
626 unsigned char
627 *pixels;
628
cristybb503372010-05-27 20:51:26 +0000629 size_t
cristy3ed852e2009-09-05 21:47:34 +0000630 bits_per_pixel,
631 bytes_per_line,
632 lsb_first,
633 scanline_pad;
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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
648 if (status == MagickFalse)
649 return(status);
650 if (image->colorspace != RGBColorspace)
651 (void) TransformImageColorspace(image,RGBColorspace);
652 /*
653 Initialize XWD file header.
654 */
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);
cristybb503372010-05-27 20:51:26 +0000671 bits_per_pixel=(size_t) (image->storage_class == DirectClass ? 24 : 8);
cristy3ed852e2009-09-05 21:47:34 +0000672 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;
691 xwd_info.window_x=0;
692 xwd_info.window_y=0;
693 xwd_info.window_bdrwidth=(CARD32) 0;
694 /*
695 Write XWD header.
696 */
697 lsb_first=1;
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)
705 {
706 XColor
707 *colors;
708
709 XWDColor
710 color;
711
712 /*
713 Dump colormap to file.
714 */
715 (void) ResetMagickMemory(&color,0,sizeof(color));
716 colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
717 sizeof(*colors));
718 if (colors == (XColor *) NULL)
719 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000720 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000721 {
cristybb503372010-05-27 20:51:26 +0000722 colors[i].pixel=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000723 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);
727 colors[i].pad='\0';
728 if ((int) (*(char *) &lsb_first) != 0)
729 {
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));
734 }
735 }
cristybb503372010-05-27 20:51:26 +0000736 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000737 {
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);
744 }
745 colors=(XColor *) RelinquishMagickMemory(colors);
746 }
747 /*
748 Allocate memory for pixels.
749 */
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);
757 /*
758 Convert MIFF to XWD raster pixels.
759 */
760 scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
cristybb503372010-05-27 20:51:26 +0000761 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000762 {
763 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
764 if (p == (const PixelPacket *) NULL)
765 break;
766 q=pixels;
767 if (image->storage_class == PseudoClass)
768 {
769 indexes=GetVirtualIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000770 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000771 *q++=(unsigned char) indexes[x];
772 }
773 else
cristybb503372010-05-27 20:51:26 +0000774 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000775 {
cristyce70c172010-01-07 17:15:30 +0000776 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
777 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
778 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000779 p++;
780 }
cristybb503372010-05-27 20:51:26 +0000781 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000782 *q++='\0';
783 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
cristycee97112010-05-28 00:44:52 +0000784 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
785 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000786 if (status == MagickFalse)
787 break;
788 }
789 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
790 (void) CloseBlob(image);
791 return(MagickTrue);
792}
793#endif