blob: 58c6549db9ca8af1f567e58e07adb452b012ccc7 [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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#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"
cristy3ed852e2009-09-05 21:47:34 +000066
67/*
68 Typedef declarations.
69*/
70typedef 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
98/*
99 Forward declarations.
100*/
101static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000102 WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000103
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%
cristy3ed852e2009-09-05 21:47:34 +0000128*/
129static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
130{
131 if (length < 4)
132 return(MagickFalse);
133 if (memcmp(magick,"\261\150\336\72",4) == 0)
134 return(MagickTrue);
135 return(MagickFalse);
136}
137
138/*
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140% %
141% %
142% %
143% I s P C X %
144% %
145% %
146% %
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148%
149% IsPCX() returns MagickTrue if the image format type, identified by the
150% magick string, is PCX.
151%
152% The format of the IsPCX method is:
153%
154% MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
155%
156% A description of each parameter follows:
157%
158% o magick: compare image format pattern against these bytes.
159%
160% o length: Specifies the length of the magick string.
161%
cristy3ed852e2009-09-05 21:47:34 +0000162*/
163static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
164{
165 if (length < 2)
166 return(MagickFalse);
167 if (memcmp(magick,"\012\002",2) == 0)
168 return(MagickTrue);
169 if (memcmp(magick,"\012\005",2) == 0)
170 return(MagickTrue);
171 return(MagickFalse);
172}
173
174/*
175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176% %
177% %
178% %
179% R e a d P C X I m a g e %
180% %
181% %
182% %
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184%
185% ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
186% It allocates the memory necessary for the new Image structure and returns
187% a pointer to the new image.
188%
189% The format of the ReadPCXImage method is:
190%
191% Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
192%
193% A description of each parameter follows:
194%
195% o image_info: the image info.
196%
197% o exception: return any errors or warnings in this structure.
198%
199*/
200
cristybb503372010-05-27 20:51:26 +0000201static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000202{
203 if (x < 0)
204 return(-x);
205 return(x);
206}
207
208static inline size_t MagickMax(const size_t x,const size_t y)
209{
210 if (x > y)
211 return(x);
212 return(y);
213}
214
215static inline size_t MagickMin(const size_t x,const size_t y)
216{
217 if (x < y)
218 return(x);
219 return(y);
220}
221
222static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
223{
224 Image
225 *image;
226
227 int
228 bits,
229 id,
230 mask;
231
cristy3ed852e2009-09-05 21:47:34 +0000232 MagickBooleanType
233 status;
234
235 MagickOffsetType
236 offset,
237 *page_table;
238
239 PCXInfo
240 pcx_info;
241
cristybb503372010-05-27 20:51:26 +0000242 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000243 x;
244
cristy4c08aed2011-07-01 19:47:50 +0000245 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000246 *q;
247
cristybb503372010-05-27 20:51:26 +0000248 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000249 i;
250
251 register unsigned char
252 *p,
253 *r;
254
cristyaff6d802011-04-26 01:46:31 +0000255 size_t
256 one,
257 pcx_packets;
258
cristy3ed852e2009-09-05 21:47:34 +0000259 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000260 count,
261 y;
cristy3ed852e2009-09-05 21:47:34 +0000262
263 unsigned char
264 packet,
265 *pcx_colormap,
266 *pcx_pixels,
267 *scanline;
268
cristy3ed852e2009-09-05 21:47:34 +0000269 /*
270 Open image file.
271 */
272 assert(image_info != (const ImageInfo *) NULL);
273 assert(image_info->signature == MagickSignature);
274 if (image_info->debug != MagickFalse)
275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
276 image_info->filename);
277 assert(exception != (ExceptionInfo *) NULL);
278 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000279 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000280 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
281 if (status == MagickFalse)
282 {
283 image=DestroyImageList(image);
284 return((Image *) NULL);
285 }
286 /*
287 Determine if this a PCX file.
288 */
289 page_table=(MagickOffsetType *) NULL;
290 if (LocaleCompare(image_info->magick,"DCX") == 0)
291 {
cristybb503372010-05-27 20:51:26 +0000292 size_t
cristy3ed852e2009-09-05 21:47:34 +0000293 magic;
294
295 /*
296 Read the DCX page table.
297 */
298 magic=ReadBlobLSBLong(image);
299 if (magic != 987654321)
300 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
301 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
302 sizeof(*page_table));
303 if (page_table == (MagickOffsetType *) NULL)
304 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
305 for (id=0; id < 1024; id++)
306 {
307 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
308 if (page_table[id] == 0)
309 break;
310 }
311 }
312 if (page_table != (MagickOffsetType *) NULL)
313 {
314 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
315 if (offset < 0)
316 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
317 }
318 pcx_colormap=(unsigned char *) NULL;
319 count=ReadBlob(image,1,&pcx_info.identifier);
320 for (id=1; id < 1024; id++)
321 {
322 /*
323 Verify PCX identifier.
324 */
325 pcx_info.version=(unsigned char) ReadBlobByte(image);
326 if ((count == 0) || (pcx_info.identifier != 0x0a))
327 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
328 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
329 pcx_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
330 pcx_info.left=ReadBlobLSBShort(image);
331 pcx_info.top=ReadBlobLSBShort(image);
332 pcx_info.right=ReadBlobLSBShort(image);
333 pcx_info.bottom=ReadBlobLSBShort(image);
334 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
335 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
336 /*
337 Read PCX raster colormap.
338 */
cristybb503372010-05-27 20:51:26 +0000339 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
cristy3ed852e2009-09-05 21:47:34 +0000340 pcx_info.left)+1UL;
cristybb503372010-05-27 20:51:26 +0000341 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
cristy3ed852e2009-09-05 21:47:34 +0000342 pcx_info.top)+1UL;
343 if ((image->columns == 0) || (image->rows == 0) ||
344 (pcx_info.bits_per_pixel == 0))
345 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
346 image->depth=pcx_info.bits_per_pixel <= 8 ? 8U : MAGICKCORE_QUANTUM_DEPTH;
347 image->units=PixelsPerInchResolution;
cristy2a11bef2011-10-28 18:33:11 +0000348 image->resolution.x=(double) pcx_info.horizontal_resolution;
349 image->resolution.y=(double) pcx_info.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +0000350 image->colors=16;
351 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
352 3*sizeof(*pcx_colormap));
353 if (pcx_colormap == (unsigned char *) NULL)
354 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
355 count=ReadBlob(image,3*image->colors,pcx_colormap);
356 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
357 pcx_info.planes=(unsigned char) ReadBlobByte(image);
cristyeaedf062010-05-29 22:36:02 +0000358 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000359 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
360 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
361 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
cristyeaedf062010-05-29 22:36:02 +0000362 image->colors=(size_t) MagickMin(one << (1UL*
cristy3ed852e2009-09-05 21:47:34 +0000363 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
cristy018f07f2011-09-04 21:15:19 +0000364 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000365 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
366 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
367 image->storage_class=DirectClass;
368 p=pcx_colormap;
cristybb503372010-05-27 20:51:26 +0000369 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000370 {
371 image->colormap[i].red=ScaleCharToQuantum(*p++);
372 image->colormap[i].green=ScaleCharToQuantum(*p++);
373 image->colormap[i].blue=ScaleCharToQuantum(*p++);
374 }
375 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
376 pcx_info.palette_info=ReadBlobLSBShort(image);
377 for (i=0; i < 58; i++)
378 (void) ReadBlobByte(image);
379 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
380 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
381 break;
382 /*
383 Read image data.
384 */
cristybb503372010-05-27 20:51:26 +0000385 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
cristy3ed852e2009-09-05 21:47:34 +0000386 pcx_info.planes;
387 pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
388 sizeof(*pcx_pixels));
389 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
390 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
391 if ((pcx_pixels == (unsigned char *) NULL) ||
392 (scanline == (unsigned char *) NULL))
393 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
394 /*
395 Uncompress image data.
396 */
397 p=pcx_pixels;
398 if (pcx_info.encoding == 0)
399 while (pcx_packets != 0)
400 {
401 packet=(unsigned char) ReadBlobByte(image);
402 if (EOFBlob(image) != MagickFalse)
cristyd550b522012-11-29 12:33:11 +0000403 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000404 *p++=packet;
405 pcx_packets--;
406 }
407 else
408 while (pcx_packets != 0)
409 {
410 packet=(unsigned char) ReadBlobByte(image);
411 if (EOFBlob(image) != MagickFalse)
cristyd550b522012-11-29 12:33:11 +0000412 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000413 if ((packet & 0xc0) != 0xc0)
414 {
415 *p++=packet;
416 pcx_packets--;
417 continue;
418 }
419 count=(ssize_t) (packet & 0x3f);
420 packet=(unsigned char) ReadBlobByte(image);
421 if (EOFBlob(image) != MagickFalse)
cristyd550b522012-11-29 12:33:11 +0000422 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000423 for ( ; count != 0; count--)
424 {
425 *p++=packet;
426 pcx_packets--;
427 if (pcx_packets == 0)
428 break;
429 }
430 }
431 if (image->storage_class == DirectClass)
cristyb0a657e2012-08-29 00:45:37 +0000432 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
433 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000434 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;
cristy6e963d82012-06-19 15:23:24 +0000451 image->colormap[1].red=QuantumRange;
452 image->colormap[1].green=QuantumRange;
453 image->colormap[1].blue=QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +0000454 }
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;
cristybb503372010-05-27 20:51:26 +0000464 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000465 {
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 */
cristybb503372010-05-27 20:51:26 +0000476 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000477 {
478 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
479 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000480 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000481 break;
cristy3ed852e2009-09-05 21:47:34 +0000482 r=scanline;
483 if (image->storage_class == DirectClass)
484 for (i=0; i < pcx_info.planes; i++)
485 {
486 r=scanline+i;
cristybb503372010-05-27 20:51:26 +0000487 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000488 {
489 switch (i)
490 {
491 case 0:
492 {
493 *r=(*p++);
494 break;
495 }
496 case 1:
497 {
498 *r=(*p++);
499 break;
500 }
501 case 2:
502 {
503 *r=(*p++);
504 break;
505 }
506 case 3:
507 default:
508 {
509 *r=(*p++);
510 break;
511 }
512 }
513 r+=pcx_info.planes;
514 }
515 }
516 else
517 if (pcx_info.planes > 1)
518 {
cristybb503372010-05-27 20:51:26 +0000519 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000520 *r++=0;
521 for (i=0; i < pcx_info.planes; i++)
522 {
523 r=scanline;
cristybb503372010-05-27 20:51:26 +0000524 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000525 {
526 bits=(*p++);
527 for (mask=0x80; mask != 0; mask>>=1)
528 {
529 if (bits & mask)
530 *r|=1 << i;
531 r++;
532 }
533 }
534 }
535 }
536 else
537 switch (pcx_info.bits_per_pixel)
538 {
539 case 1:
540 {
cristybb503372010-05-27 20:51:26 +0000541 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000542 bit;
543
cristybb503372010-05-27 20:51:26 +0000544 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
546 for (bit=7; bit >= 0; bit--)
547 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
548 p++;
549 }
550 if ((image->columns % 8) != 0)
551 {
cristybb503372010-05-27 20:51:26 +0000552 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
cristy3ed852e2009-09-05 21:47:34 +0000553 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
554 p++;
555 }
556 break;
557 }
558 case 2:
559 {
cristybb503372010-05-27 20:51:26 +0000560 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
cristy3ed852e2009-09-05 21:47:34 +0000561 {
562 *r++=(*p >> 6) & 0x3;
563 *r++=(*p >> 4) & 0x3;
564 *r++=(*p >> 2) & 0x3;
565 *r++=(*p) & 0x3;
566 p++;
567 }
568 if ((image->columns % 4) != 0)
569 {
cristybb503372010-05-27 20:51:26 +0000570 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
cristy3ed852e2009-09-05 21:47:34 +0000571 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
572 p++;
573 }
574 break;
575 }
576 case 4:
577 {
cristybb503372010-05-27 20:51:26 +0000578 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
580 *r++=(*p >> 4) & 0xf;
581 *r++=(*p) & 0xf;
582 p++;
583 }
584 if ((image->columns % 2) != 0)
585 *r++=(*p++ >> 4) & 0xf;
586 break;
587 }
588 case 8:
589 {
590 (void) CopyMagickMemory(r,p,image->columns);
591 break;
592 }
593 default:
594 break;
595 }
596 /*
597 Transfer image scanline.
598 */
599 r=scanline;
cristybb503372010-05-27 20:51:26 +0000600 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
602 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000603 SetPixelIndex(image,*r++,q);
cristy3ed852e2009-09-05 21:47:34 +0000604 else
605 {
cristy4c08aed2011-07-01 19:47:50 +0000606 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
607 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
608 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
cristy8a46d822012-08-28 23:32:39 +0000609 if (image->alpha_trait == BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000610 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
cristy3ed852e2009-09-05 21:47:34 +0000611 }
cristyed231572011-07-14 02:18:59 +0000612 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000613 }
614 if (SyncAuthenticPixels(image,exception) == MagickFalse)
615 break;
616 if (image->previous == (Image *) NULL)
617 {
cristycee97112010-05-28 00:44:52 +0000618 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000619 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000620 if (status == MagickFalse)
621 break;
622 }
623 }
624 if (image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +0000625 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000626 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 */
cristy9950d572011-10-01 18:22:35 +0000655 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000656 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%
cristybb503372010-05-27 20:51:26 +0000694% size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000695%
696*/
cristybb503372010-05-27 20:51:26 +0000697ModuleExport size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000698{
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%
cristy1e178e72011-08-28 19:44:34 +0000763% MagickBooleanType WritePCXImage(const ImageInfo *image_info,
764% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000765%
766% A description of each parameter follows.
767%
768% o image_info: the image info.
769%
770% o image: The image.
771%
cristy1e178e72011-08-28 19:44:34 +0000772% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000773%
774*/
cristy1e178e72011-08-28 19:44:34 +0000775
cristy3ed852e2009-09-05 21:47:34 +0000776static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
777 const unsigned char *pixels,Image *image)
778{
779 register const unsigned char
780 *q;
781
cristybb503372010-05-27 20:51:26 +0000782 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000783 i,
784 x;
785
786 ssize_t
787 count;
788
789 unsigned char
790 packet,
791 previous;
792
793 q=pixels;
cristybb503372010-05-27 20:51:26 +0000794 for (i=0; i < (ssize_t) pcx_info->planes; i++)
cristy3ed852e2009-09-05 21:47:34 +0000795 {
cristy1f9852b2010-09-04 15:05:36 +0000796 if (pcx_info->encoding == 0)
cristy3ed852e2009-09-05 21:47:34 +0000797 {
cristy1f9852b2010-09-04 15:05:36 +0000798 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
799 (void) WriteBlobByte(image,(unsigned char) (*q++));
cristy3ed852e2009-09-05 21:47:34 +0000800 }
cristy1f9852b2010-09-04 15:05:36 +0000801 else
802 {
803 previous=(*q++);
804 count=1;
805 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
806 {
807 packet=(*q++);
808 if ((packet == previous) && (count < 63))
809 {
810 count++;
811 continue;
812 }
813 if ((count > 1) || ((previous & 0xc0) == 0xc0))
814 {
815 count|=0xc0;
816 (void) WriteBlobByte(image,(unsigned char) count);
817 }
818 (void) WriteBlobByte(image,previous);
819 previous=packet;
820 count=1;
821 }
822 if ((count > 1) || ((previous & 0xc0) == 0xc0))
823 {
824 count|=0xc0;
825 (void) WriteBlobByte(image,(unsigned char) count);
826 }
827 (void) WriteBlobByte(image,previous);
828 }
cristy3ed852e2009-09-05 21:47:34 +0000829 }
830 return (MagickTrue);
831}
832
cristy1e178e72011-08-28 19:44:34 +0000833static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
834 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000835{
cristy3ed852e2009-09-05 21:47:34 +0000836 MagickBooleanType
837 status;
838
839 MagickOffsetType
840 offset,
841 *page_table,
842 scene;
843
844 PCXInfo
845 pcx_info;
846
cristy4c08aed2011-07-01 19:47:50 +0000847 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000848 *p;
849
cristybb503372010-05-27 20:51:26 +0000850 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000851 i,
852 x;
853
854 register unsigned char
855 *q;
856
857 size_t
858 length;
859
cristyaff6d802011-04-26 01:46:31 +0000860 ssize_t
861 y;
862
cristy3ed852e2009-09-05 21:47:34 +0000863 unsigned char
864 *pcx_colormap,
865 *pcx_pixels;
866
867 /*
868 Open output image file.
869 */
870 assert(image_info != (const ImageInfo *) NULL);
871 assert(image_info->signature == MagickSignature);
872 assert(image != (Image *) NULL);
873 assert(image->signature == MagickSignature);
874 if (image->debug != MagickFalse)
875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000876 assert(exception != (ExceptionInfo *) NULL);
877 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000878 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000879 if (status == MagickFalse)
880 return(status);
cristy3d9f5ba2012-06-26 13:37:31 +0000881 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000882 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000883 page_table=(MagickOffsetType *) NULL;
884 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
885 ((GetNextImageInList(image) != (Image *) NULL) &&
886 (image_info->adjoin != MagickFalse)))
887 {
888 /*
889 Write the DCX page table.
890 */
891 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
892 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
893 sizeof(*page_table));
894 if (page_table == (MagickOffsetType *) NULL)
895 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
896 for (scene=0; scene < 1024; scene++)
897 (void) WriteBlobLSBLong(image,0x00000000L);
898 }
899 scene=0;
900 do
901 {
902 if (page_table != (MagickOffsetType *) NULL)
903 page_table[scene]=TellBlob(image);
904 /*
905 Initialize PCX raster file header.
906 */
907 pcx_info.identifier=0x0a;
908 pcx_info.version=5;
cristy1f9852b2010-09-04 15:05:36 +0000909 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
cristy3ed852e2009-09-05 21:47:34 +0000910 pcx_info.bits_per_pixel=8;
911 if ((image->storage_class == PseudoClass) &&
cristy1e178e72011-08-28 19:44:34 +0000912 (IsImageMonochrome(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000913 pcx_info.bits_per_pixel=1;
914 pcx_info.left=0;
915 pcx_info.top=0;
916 pcx_info.right=(unsigned short) (image->columns-1);
917 pcx_info.bottom=(unsigned short) (image->rows-1);
918 switch (image->units)
919 {
920 case UndefinedResolution:
921 case PixelsPerInchResolution:
922 default:
923 {
cristy2a11bef2011-10-28 18:33:11 +0000924 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
925 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000926 break;
927 }
928 case PixelsPerCentimeterResolution:
929 {
930 pcx_info.horizontal_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000931 (2.54*image->resolution.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000932 pcx_info.vertical_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000933 (2.54*image->resolution.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000934 break;
935 }
936 }
937 pcx_info.reserved=0;
938 pcx_info.planes=1;
939 if ((image->storage_class == DirectClass) || (image->colors > 256))
940 {
941 pcx_info.planes=3;
cristy8a46d822012-08-28 23:32:39 +0000942 if (image->alpha_trait == BlendPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000943 pcx_info.planes++;
944 }
cristybb503372010-05-27 20:51:26 +0000945 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
cristy3ed852e2009-09-05 21:47:34 +0000946 pcx_info.bits_per_pixel+7)/8);
947 pcx_info.palette_info=1;
948 pcx_info.colormap_signature=0x0c;
949 /*
950 Write PCX header.
951 */
952 (void) WriteBlobByte(image,pcx_info.identifier);
953 (void) WriteBlobByte(image,pcx_info.version);
954 (void) WriteBlobByte(image,pcx_info.encoding);
955 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
956 (void) WriteBlobLSBShort(image,pcx_info.left);
957 (void) WriteBlobLSBShort(image,pcx_info.top);
958 (void) WriteBlobLSBShort(image,pcx_info.right);
959 (void) WriteBlobLSBShort(image,pcx_info.bottom);
960 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
961 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
962 /*
963 Dump colormap to file.
964 */
965 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
966 3*sizeof(*pcx_colormap));
967 if (pcx_colormap == (unsigned char *) NULL)
968 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
969 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
970 q=pcx_colormap;
971 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
cristybb503372010-05-27 20:51:26 +0000972 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000973 {
974 *q++=ScaleQuantumToChar(image->colormap[i].red);
975 *q++=ScaleQuantumToChar(image->colormap[i].green);
976 *q++=ScaleQuantumToChar(image->colormap[i].blue);
977 }
978 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
979 (void) WriteBlobByte(image,pcx_info.reserved);
980 (void) WriteBlobByte(image,pcx_info.planes);
981 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
982 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
983 for (i=0; i < 58; i++)
984 (void) WriteBlobByte(image,'\0');
985 length=(size_t) pcx_info.bytes_per_line;
cristy1f9852b2010-09-04 15:05:36 +0000986 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
987 sizeof(*pcx_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000988 if (pcx_pixels == (unsigned char *) NULL)
989 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
990 q=pcx_pixels;
991 if ((image->storage_class == DirectClass) || (image->colors > 256))
992 {
cristy4c08aed2011-07-01 19:47:50 +0000993 const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000994 *pixels;
995
996 /*
997 Convert DirectClass image to PCX raster pixels.
998 */
cristybb503372010-05-27 20:51:26 +0000999 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001000 {
cristy1e178e72011-08-28 19:44:34 +00001001 pixels=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001002 if (pixels == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001003 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 {
cristybb503372010-05-27 20:51:26 +00001012 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001013 {
cristy4c08aed2011-07-01 19:47:50 +00001014 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristyed231572011-07-14 02:18:59 +00001015 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001016 }
1017 break;
1018 }
1019 case 1:
1020 {
cristybb503372010-05-27 20:51:26 +00001021 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001022 {
cristy4c08aed2011-07-01 19:47:50 +00001023 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
cristyed231572011-07-14 02:18:59 +00001024 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001025 }
1026 break;
1027 }
1028 case 2:
1029 {
cristybb503372010-05-27 20:51:26 +00001030 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001031 {
cristy4c08aed2011-07-01 19:47:50 +00001032 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +00001033 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001034 }
1035 break;
1036 }
1037 case 3:
1038 default:
1039 {
cristybb503372010-05-27 20:51:26 +00001040 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001041 {
cristy4c08aed2011-07-01 19:47:50 +00001042 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00001043 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001044 }
1045 break;
1046 }
1047 }
1048 }
1049 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1050 break;
1051 if (image->previous == (Image *) NULL)
1052 {
cristycee97112010-05-28 00:44:52 +00001053 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1054 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001055 if (status == MagickFalse)
1056 break;
1057 }
1058 }
1059 }
1060 else
1061 {
1062 if (pcx_info.bits_per_pixel > 1)
cristybb503372010-05-27 20:51:26 +00001063 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001064 {
cristy1e178e72011-08-28 19:44:34 +00001065 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001066 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001067 break;
cristy3ed852e2009-09-05 21:47:34 +00001068 q=pcx_pixels;
cristybb503372010-05-27 20:51:26 +00001069 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001070 {
1071 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001072 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001073 }
cristy3ed852e2009-09-05 21:47:34 +00001074 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1075 break;
1076 if (image->previous == (Image *) NULL)
1077 {
cristycee97112010-05-28 00:44:52 +00001078 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1079 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001080 if (status == MagickFalse)
1081 break;
1082 }
1083 }
1084 else
1085 {
cristy4c08aed2011-07-01 19:47:50 +00001086 Quantum
cristy3ed852e2009-09-05 21:47:34 +00001087 polarity;
1088
1089 register unsigned char
1090 bit,
1091 byte;
1092
1093 /*
1094 Convert PseudoClass image to a PCX monochrome image.
1095 */
cristy101ab702011-10-13 13:06:32 +00001096 polarity=(Quantum) (GetPixelInfoIntensity(
cristy6e963d82012-06-19 15:23:24 +00001097 &image->colormap[0]) < (QuantumRange/2) ? 1 : 0);
cristy3ed852e2009-09-05 21:47:34 +00001098 if (image->colors == 2)
cristy101ab702011-10-13 13:06:32 +00001099 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) <
1100 GetPixelInfoIntensity(&image->colormap[1]) ? 1 : 0);
cristybb503372010-05-27 20:51:26 +00001101 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001102 {
cristy1e178e72011-08-28 19:44:34 +00001103 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001104 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001105 break;
cristy3ed852e2009-09-05 21:47:34 +00001106 bit=0;
1107 byte=0;
1108 q=pcx_pixels;
cristybb503372010-05-27 20:51:26 +00001109 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001110 {
1111 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +00001112 if (GetPixelIndex(image,p) == polarity)
cristy3ed852e2009-09-05 21:47:34 +00001113 byte|=0x01;
1114 bit++;
1115 if (bit == 8)
1116 {
1117 *q++=byte;
1118 bit=0;
1119 byte=0;
1120 }
cristyed231572011-07-14 02:18:59 +00001121 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001122 }
1123 if (bit != 0)
1124 *q++=byte << (8-bit);
1125 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1126 break;
1127 if (image->previous == (Image *) NULL)
1128 {
cristy1f9852b2010-09-04 15:05:36 +00001129 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1130 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001131 if (status == MagickFalse)
1132 break;
1133 }
1134 }
1135 }
1136 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1137 (void) WriteBlob(image,3*256,pcx_colormap);
1138 }
1139 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1140 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
cristyc044c482010-03-03 03:21:55 +00001141 if (page_table == (MagickOffsetType *) NULL)
1142 break;
1143 if (scene >= 1023)
1144 break;
cristy3ed852e2009-09-05 21:47:34 +00001145 if (GetNextImageInList(image) == (Image *) NULL)
1146 break;
1147 image=SyncNextImageInList(image);
1148 status=SetImageProgress(image,SaveImagesTag,scene++,
1149 GetImageListLength(image));
1150 if (status == MagickFalse)
1151 break;
cristy3ed852e2009-09-05 21:47:34 +00001152 } while (image_info->adjoin != MagickFalse);
1153 if (page_table != (MagickOffsetType *) NULL)
1154 {
1155 /*
1156 Write the DCX page table.
1157 */
1158 page_table[scene+1]=0;
1159 offset=SeekBlob(image,0L,SEEK_SET);
1160 if (offset < 0)
1161 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1162 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
cristybb503372010-05-27 20:51:26 +00001163 for (i=0; i <= (ssize_t) scene; i++)
cristy0b29b252010-05-30 01:59:46 +00001164 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
cristy3ed852e2009-09-05 21:47:34 +00001165 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1166 }
1167 if (status == MagickFalse)
1168 {
1169 char
1170 *message;
1171
1172 message=GetExceptionMessage(errno);
cristy1e178e72011-08-28 19:44:34 +00001173 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1174 "UnableToWriteFile","`%s': %s",image->filename,message);
cristy3ed852e2009-09-05 21:47:34 +00001175 message=DestroyString(message);
1176 }
1177 (void) CloseBlob(image);
1178 return(MagickTrue);
1179}