2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write ZSoft IBM PC Paintbrush Image Format %
20 % Copyright 1999-2010 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)
256 register unsigned char
276 assert(image_info != (const ImageInfo *) NULL);
277 assert(image_info->signature == MagickSignature);
278 if (image_info->debug != MagickFalse)
279 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
280 image_info->filename);
281 assert(exception != (ExceptionInfo *) NULL);
282 assert(exception->signature == MagickSignature);
283 image=AcquireImage(image_info);
284 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
285 if (status == MagickFalse)
287 image=DestroyImageList(image);
288 return((Image *) NULL);
291 Determine if this a PCX file.
293 page_table=(MagickOffsetType *) NULL;
294 if (LocaleCompare(image_info->magick,"DCX") == 0)
300 Read the DCX page table.
302 magic=ReadBlobLSBLong(image);
303 if (magic != 987654321)
304 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
305 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
306 sizeof(*page_table));
307 if (page_table == (MagickOffsetType *) NULL)
308 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
309 for (id=0; id < 1024; id++)
311 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
312 if (page_table[id] == 0)
316 if (page_table != (MagickOffsetType *) NULL)
318 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
320 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
322 pcx_colormap=(unsigned char *) NULL;
323 count=ReadBlob(image,1,&pcx_info.identifier);
324 for (id=1; id < 1024; id++)
327 Verify PCX identifier.
329 pcx_info.version=(unsigned char) ReadBlobByte(image);
330 if ((count == 0) || (pcx_info.identifier != 0x0a))
331 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
332 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
333 pcx_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
334 pcx_info.left=ReadBlobLSBShort(image);
335 pcx_info.top=ReadBlobLSBShort(image);
336 pcx_info.right=ReadBlobLSBShort(image);
337 pcx_info.bottom=ReadBlobLSBShort(image);
338 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
339 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
341 Read PCX raster colormap.
343 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
345 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
347 if ((image->columns == 0) || (image->rows == 0) ||
348 (pcx_info.bits_per_pixel == 0))
349 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
350 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
351 image->units=PixelsPerInchResolution;
352 image->x_resolution=(double) pcx_info.horizontal_resolution;
353 image->y_resolution=(double) pcx_info.vertical_resolution;
355 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
356 3*sizeof(*pcx_colormap));
357 if (pcx_colormap == (unsigned char *) NULL)
358 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
359 count=ReadBlob(image,3*image->colors,pcx_colormap);
360 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
361 pcx_info.planes=(unsigned char) ReadBlobByte(image);
363 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
364 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
365 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
366 image->colors=(size_t) MagickMin(one << (1UL*
367 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
368 if (AcquireImageColormap(image,image->colors) == MagickFalse)
369 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
370 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
371 image->storage_class=DirectClass;
373 for (i=0; i < (ssize_t) image->colors; i++)
375 image->colormap[i].red=ScaleCharToQuantum(*p++);
376 image->colormap[i].green=ScaleCharToQuantum(*p++);
377 image->colormap[i].blue=ScaleCharToQuantum(*p++);
379 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
380 pcx_info.palette_info=ReadBlobLSBShort(image);
381 for (i=0; i < 58; i++)
382 (void) ReadBlobByte(image);
383 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
384 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
389 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
391 pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
392 sizeof(*pcx_pixels));
393 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
394 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
395 if ((pcx_pixels == (unsigned char *) NULL) ||
396 (scanline == (unsigned char *) NULL))
397 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
399 Uncompress image data.
402 if (pcx_info.encoding == 0)
403 while (pcx_packets != 0)
405 packet=(unsigned char) ReadBlobByte(image);
406 if (EOFBlob(image) != MagickFalse)
412 while (pcx_packets != 0)
414 packet=(unsigned char) ReadBlobByte(image);
415 if (EOFBlob(image) != MagickFalse)
417 if ((packet & 0xc0) != 0xc0)
423 count=(ssize_t) (packet & 0x3f);
424 packet=(unsigned char) ReadBlobByte(image);
425 if (EOFBlob(image) != MagickFalse)
427 for ( ; count != 0; count--)
431 if (pcx_packets == 0)
435 if (image->storage_class == DirectClass)
436 image->matte=pcx_info.planes > 3 ? MagickTrue : MagickFalse;
438 if ((pcx_info.version == 5) ||
439 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
442 Initialize image colormap.
444 if (image->colors > 256)
445 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
446 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
451 image->colormap[0].red=(Quantum) 0;
452 image->colormap[0].green=(Quantum) 0;
453 image->colormap[0].blue=(Quantum) 0;
454 image->colormap[1].red=(Quantum) QuantumRange;
455 image->colormap[1].green=(Quantum) QuantumRange;
456 image->colormap[1].blue=(Quantum) QuantumRange;
459 if (image->colors > 16)
462 256 color images have their color map at the end of the file.
464 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
465 count=ReadBlob(image,3*image->colors,pcx_colormap);
467 for (i=0; i < (ssize_t) image->colors; i++)
469 image->colormap[i].red=ScaleCharToQuantum(*p++);
470 image->colormap[i].green=ScaleCharToQuantum(*p++);
471 image->colormap[i].blue=ScaleCharToQuantum(*p++);
474 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
477 Convert PCX raster image to pixel packets.
479 for (y=0; y < (ssize_t) image->rows; y++)
481 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
482 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
483 if (q == (PixelPacket *) NULL)
485 indexes=GetAuthenticIndexQueue(image);
487 if (image->storage_class == DirectClass)
488 for (i=0; i < pcx_info.planes; i++)
491 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
521 if (pcx_info.planes > 1)
523 for (x=0; x < (ssize_t) image->columns; x++)
525 for (i=0; i < pcx_info.planes; i++)
528 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
531 for (mask=0x80; mask != 0; mask>>=1)
541 switch (pcx_info.bits_per_pixel)
548 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
550 for (bit=7; bit >= 0; bit--)
551 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
554 if ((image->columns % 8) != 0)
556 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
557 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
564 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
566 *r++=(*p >> 6) & 0x3;
567 *r++=(*p >> 4) & 0x3;
568 *r++=(*p >> 2) & 0x3;
572 if ((image->columns % 4) != 0)
574 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
575 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
582 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
584 *r++=(*p >> 4) & 0xf;
588 if ((image->columns % 2) != 0)
589 *r++=(*p++ >> 4) & 0xf;
594 (void) CopyMagickMemory(r,p,image->columns);
601 Transfer image scanline.
604 for (x=0; x < (ssize_t) image->columns; x++)
606 if (image->storage_class == PseudoClass)
607 indexes[x]=(IndexPacket) (*r++);
610 q->red=ScaleCharToQuantum(*r++);
611 q->green=ScaleCharToQuantum(*r++);
612 q->blue=ScaleCharToQuantum(*r++);
613 if (image->matte != MagickFalse)
614 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*r++));
618 if (SyncAuthenticPixels(image,exception) == MagickFalse)
620 if (image->previous == (Image *) NULL)
622 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
624 if (status == MagickFalse)
628 if (image->storage_class == PseudoClass)
629 (void) SyncImage(image);
630 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
631 if (pcx_colormap != (unsigned char *) NULL)
632 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
633 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
634 if (EOFBlob(image) != MagickFalse)
636 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
641 Proceed to next image.
643 if (image_info->number_scenes != 0)
644 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
646 if (page_table == (MagickOffsetType *) NULL)
648 if (page_table[id] == 0)
650 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
652 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
653 count=ReadBlob(image,1,&pcx_info.identifier);
654 if ((count != 0) && (pcx_info.identifier == 0x0a))
657 Allocate next image structure.
659 AcquireNextImage(image_info,image);
660 if (GetNextImageInList(image) == (Image *) NULL)
662 image=DestroyImageList(image);
663 return((Image *) NULL);
665 image=SyncNextImageInList(image);
666 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
668 if (status == MagickFalse)
672 if (page_table != (MagickOffsetType *) NULL)
673 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
674 (void) CloseBlob(image);
675 return(GetFirstImageInList(image));
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % R e g i s t e r P C X I m a g e %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 % RegisterPCXImage() adds attributes for the PCX image format to
690 % the list of supported formats. The attributes include the image format
691 % tag, a method to read and/or write the format, whether the format
692 % supports the saving of more than one frame to the same file or blob,
693 % whether the format supports native in-memory I/O, and a brief
694 % description of the format.
696 % The format of the RegisterPCXImage method is:
698 % size_t RegisterPCXImage(void)
701 ModuleExport size_t RegisterPCXImage(void)
706 entry=SetMagickInfo("DCX");
707 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
708 entry->encoder=(EncodeImageHandler *) WritePCXImage;
709 entry->seekable_stream=MagickTrue;
710 entry->magick=(IsImageFormatHandler *) IsDCX;
711 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
712 entry->module=ConstantString("PCX");
713 (void) RegisterMagickInfo(entry);
714 entry=SetMagickInfo("PCX");
715 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
716 entry->encoder=(EncodeImageHandler *) WritePCXImage;
717 entry->magick=(IsImageFormatHandler *) IsPCX;
718 entry->adjoin=MagickFalse;
719 entry->seekable_stream=MagickTrue;
720 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
721 entry->module=ConstantString("PCX");
722 (void) RegisterMagickInfo(entry);
723 return(MagickImageCoderSignature);
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % U n r e g i s t e r P C X I m a g e %
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % UnregisterPCXImage() removes format registrations made by the
738 % PCX module from the list of supported formats.
740 % The format of the UnregisterPCXImage method is:
742 % UnregisterPCXImage(void)
745 ModuleExport void UnregisterPCXImage(void)
747 (void) UnregisterMagickInfo("DCX");
748 (void) UnregisterMagickInfo("PCX");
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % W r i t e P C X I m a g e %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
765 % The format of the WritePCXImage method is:
767 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
769 % A description of each parameter follows.
771 % o image_info: the image info.
773 % o image: The image.
777 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
778 const unsigned char *pixels,Image *image)
780 register const unsigned char
795 for (i=0; i < (ssize_t) pcx_info->planes; i++)
799 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
802 if ((packet == previous) && (count < 63))
807 if ((count > 1) || ((previous & 0xc0) == 0xc0))
810 (void) WriteBlobByte(image,(unsigned char) count);
812 (void) WriteBlobByte(image,previous);
816 if ((count > 1) || ((previous & 0xc0) == 0xc0))
819 (void) WriteBlobByte(image,(unsigned char) count);
821 (void) WriteBlobByte(image,previous);
826 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
842 register const IndexPacket
845 register const PixelPacket
852 register unsigned char
863 Open output image file.
865 assert(image_info != (const ImageInfo *) NULL);
866 assert(image_info->signature == MagickSignature);
867 assert(image != (Image *) NULL);
868 assert(image->signature == MagickSignature);
869 if (image->debug != MagickFalse)
870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
871 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
872 if (status == MagickFalse)
874 if (image->colorspace != RGBColorspace)
875 (void) TransformImageColorspace(image,RGBColorspace);
876 page_table=(MagickOffsetType *) NULL;
877 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
878 ((GetNextImageInList(image) != (Image *) NULL) &&
879 (image_info->adjoin != MagickFalse)))
882 Write the DCX page table.
884 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
885 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
886 sizeof(*page_table));
887 if (page_table == (MagickOffsetType *) NULL)
888 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
889 for (scene=0; scene < 1024; scene++)
890 (void) WriteBlobLSBLong(image,0x00000000L);
895 if (page_table != (MagickOffsetType *) NULL)
896 page_table[scene]=TellBlob(image);
898 Initialize PCX raster file header.
900 pcx_info.identifier=0x0a;
903 pcx_info.bits_per_pixel=8;
904 if ((image->storage_class == PseudoClass) &&
905 (IsMonochromeImage(image,&image->exception) != MagickFalse))
906 pcx_info.bits_per_pixel=1;
909 pcx_info.right=(unsigned short) (image->columns-1);
910 pcx_info.bottom=(unsigned short) (image->rows-1);
911 switch (image->units)
913 case UndefinedResolution:
914 case PixelsPerInchResolution:
917 pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
918 pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
921 case PixelsPerCentimeterResolution:
923 pcx_info.horizontal_resolution=(unsigned short)
924 (2.54*image->x_resolution+0.5);
925 pcx_info.vertical_resolution=(unsigned short)
926 (2.54*image->y_resolution+0.5);
932 if ((image->storage_class == DirectClass) || (image->colors > 256))
935 if (image->matte != MagickFalse)
938 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
939 pcx_info.bits_per_pixel+7)/8);
940 pcx_info.palette_info=1;
941 pcx_info.colormap_signature=0x0c;
945 (void) WriteBlobByte(image,pcx_info.identifier);
946 (void) WriteBlobByte(image,pcx_info.version);
947 (void) WriteBlobByte(image,pcx_info.encoding);
948 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
949 (void) WriteBlobLSBShort(image,pcx_info.left);
950 (void) WriteBlobLSBShort(image,pcx_info.top);
951 (void) WriteBlobLSBShort(image,pcx_info.right);
952 (void) WriteBlobLSBShort(image,pcx_info.bottom);
953 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
954 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
956 Dump colormap to file.
958 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
959 3*sizeof(*pcx_colormap));
960 if (pcx_colormap == (unsigned char *) NULL)
961 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
962 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
964 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
965 for (i=0; i < (ssize_t) image->colors; i++)
967 *q++=ScaleQuantumToChar(image->colormap[i].red);
968 *q++=ScaleQuantumToChar(image->colormap[i].green);
969 *q++=ScaleQuantumToChar(image->colormap[i].blue);
971 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
972 (void) WriteBlobByte(image,pcx_info.reserved);
973 (void) WriteBlobByte(image,pcx_info.planes);
974 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
975 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
976 for (i=0; i < 58; i++)
977 (void) WriteBlobByte(image,'\0');
978 length=(size_t) pcx_info.bytes_per_line;
979 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,
980 pcx_info.planes*sizeof(*pcx_pixels));
981 if (pcx_pixels == (unsigned char *) NULL)
982 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
984 if ((image->storage_class == DirectClass) || (image->colors > 256))
990 Convert DirectClass image to PCX raster pixels.
992 for (y=0; y < (ssize_t) image->rows; y++)
994 pixels=GetVirtualPixels(image,0,y,image->columns,1,
996 if (pixels == (const PixelPacket *) NULL)
999 for (i=0; i < pcx_info.planes; i++)
1006 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1008 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1015 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1017 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1024 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1026 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1034 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1036 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
1043 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1045 if (image->previous == (Image *) NULL)
1047 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1049 if (status == MagickFalse)
1056 if (pcx_info.bits_per_pixel > 1)
1057 for (y=0; y < (ssize_t) image->rows; y++)
1059 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1060 if (p == (const PixelPacket *) NULL)
1062 indexes=GetVirtualIndexQueue(image);
1064 for (x=0; x < (ssize_t) image->columns; x++)
1065 *q++=(unsigned char) indexes[x];
1066 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1068 if (image->previous == (Image *) NULL)
1070 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1072 if (status == MagickFalse)
1081 register unsigned char
1086 Convert PseudoClass image to a PCX monochrome image.
1088 polarity=(IndexPacket) (PixelIntensityToQuantum(
1089 &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
1090 if (image->colors == 2)
1091 polarity=(IndexPacket) (
1092 PixelIntensityToQuantum(&image->colormap[0]) <
1093 PixelIntensityToQuantum(&image->colormap[1]) ? 1 : 0);
1094 for (y=0; y < (ssize_t) image->rows; y++)
1096 p=GetVirtualPixels(image,0,y,image->columns,1,
1098 if (p == (const PixelPacket *) NULL)
1100 indexes=GetVirtualIndexQueue(image);
1104 for (x=0; x < (ssize_t) image->columns; x++)
1107 if (indexes[x] == polarity)
1119 *q++=byte << (8-bit);
1120 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1122 if (image->previous == (Image *) NULL)
1124 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1126 if (status == MagickFalse)
1131 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1132 (void) WriteBlob(image,3*256,pcx_colormap);
1134 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1135 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1136 if (page_table == (MagickOffsetType *) NULL)
1140 if (GetNextImageInList(image) == (Image *) NULL)
1142 image=SyncNextImageInList(image);
1143 status=SetImageProgress(image,SaveImagesTag,scene++,
1144 GetImageListLength(image));
1145 if (status == MagickFalse)
1147 } while (image_info->adjoin != MagickFalse);
1148 if (page_table != (MagickOffsetType *) NULL)
1151 Write the DCX page table.
1153 page_table[scene+1]=0;
1154 offset=SeekBlob(image,0L,SEEK_SET);
1156 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1157 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1158 for (i=0; i <= (ssize_t) scene; i++)
1159 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1160 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1162 if (status == MagickFalse)
1167 message=GetExceptionMessage(errno);
1168 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1169 FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1170 message=DestroyString(message);
1172 (void) CloseBlob(image);