2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
72 static MagickBooleanType
73 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsPNM() returns MagickTrue if the image format type, identified by the
87 % magick string, is PNM.
89 % The format of the IsPNM method is:
91 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o extent: Specifies the extent of the magick string.
100 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
104 if ((*magick == (unsigned char) 'P') &&
105 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
106 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
107 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d P N M I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % ReadPNMImage() reads a Portable Anymap image file and returns it.
124 % It allocates the memory necessary for the new Image structure and returns
125 % a pointer to the new image.
127 % The format of the ReadPNMImage method is:
129 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image_info: the image info.
135 % o exception: return any errors or warnings in this structure.
139 static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 const size_t extent,ExceptionInfo *exception)
142 if ((offset < 0) || (offset > (ssize_t) extent))
144 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
145 "InvalidPixel","`%s'",image->filename);
151 static void PNMComment(Image *image,ExceptionInfo *exception)
168 comment=AcquireString(GetImageProperty(image,"comment",exception));
169 extent=strlen(comment);
170 p=comment+strlen(comment);
171 for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
173 if ((size_t) (p-comment+1) >= extent)
176 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
178 if (comment == (char *) NULL)
180 p=comment+strlen(comment);
182 c=ReadBlobByte(image);
189 if (comment == (char *) NULL)
191 (void) SetImageProperty(image,"comment",comment,exception);
192 comment=DestroyString(comment);
195 static size_t PNMInteger(Image *image,const unsigned int base,
196 ExceptionInfo *exception)
205 Skip any leading whitespace.
209 c=ReadBlobByte(image);
213 PNMComment(image,exception);
214 } while (isdigit(c) == MagickFalse);
216 return((size_t) (c-(int) '0'));
225 c=ReadBlobByte(image);
228 } while (isdigit(c) != MagickFalse);
232 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
268 assert(image_info != (const ImageInfo *) NULL);
269 assert(image_info->signature == MagickSignature);
270 if (image_info->debug != MagickFalse)
271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
272 image_info->filename);
273 assert(exception != (ExceptionInfo *) NULL);
274 assert(exception->signature == MagickSignature);
275 image=AcquireImage(image_info,exception);
276 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
277 if (status == MagickFalse)
279 image=DestroyImageList(image);
280 return((Image *) NULL);
285 count=ReadBlob(image,1,(unsigned char *) &format);
289 Initialize image structure.
291 if ((count != 1) || (format != 'P'))
292 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
294 quantum_type=RGBQuantum;
296 format=(char) ReadBlobByte(image);
300 PBM, PGM, PPM, and PNM.
302 image->columns=PNMInteger(image,10,exception);
303 image->rows=PNMInteger(image,10,exception);
304 if ((format == 'f') || (format == 'F'))
307 scale[MaxTextExtent];
309 (void) ReadBlobString(image,scale);
310 quantum_scale=StringToDouble(scale,(char **) NULL);
314 if ((format == '1') || (format == '4'))
315 max_value=1; /* bitmap */
317 max_value=PNMInteger(image,10,exception);
323 keyword[MaxTextExtent],
324 value[MaxTextExtent];
335 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
337 while (isspace((int) ((unsigned char) c)) != 0)
338 c=ReadBlobByte(image);
344 PNMComment(image,exception);
345 c=ReadBlobByte(image);
346 while (isspace((int) ((unsigned char) c)) != 0)
347 c=ReadBlobByte(image);
352 if ((size_t) (p-keyword) < (MaxTextExtent-1))
354 c=ReadBlobByte(image);
355 } while (isalnum(c));
357 if (LocaleCompare(keyword,"endhdr") == 0)
359 while (isspace((int) ((unsigned char) c)) != 0)
360 c=ReadBlobByte(image);
362 while (isalnum(c) || (c == '_'))
364 if ((size_t) (p-value) < (MaxTextExtent-1))
366 c=ReadBlobByte(image);
370 Assign a value to the specified keyword.
372 if (LocaleCompare(keyword,"depth") == 0)
373 packet_size=StringToUnsignedLong(value);
375 if (LocaleCompare(keyword,"height") == 0)
376 image->rows=StringToUnsignedLong(value);
377 if (LocaleCompare(keyword,"maxval") == 0)
378 max_value=StringToUnsignedLong(value);
379 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
381 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
383 (void) SetImageColorspace(image,GRAYColorspace,exception);
384 quantum_type=GrayQuantum;
386 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
388 (void) SetImageColorspace(image,GRAYColorspace,exception);
389 image->alpha_trait=BlendPixelTrait;
390 quantum_type=GrayAlphaQuantum;
392 if (LocaleCompare(value,"GRAYSCALE") == 0)
394 quantum_type=GrayQuantum;
395 (void) SetImageColorspace(image,GRAYColorspace,exception);
397 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
399 (void) SetImageColorspace(image,GRAYColorspace,exception);
400 image->alpha_trait=BlendPixelTrait;
401 quantum_type=GrayAlphaQuantum;
403 if (LocaleCompare(value,"RGB_ALPHA") == 0)
405 image->alpha_trait=BlendPixelTrait;
406 quantum_type=RGBAQuantum;
408 if (LocaleCompare(value,"CMYK") == 0)
410 (void) SetImageColorspace(image,CMYKColorspace,exception);
411 quantum_type=CMYKQuantum;
413 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
415 (void) SetImageColorspace(image,CMYKColorspace,exception);
416 image->alpha_trait=BlendPixelTrait;
417 quantum_type=CMYKAQuantum;
420 if (LocaleCompare(keyword,"width") == 0)
421 image->columns=StringToUnsignedLong(value);
424 if ((image->columns == 0) || (image->rows == 0))
425 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
426 if (max_value > 4294967295)
427 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
428 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
430 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
431 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
434 Convert PNM pixels to runextent-encoded MIFF packets.
443 Convert PBM image to pixel packets.
445 (void) SetImageColorspace(image,GRAYColorspace,exception);
446 for (y=0; y < (ssize_t) image->rows; y++)
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
457 for (x=0; x < (ssize_t) image->columns; x++)
459 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
461 q+=GetPixelChannels(image);
463 if (SyncAuthenticPixels(image,exception) == MagickFalse)
465 if (image->previous == (Image *) NULL)
467 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
469 if (status == MagickFalse)
473 image->type=BilevelType;
482 Convert PGM image to pixel packets.
484 (void) SetImageColorspace(image,GRAYColorspace,exception);
485 for (y=0; y < (ssize_t) image->rows; y++)
493 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
494 if (q == (Quantum *) NULL)
496 for (x=0; x < (ssize_t) image->columns; x++)
498 intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
499 SetPixelGray(image,intensity,q);
500 q+=GetPixelChannels(image);
502 if (SyncAuthenticPixels(image,exception) == MagickFalse)
504 if (image->previous == (Image *) NULL)
506 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
508 if (status == MagickFalse)
512 image->type=GrayscaleType;
518 Convert PNM image to pixel packets.
520 for (y=0; y < (ssize_t) image->rows; y++)
528 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
529 if (q == (Quantum *) NULL)
531 for (x=0; x < (ssize_t) image->columns; x++)
536 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
537 SetPixelRed(image,pixel,q);
538 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
539 SetPixelGreen(image,pixel,q);
540 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
541 SetPixelBlue(image,pixel,q);
542 q+=GetPixelChannels(image);
544 if (SyncAuthenticPixels(image,exception) == MagickFalse)
546 if (image->previous == (Image *) NULL)
548 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
550 if (status == MagickFalse)
559 Convert PBM raw image to pixel packets.
561 (void) SetImageColorspace(image,GRAYColorspace,exception);
562 quantum_type=GrayQuantum;
563 if (image->storage_class == PseudoClass)
564 quantum_type=IndexQuantum;
565 quantum_info=AcquireQuantumInfo(image_info,image);
566 if (quantum_info == (QuantumInfo *) NULL)
567 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
568 SetQuantumMinIsWhite(quantum_info,MagickTrue);
569 extent=GetQuantumExtent(image,quantum_info,quantum_type);
570 for (y=0; y < (ssize_t) image->rows; y++)
588 if (status == MagickFalse)
590 pixels=GetQuantumPixels(quantum_info);
592 count=ReadBlob(image,extent,pixels);
593 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
594 (image->previous == (Image *) NULL))
599 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
601 if (proceed == MagickFalse)
606 if (count != (ssize_t) extent)
608 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
609 if (q == (Quantum *) NULL)
614 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
615 quantum_type,pixels,exception);
616 if (length != extent)
618 sync=SyncAuthenticPixels(image,exception);
619 if (sync == MagickFalse)
622 quantum_info=DestroyQuantumInfo(quantum_info);
623 if (status == MagickFalse)
624 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
625 SetQuantumImageType(image,quantum_type);
631 Convert PGM raw image to pixel packets.
633 (void) SetImageColorspace(image,GRAYColorspace,exception);
634 quantum_type=GrayQuantum;
635 if (image->depth <= 8)
636 extent=image->columns;
638 if (image->depth <= 16)
639 extent=2*image->columns;
641 extent=4*image->columns;
642 quantum_info=AcquireQuantumInfo(image_info,image);
643 if (quantum_info == (QuantumInfo *) NULL)
644 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
645 for (y=0; y < (ssize_t) image->rows; y++)
650 register const unsigned char
666 if (status == MagickFalse)
668 pixels=GetQuantumPixels(quantum_info);
670 count=ReadBlob(image,extent,pixels);
671 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
672 (image->previous == (Image *) NULL))
677 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
679 if (proceed == MagickFalse)
684 if (count != (ssize_t) extent)
686 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
687 if (q == (Quantum *) NULL)
693 switch (image->depth)
699 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
700 quantum_type,pixels,exception);
708 if (image->depth <= 8)
713 for (x=0; x < (ssize_t) image->columns; x++)
715 p=PushCharPixel(p,&pixel);
716 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
717 q+=GetPixelChannels(image);
721 if (image->depth <= 16)
726 for (x=0; x < (ssize_t) image->columns; x++)
728 p=PushShortPixel(MSBEndian,p,&pixel);
729 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
730 q+=GetPixelChannels(image);
734 for (x=0; x < (ssize_t) image->columns; x++)
736 p=PushLongPixel(MSBEndian,p,&pixel);
737 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
738 q+=GetPixelChannels(image);
743 sync=SyncAuthenticPixels(image,exception);
744 if (sync == MagickFalse)
747 quantum_info=DestroyQuantumInfo(quantum_info);
748 if (status == MagickFalse)
749 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
750 SetQuantumImageType(image,quantum_type);
756 Convert PNM raster image to pixel packets.
758 quantum_type=RGBQuantum;
759 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
760 quantum_info=AcquireQuantumInfo(image_info,image);
761 if (quantum_info == (QuantumInfo *) NULL)
762 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
763 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
764 for (y=0; y < (ssize_t) image->rows; y++)
769 register const unsigned char
785 if (status == MagickFalse)
787 pixels=GetQuantumPixels(quantum_info);
789 count=ReadBlob(image,extent,pixels);
790 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
791 (image->previous == (Image *) NULL))
796 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
798 if (proceed == MagickFalse)
803 if (count != (ssize_t) extent)
805 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
806 if (q == (Quantum *) NULL)
812 switch (image->depth)
816 for (x=0; x < (ssize_t) image->columns; x++)
818 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
819 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
820 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
821 SetPixelAlpha(image,OpaqueAlpha,q);
822 q+=GetPixelChannels(image);
831 for (x=0; x < (ssize_t) image->columns; x++)
833 p=PushShortPixel(MSBEndian,p,&pixel);
834 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
835 p=PushShortPixel(MSBEndian,p,&pixel);
836 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
837 p=PushShortPixel(MSBEndian,p,&pixel);
838 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
839 SetPixelAlpha(image,OpaqueAlpha,q);
840 q+=GetPixelChannels(image);
849 for (x=0; x < (ssize_t) image->columns; x++)
851 p=PushLongPixel(MSBEndian,p,&pixel);
852 SetPixelRed(image,ScaleLongToQuantum(pixel),q);
853 p=PushLongPixel(MSBEndian,p,&pixel);
854 SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
855 p=PushLongPixel(MSBEndian,p,&pixel);
856 SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
857 SetPixelAlpha(image,OpaqueAlpha,q);
858 q+=GetPixelChannels(image);
867 if (image->depth <= 8)
872 for (x=0; x < (ssize_t) image->columns; x++)
874 p=PushCharPixel(p,&pixel);
875 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
876 p=PushCharPixel(p,&pixel);
877 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
878 p=PushCharPixel(p,&pixel);
879 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
880 SetPixelAlpha(image,OpaqueAlpha,q);
881 q+=GetPixelChannels(image);
885 if (image->depth <= 16)
890 for (x=0; x < (ssize_t) image->columns; x++)
892 p=PushShortPixel(MSBEndian,p,&pixel);
893 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
894 p=PushShortPixel(MSBEndian,p,&pixel);
895 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
896 p=PushShortPixel(MSBEndian,p,&pixel);
897 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
898 SetPixelAlpha(image,OpaqueAlpha,q);
899 q+=GetPixelChannels(image);
903 for (x=0; x < (ssize_t) image->columns; x++)
905 p=PushLongPixel(MSBEndian,p,&pixel);
906 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
907 p=PushLongPixel(MSBEndian,p,&pixel);
908 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
909 p=PushLongPixel(MSBEndian,p,&pixel);
910 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
911 SetPixelAlpha(image,OpaqueAlpha,q);
912 q+=GetPixelChannels(image);
917 sync=SyncAuthenticPixels(image,exception);
918 if (sync == MagickFalse)
921 quantum_info=DestroyQuantumInfo(quantum_info);
922 if (status == MagickFalse)
923 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
932 Convert PAM raster image to pixel packets.
934 switch (quantum_type)
937 case GrayAlphaQuantum:
954 if (image->alpha_trait == BlendPixelTrait)
956 if (image->depth <= 8)
957 extent=channels*image->columns;
959 if (image->depth <= 16)
960 extent=2*channels*image->columns;
962 extent=4*channels*image->columns;
963 quantum_info=AcquireQuantumInfo(image_info,image);
964 if (quantum_info == (QuantumInfo *) NULL)
965 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
966 for (y=0; y < (ssize_t) image->rows; y++)
971 register const unsigned char
987 if (status == MagickFalse)
989 pixels=GetQuantumPixels(quantum_info);
991 count=ReadBlob(image,extent,pixels);
992 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
993 (image->previous == (Image *) NULL))
998 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1000 if (proceed == MagickFalse)
1005 if (count != (ssize_t) extent)
1007 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1008 if (q == (Quantum *) NULL)
1014 switch (image->depth)
1020 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1021 quantum_type,pixels,exception);
1026 switch (quantum_type)
1029 case GrayAlphaQuantum:
1034 if (image->depth <= 8)
1039 for (x=0; x < (ssize_t) image->columns; x++)
1041 p=PushCharPixel(p,&pixel);
1042 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1043 SetPixelAlpha(image,OpaqueAlpha,q);
1044 if (image->alpha_trait == BlendPixelTrait)
1046 p=PushCharPixel(p,&pixel);
1047 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1050 q+=GetPixelChannels(image);
1054 if (image->depth <= 16)
1059 for (x=0; x < (ssize_t) image->columns; x++)
1061 p=PushShortPixel(MSBEndian,p,&pixel);
1062 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1063 SetPixelAlpha(image,OpaqueAlpha,q);
1064 if (image->alpha_trait == BlendPixelTrait)
1066 p=PushShortPixel(MSBEndian,p,&pixel);
1067 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1070 q+=GetPixelChannels(image);
1074 for (x=0; x < (ssize_t) image->columns; x++)
1076 p=PushLongPixel(MSBEndian,p,&pixel);
1077 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1078 SetPixelAlpha(image,OpaqueAlpha,q);
1079 if (image->alpha_trait == BlendPixelTrait)
1081 p=PushLongPixel(MSBEndian,p,&pixel);
1082 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1084 q+=GetPixelChannels(image);
1094 if (image->depth <= 8)
1099 for (x=0; x < (ssize_t) image->columns; x++)
1101 p=PushCharPixel(p,&pixel);
1102 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1103 p=PushCharPixel(p,&pixel);
1104 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1105 p=PushCharPixel(p,&pixel);
1106 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1107 p=PushCharPixel(p,&pixel);
1108 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1109 SetPixelAlpha(image,OpaqueAlpha,q);
1110 if (image->alpha_trait == BlendPixelTrait)
1112 p=PushCharPixel(p,&pixel);
1113 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1116 q+=GetPixelChannels(image);
1120 if (image->depth <= 16)
1125 for (x=0; x < (ssize_t) image->columns; x++)
1127 p=PushShortPixel(MSBEndian,p,&pixel);
1128 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1129 p=PushShortPixel(MSBEndian,p,&pixel);
1130 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1131 p=PushShortPixel(MSBEndian,p,&pixel);
1132 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1133 p=PushShortPixel(MSBEndian,p,&pixel);
1134 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1135 SetPixelAlpha(image,OpaqueAlpha,q);
1136 if (image->alpha_trait == BlendPixelTrait)
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 == BlendPixelTrait)
1158 p=PushLongPixel(MSBEndian,p,&pixel);
1159 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1161 q+=GetPixelChannels(image);
1170 if (image->depth <= 8)
1175 for (x=0; x < (ssize_t) image->columns; x++)
1177 p=PushCharPixel(p,&pixel);
1178 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1179 p=PushCharPixel(p,&pixel);
1180 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1181 p=PushCharPixel(p,&pixel);
1182 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1183 SetPixelAlpha(image,OpaqueAlpha,q);
1184 if (image->alpha_trait == BlendPixelTrait)
1186 p=PushCharPixel(p,&pixel);
1187 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1189 q+=GetPixelChannels(image);
1193 if (image->depth <= 16)
1198 for (x=0; x < (ssize_t) image->columns; x++)
1200 p=PushShortPixel(MSBEndian,p,&pixel);
1201 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1202 p=PushShortPixel(MSBEndian,p,&pixel);
1203 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1204 p=PushShortPixel(MSBEndian,p,&pixel);
1205 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1206 SetPixelAlpha(image,OpaqueAlpha,q);
1207 if (image->alpha_trait == BlendPixelTrait)
1209 p=PushShortPixel(MSBEndian,p,&pixel);
1210 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1213 q+=GetPixelChannels(image);
1217 for (x=0; x < (ssize_t) image->columns; x++)
1219 p=PushLongPixel(MSBEndian,p,&pixel);
1220 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1221 p=PushLongPixel(MSBEndian,p,&pixel);
1222 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1223 p=PushLongPixel(MSBEndian,p,&pixel);
1224 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1225 SetPixelAlpha(image,OpaqueAlpha,q);
1226 if (image->alpha_trait == BlendPixelTrait)
1228 p=PushLongPixel(MSBEndian,p,&pixel);
1229 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1231 q+=GetPixelChannels(image);
1238 sync=SyncAuthenticPixels(image,exception);
1239 if (sync == MagickFalse)
1242 quantum_info=DestroyQuantumInfo(quantum_info);
1243 if (status == MagickFalse)
1244 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1245 SetQuantumImageType(image,quantum_type);
1252 Convert PFM raster image to pixel packets.
1255 (void) SetImageColorspace(image,GRAYColorspace,exception);
1256 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1257 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1259 quantum_info=AcquireQuantumInfo(image_info,image);
1260 if (quantum_info == (QuantumInfo *) NULL)
1261 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1262 status=SetQuantumDepth(image,quantum_info,32);
1263 if (status == MagickFalse)
1264 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1265 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1266 if (status == MagickFalse)
1267 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1268 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1269 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1270 for (y=0; y < (ssize_t) image->rows; y++)
1288 if (status == MagickFalse)
1290 pixels=GetQuantumPixels(quantum_info);
1292 count=ReadBlob(image,extent,pixels);
1293 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1294 (image->previous == (Image *) NULL))
1299 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1301 if (proceed == MagickFalse)
1306 if ((size_t) count != extent)
1308 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1309 image->columns,1,exception);
1310 if (q == (Quantum *) NULL)
1315 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1316 quantum_type,pixels,exception);
1317 if (length != extent)
1319 sync=SyncAuthenticPixels(image,exception);
1320 if (sync == MagickFalse)
1323 quantum_info=DestroyQuantumInfo(quantum_info);
1324 if (status == MagickFalse)
1325 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1326 SetQuantumImageType(image,quantum_type);
1330 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1332 if (EOFBlob(image) != MagickFalse)
1334 (void) ThrowMagickException(exception,GetMagickModule(),
1335 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1339 Proceed to next image.
1341 if (image_info->number_scenes != 0)
1342 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1344 if ((format == '1') || (format == '2') || (format == '3'))
1348 Skip to end of line.
1350 count=ReadBlob(image,1,(unsigned char *) &format);
1353 if ((count != 0) && (format == 'P'))
1355 } while (format != '\n');
1356 count=ReadBlob(image,1,(unsigned char *) &format);
1357 if ((count == 1) && (format == 'P'))
1360 Allocate next image structure.
1362 AcquireNextImage(image_info,image,exception);
1363 if (GetNextImageInList(image) == (Image *) NULL)
1365 image=DestroyImageList(image);
1366 return((Image *) NULL);
1368 image=SyncNextImageInList(image);
1369 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1370 GetBlobSize(image));
1371 if (status == MagickFalse)
1374 } while ((count == 1) && (format == 'P'));
1375 (void) CloseBlob(image);
1376 return(GetFirstImageInList(image));
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % R e g i s t e r P N M I m a g e %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 % RegisterPNMImage() adds properties for the PNM image format to
1391 % the list of supported formats. The properties include the image format
1392 % tag, a method to read and/or write the format, whether the format
1393 % supports the saving of more than one frame to the same file or blob,
1394 % whether the format supports native in-memory I/O, and a brief
1395 % description of the format.
1397 % The format of the RegisterPNMImage method is:
1399 % size_t RegisterPNMImage(void)
1402 ModuleExport size_t RegisterPNMImage(void)
1407 entry=SetMagickInfo("PAM");
1408 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1409 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1410 entry->description=ConstantString("Common 2-dimensional bitmap format");
1411 entry->mime_type=ConstantString("image/x-portable-pixmap");
1412 entry->module=ConstantString("PNM");
1413 (void) RegisterMagickInfo(entry);
1414 entry=SetMagickInfo("PBM");
1415 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1416 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1417 entry->description=ConstantString("Portable bitmap format (black and white)");
1418 entry->mime_type=ConstantString("image/x-portable-bitmap");
1419 entry->module=ConstantString("PNM");
1420 (void) RegisterMagickInfo(entry);
1421 entry=SetMagickInfo("PFM");
1422 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1423 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1424 entry->endian_support=MagickTrue;
1425 entry->description=ConstantString("Portable float format");
1426 entry->module=ConstantString("PFM");
1427 (void) RegisterMagickInfo(entry);
1428 entry=SetMagickInfo("PGM");
1429 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1430 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1431 entry->description=ConstantString("Portable graymap format (gray scale)");
1432 entry->mime_type=ConstantString("image/x-portable-greymap");
1433 entry->module=ConstantString("PNM");
1434 (void) RegisterMagickInfo(entry);
1435 entry=SetMagickInfo("PNM");
1436 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1437 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1438 entry->magick=(IsImageFormatHandler *) IsPNM;
1439 entry->description=ConstantString("Portable anymap");
1440 entry->mime_type=ConstantString("image/x-portable-pixmap");
1441 entry->module=ConstantString("PNM");
1442 (void) RegisterMagickInfo(entry);
1443 entry=SetMagickInfo("PPM");
1444 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1445 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1446 entry->description=ConstantString("Portable pixmap format (color)");
1447 entry->mime_type=ConstantString("image/x-portable-pixmap");
1448 entry->module=ConstantString("PNM");
1449 (void) RegisterMagickInfo(entry);
1450 return(MagickImageCoderSignature);
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % U n r e g i s t e r P N M I m a g e %
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % UnregisterPNMImage() removes format registrations made by the
1465 % PNM module from the list of supported formats.
1467 % The format of the UnregisterPNMImage method is:
1469 % UnregisterPNMImage(void)
1472 ModuleExport void UnregisterPNMImage(void)
1474 (void) UnregisterMagickInfo("PAM");
1475 (void) UnregisterMagickInfo("PBM");
1476 (void) UnregisterMagickInfo("PGM");
1477 (void) UnregisterMagickInfo("PNM");
1478 (void) UnregisterMagickInfo("PPM");
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 % W r i t e P N M I m a g e %
1490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1494 % The format of the WritePNMImage method is:
1496 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1497 % Image *image,ExceptionInfo *exception)
1499 % A description of each parameter follows.
1501 % o image_info: the image info.
1503 % o image: The image.
1505 % o exception: return any errors or warnings in this structure.
1508 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1509 ExceptionInfo *exception)
1512 buffer[MaxTextExtent],
1514 magick[MaxTextExtent];
1537 register unsigned char
1550 Open output image file.
1552 assert(image_info != (const ImageInfo *) NULL);
1553 assert(image_info->signature == MagickSignature);
1554 assert(image != (Image *) NULL);
1555 assert(image->signature == MagickSignature);
1556 if (image->debug != MagickFalse)
1557 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1558 assert(exception != (ExceptionInfo *) NULL);
1559 assert(exception->signature == MagickSignature);
1560 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1561 if (status == MagickFalse)
1570 Write PNM file header.
1573 quantum_type=RGBQuantum;
1574 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1575 max_value=GetQuantumRange(image->depth);
1588 if (image_info->compression == NoCompression)
1596 if (IsImageGray(image,exception) != MagickFalse)
1604 if (image_info->compression == NoCompression)
1611 if ((image_info->type != TrueColorType) &&
1612 (IsImageGray(image,exception) != MagickFalse))
1615 if (image_info->compression == NoCompression)
1617 if (IsImageMonochrome(image,exception) != MagickFalse)
1620 if (image_info->compression == NoCompression)
1629 if (image_info->compression == NoCompression)
1634 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1635 (void) WriteBlobString(image,buffer);
1636 value=GetImageProperty(image,"comment",exception);
1637 if (value != (const char *) NULL)
1643 Write comments to file.
1645 (void) WriteBlobByte(image,'#');
1646 for (p=value; *p != '\0'; p++)
1648 (void) WriteBlobByte(image,(unsigned char) *p);
1649 if ((*p == '\n') || (*p == '\r'))
1650 (void) WriteBlobByte(image,'#');
1652 (void) WriteBlobByte(image,'\n');
1656 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1657 (double) image->columns,(double) image->rows);
1658 (void) WriteBlobString(image,buffer);
1663 type[MaxTextExtent];
1668 (void) FormatLocaleString(buffer,MaxTextExtent,
1669 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1671 (void) WriteBlobString(image,buffer);
1672 quantum_type=GetQuantumType(image,exception);
1673 switch (quantum_type)
1679 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1683 case GrayAlphaQuantum:
1686 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1691 quantum_type=RGBQuantum;
1692 if (image->alpha_trait == BlendPixelTrait)
1693 quantum_type=RGBAQuantum;
1695 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1699 if (image->alpha_trait == BlendPixelTrait)
1702 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1704 if (image->depth > 32)
1706 (void) FormatLocaleString(buffer,MaxTextExtent,
1707 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1708 ((MagickOffsetType) GetQuantumRange(image->depth)));
1709 (void) WriteBlobString(image,buffer);
1710 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1712 (void) WriteBlobString(image,buffer);
1715 Convert runextent encoded to PNM raster pixels.
1725 Convert image to a PBM image.
1728 for (y=0; y < (ssize_t) image->rows; y++)
1730 register const Quantum
1736 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1737 if (p == (const Quantum *) NULL)
1739 for (x=0; x < (ssize_t) image->columns; x++)
1741 pixel=ClampToQuantum(GetPixelLuma(image,p));
1742 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1745 if ((q-pixels+2) >= 80)
1748 (void) WriteBlob(image,q-pixels,pixels);
1751 p+=GetPixelChannels(image);
1753 if (image->previous == (Image *) NULL)
1755 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1757 if (status == MagickFalse)
1764 (void) WriteBlob(image,q-pixels,pixels);
1774 Convert image to a PGM image.
1776 if (image->depth <= 8)
1777 (void) WriteBlobString(image,"255\n");
1779 if (image->depth <= 16)
1780 (void) WriteBlobString(image,"65535\n");
1782 (void) WriteBlobString(image,"4294967295\n");
1784 for (y=0; y < (ssize_t) image->rows; y++)
1786 register const Quantum
1792 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1793 if (p == (const Quantum *) NULL)
1795 for (x=0; x < (ssize_t) image->columns; x++)
1797 index=ClampToQuantum(GetPixelLuma(image,p));
1798 if (image->depth <= 8)
1799 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1800 ScaleQuantumToChar(index));
1802 if (image->depth <= 16)
1803 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1804 ScaleQuantumToShort(index));
1806 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1807 ScaleQuantumToLong(index));
1808 extent=(size_t) count;
1809 (void) strncpy((char *) q,buffer,extent);
1811 if ((q-pixels+extent) >= 80)
1814 (void) WriteBlob(image,q-pixels,pixels);
1817 p+=GetPixelChannels(image);
1819 if (image->previous == (Image *) NULL)
1821 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1823 if (status == MagickFalse)
1830 (void) WriteBlob(image,q-pixels,pixels);
1840 Convert image to a PNM image.
1842 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1843 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1844 if (image->depth <= 8)
1845 (void) WriteBlobString(image,"255\n");
1847 if (image->depth <= 16)
1848 (void) WriteBlobString(image,"65535\n");
1850 (void) WriteBlobString(image,"4294967295\n");
1852 for (y=0; y < (ssize_t) image->rows; y++)
1854 register const Quantum
1860 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1861 if (p == (const Quantum *) NULL)
1863 for (x=0; x < (ssize_t) image->columns; x++)
1865 if (image->depth <= 8)
1866 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1867 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1868 ScaleQuantumToChar(GetPixelGreen(image,p)),
1869 ScaleQuantumToChar(GetPixelBlue(image,p)));
1871 if (image->depth <= 16)
1872 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1873 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1874 ScaleQuantumToShort(GetPixelGreen(image,p)),
1875 ScaleQuantumToShort(GetPixelBlue(image,p)));
1877 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1878 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1879 ScaleQuantumToLong(GetPixelGreen(image,p)),
1880 ScaleQuantumToLong(GetPixelBlue(image,p)));
1881 extent=(size_t) count;
1882 (void) strncpy((char *) q,buffer,extent);
1884 if ((q-pixels+extent) >= 80)
1887 (void) WriteBlob(image,q-pixels,pixels);
1890 p+=GetPixelChannels(image);
1892 if (image->previous == (Image *) NULL)
1894 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1896 if (status == MagickFalse)
1903 (void) WriteBlob(image,q-pixels,pixels);
1910 Convert image to a PBM image.
1913 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1914 if (quantum_info == (QuantumInfo *) NULL)
1915 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1916 quantum_info->min_is_white=MagickTrue;
1917 pixels=GetQuantumPixels(quantum_info);
1918 for (y=0; y < (ssize_t) image->rows; y++)
1920 register const Quantum
1923 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1924 if (p == (const Quantum *) NULL)
1926 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1927 GrayQuantum,pixels,exception);
1928 count=WriteBlob(image,extent,pixels);
1929 if (count != (ssize_t) extent)
1931 if (image->previous == (Image *) NULL)
1933 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1935 if (status == MagickFalse)
1939 quantum_info=DestroyQuantumInfo(quantum_info);
1945 Convert image to a PGM image.
1947 if (image->depth > 32)
1949 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1950 ((MagickOffsetType) GetQuantumRange(image->depth)));
1951 (void) WriteBlobString(image,buffer);
1952 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1953 if (quantum_info == (QuantumInfo *) NULL)
1954 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
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 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2063 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2064 if (image->depth > 32)
2066 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2067 ((MagickOffsetType) GetQuantumRange(image->depth)));
2068 (void) WriteBlobString(image,buffer);
2069 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2070 if (quantum_info == (QuantumInfo *) NULL)
2071 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2072 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2073 pixels=GetQuantumPixels(quantum_info);
2074 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2075 for (y=0; y < (ssize_t) image->rows; y++)
2077 register const Quantum
2083 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2084 if (p == (const Quantum *) NULL)
2087 switch (image->depth)
2093 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2094 quantum_type,pixels,exception);
2099 if (image->depth <= 8)
2101 for (x=0; x < (ssize_t) image->columns; x++)
2103 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2104 q=PopCharPixel((unsigned char) pixel,q);
2105 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2106 q=PopCharPixel((unsigned char) pixel,q);
2107 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2108 q=PopCharPixel((unsigned char) pixel,q);
2109 p+=GetPixelChannels(image);
2111 extent=(size_t) (q-pixels);
2114 if (image->depth <= 16)
2116 for (x=0; x < (ssize_t) image->columns; x++)
2118 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2119 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2120 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2121 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2122 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2123 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2124 p+=GetPixelChannels(image);
2126 extent=(size_t) (q-pixels);
2129 for (x=0; x < (ssize_t) image->columns; x++)
2131 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2132 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2133 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2134 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2135 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2136 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2137 p+=GetPixelChannels(image);
2139 extent=(size_t) (q-pixels);
2143 count=WriteBlob(image,extent,pixels);
2144 if (count != (ssize_t) extent)
2146 if (image->previous == (Image *) NULL)
2148 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2150 if (status == MagickFalse)
2154 quantum_info=DestroyQuantumInfo(quantum_info);
2160 Convert image to a PAM.
2162 if (image->depth > 32)
2164 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2165 pixels=GetQuantumPixels(quantum_info);
2166 for (y=0; y < (ssize_t) image->rows; y++)
2168 register const Quantum
2174 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2175 if (p == (const Quantum *) NULL)
2178 switch (image->depth)
2184 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2185 quantum_type,pixels,exception);
2190 switch (quantum_type)
2193 case GrayAlphaQuantum:
2195 if (image->depth <= 8)
2197 for (x=0; x < (ssize_t) image->columns; x++)
2199 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2200 image,p)),max_value);
2201 q=PopCharPixel((unsigned char) pixel,q);
2202 if (image->alpha_trait == BlendPixelTrait)
2204 pixel=(unsigned char) ScaleQuantumToAny(
2205 GetPixelAlpha(image,p),max_value);
2206 q=PopCharPixel((unsigned char) pixel,q);
2208 p+=GetPixelChannels(image);
2212 if (image->depth <= 16)
2214 for (x=0; x < (ssize_t) image->columns; x++)
2216 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2217 image,p)),max_value);
2218 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2219 if (image->alpha_trait == BlendPixelTrait)
2221 pixel=(unsigned char) ScaleQuantumToAny(
2222 GetPixelAlpha(image,p),max_value);
2223 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2225 p+=GetPixelChannels(image);
2229 for (x=0; x < (ssize_t) image->columns; x++)
2231 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2233 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2234 if (image->alpha_trait == BlendPixelTrait)
2236 pixel=(unsigned char) ScaleQuantumToAny(
2237 GetPixelAlpha(image,p),max_value);
2238 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2240 p+=GetPixelChannels(image);
2247 if (image->depth <= 8)
2249 for (x=0; x < (ssize_t) image->columns; x++)
2251 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2252 q=PopCharPixel((unsigned char) pixel,q);
2253 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2255 q=PopCharPixel((unsigned char) pixel,q);
2256 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2258 q=PopCharPixel((unsigned char) pixel,q);
2259 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2261 q=PopCharPixel((unsigned char) pixel,q);
2262 if (image->alpha_trait == BlendPixelTrait)
2264 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2266 q=PopCharPixel((unsigned char) pixel,q);
2268 p+=GetPixelChannels(image);
2272 if (image->depth <= 16)
2274 for (x=0; x < (ssize_t) image->columns; x++)
2276 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2277 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2278 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2280 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2281 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2283 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2284 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2286 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2287 if (image->alpha_trait == BlendPixelTrait)
2289 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2291 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2293 p+=GetPixelChannels(image);
2297 for (x=0; x < (ssize_t) image->columns; x++)
2299 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2300 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2301 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2302 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2303 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2304 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2305 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2306 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2307 if (image->alpha_trait == BlendPixelTrait)
2309 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2311 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2313 p+=GetPixelChannels(image);
2319 if (image->depth <= 8)
2321 for (x=0; x < (ssize_t) image->columns; x++)
2323 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2324 q=PopCharPixel((unsigned char) pixel,q);
2325 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2327 q=PopCharPixel((unsigned char) pixel,q);
2328 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2330 q=PopCharPixel((unsigned char) pixel,q);
2331 if (image->alpha_trait == BlendPixelTrait)
2333 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2335 q=PopCharPixel((unsigned char) pixel,q);
2337 p+=GetPixelChannels(image);
2341 if (image->depth <= 16)
2343 for (x=0; x < (ssize_t) image->columns; x++)
2345 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2346 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2347 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2349 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2350 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2352 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2353 if (image->alpha_trait == BlendPixelTrait)
2355 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2357 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2359 p+=GetPixelChannels(image);
2363 for (x=0; x < (ssize_t) image->columns; x++)
2365 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2366 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2367 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2368 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2369 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2370 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2371 if (image->alpha_trait == BlendPixelTrait)
2373 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2375 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2377 p+=GetPixelChannels(image);
2382 extent=(size_t) (q-pixels);
2386 count=WriteBlob(image,extent,pixels);
2387 if (count != (ssize_t) extent)
2389 if (image->previous == (Image *) NULL)
2391 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2393 if (status == MagickFalse)
2397 quantum_info=DestroyQuantumInfo(quantum_info);
2403 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2406 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2407 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2408 if (quantum_info == (QuantumInfo *) NULL)
2409 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2410 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2411 if (status == MagickFalse)
2412 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2413 pixels=GetQuantumPixels(quantum_info);
2414 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2416 register const Quantum
2419 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2420 if (p == (const Quantum *) NULL)
2422 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2423 quantum_type,pixels,exception);
2424 (void) WriteBlob(image,extent,pixels);
2425 if (image->previous == (Image *) NULL)
2427 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2429 if (status == MagickFalse)
2433 quantum_info=DestroyQuantumInfo(quantum_info);
2437 if (GetNextImageInList(image) == (Image *) NULL)
2439 image=SyncNextImageInList(image);
2440 status=SetImageProgress(image,SaveImagesTag,scene++,
2441 GetImageListLength(image));
2442 if (status == MagickFalse)
2444 } while (image_info->adjoin != MagickFalse);
2445 (void) CloseBlob(image);