blob: d8c920422e89c8c2b1675abf3528174df341acc2 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristyf6ff9ea2016-12-05 09:53:35 -050020% Copyright 1999-2017 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% %
Cristyf19d4142017-04-24 11:34:30 -040026% https://www.imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
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"
Cristy8909c0f2016-05-29 16:15:13 -040059#include "MagickCore/memory-private.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/monitor.h"
61#include "MagickCore/monitor-private.h"
62#include "MagickCore/pixel-accessor.h"
63#include "MagickCore/quantum-private.h"
64#include "MagickCore/static.h"
65#include "MagickCore/string_.h"
66#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000067
68/*
69 Typedef declarations.
70*/
71typedef struct _PCXInfo
72{
73 unsigned char
74 identifier,
75 version,
76 encoding,
77 bits_per_pixel;
78
79 unsigned short
80 left,
81 top,
82 right,
83 bottom,
84 horizontal_resolution,
85 vertical_resolution;
86
87 unsigned char
88 reserved,
89 planes;
90
91 unsigned short
92 bytes_per_line,
cristy29d40332014-12-30 23:48:22 +000093 palette_info,
94 horizontal_screensize,
95 vertical_screensize;
cristy3ed852e2009-09-05 21:47:34 +000096
97 unsigned char
98 colormap_signature;
99} PCXInfo;
100
101/*
102 Forward declarations.
103*/
104static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000105 WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% I s D C X %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% IsDCX() returns MagickTrue if the image format type, identified by the
119% magick string, is DCX.
120%
121% The format of the IsDCX method is:
122%
123% MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
124%
125% A description of each parameter follows:
126%
127% o magick: compare image format pattern against these bytes.
128%
129% o length: Specifies the length of the magick string.
130%
cristy3ed852e2009-09-05 21:47:34 +0000131*/
132static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length)
133{
134 if (length < 4)
135 return(MagickFalse);
136 if (memcmp(magick,"\261\150\336\72",4) == 0)
137 return(MagickTrue);
138 return(MagickFalse);
139}
140
141/*
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143% %
144% %
145% %
146% I s P C X %
147% %
148% %
149% %
150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151%
152% IsPCX() returns MagickTrue if the image format type, identified by the
153% magick string, is PCX.
154%
155% The format of the IsPCX method is:
156%
157% MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
158%
159% A description of each parameter follows:
160%
161% o magick: compare image format pattern against these bytes.
162%
163% o length: Specifies the length of the magick string.
164%
cristy3ed852e2009-09-05 21:47:34 +0000165*/
166static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
167{
168 if (length < 2)
169 return(MagickFalse);
170 if (memcmp(magick,"\012\002",2) == 0)
171 return(MagickTrue);
172 if (memcmp(magick,"\012\005",2) == 0)
173 return(MagickTrue);
174 return(MagickFalse);
175}
176
177/*
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179% %
180% %
181% %
182% R e a d P C X I m a g e %
183% %
184% %
185% %
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187%
188% ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it.
189% It allocates the memory necessary for the new Image structure and returns
190% a pointer to the new image.
191%
192% The format of the ReadPCXImage method is:
193%
194% Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
195%
196% A description of each parameter follows:
197%
198% o image_info: the image info.
199%
200% o exception: return any errors or warnings in this structure.
201%
202*/
cristy3ed852e2009-09-05 21:47:34 +0000203static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
204{
dirk08f033c2014-10-23 21:53:50 +0000205#define ThrowPCXException(severity,tag) \
206 { \
207 scanline=(unsigned char *) RelinquishMagickMemory(scanline); \
208 pixel_info=RelinquishVirtualMemory(pixel_info); \
209 ThrowReaderException(severity,tag); \
210 }
211
cristy3ed852e2009-09-05 21:47:34 +0000212 Image
213 *image;
214
215 int
216 bits,
217 id,
218 mask;
219
cristy3ed852e2009-09-05 21:47:34 +0000220 MagickBooleanType
221 status;
222
223 MagickOffsetType
224 offset,
225 *page_table;
226
cristy0acf7e12013-06-30 23:35:36 +0000227 MemoryInfo
228 *pixel_info;
229
cristy3ed852e2009-09-05 21:47:34 +0000230 PCXInfo
231 pcx_info;
232
cristybb503372010-05-27 20:51:26 +0000233 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000234 x;
235
cristy4c08aed2011-07-01 19:47:50 +0000236 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000237 *q;
238
cristybb503372010-05-27 20:51:26 +0000239 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000240 i;
241
242 register unsigned char
243 *p,
244 *r;
245
cristyaff6d802011-04-26 01:46:31 +0000246 size_t
247 one,
248 pcx_packets;
249
cristy3ed852e2009-09-05 21:47:34 +0000250 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000251 count,
252 y;
cristy3ed852e2009-09-05 21:47:34 +0000253
254 unsigned char
255 packet,
dirk08f033c2014-10-23 21:53:50 +0000256 pcx_colormap[768],
cristy0553bd52013-06-30 15:53:50 +0000257 *pixels,
cristy3ed852e2009-09-05 21:47:34 +0000258 *scanline;
259
cristy3ed852e2009-09-05 21:47:34 +0000260 /*
261 Open image file.
262 */
263 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000264 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000265 if (image_info->debug != MagickFalse)
266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
267 image_info->filename);
268 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000269 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000270 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000271 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
272 if (status == MagickFalse)
273 {
274 image=DestroyImageList(image);
275 return((Image *) NULL);
276 }
277 /*
278 Determine if this a PCX file.
279 */
280 page_table=(MagickOffsetType *) NULL;
281 if (LocaleCompare(image_info->magick,"DCX") == 0)
282 {
cristybb503372010-05-27 20:51:26 +0000283 size_t
cristy3ed852e2009-09-05 21:47:34 +0000284 magic;
285
286 /*
287 Read the DCX page table.
288 */
289 magic=ReadBlobLSBLong(image);
290 if (magic != 987654321)
291 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
292 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
293 sizeof(*page_table));
294 if (page_table == (MagickOffsetType *) NULL)
295 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
296 for (id=0; id < 1024; id++)
297 {
298 page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image);
299 if (page_table[id] == 0)
300 break;
301 }
302 }
303 if (page_table != (MagickOffsetType *) NULL)
304 {
305 offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET);
306 if (offset < 0)
307 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
308 }
cristy3ed852e2009-09-05 21:47:34 +0000309 count=ReadBlob(image,1,&pcx_info.identifier);
310 for (id=1; id < 1024; id++)
311 {
cristyc99e4c22014-04-30 23:37:58 +0000312 int
313 bits_per_pixel;
314
cristy3ed852e2009-09-05 21:47:34 +0000315 /*
316 Verify PCX identifier.
317 */
318 pcx_info.version=(unsigned char) ReadBlobByte(image);
cristy29d40332014-12-30 23:48:22 +0000319 if ((count != 1) || (pcx_info.identifier != 0x0a))
cristy3ed852e2009-09-05 21:47:34 +0000320 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
321 pcx_info.encoding=(unsigned char) ReadBlobByte(image);
cristyc99e4c22014-04-30 23:37:58 +0000322 bits_per_pixel=ReadBlobByte(image);
323 if (bits_per_pixel == -1)
324 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
325 pcx_info.bits_per_pixel=(unsigned char) bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000326 pcx_info.left=ReadBlobLSBShort(image);
327 pcx_info.top=ReadBlobLSBShort(image);
328 pcx_info.right=ReadBlobLSBShort(image);
329 pcx_info.bottom=ReadBlobLSBShort(image);
330 pcx_info.horizontal_resolution=ReadBlobLSBShort(image);
331 pcx_info.vertical_resolution=ReadBlobLSBShort(image);
332 /*
333 Read PCX raster colormap.
334 */
cristybb503372010-05-27 20:51:26 +0000335 image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
cristy3ed852e2009-09-05 21:47:34 +0000336 pcx_info.left)+1UL;
cristybb503372010-05-27 20:51:26 +0000337 image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
cristy3ed852e2009-09-05 21:47:34 +0000338 pcx_info.top)+1UL;
339 if ((image->columns == 0) || (image->rows == 0) ||
Cristyf41bcd12017-03-28 05:59:57 -0400340 ((pcx_info.bits_per_pixel != 1) && (pcx_info.bits_per_pixel != 2) &&
341 (pcx_info.bits_per_pixel != 4) && (pcx_info.bits_per_pixel != 8)))
cristy3ed852e2009-09-05 21:47:34 +0000342 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy29d40332014-12-30 23:48:22 +0000343 image->depth=pcx_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000344 image->units=PixelsPerInchResolution;
cristy2a11bef2011-10-28 18:33:11 +0000345 image->resolution.x=(double) pcx_info.horizontal_resolution;
346 image->resolution.y=(double) pcx_info.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +0000347 image->colors=16;
cristy3ed852e2009-09-05 21:47:34 +0000348 count=ReadBlob(image,3*image->colors,pcx_colormap);
cristy7c443542015-01-05 23:19:48 +0000349 if (count != (ssize_t) (3*image->colors))
cristy29d40332014-12-30 23:48:22 +0000350 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000351 pcx_info.reserved=(unsigned char) ReadBlobByte(image);
352 pcx_info.planes=(unsigned char) ReadBlobByte(image);
Cristyf41bcd12017-03-28 05:59:57 -0400353 if (pcx_info.planes == 0)
354 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy7163b3e2014-05-24 22:18:20 +0000355 if ((pcx_info.bits_per_pixel*pcx_info.planes) >= 64)
356 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristyeaedf062010-05-29 22:36:02 +0000357 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000358 if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
359 if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
360 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
cristyeaedf062010-05-29 22:36:02 +0000361 image->colors=(size_t) MagickMin(one << (1UL*
cristy3ed852e2009-09-05 21:47:34 +0000362 (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
cristy018f07f2011-09-04 21:15:19 +0000363 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000364 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
365 if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
366 image->storage_class=DirectClass;
367 p=pcx_colormap;
cristybb503372010-05-27 20:51:26 +0000368 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000369 {
370 image->colormap[i].red=ScaleCharToQuantum(*p++);
371 image->colormap[i].green=ScaleCharToQuantum(*p++);
372 image->colormap[i].blue=ScaleCharToQuantum(*p++);
373 }
374 pcx_info.bytes_per_line=ReadBlobLSBShort(image);
375 pcx_info.palette_info=ReadBlobLSBShort(image);
cristy29d40332014-12-30 23:48:22 +0000376 pcx_info.horizontal_screensize=ReadBlobLSBShort(image);
377 pcx_info.vertical_screensize=ReadBlobLSBShort(image);
378 for (i=0; i < 54; i++)
cristy3ed852e2009-09-05 21:47:34 +0000379 (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;
cristyacabb842014-12-14 23:36:33 +0000383 status=SetImageExtent(image,image->columns,image->rows,exception);
384 if (status == MagickFalse)
385 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000386 /*
387 Read image data.
388 */
dirk11358a52016-05-29 21:59:45 +0200389 if (HeapOverflowSanityCheck(image->rows, (size_t) pcx_info.bytes_per_line) != MagickFalse)
390 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
391 pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line;
Cristyf41bcd12017-03-28 05:59:57 -0400392 if (HeapOverflowSanityCheck(pcx_packets, (size_t) pcx_info.planes) != MagickFalse)
dirk11358a52016-05-29 21:59:45 +0200393 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
394 pcx_packets=(size_t) pcx_packets*pcx_info.planes;
dirk08f033c2014-10-23 21:53:50 +0000395 if ((size_t) (pcx_info.bits_per_pixel*pcx_info.planes*image->columns) >
396 (pcx_packets*8U))
397 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000398 scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns,
399 pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline));
cristyf432c632014-12-07 15:11:28 +0000400 pixel_info=AcquireVirtualMemory(pcx_packets,2*sizeof(*pixels));
cristy0acf7e12013-06-30 23:35:36 +0000401 if ((scanline == (unsigned char *) NULL) ||
402 (pixel_info == (MemoryInfo *) NULL))
403 {
404 if (scanline != (unsigned char *) NULL)
405 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
406 if (pixel_info != (MemoryInfo *) NULL)
407 pixel_info=RelinquishVirtualMemory(pixel_info);
408 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
409 }
410 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000411 /*
412 Uncompress image data.
413 */
cristy0553bd52013-06-30 15:53:50 +0000414 p=pixels;
cristy3ed852e2009-09-05 21:47:34 +0000415 if (pcx_info.encoding == 0)
416 while (pcx_packets != 0)
417 {
418 packet=(unsigned char) ReadBlobByte(image);
419 if (EOFBlob(image) != MagickFalse)
dirk08f033c2014-10-23 21:53:50 +0000420 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000421 *p++=packet;
422 pcx_packets--;
423 }
424 else
425 while (pcx_packets != 0)
426 {
427 packet=(unsigned char) ReadBlobByte(image);
428 if (EOFBlob(image) != MagickFalse)
dirk08f033c2014-10-23 21:53:50 +0000429 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000430 if ((packet & 0xc0) != 0xc0)
431 {
432 *p++=packet;
433 pcx_packets--;
434 continue;
435 }
436 count=(ssize_t) (packet & 0x3f);
437 packet=(unsigned char) ReadBlobByte(image);
438 if (EOFBlob(image) != MagickFalse)
dirk08f033c2014-10-23 21:53:50 +0000439 ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
cristy3ed852e2009-09-05 21:47:34 +0000440 for ( ; count != 0; count--)
441 {
442 *p++=packet;
443 pcx_packets--;
444 if (pcx_packets == 0)
445 break;
446 }
447 }
448 if (image->storage_class == DirectClass)
cristyb0a657e2012-08-29 00:45:37 +0000449 image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait :
450 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000451 else
452 if ((pcx_info.version == 5) ||
453 ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
454 {
455 /*
456 Initialize image colormap.
457 */
458 if (image->colors > 256)
dirk08f033c2014-10-23 21:53:50 +0000459 ThrowPCXException(CorruptImageError,"ColormapExceeds256Colors");
cristy3ed852e2009-09-05 21:47:34 +0000460 if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
461 {
462 /*
463 Monochrome colormap.
464 */
465 image->colormap[0].red=(Quantum) 0;
466 image->colormap[0].green=(Quantum) 0;
467 image->colormap[0].blue=(Quantum) 0;
cristy6e963d82012-06-19 15:23:24 +0000468 image->colormap[1].red=QuantumRange;
469 image->colormap[1].green=QuantumRange;
470 image->colormap[1].blue=QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +0000471 }
472 else
473 if (image->colors > 16)
474 {
475 /*
476 256 color images have their color map at the end of the file.
477 */
478 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
479 count=ReadBlob(image,3*image->colors,pcx_colormap);
480 p=pcx_colormap;
cristybb503372010-05-27 20:51:26 +0000481 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000482 {
483 image->colormap[i].red=ScaleCharToQuantum(*p++);
484 image->colormap[i].green=ScaleCharToQuantum(*p++);
485 image->colormap[i].blue=ScaleCharToQuantum(*p++);
486 }
487 }
cristy3ed852e2009-09-05 21:47:34 +0000488 }
489 /*
490 Convert PCX raster image to pixel packets.
491 */
cristybb503372010-05-27 20:51:26 +0000492 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000493 {
cristy0553bd52013-06-30 15:53:50 +0000494 p=pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
cristy3ed852e2009-09-05 21:47:34 +0000495 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000496 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000497 break;
cristy3ed852e2009-09-05 21:47:34 +0000498 r=scanline;
499 if (image->storage_class == DirectClass)
500 for (i=0; i < pcx_info.planes; i++)
501 {
502 r=scanline+i;
cristybb503372010-05-27 20:51:26 +0000503 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000504 {
505 switch (i)
506 {
507 case 0:
508 {
509 *r=(*p++);
510 break;
511 }
512 case 1:
513 {
514 *r=(*p++);
515 break;
516 }
517 case 2:
518 {
519 *r=(*p++);
520 break;
521 }
522 case 3:
523 default:
524 {
525 *r=(*p++);
526 break;
527 }
528 }
529 r+=pcx_info.planes;
530 }
531 }
532 else
533 if (pcx_info.planes > 1)
534 {
cristybb503372010-05-27 20:51:26 +0000535 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000536 *r++=0;
537 for (i=0; i < pcx_info.planes; i++)
538 {
539 r=scanline;
cristybb503372010-05-27 20:51:26 +0000540 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +0000541 {
542 bits=(*p++);
543 for (mask=0x80; mask != 0; mask>>=1)
544 {
545 if (bits & mask)
546 *r|=1 << i;
547 r++;
548 }
549 }
550 }
551 }
552 else
553 switch (pcx_info.bits_per_pixel)
554 {
555 case 1:
556 {
cristybb503372010-05-27 20:51:26 +0000557 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000558 bit;
559
cristybb503372010-05-27 20:51:26 +0000560 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000561 {
562 for (bit=7; bit >= 0; bit--)
563 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
564 p++;
565 }
566 if ((image->columns % 8) != 0)
567 {
cristybb503372010-05-27 20:51:26 +0000568 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
cristy3ed852e2009-09-05 21:47:34 +0000569 *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
570 p++;
571 }
572 break;
573 }
574 case 2:
575 {
cristybb503372010-05-27 20:51:26 +0000576 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
cristy3ed852e2009-09-05 21:47:34 +0000577 {
578 *r++=(*p >> 6) & 0x3;
579 *r++=(*p >> 4) & 0x3;
580 *r++=(*p >> 2) & 0x3;
581 *r++=(*p) & 0x3;
582 p++;
583 }
584 if ((image->columns % 4) != 0)
585 {
cristybb503372010-05-27 20:51:26 +0000586 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
cristy3ed852e2009-09-05 21:47:34 +0000587 *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
588 p++;
589 }
590 break;
591 }
592 case 4:
593 {
cristybb503372010-05-27 20:51:26 +0000594 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000595 {
596 *r++=(*p >> 4) & 0xf;
597 *r++=(*p) & 0xf;
598 p++;
599 }
600 if ((image->columns % 2) != 0)
601 *r++=(*p++ >> 4) & 0xf;
602 break;
603 }
604 case 8:
605 {
606 (void) CopyMagickMemory(r,p,image->columns);
607 break;
608 }
609 default:
610 break;
611 }
612 /*
613 Transfer image scanline.
614 */
615 r=scanline;
cristybb503372010-05-27 20:51:26 +0000616 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000617 {
618 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000619 SetPixelIndex(image,*r++,q);
cristy3ed852e2009-09-05 21:47:34 +0000620 else
621 {
cristy4c08aed2011-07-01 19:47:50 +0000622 SetPixelRed(image,ScaleCharToQuantum(*r++),q);
623 SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
624 SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
cristy17f11b02014-12-20 19:37:04 +0000625 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000626 SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
cristy3ed852e2009-09-05 21:47:34 +0000627 }
cristyed231572011-07-14 02:18:59 +0000628 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000629 }
630 if (SyncAuthenticPixels(image,exception) == MagickFalse)
631 break;
632 if (image->previous == (Image *) NULL)
633 {
cristycee97112010-05-28 00:44:52 +0000634 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000635 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000636 if (status == MagickFalse)
637 break;
638 }
639 }
640 if (image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +0000641 (void) SyncImage(image,exception);
cristy0acf7e12013-06-30 23:35:36 +0000642 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
643 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000644 if (EOFBlob(image) != MagickFalse)
645 {
646 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
647 image->filename);
648 break;
649 }
650 /*
651 Proceed to next image.
652 */
653 if (image_info->number_scenes != 0)
654 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
655 break;
656 if (page_table == (MagickOffsetType *) NULL)
657 break;
658 if (page_table[id] == 0)
659 break;
660 offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET);
661 if (offset < 0)
662 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
663 count=ReadBlob(image,1,&pcx_info.identifier);
664 if ((count != 0) && (pcx_info.identifier == 0x0a))
665 {
666 /*
667 Allocate next image structure.
668 */
cristy9950d572011-10-01 18:22:35 +0000669 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000670 if (GetNextImageInList(image) == (Image *) NULL)
671 {
672 image=DestroyImageList(image);
673 return((Image *) NULL);
674 }
675 image=SyncNextImageInList(image);
676 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
677 GetBlobSize(image));
678 if (status == MagickFalse)
679 break;
680 }
681 }
682 if (page_table != (MagickOffsetType *) NULL)
683 page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
684 (void) CloseBlob(image);
685 return(GetFirstImageInList(image));
686}
687
688/*
689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690% %
691% %
692% %
693% R e g i s t e r P C X I m a g e %
694% %
695% %
696% %
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698%
699% RegisterPCXImage() adds attributes for the PCX image format to
700% the list of supported formats. The attributes include the image format
701% tag, a method to read and/or write the format, whether the format
702% supports the saving of more than one frame to the same file or blob,
703% whether the format supports native in-memory I/O, and a brief
704% description of the format.
705%
706% The format of the RegisterPCXImage method is:
707%
cristybb503372010-05-27 20:51:26 +0000708% size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000709%
710*/
cristybb503372010-05-27 20:51:26 +0000711ModuleExport size_t RegisterPCXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000712{
713 MagickInfo
714 *entry;
715
dirk06b627a2015-04-06 18:59:17 +0000716 entry=AcquireMagickInfo("PCX","DCX","ZSoft IBM PC multi-page Paintbrush");
cristy3ed852e2009-09-05 21:47:34 +0000717 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
718 entry->encoder=(EncodeImageHandler *) WritePCXImage;
Dirk Lemstra8974d772017-01-22 03:19:27 +0100719 entry->flags|=CoderDecoderSeekableStreamFlag;
720 entry->flags|=CoderEncoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +0000721 entry->magick=(IsImageFormatHandler *) IsDCX;
cristy3ed852e2009-09-05 21:47:34 +0000722 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000723 entry=AcquireMagickInfo("PCX","PCX","ZSoft IBM PC Paintbrush");
cristy3ed852e2009-09-05 21:47:34 +0000724 entry->decoder=(DecodeImageHandler *) ReadPCXImage;
725 entry->encoder=(EncodeImageHandler *) WritePCXImage;
726 entry->magick=(IsImageFormatHandler *) IsPCX;
dirk08e9a112015-02-22 01:51:41 +0000727 entry->flags^=CoderAdjoinFlag;
Dirk Lemstra8974d772017-01-22 03:19:27 +0100728 entry->flags|=CoderDecoderSeekableStreamFlag;
729 entry->flags|=CoderEncoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +0000730 (void) RegisterMagickInfo(entry);
731 return(MagickImageCoderSignature);
732}
733
734/*
735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736% %
737% %
738% %
739% U n r e g i s t e r P C X I m a g e %
740% %
741% %
742% %
743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744%
745% UnregisterPCXImage() removes format registrations made by the
746% PCX module from the list of supported formats.
747%
748% The format of the UnregisterPCXImage method is:
749%
750% UnregisterPCXImage(void)
751%
752*/
753ModuleExport void UnregisterPCXImage(void)
754{
755 (void) UnregisterMagickInfo("DCX");
756 (void) UnregisterMagickInfo("PCX");
757}
758
759/*
760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761% %
762% %
763% %
764% W r i t e P C X I m a g e %
765% %
766% %
767% %
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769%
770% WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file
771% format.
772%
773% The format of the WritePCXImage method is:
774%
cristy1e178e72011-08-28 19:44:34 +0000775% MagickBooleanType WritePCXImage(const ImageInfo *image_info,
776% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000777%
778% A description of each parameter follows.
779%
780% o image_info: the image info.
781%
782% o image: The image.
783%
cristy1e178e72011-08-28 19:44:34 +0000784% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000785%
786*/
cristy1e178e72011-08-28 19:44:34 +0000787
cristy3ed852e2009-09-05 21:47:34 +0000788static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
789 const unsigned char *pixels,Image *image)
790{
791 register const unsigned char
792 *q;
793
cristybb503372010-05-27 20:51:26 +0000794 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000795 i,
796 x;
797
798 ssize_t
799 count;
800
801 unsigned char
802 packet,
803 previous;
804
805 q=pixels;
cristybb503372010-05-27 20:51:26 +0000806 for (i=0; i < (ssize_t) pcx_info->planes; i++)
cristy3ed852e2009-09-05 21:47:34 +0000807 {
cristy1f9852b2010-09-04 15:05:36 +0000808 if (pcx_info->encoding == 0)
cristy3ed852e2009-09-05 21:47:34 +0000809 {
cristy1f9852b2010-09-04 15:05:36 +0000810 for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
811 (void) WriteBlobByte(image,(unsigned char) (*q++));
cristy3ed852e2009-09-05 21:47:34 +0000812 }
cristy1f9852b2010-09-04 15:05:36 +0000813 else
814 {
815 previous=(*q++);
816 count=1;
817 for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
818 {
819 packet=(*q++);
820 if ((packet == previous) && (count < 63))
821 {
822 count++;
823 continue;
824 }
825 if ((count > 1) || ((previous & 0xc0) == 0xc0))
826 {
827 count|=0xc0;
828 (void) WriteBlobByte(image,(unsigned char) count);
829 }
830 (void) WriteBlobByte(image,previous);
831 previous=packet;
832 count=1;
833 }
834 if ((count > 1) || ((previous & 0xc0) == 0xc0))
835 {
836 count|=0xc0;
837 (void) WriteBlobByte(image,(unsigned char) count);
838 }
839 (void) WriteBlobByte(image,previous);
840 }
cristy3ed852e2009-09-05 21:47:34 +0000841 }
842 return (MagickTrue);
843}
844
cristy1e178e72011-08-28 19:44:34 +0000845static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
846 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000847{
cristy3ed852e2009-09-05 21:47:34 +0000848 MagickBooleanType
849 status;
850
851 MagickOffsetType
852 offset,
853 *page_table,
854 scene;
855
cristy0553bd52013-06-30 15:53:50 +0000856 MemoryInfo
857 *pixel_info;
858
cristy3ed852e2009-09-05 21:47:34 +0000859 PCXInfo
860 pcx_info;
861
cristy4c08aed2011-07-01 19:47:50 +0000862 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000863 *p;
864
cristybb503372010-05-27 20:51:26 +0000865 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000866 i,
867 x;
868
869 register unsigned char
870 *q;
871
872 size_t
873 length;
874
cristyaff6d802011-04-26 01:46:31 +0000875 ssize_t
876 y;
877
cristy3ed852e2009-09-05 21:47:34 +0000878 unsigned char
879 *pcx_colormap,
cristy0553bd52013-06-30 15:53:50 +0000880 *pixels;
cristy3ed852e2009-09-05 21:47:34 +0000881
882 /*
883 Open output image file.
884 */
885 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000886 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000887 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000888 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000889 if (image->debug != MagickFalse)
890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000891 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000892 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +0000893 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000894 if (status == MagickFalse)
895 return(status);
cristyaf8d3912014-02-21 14:50:33 +0000896 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 page_table=(MagickOffsetType *) NULL;
898 if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
899 ((GetNextImageInList(image) != (Image *) NULL) &&
900 (image_info->adjoin != MagickFalse)))
901 {
902 /*
903 Write the DCX page table.
904 */
905 (void) WriteBlobLSBLong(image,0x3ADE68B1L);
906 page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL,
907 sizeof(*page_table));
908 if (page_table == (MagickOffsetType *) NULL)
909 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
910 for (scene=0; scene < 1024; scene++)
911 (void) WriteBlobLSBLong(image,0x00000000L);
912 }
913 scene=0;
914 do
915 {
916 if (page_table != (MagickOffsetType *) NULL)
917 page_table[scene]=TellBlob(image);
918 /*
919 Initialize PCX raster file header.
920 */
921 pcx_info.identifier=0x0a;
922 pcx_info.version=5;
cristy1f9852b2010-09-04 15:05:36 +0000923 pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
cristy3ed852e2009-09-05 21:47:34 +0000924 pcx_info.bits_per_pixel=8;
925 if ((image->storage_class == PseudoClass) &&
dirkf1d85482015-04-06 00:36:00 +0000926 (SetImageMonochrome(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000927 pcx_info.bits_per_pixel=1;
928 pcx_info.left=0;
929 pcx_info.top=0;
930 pcx_info.right=(unsigned short) (image->columns-1);
931 pcx_info.bottom=(unsigned short) (image->rows-1);
932 switch (image->units)
933 {
934 case UndefinedResolution:
935 case PixelsPerInchResolution:
936 default:
937 {
cristy2a11bef2011-10-28 18:33:11 +0000938 pcx_info.horizontal_resolution=(unsigned short) image->resolution.x;
939 pcx_info.vertical_resolution=(unsigned short) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000940 break;
941 }
942 case PixelsPerCentimeterResolution:
943 {
944 pcx_info.horizontal_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000945 (2.54*image->resolution.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000946 pcx_info.vertical_resolution=(unsigned short)
cristy2a11bef2011-10-28 18:33:11 +0000947 (2.54*image->resolution.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000948 break;
949 }
950 }
951 pcx_info.reserved=0;
952 pcx_info.planes=1;
953 if ((image->storage_class == DirectClass) || (image->colors > 256))
954 {
955 pcx_info.planes=3;
cristy17f11b02014-12-20 19:37:04 +0000956 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000957 pcx_info.planes++;
958 }
cristybb503372010-05-27 20:51:26 +0000959 pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
cristy3ed852e2009-09-05 21:47:34 +0000960 pcx_info.bits_per_pixel+7)/8);
961 pcx_info.palette_info=1;
962 pcx_info.colormap_signature=0x0c;
963 /*
964 Write PCX header.
965 */
966 (void) WriteBlobByte(image,pcx_info.identifier);
967 (void) WriteBlobByte(image,pcx_info.version);
968 (void) WriteBlobByte(image,pcx_info.encoding);
969 (void) WriteBlobByte(image,pcx_info.bits_per_pixel);
970 (void) WriteBlobLSBShort(image,pcx_info.left);
971 (void) WriteBlobLSBShort(image,pcx_info.top);
972 (void) WriteBlobLSBShort(image,pcx_info.right);
973 (void) WriteBlobLSBShort(image,pcx_info.bottom);
974 (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution);
975 (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution);
976 /*
977 Dump colormap to file.
978 */
979 pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL,
980 3*sizeof(*pcx_colormap));
981 if (pcx_colormap == (unsigned char *) NULL)
982 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
983 (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
984 q=pcx_colormap;
985 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
cristybb503372010-05-27 20:51:26 +0000986 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000987 {
988 *q++=ScaleQuantumToChar(image->colormap[i].red);
989 *q++=ScaleQuantumToChar(image->colormap[i].green);
990 *q++=ScaleQuantumToChar(image->colormap[i].blue);
991 }
992 (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap);
993 (void) WriteBlobByte(image,pcx_info.reserved);
994 (void) WriteBlobByte(image,pcx_info.planes);
995 (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
996 (void) WriteBlobLSBShort(image,pcx_info.palette_info);
997 for (i=0; i < 58; i++)
998 (void) WriteBlobByte(image,'\0');
999 length=(size_t) pcx_info.bytes_per_line;
cristy0553bd52013-06-30 15:53:50 +00001000 pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
1001 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001002 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00001003 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1004 q=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001005 if ((image->storage_class == DirectClass) || (image->colors > 256))
1006 {
cristy3ed852e2009-09-05 21:47:34 +00001007 /*
1008 Convert DirectClass image to PCX raster pixels.
1009 */
cristybb503372010-05-27 20:51:26 +00001010 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001011 {
cristy0553bd52013-06-30 15:53:50 +00001012 q=pixels;
cristy3ed852e2009-09-05 21:47:34 +00001013 for (i=0; i < pcx_info.planes; i++)
1014 {
cristy0acf7e12013-06-30 23:35:36 +00001015 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1016 if (p == (const Quantum *) NULL)
1017 break;
cristy3ed852e2009-09-05 21:47:34 +00001018 switch ((int) i)
1019 {
1020 case 0:
1021 {
cristybb503372010-05-27 20:51:26 +00001022 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001023 {
cristy4c08aed2011-07-01 19:47:50 +00001024 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristyed231572011-07-14 02:18:59 +00001025 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001026 }
1027 break;
1028 }
1029 case 1:
1030 {
cristybb503372010-05-27 20:51:26 +00001031 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001032 {
cristy4c08aed2011-07-01 19:47:50 +00001033 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
cristyed231572011-07-14 02:18:59 +00001034 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001035 }
1036 break;
1037 }
1038 case 2:
1039 {
cristybb503372010-05-27 20:51:26 +00001040 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001041 {
cristy4c08aed2011-07-01 19:47:50 +00001042 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +00001043 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001044 }
1045 break;
1046 }
1047 case 3:
1048 default:
1049 {
cristybb503372010-05-27 20:51:26 +00001050 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001051 {
cristy4c08aed2011-07-01 19:47:50 +00001052 *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00001053 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001054 }
1055 break;
1056 }
1057 }
1058 }
cristy0553bd52013-06-30 15:53:50 +00001059 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001060 break;
1061 if (image->previous == (Image *) NULL)
1062 {
cristycee97112010-05-28 00:44:52 +00001063 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1064 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001065 if (status == MagickFalse)
1066 break;
1067 }
1068 }
1069 }
1070 else
1071 {
1072 if (pcx_info.bits_per_pixel > 1)
cristybb503372010-05-27 20:51:26 +00001073 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001074 {
cristy1e178e72011-08-28 19:44:34 +00001075 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001076 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001077 break;
cristy0553bd52013-06-30 15:53:50 +00001078 q=pixels;
cristybb503372010-05-27 20:51:26 +00001079 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001080 {
1081 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001082 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001083 }
cristy0553bd52013-06-30 15:53:50 +00001084 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001085 break;
1086 if (image->previous == (Image *) NULL)
1087 {
cristycee97112010-05-28 00:44:52 +00001088 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1089 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001090 if (status == MagickFalse)
1091 break;
1092 }
1093 }
1094 else
1095 {
cristy3ed852e2009-09-05 21:47:34 +00001096 register unsigned char
1097 bit,
1098 byte;
1099
1100 /*
1101 Convert PseudoClass image to a PCX monochrome image.
1102 */
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;
cristy0553bd52013-06-30 15:53:50 +00001110 q=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;
cristy85636be2014-04-04 19:16:53 +00001114 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
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);
cristy0553bd52013-06-30 15:53:50 +00001127 if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001128 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 }
cristy0553bd52013-06-30 15:53:50 +00001141 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001142 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}