2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2017 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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/memory-private.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
71 typedef struct _PCXInfo
84 horizontal_resolution,
94 horizontal_screensize,
102 Forward declarations.
104 static MagickBooleanType
105 WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % IsDCX() returns MagickTrue if the image format type, identified by the
119 % magick string, is DCX.
121 % The format of the IsDCX method is:
123 % MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
125 % A description of each parameter follows:
127 % o magick: compare image format pattern against these bytes.
129 % o length: Specifies the length of the magick string.
132 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
136 if (memcmp(magick,"\261\150\336\72",4) == 0)
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % IsPCX() returns MagickTrue if the image format type, identified by the
153 % magick string, is PCX.
155 % The format of the IsPCX method is:
157 % MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
159 % A description of each parameter follows:
161 % o magick: compare image format pattern against these bytes.
163 % o length: Specifies the length of the magick string.
166 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
170 if (memcmp(magick,"\012\002",2) == 0)
172 if (memcmp(magick,"\012\005",2) == 0)
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % R e a d P C X I m a g e %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
189 % It allocates the memory necessary for the new Image structure and returns
190 % a pointer to the new image.
192 % The format of the ReadPCXImage method is:
194 % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
196 % A description of each parameter follows:
198 % o image_info: the image info.
200 % o exception: return any errors or warnings in this structure.
203 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
205 #define ThrowPCXException(severity,tag) \
207 if (scanline != (unsigned char *) NULL) \
208 scanline=(unsigned char *) RelinquishMagickMemory(scanline); \
209 if (pixel_info != (MemoryInfo *) NULL) \
210 pixel_info=RelinquishVirtualMemory(pixel_info); \
211 if (page_table != (MagickOffsetType *) NULL) \
212 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table); \
213 ThrowReaderException(severity,tag); \
246 register unsigned char
267 assert(image_info != (const ImageInfo *) NULL);
268 assert(image_info->signature == MagickCoreSignature);
269 if (image_info->debug != MagickFalse)
270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
271 image_info->filename);
272 assert(exception != (ExceptionInfo *) NULL);
273 assert(exception->signature == MagickCoreSignature);
274 image=AcquireImage(image_info,exception);
275 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
276 if (status == MagickFalse)
278 image=DestroyImageList(image);
279 return((Image *) NULL);
282 Determine if this a PCX file.
284 page_table=(MagickOffsetType *) NULL;
285 scanline=(unsigned char *) NULL;
286 pixel_info=(MemoryInfo *) NULL;
287 if (LocaleCompare(image_info->magick,"DCX") == 0)
293 Read the DCX page table.
295 magic=ReadBlobLSBLong(image);
296 if (magic != 987654321)
297 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
298 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
299 sizeof(*page_table));
300 if (page_table == (MagickOffsetType *) NULL)
301 ThrowPCXException(ResourceLimitError,"MemoryAllocationFailed");
302 for (id=0; id < 1024; id++)
304 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
305 if (page_table[id] == 0)
309 if (page_table != (MagickOffsetType *) NULL)
311 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
313 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
315 count=ReadBlob(image,1,&pcx_info.identifier);
316 for (id=1; id < 1024; id++)
322 Verify PCX identifier.
324 pcx_info.version=(unsigned char) ReadBlobByte(image);
325 if ((count != 1) || (pcx_info.identifier != 0x0a))
326 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
327 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
328 bits_per_pixel=ReadBlobByte(image);
329 if (bits_per_pixel == -1)
330 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
331 pcx_info.bits_per_pixel=(unsigned char) bits_per_pixel;
332 pcx_info.left=ReadBlobLSBShort(image);
333 pcx_info.top=ReadBlobLSBShort(image);
334 pcx_info.right=ReadBlobLSBShort(image);
335 pcx_info.bottom=ReadBlobLSBShort(image);
336 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
337 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
339 Read PCX raster colormap.
341 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
343 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
345 if ((image->columns == 0) || (image->rows == 0) ||
346 ((pcx_info.bits_per_pixel != 1) && (pcx_info.bits_per_pixel != 2) &&
347 (pcx_info.bits_per_pixel != 4) && (pcx_info.bits_per_pixel != 8)))
348 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
349 image->depth=pcx_info.bits_per_pixel;
350 image->units=PixelsPerInchResolution;
351 image->resolution.x=(double) pcx_info.horizontal_resolution;
352 image->resolution.y=(double) pcx_info.vertical_resolution;
354 count=ReadBlob(image,3*image->colors,pcx_colormap);
355 if (count != (ssize_t) (3*image->colors))
356 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
357 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
358 pcx_info.planes=(unsigned char) ReadBlobByte(image);
359 if (pcx_info.planes == 0)
360 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
361 if ((pcx_info.bits_per_pixel*pcx_info.planes) >= 64)
362 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
364 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
365 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
366 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
367 image->colors=(size_t) MagickMin(one << (1UL*
368 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
369 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
370 ThrowPCXException(ResourceLimitError,"MemoryAllocationFailed");
371 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
372 image->storage_class=DirectClass;
374 for (i=0; i < (ssize_t) image->colors; i++)
376 image->colormap[i].red=ScaleCharToQuantum(*p++);
377 image->colormap[i].green=ScaleCharToQuantum(*p++);
378 image->colormap[i].blue=ScaleCharToQuantum(*p++);
380 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
381 pcx_info.palette_info=ReadBlobLSBShort(image);
382 pcx_info.horizontal_screensize=ReadBlobLSBShort(image);
383 pcx_info.vertical_screensize=ReadBlobLSBShort(image);
384 for (i=0; i < 54; i++)
385 (void) ReadBlobByte(image);
386 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
387 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
389 status=SetImageExtent(image,image->columns,image->rows,exception);
390 if (status == MagickFalse)
391 return(DestroyImageList(image));
395 if (HeapOverflowSanityCheck(image->rows, (size_t) pcx_info.bytes_per_line) != MagickFalse)
396 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
397 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line;
398 if (HeapOverflowSanityCheck(pcx_packets, (size_t) pcx_info.planes) != MagickFalse)
399 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
400 pcx_packets=(size_t) pcx_packets*pcx_info.planes;
401 if ((size_t) (pcx_info.bits_per_pixel*pcx_info.planes*image->columns) >
403 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
404 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
405 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
406 pixel_info=AcquireVirtualMemory(pcx_packets,2*sizeof(*pixels));
407 if ((scanline == (unsigned char *) NULL) ||
408 (pixel_info == (MemoryInfo *) NULL))
410 if (scanline != (unsigned char *) NULL)
411 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
412 if (pixel_info != (MemoryInfo *) NULL)
413 pixel_info=RelinquishVirtualMemory(pixel_info);
414 ThrowPCXException(ResourceLimitError,"MemoryAllocationFailed");
416 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
418 Uncompress image data.
421 if (pcx_info.encoding == 0)
422 while (pcx_packets != 0)
424 packet=(unsigned char) ReadBlobByte(image);
425 if (EOFBlob(image) != MagickFalse)
426 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
431 while (pcx_packets != 0)
433 packet=(unsigned char) ReadBlobByte(image);
434 if (EOFBlob(image) != MagickFalse)
435 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
436 if ((packet & 0xc0) != 0xc0)
442 count=(ssize_t) (packet & 0x3f);
443 packet=(unsigned char) ReadBlobByte(image);
444 if (EOFBlob(image) != MagickFalse)
445 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
446 for ( ; count != 0; count--)
450 if (pcx_packets == 0)
454 if (image->storage_class == DirectClass)
455 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
458 if ((pcx_info.version == 5) ||
459 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
462 Initialize image colormap.
464 if (image->colors > 256)
465 ThrowPCXException(CorruptImageError,"ColormapExceeds256Colors");
466 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
471 image->colormap[0].red=(Quantum) 0;
472 image->colormap[0].green=(Quantum) 0;
473 image->colormap[0].blue=(Quantum) 0;
474 image->colormap[1].red=QuantumRange;
475 image->colormap[1].green=QuantumRange;
476 image->colormap[1].blue=QuantumRange;
479 if (image->colors > 16)
482 256 color images have their color map at the end of the file.
484 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
485 count=ReadBlob(image,3*image->colors,pcx_colormap);
487 for (i=0; i < (ssize_t) image->colors; i++)
489 image->colormap[i].red=ScaleCharToQuantum(*p++);
490 image->colormap[i].green=ScaleCharToQuantum(*p++);
491 image->colormap[i].blue=ScaleCharToQuantum(*p++);
496 Convert PCX raster image to pixel packets.
498 for (y=0; y < (ssize_t) image->rows; y++)
500 p=pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
501 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
502 if (q == (Quantum *) NULL)
505 if (image->storage_class == DirectClass)
506 for (i=0; i < pcx_info.planes; i++)
509 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
539 if (pcx_info.planes > 1)
541 for (x=0; x < (ssize_t) image->columns; x++)
543 for (i=0; i < pcx_info.planes; i++)
546 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
549 for (mask=0x80; mask != 0; mask>>=1)
559 switch (pcx_info.bits_per_pixel)
566 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
568 for (bit=7; bit >= 0; bit--)
569 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
572 if ((image->columns % 8) != 0)
574 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
575 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
582 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
584 *r++=(*p >> 6) & 0x3;
585 *r++=(*p >> 4) & 0x3;
586 *r++=(*p >> 2) & 0x3;
590 if ((image->columns % 4) != 0)
592 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
593 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
600 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
602 *r++=(*p >> 4) & 0xf;
606 if ((image->columns % 2) != 0)
607 *r++=(*p++ >> 4) & 0xf;
612 (void) CopyMagickMemory(r,p,image->columns);
619 Transfer image scanline.
622 for (x=0; x < (ssize_t) image->columns; x++)
624 if (image->storage_class == PseudoClass)
625 SetPixelIndex(image,*r++,q);
628 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
629 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
630 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
631 if (image->alpha_trait != UndefinedPixelTrait)
632 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
634 q+=GetPixelChannels(image);
636 if (SyncAuthenticPixels(image,exception) == MagickFalse)
638 if (image->previous == (Image *) NULL)
640 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
642 if (status == MagickFalse)
646 if (image->storage_class == PseudoClass)
647 (void) SyncImage(image,exception);
648 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
649 pixel_info=RelinquishVirtualMemory(pixel_info);
650 if (EOFBlob(image) != MagickFalse)
652 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
657 Proceed to next image.
659 if (image_info->number_scenes != 0)
660 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
662 if (page_table == (MagickOffsetType *) NULL)
664 if (page_table[id] == 0)
666 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
668 ThrowPCXException(CorruptImageError,"ImproperImageHeader");
669 count=ReadBlob(image,1,&pcx_info.identifier);
670 if ((count != 0) && (pcx_info.identifier == 0x0a))
673 Allocate next image structure.
675 AcquireNextImage(image_info,image,exception);
676 if (GetNextImageInList(image) == (Image *) NULL)
678 image=DestroyImageList(image);
679 return((Image *) NULL);
681 image=SyncNextImageInList(image);
682 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
684 if (status == MagickFalse)
688 if (page_table != (MagickOffsetType *) NULL)
689 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
690 (void) CloseBlob(image);
691 return(GetFirstImageInList(image));
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % R e g i s t e r P C X I m a g e %
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705 % RegisterPCXImage() adds attributes for the PCX image format to
706 % the list of supported formats. The attributes include the image format
707 % tag, a method to read and/or write the format, whether the format
708 % supports the saving of more than one frame to the same file or blob,
709 % whether the format supports native in-memory I/O, and a brief
710 % description of the format.
712 % The format of the RegisterPCXImage method is:
714 % size_t RegisterPCXImage(void)
717 ModuleExport size_t RegisterPCXImage(void)
722 entry=AcquireMagickInfo("PCX","DCX","ZSoft IBM PC multi-page Paintbrush");
723 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
724 entry->encoder=(EncodeImageHandler *) WritePCXImage;
725 entry->flags|=CoderDecoderSeekableStreamFlag;
726 entry->flags|=CoderEncoderSeekableStreamFlag;
727 entry->magick=(IsImageFormatHandler *) IsDCX;
728 (void) RegisterMagickInfo(entry);
729 entry=AcquireMagickInfo("PCX","PCX","ZSoft IBM PC Paintbrush");
730 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
731 entry->encoder=(EncodeImageHandler *) WritePCXImage;
732 entry->magick=(IsImageFormatHandler *) IsPCX;
733 entry->flags^=CoderAdjoinFlag;
734 entry->flags|=CoderDecoderSeekableStreamFlag;
735 entry->flags|=CoderEncoderSeekableStreamFlag;
736 (void) RegisterMagickInfo(entry);
737 return(MagickImageCoderSignature);
741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745 % U n r e g i s t e r P C X I m a g e %
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 % UnregisterPCXImage() removes format registrations made by the
752 % PCX module from the list of supported formats.
754 % The format of the UnregisterPCXImage method is:
756 % UnregisterPCXImage(void)
759 ModuleExport void UnregisterPCXImage(void)
761 (void) UnregisterMagickInfo("DCX");
762 (void) UnregisterMagickInfo("PCX");
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 % W r i t e P C X I m a g e %
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
779 % The format of the WritePCXImage method is:
781 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,
782 % Image *image,ExceptionInfo *exception)
784 % A description of each parameter follows.
786 % o image_info: the image info.
788 % o image: The image.
790 % o exception: return any errors or warnings in this structure.
794 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
795 const unsigned char *pixels,Image *image)
797 register const unsigned char
812 for (i=0; i < (ssize_t) pcx_info->planes; i++)
814 if (pcx_info->encoding == 0)
816 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
817 (void) WriteBlobByte(image,(unsigned char) (*q++));
823 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
826 if ((packet == previous) && (count < 63))
831 if ((count > 1) || ((previous & 0xc0) == 0xc0))
834 (void) WriteBlobByte(image,(unsigned char) count);
836 (void) WriteBlobByte(image,previous);
840 if ((count > 1) || ((previous & 0xc0) == 0xc0))
843 (void) WriteBlobByte(image,(unsigned char) count);
845 (void) WriteBlobByte(image,previous);
851 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
852 ExceptionInfo *exception)
868 register const Quantum
875 register unsigned char
889 Open output image file.
891 assert(image_info != (const ImageInfo *) NULL);
892 assert(image_info->signature == MagickCoreSignature);
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickCoreSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 assert(exception != (ExceptionInfo *) NULL);
898 assert(exception->signature == MagickCoreSignature);
899 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
900 if (status == MagickFalse)
902 (void) TransformImageColorspace(image,sRGBColorspace,exception);
903 page_table=(MagickOffsetType *) NULL;
904 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
905 ((GetNextImageInList(image) != (Image *) NULL) &&
906 (image_info->adjoin != MagickFalse)))
909 Write the DCX page table.
911 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
912 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
913 sizeof(*page_table));
914 if (page_table == (MagickOffsetType *) NULL)
915 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
916 for (scene=0; scene < 1024; scene++)
917 (void) WriteBlobLSBLong(image,0x00000000L);
922 if (page_table != (MagickOffsetType *) NULL)
923 page_table[scene]=TellBlob(image);
925 Initialize PCX raster file header.
927 pcx_info.identifier=0x0a;
929 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
930 pcx_info.bits_per_pixel=8;
931 if ((image->storage_class == PseudoClass) &&
932 (SetImageMonochrome(image,exception) != MagickFalse))
933 pcx_info.bits_per_pixel=1;
936 pcx_info.right=(unsigned short) (image->columns-1);
937 pcx_info.bottom=(unsigned short) (image->rows-1);
938 switch (image->units)
940 case UndefinedResolution:
941 case PixelsPerInchResolution:
944 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
945 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
948 case PixelsPerCentimeterResolution:
950 pcx_info.horizontal_resolution=(unsigned short)
951 (2.54*image->resolution.x+0.5);
952 pcx_info.vertical_resolution=(unsigned short)
953 (2.54*image->resolution.y+0.5);
959 if ((image->storage_class == DirectClass) || (image->colors > 256))
962 if (image->alpha_trait != UndefinedPixelTrait)
965 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
966 pcx_info.bits_per_pixel+7)/8);
967 pcx_info.palette_info=1;
968 pcx_info.colormap_signature=0x0c;
972 (void) WriteBlobByte(image,pcx_info.identifier);
973 (void) WriteBlobByte(image,pcx_info.version);
974 (void) WriteBlobByte(image,pcx_info.encoding);
975 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
976 (void) WriteBlobLSBShort(image,pcx_info.left);
977 (void) WriteBlobLSBShort(image,pcx_info.top);
978 (void) WriteBlobLSBShort(image,pcx_info.right);
979 (void) WriteBlobLSBShort(image,pcx_info.bottom);
980 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
981 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
983 Dump colormap to file.
985 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
986 3*sizeof(*pcx_colormap));
987 if (pcx_colormap == (unsigned char *) NULL)
988 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
989 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
991 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
992 for (i=0; i < (ssize_t) image->colors; i++)
994 *q++=ScaleQuantumToChar(image->colormap[i].red);
995 *q++=ScaleQuantumToChar(image->colormap[i].green);
996 *q++=ScaleQuantumToChar(image->colormap[i].blue);
998 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
999 (void) WriteBlobByte(image,pcx_info.reserved);
1000 (void) WriteBlobByte(image,pcx_info.planes);
1001 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
1002 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
1003 for (i=0; i < 58; i++)
1004 (void) WriteBlobByte(image,'\0');
1005 length=(size_t) pcx_info.bytes_per_line;
1006 pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
1007 if (pixel_info == (MemoryInfo *) NULL)
1008 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1009 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1011 if ((image->storage_class == DirectClass) || (image->colors > 256))
1014 Convert DirectClass image to PCX raster pixels.
1016 for (y=0; y < (ssize_t) image->rows; y++)
1019 for (i=0; i < pcx_info.planes; i++)
1021 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1022 if (p == (const Quantum *) NULL)
1028 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1030 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1031 p+=GetPixelChannels(image);
1037 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1039 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1040 p+=GetPixelChannels(image);
1046 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1048 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1049 p+=GetPixelChannels(image);
1056 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1058 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1059 p+=GetPixelChannels(image);
1065 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1067 if (image->previous == (Image *) NULL)
1069 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1071 if (status == MagickFalse)
1078 if (pcx_info.bits_per_pixel > 1)
1079 for (y=0; y < (ssize_t) image->rows; y++)
1081 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1082 if (p == (const Quantum *) NULL)
1085 for (x=0; x < (ssize_t) image->columns; x++)
1087 *q++=(unsigned char) GetPixelIndex(image,p);
1088 p+=GetPixelChannels(image);
1090 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1092 if (image->previous == (Image *) NULL)
1094 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1096 if (status == MagickFalse)
1102 register unsigned char
1107 Convert PseudoClass image to a PCX monochrome image.
1109 for (y=0; y < (ssize_t) image->rows; y++)
1111 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1112 if (p == (const Quantum *) NULL)
1117 for (x=0; x < (ssize_t) image->columns; x++)
1120 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
1129 p+=GetPixelChannels(image);
1132 *q++=byte << (8-bit);
1133 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1135 if (image->previous == (Image *) NULL)
1137 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1139 if (status == MagickFalse)
1144 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1145 (void) WriteBlob(image,3*256,pcx_colormap);
1147 pixel_info=RelinquishVirtualMemory(pixel_info);
1148 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1149 if (page_table == (MagickOffsetType *) NULL)
1153 if (GetNextImageInList(image) == (Image *) NULL)
1155 image=SyncNextImageInList(image);
1156 status=SetImageProgress(image,SaveImagesTag,scene++,
1157 GetImageListLength(image));
1158 if (status == MagickFalse)
1160 } while (image_info->adjoin != MagickFalse);
1161 if (page_table != (MagickOffsetType *) NULL)
1164 Write the DCX page table.
1166 page_table[scene+1]=0;
1167 offset=SeekBlob(image,0L,SEEK_SET);
1169 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1170 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1171 for (i=0; i <= (ssize_t) scene; i++)
1172 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1173 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1175 if (status == MagickFalse)
1180 message=GetExceptionMessage(errno);
1181 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1182 "UnableToWriteFile","`%s': %s",image->filename,message);
1183 message=DestroyString(message);
1185 (void) CloseBlob(image);