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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colormap.h"
49 #include "magick/colorspace.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/string_.h"
62 #include "magick/module.h"
67 typedef struct _PCXInfo
80 horizontal_resolution,
98 static MagickBooleanType
99 WritePCXImage(const ImageInfo *,Image *);
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % IsDCX() returns MagickTrue if the image format type, identified by the
113 % magick string, is DCX.
115 % The format of the IsDCX method is:
117 % MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
119 % A description of each parameter follows:
121 % o magick: compare image format pattern against these bytes.
123 % o length: Specifies the length of the magick string.
127 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
131 if (memcmp(magick,"\261\150\336\72",4) == 0)
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % IsPCX() returns MagickTrue if the image format type, identified by the
148 % magick string, is PCX.
150 % The format of the IsPCX method is:
152 % MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
154 % A description of each parameter follows:
156 % o magick: compare image format pattern against these bytes.
158 % o length: Specifies the length of the magick string.
162 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
166 if (memcmp(magick,"\012\002",2) == 0)
168 if (memcmp(magick,"\012\005",2) == 0)
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 % R e a d P C X I m a g e %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
185 % It allocates the memory necessary for the new Image structure and returns
186 % a pointer to the new image.
188 % The format of the ReadPCXImage method is:
190 % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
192 % A description of each parameter follows:
194 % o image_info: the image info.
196 % o exception: return any errors or warnings in this structure.
200 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
207 static inline size_t MagickMax(const size_t x,const size_t y)
214 static inline size_t MagickMin(const size_t x,const size_t y)
221 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);
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->x_resolution=(double) pcx_info.horizontal_resolution;
351 image->y_resolution=(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) == 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=(Quantum) QuantumRange;
453 image->colormap[1].green=(Quantum) QuantumRange;
454 image->colormap[1].blue=(Quantum) 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 == (PixelPacket *) NULL)
483 indexes=GetAuthenticIndexQueue(image);
485 if (image->storage_class == DirectClass)
486 for (i=0; i < pcx_info.planes; i++)
489 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
519 if (pcx_info.planes > 1)
521 for (x=0; x < (ssize_t) image->columns; x++)
523 for (i=0; i < pcx_info.planes; i++)
526 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
529 for (mask=0x80; mask != 0; mask>>=1)
539 switch (pcx_info.bits_per_pixel)
546 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
548 for (bit=7; bit >= 0; bit--)
549 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
552 if ((image->columns % 8) != 0)
554 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
555 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
562 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
564 *r++=(*p >> 6) & 0x3;
565 *r++=(*p >> 4) & 0x3;
566 *r++=(*p >> 2) & 0x3;
570 if ((image->columns % 4) != 0)
572 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
573 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
580 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
582 *r++=(*p >> 4) & 0xf;
586 if ((image->columns % 2) != 0)
587 *r++=(*p++ >> 4) & 0xf;
592 (void) CopyMagickMemory(r,p,image->columns);
599 Transfer image scanline.
602 for (x=0; x < (ssize_t) image->columns; x++)
604 if (image->storage_class == PseudoClass)
605 SetIndexPixelComponent(indexes+x,*r++);
608 SetRedPixelComponent(q,ScaleCharToQuantum(*r++));
609 SetGreenPixelComponent(q,ScaleCharToQuantum(*r++));
610 SetBluePixelComponent(q,ScaleCharToQuantum(*r++));
611 if (image->matte != MagickFalse)
612 SetAlphaPixelComponent(q,ScaleCharToQuantum(*r++));
616 if (SyncAuthenticPixels(image,exception) == MagickFalse)
618 if (image->previous == (Image *) NULL)
620 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
622 if (status == MagickFalse)
626 if (image->storage_class == PseudoClass)
627 (void) SyncImage(image);
628 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
629 if (pcx_colormap != (unsigned char *) NULL)
630 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
631 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
632 if (EOFBlob(image) != MagickFalse)
634 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
639 Proceed to next image.
641 if (image_info->number_scenes != 0)
642 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
644 if (page_table == (MagickOffsetType *) NULL)
646 if (page_table[id] == 0)
648 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
650 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
651 count=ReadBlob(image,1,&pcx_info.identifier);
652 if ((count != 0) && (pcx_info.identifier == 0x0a))
655 Allocate next image structure.
657 AcquireNextImage(image_info,image);
658 if (GetNextImageInList(image) == (Image *) NULL)
660 image=DestroyImageList(image);
661 return((Image *) NULL);
663 image=SyncNextImageInList(image);
664 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
666 if (status == MagickFalse)
670 if (page_table != (MagickOffsetType *) NULL)
671 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
672 (void) CloseBlob(image);
673 return(GetFirstImageInList(image));
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % R e g i s t e r P C X I m a g e %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % RegisterPCXImage() adds attributes for the PCX image format to
688 % the list of supported formats. The attributes include the image format
689 % tag, a method to read and/or write the format, whether the format
690 % supports the saving of more than one frame to the same file or blob,
691 % whether the format supports native in-memory I/O, and a brief
692 % description of the format.
694 % The format of the RegisterPCXImage method is:
696 % size_t RegisterPCXImage(void)
699 ModuleExport size_t RegisterPCXImage(void)
704 entry=SetMagickInfo("DCX");
705 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
706 entry->encoder=(EncodeImageHandler *) WritePCXImage;
707 entry->seekable_stream=MagickTrue;
708 entry->magick=(IsImageFormatHandler *) IsDCX;
709 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
710 entry->module=ConstantString("PCX");
711 (void) RegisterMagickInfo(entry);
712 entry=SetMagickInfo("PCX");
713 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
714 entry->encoder=(EncodeImageHandler *) WritePCXImage;
715 entry->magick=(IsImageFormatHandler *) IsPCX;
716 entry->adjoin=MagickFalse;
717 entry->seekable_stream=MagickTrue;
718 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
719 entry->module=ConstantString("PCX");
720 (void) RegisterMagickInfo(entry);
721 return(MagickImageCoderSignature);
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % U n r e g i s t e r P C X I m a g e %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % UnregisterPCXImage() removes format registrations made by the
736 % PCX module from the list of supported formats.
738 % The format of the UnregisterPCXImage method is:
740 % UnregisterPCXImage(void)
743 ModuleExport void UnregisterPCXImage(void)
745 (void) UnregisterMagickInfo("DCX");
746 (void) UnregisterMagickInfo("PCX");
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % W r i t e P C X I m a g e %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
763 % The format of the WritePCXImage method is:
765 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
767 % A description of each parameter follows.
769 % o image_info: the image info.
771 % o image: The image.
775 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
776 const unsigned char *pixels,Image *image)
778 register const unsigned char
793 for (i=0; i < (ssize_t) pcx_info->planes; i++)
795 if (pcx_info->encoding == 0)
797 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
798 (void) WriteBlobByte(image,(unsigned char) (*q++));
804 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
807 if ((packet == previous) && (count < 63))
812 if ((count > 1) || ((previous & 0xc0) == 0xc0))
815 (void) WriteBlobByte(image,(unsigned char) count);
817 (void) WriteBlobByte(image,previous);
821 if ((count > 1) || ((previous & 0xc0) == 0xc0))
824 (void) WriteBlobByte(image,(unsigned char) count);
826 (void) WriteBlobByte(image,previous);
832 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
845 register const IndexPacket
848 register const PixelPacket
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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
878 if (status == MagickFalse)
880 if (image->colorspace != RGBColorspace)
881 (void) TransformImageColorspace(image,RGBColorspace);
882 page_table=(MagickOffsetType *) NULL;
883 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
884 ((GetNextImageInList(image) != (Image *) NULL) &&
885 (image_info->adjoin != MagickFalse)))
888 Write the DCX page table.
890 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
891 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
892 sizeof(*page_table));
893 if (page_table == (MagickOffsetType *) NULL)
894 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
895 for (scene=0; scene < 1024; scene++)
896 (void) WriteBlobLSBLong(image,0x00000000L);
901 if (page_table != (MagickOffsetType *) NULL)
902 page_table[scene]=TellBlob(image);
904 Initialize PCX raster file header.
906 pcx_info.identifier=0x0a;
908 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
909 pcx_info.bits_per_pixel=8;
910 if ((image->storage_class == PseudoClass) &&
911 (IsMonochromeImage(image,&image->exception) != MagickFalse))
912 pcx_info.bits_per_pixel=1;
915 pcx_info.right=(unsigned short) (image->columns-1);
916 pcx_info.bottom=(unsigned short) (image->rows-1);
917 switch (image->units)
919 case UndefinedResolution:
920 case PixelsPerInchResolution:
923 pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
924 pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
927 case PixelsPerCentimeterResolution:
929 pcx_info.horizontal_resolution=(unsigned short)
930 (2.54*image->x_resolution+0.5);
931 pcx_info.vertical_resolution=(unsigned short)
932 (2.54*image->y_resolution+0.5);
938 if ((image->storage_class == DirectClass) || (image->colors > 256))
941 if (image->matte != MagickFalse)
944 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
945 pcx_info.bits_per_pixel+7)/8);
946 pcx_info.palette_info=1;
947 pcx_info.colormap_signature=0x0c;
951 (void) WriteBlobByte(image,pcx_info.identifier);
952 (void) WriteBlobByte(image,pcx_info.version);
953 (void) WriteBlobByte(image,pcx_info.encoding);
954 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
955 (void) WriteBlobLSBShort(image,pcx_info.left);
956 (void) WriteBlobLSBShort(image,pcx_info.top);
957 (void) WriteBlobLSBShort(image,pcx_info.right);
958 (void) WriteBlobLSBShort(image,pcx_info.bottom);
959 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
960 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
962 Dump colormap to file.
964 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
965 3*sizeof(*pcx_colormap));
966 if (pcx_colormap == (unsigned char *) NULL)
967 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
968 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
970 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
971 for (i=0; i < (ssize_t) image->colors; i++)
973 *q++=ScaleQuantumToChar(image->colormap[i].red);
974 *q++=ScaleQuantumToChar(image->colormap[i].green);
975 *q++=ScaleQuantumToChar(image->colormap[i].blue);
977 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
978 (void) WriteBlobByte(image,pcx_info.reserved);
979 (void) WriteBlobByte(image,pcx_info.planes);
980 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
981 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
982 for (i=0; i < 58; i++)
983 (void) WriteBlobByte(image,'\0');
984 length=(size_t) pcx_info.bytes_per_line;
985 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
986 sizeof(*pcx_pixels));
987 if (pcx_pixels == (unsigned char *) NULL)
988 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
990 if ((image->storage_class == DirectClass) || (image->colors > 256))
996 Convert DirectClass image to PCX raster pixels.
998 for (y=0; y < (ssize_t) image->rows; y++)
1000 pixels=GetVirtualPixels(image,0,y,image->columns,1,
1002 if (pixels == (const PixelPacket *) NULL)
1005 for (i=0; i < pcx_info.planes; i++)
1012 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1014 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1021 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1023 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1030 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1032 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1040 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1042 *q++=ScaleQuantumToChar((Quantum)
1043 (GetAlphaPixelComponent(p)));
1050 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1052 if (image->previous == (Image *) NULL)
1054 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1056 if (status == MagickFalse)
1063 if (pcx_info.bits_per_pixel > 1)
1064 for (y=0; y < (ssize_t) image->rows; y++)
1066 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1067 if (p == (const PixelPacket *) NULL)
1069 indexes=GetVirtualIndexQueue(image);
1071 for (x=0; x < (ssize_t) image->columns; x++)
1072 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1073 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1075 if (image->previous == (Image *) NULL)
1077 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1079 if (status == MagickFalse)
1088 register unsigned char
1093 Convert PseudoClass image to a PCX monochrome image.
1095 polarity=(IndexPacket) (PixelIntensityToQuantum(
1096 &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
1097 if (image->colors == 2)
1098 polarity=(IndexPacket) (
1099 PixelIntensityToQuantum(&image->colormap[0]) <
1100 PixelIntensityToQuantum(&image->colormap[1]) ? 1 : 0);
1101 for (y=0; y < (ssize_t) image->rows; y++)
1103 p=GetVirtualPixels(image,0,y,image->columns,1,
1105 if (p == (const PixelPacket *) NULL)
1107 indexes=GetVirtualIndexQueue(image);
1111 for (x=0; x < (ssize_t) image->columns; x++)
1114 if (GetIndexPixelComponent(indexes+x) == polarity)
1126 *q++=byte << (8-bit);
1127 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1129 if (image->previous == (Image *) NULL)
1131 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1133 if (status == MagickFalse)
1138 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1139 (void) WriteBlob(image,3*256,pcx_colormap);
1141 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1142 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1143 if (page_table == (MagickOffsetType *) NULL)
1147 if (GetNextImageInList(image) == (Image *) NULL)
1149 image=SyncNextImageInList(image);
1150 status=SetImageProgress(image,SaveImagesTag,scene++,
1151 GetImageListLength(image));
1152 if (status == MagickFalse)
1154 } while (image_info->adjoin != MagickFalse);
1155 if (page_table != (MagickOffsetType *) NULL)
1158 Write the DCX page table.
1160 page_table[scene+1]=0;
1161 offset=SeekBlob(image,0L,SEEK_SET);
1163 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1164 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1165 for (i=0; i <= (ssize_t) scene; i++)
1166 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1167 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1169 if (status == MagickFalse)
1174 message=GetExceptionMessage(errno);
1175 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1176 FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1177 message=DestroyString(message);
1179 (void) CloseBlob(image);