blob: 7ae5691ed61721f05657cc9b236e93bd28a20516 [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
165 long
166 y;
167
168 MagickBooleanType
169 authentic_colormap;
170
171 MagickStatusType
172 status;
173
174 register IndexPacket
175 *indexes;
176
177 register long
178 x;
179
180 register PixelPacket
181 *q;
182
183 register long
184 i;
185
186 register unsigned long
187 pixel;
188
189 size_t
190 length;
191
192 ssize_t
193 count;
194
195 unsigned long
196 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");
299 for (i=0; i < (long) header.ncolors; i++)
300 {
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)
317 for (i=0; i < (long) header.ncolors; i++)
318 {
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 */
350 image->columns=(unsigned long) ximage->width;
351 image->rows=(unsigned long) 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;
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 {
364 register unsigned long
365 color;
366
367 unsigned long
368 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))
403 for (y=0; y < (long) image->rows; y++)
404 {
405 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
406 if (q == (PixelPacket *) NULL)
407 break;
408 for (x=0; x < (long) image->columns; x++)
409 {
410 pixel=XGetPixel(ximage,(int) x,(int) y);
411 index=(IndexPacket) ((pixel >> red_shift) & red_mask);
412 q->red=ScaleShortToQuantum(colors[(long) index].red);
413 index=(IndexPacket) ((pixel >> green_shift) & green_mask);
414 q->green=ScaleShortToQuantum(colors[(long) index].green);
415 index=(IndexPacket) ((pixel >> blue_shift) & blue_mask);
416 q->blue=ScaleShortToQuantum(colors[(long) index].blue);
417 q++;
418 }
419 if (SyncAuthenticPixels(image,exception) == MagickFalse)
420 break;
421 status=SetImageProgress(image,LoadImageTag,y,image->rows);
422 if (status == MagickFalse)
423 break;
424 }
425 else
426 for (y=0; y < (long) image->rows; y++)
427 {
428 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
429 if (q == (PixelPacket *) NULL)
430 break;
431 for (x=0; x < (long) image->columns; x++)
432 {
433 pixel=XGetPixel(ximage,(int) x,(int) y);
434 color=(pixel >> red_shift) & red_mask;
435 color=(color*65535UL)/red_mask;
436 q->red=ScaleShortToQuantum((unsigned short) color);
437 color=(pixel >> green_shift) & green_mask;
438 color=(color*65535UL)/green_mask;
439 q->green=ScaleShortToQuantum((unsigned short) color);
440 color=(pixel >> blue_shift) & blue_mask;
441 color=(color*65535UL)/blue_mask;
442 q->blue=ScaleShortToQuantum((unsigned short) color);
443 q++;
444 }
445 if (SyncAuthenticPixels(image,exception) == MagickFalse)
446 break;
447 status=SetImageProgress(image,LoadImageTag,y,image->rows);
448 if (status == MagickFalse)
449 break;
450 }
451 break;
452 }
453 case PseudoClass:
454 {
455 /*
456 Convert X image to PseudoClass packets.
457 */
458 if (AcquireImageColormap(image,image->colors) == MagickFalse)
459 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
460 for (i=0; i < (long) image->colors; i++)
461 {
462 image->colormap[i].red=ScaleShortToQuantum(colors[i].red);
463 image->colormap[i].green=ScaleShortToQuantum(colors[i].green);
464 image->colormap[i].blue=ScaleShortToQuantum(colors[i].blue);
465 }
466 for (y=0; y < (long) image->rows; y++)
467 {
468 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
469 if (q == (PixelPacket *) NULL)
470 break;
471 indexes=GetAuthenticIndexQueue(image);
472 for (x=0; x < (long) image->columns; x++)
473 {
474 index=ConstrainColormapIndex(image,XGetPixel(ximage,(int) x,
475 (int) y));
476 indexes[x]=index;
477 *q++=image->colormap[(long) index];
478 }
479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
480 break;
481 status=SetImageProgress(image,LoadImageTag,y,image->rows);
482 if (status == MagickFalse)
483 break;
484 }
485 break;
486 }
487 }
488 /*
489 Free image and colormap.
490 */
491 if (header.ncolors != 0)
492 colors=(XColor *) RelinquishMagickMemory(colors);
493 ximage->data=DestroyString(ximage->data);
494 ximage=(XImage *) RelinquishMagickMemory(ximage);
495 if (EOFBlob(image) != MagickFalse)
496 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
497 image->filename);
498 (void) CloseBlob(image);
499 return(GetFirstImageInList(image));
500}
501#endif
502
503/*
504%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505% %
506% %
507% %
508% R e g i s t e r X W D I m a g e %
509% %
510% %
511% %
512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513%
514% RegisterXWDImage() adds properties for the XWD image format to
515% the list of supported formats. The properties include the image format
516% tag, a method to read and/or write the format, whether the format
517% supports the saving of more than one frame to the same file or blob,
518% whether the format supports native in-memory I/O, and a brief
519% description of the format.
520%
521% The format of the RegisterXWDImage method is:
522%
523% unsigned long RegisterXWDImage(void)
524%
525*/
526ModuleExport unsigned long RegisterXWDImage(void)
527{
528 MagickInfo
529 *entry;
530
531 entry=SetMagickInfo("XWD");
532#if defined(MAGICKCORE_X11_DELEGATE)
533 entry->decoder=(DecodeImageHandler *) ReadXWDImage;
534 entry->encoder=(EncodeImageHandler *) WriteXWDImage;
535#endif
536 entry->magick=(IsImageFormatHandler *) IsXWD;
537 entry->adjoin=MagickFalse;
538 entry->description=ConstantString("X Windows system window dump (color)");
539 entry->module=ConstantString("XWD");
540 (void) RegisterMagickInfo(entry);
541 return(MagickImageCoderSignature);
542}
543
544/*
545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546% %
547% %
548% %
549% U n r e g i s t e r X W D I m a g e %
550% %
551% %
552% %
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554%
555% UnregisterXWDImage() removes format registrations made by the
556% XWD module from the list of supported formats.
557%
558% The format of the UnregisterXWDImage method is:
559%
560% UnregisterXWDImage(void)
561%
562*/
563ModuleExport void UnregisterXWDImage(void)
564{
565 (void) UnregisterMagickInfo("XWD");
566}
567
568#if defined(MAGICKCORE_X11_DELEGATE)
569/*
570%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571% %
572% %
573% %
574% W r i t e X W D I m a g e %
575% %
576% %
577% %
578%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579%
580% WriteXWDImage() writes an image to a file in X window dump
581% rasterfile format.
582%
583% The format of the WriteXWDImage method is:
584%
585% MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
586%
587% A description of each parameter follows.
588%
589% o image_info: the image info.
590%
591% o image: The image.
592%
593*/
594static MagickBooleanType WriteXWDImage(const ImageInfo *image_info,Image *image)
595{
596 const char
597 *value;
598
599 long
600 y;
601
602 MagickBooleanType
603 status;
604
605 register const IndexPacket
606 *indexes;
607
608 register const PixelPacket
609 *p;
610
611 register long
612 x;
613
614 register long
615 i;
616
617 register unsigned char
618 *q;
619
620 size_t
621 length;
622
623 unsigned char
624 *pixels;
625
626 unsigned long
627 bits_per_pixel,
628 bytes_per_line,
629 lsb_first,
630 scanline_pad;
631
632 XWDFileHeader
633 xwd_info;
634
635 /*
636 Open output image file.
637 */
638 assert(image_info != (const ImageInfo *) NULL);
639 assert(image_info->signature == MagickSignature);
640 assert(image != (Image *) NULL);
641 assert(image->signature == MagickSignature);
642 if (image->debug != MagickFalse)
643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
644 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
645 if (status == MagickFalse)
646 return(status);
647 if (image->colorspace != RGBColorspace)
648 (void) TransformImageColorspace(image,RGBColorspace);
649 /*
650 Initialize XWD file header.
651 */
652 (void) ResetMagickMemory(&xwd_info,0,sizeof(xwd_info));
653 xwd_info.header_size=(CARD32) sz_XWDheader;
654 value=GetImageProperty(image,"comment");
655 if (value != (const char *) NULL)
656 xwd_info.header_size+=(CARD32) strlen(value);
657 xwd_info.header_size++;
658 xwd_info.file_version=(CARD32) XWD_FILE_VERSION;
659 xwd_info.pixmap_format=(CARD32) ZPixmap;
660 xwd_info.pixmap_depth=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
661 xwd_info.pixmap_width=(CARD32) image->columns;
662 xwd_info.pixmap_height=(CARD32) image->rows;
663 xwd_info.xoffset=(CARD32) 0;
664 xwd_info.byte_order=(CARD32) MSBFirst;
665 xwd_info.bitmap_unit=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
666 xwd_info.bitmap_bit_order=(CARD32) MSBFirst;
667 xwd_info.bitmap_pad=(CARD32) (image->storage_class == DirectClass ? 32 : 8);
668 bits_per_pixel=(unsigned long) (image->storage_class == DirectClass ? 24 : 8);
669 xwd_info.bits_per_pixel=(CARD32) bits_per_pixel;
670 bytes_per_line=(CARD32) ((((xwd_info.bits_per_pixel*
671 xwd_info.pixmap_width)+((xwd_info.bitmap_pad)-1))/
672 (xwd_info.bitmap_pad))*((xwd_info.bitmap_pad) >> 3));
673 xwd_info.bytes_per_line=(CARD32) bytes_per_line;
674 xwd_info.visual_class=(CARD32)
675 (image->storage_class == DirectClass ? DirectColor : PseudoColor);
676 xwd_info.red_mask=(CARD32)
677 (image->storage_class == DirectClass ? 0xff0000 : 0);
678 xwd_info.green_mask=(CARD32)
679 (image->storage_class == DirectClass ? 0xff00 : 0);
680 xwd_info.blue_mask=(CARD32) (image->storage_class == DirectClass ? 0xff : 0);
681 xwd_info.bits_per_rgb=(CARD32) (image->storage_class == DirectClass ? 24 : 8);
682 xwd_info.colormap_entries=(CARD32)
683 (image->storage_class == DirectClass ? 256 : image->colors);
684 xwd_info.ncolors=(unsigned int)
685 (image->storage_class == DirectClass ? 0 : image->colors);
686 xwd_info.window_width=(CARD32) image->columns;
687 xwd_info.window_height=(CARD32) image->rows;
688 xwd_info.window_x=0;
689 xwd_info.window_y=0;
690 xwd_info.window_bdrwidth=(CARD32) 0;
691 /*
692 Write XWD header.
693 */
694 lsb_first=1;
695 if ((int) (*(char *) &lsb_first) != 0)
696 MSBOrderLong((unsigned char *) &xwd_info,sizeof(xwd_info));
697 (void) WriteBlob(image,sz_XWDheader,(unsigned char *) &xwd_info);
698 if (value != (const char *) NULL)
699 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
700 (void) WriteBlob(image,1,(const unsigned char *) "\0");
701 if (image->storage_class == PseudoClass)
702 {
703 XColor
704 *colors;
705
706 XWDColor
707 color;
708
709 /*
710 Dump colormap to file.
711 */
712 (void) ResetMagickMemory(&color,0,sizeof(color));
713 colors=(XColor *) AcquireQuantumMemory((size_t) image->colors,
714 sizeof(*colors));
715 if (colors == (XColor *) NULL)
716 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
717 for (i=0; i < (long) image->colors; i++)
718 {
719 colors[i].pixel=(unsigned long) i;
720 colors[i].red=ScaleQuantumToShort(image->colormap[i].red);
721 colors[i].green=ScaleQuantumToShort(image->colormap[i].green);
722 colors[i].blue=ScaleQuantumToShort(image->colormap[i].blue);
723 colors[i].flags=(char) (DoRed | DoGreen | DoBlue);
724 colors[i].pad='\0';
725 if ((int) (*(char *) &lsb_first) != 0)
726 {
727 MSBOrderLong((unsigned char *) &colors[i].pixel,
728 sizeof(colors[i].pixel));
729 MSBOrderShort((unsigned char *) &colors[i].red,
730 3*sizeof(colors[i].red));
731 }
732 }
733 for (i=0; i < (long) image->colors; i++)
734 {
735 color.pixel=(CARD32) colors[i].pixel;
736 color.red=colors[i].red;
737 color.green=colors[i].green;
738 color.blue=colors[i].blue;
739 color.flags=(CARD8) colors[i].flags;
740 (void) WriteBlob(image,sz_XWDColor,(unsigned char *) &color);
741 }
742 colors=(XColor *) RelinquishMagickMemory(colors);
743 }
744 /*
745 Allocate memory for pixels.
746 */
747 length=3*bytes_per_line;
748 if (image->storage_class == PseudoClass)
749 length=bytes_per_line;
750 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
751 if (pixels == (unsigned char *) NULL)
752 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
753 (void) ResetMagickMemory(pixels,0,length);
754 /*
755 Convert MIFF to XWD raster pixels.
756 */
757 scanline_pad=(bytes_per_line-((image->columns*bits_per_pixel) >> 3));
758 for (y=0; y < (long) image->rows; y++)
759 {
760 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
761 if (p == (const PixelPacket *) NULL)
762 break;
763 q=pixels;
764 if (image->storage_class == PseudoClass)
765 {
766 indexes=GetVirtualIndexQueue(image);
767 for (x=0; x < (long) image->columns; x++)
768 *q++=(unsigned char) indexes[x];
769 }
770 else
771 for (x=0; x < (long) image->columns; x++)
772 {
cristyce70c172010-01-07 17:15:30 +0000773 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
774 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
775 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000776 p++;
777 }
778 for (x=0; x < (long) scanline_pad; x++)
779 *q++='\0';
780 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
781 status=SetImageProgress(image,SaveImageTag,y,image->rows);
782 if (status == MagickFalse)
783 break;
784 }
785 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
786 (void) CloseBlob(image);
787 return(MagickTrue);
788}
789#endif