blob: 1f3e764c04660e0748b12670a2cbfb1b8123fb6a [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
73 WritePNMImage(const ImageInfo *,Image *);
74
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);
cristy4c08aed2011-07-01 19:47:50 +0000440 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +0000551 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +0000639 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +0000715 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +0000823 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +0000999 if (q == (const 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);
cristy4c08aed2011-07-01 19:47:50 +00001229 if (q == (const 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%
1407% MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1408%
1409% A description of each parameter follows.
1410%
1411% o image_info: the image info.
1412%
1413% o image: The image.
1414%
1415*/
1416static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1417{
1418 char
1419 buffer[MaxTextExtent],
1420 format,
1421 magick[MaxTextExtent];
1422
1423 const char
1424 *value;
1425
cristy3ed852e2009-09-05 21:47:34 +00001426 MagickBooleanType
1427 status;
1428
1429 MagickOffsetType
1430 scene;
1431
cristy4c08aed2011-07-01 19:47:50 +00001432 Quantum
1433 index;
1434
cristy3ed852e2009-09-05 21:47:34 +00001435 QuantumAny
1436 pixel;
1437
1438 QuantumInfo
1439 *quantum_info;
1440
1441 QuantumType
1442 quantum_type;
1443
cristy3ed852e2009-09-05 21:47:34 +00001444 register unsigned char
1445 *pixels,
1446 *q;
1447
cristy3ed852e2009-09-05 21:47:34 +00001448 size_t
1449 extent,
1450 packet_size;
1451
cristy95802a72010-09-05 19:07:17 +00001452 ssize_t
cristyaff6d802011-04-26 01:46:31 +00001453 count,
1454 y;
cristy95802a72010-09-05 19:07:17 +00001455
cristy3ed852e2009-09-05 21:47:34 +00001456 /*
1457 Open output image file.
1458 */
1459 assert(image_info != (const ImageInfo *) NULL);
1460 assert(image_info->signature == MagickSignature);
1461 assert(image != (Image *) NULL);
1462 assert(image->signature == MagickSignature);
1463 if (image->debug != MagickFalse)
1464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1465 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1466 if (status == MagickFalse)
1467 return(status);
1468 scene=0;
1469 do
1470 {
1471 /*
1472 Write PNM file header.
1473 */
1474 packet_size=3;
1475 quantum_type=RGBQuantum;
1476 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1477 switch (magick[1])
1478 {
1479 case 'A':
1480 case 'a':
1481 {
1482 format='7';
1483 break;
1484 }
1485 case 'B':
1486 case 'b':
1487 {
1488 format='4';
1489 if (image_info->compression == NoCompression)
1490 format='1';
1491 break;
1492 }
1493 case 'F':
1494 case 'f':
1495 {
1496 format='F';
cristy4c08aed2011-07-01 19:47:50 +00001497 if (IsImageGray(image,&image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001498 format='f';
1499 break;
1500 }
1501 case 'G':
1502 case 'g':
1503 {
1504 format='5';
1505 if (image_info->compression == NoCompression)
1506 format='2';
1507 break;
1508 }
1509 case 'N':
1510 case 'n':
1511 {
1512 if ((image_info->type != TrueColorType) &&
cristy4c08aed2011-07-01 19:47:50 +00001513 (IsImageGray(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001514 {
1515 format='5';
1516 if (image_info->compression == NoCompression)
1517 format='2';
cristy4c08aed2011-07-01 19:47:50 +00001518 if (IsImageMonochrome(image,&image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001519 {
1520 format='4';
1521 if (image_info->compression == NoCompression)
1522 format='1';
1523 }
1524 break;
1525 }
1526 }
1527 default:
1528 {
1529 format='6';
1530 if (image_info->compression == NoCompression)
1531 format='3';
1532 break;
1533 }
1534 }
cristyb51dff52011-05-19 16:55:47 +00001535 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
cristy3ed852e2009-09-05 21:47:34 +00001536 (void) WriteBlobString(image,buffer);
1537 value=GetImageProperty(image,"comment");
1538 if (value != (const char *) NULL)
1539 {
1540 register const char
1541 *p;
1542
1543 /*
1544 Write comments to file.
1545 */
1546 (void) WriteBlobByte(image,'#');
1547 for (p=value; *p != '\0'; p++)
1548 {
1549 (void) WriteBlobByte(image,(unsigned char) *p);
1550 if ((*p == '\r') && (*(p+1) != '\0'))
1551 (void) WriteBlobByte(image,'#');
1552 if ((*p == '\n') && (*(p+1) != '\0'))
1553 (void) WriteBlobByte(image,'#');
1554 }
1555 (void) WriteBlobByte(image,'\n');
1556 }
1557 if (format != '7')
1558 {
cristy510d06a2011-07-06 23:43:54 +00001559 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001560 (void) TransformImageColorspace(image,RGBColorspace);
cristyb51dff52011-05-19 16:55:47 +00001561 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001562 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001563 (void) WriteBlobString(image,buffer);
1564 }
1565 else
1566 {
1567 char
1568 type[MaxTextExtent];
1569
1570 /*
1571 PAM header.
1572 */
cristyb51dff52011-05-19 16:55:47 +00001573 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001574 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1575 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001576 (void) WriteBlobString(image,buffer);
1577 quantum_type=GetQuantumType(image,&image->exception);
1578 switch (quantum_type)
1579 {
1580 case CMYKQuantum:
1581 case CMYKAQuantum:
1582 {
1583 packet_size=4;
1584 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1585 break;
1586 }
1587 case GrayQuantum:
1588 case GrayAlphaQuantum:
1589 {
1590 packet_size=1;
1591 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1592 break;
1593 }
1594 default:
1595 {
1596 quantum_type=RGBQuantum;
1597 if (image->matte != MagickFalse)
1598 quantum_type=RGBAQuantum;
1599 packet_size=3;
1600 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1601 break;
1602 }
1603 }
1604 if (image->matte != MagickFalse)
1605 {
1606 packet_size++;
1607 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1608 }
1609 if (image->depth > 16)
1610 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001611 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001612 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
cristy2b9582a2011-07-04 17:38:56 +00001613 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001614 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001615 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
cristy3ed852e2009-09-05 21:47:34 +00001616 type);
1617 (void) WriteBlobString(image,buffer);
1618 }
1619 /*
1620 Convert runextent encoded to PNM raster pixels.
1621 */
1622 switch (format)
1623 {
1624 case '1':
1625 {
cristy661b5ad2010-01-13 00:54:42 +00001626 unsigned char
1627 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001628
1629 /*
1630 Convert image to a PBM image.
1631 */
cristy661b5ad2010-01-13 00:54:42 +00001632 q=pixels;
cristybb503372010-05-27 20:51:26 +00001633 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001634 {
cristy4c08aed2011-07-01 19:47:50 +00001635 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001636 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001637
cristybb503372010-05-27 20:51:26 +00001638 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001639 x;
1640
1641 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001642 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001643 break;
cristybb503372010-05-27 20:51:26 +00001644 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001645 {
cristy4c08aed2011-07-01 19:47:50 +00001646 pixel=GetPixelIntensity(image,p);
cristy661b5ad2010-01-13 00:54:42 +00001647 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1648 '0' : '1');
1649 *q++=' ';
1650 if ((q-pixels+2) >= 80)
1651 {
1652 *q++='\n';
1653 (void) WriteBlob(image,q-pixels,pixels);
1654 q=pixels;
cristy661b5ad2010-01-13 00:54:42 +00001655 }
cristyed231572011-07-14 02:18:59 +00001656 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001657 }
1658 if (image->previous == (Image *) NULL)
1659 {
cristycee97112010-05-28 00:44:52 +00001660 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1661 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001662 if (status == MagickFalse)
1663 break;
1664 }
1665 }
cristy661b5ad2010-01-13 00:54:42 +00001666 if (q != pixels)
1667 {
1668 *q++='\n';
1669 (void) WriteBlob(image,q-pixels,pixels);
1670 }
cristy3ed852e2009-09-05 21:47:34 +00001671 break;
1672 }
1673 case '2':
1674 {
cristy661b5ad2010-01-13 00:54:42 +00001675 unsigned char
1676 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001677
1678 /*
1679 Convert image to a PGM image.
1680 */
1681 if (image->depth <= 8)
1682 (void) WriteBlobString(image,"255\n");
1683 else
1684 (void) WriteBlobString(image,"65535\n");
cristy661b5ad2010-01-13 00:54:42 +00001685 q=pixels;
cristybb503372010-05-27 20:51:26 +00001686 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001687 {
cristy4c08aed2011-07-01 19:47:50 +00001688 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001689 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001690
cristybb503372010-05-27 20:51:26 +00001691 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001692 x;
1693
1694 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001695 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001696 break;
cristybb503372010-05-27 20:51:26 +00001697 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001698 {
cristy4c08aed2011-07-01 19:47:50 +00001699 index=GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00001700 if (image->depth <= 8)
cristyb51dff52011-05-19 16:55:47 +00001701 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
cristy3ed852e2009-09-05 21:47:34 +00001702 ScaleQuantumToChar(index));
1703 else
cristyb51dff52011-05-19 16:55:47 +00001704 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
cristy3ed852e2009-09-05 21:47:34 +00001705 ScaleQuantumToShort(index));
cristy661b5ad2010-01-13 00:54:42 +00001706 extent=(size_t) count;
1707 (void) strncpy((char *) q,buffer,extent);
1708 q+=extent;
1709 if ((q-pixels+extent) >= 80)
1710 {
1711 *q++='\n';
1712 (void) WriteBlob(image,q-pixels,pixels);
1713 q=pixels;
1714 }
cristyed231572011-07-14 02:18:59 +00001715 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001716 }
1717 if (image->previous == (Image *) NULL)
1718 {
cristycee97112010-05-28 00:44:52 +00001719 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1720 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001721 if (status == MagickFalse)
1722 break;
1723 }
1724 }
cristy661b5ad2010-01-13 00:54:42 +00001725 if (q != pixels)
1726 {
1727 *q++='\n';
1728 (void) WriteBlob(image,q-pixels,pixels);
1729 }
cristy3ed852e2009-09-05 21:47:34 +00001730 break;
1731 }
1732 case '3':
1733 {
cristy661b5ad2010-01-13 00:54:42 +00001734 unsigned char
1735 pixels[2048];
cristy3ed852e2009-09-05 21:47:34 +00001736
1737 /*
1738 Convert image to a PNM image.
1739 */
1740 if (image->depth <= 8)
1741 (void) WriteBlobString(image,"255\n");
1742 else
1743 (void) WriteBlobString(image,"65535\n");
cristy661b5ad2010-01-13 00:54:42 +00001744 q=pixels;
cristybb503372010-05-27 20:51:26 +00001745 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001746 {
cristy4c08aed2011-07-01 19:47:50 +00001747 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001748 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001749
cristybb503372010-05-27 20:51:26 +00001750 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001751 x;
1752
1753 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001754 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001755 break;
cristybb503372010-05-27 20:51:26 +00001756 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001757 {
1758 if (image->depth <= 8)
cristyb51dff52011-05-19 16:55:47 +00001759 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
cristy4c08aed2011-07-01 19:47:50 +00001760 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1761 ScaleQuantumToChar(GetPixelGreen(image,p)),
1762 ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001763 else
cristyb51dff52011-05-19 16:55:47 +00001764 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
cristy4c08aed2011-07-01 19:47:50 +00001765 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1766 ScaleQuantumToShort(GetPixelGreen(image,p)),
1767 ScaleQuantumToShort(GetPixelBlue(image,p)));
cristy661b5ad2010-01-13 00:54:42 +00001768 extent=(size_t) count;
1769 (void) strncpy((char *) q,buffer,extent);
1770 q+=extent;
1771 if ((q-pixels+extent) >= 80)
1772 {
1773 *q++='\n';
1774 (void) WriteBlob(image,q-pixels,pixels);
1775 q=pixels;
1776 }
cristyed231572011-07-14 02:18:59 +00001777 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001778 }
1779 if (image->previous == (Image *) NULL)
1780 {
cristycee97112010-05-28 00:44:52 +00001781 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1782 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001783 if (status == MagickFalse)
1784 break;
1785 }
1786 }
cristy661b5ad2010-01-13 00:54:42 +00001787 if (q != pixels)
1788 {
1789 *q++='\n';
1790 (void) WriteBlob(image,q-pixels,pixels);
1791 }
cristy3ed852e2009-09-05 21:47:34 +00001792 break;
1793 }
1794 case '4':
1795 {
1796 /*
1797 Convert image to a PBM image.
1798 */
1799 image->depth=1;
1800 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1801 if (quantum_info == (QuantumInfo *) NULL)
1802 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1803 quantum_info->min_is_white=MagickTrue;
1804 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001805 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001806 {
cristy4c08aed2011-07-01 19:47:50 +00001807 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001808 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001809
1810 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001811 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001812 break;
cristy4c08aed2011-07-01 19:47:50 +00001813 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1814 GrayQuantum,pixels,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001815 count=WriteBlob(image,extent,pixels);
1816 if (count != (ssize_t) extent)
1817 break;
1818 if (image->previous == (Image *) NULL)
1819 {
cristycee97112010-05-28 00:44:52 +00001820 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1821 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001822 if (status == MagickFalse)
1823 break;
1824 }
1825 }
1826 quantum_info=DestroyQuantumInfo(quantum_info);
1827 break;
1828 }
1829 case '5':
1830 {
1831 QuantumAny
1832 range;
1833
1834 /*
1835 Convert image to a PGM image.
1836 */
1837 if (image->depth > 8)
1838 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001839 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristy2b9582a2011-07-04 17:38:56 +00001840 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001841 (void) WriteBlobString(image,buffer);
1842 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1843 if (quantum_info == (QuantumInfo *) NULL)
1844 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1845 quantum_info->min_is_white=MagickTrue;
1846 pixels=GetQuantumPixels(quantum_info);
1847 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1848 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00001849 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001850 {
cristy4c08aed2011-07-01 19:47:50 +00001851 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001852 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001853
cristybb503372010-05-27 20:51:26 +00001854 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001855 x;
1856
1857 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001858 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001859 break;
1860 q=pixels;
1861 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00001862 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1863 GrayQuantum,pixels,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001864 else
1865 {
1866 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00001867 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001868 {
cristy4c08aed2011-07-01 19:47:50 +00001869 if (IsPixelGray(image,p) == MagickFalse)
1870 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001871 else
1872 {
1873 if (image->depth == 8)
cristy4c08aed2011-07-01 19:47:50 +00001874 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001875 else
cristy4c08aed2011-07-01 19:47:50 +00001876 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001877 }
1878 q=PopCharPixel((unsigned char) pixel,q);
cristyed231572011-07-14 02:18:59 +00001879 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001880 }
1881 else
cristybb503372010-05-27 20:51:26 +00001882 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001883 {
cristy4c08aed2011-07-01 19:47:50 +00001884 if (IsPixelGray(image,p) == MagickFalse)
1885 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001886 else
1887 {
1888 if (image->depth == 16)
cristy4c08aed2011-07-01 19:47:50 +00001889 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001890 else
cristy4c08aed2011-07-01 19:47:50 +00001891 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001892 }
1893 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristyed231572011-07-14 02:18:59 +00001894 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001895 }
1896 extent=(size_t) (q-pixels);
1897 }
1898 count=WriteBlob(image,extent,pixels);
1899 if (count != (ssize_t) extent)
1900 break;
1901 if (image->previous == (Image *) NULL)
1902 {
cristycee97112010-05-28 00:44:52 +00001903 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1904 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001905 if (status == MagickFalse)
1906 break;
1907 }
1908 }
1909 quantum_info=DestroyQuantumInfo(quantum_info);
1910 break;
1911 }
1912 case '6':
1913 {
1914 QuantumAny
1915 range;
1916
1917 /*
1918 Convert image to a PNM image.
1919 */
1920 if (image->depth > 8)
1921 image->depth=16;
cristyb51dff52011-05-19 16:55:47 +00001922 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristy2b9582a2011-07-04 17:38:56 +00001923 ((MagickOffsetType) GetQuantumRange(image->depth)));
cristy3ed852e2009-09-05 21:47:34 +00001924 (void) WriteBlobString(image,buffer);
1925 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1926 if (quantum_info == (QuantumInfo *) NULL)
1927 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1928 pixels=GetQuantumPixels(quantum_info);
1929 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1930 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00001931 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001932 {
cristy4c08aed2011-07-01 19:47:50 +00001933 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001934 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001935
cristybb503372010-05-27 20:51:26 +00001936 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001937 x;
1938
1939 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001940 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001941 break;
1942 q=pixels;
1943 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00001944 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1945 quantum_type,pixels,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001946 else
1947 {
1948 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00001949 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001950 {
cristy4c08aed2011-07-01 19:47:50 +00001951 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001952 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001953 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001954 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001955 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001956 q=PopCharPixel((unsigned char) pixel,q);
cristyed231572011-07-14 02:18:59 +00001957 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001958 }
1959 else
cristybb503372010-05-27 20:51:26 +00001960 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001961 {
cristy4c08aed2011-07-01 19:47:50 +00001962 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001963 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001964 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001965 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00001966 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00001967 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristyed231572011-07-14 02:18:59 +00001968 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001969 }
1970 extent=(size_t) (q-pixels);
1971 }
1972 count=WriteBlob(image,extent,pixels);
1973 if (count != (ssize_t) extent)
1974 break;
1975 if (image->previous == (Image *) NULL)
1976 {
cristycee97112010-05-28 00:44:52 +00001977 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1978 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001979 if (status == MagickFalse)
1980 break;
1981 }
1982 }
1983 quantum_info=DestroyQuantumInfo(quantum_info);
1984 break;
1985 }
1986 case '7':
1987 {
1988 QuantumAny
1989 range;
1990
1991 /*
1992 Convert image to a PAM.
1993 */
1994 if (image->depth > 16)
1995 image->depth=16;
1996 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1997 pixels=GetQuantumPixels(quantum_info);
1998 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +00001999 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002000 {
cristy4c08aed2011-07-01 19:47:50 +00002001 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002002 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002003
cristybb503372010-05-27 20:51:26 +00002004 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002005 x;
2006
2007 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00002008 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002009 break;
cristy3ed852e2009-09-05 21:47:34 +00002010 q=pixels;
2011 if ((image->depth == 8) || (image->depth == 16))
cristy4c08aed2011-07-01 19:47:50 +00002012 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2013 quantum_type,pixels,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002014 else
2015 {
2016 switch (quantum_type)
2017 {
2018 case GrayQuantum:
2019 case GrayAlphaQuantum:
2020 {
2021 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002022 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002023 {
cristy4c08aed2011-07-01 19:47:50 +00002024 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002025 q=PopCharPixel((unsigned char) pixel,q);
2026 if (image->matte != MagickFalse)
2027 {
cristyaff6d802011-04-26 01:46:31 +00002028 pixel=(unsigned char) ScaleQuantumToAny(
cristy4c08aed2011-07-01 19:47:50 +00002029 GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002030 q=PopCharPixel((unsigned char) pixel,q);
2031 }
cristyed231572011-07-14 02:18:59 +00002032 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002033 }
2034 else
cristybb503372010-05-27 20:51:26 +00002035 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002036 {
cristy4c08aed2011-07-01 19:47:50 +00002037 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002038 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2039 if (image->matte != MagickFalse)
2040 {
cristyaff6d802011-04-26 01:46:31 +00002041 pixel=(unsigned char) ScaleQuantumToAny(
cristy4c08aed2011-07-01 19:47:50 +00002042 GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002043 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2044 }
cristyed231572011-07-14 02:18:59 +00002045 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002046 }
2047 break;
2048 }
2049 case CMYKQuantum:
2050 case CMYKAQuantum:
2051 {
2052 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002053 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002054 {
cristy4c08aed2011-07-01 19:47:50 +00002055 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002056 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002057 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002058 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002059 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002060 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002061 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002062 q=PopCharPixel((unsigned char) pixel,q);
2063 if (image->matte != MagickFalse)
2064 {
cristy4c08aed2011-07-01 19:47:50 +00002065 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002066 q=PopCharPixel((unsigned char) pixel,q);
2067 }
cristyed231572011-07-14 02:18:59 +00002068 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002069 }
2070 else
cristybb503372010-05-27 20:51:26 +00002071 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002072 {
cristy4c08aed2011-07-01 19:47:50 +00002073 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002074 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002075 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002076 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002077 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002078 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002079 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002080 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2081 if (image->matte != MagickFalse)
2082 {
cristy4c08aed2011-07-01 19:47:50 +00002083 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002084 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2085 }
cristyed231572011-07-14 02:18:59 +00002086 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002087 }
2088 break;
2089 }
2090 default:
2091 {
2092 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +00002093 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002094 {
cristy4c08aed2011-07-01 19:47:50 +00002095 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002096 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002097 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002098 q=PopCharPixel((unsigned char) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002099 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002100 q=PopCharPixel((unsigned char) pixel,q);
2101 if (image->matte != MagickFalse)
2102 {
cristy4c08aed2011-07-01 19:47:50 +00002103 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002104 q=PopCharPixel((unsigned char) pixel,q);
2105 }
cristyed231572011-07-14 02:18:59 +00002106 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002107 }
2108 else
cristybb503372010-05-27 20:51:26 +00002109 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002110 {
cristy4c08aed2011-07-01 19:47:50 +00002111 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002112 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002113 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002114 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00002115 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002116 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2117 if (image->matte != MagickFalse)
2118 {
cristy4c08aed2011-07-01 19:47:50 +00002119 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
cristy3ed852e2009-09-05 21:47:34 +00002120 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2121 }
cristyed231572011-07-14 02:18:59 +00002122 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002123 }
2124 break;
2125 }
2126 }
2127 extent=(size_t) (q-pixels);
2128 }
2129 count=WriteBlob(image,extent,pixels);
2130 if (count != (ssize_t) extent)
2131 break;
2132 if (image->previous == (Image *) NULL)
2133 {
cristycee97112010-05-28 00:44:52 +00002134 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2135 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002136 if (status == MagickFalse)
2137 break;
2138 }
2139 }
2140 quantum_info=DestroyQuantumInfo(quantum_info);
2141 break;
2142 }
2143 case 'F':
2144 case 'f':
2145 {
2146 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2147 "-1.0\n");
2148 image->depth=32;
2149 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2150 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2151 if (quantum_info == (QuantumInfo *) NULL)
2152 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2153 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2154 if (status == MagickFalse)
2155 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2156 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00002157 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00002158 {
cristy4c08aed2011-07-01 19:47:50 +00002159 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002160 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002161
2162 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00002163 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002164 break;
cristy4c08aed2011-07-01 19:47:50 +00002165 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2166 quantum_type,pixels,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002167 (void) WriteBlob(image,extent,pixels);
2168 if (image->previous == (Image *) NULL)
2169 {
cristycee97112010-05-28 00:44:52 +00002170 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2171 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002172 if (status == MagickFalse)
2173 break;
2174 }
2175 }
2176 quantum_info=DestroyQuantumInfo(quantum_info);
2177 break;
2178 }
2179 }
2180 if (GetNextImageInList(image) == (Image *) NULL)
2181 break;
2182 image=SyncNextImageInList(image);
2183 status=SetImageProgress(image,SaveImagesTag,scene++,
2184 GetImageListLength(image));
2185 if (status == MagickFalse)
2186 break;
2187 } while (image_info->adjoin != MagickFalse);
2188 (void) CloseBlob(image);
2189 return(MagickTrue);
2190}