blob: 1ca29538a02513608b2017afcc71c14a89c663cb [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
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/colorspace.h"
49#include "magick/exception.h"
50#include "magick/exception-private.h"
51#include "magick/image.h"
52#include "magick/image-private.h"
53#include "magick/list.h"
54#include "magick/magick.h"
55#include "magick/memory_.h"
56#include "magick/monitor.h"
57#include "magick/monitor-private.h"
58#include "magick/quantum-private.h"
59#include "magick/static.h"
60#include "magick/string_.h"
61#include "magick/module.h"
62
63/*
64 Typedef declarations.
65*/
66typedef struct _PCXInfo
67{
68 unsigned char
69 identifier,
70 version,
71 encoding,
72 bits_per_pixel;
73
74 unsigned short
75 left,
76 top,
77 right,
78 bottom,
79 horizontal_resolution,
80 vertical_resolution;
81
82 unsigned char
83 reserved,
84 planes;
85
86 unsigned short
87 bytes_per_line,
88 palette_info;
89
90 unsigned char
91 colormap_signature;
92} PCXInfo;
93
94/*
95 Forward declarations.
96*/
97static MagickBooleanType
98 WritePCXImage(const ImageInfo *,Image *);
99
100/*
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102% %
103% %
104% %
105% I s D C X %
106% %
107% %
108% %
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110%
111% IsDCX() returns MagickTrue if the image format type, identified by the
112% magick string, is DCX.
113%
114% The format of the IsDCX method is:
115%
116% MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
117%
118% A description of each parameter follows:
119%
120% o magick: compare image format pattern against these bytes.
121%
122% o length: Specifies the length of the magick string.
123%
124%
125*/
126static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
127{
128 if (length < 4)
129 return(MagickFalse);
130 if (memcmp(magick,"\261\150\336\72",4) == 0)
131 return(MagickTrue);
132 return(MagickFalse);
133}
134
135/*
136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137% %
138% %
139% %
140% I s P C X %
141% %
142% %
143% %
144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145%
146% IsPCX() returns MagickTrue if the image format type, identified by the
147% magick string, is PCX.
148%
149% The format of the IsPCX method is:
150%
151% MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
152%
153% A description of each parameter follows:
154%
155% o magick: compare image format pattern against these bytes.
156%
157% o length: Specifies the length of the magick string.
158%
159%
160*/
161static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
162{
163 if (length < 2)
164 return(MagickFalse);
165 if (memcmp(magick,"\012\002",2) == 0)
166 return(MagickTrue);
167 if (memcmp(magick,"\012\005",2) == 0)
168 return(MagickTrue);
169 return(MagickFalse);
170}
171
172/*
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174% %
175% %
176% %
177% R e a d P C X I m a g e %
178% %
179% %
180% %
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182%
183% ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
184% It allocates the memory necessary for the new Image structure and returns
185% a pointer to the new image.
186%
187% The format of the ReadPCXImage method is:
188%
189% Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
190%
191% A description of each parameter follows:
192%
193% o image_info: the image info.
194%
195% o exception: return any errors or warnings in this structure.
196%
197*/
198
199static inline long MagickAbsoluteValue(const long x)
200{
201 if (x < 0)
202 return(-x);
203 return(x);
204}
205
206static inline size_t MagickMax(const size_t x,const size_t y)
207{
208 if (x > y)
209 return(x);
210 return(y);
211}
212
213static inline size_t MagickMin(const size_t x,const size_t y)
214{
215 if (x < y)
216 return(x);
217 return(y);
218}
219
220static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
221{
222 Image
223 *image;
224
225 int
226 bits,
227 id,
228 mask;
229
230 long
231 y;
232
233 MagickBooleanType
234 status;
235
236 MagickOffsetType
237 offset,
238 *page_table;
239
240 PCXInfo
241 pcx_info;
242
243 register IndexPacket
244 *indexes;
245
246 register long
247 x;
248
249 register PixelPacket
250 *q;
251
252 register long
253 i;
254
255 register unsigned char
256 *p,
257 *r;
258
259 ssize_t
260 count;
261
262 unsigned char
263 packet,
264 *pcx_colormap,
265 *pcx_pixels,
266 *scanline;
267
268 unsigned long
269 pcx_packets;
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 unsigned long
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=(unsigned long) MagickAbsoluteValue((long) pcx_info.right-
342 pcx_info.left)+1UL;
343 image->rows=(unsigned long) MagickAbsoluteValue((long) 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 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
361 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
362 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
363 image->colors=(unsigned long) MagickMin(1UL << (1UL*
364 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
365 if (AcquireImageColormap(image,image->colors) == MagickFalse)
366 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
367 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
368 image->storage_class=DirectClass;
369 p=pcx_colormap;
370 for (i=0; i < (long) image->colors; i++)
371 {
372 image->colormap[i].red=ScaleCharToQuantum(*p++);
373 image->colormap[i].green=ScaleCharToQuantum(*p++);
374 image->colormap[i].blue=ScaleCharToQuantum(*p++);
375 }
376 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
377 pcx_info.palette_info=ReadBlobLSBShort(image);
378 for (i=0; i < 58; i++)
379 (void) ReadBlobByte(image);
380 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
381 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
382 break;
383 /*
384 Read image data.
385 */
386 pcx_packets=(unsigned long) image->rows*pcx_info.bytes_per_line*
387 pcx_info.planes;
388 pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
389 sizeof(*pcx_pixels));
390 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
391 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
392 if ((pcx_pixels == (unsigned char *) NULL) ||
393 (scanline == (unsigned char *) NULL))
394 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
395 /*
396 Uncompress image data.
397 */
398 p=pcx_pixels;
399 if (pcx_info.encoding == 0)
400 while (pcx_packets != 0)
401 {
402 packet=(unsigned char) ReadBlobByte(image);
403 if (EOFBlob(image) != MagickFalse)
404 break;
405 *p++=packet;
406 pcx_packets--;
407 }
408 else
409 while (pcx_packets != 0)
410 {
411 packet=(unsigned char) ReadBlobByte(image);
412 if (EOFBlob(image) != MagickFalse)
413 break;
414 if ((packet & 0xc0) != 0xc0)
415 {
416 *p++=packet;
417 pcx_packets--;
418 continue;
419 }
420 count=(ssize_t) (packet & 0x3f);
421 packet=(unsigned char) ReadBlobByte(image);
422 if (EOFBlob(image) != MagickFalse)
423 break;
424 for ( ; count != 0; count--)
425 {
426 *p++=packet;
427 pcx_packets--;
428 if (pcx_packets == 0)
429 break;
430 }
431 }
432 if (image->storage_class == DirectClass)
433 image->matte=pcx_info.planes > 3 ? MagickTrue : MagickFalse;
434 else
435 if ((pcx_info.version == 5) ||
436 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
437 {
438 /*
439 Initialize image colormap.
440 */
441 if (image->colors > 256)
442 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
443 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
444 {
445 /*
446 Monochrome colormap.
447 */
448 image->colormap[0].red=(Quantum) 0;
449 image->colormap[0].green=(Quantum) 0;
450 image->colormap[0].blue=(Quantum) 0;
451 image->colormap[1].red=(Quantum) QuantumRange;
452 image->colormap[1].green=(Quantum) QuantumRange;
453 image->colormap[1].blue=(Quantum) QuantumRange;
454 }
455 else
456 if (image->colors > 16)
457 {
458 /*
459 256 color images have their color map at the end of the file.
460 */
461 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
462 count=ReadBlob(image,3*image->colors,pcx_colormap);
463 p=pcx_colormap;
464 for (i=0; i < (long) image->colors; i++)
465 {
466 image->colormap[i].red=ScaleCharToQuantum(*p++);
467 image->colormap[i].green=ScaleCharToQuantum(*p++);
468 image->colormap[i].blue=ScaleCharToQuantum(*p++);
469 }
470 }
471 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
472 }
473 /*
474 Convert PCX raster image to pixel packets.
475 */
476 for (y=0; y < (long) image->rows; y++)
477 {
478 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
479 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
480 if (q == (PixelPacket *) NULL)
481 break;
482 indexes=GetAuthenticIndexQueue(image);
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 < (long) 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 < (long) image->columns; x++)
521 *r++=0;
522 for (i=0; i < pcx_info.planes; i++)
523 {
524 r=scanline;
525 for (x=0; x < (long) 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 long
543 bit;
544
545 for (x=0; x < ((long) 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 >= (long) (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 < ((long) 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 >= (long) (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 < ((long) 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 < (long) image->columns; x++)
602 {
603 if (image->storage_class == PseudoClass)
604 indexes[x]=(IndexPacket) (*r++);
605 else
606 {
607 q->red=ScaleCharToQuantum(*r++);
608 q->green=ScaleCharToQuantum(*r++);
609 q->blue=ScaleCharToQuantum(*r++);
610 if (image->matte != MagickFalse)
611 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*r++));
612 }
613 q++;
614 }
615 if (SyncAuthenticPixels(image,exception) == MagickFalse)
616 break;
617 if (image->previous == (Image *) NULL)
618 {
619 status=SetImageProgress(image,LoadImageTag,y,image->rows);
620 if (status == MagickFalse)
621 break;
622 }
623 }
624 if (image->storage_class == PseudoClass)
625 (void) SyncImage(image);
626 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
627 if (pcx_colormap != (unsigned char *) NULL)
628 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
629 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
630 if (EOFBlob(image) != MagickFalse)
631 {
632 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
633 image->filename);
634 break;
635 }
636 /*
637 Proceed to next image.
638 */
639 if (image_info->number_scenes != 0)
640 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
641 break;
642 if (page_table == (MagickOffsetType *) NULL)
643 break;
644 if (page_table[id] == 0)
645 break;
646 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
647 if (offset < 0)
648 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
649 count=ReadBlob(image,1,&pcx_info.identifier);
650 if ((count != 0) && (pcx_info.identifier == 0x0a))
651 {
652 /*
653 Allocate next image structure.
654 */
655 AcquireNextImage(image_info,image);
656 if (GetNextImageInList(image) == (Image *) NULL)
657 {
658 image=DestroyImageList(image);
659 return((Image *) NULL);
660 }
661 image=SyncNextImageInList(image);
662 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
663 GetBlobSize(image));
664 if (status == MagickFalse)
665 break;
666 }
667 }
668 if (page_table != (MagickOffsetType *) NULL)
669 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
670 (void) CloseBlob(image);
671 return(GetFirstImageInList(image));
672}
673
674/*
675%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676% %
677% %
678% %
679% R e g i s t e r P C X I m a g e %
680% %
681% %
682% %
683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684%
685% RegisterPCXImage() adds attributes for the PCX image format to
686% the list of supported formats. The attributes include the image format
687% tag, a method to read and/or write the format, whether the format
688% supports the saving of more than one frame to the same file or blob,
689% whether the format supports native in-memory I/O, and a brief
690% description of the format.
691%
692% The format of the RegisterPCXImage method is:
693%
694% unsigned long RegisterPCXImage(void)
695%
696*/
697ModuleExport unsigned long RegisterPCXImage(void)
698{
699 MagickInfo
700 *entry;
701
702 entry=SetMagickInfo("DCX");
703 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
704 entry->encoder=(EncodeImageHandler *) WritePCXImage;
705 entry->seekable_stream=MagickTrue;
706 entry->magick=(IsImageFormatHandler *) IsDCX;
707 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
708 entry->module=ConstantString("PCX");
709 (void) RegisterMagickInfo(entry);
710 entry=SetMagickInfo("PCX");
711 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
712 entry->encoder=(EncodeImageHandler *) WritePCXImage;
713 entry->magick=(IsImageFormatHandler *) IsPCX;
714 entry->adjoin=MagickFalse;
715 entry->seekable_stream=MagickTrue;
716 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
717 entry->module=ConstantString("PCX");
718 (void) RegisterMagickInfo(entry);
719 return(MagickImageCoderSignature);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% U n r e g i s t e r P C X I m a g e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% UnregisterPCXImage() removes format registrations made by the
734% PCX module from the list of supported formats.
735%
736% The format of the UnregisterPCXImage method is:
737%
738% UnregisterPCXImage(void)
739%
740*/
741ModuleExport void UnregisterPCXImage(void)
742{
743 (void) UnregisterMagickInfo("DCX");
744 (void) UnregisterMagickInfo("PCX");
745}
746
747/*
748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749% %
750% %
751% %
752% W r i t e P C X I m a g e %
753% %
754% %
755% %
756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757%
758% WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
759% format.
760%
761% The format of the WritePCXImage method is:
762%
763% MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
764%
765% A description of each parameter follows.
766%
767% o image_info: the image info.
768%
769% o image: The image.
770%
771%
772*/
773static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
774 const unsigned char *pixels,Image *image)
775{
776 register const unsigned char
777 *q;
778
779 register long
780 i,
781 x;
782
783 ssize_t
784 count;
785
786 unsigned char
787 packet,
788 previous;
789
790 q=pixels;
791 for (i=0; i < (long) pcx_info->planes; i++)
792 {
793 previous=(*q++);
794 count=1;
795 for (x=0; x < (long) (pcx_info->bytes_per_line-1); x++)
796 {
797 packet=(*q++);
798 if ((packet == previous) && (count < 63))
799 {
800 count++;
801 continue;
802 }
803 if ((count > 1) || ((previous & 0xc0) == 0xc0))
804 {
805 count|=0xc0;
806 (void) WriteBlobByte(image,(unsigned char) count);
807 }
808 (void) WriteBlobByte(image,previous);
809 previous=packet;
810 count=1;
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 }
819 return (MagickTrue);
820}
821
822static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
823{
824 long
825 y;
826
827 MagickBooleanType
828 status;
829
830 MagickOffsetType
831 offset,
832 *page_table,
833 scene;
834
835 PCXInfo
836 pcx_info;
837
838 register const IndexPacket
839 *indexes;
840
841 register const PixelPacket
842 *p;
843
844 register long
845 i,
846 x;
847
848 register unsigned char
849 *q;
850
851 size_t
852 length;
853
854 unsigned char
855 *pcx_colormap,
856 *pcx_pixels;
857
858 /*
859 Open output image file.
860 */
861 assert(image_info != (const ImageInfo *) NULL);
862 assert(image_info->signature == MagickSignature);
863 assert(image != (Image *) NULL);
864 assert(image->signature == MagickSignature);
865 if (image->debug != MagickFalse)
866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
867 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
868 if (status == MagickFalse)
869 return(status);
870 if (image->colorspace != RGBColorspace)
871 (void) TransformImageColorspace(image,RGBColorspace);
872 page_table=(MagickOffsetType *) NULL;
873 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
874 ((GetNextImageInList(image) != (Image *) NULL) &&
875 (image_info->adjoin != MagickFalse)))
876 {
877 /*
878 Write the DCX page table.
879 */
880 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
881 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
882 sizeof(*page_table));
883 if (page_table == (MagickOffsetType *) NULL)
884 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
885 for (scene=0; scene < 1024; scene++)
886 (void) WriteBlobLSBLong(image,0x00000000L);
887 }
888 scene=0;
889 do
890 {
891 if (page_table != (MagickOffsetType *) NULL)
892 page_table[scene]=TellBlob(image);
893 /*
894 Initialize PCX raster file header.
895 */
896 pcx_info.identifier=0x0a;
897 pcx_info.version=5;
898 pcx_info.encoding=1;
899 pcx_info.bits_per_pixel=8;
900 if ((image->storage_class == PseudoClass) &&
901 (IsMonochromeImage(image,&image->exception) != MagickFalse))
902 pcx_info.bits_per_pixel=1;
903 pcx_info.left=0;
904 pcx_info.top=0;
905 pcx_info.right=(unsigned short) (image->columns-1);
906 pcx_info.bottom=(unsigned short) (image->rows-1);
907 switch (image->units)
908 {
909 case UndefinedResolution:
910 case PixelsPerInchResolution:
911 default:
912 {
913 pcx_info.horizontal_resolution=(unsigned short) image->x_resolution;
914 pcx_info.vertical_resolution=(unsigned short) image->y_resolution;
915 break;
916 }
917 case PixelsPerCentimeterResolution:
918 {
919 pcx_info.horizontal_resolution=(unsigned short)
920 (2.54*image->x_resolution+0.5);
921 pcx_info.vertical_resolution=(unsigned short)
922 (2.54*image->y_resolution+0.5);
923 break;
924 }
925 }
926 pcx_info.reserved=0;
927 pcx_info.planes=1;
928 if ((image->storage_class == DirectClass) || (image->colors > 256))
929 {
930 pcx_info.planes=3;
931 if (image->matte != MagickFalse)
932 pcx_info.planes++;
933 }
934 pcx_info.bytes_per_line=(unsigned short) (((unsigned long) image->columns*
935 pcx_info.bits_per_pixel+7)/8);
936 pcx_info.palette_info=1;
937 pcx_info.colormap_signature=0x0c;
938 /*
939 Write PCX header.
940 */
941 (void) WriteBlobByte(image,pcx_info.identifier);
942 (void) WriteBlobByte(image,pcx_info.version);
943 (void) WriteBlobByte(image,pcx_info.encoding);
944 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
945 (void) WriteBlobLSBShort(image,pcx_info.left);
946 (void) WriteBlobLSBShort(image,pcx_info.top);
947 (void) WriteBlobLSBShort(image,pcx_info.right);
948 (void) WriteBlobLSBShort(image,pcx_info.bottom);
949 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
950 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
951 /*
952 Dump colormap to file.
953 */
954 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
955 3*sizeof(*pcx_colormap));
956 if (pcx_colormap == (unsigned char *) NULL)
957 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
958 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
959 q=pcx_colormap;
960 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
961 for (i=0; i < (long) image->colors; i++)
962 {
963 *q++=ScaleQuantumToChar(image->colormap[i].red);
964 *q++=ScaleQuantumToChar(image->colormap[i].green);
965 *q++=ScaleQuantumToChar(image->colormap[i].blue);
966 }
967 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
968 (void) WriteBlobByte(image,pcx_info.reserved);
969 (void) WriteBlobByte(image,pcx_info.planes);
970 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
971 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
972 for (i=0; i < 58; i++)
973 (void) WriteBlobByte(image,'\0');
974 length=(size_t) pcx_info.bytes_per_line;
975 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,
976 pcx_info.planes*sizeof(*pcx_pixels));
977 if (pcx_pixels == (unsigned char *) NULL)
978 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
979 q=pcx_pixels;
980 if ((image->storage_class == DirectClass) || (image->colors > 256))
981 {
982 const PixelPacket
983 *pixels;
984
985 /*
986 Convert DirectClass image to PCX raster pixels.
987 */
988 for (y=0; y < (long) image->rows; y++)
989 {
990 pixels=GetVirtualPixels(image,0,y,image->columns,1,
991 &image->exception);
992 if (pixels == (const PixelPacket *) NULL)
993 break;
994 q=pcx_pixels;
995 for (i=0; i < pcx_info.planes; i++)
996 {
997 p=pixels;
998 switch ((int) i)
999 {
1000 case 0:
1001 {
1002 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1003 {
1004 *q++=ScaleQuantumToChar(p->red);
1005 p++;
1006 }
1007 break;
1008 }
1009 case 1:
1010 {
1011 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1012 {
1013 *q++=ScaleQuantumToChar(p->green);
1014 p++;
1015 }
1016 break;
1017 }
1018 case 2:
1019 {
1020 for (x=0; x < (long) pcx_info.bytes_per_line; x++)
1021 {
1022 *q++=ScaleQuantumToChar(p->blue);
1023 p++;
1024 }
1025 break;
1026 }
1027 case 3:
1028 default:
1029 {
1030 for (x=(long) pcx_info.bytes_per_line; x != 0; x--)
1031 {
1032 *q++=ScaleQuantumToChar((Quantum) (QuantumRange-p->opacity));
1033 p++;
1034 }
1035 break;
1036 }
1037 }
1038 }
1039 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1040 break;
1041 if (image->previous == (Image *) NULL)
1042 {
1043 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1044 if (status == MagickFalse)
1045 break;
1046 }
1047 }
1048 }
1049 else
1050 {
1051 if (pcx_info.bits_per_pixel > 1)
1052 for (y=0; y < (long) image->rows; y++)
1053 {
1054 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1055 if (p == (const PixelPacket *) NULL)
1056 break;
1057 indexes=GetVirtualIndexQueue(image);
1058 q=pcx_pixels;
1059 for (x=0; x < (long) image->columns; x++)
1060 *q++=(unsigned char) indexes[x];
1061 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1062 break;
1063 if (image->previous == (Image *) NULL)
1064 {
1065 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1066 if (status == MagickFalse)
1067 break;
1068 }
1069 }
1070 else
1071 {
1072 IndexPacket
1073 polarity;
1074
1075 register unsigned char
1076 bit,
1077 byte;
1078
1079 /*
1080 Convert PseudoClass image to a PCX monochrome image.
1081 */
1082 polarity=(IndexPacket) (PixelIntensityToQuantum(
1083 &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
1084 if (image->colors == 2)
1085 polarity=(IndexPacket) (
1086 PixelIntensityToQuantum(&image->colormap[0]) <
1087 PixelIntensityToQuantum(&image->colormap[1]) ? 1 : 0);
1088 for (y=0; y < (long) image->rows; y++)
1089 {
1090 p=GetVirtualPixels(image,0,y,image->columns,1,
1091 &image->exception);
1092 if (p == (const PixelPacket *) NULL)
1093 break;
1094 indexes=GetVirtualIndexQueue(image);
1095 bit=0;
1096 byte=0;
1097 q=pcx_pixels;
1098 for (x=0; x < (long) image->columns; x++)
1099 {
1100 byte<<=1;
1101 if (indexes[x] == polarity)
1102 byte|=0x01;
1103 bit++;
1104 if (bit == 8)
1105 {
1106 *q++=byte;
1107 bit=0;
1108 byte=0;
1109 }
1110 p++;
1111 }
1112 if (bit != 0)
1113 *q++=byte << (8-bit);
1114 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1115 break;
1116 if (image->previous == (Image *) NULL)
1117 {
1118 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1119 if (status == MagickFalse)
1120 break;
1121 }
1122 }
1123 }
1124 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1125 (void) WriteBlob(image,3*256,pcx_colormap);
1126 }
1127 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1128 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
1129 if (GetNextImageInList(image) == (Image *) NULL)
1130 break;
1131 image=SyncNextImageInList(image);
1132 status=SetImageProgress(image,SaveImagesTag,scene++,
1133 GetImageListLength(image));
1134 if (status == MagickFalse)
1135 break;
1136 if (scene >= 1023)
1137 break;
1138 } while (image_info->adjoin != MagickFalse);
1139 if (page_table != (MagickOffsetType *) NULL)
1140 {
1141 /*
1142 Write the DCX page table.
1143 */
1144 page_table[scene+1]=0;
1145 offset=SeekBlob(image,0L,SEEK_SET);
1146 if (offset < 0)
1147 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1148 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
1149 for (i=0; i <= (long) scene; i++)
1150 (void) WriteBlobLSBLong(image,(unsigned long) page_table[i]);
1151 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1152 }
1153 if (status == MagickFalse)
1154 {
1155 char
1156 *message;
1157
1158 message=GetExceptionMessage(errno);
1159 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1160 FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
1161 message=DestroyString(message);
1162 }
1163 (void) CloseBlob(image);
1164 return(MagickTrue);
1165}