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 long MagickAbsoluteValue(const long 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
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);
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=(unsigned long) MagickAbsoluteValue((long) pcx_info.right-
344 image->rows=(unsigned long) MagickAbsoluteValue((long) 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->x_resolution=(double) pcx_info.horizontal_resolution;
352 image->y_resolution=(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);
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=(unsigned long) MagickMin(1UL << (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 < (long) 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=(unsigned long) 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 < (long) 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 < (long) 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 < (long) pcx_info.bytes_per_line; x++)
519 if (pcx_info.planes > 1)
521 for (x=0; x < (long) image->columns; x++)
523 for (i=0; i < pcx_info.planes; i++)
526 for (x=0; x < (long) 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 < ((long) 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 >= (long) (8-(image->columns % 8)); bit--)
555 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
562 for (x=0; x < ((long) 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 >= (long) (4-(image->columns % 4)); i--)
573 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
580 for (x=0; x < ((long) 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 < (long) image->columns; x++)
604 if (image->storage_class == PseudoClass)
605 indexes[x]=(IndexPacket) (*r++);
608 q->red=ScaleCharToQuantum(*r++);
609 q->green=ScaleCharToQuantum(*r++);
610 q->blue=ScaleCharToQuantum(*r++);
611 if (image->matte != MagickFalse)
612 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*r++));
616 if (SyncAuthenticPixels(image,exception) == MagickFalse)
618 if (image->previous == (Image *) NULL)
620 status=SetImageProgress(image,LoadImageTag,y,image->rows);
621 if (status == MagickFalse)
625 if (image->storage_class == PseudoClass)
626 (void) SyncImage(image);
627 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
628 if (pcx_colormap != (unsigned char *) NULL)
629 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
630 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
631 if (EOFBlob(image) != MagickFalse)
633 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
638 Proceed to next image.
640 if (image_info->number_scenes != 0)
641 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
643 if (page_table == (MagickOffsetType *) NULL)
645 if (page_table[id] == 0)
647 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
649 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
650 count=ReadBlob(image,1,&pcx_info.identifier);
651 if ((count != 0) && (pcx_info.identifier == 0x0a))
654 Allocate next image structure.
656 AcquireNextImage(image_info,image);
657 if (GetNextImageInList(image) == (Image *) NULL)
659 image=DestroyImageList(image);
660 return((Image *) NULL);
662 image=SyncNextImageInList(image);
663 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
665 if (status == MagickFalse)
669 if (page_table != (MagickOffsetType *) NULL)
670 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
671 (void) CloseBlob(image);
672 return(GetFirstImageInList(image));
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % R e g i s t e r P C X I m a g e %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % RegisterPCXImage() adds attributes for the PCX image format to
687 % the list of supported formats. The attributes include the image format
688 % tag, a method to read and/or write the format, whether the format
689 % supports the saving of more than one frame to the same file or blob,
690 % whether the format supports native in-memory I/O, and a brief
691 % description of the format.
693 % The format of the RegisterPCXImage method is:
695 % unsigned long RegisterPCXImage(void)
698 ModuleExport unsigned long RegisterPCXImage(void)
703 entry=SetMagickInfo("DCX");
704 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
705 entry->encoder=(EncodeImageHandler *) WritePCXImage;
706 entry->seekable_stream=MagickTrue;
707 entry->magick=(IsImageFormatHandler *) IsDCX;
708 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
709 entry->module=ConstantString("PCX");
710 (void) RegisterMagickInfo(entry);
711 entry=SetMagickInfo("PCX");
712 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
713 entry->encoder=(EncodeImageHandler *) WritePCXImage;
714 entry->magick=(IsImageFormatHandler *) IsPCX;
715 entry->adjoin=MagickFalse;
716 entry->seekable_stream=MagickTrue;
717 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
718 entry->module=ConstantString("PCX");
719 (void) RegisterMagickInfo(entry);
720 return(MagickImageCoderSignature);
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % U n r e g i s t e r P C X I m a g e %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % UnregisterPCXImage() removes format registrations made by the
735 % PCX module from the list of supported formats.
737 % The format of the UnregisterPCXImage method is:
739 % UnregisterPCXImage(void)
742 ModuleExport void UnregisterPCXImage(void)
744 (void) UnregisterMagickInfo("DCX");
745 (void) UnregisterMagickInfo("PCX");
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % W r i t e P C X I m a g e %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
762 % The format of the WritePCXImage method is:
764 % MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
766 % A description of each parameter follows.
768 % o image_info: the image info.
770 % o image: The image.
774 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
775 const unsigned char *pixels,Image *image)
777 register const unsigned char
792 for (i=0; i < (long) pcx_info->planes; i++)
796 for (x=0; x < (long) (pcx_info->bytes_per_line-1); x++)
799 if ((packet == previous) && (count < 63))
804 if ((count > 1) || ((previous & 0xc0) == 0xc0))
807 (void) WriteBlobByte(image,(unsigned char) count);
809 (void) WriteBlobByte(image,previous);
813 if ((count > 1) || ((previous & 0xc0) == 0xc0))
816 (void) WriteBlobByte(image,(unsigned char) count);
818 (void) WriteBlobByte(image,previous);
823 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
839 register const IndexPacket
842 register const PixelPacket
849 register unsigned char
860 Open output image file.
862 assert(image_info != (const ImageInfo *) NULL);
863 assert(image_info->signature == MagickSignature);
864 assert(image != (Image *) NULL);
865 assert(image->signature == MagickSignature);
866 if (image->debug != MagickFalse)
867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
868 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
869 if (status == MagickFalse)
871 if (image->colorspace != RGBColorspace)
872 (void) TransformImageColorspace(image,RGBColorspace);
873 page_table=(MagickOffsetType *) NULL;
874 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
875 ((GetNextImageInList(image) != (Image *) NULL) &&
876 (image_info->adjoin != MagickFalse)))
879 Write the DCX page table.
881 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
882 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
883 sizeof(*page_table));
884 if (page_table == (MagickOffsetType *) NULL)
885 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
886 for (scene=0; scene < 1024; scene++)
887 (void) WriteBlobLSBLong(image,0x00000000L);
892 if (page_table != (MagickOffsetType *) NULL)
893 page_table[scene]=TellBlob(image);
895 Initialize PCX raster file header.
897 pcx_info.identifier=0x0a;
900 pcx_info.bits_per_pixel=8;
901 if ((image->storage_class == PseudoClass) &&
902 (IsMonochromeImage(image,&image->exception) != MagickFalse))
903 pcx_info.bits_per_pixel=1;
906 pcx_info.right=(unsigned short) (image->columns-1);
907 pcx_info.bottom=(unsigned short) (image->rows-1);
908 switch (image->units)
910 case UndefinedResolution:
911 case PixelsPerInchResolution:
914 pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
915 pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
918 case PixelsPerCentimeterResolution:
920 pcx_info.horizontal_resolution=(unsigned short)
921 (2.54*image->x_resolution+0.5);
922 pcx_info.vertical_resolution=(unsigned short)
923 (2.54*image->y_resolution+0.5);
929 if ((image->storage_class == DirectClass) || (image->colors > 256))
932 if (image->matte != MagickFalse)
935 pcx_info.bytes_per_line=(unsigned short) (((unsigned long) image->columns*
936 pcx_info.bits_per_pixel+7)/8);
937 pcx_info.palette_info=1;
938 pcx_info.colormap_signature=0x0c;
942 (void) WriteBlobByte(image,pcx_info.identifier);
943 (void) WriteBlobByte(image,pcx_info.version);
944 (void) WriteBlobByte(image,pcx_info.encoding);
945 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
946 (void) WriteBlobLSBShort(image,pcx_info.left);
947 (void) WriteBlobLSBShort(image,pcx_info.top);
948 (void) WriteBlobLSBShort(image,pcx_info.right);
949 (void) WriteBlobLSBShort(image,pcx_info.bottom);
950 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
951 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
953 Dump colormap to file.
955 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
956 3*sizeof(*pcx_colormap));
957 if (pcx_colormap == (unsigned char *) NULL)
958 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
959 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
961 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
962 for (i=0; i < (long) image->colors; i++)
964 *q++=ScaleQuantumToChar(image->colormap[i].red);
965 *q++=ScaleQuantumToChar(image->colormap[i].green);
966 *q++=ScaleQuantumToChar(image->colormap[i].blue);
968 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
969 (void) WriteBlobByte(image,pcx_info.reserved);
970 (void) WriteBlobByte(image,pcx_info.planes);
971 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
972 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
973 for (i=0; i < 58; i++)
974 (void) WriteBlobByte(image,'\0');
975 length=(size_t) pcx_info.bytes_per_line;
976 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,
977 pcx_info.planes*sizeof(*pcx_pixels));
978 if (pcx_pixels == (unsigned char *) NULL)
979 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
981 if ((image->storage_class == DirectClass) || (image->colors > 256))
987 Convert DirectClass image to PCX raster pixels.
989 for (y=0; y < (long) image->rows; y++)
991 pixels=GetVirtualPixels(image,0,y,image->columns,1,
993 if (pixels == (const PixelPacket *) NULL)
996 for (i=0; i < pcx_info.planes; i++)
1003 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1005 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1012 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1014 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1021 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1023 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1031 for (x=(long) pcx_info.bytes_per_line; x != 0; x--)
1033 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
1040 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1042 if (image->previous == (Image *) NULL)
1044 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1045 if (status == MagickFalse)
1052 if (pcx_info.bits_per_pixel > 1)
1053 for (y=0; y < (long) image->rows; y++)
1055 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1056 if (p == (const PixelPacket *) NULL)
1058 indexes=GetVirtualIndexQueue(image);
1060 for (x=0; x < (long) image->columns; x++)
1061 *q++=(unsigned char) indexes[x];
1062 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1064 if (image->previous == (Image *) NULL)
1066 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1067 if (status == MagickFalse)
1076 register unsigned char
1081 Convert PseudoClass image to a PCX monochrome image.
1083 polarity=(IndexPacket) (PixelIntensityToQuantum(
1084 &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
1085 if (image->colors == 2)
1086 polarity=(IndexPacket) (
1087 PixelIntensityToQuantum(&image->colormap[0]) <
1088 PixelIntensityToQuantum(&image->colormap[1]) ? 1 : 0);
1089 for (y=0; y < (long) image->rows; y++)
1091 p=GetVirtualPixels(image,0,y,image->columns,1,
1093 if (p == (const PixelPacket *) NULL)
1095 indexes=GetVirtualIndexQueue(image);
1099 for (x=0; x < (long) image->columns; x++)
1102 if (indexes[x] == polarity)
1114 *q++=byte << (8-bit);
1115 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1117 if (image->previous == (Image *) NULL)
1119 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1120 if (status == MagickFalse)
1125 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1126 (void) WriteBlob(image,3*256,pcx_colormap);
1128 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1129 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1130 if (page_table == (MagickOffsetType *) NULL)
1134 if (GetNextImageInList(image) == (Image *) NULL)
1136 image=SyncNextImageInList(image);
1137 status=SetImageProgress(image,SaveImagesTag,scene++,
1138 GetImageListLength(image));
1139 if (status == MagickFalse)
1141 } while (image_info->adjoin != MagickFalse);
1142 if (page_table != (MagickOffsetType *) NULL)
1145 Write the DCX page table.
1147 page_table[scene+1]=0;
1148 offset=SeekBlob(image,0L,SEEK_SET);
1150 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1151 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1152 for (i=0; i <= (long) scene; i++)
1153 (void) WriteBlobLSBLong(image,(unsigned long) page_table[i]);
1154 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1156 if (status == MagickFalse)
1161 message=GetExceptionMessage(errno);
1162 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1163 FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1164 message=DestroyString(message);
1166 (void) CloseBlob(image);