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.
139 static int PNMComment(Image *image,ExceptionInfo *exception)
156 comment=AcquireString(GetImageProperty(image,"comment",exception));
157 p=comment+strlen(comment);
158 extent=strlen(comment)+MaxTextExtent;
159 for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
161 if ((size_t) (p-comment+1) >= extent)
164 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
166 if (comment == (char *) NULL)
168 p=comment+strlen(comment);
170 c=ReadBlobByte(image);
177 if (comment == (char *) NULL)
179 (void) SetImageProperty(image,"comment",comment,exception);
180 comment=DestroyString(comment);
184 static unsigned int PNMInteger(Image *image,const unsigned int base,
185 ExceptionInfo *exception)
194 Skip any leading whitespace.
198 c=ReadBlobByte(image);
202 c=PNMComment(image,exception);
203 } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
205 return((unsigned int) (c-(int) '0'));
210 while (isdigit(c) != 0) {
211 if (value > (unsigned int) (INT_MAX/10))
214 if (value > (unsigned int) (INT_MAX-(c-(int) '0')))
217 c=ReadBlobByte(image);
224 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
260 assert(image_info != (const ImageInfo *) NULL);
261 assert(image_info->signature == MagickSignature);
262 if (image_info->debug != MagickFalse)
263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
264 image_info->filename);
265 assert(exception != (ExceptionInfo *) NULL);
266 assert(exception->signature == MagickSignature);
267 image=AcquireImage(image_info,exception);
268 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
269 if (status == MagickFalse)
271 image=DestroyImageList(image);
272 return((Image *) NULL);
277 count=ReadBlob(image,1,(unsigned char *) &format);
281 Initialize image structure.
283 if ((count != 1) || (format != 'P'))
284 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
286 quantum_type=RGBQuantum;
288 format=(char) ReadBlobByte(image);
292 PBM, PGM, PPM, and PNM.
294 image->columns=(size_t) PNMInteger(image,10,exception);
295 image->rows=(size_t) PNMInteger(image,10,exception);
296 if ((format == 'f') || (format == 'F'))
299 scale[MaxTextExtent];
301 (void) ReadBlobString(image,scale);
302 quantum_scale=StringToDouble(scale,(char **) NULL);
306 if ((format == '1') || (format == '4'))
307 max_value=1; /* bitmap */
309 max_value=(QuantumAny) PNMInteger(image,10,exception);
315 keyword[MaxTextExtent],
316 value[MaxTextExtent];
327 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
329 while (isspace((int) ((unsigned char) c)) != 0)
330 c=ReadBlobByte(image);
336 c=PNMComment(image,exception);
337 c=ReadBlobByte(image);
338 while (isspace((int) ((unsigned char) c)) != 0)
339 c=ReadBlobByte(image);
344 if ((size_t) (p-keyword) < (MaxTextExtent-1))
346 c=ReadBlobByte(image);
347 } while (isalnum(c));
349 if (LocaleCompare(keyword,"endhdr") == 0)
351 while (isspace((int) ((unsigned char) c)) != 0)
352 c=ReadBlobByte(image);
354 while (isalnum(c) || (c == '_'))
356 if ((size_t) (p-value) < (MaxTextExtent-1))
358 c=ReadBlobByte(image);
362 Assign a value to the specified keyword.
364 if (LocaleCompare(keyword,"depth") == 0)
365 packet_size=StringToUnsignedLong(value);
367 if (LocaleCompare(keyword,"height") == 0)
368 image->rows=StringToUnsignedLong(value);
369 if (LocaleCompare(keyword,"maxval") == 0)
370 max_value=StringToUnsignedLong(value);
371 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
373 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
375 (void) SetImageColorspace(image,GRAYColorspace,exception);
376 quantum_type=GrayQuantum;
378 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
380 (void) SetImageColorspace(image,GRAYColorspace,exception);
381 image->alpha_trait=BlendPixelTrait;
382 quantum_type=GrayAlphaQuantum;
384 if (LocaleCompare(value,"GRAYSCALE") == 0)
386 quantum_type=GrayQuantum;
387 (void) SetImageColorspace(image,GRAYColorspace,exception);
389 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
391 (void) SetImageColorspace(image,GRAYColorspace,exception);
392 image->alpha_trait=BlendPixelTrait;
393 quantum_type=GrayAlphaQuantum;
395 if (LocaleCompare(value,"RGB_ALPHA") == 0)
397 image->alpha_trait=BlendPixelTrait;
398 quantum_type=RGBAQuantum;
400 if (LocaleCompare(value,"CMYK") == 0)
402 (void) SetImageColorspace(image,CMYKColorspace,exception);
403 quantum_type=CMYKQuantum;
405 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
407 (void) SetImageColorspace(image,CMYKColorspace,exception);
408 image->alpha_trait=BlendPixelTrait;
409 quantum_type=CMYKAQuantum;
412 if (LocaleCompare(keyword,"width") == 0)
413 image->columns=StringToUnsignedLong(value);
416 if ((image->columns == 0) || (image->rows == 0))
417 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
418 if ((max_value == 0) || (max_value > 4294967295))
419 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
420 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
422 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
423 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
425 status=SetImageExtent(image,image->columns,image->rows,exception);
426 if (status == MagickFalse)
427 return(DestroyImageList(image));
429 Convert PNM pixels to runextent-encoded MIFF packets.
437 Convert PBM image to pixel packets.
439 (void) SetImageColorspace(image,GRAYColorspace,exception);
440 for (y=0; y < (ssize_t) image->rows; y++)
448 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
449 if (q == (Quantum *) NULL)
451 for (x=0; x < (ssize_t) image->columns; x++)
453 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
455 q+=GetPixelChannels(image);
457 if (SyncAuthenticPixels(image,exception) == MagickFalse)
459 if (image->previous == (Image *) NULL)
461 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
463 if (status == MagickFalse)
467 image->type=BilevelType;
476 Convert PGM image to pixel packets.
478 (void) SetImageColorspace(image,GRAYColorspace,exception);
479 for (y=0; y < (ssize_t) image->rows; y++)
487 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
488 if (q == (Quantum *) NULL)
490 for (x=0; x < (ssize_t) image->columns; x++)
492 intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),
494 SetPixelGray(image,intensity,q);
495 q+=GetPixelChannels(image);
497 if (SyncAuthenticPixels(image,exception) == MagickFalse)
499 if (image->previous == (Image *) NULL)
501 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
503 if (status == MagickFalse)
507 image->type=GrayscaleType;
513 Convert PNM image to pixel packets.
515 for (y=0; y < (ssize_t) image->rows; y++)
523 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
524 if (q == (Quantum *) NULL)
526 for (x=0; x < (ssize_t) image->columns; x++)
531 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
532 SetPixelRed(image,pixel,q);
533 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
534 SetPixelGreen(image,pixel,q);
535 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
536 SetPixelBlue(image,pixel,q);
537 q+=GetPixelChannels(image);
539 if (SyncAuthenticPixels(image,exception) == MagickFalse)
541 if (image->previous == (Image *) NULL)
543 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
545 if (status == MagickFalse)
554 Convert PBM raw image to pixel packets.
556 (void) SetImageColorspace(image,GRAYColorspace,exception);
557 quantum_type=GrayQuantum;
558 if (image->storage_class == PseudoClass)
559 quantum_type=IndexQuantum;
560 quantum_info=AcquireQuantumInfo(image_info,image);
561 if (quantum_info == (QuantumInfo *) NULL)
562 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
563 SetQuantumMinIsWhite(quantum_info,MagickTrue);
564 extent=GetQuantumExtent(image,quantum_info,quantum_type);
565 for (y=0; y < (ssize_t) image->rows; y++)
583 if (status == MagickFalse)
585 pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
587 if (count != (ssize_t) extent)
589 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
590 (image->previous == (Image *) NULL))
595 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
597 if (proceed == MagickFalse)
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++)
646 register const unsigned char
659 if (status == MagickFalse)
661 pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
663 if (count != (ssize_t) extent)
665 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
666 (image->previous == (Image *) NULL))
671 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
673 if (proceed == MagickFalse)
677 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
678 if (q == (Quantum *) NULL)
684 switch (image->depth)
690 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
691 quantum_type,pixels,exception);
699 if (image->depth <= 8)
704 for (x=0; x < (ssize_t) image->columns; x++)
706 p=PushCharPixel(p,&pixel);
707 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
708 q+=GetPixelChannels(image);
712 if (image->depth <= 16)
717 for (x=0; x < (ssize_t) image->columns; x++)
719 p=PushShortPixel(MSBEndian,p,&pixel);
720 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
721 q+=GetPixelChannels(image);
725 for (x=0; x < (ssize_t) image->columns; x++)
727 p=PushLongPixel(MSBEndian,p,&pixel);
728 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
729 q+=GetPixelChannels(image);
734 sync=SyncAuthenticPixels(image,exception);
735 if (sync == MagickFalse)
738 quantum_info=DestroyQuantumInfo(quantum_info);
739 if (status == MagickFalse)
740 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
741 SetQuantumImageType(image,quantum_type);
747 Convert PNM raster image to pixel packets.
749 quantum_type=RGBQuantum;
750 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
751 quantum_info=AcquireQuantumInfo(image_info,image);
752 if (quantum_info == (QuantumInfo *) NULL)
753 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
754 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
755 for (y=0; y < (ssize_t) image->rows; y++)
763 register const unsigned char
776 if (status == MagickFalse)
778 pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
780 if (count != (ssize_t) extent)
782 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
783 (image->previous == (Image *) NULL))
788 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
790 if (proceed == MagickFalse)
794 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
795 if (q == (Quantum *) NULL)
801 switch (image->depth)
805 for (x=0; x < (ssize_t) image->columns; x++)
807 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
808 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
809 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
810 SetPixelAlpha(image,OpaqueAlpha,q);
811 q+=GetPixelChannels(image);
820 for (x=0; x < (ssize_t) image->columns; x++)
822 p=PushShortPixel(MSBEndian,p,&pixel);
823 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
824 p=PushShortPixel(MSBEndian,p,&pixel);
825 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
826 p=PushShortPixel(MSBEndian,p,&pixel);
827 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
828 SetPixelAlpha(image,OpaqueAlpha,q);
829 q+=GetPixelChannels(image);
838 for (x=0; x < (ssize_t) image->columns; x++)
840 p=PushLongPixel(MSBEndian,p,&pixel);
841 SetPixelRed(image,ScaleLongToQuantum(pixel),q);
842 p=PushLongPixel(MSBEndian,p,&pixel);
843 SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
844 p=PushLongPixel(MSBEndian,p,&pixel);
845 SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
846 SetPixelAlpha(image,OpaqueAlpha,q);
847 q+=GetPixelChannels(image);
856 if (image->depth <= 8)
861 for (x=0; x < (ssize_t) image->columns; x++)
863 p=PushCharPixel(p,&pixel);
864 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
865 p=PushCharPixel(p,&pixel);
866 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
867 p=PushCharPixel(p,&pixel);
868 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
869 SetPixelAlpha(image,OpaqueAlpha,q);
870 q+=GetPixelChannels(image);
874 if (image->depth <= 16)
879 for (x=0; x < (ssize_t) image->columns; x++)
881 p=PushShortPixel(MSBEndian,p,&pixel);
882 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
883 p=PushShortPixel(MSBEndian,p,&pixel);
884 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
885 p=PushShortPixel(MSBEndian,p,&pixel);
886 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
887 SetPixelAlpha(image,OpaqueAlpha,q);
888 q+=GetPixelChannels(image);
892 for (x=0; x < (ssize_t) image->columns; x++)
894 p=PushLongPixel(MSBEndian,p,&pixel);
895 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
896 p=PushLongPixel(MSBEndian,p,&pixel);
897 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
898 p=PushLongPixel(MSBEndian,p,&pixel);
899 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
900 SetPixelAlpha(image,OpaqueAlpha,q);
901 q+=GetPixelChannels(image);
906 sync=SyncAuthenticPixels(image,exception);
907 if (sync == MagickFalse)
910 quantum_info=DestroyQuantumInfo(quantum_info);
911 if (status == MagickFalse)
912 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
921 Convert PAM raster image to pixel packets.
923 switch (quantum_type)
926 case GrayAlphaQuantum:
943 if (image->alpha_trait != UndefinedPixelTrait)
945 if (image->depth <= 8)
946 extent=channels*image->columns;
948 if (image->depth <= 16)
949 extent=2*channels*image->columns;
951 extent=4*channels*image->columns;
952 quantum_info=AcquireQuantumInfo(image_info,image);
953 if (quantum_info == (QuantumInfo *) NULL)
954 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
955 for (y=0; y < (ssize_t) image->rows; y++)
963 register const unsigned char
976 if (status == MagickFalse)
978 pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
980 if (count != (ssize_t) extent)
982 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
983 (image->previous == (Image *) NULL))
988 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
990 if (proceed == MagickFalse)
994 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
995 if (q == (Quantum *) NULL)
1001 switch (image->depth)
1007 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1008 quantum_type,pixels,exception);
1013 switch (quantum_type)
1016 case GrayAlphaQuantum:
1021 if (image->depth <= 8)
1026 for (x=0; x < (ssize_t) image->columns; x++)
1028 p=PushCharPixel(p,&pixel);
1029 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1031 SetPixelAlpha(image,OpaqueAlpha,q);
1032 if (image->alpha_trait != UndefinedPixelTrait)
1034 p=PushCharPixel(p,&pixel);
1035 if (image->depth != 1)
1036 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1039 SetPixelAlpha(image,QuantumRange-
1040 ScaleAnyToQuantum(pixel,max_value),q);
1042 q+=GetPixelChannels(image);
1046 if (image->depth <= 16)
1051 for (x=0; x < (ssize_t) image->columns; x++)
1053 p=PushShortPixel(MSBEndian,p,&pixel);
1054 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1056 SetPixelAlpha(image,OpaqueAlpha,q);
1057 if (image->alpha_trait != UndefinedPixelTrait)
1059 p=PushShortPixel(MSBEndian,p,&pixel);
1060 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1063 q+=GetPixelChannels(image);
1067 for (x=0; x < (ssize_t) image->columns; x++)
1069 p=PushLongPixel(MSBEndian,p,&pixel);
1070 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1071 SetPixelAlpha(image,OpaqueAlpha,q);
1072 if (image->alpha_trait != UndefinedPixelTrait)
1074 p=PushLongPixel(MSBEndian,p,&pixel);
1075 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1078 q+=GetPixelChannels(image);
1088 if (image->depth <= 8)
1093 for (x=0; x < (ssize_t) image->columns; x++)
1095 p=PushCharPixel(p,&pixel);
1096 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1097 p=PushCharPixel(p,&pixel);
1098 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1100 p=PushCharPixel(p,&pixel);
1101 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1103 p=PushCharPixel(p,&pixel);
1104 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1106 SetPixelAlpha(image,OpaqueAlpha,q);
1107 if (image->alpha_trait != UndefinedPixelTrait)
1109 p=PushCharPixel(p,&pixel);
1110 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1113 q+=GetPixelChannels(image);
1117 if (image->depth <= 16)
1122 for (x=0; x < (ssize_t) image->columns; x++)
1124 p=PushShortPixel(MSBEndian,p,&pixel);
1125 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1126 p=PushShortPixel(MSBEndian,p,&pixel);
1127 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1129 p=PushShortPixel(MSBEndian,p,&pixel);
1130 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1132 p=PushShortPixel(MSBEndian,p,&pixel);
1133 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1135 SetPixelAlpha(image,OpaqueAlpha,q);
1136 if (image->alpha_trait != UndefinedPixelTrait)
1138 p=PushShortPixel(MSBEndian,p,&pixel);
1139 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1142 q+=GetPixelChannels(image);
1145 for (x=0; x < (ssize_t) image->columns; x++)
1147 p=PushLongPixel(MSBEndian,p,&pixel);
1148 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1149 p=PushLongPixel(MSBEndian,p,&pixel);
1150 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1151 p=PushLongPixel(MSBEndian,p,&pixel);
1152 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1153 p=PushLongPixel(MSBEndian,p,&pixel);
1154 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1155 SetPixelAlpha(image,OpaqueAlpha,q);
1156 if (image->alpha_trait != UndefinedPixelTrait)
1158 p=PushLongPixel(MSBEndian,p,&pixel);
1159 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1162 q+=GetPixelChannels(image);
1171 if (image->depth <= 8)
1176 for (x=0; x < (ssize_t) image->columns; x++)
1178 p=PushCharPixel(p,&pixel);
1179 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1180 p=PushCharPixel(p,&pixel);
1181 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1183 p=PushCharPixel(p,&pixel);
1184 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1186 SetPixelAlpha(image,OpaqueAlpha,q);
1187 if (image->alpha_trait != UndefinedPixelTrait)
1189 p=PushCharPixel(p,&pixel);
1190 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1193 q+=GetPixelChannels(image);
1197 if (image->depth <= 16)
1202 for (x=0; x < (ssize_t) image->columns; x++)
1204 p=PushShortPixel(MSBEndian,p,&pixel);
1205 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1206 p=PushShortPixel(MSBEndian,p,&pixel);
1207 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1209 p=PushShortPixel(MSBEndian,p,&pixel);
1210 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1212 SetPixelAlpha(image,OpaqueAlpha,q);
1213 if (image->alpha_trait != UndefinedPixelTrait)
1215 p=PushShortPixel(MSBEndian,p,&pixel);
1216 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1219 q+=GetPixelChannels(image);
1223 for (x=0; x < (ssize_t) image->columns; x++)
1225 p=PushLongPixel(MSBEndian,p,&pixel);
1226 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1227 p=PushLongPixel(MSBEndian,p,&pixel);
1228 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1229 p=PushLongPixel(MSBEndian,p,&pixel);
1230 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1231 SetPixelAlpha(image,OpaqueAlpha,q);
1232 if (image->alpha_trait != UndefinedPixelTrait)
1234 p=PushLongPixel(MSBEndian,p,&pixel);
1235 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1238 q+=GetPixelChannels(image);
1245 sync=SyncAuthenticPixels(image,exception);
1246 if (sync == MagickFalse)
1249 quantum_info=DestroyQuantumInfo(quantum_info);
1250 if (status == MagickFalse)
1251 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1252 SetQuantumImageType(image,quantum_type);
1259 Convert PFM raster image to pixel packets.
1262 (void) SetImageColorspace(image,GRAYColorspace,exception);
1263 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1264 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1266 quantum_info=AcquireQuantumInfo(image_info,image);
1267 if (quantum_info == (QuantumInfo *) NULL)
1268 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1269 status=SetQuantumDepth(image,quantum_info,32);
1270 if (status == MagickFalse)
1271 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1272 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1273 if (status == MagickFalse)
1274 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1275 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1276 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1277 for (y=0; y < (ssize_t) image->rows; y++)
1295 if (status == MagickFalse)
1297 pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
1299 if ((size_t) count != extent)
1301 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1302 (image->previous == (Image *) NULL))
1307 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1309 if (proceed == MagickFalse)
1313 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1314 image->columns,1,exception);
1315 if (q == (Quantum *) NULL)
1320 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1321 quantum_type,pixels,exception);
1322 if (length != extent)
1324 sync=SyncAuthenticPixels(image,exception);
1325 if (sync == MagickFalse)
1328 quantum_info=DestroyQuantumInfo(quantum_info);
1329 if (status == MagickFalse)
1330 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1331 SetQuantumImageType(image,quantum_type);
1335 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1337 if (EOFBlob(image) != MagickFalse)
1339 (void) ThrowMagickException(exception,GetMagickModule(),
1340 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1344 Proceed to next image.
1346 if (image_info->number_scenes != 0)
1347 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1349 if ((format == '1') || (format == '2') || (format == '3'))
1353 Skip to end of line.
1355 count=ReadBlob(image,1,(unsigned char *) &format);
1360 } while (format != '\n');
1361 count=ReadBlob(image,1,(unsigned char *) &format);
1362 if ((count == 1) && (format == 'P'))
1365 Allocate next image structure.
1367 AcquireNextImage(image_info,image,exception);
1368 if (GetNextImageInList(image) == (Image *) NULL)
1370 image=DestroyImageList(image);
1371 return((Image *) NULL);
1373 image=SyncNextImageInList(image);
1374 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1375 GetBlobSize(image));
1376 if (status == MagickFalse)
1379 } while ((count == 1) && (format == 'P'));
1380 (void) CloseBlob(image);
1381 return(GetFirstImageInList(image));
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 % R e g i s t e r P N M I m a g e %
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % RegisterPNMImage() adds properties for the PNM image format to
1396 % the list of supported formats. The properties include the image format
1397 % tag, a method to read and/or write the format, whether the format
1398 % supports the saving of more than one frame to the same file or blob,
1399 % whether the format supports native in-memory I/O, and a brief
1400 % description of the format.
1402 % The format of the RegisterPNMImage method is:
1404 % size_t RegisterPNMImage(void)
1407 ModuleExport size_t RegisterPNMImage(void)
1412 entry=AcquireMagickInfo("PNM","PAM","Common 2-dimensional bitmap format");
1413 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1414 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1415 entry->mime_type=ConstantString("image/x-portable-pixmap");
1416 (void) RegisterMagickInfo(entry);
1417 entry=AcquireMagickInfo("PNM","PBM",
1418 "Portable bitmap format (black and white)");
1419 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1420 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1421 entry->mime_type=ConstantString("image/x-portable-bitmap");
1422 (void) RegisterMagickInfo(entry);
1423 entry=AcquireMagickInfo("PNM","PFM","Portable float format");
1424 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1425 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1426 entry->flags|=CoderEndianSupportFlag;
1427 (void) RegisterMagickInfo(entry);
1428 entry=AcquireMagickInfo("PNM","PGM","Portable graymap format (gray scale)");
1429 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1430 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1431 entry->mime_type=ConstantString("image/x-portable-greymap");
1432 (void) RegisterMagickInfo(entry);
1433 entry=AcquireMagickInfo("PNM","PNM","Portable anymap");
1434 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1435 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1436 entry->magick=(IsImageFormatHandler *) IsPNM;
1437 entry->mime_type=ConstantString("image/x-portable-pixmap");
1438 (void) RegisterMagickInfo(entry);
1439 entry=AcquireMagickInfo("PNM","PPM","Portable pixmap format (color)");
1440 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1441 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1442 entry->mime_type=ConstantString("image/x-portable-pixmap");
1443 (void) RegisterMagickInfo(entry);
1444 return(MagickImageCoderSignature);
1448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452 % U n r e g i s t e r P N M I m a g e %
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % UnregisterPNMImage() removes format registrations made by the
1459 % PNM module from the list of supported formats.
1461 % The format of the UnregisterPNMImage method is:
1463 % UnregisterPNMImage(void)
1466 ModuleExport void UnregisterPNMImage(void)
1468 (void) UnregisterMagickInfo("PAM");
1469 (void) UnregisterMagickInfo("PBM");
1470 (void) UnregisterMagickInfo("PGM");
1471 (void) UnregisterMagickInfo("PNM");
1472 (void) UnregisterMagickInfo("PPM");
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480 % W r i t e P N M I m a g e %
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1488 % The format of the WritePNMImage method is:
1490 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1491 % Image *image,ExceptionInfo *exception)
1493 % A description of each parameter follows.
1495 % o image_info: the image info.
1497 % o image: The image.
1499 % o exception: return any errors or warnings in this structure.
1502 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1503 ExceptionInfo *exception)
1506 buffer[MaxTextExtent],
1508 magick[MaxTextExtent];
1531 register unsigned char
1544 Open output image file.
1546 assert(image_info != (const ImageInfo *) NULL);
1547 assert(image_info->signature == MagickSignature);
1548 assert(image != (Image *) NULL);
1549 assert(image->signature == MagickSignature);
1550 if (image->debug != MagickFalse)
1551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1552 assert(exception != (ExceptionInfo *) NULL);
1553 assert(exception->signature == MagickSignature);
1554 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1555 if (status == MagickFalse)
1564 Write PNM file header.
1567 quantum_type=RGBQuantum;
1568 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1569 max_value=GetQuantumRange(image->depth);
1582 if (image_info->compression == NoCompression)
1590 if (SetImageGray(image,exception) != MagickFalse)
1598 if (image_info->compression == NoCompression)
1605 if ((image_info->type != TrueColorType) &&
1606 (SetImageGray(image,exception) != MagickFalse))
1609 if (image_info->compression == NoCompression)
1611 if (SetImageMonochrome(image,exception) != MagickFalse)
1614 if (image_info->compression == NoCompression)
1623 if (image_info->compression == NoCompression)
1628 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1629 (void) WriteBlobString(image,buffer);
1630 value=GetImageProperty(image,"comment",exception);
1631 if (value != (const char *) NULL)
1637 Write comments to file.
1639 (void) WriteBlobByte(image,'#');
1640 for (p=value; *p != '\0'; p++)
1642 (void) WriteBlobByte(image,(unsigned char) *p);
1643 if ((*p == '\n') || (*p == '\r'))
1644 (void) WriteBlobByte(image,'#');
1646 (void) WriteBlobByte(image,'\n');
1650 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1651 (double) image->columns,(double) image->rows);
1652 (void) WriteBlobString(image,buffer);
1657 type[MaxTextExtent];
1662 (void) FormatLocaleString(buffer,MaxTextExtent,
1663 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1665 (void) WriteBlobString(image,buffer);
1666 quantum_type=GetQuantumType(image,exception);
1667 switch (quantum_type)
1673 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1677 case GrayAlphaQuantum:
1680 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1685 quantum_type=RGBQuantum;
1686 if (image->alpha_trait != UndefinedPixelTrait)
1687 quantum_type=RGBAQuantum;
1689 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1693 if (image->alpha_trait != UndefinedPixelTrait)
1696 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1698 if (image->depth > 32)
1700 (void) FormatLocaleString(buffer,MaxTextExtent,
1701 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1702 ((MagickOffsetType) GetQuantumRange(image->depth)));
1703 (void) WriteBlobString(image,buffer);
1704 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1706 (void) WriteBlobString(image,buffer);
1709 Convert runextent encoded to PNM raster pixels.
1719 Convert image to a PBM image.
1721 (void) SetImageType(image,BilevelType,exception);
1723 for (y=0; y < (ssize_t) image->rows; y++)
1725 register const Quantum
1731 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1732 if (p == (const Quantum *) NULL)
1734 for (x=0; x < (ssize_t) image->columns; x++)
1736 *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
1739 if ((q-pixels+2) >= 80)
1742 (void) WriteBlob(image,q-pixels,pixels);
1745 p+=GetPixelChannels(image);
1747 if (image->previous == (Image *) NULL)
1749 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1751 if (status == MagickFalse)
1758 (void) WriteBlob(image,q-pixels,pixels);
1768 Convert image to a PGM image.
1770 if (image->depth <= 8)
1771 (void) WriteBlobString(image,"255\n");
1773 if (image->depth <= 16)
1774 (void) WriteBlobString(image,"65535\n");
1776 (void) WriteBlobString(image,"4294967295\n");
1778 for (y=0; y < (ssize_t) image->rows; y++)
1780 register const Quantum
1786 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1787 if (p == (const Quantum *) NULL)
1789 for (x=0; x < (ssize_t) image->columns; x++)
1791 index=ClampToQuantum(GetPixelLuma(image,p));
1792 if (image->depth <= 8)
1793 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1794 ScaleQuantumToChar(index));
1796 if (image->depth <= 16)
1797 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1798 ScaleQuantumToShort(index));
1800 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1801 ScaleQuantumToLong(index));
1802 extent=(size_t) count;
1803 (void) strncpy((char *) q,buffer,extent);
1805 if ((q-pixels+extent) >= 80)
1808 (void) WriteBlob(image,q-pixels,pixels);
1811 p+=GetPixelChannels(image);
1813 if (image->previous == (Image *) NULL)
1815 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1817 if (status == MagickFalse)
1824 (void) WriteBlob(image,q-pixels,pixels);
1834 Convert image to a PNM image.
1836 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1837 if (image->depth <= 8)
1838 (void) WriteBlobString(image,"255\n");
1840 if (image->depth <= 16)
1841 (void) WriteBlobString(image,"65535\n");
1843 (void) WriteBlobString(image,"4294967295\n");
1845 for (y=0; y < (ssize_t) image->rows; y++)
1847 register const Quantum
1853 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1854 if (p == (const Quantum *) NULL)
1856 for (x=0; x < (ssize_t) image->columns; x++)
1858 if (image->depth <= 8)
1859 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1860 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1861 ScaleQuantumToChar(GetPixelGreen(image,p)),
1862 ScaleQuantumToChar(GetPixelBlue(image,p)));
1864 if (image->depth <= 16)
1865 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1866 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1867 ScaleQuantumToShort(GetPixelGreen(image,p)),
1868 ScaleQuantumToShort(GetPixelBlue(image,p)));
1870 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1871 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1872 ScaleQuantumToLong(GetPixelGreen(image,p)),
1873 ScaleQuantumToLong(GetPixelBlue(image,p)));
1874 extent=(size_t) count;
1875 (void) strncpy((char *) q,buffer,extent);
1877 if ((q-pixels+extent) >= 80)
1880 (void) WriteBlob(image,q-pixels,pixels);
1883 p+=GetPixelChannels(image);
1885 if (image->previous == (Image *) NULL)
1887 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1889 if (status == MagickFalse)
1896 (void) WriteBlob(image,q-pixels,pixels);
1903 Convert image to a PBM image.
1905 (void) SetImageType(image,BilevelType,exception);
1907 quantum_info=AcquireQuantumInfo(image_info,image);
1908 if (quantum_info == (QuantumInfo *) NULL)
1909 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1910 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1911 quantum_info->min_is_white=MagickTrue;
1912 pixels=GetQuantumPixels(quantum_info);
1913 for (y=0; y < (ssize_t) image->rows; y++)
1915 register const Quantum
1918 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1919 if (p == (const Quantum *) NULL)
1921 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1922 GrayQuantum,pixels,exception);
1923 count=WriteBlob(image,extent,pixels);
1924 if (count != (ssize_t) extent)
1926 if (image->previous == (Image *) NULL)
1928 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1930 if (status == MagickFalse)
1934 quantum_info=DestroyQuantumInfo(quantum_info);
1940 Convert image to a PGM image.
1942 if (image->depth > 32)
1944 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1945 ((MagickOffsetType) GetQuantumRange(image->depth)));
1946 (void) WriteBlobString(image,buffer);
1947 quantum_info=AcquireQuantumInfo(image_info,image);
1948 if (quantum_info == (QuantumInfo *) NULL)
1949 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1950 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1951 quantum_info->min_is_white=MagickTrue;
1952 pixels=GetQuantumPixels(quantum_info);
1953 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1954 for (y=0; y < (ssize_t) image->rows; y++)
1956 register const Quantum
1962 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1963 if (p == (const Quantum *) NULL)
1966 switch (image->depth)
1972 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1973 GrayQuantum,pixels,exception);
1978 if (image->depth <= 8)
1980 for (x=0; x < (ssize_t) image->columns; x++)
1982 if (IsPixelGray(image,p) == MagickFalse)
1983 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
1984 image,p)),max_value);
1987 if (image->depth == 8)
1988 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1990 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
1993 q=PopCharPixel((unsigned char) pixel,q);
1994 p+=GetPixelChannels(image);
1996 extent=(size_t) (q-pixels);
1999 if (image->depth <= 16)
2001 for (x=0; x < (ssize_t) image->columns; x++)
2003 if (IsPixelGray(image,p) == MagickFalse)
2004 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2008 if (image->depth == 16)
2009 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2011 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2014 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2015 p+=GetPixelChannels(image);
2017 extent=(size_t) (q-pixels);
2020 for (x=0; x < (ssize_t) image->columns; x++)
2022 if (IsPixelGray(image,p) == MagickFalse)
2023 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2027 if (image->depth == 16)
2028 pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2030 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2032 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2033 p+=GetPixelChannels(image);
2035 extent=(size_t) (q-pixels);
2039 count=WriteBlob(image,extent,pixels);
2040 if (count != (ssize_t) extent)
2042 if (image->previous == (Image *) NULL)
2044 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2046 if (status == MagickFalse)
2050 quantum_info=DestroyQuantumInfo(quantum_info);
2056 Convert image to a PNM image.
2058 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2059 if (image->depth > 32)
2061 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2062 ((MagickOffsetType) GetQuantumRange(image->depth)));
2063 (void) WriteBlobString(image,buffer);
2064 quantum_info=AcquireQuantumInfo(image_info,image);
2065 if (quantum_info == (QuantumInfo *) NULL)
2066 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2067 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2068 pixels=GetQuantumPixels(quantum_info);
2069 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2070 for (y=0; y < (ssize_t) image->rows; y++)
2072 register const Quantum
2078 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2079 if (p == (const Quantum *) NULL)
2082 switch (image->depth)
2088 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2089 quantum_type,pixels,exception);
2094 if (image->depth <= 8)
2096 for (x=0; x < (ssize_t) image->columns; x++)
2098 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2099 q=PopCharPixel((unsigned char) pixel,q);
2100 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2101 q=PopCharPixel((unsigned char) pixel,q);
2102 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2103 q=PopCharPixel((unsigned char) pixel,q);
2104 p+=GetPixelChannels(image);
2106 extent=(size_t) (q-pixels);
2109 if (image->depth <= 16)
2111 for (x=0; x < (ssize_t) image->columns; x++)
2113 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2114 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2115 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2116 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2117 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2118 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2119 p+=GetPixelChannels(image);
2121 extent=(size_t) (q-pixels);
2124 for (x=0; x < (ssize_t) image->columns; x++)
2126 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2127 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2128 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2129 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2130 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2131 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2132 p+=GetPixelChannels(image);
2134 extent=(size_t) (q-pixels);
2138 count=WriteBlob(image,extent,pixels);
2139 if (count != (ssize_t) extent)
2141 if (image->previous == (Image *) NULL)
2143 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2145 if (status == MagickFalse)
2149 quantum_info=DestroyQuantumInfo(quantum_info);
2155 Convert image to a PAM.
2157 if (image->depth > 32)
2159 quantum_info=AcquireQuantumInfo(image_info,image);
2160 if (quantum_info == (QuantumInfo *) NULL)
2161 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2162 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2163 pixels=GetQuantumPixels(quantum_info);
2164 for (y=0; y < (ssize_t) image->rows; y++)
2166 register const Quantum
2172 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2173 if (p == (const Quantum *) NULL)
2176 switch (image->depth)
2182 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2183 quantum_type,pixels,exception);
2188 switch (quantum_type)
2191 case GrayAlphaQuantum:
2193 if (image->depth <= 8)
2195 for (x=0; x < (ssize_t) image->columns; x++)
2197 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2198 image,p)),max_value);
2199 q=PopCharPixel((unsigned char) pixel,q);
2200 if (image->alpha_trait != UndefinedPixelTrait)
2202 pixel=(unsigned char) ScaleQuantumToAny(
2203 GetPixelAlpha(image,p),max_value);
2204 q=PopCharPixel((unsigned char) pixel,q);
2206 p+=GetPixelChannels(image);
2210 if (image->depth <= 16)
2212 for (x=0; x < (ssize_t) image->columns; x++)
2214 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2215 image,p)),max_value);
2216 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2217 if (image->alpha_trait != UndefinedPixelTrait)
2219 pixel=(unsigned char) ScaleQuantumToAny(
2220 GetPixelAlpha(image,p),max_value);
2221 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2223 p+=GetPixelChannels(image);
2227 for (x=0; x < (ssize_t) image->columns; x++)
2229 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2231 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2232 if (image->alpha_trait != UndefinedPixelTrait)
2234 pixel=(unsigned char) ScaleQuantumToAny(
2235 GetPixelAlpha(image,p),max_value);
2236 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2238 p+=GetPixelChannels(image);
2245 if (image->depth <= 8)
2247 for (x=0; x < (ssize_t) image->columns; x++)
2249 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2250 q=PopCharPixel((unsigned char) pixel,q);
2251 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2253 q=PopCharPixel((unsigned char) pixel,q);
2254 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2256 q=PopCharPixel((unsigned char) pixel,q);
2257 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2259 q=PopCharPixel((unsigned char) pixel,q);
2260 if (image->alpha_trait != UndefinedPixelTrait)
2262 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2264 q=PopCharPixel((unsigned char) pixel,q);
2266 p+=GetPixelChannels(image);
2270 if (image->depth <= 16)
2272 for (x=0; x < (ssize_t) image->columns; x++)
2274 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2275 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2276 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2278 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2279 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2281 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2282 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2284 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2285 if (image->alpha_trait != UndefinedPixelTrait)
2287 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2289 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2291 p+=GetPixelChannels(image);
2295 for (x=0; x < (ssize_t) image->columns; x++)
2297 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2298 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2299 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2300 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2301 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2302 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2303 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2304 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2305 if (image->alpha_trait != UndefinedPixelTrait)
2307 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2309 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2311 p+=GetPixelChannels(image);
2317 if (image->depth <= 8)
2319 for (x=0; x < (ssize_t) image->columns; x++)
2321 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2322 q=PopCharPixel((unsigned char) pixel,q);
2323 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2325 q=PopCharPixel((unsigned char) pixel,q);
2326 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2328 q=PopCharPixel((unsigned char) pixel,q);
2329 if (image->alpha_trait != UndefinedPixelTrait)
2331 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2333 q=PopCharPixel((unsigned char) pixel,q);
2335 p+=GetPixelChannels(image);
2339 if (image->depth <= 16)
2341 for (x=0; x < (ssize_t) image->columns; x++)
2343 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2344 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2345 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2347 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2348 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2350 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2351 if (image->alpha_trait != UndefinedPixelTrait)
2353 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2355 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2357 p+=GetPixelChannels(image);
2361 for (x=0; x < (ssize_t) image->columns; x++)
2363 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2364 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2365 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2366 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2367 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2368 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2369 if (image->alpha_trait != UndefinedPixelTrait)
2371 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2373 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2375 p+=GetPixelChannels(image);
2380 extent=(size_t) (q-pixels);
2384 count=WriteBlob(image,extent,pixels);
2385 if (count != (ssize_t) extent)
2387 if (image->previous == (Image *) NULL)
2389 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2391 if (status == MagickFalse)
2395 quantum_info=DestroyQuantumInfo(quantum_info);
2401 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2404 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2405 quantum_info=AcquireQuantumInfo(image_info,image);
2406 if (quantum_info == (QuantumInfo *) NULL)
2407 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2408 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2409 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2410 if (status == MagickFalse)
2411 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2412 pixels=GetQuantumPixels(quantum_info);
2413 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2415 register const Quantum
2418 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2419 if (p == (const Quantum *) NULL)
2421 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2422 quantum_type,pixels,exception);
2423 (void) WriteBlob(image,extent,pixels);
2424 if (image->previous == (Image *) NULL)
2426 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2428 if (status == MagickFalse)
2432 quantum_info=DestroyQuantumInfo(quantum_info);
2436 if (GetNextImageInList(image) == (Image *) NULL)
2438 image=SyncNextImageInList(image);
2439 status=SetImageProgress(image,SaveImagesTag,scene++,
2440 GetImageListLength(image));
2441 if (status == MagickFalse)
2443 } while (image_info->adjoin != MagickFalse);
2444 (void) CloseBlob(image);