2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2015 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 "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/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
70 typedef struct _PCXInfo
83 horizontal_resolution,
101 static MagickBooleanType
102 WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % IsDCX() returns MagickTrue if the image format type, identified by the
116 % magick string, is DCX.
118 % The format of the IsDCX method is:
120 % MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
122 % A description of each parameter follows:
124 % o magick: compare image format pattern against these bytes.
126 % o length: Specifies the length of the magick string.
129 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
133 if (memcmp(magick,"\261\150\336\72",4) == 0)
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % IsPCX() returns MagickTrue if the image format type, identified by the
150 % magick string, is PCX.
152 % The format of the IsPCX method is:
154 % MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
156 % A description of each parameter follows:
158 % o magick: compare image format pattern against these bytes.
160 % o length: Specifies the length of the magick string.
163 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
167 if (memcmp(magick,"\012\002",2) == 0)
169 if (memcmp(magick,"\012\005",2) == 0)
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % R e a d P C X I m a g e %
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
186 % It allocates the memory necessary for the new Image structure and returns
187 % a pointer to the new image.
189 % The format of the ReadPCXImage method is:
191 % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
193 % A description of each parameter follows:
195 % o image_info: the image info.
197 % o exception: return any errors or warnings in this structure.
201 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
208 static inline size_t MagickMax(const size_t x,const size_t y)
215 static inline size_t MagickMin(const size_t x,const size_t y)
222 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
224 #define ThrowPCXException(severity,tag) \
226 scanline=(unsigned char *) RelinquishMagickMemory(scanline); \
227 pixel_info=RelinquishVirtualMemory(pixel_info); \
228 ThrowReaderException(severity,tag); \
261 register unsigned char
282 assert(image_info != (const ImageInfo *) NULL);
283 assert(image_info->signature == MagickSignature);
284 if (image_info->debug != MagickFalse)
285 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
286 image_info->filename);
287 assert(exception != (ExceptionInfo *) NULL);
288 assert(exception->signature == MagickSignature);
289 image=AcquireImage(image_info,exception);
290 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
291 if (status == MagickFalse)
293 image=DestroyImageList(image);
294 return((Image *) NULL);
297 Determine if this a PCX file.
299 page_table=(MagickOffsetType *) NULL;
300 if (LocaleCompare(image_info->magick,"DCX") == 0)
306 Read the DCX page table.
308 magic=ReadBlobLSBLong(image);
309 if (magic != 987654321)
310 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
311 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
312 sizeof(*page_table));
313 if (page_table == (MagickOffsetType *) NULL)
314 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
315 for (id=0; id < 1024; id++)
317 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
318 if (page_table[id] == 0)
322 if (page_table != (MagickOffsetType *) NULL)
324 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
328 count=ReadBlob(image,1,&pcx_info.identifier);
329 for (id=1; id < 1024; id++)
335 Verify PCX identifier.
337 pcx_info.version=(unsigned char) ReadBlobByte(image);
338 if ((count == 0) || (pcx_info.identifier != 0x0a))
339 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
340 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
341 bits_per_pixel=ReadBlobByte(image);
342 if (bits_per_pixel == -1)
343 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
344 pcx_info.bits_per_pixel=(unsigned char) bits_per_pixel;
345 pcx_info.left=ReadBlobLSBShort(image);
346 pcx_info.top=ReadBlobLSBShort(image);
347 pcx_info.right=ReadBlobLSBShort(image);
348 pcx_info.bottom=ReadBlobLSBShort(image);
349 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
350 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
352 Read PCX raster colormap.
354 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
356 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
358 if ((image->columns == 0) || (image->rows == 0) ||
359 (pcx_info.bits_per_pixel == 0))
360 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
361 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
362 image->units=PixelsPerInchResolution;
363 image->resolution.x=(double) pcx_info.horizontal_resolution;
364 image->resolution.y=(double) pcx_info.vertical_resolution;
366 count=ReadBlob(image,3*image->colors,pcx_colormap);
367 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
368 pcx_info.planes=(unsigned char) ReadBlobByte(image);
369 if ((pcx_info.bits_per_pixel*pcx_info.planes) >= 64)
370 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
372 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
373 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
374 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
375 image->colors=(size_t) MagickMin(one << (1UL*
376 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
377 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
378 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
379 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
380 image->storage_class=DirectClass;
382 for (i=0; i < (ssize_t) image->colors; i++)
384 image->colormap[i].red=ScaleCharToQuantum(*p++);
385 image->colormap[i].green=ScaleCharToQuantum(*p++);
386 image->colormap[i].blue=ScaleCharToQuantum(*p++);
388 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
389 pcx_info.palette_info=ReadBlobLSBShort(image);
390 for (i=0; i < 58; i++)
391 (void) ReadBlobByte(image);
392 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
393 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
395 status=SetImageExtent(image,image->columns,image->rows,exception);
396 if (status == MagickFalse)
397 return(DestroyImageList(image));
401 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*pcx_info.planes;
402 if ((size_t) (pcx_info.bits_per_pixel*pcx_info.planes*image->columns) >
404 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
405 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
406 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
407 pixel_info=AcquireVirtualMemory(pcx_packets,2*sizeof(*pixels));
408 if ((scanline == (unsigned char *) NULL) ||
409 (pixel_info == (MemoryInfo *) NULL))
411 if (scanline != (unsigned char *) NULL)
412 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
413 if (pixel_info != (MemoryInfo *) NULL)
414 pixel_info=RelinquishVirtualMemory(pixel_info);
415 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
417 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
419 Uncompress image data.
422 if (pcx_info.encoding == 0)
423 while (pcx_packets != 0)
425 packet=(unsigned char) ReadBlobByte(image);
426 if (EOFBlob(image) != MagickFalse)
427 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
432 while (pcx_packets != 0)
434 packet=(unsigned char) ReadBlobByte(image);
435 if (EOFBlob(image) != MagickFalse)
436 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
437 if ((packet & 0xc0) != 0xc0)
443 count=(ssize_t) (packet & 0x3f);
444 packet=(unsigned char) ReadBlobByte(image);
445 if (EOFBlob(image) != MagickFalse)
446 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
447 for ( ; count != 0; count--)
451 if (pcx_packets == 0)
455 if (image->storage_class == DirectClass)
456 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
459 if ((pcx_info.version == 5) ||
460 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
463 Initialize image colormap.
465 if (image->colors > 256)
466 ThrowPCXException(CorruptImageError,"ColormapExceeds256Colors");
467 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
472 image->colormap[0].red=(Quantum) 0;
473 image->colormap[0].green=(Quantum) 0;
474 image->colormap[0].blue=(Quantum) 0;
475 image->colormap[1].red=QuantumRange;
476 image->colormap[1].green=QuantumRange;
477 image->colormap[1].blue=QuantumRange;
480 if (image->colors > 16)
483 256 color images have their color map at the end of the file.
485 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
486 count=ReadBlob(image,3*image->colors,pcx_colormap);
488 for (i=0; i < (ssize_t) image->colors; i++)
490 image->colormap[i].red=ScaleCharToQuantum(*p++);
491 image->colormap[i].green=ScaleCharToQuantum(*p++);
492 image->colormap[i].blue=ScaleCharToQuantum(*p++);
497 Convert PCX raster image to pixel packets.
499 for (y=0; y < (ssize_t) image->rows; y++)
501 p=pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
502 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
503 if (q == (Quantum *) NULL)
506 if (image->storage_class == DirectClass)
507 for (i=0; i < pcx_info.planes; i++)
510 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
540 if (pcx_info.planes > 1)
542 for (x=0; x < (ssize_t) image->columns; x++)
544 for (i=0; i < pcx_info.planes; i++)
547 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
550 for (mask=0x80; mask != 0; mask>>=1)
560 switch (pcx_info.bits_per_pixel)
567 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
569 for (bit=7; bit >= 0; bit--)
570 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
573 if ((image->columns % 8) != 0)
575 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
576 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
583 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
585 *r++=(*p >> 6) & 0x3;
586 *r++=(*p >> 4) & 0x3;
587 *r++=(*p >> 2) & 0x3;
591 if ((image->columns % 4) != 0)
593 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
594 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
601 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
603 *r++=(*p >> 4) & 0xf;
607 if ((image->columns % 2) != 0)
608 *r++=(*p++ >> 4) & 0xf;
613 (void) CopyMagickMemory(r,p,image->columns);
620 Transfer image scanline.
623 for (x=0; x < (ssize_t) image->columns; x++)
625 if (image->storage_class == PseudoClass)
626 SetPixelIndex(image,*r++,q);
629 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
630 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
631 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
632 if (image->alpha_trait != UndefinedPixelTrait)
633 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
635 q+=GetPixelChannels(image);
637 if (SyncAuthenticPixels(image,exception) == MagickFalse)
639 if (image->previous == (Image *) NULL)
641 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
643 if (status == MagickFalse)
647 if (image->storage_class == PseudoClass)
648 (void) SyncImage(image,exception);
649 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
650 pixel_info=RelinquishVirtualMemory(pixel_info);
651 if (EOFBlob(image) != MagickFalse)
653 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
658 Proceed to next image.
660 if (image_info->number_scenes != 0)
661 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
663 if (page_table == (MagickOffsetType *) NULL)
665 if (page_table[id] == 0)
667 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
669 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
670 count=ReadBlob(image,1,&pcx_info.identifier);
671 if ((count != 0) && (pcx_info.identifier == 0x0a))
674 Allocate next image structure.
676 AcquireNextImage(image_info,image,exception);
677 if (GetNextImageInList(image) == (Image *) NULL)
679 image=DestroyImageList(image);
680 return((Image *) NULL);
682 image=SyncNextImageInList(image);
683 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
685 if (status == MagickFalse)
689 if (page_table != (MagickOffsetType *) NULL)
690 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
691 (void) CloseBlob(image);
692 return(GetFirstImageInList(image));
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % R e g i s t e r P C X I m a g e %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % RegisterPCXImage() adds attributes for the PCX image format to
707 % the list of supported formats. The attributes include the image format
708 % tag, a method to read and/or write the format, whether the format
709 % supports the saving of more than one frame to the same file or blob,
710 % whether the format supports native in-memory I/O, and a brief
711 % description of the format.
713 % The format of the RegisterPCXImage method is:
715 % size_t RegisterPCXImage(void)
718 ModuleExport size_t RegisterPCXImage(void)
723 entry=SetMagickInfo("DCX");
724 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
725 entry->encoder=(EncodeImageHandler *) WritePCXImage;
726 entry->seekable_stream=MagickTrue;
727 entry->magick=(IsImageFormatHandler *) IsDCX;
728 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
729 entry->module=ConstantString("PCX");
730 (void) RegisterMagickInfo(entry);
731 entry=SetMagickInfo("PCX");
732 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
733 entry->encoder=(EncodeImageHandler *) WritePCXImage;
734 entry->magick=(IsImageFormatHandler *) IsPCX;
735 entry->adjoin=MagickFalse;
736 entry->seekable_stream=MagickTrue;
737 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
738 entry->module=ConstantString("PCX");
739 (void) RegisterMagickInfo(entry);
740 return(MagickImageCoderSignature);
744 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748 % U n r e g i s t e r P C X I m a g e %
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % UnregisterPCXImage() removes format registrations made by the
755 % PCX module from the list of supported formats.
757 % The format of the UnregisterPCXImage method is:
759 % UnregisterPCXImage(void)
762 ModuleExport void UnregisterPCXImage(void)
764 (void) UnregisterMagickInfo("DCX");
765 (void) UnregisterMagickInfo("PCX");
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % W r i t e P C X I m a g e %
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
782 % The format of the WritePCXImage method is:
784 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,
785 % Image *image,ExceptionInfo *exception)
787 % A description of each parameter follows.
789 % o image_info: the image info.
791 % o image: The image.
793 % o exception: return any errors or warnings in this structure.
797 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
798 const unsigned char *pixels,Image *image)
800 register const unsigned char
815 for (i=0; i < (ssize_t) pcx_info->planes; i++)
817 if (pcx_info->encoding == 0)
819 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
820 (void) WriteBlobByte(image,(unsigned char) (*q++));
826 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
829 if ((packet == previous) && (count < 63))
834 if ((count > 1) || ((previous & 0xc0) == 0xc0))
837 (void) WriteBlobByte(image,(unsigned char) count);
839 (void) WriteBlobByte(image,previous);
843 if ((count > 1) || ((previous & 0xc0) == 0xc0))
846 (void) WriteBlobByte(image,(unsigned char) count);
848 (void) WriteBlobByte(image,previous);
854 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
855 ExceptionInfo *exception)
871 register const Quantum
878 register unsigned char
892 Open output image file.
894 assert(image_info != (const ImageInfo *) NULL);
895 assert(image_info->signature == MagickSignature);
896 assert(image != (Image *) NULL);
897 assert(image->signature == MagickSignature);
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
900 assert(exception != (ExceptionInfo *) NULL);
901 assert(exception->signature == MagickSignature);
902 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
903 if (status == MagickFalse)
905 (void) TransformImageColorspace(image,sRGBColorspace,exception);
906 page_table=(MagickOffsetType *) NULL;
907 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
908 ((GetNextImageInList(image) != (Image *) NULL) &&
909 (image_info->adjoin != MagickFalse)))
912 Write the DCX page table.
914 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
915 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
916 sizeof(*page_table));
917 if (page_table == (MagickOffsetType *) NULL)
918 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
919 for (scene=0; scene < 1024; scene++)
920 (void) WriteBlobLSBLong(image,0x00000000L);
925 if (page_table != (MagickOffsetType *) NULL)
926 page_table[scene]=TellBlob(image);
928 Initialize PCX raster file header.
930 pcx_info.identifier=0x0a;
932 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
933 pcx_info.bits_per_pixel=8;
934 if ((image->storage_class == PseudoClass) &&
935 (IsImageMonochrome(image,exception) != MagickFalse))
936 pcx_info.bits_per_pixel=1;
939 pcx_info.right=(unsigned short) (image->columns-1);
940 pcx_info.bottom=(unsigned short) (image->rows-1);
941 switch (image->units)
943 case UndefinedResolution:
944 case PixelsPerInchResolution:
947 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
948 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
951 case PixelsPerCentimeterResolution:
953 pcx_info.horizontal_resolution=(unsigned short)
954 (2.54*image->resolution.x+0.5);
955 pcx_info.vertical_resolution=(unsigned short)
956 (2.54*image->resolution.y+0.5);
962 if ((image->storage_class == DirectClass) || (image->colors > 256))
965 if (image->alpha_trait != UndefinedPixelTrait)
968 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
969 pcx_info.bits_per_pixel+7)/8);
970 pcx_info.palette_info=1;
971 pcx_info.colormap_signature=0x0c;
975 (void) WriteBlobByte(image,pcx_info.identifier);
976 (void) WriteBlobByte(image,pcx_info.version);
977 (void) WriteBlobByte(image,pcx_info.encoding);
978 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
979 (void) WriteBlobLSBShort(image,pcx_info.left);
980 (void) WriteBlobLSBShort(image,pcx_info.top);
981 (void) WriteBlobLSBShort(image,pcx_info.right);
982 (void) WriteBlobLSBShort(image,pcx_info.bottom);
983 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
984 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
986 Dump colormap to file.
988 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
989 3*sizeof(*pcx_colormap));
990 if (pcx_colormap == (unsigned char *) NULL)
991 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
992 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
994 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
995 for (i=0; i < (ssize_t) image->colors; i++)
997 *q++=ScaleQuantumToChar(image->colormap[i].red);
998 *q++=ScaleQuantumToChar(image->colormap[i].green);
999 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1001 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
1002 (void) WriteBlobByte(image,pcx_info.reserved);
1003 (void) WriteBlobByte(image,pcx_info.planes);
1004 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
1005 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
1006 for (i=0; i < 58; i++)
1007 (void) WriteBlobByte(image,'\0');
1008 length=(size_t) pcx_info.bytes_per_line;
1009 pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
1010 if (pixel_info == (MemoryInfo *) NULL)
1011 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1012 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1014 if ((image->storage_class == DirectClass) || (image->colors > 256))
1017 Convert DirectClass image to PCX raster pixels.
1019 for (y=0; y < (ssize_t) image->rows; y++)
1022 for (i=0; i < pcx_info.planes; i++)
1024 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1025 if (p == (const Quantum *) NULL)
1031 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1033 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1034 p+=GetPixelChannels(image);
1040 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1042 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1043 p+=GetPixelChannels(image);
1049 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1051 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1052 p+=GetPixelChannels(image);
1059 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1061 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1062 p+=GetPixelChannels(image);
1068 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1070 if (image->previous == (Image *) NULL)
1072 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1074 if (status == MagickFalse)
1081 if (pcx_info.bits_per_pixel > 1)
1082 for (y=0; y < (ssize_t) image->rows; y++)
1084 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1085 if (p == (const Quantum *) NULL)
1088 for (x=0; x < (ssize_t) image->columns; x++)
1090 *q++=(unsigned char) GetPixelIndex(image,p);
1091 p+=GetPixelChannels(image);
1093 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1095 if (image->previous == (Image *) NULL)
1097 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1099 if (status == MagickFalse)
1105 register unsigned char
1110 Convert PseudoClass image to a PCX monochrome image.
1112 for (y=0; y < (ssize_t) image->rows; y++)
1114 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1115 if (p == (const Quantum *) NULL)
1120 for (x=0; x < (ssize_t) image->columns; x++)
1123 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
1132 p+=GetPixelChannels(image);
1135 *q++=byte << (8-bit);
1136 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1138 if (image->previous == (Image *) NULL)
1140 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1142 if (status == MagickFalse)
1147 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1148 (void) WriteBlob(image,3*256,pcx_colormap);
1150 pixel_info=RelinquishVirtualMemory(pixel_info);
1151 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1152 if (page_table == (MagickOffsetType *) NULL)
1156 if (GetNextImageInList(image) == (Image *) NULL)
1158 image=SyncNextImageInList(image);
1159 status=SetImageProgress(image,SaveImagesTag,scene++,
1160 GetImageListLength(image));
1161 if (status == MagickFalse)
1163 } while (image_info->adjoin != MagickFalse);
1164 if (page_table != (MagickOffsetType *) NULL)
1167 Write the DCX page table.
1169 page_table[scene+1]=0;
1170 offset=SeekBlob(image,0L,SEEK_SET);
1172 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1173 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1174 for (i=0; i <= (ssize_t) scene; i++)
1175 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1176 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1178 if (status == MagickFalse)
1183 message=GetExceptionMessage(errno);
1184 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1185 "UnableToWriteFile","`%s': %s",image->filename,message);
1186 message=DestroyString(message);
1188 (void) CloseBlob(image);