2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2012 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.
130 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
134 if (memcmp(magick,"\261\150\336\72",4) == 0)
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % IsPCX() returns MagickTrue if the image format type, identified by the
151 % magick string, is PCX.
153 % The format of the IsPCX method is:
155 % MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
157 % A description of each parameter follows:
159 % o magick: compare image format pattern against these bytes.
161 % o length: Specifies the length of the magick string.
165 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
169 if (memcmp(magick,"\012\002",2) == 0)
171 if (memcmp(magick,"\012\005",2) == 0)
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % R e a d P C X I m a g e %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
188 % It allocates the memory necessary for the new Image structure and returns
189 % a pointer to the new image.
191 % The format of the ReadPCXImage method is:
193 % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
195 % A description of each parameter follows:
197 % o image_info: the image info.
199 % o exception: return any errors or warnings in this structure.
203 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
210 static inline size_t MagickMax(const size_t x,const size_t y)
217 static inline size_t MagickMin(const size_t x,const size_t y)
224 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
253 register unsigned char
274 assert(image_info != (const ImageInfo *) NULL);
275 assert(image_info->signature == MagickSignature);
276 if (image_info->debug != MagickFalse)
277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
278 image_info->filename);
279 assert(exception != (ExceptionInfo *) NULL);
280 assert(exception->signature == MagickSignature);
281 image=AcquireImage(image_info,exception);
282 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
283 if (status == MagickFalse)
285 image=DestroyImageList(image);
286 return((Image *) NULL);
289 Determine if this a PCX file.
291 page_table=(MagickOffsetType *) NULL;
292 if (LocaleCompare(image_info->magick,"DCX") == 0)
298 Read the DCX page table.
300 magic=ReadBlobLSBLong(image);
301 if (magic != 987654321)
302 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
303 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
304 sizeof(*page_table));
305 if (page_table == (MagickOffsetType *) NULL)
306 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
307 for (id=0; id < 1024; id++)
309 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
310 if (page_table[id] == 0)
314 if (page_table != (MagickOffsetType *) NULL)
316 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
318 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
320 pcx_colormap=(unsigned char *) NULL;
321 count=ReadBlob(image,1,&pcx_info.identifier);
322 for (id=1; id < 1024; id++)
325 Verify PCX identifier.
327 pcx_info.version=(unsigned char) ReadBlobByte(image);
328 if ((count == 0) || (pcx_info.identifier != 0x0a))
329 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
330 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
331 pcx_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
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 == 0))
347 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
348 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
349 image->units=PixelsPerInchResolution;
350 image->resolution.x=(double) pcx_info.horizontal_resolution;
351 image->resolution.y=(double) pcx_info.vertical_resolution;
353 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
354 3*sizeof(*pcx_colormap));
355 if (pcx_colormap == (unsigned char *) NULL)
356 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
357 count=ReadBlob(image,3*image->colors,pcx_colormap);
358 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
359 pcx_info.planes=(unsigned char) ReadBlobByte(image);
361 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
362 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
363 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
364 image->colors=(size_t) MagickMin(one << (1UL*
365 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
366 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
367 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
368 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
369 image->storage_class=DirectClass;
371 for (i=0; i < (ssize_t) image->colors; i++)
373 image->colormap[i].red=ScaleCharToQuantum(*p++);
374 image->colormap[i].green=ScaleCharToQuantum(*p++);
375 image->colormap[i].blue=ScaleCharToQuantum(*p++);
377 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
378 pcx_info.palette_info=ReadBlobLSBShort(image);
379 for (i=0; i < 58; i++)
380 (void) ReadBlobByte(image);
381 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
382 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
387 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
389 pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
390 sizeof(*pcx_pixels));
391 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
392 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
393 if ((pcx_pixels == (unsigned char *) NULL) ||
394 (scanline == (unsigned char *) NULL))
395 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
397 Uncompress image data.
400 if (pcx_info.encoding == 0)
401 while (pcx_packets != 0)
403 packet=(unsigned char) ReadBlobByte(image);
404 if (EOFBlob(image) != MagickFalse)
410 while (pcx_packets != 0)
412 packet=(unsigned char) ReadBlobByte(image);
413 if (EOFBlob(image) != MagickFalse)
415 if ((packet & 0xc0) != 0xc0)
421 count=(ssize_t) (packet & 0x3f);
422 packet=(unsigned char) ReadBlobByte(image);
423 if (EOFBlob(image) != MagickFalse)
425 for ( ; count != 0; count--)
429 if (pcx_packets == 0)
433 if (image->storage_class == DirectClass)
434 image->matte=pcx_info.planes > 3 ? MagickTrue : MagickFalse;
436 if ((pcx_info.version == 5) ||
437 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
440 Initialize image colormap.
442 if (image->colors > 256)
443 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
444 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
449 image->colormap[0].red=(Quantum) 0;
450 image->colormap[0].green=(Quantum) 0;
451 image->colormap[0].blue=(Quantum) 0;
452 image->colormap[1].red=QuantumRange;
453 image->colormap[1].green=QuantumRange;
454 image->colormap[1].blue=QuantumRange;
457 if (image->colors > 16)
460 256 color images have their color map at the end of the file.
462 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
463 count=ReadBlob(image,3*image->colors,pcx_colormap);
465 for (i=0; i < (ssize_t) image->colors; i++)
467 image->colormap[i].red=ScaleCharToQuantum(*p++);
468 image->colormap[i].green=ScaleCharToQuantum(*p++);
469 image->colormap[i].blue=ScaleCharToQuantum(*p++);
472 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
475 Convert PCX raster image to pixel packets.
477 for (y=0; y < (ssize_t) image->rows; y++)
479 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
480 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
481 if (q == (Quantum *) NULL)
484 if (image->storage_class == DirectClass)
485 for (i=0; i < pcx_info.planes; i++)
488 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
518 if (pcx_info.planes > 1)
520 for (x=0; x < (ssize_t) image->columns; x++)
522 for (i=0; i < pcx_info.planes; i++)
525 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
528 for (mask=0x80; mask != 0; mask>>=1)
538 switch (pcx_info.bits_per_pixel)
545 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
547 for (bit=7; bit >= 0; bit--)
548 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
551 if ((image->columns % 8) != 0)
553 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
554 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
561 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
563 *r++=(*p >> 6) & 0x3;
564 *r++=(*p >> 4) & 0x3;
565 *r++=(*p >> 2) & 0x3;
569 if ((image->columns % 4) != 0)
571 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
572 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
579 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
581 *r++=(*p >> 4) & 0xf;
585 if ((image->columns % 2) != 0)
586 *r++=(*p++ >> 4) & 0xf;
591 (void) CopyMagickMemory(r,p,image->columns);
598 Transfer image scanline.
601 for (x=0; x < (ssize_t) image->columns; x++)
603 if (image->storage_class == PseudoClass)
604 SetPixelIndex(image,*r++,q);
607 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
608 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
609 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
610 if (image->matte != MagickFalse)
611 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
613 q+=GetPixelChannels(image);
615 if (SyncAuthenticPixels(image,exception) == MagickFalse)
617 if (image->previous == (Image *) NULL)
619 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
621 if (status == MagickFalse)
625 if (image->storage_class == PseudoClass)
626 (void) SyncImage(image,exception);
627 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
628 if (pcx_colormap != (unsigned char *) NULL)
629 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
630 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
631 if (EOFBlob(image) != MagickFalse)
633 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
638 Proceed to next image.
640 if (image_info->number_scenes != 0)
641 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
643 if (page_table == (MagickOffsetType *) NULL)
645 if (page_table[id] == 0)
647 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
649 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
650 count=ReadBlob(image,1,&pcx_info.identifier);
651 if ((count != 0) && (pcx_info.identifier == 0x0a))
654 Allocate next image structure.
656 AcquireNextImage(image_info,image,exception);
657 if (GetNextImageInList(image) == (Image *) NULL)
659 image=DestroyImageList(image);
660 return((Image *) NULL);
662 image=SyncNextImageInList(image);
663 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
665 if (status == MagickFalse)
669 if (page_table != (MagickOffsetType *) NULL)
670 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
671 (void) CloseBlob(image);
672 return(GetFirstImageInList(image));
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % R e g i s t e r P C X I m a g e %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % RegisterPCXImage() adds attributes for the PCX image format to
687 % the list of supported formats. The attributes include the image format
688 % tag, a method to read and/or write the format, whether the format
689 % supports the saving of more than one frame to the same file or blob,
690 % whether the format supports native in-memory I/O, and a brief
691 % description of the format.
693 % The format of the RegisterPCXImage method is:
695 % size_t RegisterPCXImage(void)
698 ModuleExport size_t RegisterPCXImage(void)
703 entry=SetMagickInfo("DCX");
704 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
705 entry->encoder=(EncodeImageHandler *) WritePCXImage;
706 entry->seekable_stream=MagickTrue;
707 entry->magick=(IsImageFormatHandler *) IsDCX;
708 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
709 entry->module=ConstantString("PCX");
710 (void) RegisterMagickInfo(entry);
711 entry=SetMagickInfo("PCX");
712 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
713 entry->encoder=(EncodeImageHandler *) WritePCXImage;
714 entry->magick=(IsImageFormatHandler *) IsPCX;
715 entry->adjoin=MagickFalse;
716 entry->seekable_stream=MagickTrue;
717 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
718 entry->module=ConstantString("PCX");
719 (void) RegisterMagickInfo(entry);
720 return(MagickImageCoderSignature);
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % U n r e g i s t e r P C X I m a g e %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % UnregisterPCXImage() removes format registrations made by the
735 % PCX module from the list of supported formats.
737 % The format of the UnregisterPCXImage method is:
739 % UnregisterPCXImage(void)
742 ModuleExport void UnregisterPCXImage(void)
744 (void) UnregisterMagickInfo("DCX");
745 (void) UnregisterMagickInfo("PCX");
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % W r i t e P C X I m a g e %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
762 % The format of the WritePCXImage method is:
764 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,
765 % Image *image,ExceptionInfo *exception)
767 % A description of each parameter follows.
769 % o image_info: the image info.
771 % o image: The image.
773 % o exception: return any errors or warnings in this structure.
777 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
778 const unsigned char *pixels,Image *image)
780 register const unsigned char
795 for (i=0; i < (ssize_t) pcx_info->planes; i++)
797 if (pcx_info->encoding == 0)
799 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
800 (void) WriteBlobByte(image,(unsigned char) (*q++));
806 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
809 if ((packet == previous) && (count < 63))
814 if ((count > 1) || ((previous & 0xc0) == 0xc0))
817 (void) WriteBlobByte(image,(unsigned char) count);
819 (void) WriteBlobByte(image,previous);
823 if ((count > 1) || ((previous & 0xc0) == 0xc0))
826 (void) WriteBlobByte(image,(unsigned char) count);
828 (void) WriteBlobByte(image,previous);
834 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
835 ExceptionInfo *exception)
848 register const Quantum
855 register unsigned char
869 Open output image file.
871 assert(image_info != (const ImageInfo *) NULL);
872 assert(image_info->signature == MagickSignature);
873 assert(image != (Image *) NULL);
874 assert(image->signature == MagickSignature);
875 if (image->debug != MagickFalse)
876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
877 assert(exception != (ExceptionInfo *) NULL);
878 assert(exception->signature == MagickSignature);
879 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
880 if (status == MagickFalse)
882 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
883 (IsRGBColorspace(image->colorspace) == MagickFalse) &&
884 (IsImageGray(image,exception) == MagickFalse))
885 (void) TransformImageColorspace(image,sRGBColorspace,exception);
886 page_table=(MagickOffsetType *) NULL;
887 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
888 ((GetNextImageInList(image) != (Image *) NULL) &&
889 (image_info->adjoin != MagickFalse)))
892 Write the DCX page table.
894 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
895 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
896 sizeof(*page_table));
897 if (page_table == (MagickOffsetType *) NULL)
898 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
899 for (scene=0; scene < 1024; scene++)
900 (void) WriteBlobLSBLong(image,0x00000000L);
905 if (page_table != (MagickOffsetType *) NULL)
906 page_table[scene]=TellBlob(image);
908 Initialize PCX raster file header.
910 pcx_info.identifier=0x0a;
912 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
913 pcx_info.bits_per_pixel=8;
914 if ((image->storage_class == PseudoClass) &&
915 (IsImageMonochrome(image,exception) != MagickFalse))
916 pcx_info.bits_per_pixel=1;
919 pcx_info.right=(unsigned short) (image->columns-1);
920 pcx_info.bottom=(unsigned short) (image->rows-1);
921 switch (image->units)
923 case UndefinedResolution:
924 case PixelsPerInchResolution:
927 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
928 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
931 case PixelsPerCentimeterResolution:
933 pcx_info.horizontal_resolution=(unsigned short)
934 (2.54*image->resolution.x+0.5);
935 pcx_info.vertical_resolution=(unsigned short)
936 (2.54*image->resolution.y+0.5);
942 if ((image->storage_class == DirectClass) || (image->colors > 256))
945 if (image->matte != MagickFalse)
948 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
949 pcx_info.bits_per_pixel+7)/8);
950 pcx_info.palette_info=1;
951 pcx_info.colormap_signature=0x0c;
955 (void) WriteBlobByte(image,pcx_info.identifier);
956 (void) WriteBlobByte(image,pcx_info.version);
957 (void) WriteBlobByte(image,pcx_info.encoding);
958 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
959 (void) WriteBlobLSBShort(image,pcx_info.left);
960 (void) WriteBlobLSBShort(image,pcx_info.top);
961 (void) WriteBlobLSBShort(image,pcx_info.right);
962 (void) WriteBlobLSBShort(image,pcx_info.bottom);
963 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
964 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
966 Dump colormap to file.
968 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
969 3*sizeof(*pcx_colormap));
970 if (pcx_colormap == (unsigned char *) NULL)
971 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
972 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
974 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
975 for (i=0; i < (ssize_t) image->colors; i++)
977 *q++=ScaleQuantumToChar(image->colormap[i].red);
978 *q++=ScaleQuantumToChar(image->colormap[i].green);
979 *q++=ScaleQuantumToChar(image->colormap[i].blue);
981 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
982 (void) WriteBlobByte(image,pcx_info.reserved);
983 (void) WriteBlobByte(image,pcx_info.planes);
984 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
985 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
986 for (i=0; i < 58; i++)
987 (void) WriteBlobByte(image,'\0');
988 length=(size_t) pcx_info.bytes_per_line;
989 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
990 sizeof(*pcx_pixels));
991 if (pcx_pixels == (unsigned char *) NULL)
992 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
994 if ((image->storage_class == DirectClass) || (image->colors > 256))
1000 Convert DirectClass image to PCX raster pixels.
1002 for (y=0; y < (ssize_t) image->rows; y++)
1004 pixels=GetVirtualPixels(image,0,y,image->columns,1,exception);
1005 if (pixels == (const Quantum *) NULL)
1008 for (i=0; i < pcx_info.planes; i++)
1015 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1017 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1018 p+=GetPixelChannels(image);
1024 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1026 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1027 p+=GetPixelChannels(image);
1033 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1035 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1036 p+=GetPixelChannels(image);
1043 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1045 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1046 p+=GetPixelChannels(image);
1052 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1054 if (image->previous == (Image *) NULL)
1056 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1058 if (status == MagickFalse)
1065 if (pcx_info.bits_per_pixel > 1)
1066 for (y=0; y < (ssize_t) image->rows; y++)
1068 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1069 if (p == (const Quantum *) NULL)
1072 for (x=0; x < (ssize_t) image->columns; x++)
1074 *q++=(unsigned char) GetPixelIndex(image,p);
1075 p+=GetPixelChannels(image);
1077 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1079 if (image->previous == (Image *) NULL)
1081 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1083 if (status == MagickFalse)
1092 register unsigned char
1097 Convert PseudoClass image to a PCX monochrome image.
1099 polarity=(Quantum) (GetPixelInfoIntensity(
1100 &image->colormap[0]) < (QuantumRange/2) ? 1 : 0);
1101 if (image->colors == 2)
1102 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) <
1103 GetPixelInfoIntensity(&image->colormap[1]) ? 1 : 0);
1104 for (y=0; y < (ssize_t) image->rows; y++)
1106 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1107 if (p == (const Quantum *) NULL)
1112 for (x=0; x < (ssize_t) image->columns; x++)
1115 if (GetPixelIndex(image,p) == polarity)
1124 p+=GetPixelChannels(image);
1127 *q++=byte << (8-bit);
1128 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1130 if (image->previous == (Image *) NULL)
1132 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1134 if (status == MagickFalse)
1139 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1140 (void) WriteBlob(image,3*256,pcx_colormap);
1142 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1143 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1144 if (page_table == (MagickOffsetType *) NULL)
1148 if (GetNextImageInList(image) == (Image *) NULL)
1150 image=SyncNextImageInList(image);
1151 status=SetImageProgress(image,SaveImagesTag,scene++,
1152 GetImageListLength(image));
1153 if (status == MagickFalse)
1155 } while (image_info->adjoin != MagickFalse);
1156 if (page_table != (MagickOffsetType *) NULL)
1159 Write the DCX page table.
1161 page_table[scene+1]=0;
1162 offset=SeekBlob(image,0L,SEEK_SET);
1164 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1165 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1166 for (i=0; i <= (ssize_t) scene; i++)
1167 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1168 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1170 if (status == MagickFalse)
1175 message=GetExceptionMessage(errno);
1176 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1177 "UnableToWriteFile","`%s': %s",image->filename,message);
1178 message=DestroyString(message);
1180 (void) CloseBlob(image);