]> granicus.if.org Git - imagemagick/blob - coders/pcx.c
(no commit message)
[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-2012 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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
66 \f
67 /*
68   Typedef declarations.
69 */
70 typedef struct _PCXInfo
71 {
72   unsigned char
73     identifier,
74     version,
75     encoding,
76     bits_per_pixel;
77
78   unsigned short
79     left,
80     top,
81     right,
82     bottom,
83     horizontal_resolution,
84     vertical_resolution;
85
86   unsigned char
87     reserved,
88     planes;
89
90   unsigned short
91     bytes_per_line,
92     palette_info;
93
94   unsigned char
95     colormap_signature;
96 } PCXInfo;
97 \f
98 /*
99   Forward declarations.
100 */
101 static MagickBooleanType
102   WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
103 \f
104 /*
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %                                                                             %
107 %                                                                             %
108 %                                                                             %
109 %   I s D C X                                                                 %
110 %                                                                             %
111 %                                                                             %
112 %                                                                             %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %
115 %  IsDCX() returns MagickTrue if the image format type, identified by the
116 %  magick string, is DCX.
117 %
118 %  The format of the IsDCX method is:
119 %
120 %      MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
121 %
122 %  A description of each parameter follows:
123 %
124 %    o magick: compare image format pattern against these bytes.
125 %
126 %    o length: Specifies the length of the magick string.
127 %
128 %
129 */
130 static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
131 {
132   if (length < 4)
133     return(MagickFalse);
134   if (memcmp(magick,"\261\150\336\72",4) == 0)
135     return(MagickTrue);
136   return(MagickFalse);
137 }
138 \f
139 /*
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 %                                                                             %
142 %                                                                             %
143 %                                                                             %
144 %   I s P C X                                                                 %
145 %                                                                             %
146 %                                                                             %
147 %                                                                             %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 %
150 %  IsPCX() returns MagickTrue if the image format type, identified by the
151 %  magick string, is PCX.
152 %
153 %  The format of the IsPCX method is:
154 %
155 %      MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
156 %
157 %  A description of each parameter follows:
158 %
159 %    o magick: compare image format pattern against these bytes.
160 %
161 %    o length: Specifies the length of the magick string.
162 %
163 %
164 */
165 static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
166 {
167   if (length < 2)
168     return(MagickFalse);
169   if (memcmp(magick,"\012\002",2) == 0)
170     return(MagickTrue);
171   if (memcmp(magick,"\012\005",2) == 0)
172     return(MagickTrue);
173   return(MagickFalse);
174 }
175 \f
176 /*
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 %                                                                             %
179 %                                                                             %
180 %                                                                             %
181 %   R e a d P C X I m a g e                                                   %
182 %                                                                             %
183 %                                                                             %
184 %                                                                             %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 %
187 %  ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
188 %  It allocates the memory necessary for the new Image structure and returns
189 %  a pointer to the new image.
190 %
191 %  The format of the ReadPCXImage method is:
192 %
193 %      Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
194 %
195 %  A description of each parameter follows:
196 %
197 %    o image_info: the image info.
198 %
199 %    o exception: return any errors or warnings in this structure.
200 %
201 */
202
203 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
204 {
205   if (x < 0)
206     return(-x);
207   return(x);
208 }
209
210 static inline size_t MagickMax(const size_t x,const size_t y)
211 {
212   if (x > y)
213     return(x);
214   return(y);
215 }
216
217 static inline size_t MagickMin(const size_t x,const size_t y)
218 {
219   if (x < y)
220     return(x);
221   return(y);
222 }
223
224 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
225 {
226   Image
227     *image;
228
229   int
230     bits,
231     id,
232     mask;
233
234   MagickBooleanType
235     status;
236
237   MagickOffsetType
238     offset,
239     *page_table;
240
241   PCXInfo
242     pcx_info;
243
244   register ssize_t
245     x;
246
247   register Quantum
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,exception);
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->resolution.x=(double) pcx_info.horizontal_resolution;
351     image->resolution.y=(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,exception) == 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=QuantumRange;
453               image->colormap[1].green=QuantumRange;
454               image->colormap[1].blue=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 == (Quantum *) NULL)
482         break;
483       r=scanline;
484       if (image->storage_class == DirectClass)
485         for (i=0; i < pcx_info.planes; i++)
486         {
487           r=scanline+i;
488           for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
489           {
490             switch (i)
491             {
492               case 0:
493               {
494                 *r=(*p++);
495                 break;
496               }
497               case 1:
498               {
499                 *r=(*p++);
500                 break;
501               }
502               case 2:
503               {
504                 *r=(*p++);
505                 break;
506               }
507               case 3:
508               default:
509               {
510                 *r=(*p++);
511                 break;
512               }
513             }
514             r+=pcx_info.planes;
515           }
516         }
517       else
518         if (pcx_info.planes > 1)
519           {
520             for (x=0; x < (ssize_t) image->columns; x++)
521               *r++=0;
522             for (i=0; i < pcx_info.planes; i++)
523             {
524               r=scanline;
525               for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
526               {
527                  bits=(*p++);
528                  for (mask=0x80; mask != 0; mask>>=1)
529                  {
530                    if (bits & mask)
531                      *r|=1 << i;
532                    r++;
533                  }
534                }
535             }
536           }
537         else
538           switch (pcx_info.bits_per_pixel)
539           {
540             case 1:
541             {
542               register ssize_t
543                 bit;
544
545               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
546               {
547                 for (bit=7; bit >= 0; bit--)
548                   *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
549                 p++;
550               }
551               if ((image->columns % 8) != 0)
552                 {
553                   for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
554                     *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
555                   p++;
556                 }
557               break;
558             }
559             case 2:
560             {
561               for (x=0; x < ((ssize_t) image->columns-3); x+=4)
562               {
563                 *r++=(*p >> 6) & 0x3;
564                 *r++=(*p >> 4) & 0x3;
565                 *r++=(*p >> 2) & 0x3;
566                 *r++=(*p) & 0x3;
567                 p++;
568               }
569               if ((image->columns % 4) != 0)
570                 {
571                   for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
572                     *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
573                   p++;
574                 }
575               break;
576             }
577             case 4:
578             {
579               for (x=0; x < ((ssize_t) image->columns-1); x+=2)
580               {
581                 *r++=(*p >> 4) & 0xf;
582                 *r++=(*p) & 0xf;
583                 p++;
584               }
585               if ((image->columns % 2) != 0)
586                 *r++=(*p++ >> 4) & 0xf;
587               break;
588             }
589             case 8:
590             {
591               (void) CopyMagickMemory(r,p,image->columns);
592               break;
593             }
594             default:
595               break;
596           }
597       /*
598         Transfer image scanline.
599       */
600       r=scanline;
601       for (x=0; x < (ssize_t) image->columns; x++)
602       {
603         if (image->storage_class == PseudoClass)
604           SetPixelIndex(image,*r++,q);
605         else
606           {
607             SetPixelRed(image,ScaleCharToQuantum(*r++),q);
608             SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
609             SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
610             if (image->matte != MagickFalse)
611               SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
612           }
613         q+=GetPixelChannels(image);
614       }
615       if (SyncAuthenticPixels(image,exception) == MagickFalse)
616         break;
617       if (image->previous == (Image *) NULL)
618         {
619           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
620             image->rows);
621           if (status == MagickFalse)
622             break;
623         }
624     }
625     if (image->storage_class == PseudoClass)
626       (void) SyncImage(image,exception);
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)
632       {
633         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
634           image->filename);
635         break;
636       }
637     /*
638       Proceed to next image.
639     */
640     if (image_info->number_scenes != 0)
641       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
642         break;
643     if (page_table == (MagickOffsetType *) NULL)
644       break;
645     if (page_table[id] == 0)
646       break;
647     offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
648     if (offset < 0)
649       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
650     count=ReadBlob(image,1,&pcx_info.identifier);
651     if ((count != 0) && (pcx_info.identifier == 0x0a))
652       {
653         /*
654           Allocate next image structure.
655         */
656         AcquireNextImage(image_info,image,exception);
657         if (GetNextImageInList(image) == (Image *) NULL)
658           {
659             image=DestroyImageList(image);
660             return((Image *) NULL);
661           }
662         image=SyncNextImageInList(image);
663         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
664           GetBlobSize(image));
665         if (status == MagickFalse)
666           break;
667       }
668   }
669   if (page_table != (MagickOffsetType *) NULL)
670     page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
671   (void) CloseBlob(image);
672   return(GetFirstImageInList(image));
673 }
674 \f
675 /*
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 %                                                                             %
678 %                                                                             %
679 %                                                                             %
680 %   R e g i s t e r P C X I m a g e                                           %
681 %                                                                             %
682 %                                                                             %
683 %                                                                             %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 %
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.
692 %
693 %  The format of the RegisterPCXImage method is:
694 %
695 %      size_t RegisterPCXImage(void)
696 %
697 */
698 ModuleExport size_t RegisterPCXImage(void)
699 {
700   MagickInfo
701     *entry;
702
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);
721 }
722 \f
723 /*
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 %                                                                             %
726 %                                                                             %
727 %                                                                             %
728 %   U n r e g i s t e r P C X I m a g e                                       %
729 %                                                                             %
730 %                                                                             %
731 %                                                                             %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 %
734 %  UnregisterPCXImage() removes format registrations made by the
735 %  PCX module from the list of supported formats.
736 %
737 %  The format of the UnregisterPCXImage method is:
738 %
739 %      UnregisterPCXImage(void)
740 %
741 */
742 ModuleExport void UnregisterPCXImage(void)
743 {
744   (void) UnregisterMagickInfo("DCX");
745   (void) UnregisterMagickInfo("PCX");
746 }
747 \f
748 /*
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 %                                                                             %
751 %                                                                             %
752 %                                                                             %
753 %   W r i t e P C X I m a g e                                                 %
754 %                                                                             %
755 %                                                                             %
756 %                                                                             %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 %
759 %  WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
760 %  format.
761 %
762 %  The format of the WritePCXImage method is:
763 %
764 %      MagickBooleanType WritePCXImage(const ImageInfo *image_info,
765 %        Image *image,ExceptionInfo *exception)
766 %
767 %  A description of each parameter follows.
768 %
769 %    o image_info: the image info.
770 %
771 %    o image:  The image.
772 %
773 %    o exception: return any errors or warnings in this structure.
774 %
775 */
776
777 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
778   const unsigned char *pixels,Image *image)
779 {
780   register const unsigned char
781     *q;
782
783   register ssize_t
784     i,
785     x;
786
787   ssize_t
788     count;
789
790   unsigned char
791     packet,
792     previous;
793
794   q=pixels;
795   for (i=0; i < (ssize_t) pcx_info->planes; i++)
796   {
797     if (pcx_info->encoding == 0)
798       {
799         for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
800           (void) WriteBlobByte(image,(unsigned char) (*q++));
801       }
802     else
803       {
804         previous=(*q++);
805         count=1;
806         for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
807         {
808           packet=(*q++);
809           if ((packet == previous) && (count < 63))
810             {
811               count++;
812               continue;
813             }
814           if ((count > 1) || ((previous & 0xc0) == 0xc0))
815             {
816               count|=0xc0;
817               (void) WriteBlobByte(image,(unsigned char) count);
818             }
819           (void) WriteBlobByte(image,previous);
820           previous=packet;
821           count=1;
822         }
823         if ((count > 1) || ((previous & 0xc0) == 0xc0))
824           {
825             count|=0xc0;
826             (void) WriteBlobByte(image,(unsigned char) count);
827           }
828         (void) WriteBlobByte(image,previous);
829       }
830   }
831   return (MagickTrue);
832 }
833
834 static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
835   ExceptionInfo *exception)
836 {
837   MagickBooleanType
838     status;
839
840   MagickOffsetType
841     offset,
842     *page_table,
843     scene;
844
845   PCXInfo
846     pcx_info;
847
848   register const Quantum
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   assert(exception != (ExceptionInfo *) NULL);
878   assert(exception->signature == MagickSignature);
879   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
880   if (status == MagickFalse)
881     return(status);
882   if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
883       (IsRGBColorspace(image->colorspace) == MagickFalse) &&
884       (IsImageGray(image,exception) == MagickFalse))
885     (void) TransformImageColorspace(image,sRGBColorspace,exception);
886   page_table=(MagickOffsetType *) NULL;
887   if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
888       ((GetNextImageInList(image) != (Image *) NULL) &&
889        (image_info->adjoin != MagickFalse)))
890     {
891       /*
892         Write the DCX page table.
893       */
894       (void) WriteBlobLSBLong(image,0x3ADE68B1L);
895       page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
896         sizeof(*page_table));
897       if (page_table == (MagickOffsetType *) NULL)
898         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
899       for (scene=0; scene < 1024; scene++)
900         (void) WriteBlobLSBLong(image,0x00000000L);
901     }
902   scene=0;
903   do
904   {
905     if (page_table != (MagickOffsetType *) NULL)
906       page_table[scene]=TellBlob(image);
907     /*
908       Initialize PCX raster file header.
909     */
910     pcx_info.identifier=0x0a;
911     pcx_info.version=5;
912     pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
913     pcx_info.bits_per_pixel=8;
914     if ((image->storage_class == PseudoClass) &&
915         (IsImageMonochrome(image,exception) != MagickFalse))
916       pcx_info.bits_per_pixel=1;
917     pcx_info.left=0;
918     pcx_info.top=0;
919     pcx_info.right=(unsigned short) (image->columns-1);
920     pcx_info.bottom=(unsigned short) (image->rows-1);
921     switch (image->units)
922     {
923       case UndefinedResolution:
924       case PixelsPerInchResolution:
925       default:
926       {
927         pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
928         pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
929         break;
930       }
931       case PixelsPerCentimeterResolution:
932       {
933         pcx_info.horizontal_resolution=(unsigned short)
934           (2.54*image->resolution.x+0.5);
935         pcx_info.vertical_resolution=(unsigned short)
936           (2.54*image->resolution.y+0.5);
937         break;
938       }
939     }
940     pcx_info.reserved=0;
941     pcx_info.planes=1;
942     if ((image->storage_class == DirectClass) || (image->colors > 256))
943       {
944         pcx_info.planes=3;
945         if (image->matte != MagickFalse)
946           pcx_info.planes++;
947       }
948     pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
949       pcx_info.bits_per_pixel+7)/8);
950     pcx_info.palette_info=1;
951     pcx_info.colormap_signature=0x0c;
952     /*
953       Write PCX header.
954     */
955     (void) WriteBlobByte(image,pcx_info.identifier);
956     (void) WriteBlobByte(image,pcx_info.version);
957     (void) WriteBlobByte(image,pcx_info.encoding);
958     (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
959     (void) WriteBlobLSBShort(image,pcx_info.left);
960     (void) WriteBlobLSBShort(image,pcx_info.top);
961     (void) WriteBlobLSBShort(image,pcx_info.right);
962     (void) WriteBlobLSBShort(image,pcx_info.bottom);
963     (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
964     (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
965     /*
966       Dump colormap to file.
967     */
968     pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
969       3*sizeof(*pcx_colormap));
970     if (pcx_colormap == (unsigned char *) NULL)
971       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
972     (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
973     q=pcx_colormap;
974     if ((image->storage_class == PseudoClass) && (image->colors <= 256))
975       for (i=0; i < (ssize_t) image->colors; i++)
976       {
977         *q++=ScaleQuantumToChar(image->colormap[i].red);
978         *q++=ScaleQuantumToChar(image->colormap[i].green);
979         *q++=ScaleQuantumToChar(image->colormap[i].blue);
980       }
981     (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
982     (void) WriteBlobByte(image,pcx_info.reserved);
983     (void) WriteBlobByte(image,pcx_info.planes);
984     (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
985     (void) WriteBlobLSBShort(image,pcx_info.palette_info);
986     for (i=0; i < 58; i++)
987       (void) WriteBlobByte(image,'\0');
988     length=(size_t) pcx_info.bytes_per_line;
989     pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
990       sizeof(*pcx_pixels));
991     if (pcx_pixels == (unsigned char *) NULL)
992       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
993     q=pcx_pixels;
994     if ((image->storage_class == DirectClass) || (image->colors > 256))
995       {
996         const Quantum
997           *pixels;
998
999         /*
1000           Convert DirectClass image to PCX raster pixels.
1001         */
1002         for (y=0; y < (ssize_t) image->rows; y++)
1003         {
1004           pixels=GetVirtualPixels(image,0,y,image->columns,1,exception);
1005           if (pixels == (const Quantum *) NULL)
1006             break;
1007           q=pcx_pixels;
1008           for (i=0; i < pcx_info.planes; i++)
1009           {
1010             p=pixels;
1011             switch ((int) i)
1012             {
1013               case 0:
1014               {
1015                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1016                 {
1017                   *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1018                   p+=GetPixelChannels(image);
1019                 }
1020                 break;
1021               }
1022               case 1:
1023               {
1024                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1025                 {
1026                   *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1027                   p+=GetPixelChannels(image);
1028                 }
1029                 break;
1030               }
1031               case 2:
1032               {
1033                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
1034                 {
1035                   *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1036                   p+=GetPixelChannels(image);
1037                 }
1038                 break;
1039               }
1040               case 3:
1041               default:
1042               {
1043                 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
1044                 {
1045                   *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1046                   p+=GetPixelChannels(image);
1047                 }
1048                 break;
1049               }
1050             }
1051           }
1052           if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1053             break;
1054           if (image->previous == (Image *) NULL)
1055             {
1056               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1057                 image->rows);
1058               if (status == MagickFalse)
1059                 break;
1060             }
1061         }
1062       }
1063     else
1064       {
1065         if (pcx_info.bits_per_pixel > 1)
1066           for (y=0; y < (ssize_t) image->rows; y++)
1067           {
1068             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1069             if (p == (const Quantum *) NULL)
1070               break;
1071             q=pcx_pixels;
1072             for (x=0; x < (ssize_t) image->columns; x++)
1073             {
1074               *q++=(unsigned char) GetPixelIndex(image,p);
1075               p+=GetPixelChannels(image);
1076             }
1077             if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1078               break;
1079             if (image->previous == (Image *) NULL)
1080               {
1081                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1082                 image->rows);
1083                 if (status == MagickFalse)
1084                   break;
1085               }
1086           }
1087         else
1088           {
1089             Quantum
1090               polarity;
1091
1092             register unsigned char
1093               bit,
1094               byte;
1095
1096             /*
1097               Convert PseudoClass image to a PCX monochrome image.
1098             */
1099             polarity=(Quantum) (GetPixelInfoIntensity(
1100               &image->colormap[0]) < (QuantumRange/2) ? 1 : 0);
1101             if (image->colors == 2)
1102               polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) <
1103                 GetPixelInfoIntensity(&image->colormap[1]) ? 1 : 0);
1104             for (y=0; y < (ssize_t) image->rows; y++)
1105             {
1106               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1107               if (p == (const Quantum *) NULL)
1108                 break;
1109               bit=0;
1110               byte=0;
1111               q=pcx_pixels;
1112               for (x=0; x < (ssize_t) image->columns; x++)
1113               {
1114                 byte<<=1;
1115                 if (GetPixelIndex(image,p) == polarity)
1116                   byte|=0x01;
1117                 bit++;
1118                 if (bit == 8)
1119                   {
1120                     *q++=byte;
1121                     bit=0;
1122                     byte=0;
1123                   }
1124                 p+=GetPixelChannels(image);
1125               }
1126               if (bit != 0)
1127                 *q++=byte << (8-bit);
1128               if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1129                 break;
1130               if (image->previous == (Image *) NULL)
1131                 {
1132                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1133                     y,image->rows);
1134                   if (status == MagickFalse)
1135                     break;
1136                 }
1137             }
1138           }
1139         (void) WriteBlobByte(image,pcx_info.colormap_signature);
1140         (void) WriteBlob(image,3*256,pcx_colormap);
1141       }
1142     pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1143     pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1144     if (page_table == (MagickOffsetType *) NULL)
1145       break;
1146     if (scene >= 1023)
1147       break;
1148     if (GetNextImageInList(image) == (Image *) NULL)
1149       break;
1150     image=SyncNextImageInList(image);
1151     status=SetImageProgress(image,SaveImagesTag,scene++,
1152       GetImageListLength(image));
1153     if (status == MagickFalse)
1154       break;
1155   } while (image_info->adjoin != MagickFalse);
1156   if (page_table != (MagickOffsetType *) NULL)
1157     {
1158       /*
1159         Write the DCX page table.
1160       */
1161       page_table[scene+1]=0;
1162       offset=SeekBlob(image,0L,SEEK_SET);
1163       if (offset < 0)
1164         ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1165       (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1166       for (i=0; i <= (ssize_t) scene; i++)
1167         (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
1168       page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1169     }
1170   if (status == MagickFalse)
1171     {
1172       char
1173         *message;
1174
1175       message=GetExceptionMessage(errno);
1176       (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1177         "UnableToWriteFile","`%s': %s",image->filename,message);
1178       message=DestroyString(message);
1179     }
1180   (void) CloseBlob(image);
1181   return(MagickTrue);
1182 }