2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2011 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/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
69 typedef struct _PCXInfo
82 horizontal_resolution,
100 static MagickBooleanType
101 WritePCXImage(const ImageInfo *,Image *);
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % IsDCX() returns MagickTrue if the image format type, identified by the
115 % magick string, is DCX.
117 % The format of the IsDCX method is:
119 % MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
121 % A description of each parameter follows:
123 % o magick: compare image format pattern against these bytes.
125 % 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.
164 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
168 if (memcmp(magick,"\012\002",2) == 0)
170 if (memcmp(magick,"\012\005",2) == 0)
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 % R e a d P C X I m a g e %
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
187 % It allocates the memory necessary for the new Image structure and returns
188 % a pointer to the new image.
190 % The format of the ReadPCXImage method is:
192 % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
194 % A description of each parameter follows:
196 % o image_info: the image info.
198 % o exception: return any errors or warnings in this structure.
202 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
209 static inline size_t MagickMax(const size_t x,const size_t y)
216 static inline size_t MagickMin(const size_t x,const size_t y)
223 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
252 register unsigned char
273 assert(image_info != (const ImageInfo *) NULL);
274 assert(image_info->signature == MagickSignature);
275 if (image_info->debug != MagickFalse)
276 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
277 image_info->filename);
278 assert(exception != (ExceptionInfo *) NULL);
279 assert(exception->signature == MagickSignature);
280 image=AcquireImage(image_info);
281 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
282 if (status == MagickFalse)
284 image=DestroyImageList(image);
285 return((Image *) NULL);
288 Determine if this a PCX file.
290 page_table=(MagickOffsetType *) NULL;
291 if (LocaleCompare(image_info->magick,"DCX") == 0)
297 Read the DCX page table.
299 magic=ReadBlobLSBLong(image);
300 if (magic != 987654321)
301 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
302 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
303 sizeof(*page_table));
304 if (page_table == (MagickOffsetType *) NULL)
305 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
306 for (id=0; id < 1024; id++)
308 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
309 if (page_table[id] == 0)
313 if (page_table != (MagickOffsetType *) NULL)
315 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
317 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
319 pcx_colormap=(unsigned char *) NULL;
320 count=ReadBlob(image,1,&pcx_info.identifier);
321 for (id=1; id < 1024; id++)
324 Verify PCX identifier.
326 pcx_info.version=(unsigned char) ReadBlobByte(image);
327 if ((count == 0) || (pcx_info.identifier != 0x0a))
328 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
329 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
330 pcx_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
331 pcx_info.left=ReadBlobLSBShort(image);
332 pcx_info.top=ReadBlobLSBShort(image);
333 pcx_info.right=ReadBlobLSBShort(image);
334 pcx_info.bottom=ReadBlobLSBShort(image);
335 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
336 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
338 Read PCX raster colormap.
340 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
342 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
344 if ((image->columns == 0) || (image->rows == 0) ||
345 (pcx_info.bits_per_pixel == 0))
346 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
347 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
348 image->units=PixelsPerInchResolution;
349 image->x_resolution=(double) pcx_info.horizontal_resolution;
350 image->y_resolution=(double) pcx_info.vertical_resolution;
352 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
353 3*sizeof(*pcx_colormap));
354 if (pcx_colormap == (unsigned char *) NULL)
355 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
356 count=ReadBlob(image,3*image->colors,pcx_colormap);
357 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
358 pcx_info.planes=(unsigned char) ReadBlobByte(image);
360 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
361 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
362 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
363 image->colors=(size_t) MagickMin(one << (1UL*
364 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
365 if (AcquireImageColormap(image,image->colors) == MagickFalse)
366 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
367 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
368 image->storage_class=DirectClass;
370 for (i=0; i < (ssize_t) image->colors; i++)
372 image->colormap[i].red=ScaleCharToQuantum(*p++);
373 image->colormap[i].green=ScaleCharToQuantum(*p++);
374 image->colormap[i].blue=ScaleCharToQuantum(*p++);
376 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
377 pcx_info.palette_info=ReadBlobLSBShort(image);
378 for (i=0; i < 58; i++)
379 (void) ReadBlobByte(image);
380 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
381 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
386 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
388 pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
389 sizeof(*pcx_pixels));
390 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
391 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
392 if ((pcx_pixels == (unsigned char *) NULL) ||
393 (scanline == (unsigned char *) NULL))
394 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
396 Uncompress image data.
399 if (pcx_info.encoding == 0)
400 while (pcx_packets != 0)
402 packet=(unsigned char) ReadBlobByte(image);
403 if (EOFBlob(image) != MagickFalse)
409 while (pcx_packets != 0)
411 packet=(unsigned char) ReadBlobByte(image);
412 if (EOFBlob(image) != MagickFalse)
414 if ((packet & 0xc0) != 0xc0)
420 count=(ssize_t) (packet & 0x3f);
421 packet=(unsigned char) ReadBlobByte(image);
422 if (EOFBlob(image) != MagickFalse)
424 for ( ; count != 0; count--)
428 if (pcx_packets == 0)
432 if (image->storage_class == DirectClass)
433 image->matte=pcx_info.planes > 3 ? MagickTrue : MagickFalse;
435 if ((pcx_info.version == 5) ||
436 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
439 Initialize image colormap.
441 if (image->colors > 256)
442 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
443 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
448 image->colormap[0].red=(Quantum) 0;
449 image->colormap[0].green=(Quantum) 0;
450 image->colormap[0].blue=(Quantum) 0;
451 image->colormap[1].red=(Quantum) QuantumRange;
452 image->colormap[1].green=(Quantum) QuantumRange;
453 image->colormap[1].blue=(Quantum) QuantumRange;
456 if (image->colors > 16)
459 256 color images have their color map at the end of the file.
461 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
462 count=ReadBlob(image,3*image->colors,pcx_colormap);
464 for (i=0; i < (ssize_t) image->colors; i++)
466 image->colormap[i].red=ScaleCharToQuantum(*p++);
467 image->colormap[i].green=ScaleCharToQuantum(*p++);
468 image->colormap[i].blue=ScaleCharToQuantum(*p++);
471 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
474 Convert PCX raster image to pixel packets.
476 for (y=0; y < (ssize_t) image->rows; y++)
478 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
479 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
480 if (q == (const Quantum *) NULL)
483 if (image->storage_class == DirectClass)
484 for (i=0; i < pcx_info.planes; i++)
487 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
517 if (pcx_info.planes > 1)
519 for (x=0; x < (ssize_t) image->columns; x++)
521 for (i=0; i < pcx_info.planes; i++)
524 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
527 for (mask=0x80; mask != 0; mask>>=1)
537 switch (pcx_info.bits_per_pixel)
544 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
546 for (bit=7; bit >= 0; bit--)
547 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
550 if ((image->columns % 8) != 0)
552 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
553 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
560 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
562 *r++=(*p >> 6) & 0x3;
563 *r++=(*p >> 4) & 0x3;
564 *r++=(*p >> 2) & 0x3;
568 if ((image->columns % 4) != 0)
570 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
571 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
578 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
580 *r++=(*p >> 4) & 0xf;
584 if ((image->columns % 2) != 0)
585 *r++=(*p++ >> 4) & 0xf;
590 (void) CopyMagickMemory(r,p,image->columns);
597 Transfer image scanline.
600 for (x=0; x < (ssize_t) image->columns; x++)
602 if (image->storage_class == PseudoClass)
603 SetPixelIndex(image,*r++,q);
606 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
607 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
608 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
609 if (image->matte != MagickFalse)
610 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
612 q+=GetPixelChannels(image);
614 if (SyncAuthenticPixels(image,exception) == MagickFalse)
616 if (image->previous == (Image *) NULL)
618 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
620 if (status == MagickFalse)
624 if (image->storage_class == PseudoClass)
625 (void) SyncImage(image);
626 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
627 if (pcx_colormap != (unsigned char *) NULL)
628 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
629 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
630 if (EOFBlob(image) != MagickFalse)
632 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
637 Proceed to next image.
639 if (image_info->number_scenes != 0)
640 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
642 if (page_table == (MagickOffsetType *) NULL)
644 if (page_table[id] == 0)
646 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
648 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
649 count=ReadBlob(image,1,&pcx_info.identifier);
650 if ((count != 0) && (pcx_info.identifier == 0x0a))
653 Allocate next image structure.
655 AcquireNextImage(image_info,image);
656 if (GetNextImageInList(image) == (Image *) NULL)
658 image=DestroyImageList(image);
659 return((Image *) NULL);
661 image=SyncNextImageInList(image);
662 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
664 if (status == MagickFalse)
668 if (page_table != (MagickOffsetType *) NULL)
669 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
670 (void) CloseBlob(image);
671 return(GetFirstImageInList(image));
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % R e g i s t e r P C X I m a g e %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % RegisterPCXImage() adds attributes for the PCX image format to
686 % the list of supported formats. The attributes include the image format
687 % tag, a method to read and/or write the format, whether the format
688 % supports the saving of more than one frame to the same file or blob,
689 % whether the format supports native in-memory I/O, and a brief
690 % description of the format.
692 % The format of the RegisterPCXImage method is:
694 % size_t RegisterPCXImage(void)
697 ModuleExport size_t RegisterPCXImage(void)
702 entry=SetMagickInfo("DCX");
703 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
704 entry->encoder=(EncodeImageHandler *) WritePCXImage;
705 entry->seekable_stream=MagickTrue;
706 entry->magick=(IsImageFormatHandler *) IsDCX;
707 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
708 entry->module=ConstantString("PCX");
709 (void) RegisterMagickInfo(entry);
710 entry=SetMagickInfo("PCX");
711 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
712 entry->encoder=(EncodeImageHandler *) WritePCXImage;
713 entry->magick=(IsImageFormatHandler *) IsPCX;
714 entry->adjoin=MagickFalse;
715 entry->seekable_stream=MagickTrue;
716 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
717 entry->module=ConstantString("PCX");
718 (void) RegisterMagickInfo(entry);
719 return(MagickImageCoderSignature);
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % U n r e g i s t e r P C X I m a g e %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % UnregisterPCXImage() removes format registrations made by the
734 % PCX module from the list of supported formats.
736 % The format of the UnregisterPCXImage method is:
738 % UnregisterPCXImage(void)
741 ModuleExport void UnregisterPCXImage(void)
743 (void) UnregisterMagickInfo("DCX");
744 (void) UnregisterMagickInfo("PCX");
748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 % W r i t e P C X I m a g e %
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
761 % The format of the WritePCXImage method is:
763 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
765 % A description of each parameter follows.
767 % o image_info: the image info.
769 % o image: The image.
773 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
774 const unsigned char *pixels,Image *image)
776 register const unsigned char
791 for (i=0; i < (ssize_t) pcx_info->planes; i++)
793 if (pcx_info->encoding == 0)
795 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
796 (void) WriteBlobByte(image,(unsigned char) (*q++));
802 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
805 if ((packet == previous) && (count < 63))
810 if ((count > 1) || ((previous & 0xc0) == 0xc0))
813 (void) WriteBlobByte(image,(unsigned char) count);
815 (void) WriteBlobByte(image,previous);
819 if ((count > 1) || ((previous & 0xc0) == 0xc0))
822 (void) WriteBlobByte(image,(unsigned char) count);
824 (void) WriteBlobByte(image,previous);
830 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
843 register const Quantum
850 register unsigned char
864 Open output image file.
866 assert(image_info != (const ImageInfo *) NULL);
867 assert(image_info->signature == MagickSignature);
868 assert(image != (Image *) NULL);
869 assert(image->signature == MagickSignature);
870 if (image->debug != MagickFalse)
871 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
872 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
873 if (status == MagickFalse)
875 if (image->colorspace != RGBColorspace)
876 (void) TransformImageColorspace(image,RGBColorspace);
877 page_table=(MagickOffsetType *) NULL;
878 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
879 ((GetNextImageInList(image) != (Image *) NULL) &&
880 (image_info->adjoin != MagickFalse)))
883 Write the DCX page table.
885 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
886 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
887 sizeof(*page_table));
888 if (page_table == (MagickOffsetType *) NULL)
889 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
890 for (scene=0; scene < 1024; scene++)
891 (void) WriteBlobLSBLong(image,0x00000000L);
896 if (page_table != (MagickOffsetType *) NULL)
897 page_table[scene]=TellBlob(image);
899 Initialize PCX raster file header.
901 pcx_info.identifier=0x0a;
903 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
904 pcx_info.bits_per_pixel=8;
905 if ((image->storage_class == PseudoClass) &&
906 (IsImageMonochrome(image,&image->exception) != MagickFalse))
907 pcx_info.bits_per_pixel=1;
910 pcx_info.right=(unsigned short) (image->columns-1);
911 pcx_info.bottom=(unsigned short) (image->rows-1);
912 switch (image->units)
914 case UndefinedResolution:
915 case PixelsPerInchResolution:
918 pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
919 pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
922 case PixelsPerCentimeterResolution:
924 pcx_info.horizontal_resolution=(unsigned short)
925 (2.54*image->x_resolution+0.5);
926 pcx_info.vertical_resolution=(unsigned short)
927 (2.54*image->y_resolution+0.5);
933 if ((image->storage_class == DirectClass) || (image->colors > 256))
936 if (image->matte != MagickFalse)
939 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
940 pcx_info.bits_per_pixel+7)/8);
941 pcx_info.palette_info=1;
942 pcx_info.colormap_signature=0x0c;
946 (void) WriteBlobByte(image,pcx_info.identifier);
947 (void) WriteBlobByte(image,pcx_info.version);
948 (void) WriteBlobByte(image,pcx_info.encoding);
949 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
950 (void) WriteBlobLSBShort(image,pcx_info.left);
951 (void) WriteBlobLSBShort(image,pcx_info.top);
952 (void) WriteBlobLSBShort(image,pcx_info.right);
953 (void) WriteBlobLSBShort(image,pcx_info.bottom);
954 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
955 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
957 Dump colormap to file.
959 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
960 3*sizeof(*pcx_colormap));
961 if (pcx_colormap == (unsigned char *) NULL)
962 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
963 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
965 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
966 for (i=0; i < (ssize_t) image->colors; i++)
968 *q++=ScaleQuantumToChar(image->colormap[i].red);
969 *q++=ScaleQuantumToChar(image->colormap[i].green);
970 *q++=ScaleQuantumToChar(image->colormap[i].blue);
972 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
973 (void) WriteBlobByte(image,pcx_info.reserved);
974 (void) WriteBlobByte(image,pcx_info.planes);
975 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
976 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
977 for (i=0; i < 58; i++)
978 (void) WriteBlobByte(image,'\0');
979 length=(size_t) pcx_info.bytes_per_line;
980 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
981 sizeof(*pcx_pixels));
982 if (pcx_pixels == (unsigned char *) NULL)
983 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
985 if ((image->storage_class == DirectClass) || (image->colors > 256))
991 Convert DirectClass image to PCX raster pixels.
993 for (y=0; y < (ssize_t) image->rows; y++)
995 pixels=GetVirtualPixels(image,0,y,image->columns,1,
997 if (pixels == (const Quantum *) NULL)
1000 for (i=0; i < pcx_info.planes; i++)
1007 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1009 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1010 p+=GetPixelChannels(image);
1016 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1018 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1019 p+=GetPixelChannels(image);
1025 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1027 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1028 p+=GetPixelChannels(image);
1035 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1037 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1038 p+=GetPixelChannels(image);
1044 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1046 if (image->previous == (Image *) NULL)
1048 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1050 if (status == MagickFalse)
1057 if (pcx_info.bits_per_pixel > 1)
1058 for (y=0; y < (ssize_t) image->rows; y++)
1060 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1061 if (p == (const Quantum *) NULL)
1064 for (x=0; x < (ssize_t) image->columns; x++)
1066 *q++=(unsigned char) GetPixelIndex(image,p);
1067 p+=GetPixelChannels(image);
1069 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1071 if (image->previous == (Image *) NULL)
1073 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1075 if (status == MagickFalse)
1084 register unsigned char
1089 Convert PseudoClass image to a PCX monochrome image.
1091 polarity=(Quantum) (GetPixelPacketIntensity(
1092 &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
1093 if (image->colors == 2)
1094 polarity=(Quantum) (GetPixelPacketIntensity(&image->colormap[0]) <
1095 GetPixelPacketIntensity(&image->colormap[1]) ? 1 : 0);
1096 for (y=0; y < (ssize_t) image->rows; y++)
1098 p=GetVirtualPixels(image,0,y,image->columns,1,
1100 if (p == (const Quantum *) NULL)
1105 for (x=0; x < (ssize_t) image->columns; x++)
1108 if (GetPixelIndex(image,p) == polarity)
1117 p+=GetPixelChannels(image);
1120 *q++=byte << (8-bit);
1121 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1123 if (image->previous == (Image *) NULL)
1125 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1127 if (status == MagickFalse)
1132 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1133 (void) WriteBlob(image,3*256,pcx_colormap);
1135 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1136 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1137 if (page_table == (MagickOffsetType *) NULL)
1141 if (GetNextImageInList(image) == (Image *) NULL)
1143 image=SyncNextImageInList(image);
1144 status=SetImageProgress(image,SaveImagesTag,scene++,
1145 GetImageListLength(image));
1146 if (status == MagickFalse)
1148 } while (image_info->adjoin != MagickFalse);
1149 if (page_table != (MagickOffsetType *) NULL)
1152 Write the DCX page table.
1154 page_table[scene+1]=0;
1155 offset=SeekBlob(image,0L,SEEK_SET);
1157 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1158 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1159 for (i=0; i <= (ssize_t) scene; i++)
1160 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1161 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1163 if (status == MagickFalse)
1168 message=GetExceptionMessage(errno);
1169 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1170 FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1171 message=DestroyString(message);
1173 (void) CloseBlob(image);