blob: 0b1e6496a4b7a152fc3aa2abe4ffd37bbd6dc861 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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%
128%
129*/
130static 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
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*/
165static 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
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
cristybb503372010-05-27 20:51:26 +0000203static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000204{
205 if (x < 0)
206 return(-x);
207 return(x);
208}
209
210static 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
217static 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
224static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
225{
226 Image
227 *image;
228
229 int
230 bits,
231 id,
232 mask;
233
cristy3ed852e2009-09-05 21:47:34 +0000234 MagickBooleanType
235 status;
236
237 MagickOffsetType
238 offset,
239 *page_table;
240
241 PCXInfo
242 pcx_info;
243
cristybb503372010-05-27 20:51:26 +0000244 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000245 x;
246
cristy4c08aed2011-07-01 19:47:50 +0000247 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000248 *q;
249
cristybb503372010-05-27 20:51:26 +0000250 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000251 i;
252
253 register unsigned char
254 *p,
255 *r;
256
cristyaff6d802011-04-26 01:46:31 +0000257 size_t
258 one,
259 pcx_packets;
260
cristy3ed852e2009-09-05 21:47:34 +0000261 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000262 count,
263 y;
cristy3ed852e2009-09-05 21:47:34 +0000264
265 unsigned char
266 packet,
267 *pcx_colormap,
268 *pcx_pixels,
269 *scanline;
270
cristy3ed852e2009-09-05 21:47:34 +0000271 /*
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);
cristy9950d572011-10-01 18:22:35 +0000281 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000282 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 {
cristybb503372010-05-27 20:51:26 +0000294 size_t
cristy3ed852e2009-09-05 21:47:34 +0000295 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 */
cristybb503372010-05-27 20:51:26 +0000341 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
cristy3ed852e2009-09-05 21:47:34 +0000342 pcx_info.left)+1UL;
cristybb503372010-05-27 20:51:26 +0000343 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
cristy3ed852e2009-09-05 21:47:34 +0000344 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;
cristy2a11bef2011-10-28 18:33:11 +0000350 image->resolution.x=(double) pcx_info.horizontal_resolution;
351 image->resolution.y=(double) pcx_info.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +0000352 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);
cristyeaedf062010-05-29 22:36:02 +0000360 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000361 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))
cristyeaedf062010-05-29 22:36:02 +0000364 image->colors=(size_t) MagickMin(one << (1UL*
cristy3ed852e2009-09-05 21:47:34 +0000365 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
cristy018f07f2011-09-04 21:15:19 +0000366 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000367 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
368 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
369 image->storage_class=DirectClass;
370 p=pcx_colormap;
cristybb503372010-05-27 20:51:26 +0000371 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000372 {
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 */
cristybb503372010-05-27 20:51:26 +0000387 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
cristy3ed852e2009-09-05 21:47:34 +0000388 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)
cristyb0a657e2012-08-29 00:45:37 +0000434 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
435 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000436 else
437 if ((pcx_info.version == 5) ||
438 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
439 {
440 /*
441 Initialize image colormap.
442 */
443 if (image->colors > 256)
444 ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors");
445 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
446 {
447 /*
448 Monochrome colormap.
449 */
450 image->colormap[0].red=(Quantum) 0;
451 image->colormap[0].green=(Quantum) 0;
452 image->colormap[0].blue=(Quantum) 0;
cristy6e963d82012-06-19 15:23:24 +0000453 image->colormap[1].red=QuantumRange;
454 image->colormap[1].green=QuantumRange;
455 image->colormap[1].blue=QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +0000456 }
457 else
458 if (image->colors > 16)
459 {
460 /*
461 256 color images have their color map at the end of the file.
462 */
463 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
464 count=ReadBlob(image,3*image->colors,pcx_colormap);
465 p=pcx_colormap;
cristybb503372010-05-27 20:51:26 +0000466 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000467 {
468 image->colormap[i].red=ScaleCharToQuantum(*p++);
469 image->colormap[i].green=ScaleCharToQuantum(*p++);
470 image->colormap[i].blue=ScaleCharToQuantum(*p++);
471 }
472 }
473 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
474 }
475 /*
476 Convert PCX raster image to pixel packets.
477 */
cristybb503372010-05-27 20:51:26 +0000478 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000479 {
480 p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
481 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000482 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000483 break;
cristy3ed852e2009-09-05 21:47:34 +0000484 r=scanline;
485 if (image->storage_class == DirectClass)
486 for (i=0; i < pcx_info.planes; i++)
487 {
488 r=scanline+i;
cristybb503372010-05-27 20:51:26 +0000489 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000490 {
491 switch (i)
492 {
493 case 0:
494 {
495 *r=(*p++);
496 break;
497 }
498 case 1:
499 {
500 *r=(*p++);
501 break;
502 }
503 case 2:
504 {
505 *r=(*p++);
506 break;
507 }
508 case 3:
509 default:
510 {
511 *r=(*p++);
512 break;
513 }
514 }
515 r+=pcx_info.planes;
516 }
517 }
518 else
519 if (pcx_info.planes > 1)
520 {
cristybb503372010-05-27 20:51:26 +0000521 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000522 *r++=0;
523 for (i=0; i < pcx_info.planes; i++)
524 {
525 r=scanline;
cristybb503372010-05-27 20:51:26 +0000526 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000527 {
528 bits=(*p++);
529 for (mask=0x80; mask != 0; mask>>=1)
530 {
531 if (bits & mask)
532 *r|=1 << i;
533 r++;
534 }
535 }
536 }
537 }
538 else
539 switch (pcx_info.bits_per_pixel)
540 {
541 case 1:
542 {
cristybb503372010-05-27 20:51:26 +0000543 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000544 bit;
545
cristybb503372010-05-27 20:51:26 +0000546 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000547 {
548 for (bit=7; bit >= 0; bit--)
549 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
550 p++;
551 }
552 if ((image->columns % 8) != 0)
553 {
cristybb503372010-05-27 20:51:26 +0000554 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
cristy3ed852e2009-09-05 21:47:34 +0000555 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
556 p++;
557 }
558 break;
559 }
560 case 2:
561 {
cristybb503372010-05-27 20:51:26 +0000562 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
cristy3ed852e2009-09-05 21:47:34 +0000563 {
564 *r++=(*p >> 6) & 0x3;
565 *r++=(*p >> 4) & 0x3;
566 *r++=(*p >> 2) & 0x3;
567 *r++=(*p) & 0x3;
568 p++;
569 }
570 if ((image->columns % 4) != 0)
571 {
cristybb503372010-05-27 20:51:26 +0000572 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
cristy3ed852e2009-09-05 21:47:34 +0000573 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
574 p++;
575 }
576 break;
577 }
578 case 4:
579 {
cristybb503372010-05-27 20:51:26 +0000580 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000581 {
582 *r++=(*p >> 4) & 0xf;
583 *r++=(*p) & 0xf;
584 p++;
585 }
586 if ((image->columns % 2) != 0)
587 *r++=(*p++ >> 4) & 0xf;
588 break;
589 }
590 case 8:
591 {
592 (void) CopyMagickMemory(r,p,image->columns);
593 break;
594 }
595 default:
596 break;
597 }
598 /*
599 Transfer image scanline.
600 */
601 r=scanline;
cristybb503372010-05-27 20:51:26 +0000602 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000603 {
604 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000605 SetPixelIndex(image,*r++,q);
cristy3ed852e2009-09-05 21:47:34 +0000606 else
607 {
cristy4c08aed2011-07-01 19:47:50 +0000608 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
609 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
610 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
cristy8a46d822012-08-28 23:32:39 +0000611 if (image->alpha_trait == BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000612 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
cristy3ed852e2009-09-05 21:47:34 +0000613 }
cristyed231572011-07-14 02:18:59 +0000614 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000615 }
616 if (SyncAuthenticPixels(image,exception) == MagickFalse)
617 break;
618 if (image->previous == (Image *) NULL)
619 {
cristycee97112010-05-28 00:44:52 +0000620 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000621 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000622 if (status == MagickFalse)
623 break;
624 }
625 }
626 if (image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +0000627 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000628 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
629 if (pcx_colormap != (unsigned char *) NULL)
630 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
631 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
632 if (EOFBlob(image) != MagickFalse)
633 {
634 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
635 image->filename);
636 break;
637 }
638 /*
639 Proceed to next image.
640 */
641 if (image_info->number_scenes != 0)
642 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
643 break;
644 if (page_table == (MagickOffsetType *) NULL)
645 break;
646 if (page_table[id] == 0)
647 break;
648 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
649 if (offset < 0)
650 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
651 count=ReadBlob(image,1,&pcx_info.identifier);
652 if ((count != 0) && (pcx_info.identifier == 0x0a))
653 {
654 /*
655 Allocate next image structure.
656 */
cristy9950d572011-10-01 18:22:35 +0000657 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000658 if (GetNextImageInList(image) == (Image *) NULL)
659 {
660 image=DestroyImageList(image);
661 return((Image *) NULL);
662 }
663 image=SyncNextImageInList(image);
664 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
665 GetBlobSize(image));
666 if (status == MagickFalse)
667 break;
668 }
669 }
670 if (page_table != (MagickOffsetType *) NULL)
671 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
672 (void) CloseBlob(image);
673 return(GetFirstImageInList(image));
674}
675
676/*
677%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678% %
679% %
680% %
681% R e g i s t e r P C X I m a g e %
682% %
683% %
684% %
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686%
687% RegisterPCXImage() adds attributes for the PCX image format to
688% the list of supported formats. The attributes include the image format
689% tag, a method to read and/or write the format, whether the format
690% supports the saving of more than one frame to the same file or blob,
691% whether the format supports native in-memory I/O, and a brief
692% description of the format.
693%
694% The format of the RegisterPCXImage method is:
695%
cristybb503372010-05-27 20:51:26 +0000696% size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000697%
698*/
cristybb503372010-05-27 20:51:26 +0000699ModuleExport size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000700{
701 MagickInfo
702 *entry;
703
704 entry=SetMagickInfo("DCX");
705 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
706 entry->encoder=(EncodeImageHandler *) WritePCXImage;
707 entry->seekable_stream=MagickTrue;
708 entry->magick=(IsImageFormatHandler *) IsDCX;
709 entry->description=ConstantString("ZSoft IBM PC multi-page Paintbrush");
710 entry->module=ConstantString("PCX");
711 (void) RegisterMagickInfo(entry);
712 entry=SetMagickInfo("PCX");
713 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
714 entry->encoder=(EncodeImageHandler *) WritePCXImage;
715 entry->magick=(IsImageFormatHandler *) IsPCX;
716 entry->adjoin=MagickFalse;
717 entry->seekable_stream=MagickTrue;
718 entry->description=ConstantString("ZSoft IBM PC Paintbrush");
719 entry->module=ConstantString("PCX");
720 (void) RegisterMagickInfo(entry);
721 return(MagickImageCoderSignature);
722}
723
724/*
725%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726% %
727% %
728% %
729% U n r e g i s t e r P C X I m a g e %
730% %
731% %
732% %
733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734%
735% UnregisterPCXImage() removes format registrations made by the
736% PCX module from the list of supported formats.
737%
738% The format of the UnregisterPCXImage method is:
739%
740% UnregisterPCXImage(void)
741%
742*/
743ModuleExport void UnregisterPCXImage(void)
744{
745 (void) UnregisterMagickInfo("DCX");
746 (void) UnregisterMagickInfo("PCX");
747}
748
749/*
750%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751% %
752% %
753% %
754% W r i t e P C X I m a g e %
755% %
756% %
757% %
758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759%
760% WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
761% format.
762%
763% The format of the WritePCXImage method is:
764%
cristy1e178e72011-08-28 19:44:34 +0000765% MagickBooleanType WritePCXImage(const ImageInfo *image_info,
766% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000767%
768% A description of each parameter follows.
769%
770% o image_info: the image info.
771%
772% o image: The image.
773%
cristy1e178e72011-08-28 19:44:34 +0000774% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000775%
776*/
cristy1e178e72011-08-28 19:44:34 +0000777
cristy3ed852e2009-09-05 21:47:34 +0000778static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
779 const unsigned char *pixels,Image *image)
780{
781 register const unsigned char
782 *q;
783
cristybb503372010-05-27 20:51:26 +0000784 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000785 i,
786 x;
787
788 ssize_t
789 count;
790
791 unsigned char
792 packet,
793 previous;
794
795 q=pixels;
cristybb503372010-05-27 20:51:26 +0000796 for (i=0; i < (ssize_t) pcx_info->planes; i++)
cristy3ed852e2009-09-05 21:47:34 +0000797 {
cristy1f9852b2010-09-04 15:05:36 +0000798 if (pcx_info->encoding == 0)
cristy3ed852e2009-09-05 21:47:34 +0000799 {
cristy1f9852b2010-09-04 15:05:36 +0000800 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
801 (void) WriteBlobByte(image,(unsigned char) (*q++));
cristy3ed852e2009-09-05 21:47:34 +0000802 }
cristy1f9852b2010-09-04 15:05:36 +0000803 else
804 {
805 previous=(*q++);
806 count=1;
807 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
808 {
809 packet=(*q++);
810 if ((packet == previous) && (count < 63))
811 {
812 count++;
813 continue;
814 }
815 if ((count > 1) || ((previous & 0xc0) == 0xc0))
816 {
817 count|=0xc0;
818 (void) WriteBlobByte(image,(unsigned char) count);
819 }
820 (void) WriteBlobByte(image,previous);
821 previous=packet;
822 count=1;
823 }
824 if ((count > 1) || ((previous & 0xc0) == 0xc0))
825 {
826 count|=0xc0;
827 (void) WriteBlobByte(image,(unsigned char) count);
828 }
829 (void) WriteBlobByte(image,previous);
830 }
cristy3ed852e2009-09-05 21:47:34 +0000831 }
832 return (MagickTrue);
833}
834
cristy1e178e72011-08-28 19:44:34 +0000835static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
836 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000837{
cristy3ed852e2009-09-05 21:47:34 +0000838 MagickBooleanType
839 status;
840
841 MagickOffsetType
842 offset,
843 *page_table,
844 scene;
845
846 PCXInfo
847 pcx_info;
848
cristy4c08aed2011-07-01 19:47:50 +0000849 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000850 *p;
851
cristybb503372010-05-27 20:51:26 +0000852 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000853 i,
854 x;
855
856 register unsigned char
857 *q;
858
859 size_t
860 length;
861
cristyaff6d802011-04-26 01:46:31 +0000862 ssize_t
863 y;
864
cristy3ed852e2009-09-05 21:47:34 +0000865 unsigned char
866 *pcx_colormap,
867 *pcx_pixels;
868
869 /*
870 Open output image file.
871 */
872 assert(image_info != (const ImageInfo *) NULL);
873 assert(image_info->signature == MagickSignature);
874 assert(image != (Image *) NULL);
875 assert(image->signature == MagickSignature);
876 if (image->debug != MagickFalse)
877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000878 assert(exception != (ExceptionInfo *) NULL);
879 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000880 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000881 if (status == MagickFalse)
882 return(status);
cristy3d9f5ba2012-06-26 13:37:31 +0000883 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000884 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000885 page_table=(MagickOffsetType *) NULL;
886 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
887 ((GetNextImageInList(image) != (Image *) NULL) &&
888 (image_info->adjoin != MagickFalse)))
889 {
890 /*
891 Write the DCX page table.
892 */
893 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
894 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
895 sizeof(*page_table));
896 if (page_table == (MagickOffsetType *) NULL)
897 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
898 for (scene=0; scene < 1024; scene++)
899 (void) WriteBlobLSBLong(image,0x00000000L);
900 }
901 scene=0;
902 do
903 {
904 if (page_table != (MagickOffsetType *) NULL)
905 page_table[scene]=TellBlob(image);
906 /*
907 Initialize PCX raster file header.
908 */
909 pcx_info.identifier=0x0a;
910 pcx_info.version=5;
cristy1f9852b2010-09-04 15:05:36 +0000911 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
cristy3ed852e2009-09-05 21:47:34 +0000912 pcx_info.bits_per_pixel=8;
913 if ((image->storage_class == PseudoClass) &&
cristy1e178e72011-08-28 19:44:34 +0000914 (IsImageMonochrome(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000915 pcx_info.bits_per_pixel=1;
916 pcx_info.left=0;
917 pcx_info.top=0;
918 pcx_info.right=(unsigned short) (image->columns-1);
919 pcx_info.bottom=(unsigned short) (image->rows-1);
920 switch (image->units)
921 {
922 case UndefinedResolution:
923 case PixelsPerInchResolution:
924 default:
925 {
cristy2a11bef2011-10-28 18:33:11 +0000926 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
927 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000928 break;
929 }
930 case PixelsPerCentimeterResolution:
931 {
932 pcx_info.horizontal_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000933 (2.54*image->resolution.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000934 pcx_info.vertical_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000935 (2.54*image->resolution.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000936 break;
937 }
938 }
939 pcx_info.reserved=0;
940 pcx_info.planes=1;
941 if ((image->storage_class == DirectClass) || (image->colors > 256))
942 {
943 pcx_info.planes=3;
cristy8a46d822012-08-28 23:32:39 +0000944 if (image->alpha_trait == BlendPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000945 pcx_info.planes++;
946 }
cristybb503372010-05-27 20:51:26 +0000947 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
cristy3ed852e2009-09-05 21:47:34 +0000948 pcx_info.bits_per_pixel+7)/8);
949 pcx_info.palette_info=1;
950 pcx_info.colormap_signature=0x0c;
951 /*
952 Write PCX header.
953 */
954 (void) WriteBlobByte(image,pcx_info.identifier);
955 (void) WriteBlobByte(image,pcx_info.version);
956 (void) WriteBlobByte(image,pcx_info.encoding);
957 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
958 (void) WriteBlobLSBShort(image,pcx_info.left);
959 (void) WriteBlobLSBShort(image,pcx_info.top);
960 (void) WriteBlobLSBShort(image,pcx_info.right);
961 (void) WriteBlobLSBShort(image,pcx_info.bottom);
962 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
963 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
964 /*
965 Dump colormap to file.
966 */
967 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
968 3*sizeof(*pcx_colormap));
969 if (pcx_colormap == (unsigned char *) NULL)
970 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
971 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
972 q=pcx_colormap;
973 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
cristybb503372010-05-27 20:51:26 +0000974 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000975 {
976 *q++=ScaleQuantumToChar(image->colormap[i].red);
977 *q++=ScaleQuantumToChar(image->colormap[i].green);
978 *q++=ScaleQuantumToChar(image->colormap[i].blue);
979 }
980 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
981 (void) WriteBlobByte(image,pcx_info.reserved);
982 (void) WriteBlobByte(image,pcx_info.planes);
983 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
984 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
985 for (i=0; i < 58; i++)
986 (void) WriteBlobByte(image,'\0');
987 length=(size_t) pcx_info.bytes_per_line;
cristy1f9852b2010-09-04 15:05:36 +0000988 pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
989 sizeof(*pcx_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000990 if (pcx_pixels == (unsigned char *) NULL)
991 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
992 q=pcx_pixels;
993 if ((image->storage_class == DirectClass) || (image->colors > 256))
994 {
cristy4c08aed2011-07-01 19:47:50 +0000995 const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000996 *pixels;
997
998 /*
999 Convert DirectClass image to PCX raster pixels.
1000 */
cristybb503372010-05-27 20:51:26 +00001001 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001002 {
cristy1e178e72011-08-28 19:44:34 +00001003 pixels=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001004 if (pixels == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001005 break;
1006 q=pcx_pixels;
1007 for (i=0; i < pcx_info.planes; i++)
1008 {
1009 p=pixels;
1010 switch ((int) i)
1011 {
1012 case 0:
1013 {
cristybb503372010-05-27 20:51:26 +00001014 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001015 {
cristy4c08aed2011-07-01 19:47:50 +00001016 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristyed231572011-07-14 02:18:59 +00001017 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001018 }
1019 break;
1020 }
1021 case 1:
1022 {
cristybb503372010-05-27 20:51:26 +00001023 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001024 {
cristy4c08aed2011-07-01 19:47:50 +00001025 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
cristyed231572011-07-14 02:18:59 +00001026 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001027 }
1028 break;
1029 }
1030 case 2:
1031 {
cristybb503372010-05-27 20:51:26 +00001032 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001033 {
cristy4c08aed2011-07-01 19:47:50 +00001034 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +00001035 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001036 }
1037 break;
1038 }
1039 case 3:
1040 default:
1041 {
cristybb503372010-05-27 20:51:26 +00001042 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001043 {
cristy4c08aed2011-07-01 19:47:50 +00001044 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00001045 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001046 }
1047 break;
1048 }
1049 }
1050 }
1051 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1052 break;
1053 if (image->previous == (Image *) NULL)
1054 {
cristycee97112010-05-28 00:44:52 +00001055 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1056 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001057 if (status == MagickFalse)
1058 break;
1059 }
1060 }
1061 }
1062 else
1063 {
1064 if (pcx_info.bits_per_pixel > 1)
cristybb503372010-05-27 20:51:26 +00001065 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001066 {
cristy1e178e72011-08-28 19:44:34 +00001067 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001068 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001069 break;
cristy3ed852e2009-09-05 21:47:34 +00001070 q=pcx_pixels;
cristybb503372010-05-27 20:51:26 +00001071 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001072 {
1073 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001074 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001075 }
cristy3ed852e2009-09-05 21:47:34 +00001076 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1077 break;
1078 if (image->previous == (Image *) NULL)
1079 {
cristycee97112010-05-28 00:44:52 +00001080 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1081 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001082 if (status == MagickFalse)
1083 break;
1084 }
1085 }
1086 else
1087 {
cristy4c08aed2011-07-01 19:47:50 +00001088 Quantum
cristy3ed852e2009-09-05 21:47:34 +00001089 polarity;
1090
1091 register unsigned char
1092 bit,
1093 byte;
1094
1095 /*
1096 Convert PseudoClass image to a PCX monochrome image.
1097 */
cristy101ab702011-10-13 13:06:32 +00001098 polarity=(Quantum) (GetPixelInfoIntensity(
cristy6e963d82012-06-19 15:23:24 +00001099 &image->colormap[0]) < (QuantumRange/2) ? 1 : 0);
cristy3ed852e2009-09-05 21:47:34 +00001100 if (image->colors == 2)
cristy101ab702011-10-13 13:06:32 +00001101 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) <
1102 GetPixelInfoIntensity(&image->colormap[1]) ? 1 : 0);
cristybb503372010-05-27 20:51:26 +00001103 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001104 {
cristy1e178e72011-08-28 19:44:34 +00001105 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001106 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001107 break;
cristy3ed852e2009-09-05 21:47:34 +00001108 bit=0;
1109 byte=0;
1110 q=pcx_pixels;
cristybb503372010-05-27 20:51:26 +00001111 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001112 {
1113 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +00001114 if (GetPixelIndex(image,p) == polarity)
cristy3ed852e2009-09-05 21:47:34 +00001115 byte|=0x01;
1116 bit++;
1117 if (bit == 8)
1118 {
1119 *q++=byte;
1120 bit=0;
1121 byte=0;
1122 }
cristyed231572011-07-14 02:18:59 +00001123 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001124 }
1125 if (bit != 0)
1126 *q++=byte << (8-bit);
1127 if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
1128 break;
1129 if (image->previous == (Image *) NULL)
1130 {
cristy1f9852b2010-09-04 15:05:36 +00001131 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1132 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001133 if (status == MagickFalse)
1134 break;
1135 }
1136 }
1137 }
1138 (void) WriteBlobByte(image,pcx_info.colormap_signature);
1139 (void) WriteBlob(image,3*256,pcx_colormap);
1140 }
1141 pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
1142 pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
cristyc044c482010-03-03 03:21:55 +00001143 if (page_table == (MagickOffsetType *) NULL)
1144 break;
1145 if (scene >= 1023)
1146 break;
cristy3ed852e2009-09-05 21:47:34 +00001147 if (GetNextImageInList(image) == (Image *) NULL)
1148 break;
1149 image=SyncNextImageInList(image);
1150 status=SetImageProgress(image,SaveImagesTag,scene++,
1151 GetImageListLength(image));
1152 if (status == MagickFalse)
1153 break;
cristy3ed852e2009-09-05 21:47:34 +00001154 } while (image_info->adjoin != MagickFalse);
1155 if (page_table != (MagickOffsetType *) NULL)
1156 {
1157 /*
1158 Write the DCX page table.
1159 */
1160 page_table[scene+1]=0;
1161 offset=SeekBlob(image,0L,SEEK_SET);
1162 if (offset < 0)
1163 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1164 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
cristybb503372010-05-27 20:51:26 +00001165 for (i=0; i <= (ssize_t) scene; i++)
cristy0b29b252010-05-30 01:59:46 +00001166 (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
cristy3ed852e2009-09-05 21:47:34 +00001167 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
1168 }
1169 if (status == MagickFalse)
1170 {
1171 char
1172 *message;
1173
1174 message=GetExceptionMessage(errno);
cristy1e178e72011-08-28 19:44:34 +00001175 (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
1176 "UnableToWriteFile","`%s': %s",image->filename,message);
cristy3ed852e2009-09-05 21:47:34 +00001177 message=DestroyString(message);
1178 }
1179 (void) CloseBlob(image);
1180 return(MagickTrue);
1181}