2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2014 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)
254 register unsigned char
275 assert(image_info != (const ImageInfo *) NULL);
276 assert(image_info->signature == MagickSignature);
277 if (image_info->debug != MagickFalse)
278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
279 image_info->filename);
280 assert(exception != (ExceptionInfo *) NULL);
281 assert(exception->signature == MagickSignature);
282 image=AcquireImage(image_info,exception);
283 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
284 if (status == MagickFalse)
286 image=DestroyImageList(image);
287 return((Image *) NULL);
290 Determine if this a PCX file.
292 page_table=(MagickOffsetType *) NULL;
293 if (LocaleCompare(image_info->magick,"DCX") == 0)
299 Read the DCX page table.
301 magic=ReadBlobLSBLong(image);
302 if (magic != 987654321)
303 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
304 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
305 sizeof(*page_table));
306 if (page_table == (MagickOffsetType *) NULL)
307 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
308 for (id=0; id < 1024; id++)
310 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
311 if (page_table[id] == 0)
315 if (page_table != (MagickOffsetType *) NULL)
317 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
319 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
321 pcx_colormap=(unsigned char *) NULL;
322 count=ReadBlob(image,1,&pcx_info.identifier);
323 for (id=1; id < 1024; id++)
326 Verify PCX identifier.
328 pcx_info.version=(unsigned char) ReadBlobByte(image);
329 if ((count == 0) || (pcx_info.identifier != 0x0a))
330 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
331 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
332 pcx_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
333 pcx_info.left=ReadBlobLSBShort(image);
334 pcx_info.top=ReadBlobLSBShort(image);
335 pcx_info.right=ReadBlobLSBShort(image);
336 pcx_info.bottom=ReadBlobLSBShort(image);
337 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
338 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
340 Read PCX raster colormap.
342 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
344 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
346 if ((image->columns == 0) || (image->rows == 0) ||
347 (pcx_info.bits_per_pixel == 0))
348 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
349 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
350 image->units=PixelsPerInchResolution;
351 image->resolution.x=(double) pcx_info.horizontal_resolution;
352 image->resolution.y=(double) pcx_info.vertical_resolution;
354 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
355 3*sizeof(*pcx_colormap));
356 if (pcx_colormap == (unsigned char *) NULL)
357 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
358 count=ReadBlob(image,3*image->colors,pcx_colormap);
359 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
360 pcx_info.planes=(unsigned char) ReadBlobByte(image);
362 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
363 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
364 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
365 image->colors=(size_t) MagickMin(one << (1UL*
366 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
367 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
368 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
369 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
370 image->storage_class=DirectClass;
372 for (i=0; i < (ssize_t) image->colors; i++)
374 image->colormap[i].red=ScaleCharToQuantum(*p++);
375 image->colormap[i].green=ScaleCharToQuantum(*p++);
376 image->colormap[i].blue=ScaleCharToQuantum(*p++);
378 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
379 pcx_info.palette_info=ReadBlobLSBShort(image);
380 for (i=0; i < 58; i++)
381 (void) ReadBlobByte(image);
382 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
383 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
388 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*pcx_info.planes;
389 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
390 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
391 pixel_info=AcquireVirtualMemory(pcx_packets,sizeof(*pixels));
392 if ((scanline == (unsigned char *) NULL) ||
393 (pixel_info == (MemoryInfo *) NULL))
395 if (scanline != (unsigned char *) NULL)
396 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
397 if (pixel_info != (MemoryInfo *) NULL)
398 pixel_info=RelinquishVirtualMemory(pixel_info);
399 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
401 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
403 Uncompress image data.
406 if (pcx_info.encoding == 0)
407 while (pcx_packets != 0)
409 packet=(unsigned char) ReadBlobByte(image);
410 if (EOFBlob(image) != MagickFalse)
411 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
416 while (pcx_packets != 0)
418 packet=(unsigned char) ReadBlobByte(image);
419 if (EOFBlob(image) != MagickFalse)
420 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
421 if ((packet & 0xc0) != 0xc0)
427 count=(ssize_t) (packet & 0x3f);
428 packet=(unsigned char) ReadBlobByte(image);
429 if (EOFBlob(image) != MagickFalse)
430 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
431 for ( ; count != 0; count--)
435 if (pcx_packets == 0)
439 if (image->storage_class == DirectClass)
440 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
443 if ((pcx_info.version == 5) ||
444 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
447 Initialize image colormap.
449 if (image->colors > 256)
450 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
451 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
456 image->colormap[0].red=(Quantum) 0;
457 image->colormap[0].green=(Quantum) 0;
458 image->colormap[0].blue=(Quantum) 0;
459 image->colormap[1].red=QuantumRange;
460 image->colormap[1].green=QuantumRange;
461 image->colormap[1].blue=QuantumRange;
464 if (image->colors > 16)
467 256 color images have their color map at the end of the file.
469 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
470 count=ReadBlob(image,3*image->colors,pcx_colormap);
472 for (i=0; i < (ssize_t) image->colors; i++)
474 image->colormap[i].red=ScaleCharToQuantum(*p++);
475 image->colormap[i].green=ScaleCharToQuantum(*p++);
476 image->colormap[i].blue=ScaleCharToQuantum(*p++);
479 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
482 Convert PCX raster image to pixel packets.
484 for (y=0; y < (ssize_t) image->rows; y++)
486 p=pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
487 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
488 if (q == (Quantum *) NULL)
491 if (image->storage_class == DirectClass)
492 for (i=0; i < pcx_info.planes; i++)
495 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
525 if (pcx_info.planes > 1)
527 for (x=0; x < (ssize_t) image->columns; x++)
529 for (i=0; i < pcx_info.planes; i++)
532 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
535 for (mask=0x80; mask != 0; mask>>=1)
545 switch (pcx_info.bits_per_pixel)
552 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
554 for (bit=7; bit >= 0; bit--)
555 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
558 if ((image->columns % 8) != 0)
560 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
561 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
568 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
570 *r++=(*p >> 6) & 0x3;
571 *r++=(*p >> 4) & 0x3;
572 *r++=(*p >> 2) & 0x3;
576 if ((image->columns % 4) != 0)
578 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
579 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
586 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
588 *r++=(*p >> 4) & 0xf;
592 if ((image->columns % 2) != 0)
593 *r++=(*p++ >> 4) & 0xf;
598 (void) CopyMagickMemory(r,p,image->columns);
605 Transfer image scanline.
608 for (x=0; x < (ssize_t) image->columns; x++)
610 if (image->storage_class == PseudoClass)
611 SetPixelIndex(image,*r++,q);
614 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
615 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
616 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
617 if (image->alpha_trait == BlendPixelTrait)
618 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
620 q+=GetPixelChannels(image);
622 if (SyncAuthenticPixels(image,exception) == MagickFalse)
624 if (image->previous == (Image *) NULL)
626 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
628 if (status == MagickFalse)
632 if (image->storage_class == PseudoClass)
633 (void) SyncImage(image,exception);
634 if (pcx_colormap != (unsigned char *) NULL)
635 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
636 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
637 pixel_info=RelinquishVirtualMemory(pixel_info);
638 if (EOFBlob(image) != MagickFalse)
640 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
645 Proceed to next image.
647 if (image_info->number_scenes != 0)
648 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
650 if (page_table == (MagickOffsetType *) NULL)
652 if (page_table[id] == 0)
654 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
656 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
657 count=ReadBlob(image,1,&pcx_info.identifier);
658 if ((count != 0) && (pcx_info.identifier == 0x0a))
661 Allocate next image structure.
663 AcquireNextImage(image_info,image,exception);
664 if (GetNextImageInList(image) == (Image *) NULL)
666 image=DestroyImageList(image);
667 return((Image *) NULL);
669 image=SyncNextImageInList(image);
670 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
672 if (status == MagickFalse)
676 if (page_table != (MagickOffsetType *) NULL)
677 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
678 (void) CloseBlob(image);
679 return(GetFirstImageInList(image));
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % R e g i s t e r P C X I m a g e %
691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
693 % RegisterPCXImage() adds attributes for the PCX image format to
694 % the list of supported formats. The attributes include the image format
695 % tag, a method to read and/or write the format, whether the format
696 % supports the saving of more than one frame to the same file or blob,
697 % whether the format supports native in-memory I/O, and a brief
698 % description of the format.
700 % The format of the RegisterPCXImage method is:
702 % size_t RegisterPCXImage(void)
705 ModuleExport size_t RegisterPCXImage(void)
710 entry=SetMagickInfo("DCX");
711 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
712 entry->encoder=(EncodeImageHandler *) WritePCXImage;
713 entry->seekable_stream=MagickTrue;
714 entry->magick=(IsImageFormatHandler *) IsDCX;
715 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
716 entry->module=ConstantString("PCX");
717 (void) RegisterMagickInfo(entry);
718 entry=SetMagickInfo("PCX");
719 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
720 entry->encoder=(EncodeImageHandler *) WritePCXImage;
721 entry->magick=(IsImageFormatHandler *) IsPCX;
722 entry->adjoin=MagickFalse;
723 entry->seekable_stream=MagickTrue;
724 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
725 entry->module=ConstantString("PCX");
726 (void) RegisterMagickInfo(entry);
727 return(MagickImageCoderSignature);
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % U n r e g i s t e r P C X I m a g e %
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741 % UnregisterPCXImage() removes format registrations made by the
742 % PCX module from the list of supported formats.
744 % The format of the UnregisterPCXImage method is:
746 % UnregisterPCXImage(void)
749 ModuleExport void UnregisterPCXImage(void)
751 (void) UnregisterMagickInfo("DCX");
752 (void) UnregisterMagickInfo("PCX");
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % W r i t e P C X I m a g e %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
769 % The format of the WritePCXImage method is:
771 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,
772 % Image *image,ExceptionInfo *exception)
774 % A description of each parameter follows.
776 % o image_info: the image info.
778 % o image: The image.
780 % o exception: return any errors or warnings in this structure.
784 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
785 const unsigned char *pixels,Image *image)
787 register const unsigned char
802 for (i=0; i < (ssize_t) pcx_info->planes; i++)
804 if (pcx_info->encoding == 0)
806 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
807 (void) WriteBlobByte(image,(unsigned char) (*q++));
813 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
816 if ((packet == previous) && (count < 63))
821 if ((count > 1) || ((previous & 0xc0) == 0xc0))
824 (void) WriteBlobByte(image,(unsigned char) count);
826 (void) WriteBlobByte(image,previous);
830 if ((count > 1) || ((previous & 0xc0) == 0xc0))
833 (void) WriteBlobByte(image,(unsigned char) count);
835 (void) WriteBlobByte(image,previous);
841 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
842 ExceptionInfo *exception)
858 register const Quantum
865 register unsigned char
879 Open output image file.
881 assert(image_info != (const ImageInfo *) NULL);
882 assert(image_info->signature == MagickSignature);
883 assert(image != (Image *) NULL);
884 assert(image->signature == MagickSignature);
885 if (image->debug != MagickFalse)
886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
887 assert(exception != (ExceptionInfo *) NULL);
888 assert(exception->signature == MagickSignature);
889 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
890 if (status == MagickFalse)
892 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
893 (void) TransformImageColorspace(image,sRGBColorspace,exception);
894 page_table=(MagickOffsetType *) NULL;
895 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
896 ((GetNextImageInList(image) != (Image *) NULL) &&
897 (image_info->adjoin != MagickFalse)))
900 Write the DCX page table.
902 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
903 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
904 sizeof(*page_table));
905 if (page_table == (MagickOffsetType *) NULL)
906 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
907 for (scene=0; scene < 1024; scene++)
908 (void) WriteBlobLSBLong(image,0x00000000L);
913 if (page_table != (MagickOffsetType *) NULL)
914 page_table[scene]=TellBlob(image);
916 Initialize PCX raster file header.
918 pcx_info.identifier=0x0a;
920 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
921 pcx_info.bits_per_pixel=8;
922 if ((image->storage_class == PseudoClass) &&
923 (IsImageMonochrome(image,exception) != MagickFalse))
924 pcx_info.bits_per_pixel=1;
927 pcx_info.right=(unsigned short) (image->columns-1);
928 pcx_info.bottom=(unsigned short) (image->rows-1);
929 switch (image->units)
931 case UndefinedResolution:
932 case PixelsPerInchResolution:
935 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
936 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
939 case PixelsPerCentimeterResolution:
941 pcx_info.horizontal_resolution=(unsigned short)
942 (2.54*image->resolution.x+0.5);
943 pcx_info.vertical_resolution=(unsigned short)
944 (2.54*image->resolution.y+0.5);
950 if ((image->storage_class == DirectClass) || (image->colors > 256))
953 if (image->alpha_trait == BlendPixelTrait)
956 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
957 pcx_info.bits_per_pixel+7)/8);
958 pcx_info.palette_info=1;
959 pcx_info.colormap_signature=0x0c;
963 (void) WriteBlobByte(image,pcx_info.identifier);
964 (void) WriteBlobByte(image,pcx_info.version);
965 (void) WriteBlobByte(image,pcx_info.encoding);
966 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
967 (void) WriteBlobLSBShort(image,pcx_info.left);
968 (void) WriteBlobLSBShort(image,pcx_info.top);
969 (void) WriteBlobLSBShort(image,pcx_info.right);
970 (void) WriteBlobLSBShort(image,pcx_info.bottom);
971 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
972 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
974 Dump colormap to file.
976 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
977 3*sizeof(*pcx_colormap));
978 if (pcx_colormap == (unsigned char *) NULL)
979 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
980 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
982 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
983 for (i=0; i < (ssize_t) image->colors; i++)
985 *q++=ScaleQuantumToChar(image->colormap[i].red);
986 *q++=ScaleQuantumToChar(image->colormap[i].green);
987 *q++=ScaleQuantumToChar(image->colormap[i].blue);
989 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
990 (void) WriteBlobByte(image,pcx_info.reserved);
991 (void) WriteBlobByte(image,pcx_info.planes);
992 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
993 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
994 for (i=0; i < 58; i++)
995 (void) WriteBlobByte(image,'\0');
996 length=(size_t) pcx_info.bytes_per_line;
997 pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
998 if (pixel_info == (MemoryInfo *) NULL)
999 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1000 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1002 if ((image->storage_class == DirectClass) || (image->colors > 256))
1005 Convert DirectClass image to PCX raster pixels.
1007 for (y=0; y < (ssize_t) image->rows; y++)
1010 for (i=0; i < pcx_info.planes; i++)
1012 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1013 if (p == (const Quantum *) NULL)
1019 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1021 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1022 p+=GetPixelChannels(image);
1028 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1030 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1031 p+=GetPixelChannels(image);
1037 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1039 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1040 p+=GetPixelChannels(image);
1047 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1049 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1050 p+=GetPixelChannels(image);
1056 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1058 if (image->previous == (Image *) NULL)
1060 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1062 if (status == MagickFalse)
1069 if (pcx_info.bits_per_pixel > 1)
1070 for (y=0; y < (ssize_t) image->rows; y++)
1072 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1073 if (p == (const Quantum *) NULL)
1076 for (x=0; x < (ssize_t) image->columns; x++)
1078 *q++=(unsigned char) GetPixelIndex(image,p);
1079 p+=GetPixelChannels(image);
1081 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1083 if (image->previous == (Image *) NULL)
1085 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1087 if (status == MagickFalse)
1096 register unsigned char
1101 Convert PseudoClass image to a PCX monochrome image.
1103 polarity=(Quantum) (GetPixelInfoIntensity(
1104 &image->colormap[0]) < (QuantumRange/2) ? 1 : 0);
1105 if (image->colors == 2)
1106 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) <
1107 GetPixelInfoIntensity(&image->colormap[1]) ? 1 : 0);
1108 for (y=0; y < (ssize_t) image->rows; y++)
1110 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1111 if (p == (const Quantum *) NULL)
1116 for (x=0; x < (ssize_t) image->columns; x++)
1119 if (GetPixelIndex(image,p) == polarity)
1128 p+=GetPixelChannels(image);
1131 *q++=byte << (8-bit);
1132 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1134 if (image->previous == (Image *) NULL)
1136 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1138 if (status == MagickFalse)
1143 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1144 (void) WriteBlob(image,3*256,pcx_colormap);
1146 pixel_info=RelinquishVirtualMemory(pixel_info);
1147 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1148 if (page_table == (MagickOffsetType *) NULL)
1152 if (GetNextImageInList(image) == (Image *) NULL)
1154 image=SyncNextImageInList(image);
1155 status=SetImageProgress(image,SaveImagesTag,scene++,
1156 GetImageListLength(image));
1157 if (status == MagickFalse)
1159 } while (image_info->adjoin != MagickFalse);
1160 if (page_table != (MagickOffsetType *) NULL)
1163 Write the DCX page table.
1165 page_table[scene+1]=0;
1166 offset=SeekBlob(image,0L,SEEK_SET);
1168 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1169 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1170 for (i=0; i <= (ssize_t) scene; i++)
1171 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1172 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1174 if (status == MagickFalse)
1179 message=GetExceptionMessage(errno);
1180 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1181 "UnableToWriteFile","`%s': %s",image->filename,message);
1182 message=DestroyString(message);
1184 (void) CloseBlob(image);