2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2015 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.
138 static void PNMComment(Image *image,ExceptionInfo *exception)
155 comment=AcquireString(GetImageProperty(image,"comment",exception));
156 extent=MaxTextExtent;
157 p=comment+strlen(comment);
158 for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
160 if ((size_t) (p-comment+1) >= extent)
163 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
165 if (comment == (char *) NULL)
167 p=comment+strlen(comment);
169 c=ReadBlobByte(image);
176 if (comment == (char *) NULL)
178 (void) SetImageProperty(image,"comment",comment,exception);
179 comment=DestroyString(comment);
182 static unsigned int PNMInteger(Image *image,const unsigned int base,
183 ExceptionInfo *exception)
192 Skip any leading whitespace.
196 c=ReadBlobByte(image);
200 PNMComment(image,exception);
201 } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
203 return((unsigned int) (c-(int) '0'));
208 while (isdigit(c) != 0) {
209 if (value > (unsigned int) (INT_MAX/10))
212 if (value > (INT_MAX-(c-(int) '0')))
215 c=ReadBlobByte(image);
222 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
258 assert(image_info != (const ImageInfo *) NULL);
259 assert(image_info->signature == MagickSignature);
260 if (image_info->debug != MagickFalse)
261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
262 image_info->filename);
263 assert(exception != (ExceptionInfo *) NULL);
264 assert(exception->signature == MagickSignature);
265 image=AcquireImage(image_info,exception);
266 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
267 if (status == MagickFalse)
269 image=DestroyImageList(image);
270 return((Image *) NULL);
275 count=ReadBlob(image,1,(unsigned char *) &format);
279 Initialize image structure.
281 if ((count != 1) || (format != 'P'))
282 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
284 quantum_type=RGBQuantum;
286 format=(char) ReadBlobByte(image);
290 PBM, PGM, PPM, and PNM.
292 image->columns=(size_t) PNMInteger(image,10,exception);
293 image->rows=(size_t) PNMInteger(image,10,exception);
294 if ((format == 'f') || (format == 'F'))
297 scale[MaxTextExtent];
299 (void) ReadBlobString(image,scale);
300 quantum_scale=StringToDouble(scale,(char **) NULL);
304 if ((format == '1') || (format == '4'))
305 max_value=1; /* bitmap */
307 max_value=(QuantumAny) PNMInteger(image,10,exception);
313 keyword[MaxTextExtent],
314 value[MaxTextExtent];
325 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
327 while (isspace((int) ((unsigned char) c)) != 0)
328 c=ReadBlobByte(image);
334 PNMComment(image,exception);
335 c=ReadBlobByte(image);
336 while (isspace((int) ((unsigned char) c)) != 0)
337 c=ReadBlobByte(image);
342 if ((size_t) (p-keyword) < (MaxTextExtent-1))
344 c=ReadBlobByte(image);
345 } while (isalnum(c));
347 if (LocaleCompare(keyword,"endhdr") == 0)
349 while (isspace((int) ((unsigned char) c)) != 0)
350 c=ReadBlobByte(image);
352 while (isalnum(c) || (c == '_'))
354 if ((size_t) (p-value) < (MaxTextExtent-1))
356 c=ReadBlobByte(image);
360 Assign a value to the specified keyword.
362 if (LocaleCompare(keyword,"depth") == 0)
363 packet_size=StringToUnsignedLong(value);
365 if (LocaleCompare(keyword,"height") == 0)
366 image->rows=StringToUnsignedLong(value);
367 if (LocaleCompare(keyword,"maxval") == 0)
368 max_value=StringToUnsignedLong(value);
369 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
371 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
373 (void) SetImageColorspace(image,GRAYColorspace,exception);
374 quantum_type=GrayQuantum;
376 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
378 (void) SetImageColorspace(image,GRAYColorspace,exception);
379 image->alpha_trait=BlendPixelTrait;
380 quantum_type=GrayAlphaQuantum;
382 if (LocaleCompare(value,"GRAYSCALE") == 0)
384 quantum_type=GrayQuantum;
385 (void) SetImageColorspace(image,GRAYColorspace,exception);
387 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
389 (void) SetImageColorspace(image,GRAYColorspace,exception);
390 image->alpha_trait=BlendPixelTrait;
391 quantum_type=GrayAlphaQuantum;
393 if (LocaleCompare(value,"RGB_ALPHA") == 0)
395 image->alpha_trait=BlendPixelTrait;
396 quantum_type=RGBAQuantum;
398 if (LocaleCompare(value,"CMYK") == 0)
400 (void) SetImageColorspace(image,CMYKColorspace,exception);
401 quantum_type=CMYKQuantum;
403 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
405 (void) SetImageColorspace(image,CMYKColorspace,exception);
406 image->alpha_trait=BlendPixelTrait;
407 quantum_type=CMYKAQuantum;
410 if (LocaleCompare(keyword,"width") == 0)
411 image->columns=StringToUnsignedLong(value);
414 if ((image->columns == 0) || (image->rows == 0))
415 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
416 if ((max_value == 0) || (max_value > 4294967295))
417 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
418 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
420 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
421 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
423 status=SetImageExtent(image,image->columns,image->rows,exception);
424 if (status == MagickFalse)
425 return(DestroyImageList(image));
427 Convert PNM pixels to runextent-encoded MIFF packets.
435 Convert PBM image to pixel packets.
437 (void) SetImageColorspace(image,GRAYColorspace,exception);
438 for (y=0; y < (ssize_t) image->rows; y++)
446 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
447 if (q == (Quantum *) NULL)
449 for (x=0; x < (ssize_t) image->columns; x++)
451 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
453 q+=GetPixelChannels(image);
455 if (SyncAuthenticPixels(image,exception) == MagickFalse)
457 if (image->previous == (Image *) NULL)
459 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
461 if (status == MagickFalse)
465 image->type=BilevelType;
474 Convert PGM image to pixel packets.
476 (void) SetImageColorspace(image,GRAYColorspace,exception);
477 for (y=0; y < (ssize_t) image->rows; y++)
485 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
486 if (q == (Quantum *) NULL)
488 for (x=0; x < (ssize_t) image->columns; x++)
490 intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),
492 SetPixelGray(image,intensity,q);
493 q+=GetPixelChannels(image);
495 if (SyncAuthenticPixels(image,exception) == MagickFalse)
497 if (image->previous == (Image *) NULL)
499 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
501 if (status == MagickFalse)
505 image->type=GrayscaleType;
511 Convert PNM image to pixel packets.
513 for (y=0; y < (ssize_t) image->rows; y++)
521 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
522 if (q == (Quantum *) NULL)
524 for (x=0; x < (ssize_t) image->columns; x++)
529 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
530 SetPixelRed(image,pixel,q);
531 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
532 SetPixelGreen(image,pixel,q);
533 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
534 SetPixelBlue(image,pixel,q);
535 q+=GetPixelChannels(image);
537 if (SyncAuthenticPixels(image,exception) == MagickFalse)
539 if (image->previous == (Image *) NULL)
541 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
543 if (status == MagickFalse)
552 Convert PBM raw image to pixel packets.
554 (void) SetImageColorspace(image,GRAYColorspace,exception);
555 quantum_type=GrayQuantum;
556 if (image->storage_class == PseudoClass)
557 quantum_type=IndexQuantum;
558 quantum_info=AcquireQuantumInfo(image_info,image);
559 if (quantum_info == (QuantumInfo *) NULL)
560 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
561 SetQuantumMinIsWhite(quantum_info,MagickTrue);
562 extent=GetQuantumExtent(image,quantum_info,quantum_type);
563 for (y=0; y < (ssize_t) image->rows; y++)
581 if (status == MagickFalse)
583 pixels=GetQuantumPixels(quantum_info);
585 count=ReadBlob(image,extent,pixels);
586 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
587 (image->previous == (Image *) NULL))
592 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
594 if (proceed == MagickFalse)
599 if (count != (ssize_t) extent)
601 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
602 if (q == (Quantum *) NULL)
607 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
608 quantum_type,pixels,exception);
609 if (length != extent)
611 sync=SyncAuthenticPixels(image,exception);
612 if (sync == MagickFalse)
615 quantum_info=DestroyQuantumInfo(quantum_info);
616 if (status == MagickFalse)
617 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
618 SetQuantumImageType(image,quantum_type);
624 Convert PGM raw image to pixel packets.
626 (void) SetImageColorspace(image,GRAYColorspace,exception);
627 quantum_type=GrayQuantum;
628 if (image->depth <= 8)
629 extent=image->columns;
631 if (image->depth <= 16)
632 extent=2*image->columns;
634 extent=4*image->columns;
635 quantum_info=AcquireQuantumInfo(image_info,image);
636 if (quantum_info == (QuantumInfo *) NULL)
637 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
638 for (y=0; y < (ssize_t) image->rows; y++)
643 register const unsigned char
659 if (status == MagickFalse)
661 pixels=GetQuantumPixels(quantum_info);
663 count=ReadBlob(image,extent,pixels);
664 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
665 (image->previous == (Image *) NULL))
670 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
672 if (proceed == MagickFalse)
677 if (count != (ssize_t) extent)
679 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
680 if (q == (Quantum *) NULL)
686 switch (image->depth)
692 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
693 quantum_type,pixels,exception);
701 if (image->depth <= 8)
706 for (x=0; x < (ssize_t) image->columns; x++)
708 p=PushCharPixel(p,&pixel);
709 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
710 q+=GetPixelChannels(image);
714 if (image->depth <= 16)
719 for (x=0; x < (ssize_t) image->columns; x++)
721 p=PushShortPixel(MSBEndian,p,&pixel);
722 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
723 q+=GetPixelChannels(image);
727 for (x=0; x < (ssize_t) image->columns; x++)
729 p=PushLongPixel(MSBEndian,p,&pixel);
730 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
731 q+=GetPixelChannels(image);
736 sync=SyncAuthenticPixels(image,exception);
737 if (sync == MagickFalse)
740 quantum_info=DestroyQuantumInfo(quantum_info);
741 if (status == MagickFalse)
742 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
743 SetQuantumImageType(image,quantum_type);
749 Convert PNM raster image to pixel packets.
751 quantum_type=RGBQuantum;
752 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
753 quantum_info=AcquireQuantumInfo(image_info,image);
754 if (quantum_info == (QuantumInfo *) NULL)
755 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
756 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
757 for (y=0; y < (ssize_t) image->rows; y++)
762 register const unsigned char
778 if (status == MagickFalse)
780 pixels=GetQuantumPixels(quantum_info);
782 count=ReadBlob(image,extent,pixels);
783 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
784 (image->previous == (Image *) NULL))
789 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
791 if (proceed == MagickFalse)
796 if (count != (ssize_t) extent)
798 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
799 if (q == (Quantum *) NULL)
805 switch (image->depth)
809 for (x=0; x < (ssize_t) image->columns; x++)
811 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
812 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
813 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
814 SetPixelAlpha(image,OpaqueAlpha,q);
815 q+=GetPixelChannels(image);
824 for (x=0; x < (ssize_t) image->columns; x++)
826 p=PushShortPixel(MSBEndian,p,&pixel);
827 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
828 p=PushShortPixel(MSBEndian,p,&pixel);
829 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
830 p=PushShortPixel(MSBEndian,p,&pixel);
831 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
832 SetPixelAlpha(image,OpaqueAlpha,q);
833 q+=GetPixelChannels(image);
842 for (x=0; x < (ssize_t) image->columns; x++)
844 p=PushLongPixel(MSBEndian,p,&pixel);
845 SetPixelRed(image,ScaleLongToQuantum(pixel),q);
846 p=PushLongPixel(MSBEndian,p,&pixel);
847 SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
848 p=PushLongPixel(MSBEndian,p,&pixel);
849 SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
850 SetPixelAlpha(image,OpaqueAlpha,q);
851 q+=GetPixelChannels(image);
860 if (image->depth <= 8)
865 for (x=0; x < (ssize_t) image->columns; x++)
867 p=PushCharPixel(p,&pixel);
868 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
869 p=PushCharPixel(p,&pixel);
870 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
871 p=PushCharPixel(p,&pixel);
872 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
873 SetPixelAlpha(image,OpaqueAlpha,q);
874 q+=GetPixelChannels(image);
878 if (image->depth <= 16)
883 for (x=0; x < (ssize_t) image->columns; x++)
885 p=PushShortPixel(MSBEndian,p,&pixel);
886 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
887 p=PushShortPixel(MSBEndian,p,&pixel);
888 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
889 p=PushShortPixel(MSBEndian,p,&pixel);
890 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
891 SetPixelAlpha(image,OpaqueAlpha,q);
892 q+=GetPixelChannels(image);
896 for (x=0; x < (ssize_t) image->columns; x++)
898 p=PushLongPixel(MSBEndian,p,&pixel);
899 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
900 p=PushLongPixel(MSBEndian,p,&pixel);
901 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
902 p=PushLongPixel(MSBEndian,p,&pixel);
903 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
904 SetPixelAlpha(image,OpaqueAlpha,q);
905 q+=GetPixelChannels(image);
910 sync=SyncAuthenticPixels(image,exception);
911 if (sync == MagickFalse)
914 quantum_info=DestroyQuantumInfo(quantum_info);
915 if (status == MagickFalse)
916 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
925 Convert PAM raster image to pixel packets.
927 switch (quantum_type)
930 case GrayAlphaQuantum:
947 if (image->alpha_trait == BlendPixelTrait)
949 if (image->depth <= 8)
950 extent=channels*image->columns;
952 if (image->depth <= 16)
953 extent=2*channels*image->columns;
955 extent=4*channels*image->columns;
956 quantum_info=AcquireQuantumInfo(image_info,image);
957 if (quantum_info == (QuantumInfo *) NULL)
958 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
959 for (y=0; y < (ssize_t) image->rows; y++)
964 register const unsigned char
980 if (status == MagickFalse)
982 pixels=GetQuantumPixels(quantum_info);
984 count=ReadBlob(image,extent,pixels);
985 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
986 (image->previous == (Image *) NULL))
991 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
993 if (proceed == MagickFalse)
998 if (count != (ssize_t) extent)
1000 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1001 if (q == (Quantum *) NULL)
1007 switch (image->depth)
1013 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1014 quantum_type,pixels,exception);
1019 switch (quantum_type)
1022 case GrayAlphaQuantum:
1027 if (image->depth <= 8)
1032 for (x=0; x < (ssize_t) image->columns; x++)
1034 p=PushCharPixel(p,&pixel);
1035 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1036 SetPixelAlpha(image,OpaqueAlpha,q);
1037 if (image->alpha_trait == BlendPixelTrait)
1039 p=PushCharPixel(p,&pixel);
1040 if (image->depth != 1)
1041 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1044 SetPixelAlpha(image,QuantumRange-
1045 ScaleAnyToQuantum(pixel,max_value),q);
1047 q+=GetPixelChannels(image);
1051 if (image->depth <= 16)
1056 for (x=0; x < (ssize_t) image->columns; x++)
1058 p=PushShortPixel(MSBEndian,p,&pixel);
1059 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1061 SetPixelAlpha(image,OpaqueAlpha,q);
1062 if (image->alpha_trait == BlendPixelTrait)
1064 p=PushShortPixel(MSBEndian,p,&pixel);
1065 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1068 q+=GetPixelChannels(image);
1072 for (x=0; x < (ssize_t) image->columns; x++)
1074 p=PushLongPixel(MSBEndian,p,&pixel);
1075 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1076 SetPixelAlpha(image,OpaqueAlpha,q);
1077 if (image->alpha_trait == BlendPixelTrait)
1079 p=PushLongPixel(MSBEndian,p,&pixel);
1080 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1082 q+=GetPixelChannels(image);
1092 if (image->depth <= 8)
1097 for (x=0; x < (ssize_t) image->columns; x++)
1099 p=PushCharPixel(p,&pixel);
1100 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1101 p=PushCharPixel(p,&pixel);
1102 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1103 p=PushCharPixel(p,&pixel);
1104 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1105 p=PushCharPixel(p,&pixel);
1106 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1107 SetPixelAlpha(image,OpaqueAlpha,q);
1108 if (image->alpha_trait == BlendPixelTrait)
1110 p=PushCharPixel(p,&pixel);
1111 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1114 q+=GetPixelChannels(image);
1118 if (image->depth <= 16)
1123 for (x=0; x < (ssize_t) image->columns; x++)
1125 p=PushShortPixel(MSBEndian,p,&pixel);
1126 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1127 p=PushShortPixel(MSBEndian,p,&pixel);
1128 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1129 p=PushShortPixel(MSBEndian,p,&pixel);
1130 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1131 p=PushShortPixel(MSBEndian,p,&pixel);
1132 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1133 SetPixelAlpha(image,OpaqueAlpha,q);
1134 if (image->alpha_trait == BlendPixelTrait)
1136 p=PushShortPixel(MSBEndian,p,&pixel);
1137 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1140 q+=GetPixelChannels(image);
1143 for (x=0; x < (ssize_t) image->columns; x++)
1145 p=PushLongPixel(MSBEndian,p,&pixel);
1146 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1147 p=PushLongPixel(MSBEndian,p,&pixel);
1148 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1149 p=PushLongPixel(MSBEndian,p,&pixel);
1150 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1151 p=PushLongPixel(MSBEndian,p,&pixel);
1152 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1153 SetPixelAlpha(image,OpaqueAlpha,q);
1154 if (image->alpha_trait == BlendPixelTrait)
1156 p=PushLongPixel(MSBEndian,p,&pixel);
1157 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1159 q+=GetPixelChannels(image);
1168 if (image->depth <= 8)
1173 for (x=0; x < (ssize_t) image->columns; x++)
1175 p=PushCharPixel(p,&pixel);
1176 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1177 p=PushCharPixel(p,&pixel);
1178 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1179 p=PushCharPixel(p,&pixel);
1180 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1181 SetPixelAlpha(image,OpaqueAlpha,q);
1182 if (image->alpha_trait == BlendPixelTrait)
1184 p=PushCharPixel(p,&pixel);
1185 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1187 q+=GetPixelChannels(image);
1191 if (image->depth <= 16)
1196 for (x=0; x < (ssize_t) image->columns; x++)
1198 p=PushShortPixel(MSBEndian,p,&pixel);
1199 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1200 p=PushShortPixel(MSBEndian,p,&pixel);
1201 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1202 p=PushShortPixel(MSBEndian,p,&pixel);
1203 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1204 SetPixelAlpha(image,OpaqueAlpha,q);
1205 if (image->alpha_trait == BlendPixelTrait)
1207 p=PushShortPixel(MSBEndian,p,&pixel);
1208 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1211 q+=GetPixelChannels(image);
1215 for (x=0; x < (ssize_t) image->columns; x++)
1217 p=PushLongPixel(MSBEndian,p,&pixel);
1218 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1219 p=PushLongPixel(MSBEndian,p,&pixel);
1220 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1221 p=PushLongPixel(MSBEndian,p,&pixel);
1222 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1223 SetPixelAlpha(image,OpaqueAlpha,q);
1224 if (image->alpha_trait == BlendPixelTrait)
1226 p=PushLongPixel(MSBEndian,p,&pixel);
1227 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1229 q+=GetPixelChannels(image);
1236 sync=SyncAuthenticPixels(image,exception);
1237 if (sync == MagickFalse)
1240 quantum_info=DestroyQuantumInfo(quantum_info);
1241 if (status == MagickFalse)
1242 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1243 SetQuantumImageType(image,quantum_type);
1250 Convert PFM raster image to pixel packets.
1253 (void) SetImageColorspace(image,GRAYColorspace,exception);
1254 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1255 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1257 quantum_info=AcquireQuantumInfo(image_info,image);
1258 if (quantum_info == (QuantumInfo *) NULL)
1259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1260 status=SetQuantumDepth(image,quantum_info,32);
1261 if (status == MagickFalse)
1262 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1263 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1264 if (status == MagickFalse)
1265 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1266 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1267 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1268 for (y=0; y < (ssize_t) image->rows; y++)
1286 if (status == MagickFalse)
1288 pixels=GetQuantumPixels(quantum_info);
1290 count=ReadBlob(image,extent,pixels);
1291 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1292 (image->previous == (Image *) NULL))
1297 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1299 if (proceed == MagickFalse)
1304 if ((size_t) count != extent)
1306 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1307 image->columns,1,exception);
1308 if (q == (Quantum *) NULL)
1313 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1314 quantum_type,pixels,exception);
1315 if (length != extent)
1317 sync=SyncAuthenticPixels(image,exception);
1318 if (sync == MagickFalse)
1321 quantum_info=DestroyQuantumInfo(quantum_info);
1322 if (status == MagickFalse)
1323 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1324 SetQuantumImageType(image,quantum_type);
1328 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1330 if (EOFBlob(image) != MagickFalse)
1332 (void) ThrowMagickException(exception,GetMagickModule(),
1333 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1337 Proceed to next image.
1339 if (image_info->number_scenes != 0)
1340 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1342 if ((format == '1') || (format == '2') || (format == '3'))
1346 Skip to end of line.
1348 count=ReadBlob(image,1,(unsigned char *) &format);
1351 if ((count != 0) && (format == 'P'))
1353 } while (format != '\n');
1354 count=ReadBlob(image,1,(unsigned char *) &format);
1355 if ((count == 1) && (format == 'P'))
1358 Allocate next image structure.
1360 AcquireNextImage(image_info,image,exception);
1361 if (GetNextImageInList(image) == (Image *) NULL)
1363 image=DestroyImageList(image);
1364 return((Image *) NULL);
1366 image=SyncNextImageInList(image);
1367 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1368 GetBlobSize(image));
1369 if (status == MagickFalse)
1372 } while ((count == 1) && (format == 'P'));
1373 (void) CloseBlob(image);
1374 return(GetFirstImageInList(image));
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % R e g i s t e r P N M I m a g e %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % RegisterPNMImage() adds properties for the PNM image format to
1389 % the list of supported formats. The properties include the image format
1390 % tag, a method to read and/or write the format, whether the format
1391 % supports the saving of more than one frame to the same file or blob,
1392 % whether the format supports native in-memory I/O, and a brief
1393 % description of the format.
1395 % The format of the RegisterPNMImage method is:
1397 % size_t RegisterPNMImage(void)
1400 ModuleExport size_t RegisterPNMImage(void)
1405 entry=SetMagickInfo("PAM");
1406 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1407 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1408 entry->description=ConstantString("Common 2-dimensional bitmap format");
1409 entry->mime_type=ConstantString("image/x-portable-pixmap");
1410 entry->module=ConstantString("PNM");
1411 (void) RegisterMagickInfo(entry);
1412 entry=SetMagickInfo("PBM");
1413 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1414 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1415 entry->description=ConstantString("Portable bitmap format (black and white)");
1416 entry->mime_type=ConstantString("image/x-portable-bitmap");
1417 entry->module=ConstantString("PNM");
1418 (void) RegisterMagickInfo(entry);
1419 entry=SetMagickInfo("PFM");
1420 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1421 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1422 entry->endian_support=MagickTrue;
1423 entry->description=ConstantString("Portable float format");
1424 entry->module=ConstantString("PFM");
1425 (void) RegisterMagickInfo(entry);
1426 entry=SetMagickInfo("PGM");
1427 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1428 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1429 entry->description=ConstantString("Portable graymap format (gray scale)");
1430 entry->mime_type=ConstantString("image/x-portable-greymap");
1431 entry->module=ConstantString("PNM");
1432 (void) RegisterMagickInfo(entry);
1433 entry=SetMagickInfo("PNM");
1434 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1435 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1436 entry->magick=(IsImageFormatHandler *) IsPNM;
1437 entry->description=ConstantString("Portable anymap");
1438 entry->mime_type=ConstantString("image/x-portable-pixmap");
1439 entry->module=ConstantString("PNM");
1440 (void) RegisterMagickInfo(entry);
1441 entry=SetMagickInfo("PPM");
1442 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1443 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1444 entry->description=ConstantString("Portable pixmap format (color)");
1445 entry->mime_type=ConstantString("image/x-portable-pixmap");
1446 entry->module=ConstantString("PNM");
1447 (void) RegisterMagickInfo(entry);
1448 return(MagickImageCoderSignature);
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 % U n r e g i s t e r P N M I m a g e %
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % UnregisterPNMImage() removes format registrations made by the
1463 % PNM module from the list of supported formats.
1465 % The format of the UnregisterPNMImage method is:
1467 % UnregisterPNMImage(void)
1470 ModuleExport void UnregisterPNMImage(void)
1472 (void) UnregisterMagickInfo("PAM");
1473 (void) UnregisterMagickInfo("PBM");
1474 (void) UnregisterMagickInfo("PGM");
1475 (void) UnregisterMagickInfo("PNM");
1476 (void) UnregisterMagickInfo("PPM");
1480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % W r i t e P N M I m a g e %
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1492 % The format of the WritePNMImage method is:
1494 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1495 % Image *image,ExceptionInfo *exception)
1497 % A description of each parameter follows.
1499 % o image_info: the image info.
1501 % o image: The image.
1503 % o exception: return any errors or warnings in this structure.
1506 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1507 ExceptionInfo *exception)
1510 buffer[MaxTextExtent],
1512 magick[MaxTextExtent];
1535 register unsigned char
1548 Open output image file.
1550 assert(image_info != (const ImageInfo *) NULL);
1551 assert(image_info->signature == MagickSignature);
1552 assert(image != (Image *) NULL);
1553 assert(image->signature == MagickSignature);
1554 if (image->debug != MagickFalse)
1555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1556 assert(exception != (ExceptionInfo *) NULL);
1557 assert(exception->signature == MagickSignature);
1558 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1559 if (status == MagickFalse)
1568 Write PNM file header.
1571 quantum_type=RGBQuantum;
1572 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1573 max_value=GetQuantumRange(image->depth);
1586 if (image_info->compression == NoCompression)
1594 if (IsImageGray(image,exception) != MagickFalse)
1602 if (image_info->compression == NoCompression)
1609 if ((image_info->type != TrueColorType) &&
1610 (IsImageGray(image,exception) != MagickFalse))
1613 if (image_info->compression == NoCompression)
1615 if (IsImageMonochrome(image,exception) != MagickFalse)
1618 if (image_info->compression == NoCompression)
1627 if (image_info->compression == NoCompression)
1632 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1633 (void) WriteBlobString(image,buffer);
1634 value=GetImageProperty(image,"comment",exception);
1635 if (value != (const char *) NULL)
1641 Write comments to file.
1643 (void) WriteBlobByte(image,'#');
1644 for (p=value; *p != '\0'; p++)
1646 (void) WriteBlobByte(image,(unsigned char) *p);
1647 if ((*p == '\n') || (*p == '\r'))
1648 (void) WriteBlobByte(image,'#');
1650 (void) WriteBlobByte(image,'\n');
1654 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1655 (double) image->columns,(double) image->rows);
1656 (void) WriteBlobString(image,buffer);
1661 type[MaxTextExtent];
1666 (void) FormatLocaleString(buffer,MaxTextExtent,
1667 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1669 (void) WriteBlobString(image,buffer);
1670 quantum_type=GetQuantumType(image,exception);
1671 switch (quantum_type)
1677 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1681 case GrayAlphaQuantum:
1684 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1689 quantum_type=RGBQuantum;
1690 if (image->alpha_trait == BlendPixelTrait)
1691 quantum_type=RGBAQuantum;
1693 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1697 if (image->alpha_trait == BlendPixelTrait)
1700 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1702 if (image->depth > 32)
1704 (void) FormatLocaleString(buffer,MaxTextExtent,
1705 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1706 ((MagickOffsetType) GetQuantumRange(image->depth)));
1707 (void) WriteBlobString(image,buffer);
1708 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1710 (void) WriteBlobString(image,buffer);
1713 Convert runextent encoded to PNM raster pixels.
1723 Convert image to a PBM image.
1725 (void) SetImageType(image,BilevelType,exception);
1727 for (y=0; y < (ssize_t) image->rows; y++)
1729 register const Quantum
1735 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1736 if (p == (const Quantum *) NULL)
1738 for (x=0; x < (ssize_t) image->columns; x++)
1740 *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
1743 if ((q-pixels+2) >= 80)
1746 (void) WriteBlob(image,q-pixels,pixels);
1749 p+=GetPixelChannels(image);
1751 if (image->previous == (Image *) NULL)
1753 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1755 if (status == MagickFalse)
1762 (void) WriteBlob(image,q-pixels,pixels);
1772 Convert image to a PGM image.
1774 if (image->depth <= 8)
1775 (void) WriteBlobString(image,"255\n");
1777 if (image->depth <= 16)
1778 (void) WriteBlobString(image,"65535\n");
1780 (void) WriteBlobString(image,"4294967295\n");
1782 for (y=0; y < (ssize_t) image->rows; y++)
1784 register const Quantum
1790 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1791 if (p == (const Quantum *) NULL)
1793 for (x=0; x < (ssize_t) image->columns; x++)
1795 index=ClampToQuantum(GetPixelLuma(image,p));
1796 if (image->depth <= 8)
1797 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1798 ScaleQuantumToChar(index));
1800 if (image->depth <= 16)
1801 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1802 ScaleQuantumToShort(index));
1804 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1805 ScaleQuantumToLong(index));
1806 extent=(size_t) count;
1807 (void) strncpy((char *) q,buffer,extent);
1809 if ((q-pixels+extent) >= 80)
1812 (void) WriteBlob(image,q-pixels,pixels);
1815 p+=GetPixelChannels(image);
1817 if (image->previous == (Image *) NULL)
1819 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1821 if (status == MagickFalse)
1828 (void) WriteBlob(image,q-pixels,pixels);
1838 Convert image to a PNM image.
1840 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1841 if (image->depth <= 8)
1842 (void) WriteBlobString(image,"255\n");
1844 if (image->depth <= 16)
1845 (void) WriteBlobString(image,"65535\n");
1847 (void) WriteBlobString(image,"4294967295\n");
1849 for (y=0; y < (ssize_t) image->rows; y++)
1851 register const Quantum
1857 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1858 if (p == (const Quantum *) NULL)
1860 for (x=0; x < (ssize_t) image->columns; x++)
1862 if (image->depth <= 8)
1863 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1864 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1865 ScaleQuantumToChar(GetPixelGreen(image,p)),
1866 ScaleQuantumToChar(GetPixelBlue(image,p)));
1868 if (image->depth <= 16)
1869 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1870 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1871 ScaleQuantumToShort(GetPixelGreen(image,p)),
1872 ScaleQuantumToShort(GetPixelBlue(image,p)));
1874 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1875 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1876 ScaleQuantumToLong(GetPixelGreen(image,p)),
1877 ScaleQuantumToLong(GetPixelBlue(image,p)));
1878 extent=(size_t) count;
1879 (void) strncpy((char *) q,buffer,extent);
1881 if ((q-pixels+extent) >= 80)
1884 (void) WriteBlob(image,q-pixels,pixels);
1887 p+=GetPixelChannels(image);
1889 if (image->previous == (Image *) NULL)
1891 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1893 if (status == MagickFalse)
1900 (void) WriteBlob(image,q-pixels,pixels);
1907 Convert image to a PBM image.
1909 (void) SetImageType(image,BilevelType,exception);
1911 quantum_info=AcquireQuantumInfo(image_info,image);
1912 if (quantum_info == (QuantumInfo *) NULL)
1913 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1914 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1915 quantum_info->min_is_white=MagickTrue;
1916 pixels=GetQuantumPixels(quantum_info);
1917 for (y=0; y < (ssize_t) image->rows; y++)
1919 register const Quantum
1922 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1923 if (p == (const Quantum *) NULL)
1925 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1926 GrayQuantum,pixels,exception);
1927 count=WriteBlob(image,extent,pixels);
1928 if (count != (ssize_t) extent)
1930 if (image->previous == (Image *) NULL)
1932 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1934 if (status == MagickFalse)
1938 quantum_info=DestroyQuantumInfo(quantum_info);
1944 Convert image to a PGM image.
1946 if (image->depth > 32)
1948 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1949 ((MagickOffsetType) GetQuantumRange(image->depth)));
1950 (void) WriteBlobString(image,buffer);
1951 quantum_info=AcquireQuantumInfo(image_info,image);
1952 if (quantum_info == (QuantumInfo *) NULL)
1953 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1954 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1955 quantum_info->min_is_white=MagickTrue;
1956 pixels=GetQuantumPixels(quantum_info);
1957 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1958 for (y=0; y < (ssize_t) image->rows; y++)
1960 register const Quantum
1966 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1967 if (p == (const Quantum *) NULL)
1970 switch (image->depth)
1976 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1977 GrayQuantum,pixels,exception);
1982 if (image->depth <= 8)
1984 for (x=0; x < (ssize_t) image->columns; x++)
1986 if (IsPixelGray(image,p) == MagickFalse)
1987 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
1988 image,p)),max_value);
1991 if (image->depth == 8)
1992 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1994 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
1997 q=PopCharPixel((unsigned char) pixel,q);
1998 p+=GetPixelChannels(image);
2000 extent=(size_t) (q-pixels);
2003 if (image->depth <= 16)
2005 for (x=0; x < (ssize_t) image->columns; x++)
2007 if (IsPixelGray(image,p) == MagickFalse)
2008 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2012 if (image->depth == 16)
2013 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2015 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2018 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2019 p+=GetPixelChannels(image);
2021 extent=(size_t) (q-pixels);
2024 for (x=0; x < (ssize_t) image->columns; x++)
2026 if (IsPixelGray(image,p) == MagickFalse)
2027 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2031 if (image->depth == 16)
2032 pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2034 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2036 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2037 p+=GetPixelChannels(image);
2039 extent=(size_t) (q-pixels);
2043 count=WriteBlob(image,extent,pixels);
2044 if (count != (ssize_t) extent)
2046 if (image->previous == (Image *) NULL)
2048 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2050 if (status == MagickFalse)
2054 quantum_info=DestroyQuantumInfo(quantum_info);
2060 Convert image to a PNM image.
2062 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2063 if (image->depth > 32)
2065 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2066 ((MagickOffsetType) GetQuantumRange(image->depth)));
2067 (void) WriteBlobString(image,buffer);
2068 quantum_info=AcquireQuantumInfo(image_info,image);
2069 if (quantum_info == (QuantumInfo *) NULL)
2070 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2071 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2072 pixels=GetQuantumPixels(quantum_info);
2073 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2074 for (y=0; y < (ssize_t) image->rows; y++)
2076 register const Quantum
2082 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2083 if (p == (const Quantum *) NULL)
2086 switch (image->depth)
2092 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2093 quantum_type,pixels,exception);
2098 if (image->depth <= 8)
2100 for (x=0; x < (ssize_t) image->columns; x++)
2102 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2103 q=PopCharPixel((unsigned char) pixel,q);
2104 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2105 q=PopCharPixel((unsigned char) pixel,q);
2106 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2107 q=PopCharPixel((unsigned char) pixel,q);
2108 p+=GetPixelChannels(image);
2110 extent=(size_t) (q-pixels);
2113 if (image->depth <= 16)
2115 for (x=0; x < (ssize_t) image->columns; x++)
2117 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2118 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2119 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2120 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2121 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2122 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2123 p+=GetPixelChannels(image);
2125 extent=(size_t) (q-pixels);
2128 for (x=0; x < (ssize_t) image->columns; x++)
2130 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2131 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2132 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2133 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2134 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2135 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2136 p+=GetPixelChannels(image);
2138 extent=(size_t) (q-pixels);
2142 count=WriteBlob(image,extent,pixels);
2143 if (count != (ssize_t) extent)
2145 if (image->previous == (Image *) NULL)
2147 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2149 if (status == MagickFalse)
2153 quantum_info=DestroyQuantumInfo(quantum_info);
2159 Convert image to a PAM.
2161 if (image->depth > 32)
2163 quantum_info=AcquireQuantumInfo(image_info,image);
2164 if (quantum_info == (QuantumInfo *) NULL)
2165 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2166 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2167 pixels=GetQuantumPixels(quantum_info);
2168 for (y=0; y < (ssize_t) image->rows; y++)
2170 register const Quantum
2176 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2177 if (p == (const Quantum *) NULL)
2180 switch (image->depth)
2186 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2187 quantum_type,pixels,exception);
2192 switch (quantum_type)
2195 case GrayAlphaQuantum:
2197 if (image->depth <= 8)
2199 for (x=0; x < (ssize_t) image->columns; x++)
2201 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2202 image,p)),max_value);
2203 q=PopCharPixel((unsigned char) pixel,q);
2204 if (image->alpha_trait == BlendPixelTrait)
2206 pixel=(unsigned char) ScaleQuantumToAny(
2207 GetPixelAlpha(image,p),max_value);
2208 q=PopCharPixel((unsigned char) pixel,q);
2210 p+=GetPixelChannels(image);
2214 if (image->depth <= 16)
2216 for (x=0; x < (ssize_t) image->columns; x++)
2218 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2219 image,p)),max_value);
2220 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2221 if (image->alpha_trait == BlendPixelTrait)
2223 pixel=(unsigned char) ScaleQuantumToAny(
2224 GetPixelAlpha(image,p),max_value);
2225 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2227 p+=GetPixelChannels(image);
2231 for (x=0; x < (ssize_t) image->columns; x++)
2233 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2235 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2236 if (image->alpha_trait == BlendPixelTrait)
2238 pixel=(unsigned char) ScaleQuantumToAny(
2239 GetPixelAlpha(image,p),max_value);
2240 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2242 p+=GetPixelChannels(image);
2249 if (image->depth <= 8)
2251 for (x=0; x < (ssize_t) image->columns; x++)
2253 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2254 q=PopCharPixel((unsigned char) pixel,q);
2255 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2257 q=PopCharPixel((unsigned char) pixel,q);
2258 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2260 q=PopCharPixel((unsigned char) pixel,q);
2261 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2263 q=PopCharPixel((unsigned char) pixel,q);
2264 if (image->alpha_trait == BlendPixelTrait)
2266 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2268 q=PopCharPixel((unsigned char) pixel,q);
2270 p+=GetPixelChannels(image);
2274 if (image->depth <= 16)
2276 for (x=0; x < (ssize_t) image->columns; x++)
2278 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2279 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2280 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2282 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2283 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2285 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2286 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2288 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2289 if (image->alpha_trait == BlendPixelTrait)
2291 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2293 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2295 p+=GetPixelChannels(image);
2299 for (x=0; x < (ssize_t) image->columns; x++)
2301 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2302 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2303 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2304 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2305 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2306 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2307 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2308 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2309 if (image->alpha_trait == BlendPixelTrait)
2311 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2313 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2315 p+=GetPixelChannels(image);
2321 if (image->depth <= 8)
2323 for (x=0; x < (ssize_t) image->columns; x++)
2325 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2326 q=PopCharPixel((unsigned char) pixel,q);
2327 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2329 q=PopCharPixel((unsigned char) pixel,q);
2330 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2332 q=PopCharPixel((unsigned char) pixel,q);
2333 if (image->alpha_trait == BlendPixelTrait)
2335 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2337 q=PopCharPixel((unsigned char) pixel,q);
2339 p+=GetPixelChannels(image);
2343 if (image->depth <= 16)
2345 for (x=0; x < (ssize_t) image->columns; x++)
2347 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2348 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2349 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2351 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2352 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2354 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2355 if (image->alpha_trait == BlendPixelTrait)
2357 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2359 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2361 p+=GetPixelChannels(image);
2365 for (x=0; x < (ssize_t) image->columns; x++)
2367 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2368 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2369 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2370 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2371 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2372 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2373 if (image->alpha_trait == BlendPixelTrait)
2375 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2377 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2379 p+=GetPixelChannels(image);
2384 extent=(size_t) (q-pixels);
2388 count=WriteBlob(image,extent,pixels);
2389 if (count != (ssize_t) extent)
2391 if (image->previous == (Image *) NULL)
2393 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2395 if (status == MagickFalse)
2399 quantum_info=DestroyQuantumInfo(quantum_info);
2405 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2408 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2409 quantum_info=AcquireQuantumInfo(image_info,image);
2410 if (quantum_info == (QuantumInfo *) NULL)
2411 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2412 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2413 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2414 if (status == MagickFalse)
2415 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2416 pixels=GetQuantumPixels(quantum_info);
2417 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2419 register const Quantum
2422 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2423 if (p == (const Quantum *) NULL)
2425 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2426 quantum_type,pixels,exception);
2427 (void) WriteBlob(image,extent,pixels);
2428 if (image->previous == (Image *) NULL)
2430 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2432 if (status == MagickFalse)
2436 quantum_info=DestroyQuantumInfo(quantum_info);
2440 if (GetNextImageInList(image) == (Image *) NULL)
2442 image=SyncNextImageInList(image);
2443 status=SetImageProgress(image,SaveImagesTag,scene++,
2444 GetImageListLength(image));
2445 if (status == MagickFalse)
2447 } while (image_info->adjoin != MagickFalse);
2448 (void) CloseBlob(image);