2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2014 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 if (image->depth != 1)
1048 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1051 SetPixelAlpha(image,QuantumRange-
1052 ScaleAnyToQuantum(pixel,max_value),q);
1054 q+=GetPixelChannels(image);
1058 if (image->depth <= 16)
1063 for (x=0; x < (ssize_t) image->columns; x++)
1065 p=PushShortPixel(MSBEndian,p,&pixel);
1066 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1068 SetPixelAlpha(image,OpaqueAlpha,q);
1069 if (image->alpha_trait == BlendPixelTrait)
1071 p=PushShortPixel(MSBEndian,p,&pixel);
1072 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1075 q+=GetPixelChannels(image);
1079 for (x=0; x < (ssize_t) image->columns; x++)
1081 p=PushLongPixel(MSBEndian,p,&pixel);
1082 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1083 SetPixelAlpha(image,OpaqueAlpha,q);
1084 if (image->alpha_trait == BlendPixelTrait)
1086 p=PushLongPixel(MSBEndian,p,&pixel);
1087 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1089 q+=GetPixelChannels(image);
1099 if (image->depth <= 8)
1104 for (x=0; x < (ssize_t) image->columns; x++)
1106 p=PushCharPixel(p,&pixel);
1107 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1108 p=PushCharPixel(p,&pixel);
1109 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1110 p=PushCharPixel(p,&pixel);
1111 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1112 p=PushCharPixel(p,&pixel);
1113 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1114 SetPixelAlpha(image,OpaqueAlpha,q);
1115 if (image->alpha_trait == BlendPixelTrait)
1117 p=PushCharPixel(p,&pixel);
1118 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1121 q+=GetPixelChannels(image);
1125 if (image->depth <= 16)
1130 for (x=0; x < (ssize_t) image->columns; x++)
1132 p=PushShortPixel(MSBEndian,p,&pixel);
1133 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1134 p=PushShortPixel(MSBEndian,p,&pixel);
1135 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1136 p=PushShortPixel(MSBEndian,p,&pixel);
1137 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1138 p=PushShortPixel(MSBEndian,p,&pixel);
1139 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1140 SetPixelAlpha(image,OpaqueAlpha,q);
1141 if (image->alpha_trait == BlendPixelTrait)
1143 p=PushShortPixel(MSBEndian,p,&pixel);
1144 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1147 q+=GetPixelChannels(image);
1150 for (x=0; x < (ssize_t) image->columns; x++)
1152 p=PushLongPixel(MSBEndian,p,&pixel);
1153 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1154 p=PushLongPixel(MSBEndian,p,&pixel);
1155 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1156 p=PushLongPixel(MSBEndian,p,&pixel);
1157 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1158 p=PushLongPixel(MSBEndian,p,&pixel);
1159 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1160 SetPixelAlpha(image,OpaqueAlpha,q);
1161 if (image->alpha_trait == BlendPixelTrait)
1163 p=PushLongPixel(MSBEndian,p,&pixel);
1164 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1166 q+=GetPixelChannels(image);
1175 if (image->depth <= 8)
1180 for (x=0; x < (ssize_t) image->columns; x++)
1182 p=PushCharPixel(p,&pixel);
1183 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1184 p=PushCharPixel(p,&pixel);
1185 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1186 p=PushCharPixel(p,&pixel);
1187 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1188 SetPixelAlpha(image,OpaqueAlpha,q);
1189 if (image->alpha_trait == BlendPixelTrait)
1191 p=PushCharPixel(p,&pixel);
1192 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1194 q+=GetPixelChannels(image);
1198 if (image->depth <= 16)
1203 for (x=0; x < (ssize_t) image->columns; x++)
1205 p=PushShortPixel(MSBEndian,p,&pixel);
1206 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1207 p=PushShortPixel(MSBEndian,p,&pixel);
1208 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1209 p=PushShortPixel(MSBEndian,p,&pixel);
1210 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1211 SetPixelAlpha(image,OpaqueAlpha,q);
1212 if (image->alpha_trait == BlendPixelTrait)
1214 p=PushShortPixel(MSBEndian,p,&pixel);
1215 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1218 q+=GetPixelChannels(image);
1222 for (x=0; x < (ssize_t) image->columns; x++)
1224 p=PushLongPixel(MSBEndian,p,&pixel);
1225 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1226 p=PushLongPixel(MSBEndian,p,&pixel);
1227 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1228 p=PushLongPixel(MSBEndian,p,&pixel);
1229 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1230 SetPixelAlpha(image,OpaqueAlpha,q);
1231 if (image->alpha_trait == BlendPixelTrait)
1233 p=PushLongPixel(MSBEndian,p,&pixel);
1234 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
1236 q+=GetPixelChannels(image);
1243 sync=SyncAuthenticPixels(image,exception);
1244 if (sync == MagickFalse)
1247 quantum_info=DestroyQuantumInfo(quantum_info);
1248 if (status == MagickFalse)
1249 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1250 SetQuantumImageType(image,quantum_type);
1257 Convert PFM raster image to pixel packets.
1260 (void) SetImageColorspace(image,GRAYColorspace,exception);
1261 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1262 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1264 quantum_info=AcquireQuantumInfo(image_info,image);
1265 if (quantum_info == (QuantumInfo *) NULL)
1266 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1267 status=SetQuantumDepth(image,quantum_info,32);
1268 if (status == MagickFalse)
1269 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1270 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1271 if (status == MagickFalse)
1272 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1273 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1274 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1275 for (y=0; y < (ssize_t) image->rows; y++)
1293 if (status == MagickFalse)
1295 pixels=GetQuantumPixels(quantum_info);
1297 count=ReadBlob(image,extent,pixels);
1298 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1299 (image->previous == (Image *) NULL))
1304 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1306 if (proceed == MagickFalse)
1311 if ((size_t) count != extent)
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);
1358 if ((count != 0) && (format == 'P'))
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=SetMagickInfo("PAM");
1413 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1414 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1415 entry->description=ConstantString("Common 2-dimensional bitmap format");
1416 entry->mime_type=ConstantString("image/x-portable-pixmap");
1417 entry->module=ConstantString("PNM");
1418 (void) RegisterMagickInfo(entry);
1419 entry=SetMagickInfo("PBM");
1420 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1421 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1422 entry->description=ConstantString("Portable bitmap format (black and white)");
1423 entry->mime_type=ConstantString("image/x-portable-bitmap");
1424 entry->module=ConstantString("PNM");
1425 (void) RegisterMagickInfo(entry);
1426 entry=SetMagickInfo("PFM");
1427 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1428 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1429 entry->endian_support=MagickTrue;
1430 entry->description=ConstantString("Portable float format");
1431 entry->module=ConstantString("PFM");
1432 (void) RegisterMagickInfo(entry);
1433 entry=SetMagickInfo("PGM");
1434 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1435 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1436 entry->description=ConstantString("Portable graymap format (gray scale)");
1437 entry->mime_type=ConstantString("image/x-portable-greymap");
1438 entry->module=ConstantString("PNM");
1439 (void) RegisterMagickInfo(entry);
1440 entry=SetMagickInfo("PNM");
1441 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1442 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1443 entry->magick=(IsImageFormatHandler *) IsPNM;
1444 entry->description=ConstantString("Portable anymap");
1445 entry->mime_type=ConstantString("image/x-portable-pixmap");
1446 entry->module=ConstantString("PNM");
1447 (void) RegisterMagickInfo(entry);
1448 entry=SetMagickInfo("PPM");
1449 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1450 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1451 entry->description=ConstantString("Portable pixmap format (color)");
1452 entry->mime_type=ConstantString("image/x-portable-pixmap");
1453 entry->module=ConstantString("PNM");
1454 (void) RegisterMagickInfo(entry);
1455 return(MagickImageCoderSignature);
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % U n r e g i s t e r P N M I m a g e %
1467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469 % UnregisterPNMImage() removes format registrations made by the
1470 % PNM module from the list of supported formats.
1472 % The format of the UnregisterPNMImage method is:
1474 % UnregisterPNMImage(void)
1477 ModuleExport void UnregisterPNMImage(void)
1479 (void) UnregisterMagickInfo("PAM");
1480 (void) UnregisterMagickInfo("PBM");
1481 (void) UnregisterMagickInfo("PGM");
1482 (void) UnregisterMagickInfo("PNM");
1483 (void) UnregisterMagickInfo("PPM");
1487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % W r i t e P N M I m a g e %
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1499 % The format of the WritePNMImage method is:
1501 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1502 % Image *image,ExceptionInfo *exception)
1504 % A description of each parameter follows.
1506 % o image_info: the image info.
1508 % o image: The image.
1510 % o exception: return any errors or warnings in this structure.
1513 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1514 ExceptionInfo *exception)
1517 buffer[MaxTextExtent],
1519 magick[MaxTextExtent];
1542 register unsigned char
1555 Open output image file.
1557 assert(image_info != (const ImageInfo *) NULL);
1558 assert(image_info->signature == MagickSignature);
1559 assert(image != (Image *) NULL);
1560 assert(image->signature == MagickSignature);
1561 if (image->debug != MagickFalse)
1562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1563 assert(exception != (ExceptionInfo *) NULL);
1564 assert(exception->signature == MagickSignature);
1565 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1566 if (status == MagickFalse)
1575 Write PNM file header.
1578 quantum_type=RGBQuantum;
1579 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1580 max_value=GetQuantumRange(image->depth);
1593 if (image_info->compression == NoCompression)
1601 if (IsImageGray(image,exception) != MagickFalse)
1609 if (image_info->compression == NoCompression)
1616 if ((image_info->type != TrueColorType) &&
1617 (IsImageGray(image,exception) != MagickFalse))
1620 if (image_info->compression == NoCompression)
1622 if (IsImageMonochrome(image,exception) != MagickFalse)
1625 if (image_info->compression == NoCompression)
1634 if (image_info->compression == NoCompression)
1639 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1640 (void) WriteBlobString(image,buffer);
1641 value=GetImageProperty(image,"comment",exception);
1642 if (value != (const char *) NULL)
1648 Write comments to file.
1650 (void) WriteBlobByte(image,'#');
1651 for (p=value; *p != '\0'; p++)
1653 (void) WriteBlobByte(image,(unsigned char) *p);
1654 if ((*p == '\n') || (*p == '\r'))
1655 (void) WriteBlobByte(image,'#');
1657 (void) WriteBlobByte(image,'\n');
1661 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1662 (double) image->columns,(double) image->rows);
1663 (void) WriteBlobString(image,buffer);
1668 type[MaxTextExtent];
1673 (void) FormatLocaleString(buffer,MaxTextExtent,
1674 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1676 (void) WriteBlobString(image,buffer);
1677 quantum_type=GetQuantumType(image,exception);
1678 switch (quantum_type)
1684 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1688 case GrayAlphaQuantum:
1691 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1696 quantum_type=RGBQuantum;
1697 if (image->alpha_trait == BlendPixelTrait)
1698 quantum_type=RGBAQuantum;
1700 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1704 if (image->alpha_trait == BlendPixelTrait)
1707 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1709 if (image->depth > 32)
1711 (void) FormatLocaleString(buffer,MaxTextExtent,
1712 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1713 ((MagickOffsetType) GetQuantumRange(image->depth)));
1714 (void) WriteBlobString(image,buffer);
1715 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1717 (void) WriteBlobString(image,buffer);
1720 Convert runextent encoded to PNM raster pixels.
1730 Convert image to a PBM image.
1732 (void) SetImageType(image,BilevelType,exception);
1734 for (y=0; y < (ssize_t) image->rows; y++)
1736 register const Quantum
1742 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1743 if (p == (const Quantum *) NULL)
1745 for (x=0; x < (ssize_t) image->columns; x++)
1747 *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
1750 if ((q-pixels+2) >= 80)
1753 (void) WriteBlob(image,q-pixels,pixels);
1756 p+=GetPixelChannels(image);
1758 if (image->previous == (Image *) NULL)
1760 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1762 if (status == MagickFalse)
1769 (void) WriteBlob(image,q-pixels,pixels);
1779 Convert image to a PGM image.
1781 if (image->depth <= 8)
1782 (void) WriteBlobString(image,"255\n");
1784 if (image->depth <= 16)
1785 (void) WriteBlobString(image,"65535\n");
1787 (void) WriteBlobString(image,"4294967295\n");
1789 for (y=0; y < (ssize_t) image->rows; y++)
1791 register const Quantum
1797 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1798 if (p == (const Quantum *) NULL)
1800 for (x=0; x < (ssize_t) image->columns; x++)
1802 index=ClampToQuantum(GetPixelLuma(image,p));
1803 if (image->depth <= 8)
1804 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1805 ScaleQuantumToChar(index));
1807 if (image->depth <= 16)
1808 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1809 ScaleQuantumToShort(index));
1811 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1812 ScaleQuantumToLong(index));
1813 extent=(size_t) count;
1814 (void) strncpy((char *) q,buffer,extent);
1816 if ((q-pixels+extent) >= 80)
1819 (void) WriteBlob(image,q-pixels,pixels);
1822 p+=GetPixelChannels(image);
1824 if (image->previous == (Image *) NULL)
1826 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1828 if (status == MagickFalse)
1835 (void) WriteBlob(image,q-pixels,pixels);
1845 Convert image to a PNM image.
1847 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1848 if (image->depth <= 8)
1849 (void) WriteBlobString(image,"255\n");
1851 if (image->depth <= 16)
1852 (void) WriteBlobString(image,"65535\n");
1854 (void) WriteBlobString(image,"4294967295\n");
1856 for (y=0; y < (ssize_t) image->rows; y++)
1858 register const Quantum
1864 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1865 if (p == (const Quantum *) NULL)
1867 for (x=0; x < (ssize_t) image->columns; x++)
1869 if (image->depth <= 8)
1870 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1871 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1872 ScaleQuantumToChar(GetPixelGreen(image,p)),
1873 ScaleQuantumToChar(GetPixelBlue(image,p)));
1875 if (image->depth <= 16)
1876 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1877 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1878 ScaleQuantumToShort(GetPixelGreen(image,p)),
1879 ScaleQuantumToShort(GetPixelBlue(image,p)));
1881 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1882 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1883 ScaleQuantumToLong(GetPixelGreen(image,p)),
1884 ScaleQuantumToLong(GetPixelBlue(image,p)));
1885 extent=(size_t) count;
1886 (void) strncpy((char *) q,buffer,extent);
1888 if ((q-pixels+extent) >= 80)
1891 (void) WriteBlob(image,q-pixels,pixels);
1894 p+=GetPixelChannels(image);
1896 if (image->previous == (Image *) NULL)
1898 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1900 if (status == MagickFalse)
1907 (void) WriteBlob(image,q-pixels,pixels);
1914 Convert image to a PBM image.
1916 (void) SetImageType(image,BilevelType,exception);
1918 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1919 if (quantum_info == (QuantumInfo *) NULL)
1920 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1921 quantum_info->min_is_white=MagickTrue;
1922 pixels=GetQuantumPixels(quantum_info);
1923 for (y=0; y < (ssize_t) image->rows; y++)
1925 register const Quantum
1928 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1929 if (p == (const Quantum *) NULL)
1931 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1932 GrayQuantum,pixels,exception);
1933 count=WriteBlob(image,extent,pixels);
1934 if (count != (ssize_t) extent)
1936 if (image->previous == (Image *) NULL)
1938 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1940 if (status == MagickFalse)
1944 quantum_info=DestroyQuantumInfo(quantum_info);
1950 Convert image to a PGM image.
1952 if (image->depth > 32)
1954 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1955 ((MagickOffsetType) GetQuantumRange(image->depth)));
1956 (void) WriteBlobString(image,buffer);
1957 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1958 if (quantum_info == (QuantumInfo *) NULL)
1959 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1960 quantum_info->min_is_white=MagickTrue;
1961 pixels=GetQuantumPixels(quantum_info);
1962 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1963 for (y=0; y < (ssize_t) image->rows; y++)
1965 register const Quantum
1971 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1972 if (p == (const Quantum *) NULL)
1975 switch (image->depth)
1981 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1982 GrayQuantum,pixels,exception);
1987 if (image->depth <= 8)
1989 for (x=0; x < (ssize_t) image->columns; x++)
1991 if (IsPixelGray(image,p) == MagickFalse)
1992 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
1993 image,p)),max_value);
1996 if (image->depth == 8)
1997 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1999 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2002 q=PopCharPixel((unsigned char) pixel,q);
2003 p+=GetPixelChannels(image);
2005 extent=(size_t) (q-pixels);
2008 if (image->depth <= 16)
2010 for (x=0; x < (ssize_t) image->columns; x++)
2012 if (IsPixelGray(image,p) == MagickFalse)
2013 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2017 if (image->depth == 16)
2018 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2020 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2023 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2024 p+=GetPixelChannels(image);
2026 extent=(size_t) (q-pixels);
2029 for (x=0; x < (ssize_t) image->columns; x++)
2031 if (IsPixelGray(image,p) == MagickFalse)
2032 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2036 if (image->depth == 16)
2037 pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2039 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2041 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2042 p+=GetPixelChannels(image);
2044 extent=(size_t) (q-pixels);
2048 count=WriteBlob(image,extent,pixels);
2049 if (count != (ssize_t) extent)
2051 if (image->previous == (Image *) NULL)
2053 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2055 if (status == MagickFalse)
2059 quantum_info=DestroyQuantumInfo(quantum_info);
2065 Convert image to a PNM image.
2067 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2068 if (image->depth > 32)
2070 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2071 ((MagickOffsetType) GetQuantumRange(image->depth)));
2072 (void) WriteBlobString(image,buffer);
2073 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2074 if (quantum_info == (QuantumInfo *) NULL)
2075 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2076 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2077 pixels=GetQuantumPixels(quantum_info);
2078 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2079 for (y=0; y < (ssize_t) image->rows; y++)
2081 register const Quantum
2087 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2088 if (p == (const Quantum *) NULL)
2091 switch (image->depth)
2097 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2098 quantum_type,pixels,exception);
2103 if (image->depth <= 8)
2105 for (x=0; x < (ssize_t) image->columns; x++)
2107 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2108 q=PopCharPixel((unsigned char) pixel,q);
2109 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2110 q=PopCharPixel((unsigned char) pixel,q);
2111 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2112 q=PopCharPixel((unsigned char) pixel,q);
2113 p+=GetPixelChannels(image);
2115 extent=(size_t) (q-pixels);
2118 if (image->depth <= 16)
2120 for (x=0; x < (ssize_t) image->columns; x++)
2122 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2123 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2124 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2125 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2126 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2127 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2128 p+=GetPixelChannels(image);
2130 extent=(size_t) (q-pixels);
2133 for (x=0; x < (ssize_t) image->columns; x++)
2135 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2136 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2137 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2138 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2139 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2140 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2141 p+=GetPixelChannels(image);
2143 extent=(size_t) (q-pixels);
2147 count=WriteBlob(image,extent,pixels);
2148 if (count != (ssize_t) extent)
2150 if (image->previous == (Image *) NULL)
2152 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2154 if (status == MagickFalse)
2158 quantum_info=DestroyQuantumInfo(quantum_info);
2164 Convert image to a PAM.
2166 if (image->depth > 32)
2168 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2169 pixels=GetQuantumPixels(quantum_info);
2170 for (y=0; y < (ssize_t) image->rows; y++)
2172 register const Quantum
2178 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2179 if (p == (const Quantum *) NULL)
2182 switch (image->depth)
2188 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2189 quantum_type,pixels,exception);
2194 switch (quantum_type)
2197 case GrayAlphaQuantum:
2199 if (image->depth <= 8)
2201 for (x=0; x < (ssize_t) image->columns; x++)
2203 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2204 image,p)),max_value);
2205 q=PopCharPixel((unsigned char) pixel,q);
2206 if (image->alpha_trait == BlendPixelTrait)
2208 pixel=(unsigned char) ScaleQuantumToAny(
2209 GetPixelAlpha(image,p),max_value);
2210 q=PopCharPixel((unsigned char) pixel,q);
2212 p+=GetPixelChannels(image);
2216 if (image->depth <= 16)
2218 for (x=0; x < (ssize_t) image->columns; x++)
2220 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2221 image,p)),max_value);
2222 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2223 if (image->alpha_trait == BlendPixelTrait)
2225 pixel=(unsigned char) ScaleQuantumToAny(
2226 GetPixelAlpha(image,p),max_value);
2227 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2229 p+=GetPixelChannels(image);
2233 for (x=0; x < (ssize_t) image->columns; x++)
2235 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2237 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2238 if (image->alpha_trait == BlendPixelTrait)
2240 pixel=(unsigned char) ScaleQuantumToAny(
2241 GetPixelAlpha(image,p),max_value);
2242 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2244 p+=GetPixelChannels(image);
2251 if (image->depth <= 8)
2253 for (x=0; x < (ssize_t) image->columns; x++)
2255 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2256 q=PopCharPixel((unsigned char) pixel,q);
2257 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2259 q=PopCharPixel((unsigned char) pixel,q);
2260 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2262 q=PopCharPixel((unsigned char) pixel,q);
2263 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2265 q=PopCharPixel((unsigned char) pixel,q);
2266 if (image->alpha_trait == BlendPixelTrait)
2268 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2270 q=PopCharPixel((unsigned char) pixel,q);
2272 p+=GetPixelChannels(image);
2276 if (image->depth <= 16)
2278 for (x=0; x < (ssize_t) image->columns; x++)
2280 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2281 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2282 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2284 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2285 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2287 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2288 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2290 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2291 if (image->alpha_trait == BlendPixelTrait)
2293 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2295 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2297 p+=GetPixelChannels(image);
2301 for (x=0; x < (ssize_t) image->columns; x++)
2303 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2304 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2305 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2306 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2307 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2308 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2309 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2310 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2311 if (image->alpha_trait == BlendPixelTrait)
2313 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2315 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2317 p+=GetPixelChannels(image);
2323 if (image->depth <= 8)
2325 for (x=0; x < (ssize_t) image->columns; x++)
2327 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2328 q=PopCharPixel((unsigned char) pixel,q);
2329 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2331 q=PopCharPixel((unsigned char) pixel,q);
2332 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2334 q=PopCharPixel((unsigned char) pixel,q);
2335 if (image->alpha_trait == BlendPixelTrait)
2337 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2339 q=PopCharPixel((unsigned char) pixel,q);
2341 p+=GetPixelChannels(image);
2345 if (image->depth <= 16)
2347 for (x=0; x < (ssize_t) image->columns; x++)
2349 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2350 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2351 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2353 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2354 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2356 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2357 if (image->alpha_trait == BlendPixelTrait)
2359 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2361 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2363 p+=GetPixelChannels(image);
2367 for (x=0; x < (ssize_t) image->columns; x++)
2369 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2370 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2371 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2372 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2373 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2374 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2375 if (image->alpha_trait == BlendPixelTrait)
2377 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2379 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2381 p+=GetPixelChannels(image);
2386 extent=(size_t) (q-pixels);
2390 count=WriteBlob(image,extent,pixels);
2391 if (count != (ssize_t) extent)
2393 if (image->previous == (Image *) NULL)
2395 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2397 if (status == MagickFalse)
2401 quantum_info=DestroyQuantumInfo(quantum_info);
2407 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2410 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2411 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2412 if (quantum_info == (QuantumInfo *) NULL)
2413 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2414 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2415 if (status == MagickFalse)
2416 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2417 pixels=GetQuantumPixels(quantum_info);
2418 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2420 register const Quantum
2423 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2424 if (p == (const Quantum *) NULL)
2426 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2427 quantum_type,pixels,exception);
2428 (void) WriteBlob(image,extent,pixels);
2429 if (image->previous == (Image *) NULL)
2431 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2433 if (status == MagickFalse)
2437 quantum_info=DestroyQuantumInfo(quantum_info);
2441 if (GetNextImageInList(image) == (Image *) NULL)
2443 image=SyncNextImageInList(image);
2444 status=SetImageProgress(image,SaveImagesTag,scene++,
2445 GetImageListLength(image));
2446 if (status == MagickFalse)
2448 } while (image_info->adjoin != MagickFalse);
2449 (void) CloseBlob(image);