2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #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"
50 #include "MagickCore/colorspace-private.h"
51 #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"
72 static MagickBooleanType
73 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsPNM() returns MagickTrue if the image format type, identified by the
87 % magick string, is PNM.
89 % The format of the IsPNM method is:
91 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o extent: Specifies the extent of the magick string.
100 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
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')))
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d P N M I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
127 % The format of the ReadPNMImage method is:
129 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image_info: the image info.
135 % o exception: return any errors or warnings in this structure.
139 static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 const size_t extent,ExceptionInfo *exception)
142 if ((offset < 0) || (offset > (ssize_t) extent))
144 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
145 "InvalidPixel","`%s'",image->filename);
151 static size_t PNMInteger(Image *image,const unsigned int base,
152 ExceptionInfo *exception)
168 Skip any leading whitespace.
170 extent=MaxTextExtent;
171 comment=(char *) NULL;
175 c=ReadBlobByte(image);
183 if (comment == (char *) NULL)
184 comment=AcquireString((char *) NULL);
185 p=comment+strlen(comment);
186 for ( ; (c != EOF) && (c != (int) '\n'); p++)
188 if ((size_t) (p-comment+1) >= extent)
191 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
193 if (comment == (char *) NULL)
195 p=comment+strlen(comment);
197 c=ReadBlobByte(image);
204 if (comment == (char *) NULL)
208 } while (isdigit(c) == MagickFalse);
209 if (comment != (char *) NULL)
211 (void) SetImageProperty(image,"comment",comment,exception);
212 comment=DestroyString(comment);
215 return((size_t) (c-(int) '0'));
224 c=ReadBlobByte(image);
227 } while (isdigit(c) != MagickFalse);
231 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
271 assert(image_info != (const ImageInfo *) NULL);
272 assert(image_info->signature == MagickSignature);
273 if (image_info->debug != MagickFalse)
274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
275 image_info->filename);
276 assert(exception != (ExceptionInfo *) NULL);
277 assert(exception->signature == MagickSignature);
278 image=AcquireImage(image_info,exception);
279 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
280 if (status == MagickFalse)
282 image=DestroyImageList(image);
283 return((Image *) NULL);
288 count=ReadBlob(image,1,(unsigned char *) &format);
292 Initialize image structure.
294 if ((count != 1) || (format != 'P'))
295 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
297 quantum_type=RGBQuantum;
299 format=(char) ReadBlobByte(image);
303 PBM, PGM, PPM, and PNM.
305 image->columns=PNMInteger(image,10,exception);
306 image->rows=PNMInteger(image,10,exception);
307 if ((format == 'f') || (format == 'F'))
310 scale[MaxTextExtent];
312 (void) ReadBlobString(image,scale);
313 quantum_scale=StringToDouble(scale,(char **) NULL);
317 if ((format == '1') || (format == '4'))
318 max_value=1; /* bitmap */
320 max_value=PNMInteger(image,10,exception);
326 keyword[MaxTextExtent],
327 value[MaxTextExtent];
338 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
340 while (isspace((int) ((unsigned char) c)) != 0)
341 c=ReadBlobByte(image);
345 if ((size_t) (p-keyword) < (MaxTextExtent-1))
347 c=ReadBlobByte(image);
348 } while (isalnum(c));
350 if (LocaleCompare(keyword,"endhdr") == 0)
352 while (isspace((int) ((unsigned char) c)) != 0)
353 c=ReadBlobByte(image);
355 while (isalnum(c) || (c == '_'))
357 if ((size_t) (p-value) < (MaxTextExtent-1))
359 c=ReadBlobByte(image);
363 Assign a value to the specified keyword.
365 if (LocaleCompare(keyword,"depth") == 0)
366 packet_size=StringToUnsignedLong(value);
368 if (LocaleCompare(keyword,"height") == 0)
369 image->rows=StringToUnsignedLong(value);
370 if (LocaleCompare(keyword,"maxval") == 0)
371 max_value=StringToUnsignedLong(value);
372 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
374 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
375 quantum_type=GrayQuantum;
376 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
378 quantum_type=GrayAlphaQuantum;
379 image->matte=MagickTrue;
381 if (LocaleCompare(value,"GRAYSCALE") == 0)
383 image->colorspace=GRAYColorspace;
384 quantum_type=GrayQuantum;
386 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
388 image->colorspace=GRAYColorspace;
389 image->matte=MagickTrue;
390 quantum_type=GrayAlphaQuantum;
392 if (LocaleCompare(value,"RGB_ALPHA") == 0)
394 quantum_type=RGBAQuantum;
395 image->matte=MagickTrue;
397 if (LocaleCompare(value,"CMYK") == 0)
399 quantum_type=CMYKQuantum;
400 image->colorspace=CMYKColorspace;
402 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
404 quantum_type=CMYKAQuantum;
405 image->colorspace=CMYKColorspace;
406 image->matte=MagickTrue;
409 if (LocaleCompare(keyword,"width") == 0)
410 image->columns=StringToUnsignedLong(value);
413 if ((image->columns == 0) || (image->rows == 0))
414 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
415 if (max_value >= 65536)
416 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
417 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
419 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
420 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
423 Convert PNM pixels to runextent-encoded MIFF packets.
432 Convert PBM image to pixel packets.
434 image->colorspace=GRAYColorspace;
435 for (y=0; y < (ssize_t) image->rows; y++)
443 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
444 if (q == (Quantum *) NULL)
446 for (x=0; x < (ssize_t) image->columns; x++)
448 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
450 q+=GetPixelChannels(image);
452 if (SyncAuthenticPixels(image,exception) == MagickFalse)
454 if (image->previous == (Image *) NULL)
456 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
458 if (status == MagickFalse)
462 image->type=BilevelType;
471 Convert PGM image to pixel packets.
473 image->colorspace=GRAYColorspace;
474 scale=(Quantum *) NULL;
475 if (max_value != (1U*QuantumRange))
478 Compute pixel scaling table.
480 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
482 if (scale == (Quantum *) NULL)
483 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
484 for (i=0; i <= (ssize_t) max_value; i++)
485 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
487 for (y=0; y < (ssize_t) image->rows; y++)
495 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
496 if (q == (Quantum *) NULL)
498 for (x=0; x < (ssize_t) image->columns; x++)
500 intensity=PNMInteger(image,10,exception);
501 SetPixelGray(image,intensity,q);
502 if (scale != (Quantum *) NULL)
503 SetPixelGray(image,scale[ConstrainPixel(image,(ssize_t) intensity,
504 max_value,exception)],q);
505 q+=GetPixelChannels(image);
507 if (SyncAuthenticPixels(image,exception) == MagickFalse)
509 if (image->previous == (Image *) NULL)
511 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
513 if (status == MagickFalse)
517 image->type=GrayscaleType;
518 if (scale != (Quantum *) NULL)
519 scale=(Quantum *) RelinquishMagickMemory(scale);
528 Convert PNM image to pixel packets.
530 scale=(Quantum *) NULL;
531 if (max_value != (1U*QuantumRange))
534 Compute pixel scaling table.
536 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
538 if (scale == (Quantum *) NULL)
539 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
540 for (i=0; i <= (ssize_t) max_value; i++)
541 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
543 for (y=0; y < (ssize_t) image->rows; y++)
551 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
552 if (q == (Quantum *) NULL)
554 for (x=0; x < (ssize_t) image->columns; x++)
556 pixel.red=(MagickRealType) PNMInteger(image,10,exception);
557 pixel.green=(MagickRealType) PNMInteger(image,10,exception);
558 pixel.blue=(MagickRealType) PNMInteger(image,10,exception);
559 if (scale != (Quantum *) NULL)
561 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
562 pixel.red,max_value,exception)];
563 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
564 (ssize_t) pixel.green,max_value,exception)];
565 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
566 pixel.blue,max_value,exception)];
568 SetPixelRed(image,ClampToQuantum(pixel.red),q);
569 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
570 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
571 q+=GetPixelChannels(image);
573 if (SyncAuthenticPixels(image,exception) == MagickFalse)
575 if (image->previous == (Image *) NULL)
577 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
579 if (status == MagickFalse)
583 if (scale != (Quantum *) NULL)
584 scale=(Quantum *) RelinquishMagickMemory(scale);
590 Convert PBM raw image to pixel packets.
592 image->colorspace=GRAYColorspace;
593 quantum_type=GrayQuantum;
594 if (image->storage_class == PseudoClass)
595 quantum_type=IndexQuantum;
596 quantum_info=AcquireQuantumInfo(image_info,image);
597 if (quantum_info == (QuantumInfo *) NULL)
598 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
599 SetQuantumMinIsWhite(quantum_info,MagickTrue);
600 extent=GetQuantumExtent(image,quantum_info,quantum_type);
601 for (y=0; y < (ssize_t) image->rows; y++)
619 if (status == MagickFalse)
621 pixels=GetQuantumPixels(quantum_info);
623 count=ReadBlob(image,extent,pixels);
624 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
625 (image->previous == (Image *) NULL))
630 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
632 if (proceed == MagickFalse)
637 if (count != (ssize_t) extent)
639 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
640 if (q == (Quantum *) NULL)
645 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
646 quantum_type,pixels,exception);
647 if (length != extent)
649 sync=SyncAuthenticPixels(image,exception);
650 if (sync == MagickFalse)
653 quantum_info=DestroyQuantumInfo(quantum_info);
654 if (status == MagickFalse)
655 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
656 SetQuantumImageType(image,quantum_type);
665 Convert PGM raw image to pixel packets.
667 image->colorspace=GRAYColorspace;
668 range=GetQuantumRange(image->depth);
669 quantum_type=GrayQuantum;
670 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
671 quantum_info=AcquireQuantumInfo(image_info,image);
672 if (quantum_info == (QuantumInfo *) NULL)
673 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
674 for (y=0; y < (ssize_t) image->rows; y++)
679 register const unsigned char
695 if (status == MagickFalse)
697 pixels=GetQuantumPixels(quantum_info);
699 count=ReadBlob(image,extent,pixels);
700 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
701 (image->previous == (Image *) NULL))
706 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
708 if (proceed == MagickFalse)
713 if (count != (ssize_t) extent)
715 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
716 if (q == (Quantum *) NULL)
722 if ((image->depth == 8) || (image->depth == 16))
723 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
724 quantum_type,pixels,exception);
726 if (image->depth <= 8)
731 for (x=0; x < (ssize_t) image->columns; x++)
733 p=PushCharPixel(p,&pixel);
734 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
735 q+=GetPixelChannels(image);
743 for (x=0; x < (ssize_t) image->columns; x++)
745 p=PushShortPixel(MSBEndian,p,&pixel);
746 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
747 q+=GetPixelChannels(image);
750 sync=SyncAuthenticPixels(image,exception);
751 if (sync == MagickFalse)
754 quantum_info=DestroyQuantumInfo(quantum_info);
755 if (status == MagickFalse)
756 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
757 SetQuantumImageType(image,quantum_type);
769 Convert PNM raster image to pixel packets.
772 quantum_type=RGBQuantum;
773 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
774 range=GetQuantumRange(image->depth);
775 quantum_info=AcquireQuantumInfo(image_info,image);
776 if (quantum_info == (QuantumInfo *) NULL)
777 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
778 for (y=0; y < (ssize_t) image->rows; y++)
783 register const unsigned char
799 if (status == MagickFalse)
801 pixels=GetQuantumPixels(quantum_info);
803 count=ReadBlob(image,extent,pixels);
804 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
805 (image->previous == (Image *) NULL))
810 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
812 if (proceed == MagickFalse)
817 if (count != (ssize_t) extent)
819 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
820 if (q == (Quantum *) NULL)
826 if (image->depth == 8)
827 for (x=0; x < (ssize_t) image->columns; x++)
829 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
830 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
831 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
832 SetPixelAlpha(image,OpaqueAlpha,q);
833 q+=GetPixelChannels(image);
836 if (image->depth == 16)
841 for (x=0; x < (ssize_t) image->columns; x++)
843 p=PushShortPixel(MSBEndian,p,&pixel);
844 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
845 p=PushShortPixel(MSBEndian,p,&pixel);
846 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
847 p=PushShortPixel(MSBEndian,p,&pixel);
848 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
849 SetPixelAlpha(image,OpaqueAlpha,q);
850 q+=GetPixelChannels(image);
854 if (image->depth <= 8)
859 for (x=0; x < (ssize_t) image->columns; x++)
861 p=PushCharPixel(p,&pixel);
862 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
863 p=PushCharPixel(p,&pixel);
864 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
865 p=PushCharPixel(p,&pixel);
866 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
867 SetPixelAlpha(image,OpaqueAlpha,q);
868 q+=GetPixelChannels(image);
876 for (x=0; x < (ssize_t) image->columns; x++)
878 p=PushShortPixel(MSBEndian,p,&pixel);
879 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
880 p=PushShortPixel(MSBEndian,p,&pixel);
881 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
882 p=PushShortPixel(MSBEndian,p,&pixel);
883 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
884 SetPixelAlpha(image,OpaqueAlpha,q);
885 q+=GetPixelChannels(image);
888 if ((type == BilevelType) || (type == GrayscaleType))
890 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
891 for (x=0; x < (ssize_t) image->columns; x++)
893 if ((type == BilevelType) &&
894 (IsPixelMonochrome(image,q) == MagickFalse))
895 type=IsPixelGray(image,q) == MagickFalse ? UndefinedType :
897 if ((type == GrayscaleType) &&
898 (IsPixelGray(image,q) == MagickFalse))
900 if ((type != BilevelType) && (type != GrayscaleType))
902 q+=GetPixelChannels(image);
905 sync=SyncAuthenticPixels(image,exception);
906 if (sync == MagickFalse)
909 quantum_info=DestroyQuantumInfo(quantum_info);
910 if (status == MagickFalse)
911 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
912 if (type != UndefinedType)
925 Convert PAM raster image to pixel packets.
927 range=GetQuantumRange(image->depth);
928 switch (quantum_type)
931 case GrayAlphaQuantum:
948 if (image->matte != MagickFalse)
950 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
951 quantum_info=AcquireQuantumInfo(image_info,image);
952 if (quantum_info == (QuantumInfo *) NULL)
953 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
954 for (y=0; y < (ssize_t) image->rows; y++)
959 register const unsigned char
975 if (status == MagickFalse)
977 pixels=GetQuantumPixels(quantum_info);
979 count=ReadBlob(image,extent,pixels);
980 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
981 (image->previous == (Image *) NULL))
986 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
988 if (proceed == MagickFalse)
993 if (count != (ssize_t) extent)
995 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
996 if (q == (Quantum *) NULL)
1002 if ((image->depth == 8) || (image->depth == 16))
1003 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1004 quantum_type,pixels,exception);
1006 switch (quantum_type)
1009 case GrayAlphaQuantum:
1011 if (image->depth <= 8)
1016 for (x=0; x < (ssize_t) image->columns; x++)
1018 p=PushCharPixel(p,&pixel);
1019 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1020 SetPixelAlpha(image,OpaqueAlpha,q);
1021 if (image->matte != MagickFalse)
1023 p=PushCharPixel(p,&pixel);
1024 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1026 q+=GetPixelChannels(image);
1034 for (x=0; x < (ssize_t) image->columns; x++)
1036 p=PushShortPixel(MSBEndian,p,&pixel);
1037 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1038 SetPixelAlpha(image,OpaqueAlpha,q);
1039 if (image->matte != MagickFalse)
1041 p=PushShortPixel(MSBEndian,p,&pixel);
1042 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1044 q+=GetPixelChannels(image);
1052 if (image->depth <= 8)
1057 for (x=0; x < (ssize_t) image->columns; x++)
1059 p=PushCharPixel(p,&pixel);
1060 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1061 p=PushCharPixel(p,&pixel);
1062 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1063 p=PushCharPixel(p,&pixel);
1064 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1065 p=PushCharPixel(p,&pixel);
1066 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1067 SetPixelAlpha(image,OpaqueAlpha,q);
1068 if (image->matte != MagickFalse)
1070 p=PushCharPixel(p,&pixel);
1071 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1073 q+=GetPixelChannels(image);
1081 for (x=0; x < (ssize_t) image->columns; x++)
1083 p=PushShortPixel(MSBEndian,p,&pixel);
1084 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1085 p=PushShortPixel(MSBEndian,p,&pixel);
1086 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1087 p=PushShortPixel(MSBEndian,p,&pixel);
1088 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1089 p=PushShortPixel(MSBEndian,p,&pixel);
1090 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1091 SetPixelAlpha(image,OpaqueAlpha,q);
1092 if (image->matte != MagickFalse)
1094 p=PushShortPixel(MSBEndian,p,&pixel);
1095 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1097 q+=GetPixelChannels(image);
1104 if (image->depth <= 8)
1109 for (x=0; x < (ssize_t) image->columns; x++)
1111 p=PushCharPixel(p,&pixel);
1112 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1113 p=PushCharPixel(p,&pixel);
1114 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1115 p=PushCharPixel(p,&pixel);
1116 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1117 SetPixelAlpha(image,OpaqueAlpha,q);
1118 if (image->matte != MagickFalse)
1120 p=PushCharPixel(p,&pixel);
1121 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1123 q+=GetPixelChannels(image);
1131 for (x=0; x < (ssize_t) image->columns; x++)
1133 p=PushShortPixel(MSBEndian,p,&pixel);
1134 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1135 p=PushShortPixel(MSBEndian,p,&pixel);
1136 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1137 p=PushShortPixel(MSBEndian,p,&pixel);
1138 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1139 SetPixelAlpha(image,OpaqueAlpha,q);
1140 if (image->matte != MagickFalse)
1142 p=PushShortPixel(MSBEndian,p,&pixel);
1143 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1145 q+=GetPixelChannels(image);
1151 sync=SyncAuthenticPixels(image,exception);
1152 if (sync == MagickFalse)
1155 quantum_info=DestroyQuantumInfo(quantum_info);
1156 if (status == MagickFalse)
1157 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1158 SetQuantumImageType(image,quantum_type);
1165 Convert PFM raster image to pixel packets.
1168 image->colorspace=GRAYColorspace;
1169 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1170 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1172 quantum_info=AcquireQuantumInfo(image_info,image);
1173 if (quantum_info == (QuantumInfo *) NULL)
1174 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1175 status=SetQuantumDepth(image,quantum_info,32);
1176 if (status == MagickFalse)
1177 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1178 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1179 if (status == MagickFalse)
1180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1181 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1182 fabs(quantum_scale));
1183 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1184 for (y=0; y < (ssize_t) image->rows; y++)
1202 if (status == MagickFalse)
1204 pixels=GetQuantumPixels(quantum_info);
1206 count=ReadBlob(image,extent,pixels);
1207 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1208 (image->previous == (Image *) NULL))
1213 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1215 if (proceed == MagickFalse)
1220 if ((size_t) count != extent)
1222 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1223 image->columns,1,exception);
1224 if (q == (Quantum *) NULL)
1229 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1230 quantum_type,pixels,exception);
1231 if (length != extent)
1233 sync=SyncAuthenticPixels(image,exception);
1234 if (sync == MagickFalse)
1237 quantum_info=DestroyQuantumInfo(quantum_info);
1238 if (status == MagickFalse)
1239 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1240 SetQuantumImageType(image,quantum_type);
1244 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1246 if (EOFBlob(image) != MagickFalse)
1248 (void) ThrowMagickException(exception,GetMagickModule(),
1249 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1253 Proceed to next image.
1255 if (image_info->number_scenes != 0)
1256 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1258 if ((format == '1') || (format == '2') || (format == '3'))
1262 Skip to end of line.
1264 count=ReadBlob(image,1,(unsigned char *) &format);
1267 if ((count != 0) && (format == 'P'))
1269 } while (format != '\n');
1270 count=ReadBlob(image,1,(unsigned char *) &format);
1271 if ((count == 1) && (format == 'P'))
1274 Allocate next image structure.
1276 AcquireNextImage(image_info,image,exception);
1277 if (GetNextImageInList(image) == (Image *) NULL)
1279 image=DestroyImageList(image);
1280 return((Image *) NULL);
1282 image=SyncNextImageInList(image);
1283 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1284 GetBlobSize(image));
1285 if (status == MagickFalse)
1288 } while ((count == 1) && (format == 'P'));
1289 (void) CloseBlob(image);
1290 return(GetFirstImageInList(image));
1294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298 % R e g i s t e r P N M I m a g e %
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1304 % RegisterPNMImage() adds properties for the PNM image format to
1305 % the list of supported formats. The properties include the image format
1306 % tag, a method to read and/or write the format, whether the format
1307 % supports the saving of more than one frame to the same file or blob,
1308 % whether the format supports native in-memory I/O, and a brief
1309 % description of the format.
1311 % The format of the RegisterPNMImage method is:
1313 % size_t RegisterPNMImage(void)
1316 ModuleExport size_t RegisterPNMImage(void)
1321 entry=SetMagickInfo("PAM");
1322 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1323 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1324 entry->description=ConstantString("Common 2-dimensional bitmap format");
1325 entry->module=ConstantString("PNM");
1326 (void) RegisterMagickInfo(entry);
1327 entry=SetMagickInfo("PBM");
1328 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1329 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1330 entry->description=ConstantString("Portable bitmap format (black and white)");
1331 entry->module=ConstantString("PNM");
1332 (void) RegisterMagickInfo(entry);
1333 entry=SetMagickInfo("PFM");
1334 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1335 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1336 entry->endian_support=MagickTrue;
1337 entry->description=ConstantString("Portable float format");
1338 entry->module=ConstantString("PFM");
1339 (void) RegisterMagickInfo(entry);
1340 entry=SetMagickInfo("PGM");
1341 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1342 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1343 entry->description=ConstantString("Portable graymap format (gray scale)");
1344 entry->module=ConstantString("PNM");
1345 (void) RegisterMagickInfo(entry);
1346 entry=SetMagickInfo("PNM");
1347 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1348 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1349 entry->magick=(IsImageFormatHandler *) IsPNM;
1350 entry->description=ConstantString("Portable anymap");
1351 entry->module=ConstantString("PNM");
1352 (void) RegisterMagickInfo(entry);
1353 entry=SetMagickInfo("PPM");
1354 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1355 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1356 entry->description=ConstantString("Portable pixmap format (color)");
1357 entry->module=ConstantString("PNM");
1358 (void) RegisterMagickInfo(entry);
1359 return(MagickImageCoderSignature);
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1367 % U n r e g i s t e r P N M I m a g e %
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 % UnregisterPNMImage() removes format registrations made by the
1374 % PNM module from the list of supported formats.
1376 % The format of the UnregisterPNMImage method is:
1378 % UnregisterPNMImage(void)
1381 ModuleExport void UnregisterPNMImage(void)
1383 (void) UnregisterMagickInfo("PAM");
1384 (void) UnregisterMagickInfo("PBM");
1385 (void) UnregisterMagickInfo("PGM");
1386 (void) UnregisterMagickInfo("PNM");
1387 (void) UnregisterMagickInfo("PPM");
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % W r i t e P N M I m a g e %
1399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1403 % The format of the WritePNMImage method is:
1405 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1406 % Image *image,ExceptionInfo *exception)
1408 % A description of each parameter follows.
1410 % o image_info: the image info.
1412 % o image: The image.
1414 % o exception: return any errors or warnings in this structure.
1417 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1418 ExceptionInfo *exception)
1421 buffer[MaxTextExtent],
1423 magick[MaxTextExtent];
1446 register unsigned char
1459 Open output image file.
1461 assert(image_info != (const ImageInfo *) NULL);
1462 assert(image_info->signature == MagickSignature);
1463 assert(image != (Image *) NULL);
1464 assert(image->signature == MagickSignature);
1465 if (image->debug != MagickFalse)
1466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1467 assert(exception != (ExceptionInfo *) NULL);
1468 assert(exception->signature == MagickSignature);
1469 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1470 if (status == MagickFalse)
1476 Write PNM file header.
1479 quantum_type=RGBQuantum;
1480 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1493 if (image_info->compression == NoCompression)
1501 if (IsImageGray(image,exception) != MagickFalse)
1509 if (image_info->compression == NoCompression)
1516 if ((image_info->type != TrueColorType) &&
1517 (IsImageGray(image,exception) != MagickFalse))
1520 if (image_info->compression == NoCompression)
1522 if (IsImageMonochrome(image,exception) != MagickFalse)
1525 if (image_info->compression == NoCompression)
1534 if (image_info->compression == NoCompression)
1539 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1540 (void) WriteBlobString(image,buffer);
1541 value=GetImageProperty(image,"comment",exception);
1542 if (value != (const char *) NULL)
1548 Write comments to file.
1550 (void) WriteBlobByte(image,'#');
1551 for (p=value; *p != '\0'; p++)
1553 (void) WriteBlobByte(image,(unsigned char) *p);
1554 if ((*p == '\r') && (*(p+1) != '\0'))
1555 (void) WriteBlobByte(image,'#');
1556 if ((*p == '\n') && (*(p+1) != '\0'))
1557 (void) WriteBlobByte(image,'#');
1559 (void) WriteBlobByte(image,'\n');
1563 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1564 (void) TransformImageColorspace(image,RGBColorspace,exception);
1565 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1566 (double) image->columns,(double) image->rows);
1567 (void) WriteBlobString(image,buffer);
1572 type[MaxTextExtent];
1577 (void) FormatLocaleString(buffer,MaxTextExtent,
1578 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1580 (void) WriteBlobString(image,buffer);
1581 quantum_type=GetQuantumType(image,exception);
1582 switch (quantum_type)
1588 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1592 case GrayAlphaQuantum:
1595 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1600 quantum_type=RGBQuantum;
1601 if (image->matte != MagickFalse)
1602 quantum_type=RGBAQuantum;
1604 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1608 if (image->matte != MagickFalse)
1611 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1613 if (image->depth > 16)
1615 (void) FormatLocaleString(buffer,MaxTextExtent,
1616 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1617 ((MagickOffsetType) GetQuantumRange(image->depth)));
1618 (void) WriteBlobString(image,buffer);
1619 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1621 (void) WriteBlobString(image,buffer);
1624 Convert runextent encoded to PNM raster pixels.
1634 Convert image to a PBM image.
1637 for (y=0; y < (ssize_t) image->rows; y++)
1639 register const Quantum
1645 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1646 if (p == (const Quantum *) NULL)
1648 for (x=0; x < (ssize_t) image->columns; x++)
1650 pixel=GetPixelIntensity(image,p);
1651 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1654 if ((q-pixels+2) >= 80)
1657 (void) WriteBlob(image,q-pixels,pixels);
1660 p+=GetPixelChannels(image);
1662 if (image->previous == (Image *) NULL)
1664 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1666 if (status == MagickFalse)
1673 (void) WriteBlob(image,q-pixels,pixels);
1683 Convert image to a PGM image.
1685 if (image->depth <= 8)
1686 (void) WriteBlobString(image,"255\n");
1688 (void) WriteBlobString(image,"65535\n");
1690 for (y=0; y < (ssize_t) image->rows; y++)
1692 register const Quantum
1698 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1699 if (p == (const Quantum *) NULL)
1701 for (x=0; x < (ssize_t) image->columns; x++)
1703 index=GetPixelIntensity(image,p);
1704 if (image->depth <= 8)
1705 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1706 ScaleQuantumToChar(index));
1708 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1709 ScaleQuantumToShort(index));
1710 extent=(size_t) count;
1711 (void) strncpy((char *) q,buffer,extent);
1713 if ((q-pixels+extent) >= 80)
1716 (void) WriteBlob(image,q-pixels,pixels);
1719 p+=GetPixelChannels(image);
1721 if (image->previous == (Image *) NULL)
1723 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1725 if (status == MagickFalse)
1732 (void) WriteBlob(image,q-pixels,pixels);
1742 Convert image to a PNM image.
1744 if (image->depth <= 8)
1745 (void) WriteBlobString(image,"255\n");
1747 (void) WriteBlobString(image,"65535\n");
1749 for (y=0; y < (ssize_t) image->rows; y++)
1751 register const Quantum
1757 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1758 if (p == (const Quantum *) NULL)
1760 for (x=0; x < (ssize_t) image->columns; x++)
1762 if (image->depth <= 8)
1763 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1764 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1765 ScaleQuantumToChar(GetPixelGreen(image,p)),
1766 ScaleQuantumToChar(GetPixelBlue(image,p)));
1768 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1769 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1770 ScaleQuantumToShort(GetPixelGreen(image,p)),
1771 ScaleQuantumToShort(GetPixelBlue(image,p)));
1772 extent=(size_t) count;
1773 (void) strncpy((char *) q,buffer,extent);
1775 if ((q-pixels+extent) >= 80)
1778 (void) WriteBlob(image,q-pixels,pixels);
1781 p+=GetPixelChannels(image);
1783 if (image->previous == (Image *) NULL)
1785 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1787 if (status == MagickFalse)
1794 (void) WriteBlob(image,q-pixels,pixels);
1801 Convert image to a PBM image.
1804 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1805 if (quantum_info == (QuantumInfo *) NULL)
1806 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1807 quantum_info->min_is_white=MagickTrue;
1808 pixels=GetQuantumPixels(quantum_info);
1809 for (y=0; y < (ssize_t) image->rows; y++)
1811 register const Quantum
1814 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1815 if (p == (const Quantum *) NULL)
1817 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1818 GrayQuantum,pixels,exception);
1819 count=WriteBlob(image,extent,pixels);
1820 if (count != (ssize_t) extent)
1822 if (image->previous == (Image *) NULL)
1824 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1826 if (status == MagickFalse)
1830 quantum_info=DestroyQuantumInfo(quantum_info);
1839 Convert image to a PGM image.
1841 if (image->depth > 8)
1843 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1844 ((MagickOffsetType) GetQuantumRange(image->depth)));
1845 (void) WriteBlobString(image,buffer);
1846 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1847 if (quantum_info == (QuantumInfo *) NULL)
1848 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1849 quantum_info->min_is_white=MagickTrue;
1850 pixels=GetQuantumPixels(quantum_info);
1851 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1852 range=GetQuantumRange(image->depth);
1853 for (y=0; y < (ssize_t) image->rows; y++)
1855 register const Quantum
1861 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1862 if (p == (const Quantum *) NULL)
1865 if ((image->depth == 8) || (image->depth == 16))
1866 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1867 GrayQuantum,pixels,exception);
1870 if (image->depth <= 8)
1871 for (x=0; x < (ssize_t) image->columns; x++)
1873 if (IsPixelGray(image,p) == MagickFalse)
1874 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1877 if (image->depth == 8)
1878 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1880 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1882 q=PopCharPixel((unsigned char) pixel,q);
1883 p+=GetPixelChannels(image);
1886 for (x=0; x < (ssize_t) image->columns; x++)
1888 if (IsPixelGray(image,p) == MagickFalse)
1889 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1892 if (image->depth == 16)
1893 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
1895 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1897 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1898 p+=GetPixelChannels(image);
1900 extent=(size_t) (q-pixels);
1902 count=WriteBlob(image,extent,pixels);
1903 if (count != (ssize_t) extent)
1905 if (image->previous == (Image *) NULL)
1907 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1909 if (status == MagickFalse)
1913 quantum_info=DestroyQuantumInfo(quantum_info);
1922 Convert image to a PNM image.
1924 if (image->depth > 8)
1926 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1927 ((MagickOffsetType) GetQuantumRange(image->depth)));
1928 (void) WriteBlobString(image,buffer);
1929 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1930 if (quantum_info == (QuantumInfo *) NULL)
1931 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1932 pixels=GetQuantumPixels(quantum_info);
1933 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1934 range=GetQuantumRange(image->depth);
1935 for (y=0; y < (ssize_t) image->rows; y++)
1937 register const Quantum
1943 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1944 if (p == (const Quantum *) NULL)
1947 if ((image->depth == 8) || (image->depth == 16))
1948 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1949 quantum_type,pixels,exception);
1952 if (image->depth <= 8)
1953 for (x=0; x < (ssize_t) image->columns; x++)
1955 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1956 q=PopCharPixel((unsigned char) pixel,q);
1957 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1958 q=PopCharPixel((unsigned char) pixel,q);
1959 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1960 q=PopCharPixel((unsigned char) pixel,q);
1961 p+=GetPixelChannels(image);
1964 for (x=0; x < (ssize_t) image->columns; x++)
1966 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1967 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1968 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1969 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1970 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1971 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1972 p+=GetPixelChannels(image);
1974 extent=(size_t) (q-pixels);
1976 count=WriteBlob(image,extent,pixels);
1977 if (count != (ssize_t) extent)
1979 if (image->previous == (Image *) NULL)
1981 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1983 if (status == MagickFalse)
1987 quantum_info=DestroyQuantumInfo(quantum_info);
1996 Convert image to a PAM.
1998 if (image->depth > 16)
2000 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2001 pixels=GetQuantumPixels(quantum_info);
2002 range=GetQuantumRange(image->depth);
2003 for (y=0; y < (ssize_t) image->rows; y++)
2005 register const Quantum
2011 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2012 if (p == (const Quantum *) NULL)
2015 if ((image->depth == 8) || (image->depth == 16))
2016 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2017 quantum_type,pixels,exception);
2020 switch (quantum_type)
2023 case GrayAlphaQuantum:
2025 if (image->depth <= 8)
2026 for (x=0; x < (ssize_t) image->columns; x++)
2028 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2029 q=PopCharPixel((unsigned char) pixel,q);
2030 if (image->matte != MagickFalse)
2032 pixel=(unsigned char) ScaleQuantumToAny(
2033 GetPixelAlpha(image,p),range);
2034 q=PopCharPixel((unsigned char) pixel,q);
2036 p+=GetPixelChannels(image);
2039 for (x=0; x < (ssize_t) image->columns; x++)
2041 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2042 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2043 if (image->matte != MagickFalse)
2045 pixel=(unsigned char) ScaleQuantumToAny(
2046 GetPixelAlpha(image,p),range);
2047 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2049 p+=GetPixelChannels(image);
2056 if (image->depth <= 8)
2057 for (x=0; x < (ssize_t) image->columns; x++)
2059 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2060 q=PopCharPixel((unsigned char) pixel,q);
2061 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2062 q=PopCharPixel((unsigned char) pixel,q);
2063 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2064 q=PopCharPixel((unsigned char) pixel,q);
2065 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2066 q=PopCharPixel((unsigned char) pixel,q);
2067 if (image->matte != MagickFalse)
2069 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2070 q=PopCharPixel((unsigned char) pixel,q);
2072 p+=GetPixelChannels(image);
2075 for (x=0; x < (ssize_t) image->columns; x++)
2077 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2078 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2079 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2080 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2081 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2082 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2083 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2084 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2085 if (image->matte != MagickFalse)
2087 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2088 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2090 p+=GetPixelChannels(image);
2096 if (image->depth <= 8)
2097 for (x=0; x < (ssize_t) image->columns; x++)
2099 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2100 q=PopCharPixel((unsigned char) pixel,q);
2101 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2102 q=PopCharPixel((unsigned char) pixel,q);
2103 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2104 q=PopCharPixel((unsigned char) pixel,q);
2105 if (image->matte != MagickFalse)
2107 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2108 q=PopCharPixel((unsigned char) pixel,q);
2110 p+=GetPixelChannels(image);
2113 for (x=0; x < (ssize_t) image->columns; x++)
2115 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2116 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2117 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2118 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2119 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2120 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2121 if (image->matte != MagickFalse)
2123 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2124 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2126 p+=GetPixelChannels(image);
2131 extent=(size_t) (q-pixels);
2133 count=WriteBlob(image,extent,pixels);
2134 if (count != (ssize_t) extent)
2136 if (image->previous == (Image *) NULL)
2138 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2140 if (status == MagickFalse)
2144 quantum_info=DestroyQuantumInfo(quantum_info);
2150 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2153 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2154 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2155 if (quantum_info == (QuantumInfo *) NULL)
2156 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2157 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2158 if (status == MagickFalse)
2159 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2160 pixels=GetQuantumPixels(quantum_info);
2161 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2163 register const Quantum
2166 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2167 if (p == (const Quantum *) NULL)
2169 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2170 quantum_type,pixels,exception);
2171 (void) WriteBlob(image,extent,pixels);
2172 if (image->previous == (Image *) NULL)
2174 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2176 if (status == MagickFalse)
2180 quantum_info=DestroyQuantumInfo(quantum_info);
2184 if (GetNextImageInList(image) == (Image *) NULL)
2186 image=SyncNextImageInList(image);
2187 status=SetImageProgress(image,SaveImagesTag,scene++,
2188 GetImageListLength(image));
2189 if (status == MagickFalse)
2191 } while (image_info->adjoin != MagickFalse);
2192 (void) CloseBlob(image);