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 (void) TransformImageColorspace(image,sRGBColorspace,exception);
893 page_table=(MagickOffsetType *) NULL;
894 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
895 ((GetNextImageInList(image) != (Image *) NULL) &&
896 (image_info->adjoin != MagickFalse)))
899 Write the DCX page table.
901 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
902 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
903 sizeof(*page_table));
904 if (page_table == (MagickOffsetType *) NULL)
905 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
906 for (scene=0; scene < 1024; scene++)
907 (void) WriteBlobLSBLong(image,0x00000000L);
912 if (page_table != (MagickOffsetType *) NULL)
913 page_table[scene]=TellBlob(image);
915 Initialize PCX raster file header.
917 pcx_info.identifier=0x0a;
919 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
920 pcx_info.bits_per_pixel=8;
921 if ((image->storage_class == PseudoClass) &&
922 (IsImageMonochrome(image,exception) != MagickFalse))
923 pcx_info.bits_per_pixel=1;
926 pcx_info.right=(unsigned short) (image->columns-1);
927 pcx_info.bottom=(unsigned short) (image->rows-1);
928 switch (image->units)
930 case UndefinedResolution:
931 case PixelsPerInchResolution:
934 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
935 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
938 case PixelsPerCentimeterResolution:
940 pcx_info.horizontal_resolution=(unsigned short)
941 (2.54*image->resolution.x+0.5);
942 pcx_info.vertical_resolution=(unsigned short)
943 (2.54*image->resolution.y+0.5);
949 if ((image->storage_class == DirectClass) || (image->colors > 256))
952 if (image->alpha_trait == BlendPixelTrait)
955 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
956 pcx_info.bits_per_pixel+7)/8);
957 pcx_info.palette_info=1;
958 pcx_info.colormap_signature=0x0c;
962 (void) WriteBlobByte(image,pcx_info.identifier);
963 (void) WriteBlobByte(image,pcx_info.version);
964 (void) WriteBlobByte(image,pcx_info.encoding);
965 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
966 (void) WriteBlobLSBShort(image,pcx_info.left);
967 (void) WriteBlobLSBShort(image,pcx_info.top);
968 (void) WriteBlobLSBShort(image,pcx_info.right);
969 (void) WriteBlobLSBShort(image,pcx_info.bottom);
970 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
971 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
973 Dump colormap to file.
975 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
976 3*sizeof(*pcx_colormap));
977 if (pcx_colormap == (unsigned char *) NULL)
978 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
979 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
981 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
982 for (i=0; i < (ssize_t) image->colors; i++)
984 *q++=ScaleQuantumToChar(image->colormap[i].red);
985 *q++=ScaleQuantumToChar(image->colormap[i].green);
986 *q++=ScaleQuantumToChar(image->colormap[i].blue);
988 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
989 (void) WriteBlobByte(image,pcx_info.reserved);
990 (void) WriteBlobByte(image,pcx_info.planes);
991 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
992 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
993 for (i=0; i < 58; i++)
994 (void) WriteBlobByte(image,'\0');
995 length=(size_t) pcx_info.bytes_per_line;
996 pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
997 if (pixel_info == (MemoryInfo *) NULL)
998 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
999 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1001 if ((image->storage_class == DirectClass) || (image->colors > 256))
1004 Convert DirectClass image to PCX raster pixels.
1006 for (y=0; y < (ssize_t) image->rows; y++)
1009 for (i=0; i < pcx_info.planes; i++)
1011 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1012 if (p == (const Quantum *) NULL)
1018 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1020 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1021 p+=GetPixelChannels(image);
1027 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1029 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1030 p+=GetPixelChannels(image);
1036 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1038 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1039 p+=GetPixelChannels(image);
1046 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1048 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1049 p+=GetPixelChannels(image);
1055 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1057 if (image->previous == (Image *) NULL)
1059 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1061 if (status == MagickFalse)
1068 if (pcx_info.bits_per_pixel > 1)
1069 for (y=0; y < (ssize_t) image->rows; y++)
1071 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1072 if (p == (const Quantum *) NULL)
1075 for (x=0; x < (ssize_t) image->columns; x++)
1077 *q++=(unsigned char) GetPixelIndex(image,p);
1078 p+=GetPixelChannels(image);
1080 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1082 if (image->previous == (Image *) NULL)
1084 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1086 if (status == MagickFalse)
1092 register unsigned char
1097 Convert PseudoClass image to a PCX monochrome image.
1099 for (y=0; y < (ssize_t) image->rows; y++)
1101 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1102 if (p == (const Quantum *) NULL)
1107 for (x=0; x < (ssize_t) image->columns; x++)
1110 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
1119 p+=GetPixelChannels(image);
1122 *q++=byte << (8-bit);
1123 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
1125 if (image->previous == (Image *) NULL)
1127 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1129 if (status == MagickFalse)
1134 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1135 (void) WriteBlob(image,3*256,pcx_colormap);
1137 pixel_info=RelinquishVirtualMemory(pixel_info);
1138 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1139 if (page_table == (MagickOffsetType *) NULL)
1143 if (GetNextImageInList(image) == (Image *) NULL)
1145 image=SyncNextImageInList(image);
1146 status=SetImageProgress(image,SaveImagesTag,scene++,
1147 GetImageListLength(image));
1148 if (status == MagickFalse)
1150 } while (image_info->adjoin != MagickFalse);
1151 if (page_table != (MagickOffsetType *) NULL)
1154 Write the DCX page table.
1156 page_table[scene+1]=0;
1157 offset=SeekBlob(image,0L,SEEK_SET);
1159 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1160 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1161 for (i=0; i <= (ssize_t) scene; i++)
1162 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1163 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1165 if (status == MagickFalse)
1170 message=GetExceptionMessage(errno);
1171 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1172 "UnableToWriteFile","`%s': %s",image->filename,message);
1173 message=DestroyString(message);
1175 (void) CloseBlob(image);