blob: 767d757a88e47b806686a5a1b8e8926c0f101d44 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP N N M M %
7% P P NN N MM MM %
8% PPPP N N N M M M %
9% P N NN M M %
10% P N N M M %
11% %
12% %
13% Read/Write PBMPlus Portable Anymap Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/exception.h"
52#include "MagickCore/exception-private.h"
53#include "MagickCore/image.h"
54#include "MagickCore/image-private.h"
55#include "MagickCore/list.h"
56#include "MagickCore/magick.h"
57#include "MagickCore/memory_.h"
58#include "MagickCore/module.h"
59#include "MagickCore/monitor.h"
60#include "MagickCore/monitor-private.h"
61#include "MagickCore/pixel-accessor.h"
62#include "MagickCore/property.h"
63#include "MagickCore/quantum-private.h"
64#include "MagickCore/static.h"
65#include "MagickCore/statistic.h"
66#include "MagickCore/string_.h"
67#include "MagickCore/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000068
69/*
70 Forward declarations.
71*/
72static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000073 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000074
75/*
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77% %
78% %
79% %
80% I s P N M %
81% %
82% %
83% %
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85%
86% IsPNM() returns MagickTrue if the image format type, identified by the
87% magick string, is PNM.
88%
89% The format of the IsPNM method is:
90%
91% MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
92%
93% A description of each parameter follows:
94%
95% o magick: compare image format pattern against these bytes.
96%
97% o extent: Specifies the extent of the magick string.
98%
99*/
100static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
101{
102 if (extent < 2)
103 return(MagickFalse);
104 if ((*magick == (unsigned char) 'P') &&
105 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
106 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
107 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
108 return(MagickTrue);
109 return(MagickFalse);
110}
111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
117% R e a d P N M I m a g e %
118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
123% ReadPNMImage() reads a Portable Anymap image file and returns it.
124% It allocates the memory necessary for the new Image structure and returns
125% a pointer to the new image.
126%
127% The format of the ReadPNMImage method is:
128%
129% Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
130%
131% A description of each parameter follows:
132%
133% o image_info: the image info.
134%
135% o exception: return any errors or warnings in this structure.
136%
137*/
138
cristybb503372010-05-27 20:51:26 +0000139static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 const size_t extent)
cristy3ed852e2009-09-05 21:47:34 +0000141{
cristybb503372010-05-27 20:51:26 +0000142 if ((offset < 0) || (offset > (ssize_t) extent))
cristy3ed852e2009-09-05 21:47:34 +0000143 {
144 (void) ThrowMagickException(&image->exception,GetMagickModule(),
145 CorruptImageError,"InvalidPixel","`%s'",image->filename);
146 return(0);
147 }
148 return(offset);
149}
150
cristybb503372010-05-27 20:51:26 +0000151static size_t PNMInteger(Image *image,const unsigned int base)
cristy3ed852e2009-09-05 21:47:34 +0000152{
153 char
154 *comment;
155
156 int
157 c;
158
159 register char
160 *p;
161
162 size_t
cristyaff6d802011-04-26 01:46:31 +0000163 extent,
cristy3ed852e2009-09-05 21:47:34 +0000164 value;
165
166 /*
167 Skip any leading whitespace.
168 */
169 extent=MaxTextExtent;
170 comment=(char *) NULL;
171 p=comment;
172 do
173 {
174 c=ReadBlobByte(image);
175 if (c == EOF)
176 return(0);
177 if (c == (int) '#')
178 {
179 /*
180 Read comment.
181 */
182 if (comment == (char *) NULL)
183 comment=AcquireString((char *) NULL);
184 p=comment+strlen(comment);
185 for ( ; (c != EOF) && (c != (int) '\n'); p++)
186 {
187 if ((size_t) (p-comment+1) >= extent)
188 {
189 extent<<=1;
190 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
191 sizeof(*comment));
192 if (comment == (char *) NULL)
193 break;
194 p=comment+strlen(comment);
195 }
196 c=ReadBlobByte(image);
197 *p=(char) c;
198 *(p+1)='\0';
199 }
200 if (comment == (char *) NULL)
201 return(0);
202 continue;
203 }
204 } while (isdigit(c) == MagickFalse);
205 if (comment != (char *) NULL)
206 {
207 (void) SetImageProperty(image,"comment",comment);
208 comment=DestroyString(comment);
209 }
210 if (base == 2)
cristybb503372010-05-27 20:51:26 +0000211 return((size_t) (c-(int) '0'));
cristy3ed852e2009-09-05 21:47:34 +0000212 /*
213 Evaluate number.
214 */
215 value=0;
216 do
217 {
218 value*=10;
219 value+=c-(int) '0';
220 c=ReadBlobByte(image);
221 if (c == EOF)
222 return(value);
223 } while (isdigit(c) != MagickFalse);
224 return(value);
225}
226
227static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
228{
229 char
230 format;
231
232 double
233 quantum_scale;
234
235 Image
236 *image;
237
cristy3ed852e2009-09-05 21:47:34 +0000238 MagickBooleanType
239 status;
240
241 Quantum
242 *scale;
243
244 QuantumInfo
245 *quantum_info;
246
247 QuantumType
248 quantum_type;
249
cristybb503372010-05-27 20:51:26 +0000250 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000251 i;
252
253 size_t
cristyaff6d802011-04-26 01:46:31 +0000254 depth,
cristy3ed852e2009-09-05 21:47:34 +0000255 extent,
cristyaff6d802011-04-26 01:46:31 +0000256 max_value,
cristy3ed852e2009-09-05 21:47:34 +0000257 packet_size;
258
259 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000260 count,
261 row,
262 y;
cristy3ed852e2009-09-05 21:47:34 +0000263
cristy3ed852e2009-09-05 21:47:34 +0000264 /*
265 Open image file.
266 */
267 assert(image_info != (const ImageInfo *) NULL);
268 assert(image_info->signature == MagickSignature);
269 if (image_info->debug != MagickFalse)
270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
271 image_info->filename);
272 assert(exception != (ExceptionInfo *) NULL);
273 assert(exception->signature == MagickSignature);
274 image=AcquireImage(image_info);
275 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
276 if (status == MagickFalse)
277 {
278 image=DestroyImageList(image);
279 return((Image *) NULL);
280 }
281 /*
282 Read PNM image.
283 */
284 count=ReadBlob(image,1,(unsigned char *) &format);
285 do
286 {
287 /*
288 Initialize image structure.
289 */
290 if ((count != 1) || (format != 'P'))
291 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
292 max_value=1;
293 quantum_type=RGBQuantum;
294 quantum_scale=1.0;
295 format=(char) ReadBlobByte(image);
296 if (format != '7')
297 {
298 /*
299 PBM, PGM, PPM, and PNM.
300 */
301 image->columns=PNMInteger(image,10);
302 image->rows=PNMInteger(image,10);
303 if ((format == 'f') || (format == 'F'))
304 {
305 char
306 scale[MaxTextExtent];
307
308 (void) ReadBlobString(image,scale);
cristyc1acd842011-05-19 23:05:47 +0000309 quantum_scale=InterpretLocaleValue(scale,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000310 }
311 else
312 {
313 if ((format == '1') || (format == '4'))
314 max_value=1; /* bitmap */
315 else
316 max_value=PNMInteger(image,10);
317 }
318 }
319 else
320 {
321 char
322 keyword[MaxTextExtent],
323 value[MaxTextExtent];
324
325 int
326 c;
327
328 register char
329 *p;
330
331 /*
332 PAM.
333 */
334 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
335 {
336 while (isspace((int) ((unsigned char) c)) != 0)
337 c=ReadBlobByte(image);
338 p=keyword;
339 do
340 {
341 if ((size_t) (p-keyword) < (MaxTextExtent-1))
342 *p++=c;
343 c=ReadBlobByte(image);
344 } while (isalnum(c));
345 *p='\0';
346 if (LocaleCompare(keyword,"endhdr") == 0)
347 break;
348 while (isspace((int) ((unsigned char) c)) != 0)
349 c=ReadBlobByte(image);
350 p=value;
351 while (isalnum(c) || (c == '_'))
352 {
353 if ((size_t) (p-value) < (MaxTextExtent-1))
354 *p++=c;
355 c=ReadBlobByte(image);
356 }
357 *p='\0';
358 /*
359 Assign a value to the specified keyword.
360 */
361 if (LocaleCompare(keyword,"depth") == 0)
cristye27293e2009-12-18 02:53:20 +0000362 packet_size=StringToUnsignedLong(value);
cristyda16f162011-02-19 23:52:17 +0000363 (void) packet_size;
cristy3ed852e2009-09-05 21:47:34 +0000364 if (LocaleCompare(keyword,"height") == 0)
cristye27293e2009-12-18 02:53:20 +0000365 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000366 if (LocaleCompare(keyword,"maxval") == 0)
cristye27293e2009-12-18 02:53:20 +0000367 max_value=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000368 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
369 {
370 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
371 quantum_type=GrayQuantum;
372 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
373 {
374 quantum_type=GrayAlphaQuantum;
375 image->matte=MagickTrue;
376 }
377 if (LocaleCompare(value,"GRAYSCALE") == 0)
cristyb3a73b52011-07-26 01:34:43 +0000378 {
379 image->colorspace=GRAYColorspace;
380 quantum_type=GrayQuantum;
381 }
cristy3ed852e2009-09-05 21:47:34 +0000382 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
383 {
cristyb3a73b52011-07-26 01:34:43 +0000384 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000385 image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +0000386 quantum_type=GrayAlphaQuantum;
cristy3ed852e2009-09-05 21:47:34 +0000387 }
388 if (LocaleCompare(value,"RGB_ALPHA") == 0)
389 {
390 quantum_type=RGBAQuantum;
391 image->matte=MagickTrue;
392 }
393 if (LocaleCompare(value,"CMYK") == 0)
394 {
395 quantum_type=CMYKQuantum;
396 image->colorspace=CMYKColorspace;
397 }
398 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
399 {
400 quantum_type=CMYKAQuantum;
401 image->colorspace=CMYKColorspace;
402 image->matte=MagickTrue;
403 }
404 }
405 if (LocaleCompare(keyword,"width") == 0)
cristye27293e2009-12-18 02:53:20 +0000406 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000407 }
408 }
409 if ((image->columns == 0) || (image->rows == 0))
410 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
411 if (max_value >= 65536)
412 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3f1ee592011-06-15 23:03:58 +0000413 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
cristy3ed852e2009-09-05 21:47:34 +0000414 image->depth=depth;
415 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
416 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
417 break;
418 /*
419 Convert PNM pixels to runextent-encoded MIFF packets.
420 */
421 status=MagickTrue;
422 row=0;
423 switch (format)
424 {
425 case '1':
426 {
427 /*
428 Convert PBM image to pixel packets.
429 */
cristyb3a73b52011-07-26 01:34:43 +0000430 image->colorspace=GRAYColorspace;
cristybb503372010-05-27 20:51:26 +0000431 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000432 {
cristybb503372010-05-27 20:51:26 +0000433 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000434 x;
435
cristy4c08aed2011-07-01 19:47:50 +0000436 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000437 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000438
439 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000440 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000441 break;
cristybb503372010-05-27 20:51:26 +0000442 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000443 {
cristy4c08aed2011-07-01 19:47:50 +0000444 SetPixelRed(image,PNMInteger(image,2) == 0 ? QuantumRange : 0,q);
445 SetPixelGreen(image,GetPixelRed(image,q),q);
446 SetPixelBlue(image,GetPixelRed(image,q),q);
cristyed231572011-07-14 02:18:59 +0000447 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000448 }
449 if (SyncAuthenticPixels(image,exception) == MagickFalse)
450 break;
451 if (image->previous == (Image *) NULL)
452 {
cristycee97112010-05-28 00:44:52 +0000453 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
454 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000455 if (status == MagickFalse)
456 break;
457 }
458 }
459 image->type=BilevelType;
460 break;
461 }
462 case '2':
463 {
cristybb503372010-05-27 20:51:26 +0000464 size_t
cristy3ed852e2009-09-05 21:47:34 +0000465 intensity;
466
467 /*
468 Convert PGM image to pixel packets.
469 */
cristyb3a73b52011-07-26 01:34:43 +0000470 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000471 scale=(Quantum *) NULL;
472 if (max_value != (1U*QuantumRange))
473 {
474 /*
475 Compute pixel scaling table.
476 */
477 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
478 sizeof(*scale));
479 if (scale == (Quantum *) NULL)
480 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000481 for (i=0; i <= (ssize_t) max_value; i++)
cristy3ed852e2009-09-05 21:47:34 +0000482 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
483 }
cristybb503372010-05-27 20:51:26 +0000484 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000485 {
cristybb503372010-05-27 20:51:26 +0000486 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000487 x;
488
cristy4c08aed2011-07-01 19:47:50 +0000489 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000490 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000491
492 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyb3a73b52011-07-26 01:34:43 +0000493 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000494 break;
cristybb503372010-05-27 20:51:26 +0000495 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000496 {
497 intensity=PNMInteger(image,10);
cristy4c08aed2011-07-01 19:47:50 +0000498 SetPixelRed(image,intensity,q);
cristyac23fd22010-05-11 14:48:58 +0000499 if (scale != (Quantum *) NULL)
cristy4c08aed2011-07-01 19:47:50 +0000500 SetPixelRed(image,scale[ConstrainPixel(image,(ssize_t) intensity,
501 max_value)],q);
502 SetPixelGreen(image,GetPixelRed(image,q),q);
503 SetPixelBlue(image,GetPixelRed(image,q),q);
cristyed231572011-07-14 02:18:59 +0000504 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000505 }
506 if (SyncAuthenticPixels(image,exception) == MagickFalse)
507 break;
508 if (image->previous == (Image *) NULL)
509 {
cristycee97112010-05-28 00:44:52 +0000510 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
511 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000512 if (status == MagickFalse)
513 break;
514 }
515 }
516 image->type=GrayscaleType;
517 if (scale != (Quantum *) NULL)
518 scale=(Quantum *) RelinquishMagickMemory(scale);
519 break;
520 }
521 case '3':
522 {
cristy4c08aed2011-07-01 19:47:50 +0000523 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000524 pixel;
525
526 /*
527 Convert PNM image to pixel packets.
528 */
529 scale=(Quantum *) NULL;
530 if (max_value != (1U*QuantumRange))
531 {
532 /*
533 Compute pixel scaling table.
534 */
535 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
536 sizeof(*scale));
537 if (scale == (Quantum *) NULL)
538 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000539 for (i=0; i <= (ssize_t) max_value; i++)
cristy3ed852e2009-09-05 21:47:34 +0000540 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
541 }
cristybb503372010-05-27 20:51:26 +0000542 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000543 {
cristybb503372010-05-27 20:51:26 +0000544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000545 x;
546
cristy4c08aed2011-07-01 19:47:50 +0000547 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000548 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000549
550 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000551 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000552 break;
cristybb503372010-05-27 20:51:26 +0000553 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000554 {
555 pixel.red=(MagickRealType) PNMInteger(image,10);
556 pixel.green=(MagickRealType) PNMInteger(image,10);
557 pixel.blue=(MagickRealType) PNMInteger(image,10);
558 if (scale != (Quantum *) NULL)
559 {
cristybb503372010-05-27 20:51:26 +0000560 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
cristy3ed852e2009-09-05 21:47:34 +0000561 pixel.red,max_value)];
cristy34575212010-11-06 12:39:23 +0000562 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
563 (ssize_t) pixel.green,max_value)];
cristybb503372010-05-27 20:51:26 +0000564 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
cristy3ed852e2009-09-05 21:47:34 +0000565 pixel.blue,max_value)];
566 }
cristy4c08aed2011-07-01 19:47:50 +0000567 SetPixelRed(image,pixel.red,q);
568 SetPixelGreen(image,pixel.green,q);
569 SetPixelBlue(image,pixel.blue,q);
cristyed231572011-07-14 02:18:59 +0000570 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000571 }
572 if (SyncAuthenticPixels(image,exception) == MagickFalse)
573 break;
574 if (image->previous == (Image *) NULL)
575 {
cristycee97112010-05-28 00:44:52 +0000576 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
577 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000578 if (status == MagickFalse)
579 break;
580 }
581 }
582 if (scale != (Quantum *) NULL)
583 scale=(Quantum *) RelinquishMagickMemory(scale);
584 break;
585 }
586 case '4':
587 {
588 /*
589 Convert PBM raw image to pixel packets.
590 */
cristyb3a73b52011-07-26 01:34:43 +0000591 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000592 quantum_type=GrayQuantum;
593 if (image->storage_class == PseudoClass)
594 quantum_type=IndexQuantum;
595 quantum_info=AcquireQuantumInfo(image_info,image);
596 if (quantum_info == (QuantumInfo *) NULL)
597 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
598 SetQuantumMinIsWhite(quantum_info,MagickTrue);
599 extent=GetQuantumExtent(image,quantum_info,quantum_type);
cristybb503372010-05-27 20:51:26 +0000600 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
cristy3ed852e2009-09-05 21:47:34 +0000602 MagickBooleanType
603 sync;
604
cristy4c08aed2011-07-01 19:47:50 +0000605 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000606 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000607
608 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000609 count,
610 offset;
cristy3ed852e2009-09-05 21:47:34 +0000611
612 size_t
613 length;
614
615 unsigned char
616 *pixels;
617
618 if (status == MagickFalse)
619 continue;
620 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000621 {
622 count=ReadBlob(image,extent,pixels);
623 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
624 (image->previous == (Image *) NULL))
625 {
626 MagickBooleanType
627 proceed;
628
cristycee97112010-05-28 00:44:52 +0000629 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
630 row,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000631 if (proceed == MagickFalse)
632 status=MagickFalse;
633 }
634 offset=row++;
635 }
636 if (count != (ssize_t) extent)
637 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +0000638 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000639 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 status=MagickFalse;
642 continue;
643 }
cristyaa740112010-03-30 17:58:44 +0000644 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
645 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000646 if (length != extent)
647 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +0000648 sync=SyncAuthenticPixels(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000649 if (sync == MagickFalse)
650 status=MagickFalse;
651 }
cristy3ed852e2009-09-05 21:47:34 +0000652 quantum_info=DestroyQuantumInfo(quantum_info);
653 if (status == MagickFalse)
654 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
655 SetQuantumImageType(image,quantum_type);
656 break;
657 }
658 case '5':
659 {
660 QuantumAny
661 range;
662
663 /*
664 Convert PGM raw image to pixel packets.
665 */
cristyb3a73b52011-07-26 01:34:43 +0000666 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000667 range=GetQuantumRange(image->depth);
668 quantum_type=GrayQuantum;
669 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
670 quantum_info=AcquireQuantumInfo(image_info,image);
671 if (quantum_info == (QuantumInfo *) NULL)
672 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000673 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000674 {
cristy3ed852e2009-09-05 21:47:34 +0000675 MagickBooleanType
676 sync;
677
678 register const unsigned char
cristy3f2302b2010-03-11 03:16:37 +0000679 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000680
cristybb503372010-05-27 20:51:26 +0000681 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000682 x;
683
cristy4c08aed2011-07-01 19:47:50 +0000684 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000685 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000686
687 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000688 count,
689 offset;
cristy3ed852e2009-09-05 21:47:34 +0000690
691 unsigned char
692 *pixels;
693
694 if (status == MagickFalse)
695 continue;
696 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000697 {
698 count=ReadBlob(image,extent,pixels);
699 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
700 (image->previous == (Image *) NULL))
701 {
702 MagickBooleanType
703 proceed;
704
cristy34575212010-11-06 12:39:23 +0000705 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
706 row,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000707 if (proceed == MagickFalse)
708 status=MagickFalse;
709 }
710 offset=row++;
711 }
712 if (count != (ssize_t) extent)
713 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +0000714 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000715 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000716 {
717 status=MagickFalse;
718 continue;
719 }
720 p=pixels;
721 if ((image->depth == 8) || (image->depth == 16))
cristyaa740112010-03-30 17:58:44 +0000722 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +0000723 quantum_type,pixels,exception);
724 else
725 if (image->depth <= 8)
726 {
727 unsigned char
728 pixel;
729
cristybb503372010-05-27 20:51:26 +0000730 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000731 {
732 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000733 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
734 SetPixelGreen(image,GetPixelRed(image,q),q);
735 SetPixelBlue(image,GetPixelRed(image,q),q);
cristyed231572011-07-14 02:18:59 +0000736 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000737 }
738 }
739 else
740 {
741 unsigned short
742 pixel;
743
cristybb503372010-05-27 20:51:26 +0000744 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000745 {
746 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000747 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
748 SetPixelGreen(image,GetPixelRed(image,q),q);
749 SetPixelBlue(image,GetPixelRed(image,q),q);
cristyed231572011-07-14 02:18:59 +0000750 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000751 }
752 }
cristyaa740112010-03-30 17:58:44 +0000753 sync=SyncAuthenticPixels(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000754 if (sync == MagickFalse)
755 status=MagickFalse;
756 }
cristy3ed852e2009-09-05 21:47:34 +0000757 quantum_info=DestroyQuantumInfo(quantum_info);
758 if (status == MagickFalse)
759 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
760 SetQuantumImageType(image,quantum_type);
761 break;
762 }
763 case '6':
764 {
765 ImageType
766 type;
767
768 QuantumAny
769 range;
770
771 /*
772 Convert PNM raster image to pixel packets.
773 */
774 type=BilevelType;
775 quantum_type=RGBQuantum;
776 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
777 range=GetQuantumRange(image->depth);
778 quantum_info=AcquireQuantumInfo(image_info,image);
779 if (quantum_info == (QuantumInfo *) NULL)
780 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000781 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000782 {
cristy3ed852e2009-09-05 21:47:34 +0000783 MagickBooleanType
784 sync;
785
786 register const unsigned char
cristy3f2302b2010-03-11 03:16:37 +0000787 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000788
cristybb503372010-05-27 20:51:26 +0000789 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000790 x;
791
cristy4c08aed2011-07-01 19:47:50 +0000792 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000793 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000794
795 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000796 count,
797 offset;
cristy3ed852e2009-09-05 21:47:34 +0000798
cristy3ed852e2009-09-05 21:47:34 +0000799 unsigned char
800 *pixels;
801
802 if (status == MagickFalse)
803 continue;
804 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000805 {
806 count=ReadBlob(image,extent,pixels);
807 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
808 (image->previous == (Image *) NULL))
809 {
810 MagickBooleanType
811 proceed;
812
cristy34575212010-11-06 12:39:23 +0000813 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
814 row,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000815 if (proceed == MagickFalse)
816 status=MagickFalse;
817 }
818 offset=row++;
819 }
820 if (count != (ssize_t) extent)
821 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +0000822 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000823 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000824 {
825 status=MagickFalse;
826 continue;
827 }
828 p=pixels;
cristye90d7402010-03-14 18:21:29 +0000829 if (image->depth == 8)
cristybb503372010-05-27 20:51:26 +0000830 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000831 {
cristy4c08aed2011-07-01 19:47:50 +0000832 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
833 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
834 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
835 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000836 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000837 }
838 else
cristye90d7402010-03-14 18:21:29 +0000839 if (image->depth == 16)
cristy3ed852e2009-09-05 21:47:34 +0000840 {
841 unsigned short
842 pixel;
843
cristybb503372010-05-27 20:51:26 +0000844 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000845 {
846 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000847 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000848 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000849 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
cristy3ed852e2009-09-05 21:47:34 +0000850 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000851 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
852 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000853 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000854 }
855 }
cristye90d7402010-03-14 18:21:29 +0000856 else
857 if (image->depth <= 8)
858 {
859 unsigned char
860 pixel;
861
cristybb503372010-05-27 20:51:26 +0000862 for (x=0; x < (ssize_t) image->columns; x++)
cristye90d7402010-03-14 18:21:29 +0000863 {
864 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000865 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristye90d7402010-03-14 18:21:29 +0000866 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000867 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristye90d7402010-03-14 18:21:29 +0000868 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000869 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
870 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000871 q+=GetPixelChannels(image);
cristye90d7402010-03-14 18:21:29 +0000872 }
873 }
874 else
875 {
876 unsigned short
877 pixel;
878
cristybb503372010-05-27 20:51:26 +0000879 for (x=0; x < (ssize_t) image->columns; x++)
cristye90d7402010-03-14 18:21:29 +0000880 {
881 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000882 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristye90d7402010-03-14 18:21:29 +0000883 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000884 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristye90d7402010-03-14 18:21:29 +0000885 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000886 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
887 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000888 q+=GetPixelChannels(image);
cristye90d7402010-03-14 18:21:29 +0000889 }
890 }
cristy3ed852e2009-09-05 21:47:34 +0000891 if ((type == BilevelType) || (type == GrayscaleType))
cristy3ed852e2009-09-05 21:47:34 +0000892 {
cristyaa740112010-03-30 17:58:44 +0000893 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
cristybb503372010-05-27 20:51:26 +0000894 for (x=0; x < (ssize_t) image->columns; x++)
cristye90d7402010-03-14 18:21:29 +0000895 {
896 if ((type == BilevelType) &&
cristy4c08aed2011-07-01 19:47:50 +0000897 (IsPixelMonochrome(image,q) == MagickFalse))
898 type=IsPixelGray(image,q) == MagickFalse ? UndefinedType :
cristye90d7402010-03-14 18:21:29 +0000899 GrayscaleType;
cristy4c08aed2011-07-01 19:47:50 +0000900 if ((type == GrayscaleType) &&
901 (IsPixelGray(image,q) == MagickFalse))
cristy5f1c1ff2010-12-23 21:38:06 +0000902 type=UndefinedType;
cristye90d7402010-03-14 18:21:29 +0000903 if ((type != BilevelType) && (type != GrayscaleType))
904 break;
cristyed231572011-07-14 02:18:59 +0000905 q+=GetPixelChannels(image);
cristye90d7402010-03-14 18:21:29 +0000906 }
cristy3ed852e2009-09-05 21:47:34 +0000907 }
cristyaa740112010-03-30 17:58:44 +0000908 sync=SyncAuthenticPixels(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 if (sync == MagickFalse)
910 status=MagickFalse;
911 }
cristy3ed852e2009-09-05 21:47:34 +0000912 quantum_info=DestroyQuantumInfo(quantum_info);
913 if (status == MagickFalse)
914 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
cristy5f1c1ff2010-12-23 21:38:06 +0000915 if (type != UndefinedType)
cristy3ed852e2009-09-05 21:47:34 +0000916 image->type=type;
917 break;
918 }
919 case '7':
920 {
cristy3ed852e2009-09-05 21:47:34 +0000921 QuantumAny
922 range;
923
cristybb503372010-05-27 20:51:26 +0000924 size_t
cristy3ed852e2009-09-05 21:47:34 +0000925 channels;
926
927 /*
928 Convert PAM raster image to pixel packets.
929 */
930 range=GetQuantumRange(image->depth);
931 switch (quantum_type)
932 {
933 case GrayQuantum:
934 case GrayAlphaQuantum:
935 {
936 channels=1;
937 break;
938 }
939 case CMYKQuantum:
940 case CMYKAQuantum:
941 {
942 channels=4;
943 break;
944 }
945 default:
946 {
947 channels=3;
948 break;
949 }
950 }
951 if (image->matte != MagickFalse)
952 channels++;
953 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
954 quantum_info=AcquireQuantumInfo(image_info,image);
955 if (quantum_info == (QuantumInfo *) NULL)
956 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000957 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000958 {
cristy3ed852e2009-09-05 21:47:34 +0000959 MagickBooleanType
960 sync;
961
962 register const unsigned char
cristy3f2302b2010-03-11 03:16:37 +0000963 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000964
cristybb503372010-05-27 20:51:26 +0000965 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000966 x;
967
cristy4c08aed2011-07-01 19:47:50 +0000968 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000969 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000970
971 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000972 count,
973 offset;
cristy3ed852e2009-09-05 21:47:34 +0000974
975 unsigned char
976 *pixels;
977
978 if (status == MagickFalse)
979 continue;
980 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000981 {
982 count=ReadBlob(image,extent,pixels);
983 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
984 (image->previous == (Image *) NULL))
985 {
986 MagickBooleanType
987 proceed;
988
cristy34575212010-11-06 12:39:23 +0000989 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
990 row,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000991 if (proceed == MagickFalse)
992 status=MagickFalse;
993 }
994 offset=row++;
995 }
996 if (count != (ssize_t) extent)
997 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +0000998 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000999 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001000 {
1001 status=MagickFalse;
1002 continue;
1003 }
cristy3ed852e2009-09-05 21:47:34 +00001004 p=pixels;
1005 if ((image->depth == 8) || (image->depth == 16))
cristyaa740112010-03-30 17:58:44 +00001006 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +00001007 quantum_type,pixels,exception);
1008 else
1009 switch (quantum_type)
1010 {
1011 case GrayQuantum:
1012 case GrayAlphaQuantum:
1013 {
1014 if (image->depth <= 8)
1015 {
1016 unsigned char
1017 pixel;
1018
cristybb503372010-05-27 20:51:26 +00001019 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001020 {
1021 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001022 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1023 SetPixelGreen(image,GetPixelRed(image,q),q);
1024 SetPixelBlue(image,GetPixelRed(image,q),q);
1025 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001026 if (image->matte != MagickFalse)
1027 {
1028 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001029 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001030 }
cristyed231572011-07-14 02:18:59 +00001031 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001032 }
1033 }
1034 else
1035 {
1036 unsigned short
1037 pixel;
1038
cristybb503372010-05-27 20:51:26 +00001039 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001040 {
1041 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001042 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1043 SetPixelGreen(image,GetPixelRed(image,q),q);
1044 SetPixelBlue(image,GetPixelRed(image,q),q);
1045 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001046 if (image->matte != MagickFalse)
1047 {
1048 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001049 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001050 }
cristyed231572011-07-14 02:18:59 +00001051 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001052 }
1053 }
1054 break;
1055 }
1056 case CMYKQuantum:
1057 case CMYKAQuantum:
1058 {
1059 if (image->depth <= 8)
1060 {
1061 unsigned char
1062 pixel;
1063
cristybb503372010-05-27 20:51:26 +00001064 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001065 {
1066 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001067 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001068 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001069 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001070 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001071 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001072 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001073 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1074 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001075 if (image->matte != MagickFalse)
1076 {
1077 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001078 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001079 }
cristyed231572011-07-14 02:18:59 +00001080 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001081 }
1082 }
1083 else
1084 {
1085 unsigned short
1086 pixel;
1087
cristybb503372010-05-27 20:51:26 +00001088 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
1090 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001091 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001092 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001093 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001094 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001095 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001096 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001097 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1098 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001099 if (image->matte != MagickFalse)
1100 {
1101 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001102 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001103 }
cristyed231572011-07-14 02:18:59 +00001104 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001105 }
1106 }
1107 break;
1108 }
1109 default:
1110 {
1111 if (image->depth <= 8)
1112 {
1113 unsigned char
1114 pixel;
1115
cristybb503372010-05-27 20:51:26 +00001116 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001117 {
1118 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001119 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001120 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001121 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001122 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001123 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1124 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001125 if (image->matte != MagickFalse)
1126 {
1127 p=PushCharPixel(p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001128 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001129 }
cristyed231572011-07-14 02:18:59 +00001130 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001131 }
1132 }
1133 else
1134 {
1135 unsigned short
1136 pixel;
1137
cristybb503372010-05-27 20:51:26 +00001138 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001139 {
1140 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001141 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001142 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001143 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001144 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001145 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1146 SetPixelAlpha(image,OpaqueAlpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001147 if (image->matte != MagickFalse)
1148 {
1149 p=PushShortPixel(MSBEndian,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00001150 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
cristy3ed852e2009-09-05 21:47:34 +00001151 }
cristyed231572011-07-14 02:18:59 +00001152 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001153 }
1154 }
1155 break;
1156 }
1157 }
cristyaa740112010-03-30 17:58:44 +00001158 sync=SyncAuthenticPixels(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001159 if (sync == MagickFalse)
1160 status=MagickFalse;
1161 }
cristy3ed852e2009-09-05 21:47:34 +00001162 quantum_info=DestroyQuantumInfo(quantum_info);
1163 if (status == MagickFalse)
1164 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1165 SetQuantumImageType(image,quantum_type);
1166 break;
1167 }
1168 case 'F':
1169 case 'f':
1170 {
1171 /*
1172 Convert PFM raster image to pixel packets.
1173 */
1174 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1175 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1176 image->depth=32;
1177 quantum_info=AcquireQuantumInfo(image_info,image);
1178 if (quantum_info == (QuantumInfo *) NULL)
1179 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1180 status=SetQuantumDepth(image,quantum_info,32);
1181 if (status == MagickFalse)
1182 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1183 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1184 if (status == MagickFalse)
1185 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1186 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1187 fabs(quantum_scale));
1188 extent=GetQuantumExtent(image,quantum_info,quantum_type);
cristybb503372010-05-27 20:51:26 +00001189 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001190 {
cristy3ed852e2009-09-05 21:47:34 +00001191 MagickBooleanType
1192 sync;
1193
cristy4c08aed2011-07-01 19:47:50 +00001194 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001195 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001196
1197 ssize_t
cristyaff6d802011-04-26 01:46:31 +00001198 count,
1199 offset;
cristy3ed852e2009-09-05 21:47:34 +00001200
1201 size_t
1202 length;
1203
1204 unsigned char
1205 *pixels;
1206
1207 if (status == MagickFalse)
1208 continue;
1209 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001210 {
1211 count=ReadBlob(image,extent,pixels);
1212 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1213 (image->previous == (Image *) NULL))
1214 {
1215 MagickBooleanType
1216 proceed;
1217
cristy34575212010-11-06 12:39:23 +00001218 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1219 row,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001220 if (proceed == MagickFalse)
1221 status=MagickFalse;
1222 }
1223 offset=row++;
1224 }
1225 if ((size_t) count != extent)
1226 status=MagickFalse;
cristybb503372010-05-27 20:51:26 +00001227 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
cristyaa740112010-03-30 17:58:44 +00001228 image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001229 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001230 {
1231 status=MagickFalse;
1232 continue;
1233 }
cristyaa740112010-03-30 17:58:44 +00001234 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1235 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001236 if (length != extent)
1237 status=MagickFalse;
cristyaa740112010-03-30 17:58:44 +00001238 sync=SyncAuthenticPixels(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001239 if (sync == MagickFalse)
1240 status=MagickFalse;
1241 }
cristy3ed852e2009-09-05 21:47:34 +00001242 quantum_info=DestroyQuantumInfo(quantum_info);
1243 if (status == MagickFalse)
1244 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1245 SetQuantumImageType(image,quantum_type);
1246 break;
1247 }
1248 default:
1249 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1250 }
1251 if (EOFBlob(image) != MagickFalse)
1252 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1253 "UnexpectedEndOfFile","`%s'",image->filename);
1254 /*
1255 Proceed to next image.
1256 */
1257 if (image_info->number_scenes != 0)
1258 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1259 break;
1260 if ((format == '1') || (format == '2') || (format == '3'))
1261 do
1262 {
1263 /*
1264 Skip to end of line.
1265 */
1266 count=ReadBlob(image,1,(unsigned char *) &format);
1267 if (count == 0)
1268 break;
1269 if ((count != 0) && (format == 'P'))
1270 break;
1271 } while (format != '\n');
1272 count=ReadBlob(image,1,(unsigned char *) &format);
1273 if ((count == 1) && (format == 'P'))
1274 {
1275 /*
1276 Allocate next image structure.
1277 */
1278 AcquireNextImage(image_info,image);
1279 if (GetNextImageInList(image) == (Image *) NULL)
1280 {
1281 image=DestroyImageList(image);
1282 return((Image *) NULL);
1283 }
1284 image=SyncNextImageInList(image);
1285 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1286 GetBlobSize(image));
1287 if (status == MagickFalse)
1288 break;
1289 }
1290 } while ((count == 1) && (format == 'P'));
1291 (void) CloseBlob(image);
1292 return(GetFirstImageInList(image));
1293}
1294
1295/*
1296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1297% %
1298% %
1299% %
1300% R e g i s t e r P N M I m a g e %
1301% %
1302% %
1303% %
1304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1305%
1306% RegisterPNMImage() adds properties for the PNM image format to
1307% the list of supported formats. The properties include the image format
1308% tag, a method to read and/or write the format, whether the format
1309% supports the saving of more than one frame to the same file or blob,
1310% whether the format supports native in-memory I/O, and a brief
1311% description of the format.
1312%
1313% The format of the RegisterPNMImage method is:
1314%
cristybb503372010-05-27 20:51:26 +00001315% size_t RegisterPNMImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001316%
1317*/
cristybb503372010-05-27 20:51:26 +00001318ModuleExport size_t RegisterPNMImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001319{
1320 MagickInfo
1321 *entry;
1322
1323 entry=SetMagickInfo("PAM");
1324 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1325 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1326 entry->description=ConstantString("Common 2-dimensional bitmap format");
1327 entry->module=ConstantString("PNM");
1328 (void) RegisterMagickInfo(entry);
1329 entry=SetMagickInfo("PBM");
1330 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1331 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1332 entry->description=ConstantString("Portable bitmap format (black and white)");
1333 entry->module=ConstantString("PNM");
1334 (void) RegisterMagickInfo(entry);
1335 entry=SetMagickInfo("PFM");
1336 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1337 entry->encoder=(EncodeImageHandler *) WritePNMImage;
cristye96405a2010-05-19 02:24:31 +00001338 entry->endian_support=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001339 entry->description=ConstantString("Portable float format");
1340 entry->module=ConstantString("PFM");
1341 (void) RegisterMagickInfo(entry);
1342 entry=SetMagickInfo("PGM");
1343 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1344 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1345 entry->description=ConstantString("Portable graymap format (gray scale)");
1346 entry->module=ConstantString("PNM");
1347 (void) RegisterMagickInfo(entry);
1348 entry=SetMagickInfo("PNM");
1349 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1350 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1351 entry->magick=(IsImageFormatHandler *) IsPNM;
1352 entry->description=ConstantString("Portable anymap");
1353 entry->module=ConstantString("PNM");
1354 (void) RegisterMagickInfo(entry);
1355 entry=SetMagickInfo("PPM");
1356 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1357 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1358 entry->description=ConstantString("Portable pixmap format (color)");
1359 entry->module=ConstantString("PNM");
1360 (void) RegisterMagickInfo(entry);
1361 return(MagickImageCoderSignature);
1362}
1363
1364/*
1365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366% %
1367% %
1368% %
1369% U n r e g i s t e r P N M I m a g e %
1370% %
1371% %
1372% %
1373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374%
1375% UnregisterPNMImage() removes format registrations made by the
1376% PNM module from the list of supported formats.
1377%
1378% The format of the UnregisterPNMImage method is:
1379%
1380% UnregisterPNMImage(void)
1381%
1382*/
1383ModuleExport void UnregisterPNMImage(void)
1384{
1385 (void) UnregisterMagickInfo("PAM");
1386 (void) UnregisterMagickInfo("PBM");
1387 (void) UnregisterMagickInfo("PGM");
1388 (void) UnregisterMagickInfo("PNM");
1389 (void) UnregisterMagickInfo("PPM");
1390}
1391
1392/*
1393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1394% %
1395% %
1396% %
1397% W r i t e P N M I m a g e %
1398% %
1399% %
1400% %
1401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402%
cristy661b5ad2010-01-13 00:54:42 +00001403% WritePNMImage() writes an image to a file in the PNM rasterfile format.
cristy3ed852e2009-09-05 21:47:34 +00001404%
1405% The format of the WritePNMImage method is:
1406%
cristy1e178e72011-08-28 19:44:34 +00001407% MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1408% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001409%
1410% A description of each parameter follows.
1411%
1412% o image_info: the image info.
1413%
1414% o image: The image.
1415%
cristy1e178e72011-08-28 19:44:34 +00001416% o exception: return any errors or warnings in this structure.
1417%
cristy3ed852e2009-09-05 21:47:34 +00001418*/
cristy1e178e72011-08-28 19:44:34 +00001419static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1420 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001421{
1422 char
1423 buffer[MaxTextExtent],
1424 format,
1425 magick[MaxTextExtent];
1426
1427 const char
1428 *value;
1429
cristy3ed852e2009-09-05 21:47:34 +00001430 MagickBooleanType
1431 status;
1432
1433 MagickOffsetType
1434 scene;
1435
cristy4c08aed2011-07-01 19:47:50 +00001436 Quantum
1437 index;
1438
cristy3ed852e2009-09-05 21:47:34 +00001439 QuantumAny
1440 pixel;
1441
1442 QuantumInfo
1443 *quantum_info;
1444
1445 QuantumType
1446 quantum_type;
1447
cristy3ed852e2009-09-05 21:47:34 +00001448 register unsigned char
1449 *pixels,
1450 *q;
1451
cristy3ed852e2009-09-05 21:47:34 +00001452 size_t
1453 extent,
1454 packet_size;
1455
cristy95802a72010-09-05 19:07:17 +00001456 ssize_t
cristyaff6d802011-04-26 01:46:31 +00001457 count,
1458 y;
cristy95802a72010-09-05 19:07:17 +00001459
cristy3ed852e2009-09-05 21:47:34 +00001460 /*
1461 Open output image file.
1462 */
1463 assert(image_info != (const ImageInfo *) NULL);
1464 assert(image_info->signature == MagickSignature);
1465 assert(image != (Image *) NULL);
1466 assert(image->signature == MagickSignature);
1467 if (image->debug != MagickFalse)
1468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001469 assert(exception != (ExceptionInfo *) NULL);
1470 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001471 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001472 if (status == MagickFalse)
1473 return(status);
1474 scene=0;
1475 do
1476 {
1477 /*
1478 Write PNM file header.
1479 */
1480 packet_size=3;
1481 quantum_type=RGBQuantum;
1482 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1483 switch (magick[1])
1484 {
1485 case 'A':
1486 case 'a':
1487 {
1488 format='7';
1489 break;
1490 }
1491 case 'B':
1492 case 'b':
1493 {
1494 format='4';
1495 if (image_info->compression == NoCompression)
1496 format='1';
1497 break;
1498 }
1499 case 'F':
1500 case 'f':
1501 {
1502 format='F';
cristy1e178e72011-08-28 19:44:34 +00001503 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001504 format='f';
1505 break;
1506 }
1507 case 'G':
1508 case 'g':
1509 {
1510 format='5';
1511 if (image_info->compression == NoCompression)
1512 format='2';
1513 break;
1514 }
1515 case 'N':
1516 case 'n':
1517 {
1518 if ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001519 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001520 {
1521 format='5';
1522 if (image_info->compression == NoCompression)
1523 format='2';
cristy1e178e72011-08-28 19:44:34 +00001524 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001525 {
1526 format='4';
1527 if (image_info->compression == NoCompression)
1528 format='1';
1529 }
1530 break;
1531 }
1532 }
1533 default:
1534 {
1535 format='6';
1536 if (image_info->compression == NoCompression)
1537 format='3';
1538 break;
1539 }
1540 }
cristyb51dff52011-05-19 16:55:47 +00001541 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
cristy3ed852e2009-09-05 21:47:34 +00001542 (void) WriteBlobString(image,buffer);
1543 value=GetImageProperty(image,"comment");
1544 if (value != (const char *) NULL)
1545 {
1546 register const char
1547 *p;
1548
1549 /*
1550 Write comments to file.
1551 */
1552 (void) WriteBlobByte(image,'#');
1553 for (p=value; *p != '\0'; p++)
1554 {
1555 (void) WriteBlobByte(image,(unsigned char) *p);
1556 if ((*p == '\r') && (*(p+1) != '\0'))
1557 (void) WriteBlobByte(image,'#');
1558 if ((*p == '\n') && (*(p+1) != '\0'))
1559 (void) WriteBlobByte(image,'#');
1560 }
1561 (void) WriteBlobByte(image,'\n');
1562 }
1563 if (format != '7')
1564 {
cristy510d06a2011-07-06 23:43:54 +00001565 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001566 (void) TransformImageColorspace(image,RGBColorspace);
cristyb51dff52011-05-19 16:55:47 +00001567 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001568 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001569 (void) WriteBlobString(image,buffer);
1570 }
1571 else
1572 {
1573 char
1574 type[MaxTextExtent];
1575
1576 /*
1577 PAM header.
1578 */
cristyb51dff52011-05-19 16:55:47 +00001579 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001580 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1581 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001582 (void) WriteBlobString(image,buffer);
cristy1e178e72011-08-28 19:44:34 +00001583 quantum_type=GetQuantumType(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001584 switch (quantum_type)
1585 {
1586 case CMYKQuantum:
1587 case CMYKAQuantum:
1588 {
1589 packet_size=4;
1590 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1591 break;
1592 }
1593 case GrayQuantum:
1594 case GrayAlphaQuantum:
1595 {
1596 packet_size=1;
1597 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1598 break;
1599 }
1600 default:
1601 {
1602 quantum_type=RGBQuantum;
1603 if (image->matte != MagickFalse)
1604 quantum_type=RGBAQuantum;
1605 packet_size=3;
1606 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1607 break;
1608 }
1609 }
1610 if (image->matte != MagickFalse)
1611 {
1612 packet_size++;
1613 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1614 }
1615 if (image->depth > 16)
1616 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001617 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001618 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
cristy2b9582a2011-07-04 17:38:56 +00001619 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001620 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001621 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
cristy3ed852e2009-09-05 21:47:34 +00001622 type);
1623 (void) WriteBlobString(image,buffer);
1624 }
1625 /*
1626 Convert runextent encoded to PNM raster pixels.
1627 */
1628 switch (format)
1629 {
1630 case '1':
1631 {
cristy661b5ad2010-01-13 00:54:42 +00001632 unsigned char
1633 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001634
1635 /*
1636 Convert image to a PBM image.
1637 */
cristy661b5ad2010-01-13 00:54:42 +00001638 q=pixels;
cristybb503372010-05-27 20:51:26 +00001639 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001640 {
cristy4c08aed2011-07-01 19:47:50 +00001641 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001642 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001643
cristybb503372010-05-27 20:51:26 +00001644 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001645 x;
1646
cristy1e178e72011-08-28 19:44:34 +00001647 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001648 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001649 break;
cristybb503372010-05-27 20:51:26 +00001650 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001651 {
cristy4c08aed2011-07-01 19:47:50 +00001652 pixel=GetPixelIntensity(image,p);
cristy661b5ad2010-01-13 00:54:42 +00001653 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1654 '0' : '1');
1655 *q++=' ';
1656 if ((q-pixels+2) >= 80)
1657 {
1658 *q++='\n';
1659 (void) WriteBlob(image,q-pixels,pixels);
1660 q=pixels;
cristy661b5ad2010-01-13 00:54:42 +00001661 }
cristyed231572011-07-14 02:18:59 +00001662 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001663 }
1664 if (image->previous == (Image *) NULL)
1665 {
cristycee97112010-05-28 00:44:52 +00001666 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1667 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001668 if (status == MagickFalse)
1669 break;
1670 }
1671 }
cristy661b5ad2010-01-13 00:54:42 +00001672 if (q != pixels)
1673 {
1674 *q++='\n';
1675 (void) WriteBlob(image,q-pixels,pixels);
1676 }
cristy3ed852e2009-09-05 21:47:34 +00001677 break;
1678 }
1679 case '2':
1680 {
cristy661b5ad2010-01-13 00:54:42 +00001681 unsigned char
1682 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001683
1684 /*
1685 Convert image to a PGM image.
1686 */
1687 if (image->depth <= 8)
1688 (void) WriteBlobString(image,"255\n");
1689 else
1690 (void) WriteBlobString(image,"65535\n");
cristy661b5ad2010-01-13 00:54:42 +00001691 q=pixels;
cristybb503372010-05-27 20:51:26 +00001692 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001693 {
cristy4c08aed2011-07-01 19:47:50 +00001694 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001695 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001696
cristybb503372010-05-27 20:51:26 +00001697 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001698 x;
1699
cristy1e178e72011-08-28 19:44:34 +00001700 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001701 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001702 break;
cristybb503372010-05-27 20:51:26 +00001703 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001704 {
cristy4c08aed2011-07-01 19:47:50 +00001705 index=GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00001706 if (image->depth <= 8)
cristyb51dff52011-05-19 16:55:47 +00001707 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
cristy3ed852e2009-09-05 21:47:34 +00001708 ScaleQuantumToChar(index));
1709 else
cristyb51dff52011-05-19 16:55:47 +00001710 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
cristy3ed852e2009-09-05 21:47:34 +00001711 ScaleQuantumToShort(index));
cristy661b5ad2010-01-13 00:54:42 +00001712 extent=(size_t) count;
1713 (void) strncpy((char *) q,buffer,extent);
1714 q+=extent;
1715 if ((q-pixels+extent) >= 80)
1716 {
1717 *q++='\n';
1718 (void) WriteBlob(image,q-pixels,pixels);
1719 q=pixels;
1720 }
cristyed231572011-07-14 02:18:59 +00001721 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001722 }
1723 if (image->previous == (Image *) NULL)
1724 {
cristycee97112010-05-28 00:44:52 +00001725 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1726 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001727 if (status == MagickFalse)
1728 break;
1729 }
1730 }
cristy661b5ad2010-01-13 00:54:42 +00001731 if (q != pixels)
1732 {
1733 *q++='\n';
1734 (void) WriteBlob(image,q-pixels,pixels);
1735 }
cristy3ed852e2009-09-05 21:47:34 +00001736 break;
1737 }
1738 case '3':
1739 {
cristy661b5ad2010-01-13 00:54:42 +00001740 unsigned char
1741 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001742
1743 /*
1744 Convert image to a PNM image.
1745 */
1746 if (image->depth <= 8)
1747 (void) WriteBlobString(image,"255\n");
1748 else
1749 (void) WriteBlobString(image,"65535\n");
cristy661b5ad2010-01-13 00:54:42 +00001750 q=pixels;
cristybb503372010-05-27 20:51:26 +00001751 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001752 {
cristy4c08aed2011-07-01 19:47:50 +00001753 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001754 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001755
cristybb503372010-05-27 20:51:26 +00001756 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001757 x;
1758
cristy1e178e72011-08-28 19:44:34 +00001759 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001760 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001761 break;
cristybb503372010-05-27 20:51:26 +00001762 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001763 {
1764 if (image->depth <= 8)
cristyb51dff52011-05-19 16:55:47 +00001765 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
cristy4c08aed2011-07-01 19:47:50 +00001766 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1767 ScaleQuantumToChar(GetPixelGreen(image,p)),
1768 ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001769 else
cristyb51dff52011-05-19 16:55:47 +00001770 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
cristy4c08aed2011-07-01 19:47:50 +00001771 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1772 ScaleQuantumToShort(GetPixelGreen(image,p)),
1773 ScaleQuantumToShort(GetPixelBlue(image,p)));
cristy661b5ad2010-01-13 00:54:42 +00001774 extent=(size_t) count;
1775 (void) strncpy((char *) q,buffer,extent);
1776 q+=extent;
1777 if ((q-pixels+extent) >= 80)
1778 {
1779 *q++='\n';
1780 (void) WriteBlob(image,q-pixels,pixels);
1781 q=pixels;
1782 }
cristyed231572011-07-14 02:18:59 +00001783 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001784 }
1785 if (image->previous == (Image *) NULL)
1786 {
cristycee97112010-05-28 00:44:52 +00001787 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1788 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001789 if (status == MagickFalse)
1790 break;
1791 }
1792 }
cristy661b5ad2010-01-13 00:54:42 +00001793 if (q != pixels)
1794 {
1795 *q++='\n';
1796 (void) WriteBlob(image,q-pixels,pixels);
1797 }
cristy3ed852e2009-09-05 21:47:34 +00001798 break;
1799 }
1800 case '4':
1801 {
1802 /*
1803 Convert image to a PBM image.
1804 */
1805 image->depth=1;
1806 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1807 if (quantum_info == (QuantumInfo *) NULL)
1808 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1809 quantum_info->min_is_white=MagickTrue;
1810 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001811 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001812 {
cristy4c08aed2011-07-01 19:47:50 +00001813 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001814 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001815
cristy1e178e72011-08-28 19:44:34 +00001816 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001817 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001818 break;
cristy4c08aed2011-07-01 19:47:50 +00001819 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00001820 GrayQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001821 count=WriteBlob(image,extent,pixels);
1822 if (count != (ssize_t) extent)
1823 break;
1824 if (image->previous == (Image *) NULL)
1825 {
cristycee97112010-05-28 00:44:52 +00001826 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1827 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001828 if (status == MagickFalse)
1829 break;
1830 }
1831 }
1832 quantum_info=DestroyQuantumInfo(quantum_info);
1833 break;
1834 }
1835 case '5':
1836 {
1837 QuantumAny
1838 range;
1839
1840 /*
1841 Convert image to a PGM image.
1842 */
1843 if (image->depth > 8)
1844 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001845 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristy2b9582a2011-07-04 17:38:56 +00001846 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001847 (void) WriteBlobString(image,buffer);
1848 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1849 if (quantum_info == (QuantumInfo *) NULL)
1850 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1851 quantum_info->min_is_white=MagickTrue;
1852 pixels=GetQuantumPixels(quantum_info);
1853 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1854 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00001855 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001856 {
cristy4c08aed2011-07-01 19:47:50 +00001857 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001858 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001859
cristybb503372010-05-27 20:51:26 +00001860 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001861 x;
1862
cristy1e178e72011-08-28 19:44:34 +00001863 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001864 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001865 break;
1866 q=pixels;
1867 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00001868 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00001869 GrayQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001870 else
1871 {
1872 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00001873 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001874 {
cristy4c08aed2011-07-01 19:47:50 +00001875 if (IsPixelGray(image,p) == MagickFalse)
1876 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001877 else
1878 {
1879 if (image->depth == 8)
cristy4c08aed2011-07-01 19:47:50 +00001880 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001881 else
cristy4c08aed2011-07-01 19:47:50 +00001882 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001883 }
1884 q=PopCharPixel((unsigned char) pixel,q);
cristyed231572011-07-14 02:18:59 +00001885 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001886 }
1887 else
cristybb503372010-05-27 20:51:26 +00001888 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001889 {
cristy4c08aed2011-07-01 19:47:50 +00001890 if (IsPixelGray(image,p) == MagickFalse)
1891 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001892 else
1893 {
1894 if (image->depth == 16)
cristy4c08aed2011-07-01 19:47:50 +00001895 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001896 else
cristy4c08aed2011-07-01 19:47:50 +00001897 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001898 }
1899 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristyed231572011-07-14 02:18:59 +00001900 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001901 }
1902 extent=(size_t) (q-pixels);
1903 }
1904 count=WriteBlob(image,extent,pixels);
1905 if (count != (ssize_t) extent)
1906 break;
1907 if (image->previous == (Image *) NULL)
1908 {
cristycee97112010-05-28 00:44:52 +00001909 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1910 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001911 if (status == MagickFalse)
1912 break;
1913 }
1914 }
1915 quantum_info=DestroyQuantumInfo(quantum_info);
1916 break;
1917 }
1918 case '6':
1919 {
1920 QuantumAny
1921 range;
1922
1923 /*
1924 Convert image to a PNM image.
1925 */
1926 if (image->depth > 8)
1927 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001928 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristy2b9582a2011-07-04 17:38:56 +00001929 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001930 (void) WriteBlobString(image,buffer);
1931 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1932 if (quantum_info == (QuantumInfo *) NULL)
1933 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1934 pixels=GetQuantumPixels(quantum_info);
1935 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1936 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00001937 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001938 {
cristy4c08aed2011-07-01 19:47:50 +00001939 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001940 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001941
cristybb503372010-05-27 20:51:26 +00001942 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001943 x;
1944
cristy1e178e72011-08-28 19:44:34 +00001945 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001946 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001947 break;
1948 q=pixels;
1949 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00001950 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00001951 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001952 else
1953 {
1954 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00001955 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001956 {
cristy4c08aed2011-07-01 19:47:50 +00001957 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001958 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001959 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001960 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001961 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001962 q=PopCharPixel((unsigned char) pixel,q);
cristyed231572011-07-14 02:18:59 +00001963 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001964 }
1965 else
cristybb503372010-05-27 20:51:26 +00001966 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001967 {
cristy4c08aed2011-07-01 19:47:50 +00001968 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001969 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001970 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001971 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001972 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001973 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristyed231572011-07-14 02:18:59 +00001974 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001975 }
1976 extent=(size_t) (q-pixels);
1977 }
1978 count=WriteBlob(image,extent,pixels);
1979 if (count != (ssize_t) extent)
1980 break;
1981 if (image->previous == (Image *) NULL)
1982 {
cristycee97112010-05-28 00:44:52 +00001983 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1984 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001985 if (status == MagickFalse)
1986 break;
1987 }
1988 }
1989 quantum_info=DestroyQuantumInfo(quantum_info);
1990 break;
1991 }
1992 case '7':
1993 {
1994 QuantumAny
1995 range;
1996
1997 /*
1998 Convert image to a PAM.
1999 */
2000 if (image->depth > 16)
2001 image->depth=16;
2002 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2003 pixels=GetQuantumPixels(quantum_info);
2004 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00002005 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002006 {
cristy4c08aed2011-07-01 19:47:50 +00002007 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002008 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002009
cristybb503372010-05-27 20:51:26 +00002010 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002011 x;
2012
cristy1e178e72011-08-28 19:44:34 +00002013 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002014 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002015 break;
cristy3ed852e2009-09-05 21:47:34 +00002016 q=pixels;
2017 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00002018 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002019 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002020 else
2021 {
2022 switch (quantum_type)
2023 {
2024 case GrayQuantum:
2025 case GrayAlphaQuantum:
2026 {
2027 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002028 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002029 {
cristy4c08aed2011-07-01 19:47:50 +00002030 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002031 q=PopCharPixel((unsigned char) pixel,q);
2032 if (image->matte != MagickFalse)
2033 {
cristyaff6d802011-04-26 01:46:31 +00002034 pixel=(unsigned char) ScaleQuantumToAny(
cristy4c08aed2011-07-01 19:47:50 +00002035 GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002036 q=PopCharPixel((unsigned char) pixel,q);
2037 }
cristyed231572011-07-14 02:18:59 +00002038 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002039 }
2040 else
cristybb503372010-05-27 20:51:26 +00002041 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002042 {
cristy4c08aed2011-07-01 19:47:50 +00002043 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002044 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2045 if (image->matte != MagickFalse)
2046 {
cristyaff6d802011-04-26 01:46:31 +00002047 pixel=(unsigned char) ScaleQuantumToAny(
cristy4c08aed2011-07-01 19:47:50 +00002048 GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002049 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2050 }
cristyed231572011-07-14 02:18:59 +00002051 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002052 }
2053 break;
2054 }
2055 case CMYKQuantum:
2056 case CMYKAQuantum:
2057 {
2058 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002059 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002060 {
cristy4c08aed2011-07-01 19:47:50 +00002061 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002062 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002063 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002064 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002065 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002066 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002067 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002068 q=PopCharPixel((unsigned char) pixel,q);
2069 if (image->matte != MagickFalse)
2070 {
cristy4c08aed2011-07-01 19:47:50 +00002071 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002072 q=PopCharPixel((unsigned char) pixel,q);
2073 }
cristyed231572011-07-14 02:18:59 +00002074 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002075 }
2076 else
cristybb503372010-05-27 20:51:26 +00002077 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002078 {
cristy4c08aed2011-07-01 19:47:50 +00002079 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002080 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002081 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002082 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002083 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002084 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002085 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002086 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2087 if (image->matte != MagickFalse)
2088 {
cristy4c08aed2011-07-01 19:47:50 +00002089 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002090 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2091 }
cristyed231572011-07-14 02:18:59 +00002092 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002093 }
2094 break;
2095 }
2096 default:
2097 {
2098 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002099 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002100 {
cristy4c08aed2011-07-01 19:47:50 +00002101 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002102 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002103 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002104 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002105 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002106 q=PopCharPixel((unsigned char) pixel,q);
2107 if (image->matte != MagickFalse)
2108 {
cristy4c08aed2011-07-01 19:47:50 +00002109 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002110 q=PopCharPixel((unsigned char) pixel,q);
2111 }
cristyed231572011-07-14 02:18:59 +00002112 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002113 }
2114 else
cristybb503372010-05-27 20:51:26 +00002115 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002116 {
cristy4c08aed2011-07-01 19:47:50 +00002117 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002118 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002119 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002120 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002121 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002122 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2123 if (image->matte != MagickFalse)
2124 {
cristy4c08aed2011-07-01 19:47:50 +00002125 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002126 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2127 }
cristyed231572011-07-14 02:18:59 +00002128 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002129 }
2130 break;
2131 }
2132 }
2133 extent=(size_t) (q-pixels);
2134 }
2135 count=WriteBlob(image,extent,pixels);
2136 if (count != (ssize_t) extent)
2137 break;
2138 if (image->previous == (Image *) NULL)
2139 {
cristycee97112010-05-28 00:44:52 +00002140 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2141 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002142 if (status == MagickFalse)
2143 break;
2144 }
2145 }
2146 quantum_info=DestroyQuantumInfo(quantum_info);
2147 break;
2148 }
2149 case 'F':
2150 case 'f':
2151 {
2152 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2153 "-1.0\n");
2154 image->depth=32;
2155 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2156 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2157 if (quantum_info == (QuantumInfo *) NULL)
2158 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2159 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2160 if (status == MagickFalse)
2161 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2162 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00002163 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00002164 {
cristy4c08aed2011-07-01 19:47:50 +00002165 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002166 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002167
cristy1e178e72011-08-28 19:44:34 +00002168 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002169 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002170 break;
cristy4c08aed2011-07-01 19:47:50 +00002171 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002172 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002173 (void) WriteBlob(image,extent,pixels);
2174 if (image->previous == (Image *) NULL)
2175 {
cristycee97112010-05-28 00:44:52 +00002176 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2177 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002178 if (status == MagickFalse)
2179 break;
2180 }
2181 }
2182 quantum_info=DestroyQuantumInfo(quantum_info);
2183 break;
2184 }
2185 }
2186 if (GetNextImageInList(image) == (Image *) NULL)
2187 break;
2188 image=SyncNextImageInList(image);
2189 status=SetImageProgress(image,SaveImagesTag,scene++,
2190 GetImageListLength(image));
2191 if (status == MagickFalse)
2192 break;
2193 } while (image_info->adjoin != MagickFalse);
2194 (void) CloseBlob(image);
2195 return(MagickTrue);
2196}