]> granicus.if.org Git - imagemagick/blob - coders/pcx.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[imagemagick] / coders / pcx.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP    CCCC  X   X                              %
7 %                            P   P  C       X X                               %
8 %                            PPPP   C        X                                %
9 %                            P      C       X X                               %
10 %                            P       CCCC  X   X                              %
11 %                                                                             %
12 %                                                                             %
13 %                Read/Write ZSoft IBM PC Paintbrush Image Format              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
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"
63 \f
64 /*
65   Typedef declarations.
66 */
67 typedef struct _PCXInfo
68 {
69   unsigned char
70     identifier,
71     version,
72     encoding,
73     bits_per_pixel;
74
75   unsigned short
76     left,
77     top,
78     right,
79     bottom,
80     horizontal_resolution,
81     vertical_resolution;
82
83   unsigned char
84     reserved,
85     planes;
86
87   unsigned short
88     bytes_per_line,
89     palette_info;
90
91   unsigned char
92     colormap_signature;
93 } PCXInfo;
94 \f
95 /*
96   Forward declarations.
97 */
98 static MagickBooleanType
99   WritePCXImage(const ImageInfo *,Image *);
100 \f
101 /*
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 %                                                                             %
104 %                                                                             %
105 %                                                                             %
106 %   I s D C X                                                                 %
107 %                                                                             %
108 %                                                                             %
109 %                                                                             %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %
112 %  IsDCX() returns MagickTrue if the image format type, identified by the
113 %  magick string, is DCX.
114 %
115 %  The format of the IsDCX method is:
116 %
117 %      MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
118 %
119 %  A description of each parameter follows:
120 %
121 %    o magick: compare image format pattern against these bytes.
122 %
123 %    o length: Specifies the length of the magick string.
124 %
125 %
126 */
127 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
128 {
129   if (length < 4)
130     return(MagickFalse);
131   if (memcmp(magick,"\261\150\336\72",4) == 0)
132     return(MagickTrue);
133   return(MagickFalse);
134 }
135 \f
136 /*
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 %                                                                             %
139 %                                                                             %
140 %                                                                             %
141 %   I s P C X                                                                 %
142 %                                                                             %
143 %                                                                             %
144 %                                                                             %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 %
147 %  IsPCX() returns MagickTrue if the image format type, identified by the
148 %  magick string, is PCX.
149 %
150 %  The format of the IsPCX method is:
151 %
152 %      MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
153 %
154 %  A description of each parameter follows:
155 %
156 %    o magick: compare image format pattern against these bytes.
157 %
158 %    o length: Specifies the length of the magick string.
159 %
160 %
161 */
162 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
163 {
164   if (length < 2)
165     return(MagickFalse);
166   if (memcmp(magick,"\012\002",2) == 0)
167     return(MagickTrue);
168   if (memcmp(magick,"\012\005",2) == 0)
169     return(MagickTrue);
170   return(MagickFalse);
171 }
172 \f
173 /*
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 %                                                                             %
176 %                                                                             %
177 %                                                                             %
178 %   R e a d P C X I m a g e                                                   %
179 %                                                                             %
180 %                                                                             %
181 %                                                                             %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %
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.
187 %
188 %  The format of the ReadPCXImage method is:
189 %
190 %      Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
191 %
192 %  A description of each parameter follows:
193 %
194 %    o image_info: the image info.
195 %
196 %    o exception: return any errors or warnings in this structure.
197 %
198 */
199
200 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
201 {
202   if (x < 0)
203     return(-x);
204   return(x);
205 }
206
207 static inline size_t MagickMax(const size_t x,const size_t y)
208 {
209   if (x > y)
210     return(x);
211   return(y);
212 }
213
214 static inline size_t MagickMin(const size_t x,const size_t y)
215 {
216   if (x < y)
217     return(x);
218   return(y);
219 }
220
221 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
222 {
223   Image
224     *image;
225
226   int
227     bits,
228     id,
229     mask;
230
231   MagickBooleanType
232     status;
233
234   MagickOffsetType
235     offset,
236     *page_table;
237
238   PCXInfo
239     pcx_info;
240
241   register IndexPacket
242     *indexes;
243
244   register ssize_t
245     x;
246
247   register PixelPacket
248     *q;
249
250   register ssize_t
251     i;
252
253   register unsigned char
254     *p,
255     *r;
256
257   size_t
258     one,
259     pcx_packets;
260
261   ssize_t
262     count,
263     y;
264
265   unsigned char
266     packet,
267     *pcx_colormap,
268     *pcx_pixels,
269     *scanline;
270
271   /*
272     Open image file.
273   */
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)
284     {
285       image=DestroyImageList(image);
286       return((Image *) NULL);
287     }
288   /*
289     Determine if this a PCX file.
290   */
291   page_table=(MagickOffsetType *) NULL;
292   if (LocaleCompare(image_info->magick,"DCX") == 0)
293     {
294       size_t
295         magic;
296
297       /*
298         Read the DCX page table.
299       */
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++)
308       {
309         page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
310         if (page_table[id] == 0)
311           break;
312       }
313     }
314   if (page_table != (MagickOffsetType *) NULL)
315     {
316       offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
317       if (offset < 0)
318         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
319     }
320   pcx_colormap=(unsigned char *) NULL;
321   count=ReadBlob(image,1,&pcx_info.identifier);
322   for (id=1; id < 1024; id++)
323   {
324     /*
325       Verify PCX identifier.
326     */
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);
338     /*
339       Read PCX raster colormap.
340     */
341     image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
342       pcx_info.left)+1UL;
343     image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
344       pcx_info.top)+1UL;
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;
352     image->colors=16;
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);
360     one=1;
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;
370     p=pcx_colormap;
371     for (i=0; i < (ssize_t) image->colors; i++)
372     {
373       image->colormap[i].red=ScaleCharToQuantum(*p++);
374       image->colormap[i].green=ScaleCharToQuantum(*p++);
375       image->colormap[i].blue=ScaleCharToQuantum(*p++);
376     }
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))
383         break;
384     /*
385       Read image data.
386     */
387     pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
388       pcx_info.planes;
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");
396     /*
397       Uncompress image data.
398     */
399     p=pcx_pixels;
400     if (pcx_info.encoding == 0)
401       while (pcx_packets != 0)
402       {
403         packet=(unsigned char) ReadBlobByte(image);
404         if (EOFBlob(image) != MagickFalse)
405           break;
406         *p++=packet;
407         pcx_packets--;
408       }
409     else
410       while (pcx_packets != 0)
411       {
412         packet=(unsigned char) ReadBlobByte(image);
413         if (EOFBlob(image) != MagickFalse)
414           break;
415         if ((packet & 0xc0) != 0xc0)
416           {
417             *p++=packet;
418             pcx_packets--;
419             continue;
420           }
421         count=(ssize_t) (packet & 0x3f);
422         packet=(unsigned char) ReadBlobByte(image);
423         if (EOFBlob(image) != MagickFalse)
424           break;
425         for ( ; count != 0; count--)
426         {
427           *p++=packet;
428           pcx_packets--;
429           if (pcx_packets == 0)
430             break;
431         }
432       }
433     if (image->storage_class == DirectClass)
434       image->matte=pcx_info.planes > 3 ? MagickTrue : MagickFalse;
435     else
436       if ((pcx_info.version == 5) ||
437           ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
438         {
439           /*
440             Initialize image colormap.
441           */
442           if (image->colors > 256)
443             ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
444           if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
445             {
446               /*
447                 Monochrome colormap.
448               */
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;
455             }
456           else
457             if (image->colors > 16)
458               {
459                 /*
460                   256 color images have their color map at the end of the file.
461                 */
462                 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
463                 count=ReadBlob(image,3*image->colors,pcx_colormap);
464                 p=pcx_colormap;
465                 for (i=0; i < (ssize_t) image->colors; i++)
466                 {
467                   image->colormap[i].red=ScaleCharToQuantum(*p++);
468                   image->colormap[i].green=ScaleCharToQuantum(*p++);
469                   image->colormap[i].blue=ScaleCharToQuantum(*p++);
470                 }
471             }
472           pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
473         }
474     /*
475       Convert PCX raster image to pixel packets.
476     */
477     for (y=0; y < (ssize_t) image->rows; y++)
478     {
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)
482         break;
483       indexes=GetAuthenticIndexQueue(image);
484       r=scanline;
485       if (image->storage_class == DirectClass)
486         for (i=0; i < pcx_info.planes; i++)
487         {
488           r=scanline+i;
489           for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
490           {
491             switch (i)
492             {
493               case 0:
494               {
495                 *r=(*p++);
496                 break;
497               }
498               case 1:
499               {
500                 *r=(*p++);
501                 break;
502               }
503               case 2:
504               {
505                 *r=(*p++);
506                 break;
507               }
508               case 3:
509               default:
510               {
511                 *r=(*p++);
512                 break;
513               }
514             }
515             r+=pcx_info.planes;
516           }
517         }
518       else
519         if (pcx_info.planes > 1)
520           {
521             for (x=0; x < (ssize_t) image->columns; x++)
522               *r++=0;
523             for (i=0; i < pcx_info.planes; i++)
524             {
525               r=scanline;
526               for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
527               {
528                  bits=(*p++);
529                  for (mask=0x80; mask != 0; mask>>=1)
530                  {
531                    if (bits & mask)
532                      *r|=1 << i;
533                    r++;
534                  }
535                }
536             }
537           }
538         else
539           switch (pcx_info.bits_per_pixel)
540           {
541             case 1:
542             {
543               register ssize_t
544                 bit;
545
546               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
547               {
548                 for (bit=7; bit >= 0; bit--)
549                   *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
550                 p++;
551               }
552               if ((image->columns % 8) != 0)
553                 {
554                   for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
555                     *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
556                   p++;
557                 }
558               break;
559             }
560             case 2:
561             {
562               for (x=0; x < ((ssize_t) image->columns-3); x+=4)
563               {
564                 *r++=(*p >> 6) & 0x3;
565                 *r++=(*p >> 4) & 0x3;
566                 *r++=(*p >> 2) & 0x3;
567                 *r++=(*p) & 0x3;
568                 p++;
569               }
570               if ((image->columns % 4) != 0)
571                 {
572                   for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
573                     *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
574                   p++;
575                 }
576               break;
577             }
578             case 4:
579             {
580               for (x=0; x < ((ssize_t) image->columns-1); x+=2)
581               {
582                 *r++=(*p >> 4) & 0xf;
583                 *r++=(*p) & 0xf;
584                 p++;
585               }
586               if ((image->columns % 2) != 0)
587                 *r++=(*p++ >> 4) & 0xf;
588               break;
589             }
590             case 8:
591             {
592               (void) CopyMagickMemory(r,p,image->columns);
593               break;
594             }
595             default:
596               break;
597           }
598       /*
599         Transfer image scanline.
600       */
601       r=scanline;
602       for (x=0; x < (ssize_t) image->columns; x++)
603       {
604         if (image->storage_class == PseudoClass)
605           SetIndexPixelComponent(indexes+x,*r++);
606         else
607           {
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++));
613           }
614         q++;
615       }
616       if (SyncAuthenticPixels(image,exception) == MagickFalse)
617         break;
618       if (image->previous == (Image *) NULL)
619         {
620           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
621             image->rows);
622           if (status == MagickFalse)
623             break;
624         }
625     }
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)
633       {
634         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
635           image->filename);
636         break;
637       }
638     /*
639       Proceed to next image.
640     */
641     if (image_info->number_scenes != 0)
642       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
643         break;
644     if (page_table == (MagickOffsetType *) NULL)
645       break;
646     if (page_table[id] == 0)
647       break;
648     offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
649     if (offset < 0)
650       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
651     count=ReadBlob(image,1,&pcx_info.identifier);
652     if ((count != 0) && (pcx_info.identifier == 0x0a))
653       {
654         /*
655           Allocate next image structure.
656         */
657         AcquireNextImage(image_info,image);
658         if (GetNextImageInList(image) == (Image *) NULL)
659           {
660             image=DestroyImageList(image);
661             return((Image *) NULL);
662           }
663         image=SyncNextImageInList(image);
664         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
665           GetBlobSize(image));
666         if (status == MagickFalse)
667           break;
668       }
669   }
670   if (page_table != (MagickOffsetType *) NULL)
671     page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
672   (void) CloseBlob(image);
673   return(GetFirstImageInList(image));
674 }
675 \f
676 /*
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 %                                                                             %
679 %                                                                             %
680 %                                                                             %
681 %   R e g i s t e r P C X I m a g e                                           %
682 %                                                                             %
683 %                                                                             %
684 %                                                                             %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 %
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.
693 %
694 %  The format of the RegisterPCXImage method is:
695 %
696 %      size_t RegisterPCXImage(void)
697 %
698 */
699 ModuleExport size_t RegisterPCXImage(void)
700 {
701   MagickInfo
702     *entry;
703
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);
722 }
723 \f
724 /*
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 %                                                                             %
727 %                                                                             %
728 %                                                                             %
729 %   U n r e g i s t e r P C X I m a g e                                       %
730 %                                                                             %
731 %                                                                             %
732 %                                                                             %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 %
735 %  UnregisterPCXImage() removes format registrations made by the
736 %  PCX module from the list of supported formats.
737 %
738 %  The format of the UnregisterPCXImage method is:
739 %
740 %      UnregisterPCXImage(void)
741 %
742 */
743 ModuleExport void UnregisterPCXImage(void)
744 {
745   (void) UnregisterMagickInfo("DCX");
746   (void) UnregisterMagickInfo("PCX");
747 }
748 \f
749 /*
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 %                                                                             %
752 %                                                                             %
753 %                                                                             %
754 %   W r i t e P C X I m a g e                                                 %
755 %                                                                             %
756 %                                                                             %
757 %                                                                             %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 %
760 %  WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
761 %  format.
762 %
763 %  The format of the WritePCXImage method is:
764 %
765 %      MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
766 %
767 %  A description of each parameter follows.
768 %
769 %    o image_info: the image info.
770 %
771 %    o image:  The image.
772 %
773 %
774 */
775 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
776   const unsigned char *pixels,Image *image)
777 {
778   register const unsigned char
779     *q;
780
781   register ssize_t
782     i,
783     x;
784
785   ssize_t
786     count;
787
788   unsigned char
789     packet,
790     previous;
791
792   q=pixels;
793   for (i=0; i < (ssize_t) pcx_info->planes; i++)
794   {
795     if (pcx_info->encoding == 0)
796       {
797         for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
798           (void) WriteBlobByte(image,(unsigned char) (*q++));
799       }
800     else
801       {
802         previous=(*q++);
803         count=1;
804         for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
805         {
806           packet=(*q++);
807           if ((packet == previous) && (count < 63))
808             {
809               count++;
810               continue;
811             }
812           if ((count > 1) || ((previous & 0xc0) == 0xc0))
813             {
814               count|=0xc0;
815               (void) WriteBlobByte(image,(unsigned char) count);
816             }
817           (void) WriteBlobByte(image,previous);
818           previous=packet;
819           count=1;
820         }
821         if ((count > 1) || ((previous & 0xc0) == 0xc0))
822           {
823             count|=0xc0;
824             (void) WriteBlobByte(image,(unsigned char) count);
825           }
826         (void) WriteBlobByte(image,previous);
827       }
828   }
829   return (MagickTrue);
830 }
831
832 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
833 {
834   MagickBooleanType
835     status;
836
837   MagickOffsetType
838     offset,
839     *page_table,
840     scene;
841
842   PCXInfo
843     pcx_info;
844
845   register const IndexPacket
846     *indexes;
847
848   register const PixelPacket
849     *p;
850
851   register ssize_t
852     i,
853     x;
854
855   register unsigned char
856     *q;
857
858   size_t
859     length;
860
861   ssize_t
862     y;
863
864   unsigned char
865     *pcx_colormap,
866     *pcx_pixels;
867
868   /*
869     Open output image file.
870   */
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)
879     return(status);
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)))
886     {
887       /*
888         Write the DCX page table.
889       */
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);
897     }
898   scene=0;
899   do
900   {
901     if (page_table != (MagickOffsetType *) NULL)
902       page_table[scene]=TellBlob(image);
903     /*
904       Initialize PCX raster file header.
905     */
906     pcx_info.identifier=0x0a;
907     pcx_info.version=5;
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;
913     pcx_info.left=0;
914     pcx_info.top=0;
915     pcx_info.right=(unsigned short) (image->columns-1);
916     pcx_info.bottom=(unsigned short) (image->rows-1);
917     switch (image->units)
918     {
919       case UndefinedResolution:
920       case PixelsPerInchResolution:
921       default:
922       {
923         pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
924         pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
925         break;
926       }
927       case PixelsPerCentimeterResolution:
928       {
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);
933         break;
934       }
935     }
936     pcx_info.reserved=0;
937     pcx_info.planes=1;
938     if ((image->storage_class == DirectClass) || (image->colors > 256))
939       {
940         pcx_info.planes=3;
941         if (image->matte != MagickFalse)
942           pcx_info.planes++;
943       }
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;
948     /*
949       Write PCX header.
950     */
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);
961     /*
962       Dump colormap to file.
963     */
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));
969     q=pcx_colormap;
970     if ((image->storage_class == PseudoClass) && (image->colors <= 256))
971       for (i=0; i < (ssize_t) image->colors; i++)
972       {
973         *q++=ScaleQuantumToChar(image->colormap[i].red);
974         *q++=ScaleQuantumToChar(image->colormap[i].green);
975         *q++=ScaleQuantumToChar(image->colormap[i].blue);
976       }
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");
989     q=pcx_pixels;
990     if ((image->storage_class == DirectClass) || (image->colors > 256))
991       {
992         const PixelPacket
993           *pixels;
994
995         /*
996           Convert DirectClass image to PCX raster pixels.
997         */
998         for (y=0; y < (ssize_t) image->rows; y++)
999         {
1000           pixels=GetVirtualPixels(image,0,y,image->columns,1,
1001             &image->exception);
1002           if (pixels == (const PixelPacket *) NULL)
1003             break;
1004           q=pcx_pixels;
1005           for (i=0; i < pcx_info.planes; i++)
1006           {
1007             p=pixels;
1008             switch ((int) i)
1009             {
1010               case 0:
1011               {
1012                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1013                 {
1014                   *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1015                   p++;
1016                 }
1017                 break;
1018               }
1019               case 1:
1020               {
1021                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1022                 {
1023                   *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1024                   p++;
1025                 }
1026                 break;
1027               }
1028               case 2:
1029               {
1030                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1031                 {
1032                   *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1033                   p++;
1034                 }
1035                 break;
1036               }
1037               case 3:
1038               default:
1039               {
1040                 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1041                 {
1042                   *q++=ScaleQuantumToChar((Quantum)
1043                     (GetAlphaPixelComponent(p)));
1044                   p++;
1045                 }
1046                 break;
1047               }
1048             }
1049           }
1050           if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1051             break;
1052           if (image->previous == (Image *) NULL)
1053             {
1054               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1055                 image->rows);
1056               if (status == MagickFalse)
1057                 break;
1058             }
1059         }
1060       }
1061     else
1062       {
1063         if (pcx_info.bits_per_pixel > 1)
1064           for (y=0; y < (ssize_t) image->rows; y++)
1065           {
1066             p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1067             if (p == (const PixelPacket *) NULL)
1068               break;
1069             indexes=GetVirtualIndexQueue(image);
1070             q=pcx_pixels;
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)
1074               break;
1075             if (image->previous == (Image *) NULL)
1076               {
1077                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1078                 image->rows);
1079                 if (status == MagickFalse)
1080                   break;
1081               }
1082           }
1083         else
1084           {
1085             IndexPacket
1086               polarity;
1087
1088             register unsigned char
1089               bit,
1090               byte;
1091
1092             /*
1093               Convert PseudoClass image to a PCX monochrome image.
1094             */
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++)
1102             {
1103               p=GetVirtualPixels(image,0,y,image->columns,1,
1104                 &image->exception);
1105               if (p == (const PixelPacket *) NULL)
1106                 break;
1107               indexes=GetVirtualIndexQueue(image);
1108               bit=0;
1109               byte=0;
1110               q=pcx_pixels;
1111               for (x=0; x < (ssize_t) image->columns; x++)
1112               {
1113                 byte<<=1;
1114                 if (GetIndexPixelComponent(indexes+x) == polarity)
1115                   byte|=0x01;
1116                 bit++;
1117                 if (bit == 8)
1118                   {
1119                     *q++=byte;
1120                     bit=0;
1121                     byte=0;
1122                   }
1123                 p++;
1124               }
1125               if (bit != 0)
1126                 *q++=byte << (8-bit);
1127               if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1128                 break;
1129               if (image->previous == (Image *) NULL)
1130                 {
1131                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1132                     y,image->rows);
1133                   if (status == MagickFalse)
1134                     break;
1135                 }
1136             }
1137           }
1138         (void) WriteBlobByte(image,pcx_info.colormap_signature);
1139         (void) WriteBlob(image,3*256,pcx_colormap);
1140       }
1141     pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1142     pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1143     if (page_table == (MagickOffsetType *) NULL)
1144       break;
1145     if (scene >= 1023)
1146       break;
1147     if (GetNextImageInList(image) == (Image *) NULL)
1148       break;
1149     image=SyncNextImageInList(image);
1150     status=SetImageProgress(image,SaveImagesTag,scene++,
1151       GetImageListLength(image));
1152     if (status == MagickFalse)
1153       break;
1154   } while (image_info->adjoin != MagickFalse);
1155   if (page_table != (MagickOffsetType *) NULL)
1156     {
1157       /*
1158         Write the DCX page table.
1159       */
1160       page_table[scene+1]=0;
1161       offset=SeekBlob(image,0L,SEEK_SET);
1162       if (offset < 0)
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);
1168     }
1169   if (status == MagickFalse)
1170     {
1171       char
1172         *message;
1173
1174       message=GetExceptionMessage(errno);
1175       (void) ThrowMagickException(&image->exception,GetMagickModule(),
1176         FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1177       message=DestroyString(message);
1178     }
1179   (void) CloseBlob(image);
1180   return(MagickTrue);
1181 }