2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
72 static MagickBooleanType
73 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsPNM() returns MagickTrue if the image format type, identified by the
87 % magick string, is PNM.
89 % The format of the IsPNM method is:
91 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o extent: Specifies the extent of the magick string.
100 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
104 if ((*magick == (unsigned char) 'P') &&
105 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
106 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
107 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d P N M I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % ReadPNMImage() reads a Portable Anymap image file and returns it.
124 % It allocates the memory necessary for the new Image structure and returns
125 % a pointer to the new image.
127 % The format of the ReadPNMImage method is:
129 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image_info: the image info.
135 % o exception: return any errors or warnings in this structure.
139 static int PNMComment(Image *image,ExceptionInfo *exception)
156 comment=AcquireString(GetImageProperty(image,"comment",exception));
157 p=comment+strlen(comment);
158 extent=strlen(comment)+MagickPathExtent;
159 for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
161 if ((size_t) (p-comment+1) >= extent)
164 comment=(char *) ResizeQuantumMemory(comment,extent+MagickPathExtent,
166 if (comment == (char *) NULL)
168 p=comment+strlen(comment);
170 c=ReadBlobByte(image);
177 if (comment == (char *) NULL)
179 (void) SetImageProperty(image,"comment",comment,exception);
180 comment=DestroyString(comment);
184 static unsigned int PNMInteger(Image *image,const unsigned int base,
185 ExceptionInfo *exception)
194 Skip any leading whitespace.
198 c=ReadBlobByte(image);
202 c=PNMComment(image,exception);
203 } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
205 return((unsigned int) (c-(int) '0'));
210 while (isdigit(c) != 0) {
211 if (value > (unsigned int) (INT_MAX/10))
214 if (value > (unsigned int) (INT_MAX-(c-(int) '0')))
217 c=ReadBlobByte(image);
224 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
260 assert(image_info != (const ImageInfo *) NULL);
261 assert(image_info->signature == MagickCoreSignature);
262 if (image_info->debug != MagickFalse)
263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
264 image_info->filename);
265 assert(exception != (ExceptionInfo *) NULL);
266 assert(exception->signature == MagickCoreSignature);
267 image=AcquireImage(image_info,exception);
268 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
269 if (status == MagickFalse)
271 image=DestroyImageList(image);
272 return((Image *) NULL);
277 count=ReadBlob(image,1,(unsigned char *) &format);
281 Initialize image structure.
283 if ((count != 1) || (format != 'P'))
284 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
286 quantum_type=RGBQuantum;
288 format=(char) ReadBlobByte(image);
292 PBM, PGM, PPM, and PNM.
294 image->columns=(size_t) PNMInteger(image,10,exception);
295 image->rows=(size_t) PNMInteger(image,10,exception);
296 if ((format == 'f') || (format == 'F'))
299 scale[MagickPathExtent];
301 (void) ReadBlobString(image,scale);
302 quantum_scale=StringToDouble(scale,(char **) NULL);
306 if ((format == '1') || (format == '4'))
307 max_value=1; /* bitmap */
309 max_value=(QuantumAny) PNMInteger(image,10,exception);
315 keyword[MagickPathExtent],
316 value[MagickPathExtent];
327 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
329 while (isspace((int) ((unsigned char) c)) != 0)
330 c=ReadBlobByte(image);
336 c=PNMComment(image,exception);
337 c=ReadBlobByte(image);
338 while (isspace((int) ((unsigned char) c)) != 0)
339 c=ReadBlobByte(image);
344 if ((size_t) (p-keyword) < (MagickPathExtent-1))
346 c=ReadBlobByte(image);
347 } while (isalnum(c));
349 if (LocaleCompare(keyword,"endhdr") == 0)
351 while (isspace((int) ((unsigned char) c)) != 0)
352 c=ReadBlobByte(image);
354 while (isalnum(c) || (c == '_'))
356 if ((size_t) (p-value) < (MagickPathExtent-1))
358 c=ReadBlobByte(image);
362 Assign a value to the specified keyword.
364 if (LocaleCompare(keyword,"depth") == 0)
365 packet_size=StringToUnsignedLong(value);
367 if (LocaleCompare(keyword,"height") == 0)
368 image->rows=StringToUnsignedLong(value);
369 if (LocaleCompare(keyword,"maxval") == 0)
370 max_value=StringToUnsignedLong(value);
371 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
373 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
375 (void) SetImageColorspace(image,GRAYColorspace,exception);
376 quantum_type=GrayQuantum;
378 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
380 (void) SetImageColorspace(image,GRAYColorspace,exception);
381 image->alpha_trait=BlendPixelTrait;
382 quantum_type=GrayAlphaQuantum;
384 if (LocaleCompare(value,"GRAYSCALE") == 0)
386 quantum_type=GrayQuantum;
387 (void) SetImageColorspace(image,GRAYColorspace,exception);
389 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
391 (void) SetImageColorspace(image,GRAYColorspace,exception);
392 image->alpha_trait=BlendPixelTrait;
393 quantum_type=GrayAlphaQuantum;
395 if (LocaleCompare(value,"RGB_ALPHA") == 0)
397 image->alpha_trait=BlendPixelTrait;
398 quantum_type=RGBAQuantum;
400 if (LocaleCompare(value,"CMYK") == 0)
402 (void) SetImageColorspace(image,CMYKColorspace,exception);
403 quantum_type=CMYKQuantum;
405 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
407 (void) SetImageColorspace(image,CMYKColorspace,exception);
408 image->alpha_trait=BlendPixelTrait;
409 quantum_type=CMYKAQuantum;
412 if (LocaleCompare(keyword,"width") == 0)
413 image->columns=StringToUnsignedLong(value);
416 if ((image->columns == 0) || (image->rows == 0))
417 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
418 if ((max_value == 0) || (max_value > 4294967295))
419 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
420 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
422 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
423 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
425 status=SetImageExtent(image,image->columns,image->rows,exception);
426 if (status == MagickFalse)
427 return(DestroyImageList(image));
429 Convert PNM pixels to runextent-encoded MIFF packets.
437 Convert PBM image to pixel packets.
439 (void) SetImageColorspace(image,GRAYColorspace,exception);
440 for (y=0; y < (ssize_t) image->rows; y++)
448 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
449 if (q == (Quantum *) NULL)
451 for (x=0; x < (ssize_t) image->columns; x++)
453 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
455 if (EOFBlob(image) != MagickFalse)
457 q+=GetPixelChannels(image);
459 if (SyncAuthenticPixels(image,exception) == MagickFalse)
461 if (image->previous == (Image *) NULL)
463 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
465 if (status == MagickFalse)
468 if (EOFBlob(image) != MagickFalse)
471 image->type=BilevelType;
480 Convert PGM image to pixel packets.
482 (void) SetImageColorspace(image,GRAYColorspace,exception);
483 for (y=0; y < (ssize_t) image->rows; y++)
491 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
492 if (q == (Quantum *) NULL)
494 for (x=0; x < (ssize_t) image->columns; x++)
496 intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),
498 if (EOFBlob(image) != MagickFalse)
500 SetPixelGray(image,intensity,q);
501 q+=GetPixelChannels(image);
503 if (SyncAuthenticPixels(image,exception) == MagickFalse)
505 if (image->previous == (Image *) NULL)
507 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
509 if (status == MagickFalse)
512 if (EOFBlob(image) != MagickFalse)
515 image->type=GrayscaleType;
521 Convert PNM image to pixel packets.
523 for (y=0; y < (ssize_t) image->rows; y++)
531 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
532 if (q == (Quantum *) NULL)
534 for (x=0; x < (ssize_t) image->columns; x++)
539 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
540 if (EOFBlob(image) != MagickFalse)
542 SetPixelRed(image,pixel,q);
543 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
544 SetPixelGreen(image,pixel,q);
545 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
546 SetPixelBlue(image,pixel,q);
547 q+=GetPixelChannels(image);
549 if (SyncAuthenticPixels(image,exception) == MagickFalse)
551 if (image->previous == (Image *) NULL)
553 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
555 if (status == MagickFalse)
558 if (EOFBlob(image) != MagickFalse)
566 Convert PBM raw image to pixel packets.
568 (void) SetImageColorspace(image,GRAYColorspace,exception);
569 quantum_type=GrayQuantum;
570 if (image->storage_class == PseudoClass)
571 quantum_type=IndexQuantum;
572 quantum_info=AcquireQuantumInfo(image_info,image);
573 if (quantum_info == (QuantumInfo *) NULL)
574 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
575 SetQuantumMinIsWhite(quantum_info,MagickTrue);
576 extent=GetQuantumExtent(image,quantum_info,quantum_type);
577 for (y=0; y < (ssize_t) image->rows; y++)
595 if (status == MagickFalse)
597 pixels=(unsigned char *) ReadBlobStream(image,extent,
598 GetQuantumPixels(quantum_info),&count);
599 if (count != (ssize_t) extent)
601 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
602 (image->previous == (Image *) NULL))
607 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
609 if (proceed == MagickFalse)
613 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
614 if (q == (Quantum *) NULL)
619 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
620 quantum_type,pixels,exception);
621 if (length != extent)
623 sync=SyncAuthenticPixels(image,exception);
624 if (sync == MagickFalse)
627 quantum_info=DestroyQuantumInfo(quantum_info);
628 if (status == MagickFalse)
629 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
630 SetQuantumImageType(image,quantum_type);
636 Convert PGM raw image to pixel packets.
638 (void) SetImageColorspace(image,GRAYColorspace,exception);
639 quantum_type=GrayQuantum;
640 if (image->depth <= 8)
641 extent=image->columns;
643 if (image->depth <= 16)
644 extent=2*image->columns;
646 extent=4*image->columns;
647 quantum_info=AcquireQuantumInfo(image_info,image);
648 if (quantum_info == (QuantumInfo *) NULL)
649 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
650 for (y=0; y < (ssize_t) image->rows; y++)
658 register const unsigned char
671 if (status == MagickFalse)
673 pixels=(unsigned char *) ReadBlobStream(image,extent,
674 GetQuantumPixels(quantum_info),&count);
675 if (count != (ssize_t) extent)
677 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
678 (image->previous == (Image *) NULL))
683 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
685 if (proceed == MagickFalse)
689 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
690 if (q == (Quantum *) NULL)
696 switch (image->depth)
702 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
703 quantum_type,pixels,exception);
711 if (image->depth <= 8)
716 for (x=0; x < (ssize_t) image->columns; x++)
718 p=PushCharPixel(p,&pixel);
719 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
720 q+=GetPixelChannels(image);
724 if (image->depth <= 16)
729 for (x=0; x < (ssize_t) image->columns; x++)
731 p=PushShortPixel(MSBEndian,p,&pixel);
732 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
733 q+=GetPixelChannels(image);
737 for (x=0; x < (ssize_t) image->columns; x++)
739 p=PushLongPixel(MSBEndian,p,&pixel);
740 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
741 q+=GetPixelChannels(image);
746 sync=SyncAuthenticPixels(image,exception);
747 if (sync == MagickFalse)
750 quantum_info=DestroyQuantumInfo(quantum_info);
751 if (status == MagickFalse)
752 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
753 SetQuantumImageType(image,quantum_type);
759 Convert PNM raster image to pixel packets.
761 quantum_type=RGBQuantum;
762 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
763 quantum_info=AcquireQuantumInfo(image_info,image);
764 if (quantum_info == (QuantumInfo *) NULL)
765 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
766 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
767 for (y=0; y < (ssize_t) image->rows; y++)
775 register const unsigned char
788 if (status == MagickFalse)
790 pixels=(unsigned char *) ReadBlobStream(image,extent,
791 GetQuantumPixels(quantum_info),&count);
792 if (count != (ssize_t) extent)
794 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
795 (image->previous == (Image *) NULL))
800 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
802 if (proceed == MagickFalse)
806 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
807 if (q == (Quantum *) NULL)
813 switch (image->depth)
817 for (x=0; x < (ssize_t) image->columns; x++)
819 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
820 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
821 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
822 SetPixelAlpha(image,OpaqueAlpha,q);
823 q+=GetPixelChannels(image);
832 for (x=0; x < (ssize_t) image->columns; x++)
834 p=PushShortPixel(MSBEndian,p,&pixel);
835 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
836 p=PushShortPixel(MSBEndian,p,&pixel);
837 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
838 p=PushShortPixel(MSBEndian,p,&pixel);
839 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
840 SetPixelAlpha(image,OpaqueAlpha,q);
841 q+=GetPixelChannels(image);
850 for (x=0; x < (ssize_t) image->columns; x++)
852 p=PushLongPixel(MSBEndian,p,&pixel);
853 SetPixelRed(image,ScaleLongToQuantum(pixel),q);
854 p=PushLongPixel(MSBEndian,p,&pixel);
855 SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
856 p=PushLongPixel(MSBEndian,p,&pixel);
857 SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
858 SetPixelAlpha(image,OpaqueAlpha,q);
859 q+=GetPixelChannels(image);
868 if (image->depth <= 8)
873 for (x=0; x < (ssize_t) image->columns; x++)
875 p=PushCharPixel(p,&pixel);
876 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
877 p=PushCharPixel(p,&pixel);
878 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
879 p=PushCharPixel(p,&pixel);
880 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
881 SetPixelAlpha(image,OpaqueAlpha,q);
882 q+=GetPixelChannels(image);
886 if (image->depth <= 16)
891 for (x=0; x < (ssize_t) image->columns; x++)
893 p=PushShortPixel(MSBEndian,p,&pixel);
894 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
895 p=PushShortPixel(MSBEndian,p,&pixel);
896 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
897 p=PushShortPixel(MSBEndian,p,&pixel);
898 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
899 SetPixelAlpha(image,OpaqueAlpha,q);
900 q+=GetPixelChannels(image);
904 for (x=0; x < (ssize_t) image->columns; x++)
906 p=PushLongPixel(MSBEndian,p,&pixel);
907 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
908 p=PushLongPixel(MSBEndian,p,&pixel);
909 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
910 p=PushLongPixel(MSBEndian,p,&pixel);
911 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
912 SetPixelAlpha(image,OpaqueAlpha,q);
913 q+=GetPixelChannels(image);
918 sync=SyncAuthenticPixels(image,exception);
919 if (sync == MagickFalse)
922 quantum_info=DestroyQuantumInfo(quantum_info);
923 if (status == MagickFalse)
924 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
933 Convert PAM raster image to pixel packets.
935 switch (quantum_type)
938 case GrayAlphaQuantum:
955 if (image->alpha_trait != UndefinedPixelTrait)
957 if (image->depth <= 8)
958 extent=channels*image->columns;
960 if (image->depth <= 16)
961 extent=2*channels*image->columns;
963 extent=4*channels*image->columns;
964 quantum_info=AcquireQuantumInfo(image_info,image);
965 if (quantum_info == (QuantumInfo *) NULL)
966 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
967 for (y=0; y < (ssize_t) image->rows; y++)
975 register const unsigned char
988 if (status == MagickFalse)
990 pixels=(unsigned char *) ReadBlobStream(image,extent,
991 GetQuantumPixels(quantum_info),&count);
992 if (count != (ssize_t) extent)
994 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
995 (image->previous == (Image *) NULL))
1000 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1002 if (proceed == MagickFalse)
1006 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1007 if (q == (Quantum *) NULL)
1013 switch (image->depth)
1019 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1020 quantum_type,pixels,exception);
1025 switch (quantum_type)
1028 case GrayAlphaQuantum:
1033 if (image->depth <= 8)
1038 for (x=0; x < (ssize_t) image->columns; x++)
1040 p=PushCharPixel(p,&pixel);
1041 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1043 SetPixelAlpha(image,OpaqueAlpha,q);
1044 if (image->alpha_trait != UndefinedPixelTrait)
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 != UndefinedPixelTrait)
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 != UndefinedPixelTrait)
1086 p=PushLongPixel(MSBEndian,p,&pixel);
1087 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1090 q+=GetPixelChannels(image);
1100 if (image->depth <= 8)
1105 for (x=0; x < (ssize_t) image->columns; x++)
1107 p=PushCharPixel(p,&pixel);
1108 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1109 p=PushCharPixel(p,&pixel);
1110 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1112 p=PushCharPixel(p,&pixel);
1113 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1115 p=PushCharPixel(p,&pixel);
1116 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1118 SetPixelAlpha(image,OpaqueAlpha,q);
1119 if (image->alpha_trait != UndefinedPixelTrait)
1121 p=PushCharPixel(p,&pixel);
1122 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1125 q+=GetPixelChannels(image);
1129 if (image->depth <= 16)
1134 for (x=0; x < (ssize_t) image->columns; x++)
1136 p=PushShortPixel(MSBEndian,p,&pixel);
1137 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1138 p=PushShortPixel(MSBEndian,p,&pixel);
1139 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1141 p=PushShortPixel(MSBEndian,p,&pixel);
1142 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1144 p=PushShortPixel(MSBEndian,p,&pixel);
1145 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1147 SetPixelAlpha(image,OpaqueAlpha,q);
1148 if (image->alpha_trait != UndefinedPixelTrait)
1150 p=PushShortPixel(MSBEndian,p,&pixel);
1151 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1154 q+=GetPixelChannels(image);
1157 for (x=0; x < (ssize_t) image->columns; x++)
1159 p=PushLongPixel(MSBEndian,p,&pixel);
1160 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1161 p=PushLongPixel(MSBEndian,p,&pixel);
1162 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1163 p=PushLongPixel(MSBEndian,p,&pixel);
1164 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1165 p=PushLongPixel(MSBEndian,p,&pixel);
1166 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1167 SetPixelAlpha(image,OpaqueAlpha,q);
1168 if (image->alpha_trait != UndefinedPixelTrait)
1170 p=PushLongPixel(MSBEndian,p,&pixel);
1171 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1174 q+=GetPixelChannels(image);
1183 if (image->depth <= 8)
1188 for (x=0; x < (ssize_t) image->columns; x++)
1190 p=PushCharPixel(p,&pixel);
1191 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1192 p=PushCharPixel(p,&pixel);
1193 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1195 p=PushCharPixel(p,&pixel);
1196 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1198 SetPixelAlpha(image,OpaqueAlpha,q);
1199 if (image->alpha_trait != UndefinedPixelTrait)
1201 p=PushCharPixel(p,&pixel);
1202 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1205 q+=GetPixelChannels(image);
1209 if (image->depth <= 16)
1214 for (x=0; x < (ssize_t) image->columns; x++)
1216 p=PushShortPixel(MSBEndian,p,&pixel);
1217 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1218 p=PushShortPixel(MSBEndian,p,&pixel);
1219 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1221 p=PushShortPixel(MSBEndian,p,&pixel);
1222 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1224 SetPixelAlpha(image,OpaqueAlpha,q);
1225 if (image->alpha_trait != UndefinedPixelTrait)
1227 p=PushShortPixel(MSBEndian,p,&pixel);
1228 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1231 q+=GetPixelChannels(image);
1235 for (x=0; x < (ssize_t) image->columns; x++)
1237 p=PushLongPixel(MSBEndian,p,&pixel);
1238 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1239 p=PushLongPixel(MSBEndian,p,&pixel);
1240 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1241 p=PushLongPixel(MSBEndian,p,&pixel);
1242 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1243 SetPixelAlpha(image,OpaqueAlpha,q);
1244 if (image->alpha_trait != UndefinedPixelTrait)
1246 p=PushLongPixel(MSBEndian,p,&pixel);
1247 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1250 q+=GetPixelChannels(image);
1257 sync=SyncAuthenticPixels(image,exception);
1258 if (sync == MagickFalse)
1261 quantum_info=DestroyQuantumInfo(quantum_info);
1262 if (status == MagickFalse)
1263 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1264 SetQuantumImageType(image,quantum_type);
1271 Convert PFM raster image to pixel packets.
1274 (void) SetImageColorspace(image,GRAYColorspace,exception);
1275 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1276 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1278 quantum_info=AcquireQuantumInfo(image_info,image);
1279 if (quantum_info == (QuantumInfo *) NULL)
1280 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1281 status=SetQuantumDepth(image,quantum_info,32);
1282 if (status == MagickFalse)
1283 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1284 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1285 if (status == MagickFalse)
1286 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1287 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1288 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1289 for (y=0; y < (ssize_t) image->rows; y++)
1307 if (status == MagickFalse)
1309 pixels=(unsigned char *) ReadBlobStream(image,extent,
1310 GetQuantumPixels(quantum_info),&count);
1311 if ((size_t) count != extent)
1313 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1314 (image->previous == (Image *) NULL))
1319 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1321 if (proceed == MagickFalse)
1325 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1326 image->columns,1,exception);
1327 if (q == (Quantum *) NULL)
1332 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1333 quantum_type,pixels,exception);
1334 if (length != extent)
1336 sync=SyncAuthenticPixels(image,exception);
1337 if (sync == MagickFalse)
1340 quantum_info=DestroyQuantumInfo(quantum_info);
1341 if (status == MagickFalse)
1342 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1343 SetQuantumImageType(image,quantum_type);
1347 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1349 if (EOFBlob(image) != MagickFalse)
1351 (void) ThrowMagickException(exception,GetMagickModule(),
1352 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1356 Proceed to next image.
1358 if (image_info->number_scenes != 0)
1359 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1361 if ((format == '1') || (format == '2') || (format == '3'))
1365 Skip to end of line.
1367 count=ReadBlob(image,1,(unsigned char *) &format);
1372 } while (format != '\n');
1373 count=ReadBlob(image,1,(unsigned char *) &format);
1374 if ((count == 1) && (format == 'P'))
1377 Allocate next image structure.
1379 AcquireNextImage(image_info,image,exception);
1380 if (GetNextImageInList(image) == (Image *) NULL)
1382 image=DestroyImageList(image);
1383 return((Image *) NULL);
1385 image=SyncNextImageInList(image);
1386 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1387 GetBlobSize(image));
1388 if (status == MagickFalse)
1391 } while ((count == 1) && (format == 'P'));
1392 (void) CloseBlob(image);
1393 return(GetFirstImageInList(image));
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 % R e g i s t e r P N M I m a g e %
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 % RegisterPNMImage() adds properties for the PNM image format to
1408 % the list of supported formats. The properties include the image format
1409 % tag, a method to read and/or write the format, whether the format
1410 % supports the saving of more than one frame to the same file or blob,
1411 % whether the format supports native in-memory I/O, and a brief
1412 % description of the format.
1414 % The format of the RegisterPNMImage method is:
1416 % size_t RegisterPNMImage(void)
1419 ModuleExport size_t RegisterPNMImage(void)
1424 entry=AcquireMagickInfo("PNM","PAM","Common 2-dimensional bitmap format");
1425 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1426 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1427 entry->mime_type=ConstantString("image/x-portable-pixmap");
1428 (void) RegisterMagickInfo(entry);
1429 entry=AcquireMagickInfo("PNM","PBM",
1430 "Portable bitmap format (black and white)");
1431 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1432 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1433 entry->mime_type=ConstantString("image/x-portable-bitmap");
1434 (void) RegisterMagickInfo(entry);
1435 entry=AcquireMagickInfo("PNM","PFM","Portable float format");
1436 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1437 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1438 entry->flags|=CoderEndianSupportFlag;
1439 (void) RegisterMagickInfo(entry);
1440 entry=AcquireMagickInfo("PNM","PGM","Portable graymap format (gray scale)");
1441 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1442 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1443 entry->mime_type=ConstantString("image/x-portable-greymap");
1444 (void) RegisterMagickInfo(entry);
1445 entry=AcquireMagickInfo("PNM","PNM","Portable anymap");
1446 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1447 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1448 entry->magick=(IsImageFormatHandler *) IsPNM;
1449 entry->mime_type=ConstantString("image/x-portable-pixmap");
1450 (void) RegisterMagickInfo(entry);
1451 entry=AcquireMagickInfo("PNM","PPM","Portable pixmap format (color)");
1452 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1453 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1454 entry->mime_type=ConstantString("image/x-portable-pixmap");
1455 (void) RegisterMagickInfo(entry);
1456 return(MagickImageCoderSignature);
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % U n r e g i s t e r P N M I m a g e %
1468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 % UnregisterPNMImage() removes format registrations made by the
1471 % PNM module from the list of supported formats.
1473 % The format of the UnregisterPNMImage method is:
1475 % UnregisterPNMImage(void)
1478 ModuleExport void UnregisterPNMImage(void)
1480 (void) UnregisterMagickInfo("PAM");
1481 (void) UnregisterMagickInfo("PBM");
1482 (void) UnregisterMagickInfo("PGM");
1483 (void) UnregisterMagickInfo("PNM");
1484 (void) UnregisterMagickInfo("PPM");
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 % W r i t e P N M I m a g e %
1496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1500 % The format of the WritePNMImage method is:
1502 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1503 % Image *image,ExceptionInfo *exception)
1505 % A description of each parameter follows.
1507 % o image_info: the image info.
1509 % o image: The image.
1511 % o exception: return any errors or warnings in this structure.
1514 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1515 ExceptionInfo *exception)
1518 buffer[MagickPathExtent],
1520 magick[MagickPathExtent];
1543 register unsigned char
1556 Open output image file.
1558 assert(image_info != (const ImageInfo *) NULL);
1559 assert(image_info->signature == MagickCoreSignature);
1560 assert(image != (Image *) NULL);
1561 assert(image->signature == MagickCoreSignature);
1562 if (image->debug != MagickFalse)
1563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1564 assert(exception != (ExceptionInfo *) NULL);
1565 assert(exception->signature == MagickCoreSignature);
1566 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1567 if (status == MagickFalse)
1576 Write PNM file header.
1579 quantum_type=RGBQuantum;
1580 (void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
1581 max_value=GetQuantumRange(image->depth);
1594 if (image_info->compression == NoCompression)
1602 if (SetImageGray(image,exception) != MagickFalse)
1610 if (image_info->compression == NoCompression)
1617 if ((image_info->type != TrueColorType) &&
1618 (SetImageGray(image,exception) != MagickFalse))
1621 if (image_info->compression == NoCompression)
1623 if (SetImageMonochrome(image,exception) != MagickFalse)
1626 if (image_info->compression == NoCompression)
1635 if (image_info->compression == NoCompression)
1640 (void) FormatLocaleString(buffer,MagickPathExtent,"P%c\n",format);
1641 (void) WriteBlobString(image,buffer);
1642 value=GetImageProperty(image,"comment",exception);
1643 if (value != (const char *) NULL)
1649 Write comments to file.
1651 (void) WriteBlobByte(image,'#');
1652 for (p=value; *p != '\0'; p++)
1654 (void) WriteBlobByte(image,(unsigned char) *p);
1655 if ((*p == '\n') || (*p == '\r'))
1656 (void) WriteBlobByte(image,'#');
1658 (void) WriteBlobByte(image,'\n');
1662 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",
1663 (double) image->columns,(double) image->rows);
1664 (void) WriteBlobString(image,buffer);
1669 type[MagickPathExtent];
1674 (void) FormatLocaleString(buffer,MagickPathExtent,
1675 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1677 (void) WriteBlobString(image,buffer);
1678 quantum_type=GetQuantumType(image,exception);
1679 switch (quantum_type)
1685 (void) CopyMagickString(type,"CMYK",MagickPathExtent);
1689 case GrayAlphaQuantum:
1692 (void) CopyMagickString(type,"GRAYSCALE",MagickPathExtent);
1693 if (IsImageMonochrome(image) != MagickFalse)
1694 (void) CopyMagickString(type,"BLACKANDWHITE",MagickPathExtent);
1699 quantum_type=RGBQuantum;
1700 if (image->alpha_trait != UndefinedPixelTrait)
1701 quantum_type=RGBAQuantum;
1703 (void) CopyMagickString(type,"RGB",MagickPathExtent);
1707 if (image->alpha_trait != UndefinedPixelTrait)
1710 (void) ConcatenateMagickString(type,"_ALPHA",MagickPathExtent);
1712 if (image->depth > 32)
1714 (void) FormatLocaleString(buffer,MagickPathExtent,
1715 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1716 ((MagickOffsetType) GetQuantumRange(image->depth)));
1717 (void) WriteBlobString(image,buffer);
1718 (void) FormatLocaleString(buffer,MagickPathExtent,
1719 "TUPLTYPE %s\nENDHDR\n",type);
1720 (void) WriteBlobString(image,buffer);
1723 Convert runextent encoded to PNM raster pixels.
1733 Convert image to a PBM image.
1735 (void) SetImageType(image,BilevelType,exception);
1737 for (y=0; y < (ssize_t) image->rows; y++)
1739 register const Quantum
1745 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1746 if (p == (const Quantum *) NULL)
1748 for (x=0; x < (ssize_t) image->columns; x++)
1750 *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
1753 if ((q-pixels+1) >= (ssize_t) sizeof(pixels))
1756 (void) WriteBlob(image,q-pixels,pixels);
1759 p+=GetPixelChannels(image);
1762 (void) WriteBlob(image,q-pixels,pixels);
1764 if (image->previous == (Image *) NULL)
1766 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1768 if (status == MagickFalse)
1775 (void) WriteBlob(image,q-pixels,pixels);
1785 Convert image to a PGM image.
1787 if (image->depth <= 8)
1788 (void) WriteBlobString(image,"255\n");
1790 if (image->depth <= 16)
1791 (void) WriteBlobString(image,"65535\n");
1793 (void) WriteBlobString(image,"4294967295\n");
1795 for (y=0; y < (ssize_t) image->rows; y++)
1797 register const Quantum
1803 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1804 if (p == (const Quantum *) NULL)
1806 for (x=0; x < (ssize_t) image->columns; x++)
1808 index=ClampToQuantum(GetPixelLuma(image,p));
1809 if (image->depth <= 8)
1810 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,"%u ",
1811 ScaleQuantumToChar(index));
1813 if (image->depth <= 16)
1814 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1815 "%u ",ScaleQuantumToShort(index));
1817 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1818 "%u ",ScaleQuantumToLong(index));
1819 extent=(size_t) count;
1820 (void) strncpy((char *) q,buffer,extent);
1822 if ((q-pixels+extent+1) >= sizeof(pixels))
1825 (void) WriteBlob(image,q-pixels,pixels);
1828 p+=GetPixelChannels(image);
1831 (void) WriteBlob(image,q-pixels,pixels);
1833 if (image->previous == (Image *) NULL)
1835 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1837 if (status == MagickFalse)
1844 (void) WriteBlob(image,q-pixels,pixels);
1854 Convert image to a PNM image.
1856 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1857 if (image->depth <= 8)
1858 (void) WriteBlobString(image,"255\n");
1860 if (image->depth <= 16)
1861 (void) WriteBlobString(image,"65535\n");
1863 (void) WriteBlobString(image,"4294967295\n");
1865 for (y=0; y < (ssize_t) image->rows; y++)
1867 register const Quantum
1873 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1874 if (p == (const Quantum *) NULL)
1876 for (x=0; x < (ssize_t) image->columns; x++)
1878 if (image->depth <= 8)
1879 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1880 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1881 ScaleQuantumToChar(GetPixelGreen(image,p)),
1882 ScaleQuantumToChar(GetPixelBlue(image,p)));
1884 if (image->depth <= 16)
1885 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1886 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1887 ScaleQuantumToShort(GetPixelGreen(image,p)),
1888 ScaleQuantumToShort(GetPixelBlue(image,p)));
1890 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1891 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1892 ScaleQuantumToLong(GetPixelGreen(image,p)),
1893 ScaleQuantumToLong(GetPixelBlue(image,p)));
1894 extent=(size_t) count;
1895 (void) strncpy((char *) q,buffer,extent);
1897 if ((q-pixels+extent+1) >= sizeof(pixels))
1900 (void) WriteBlob(image,q-pixels,pixels);
1903 p+=GetPixelChannels(image);
1906 (void) WriteBlob(image,q-pixels,pixels);
1908 if (image->previous == (Image *) NULL)
1910 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1912 if (status == MagickFalse)
1919 (void) WriteBlob(image,q-pixels,pixels);
1926 Convert image to a PBM image.
1928 (void) SetImageType(image,BilevelType,exception);
1930 quantum_info=AcquireQuantumInfo(image_info,image);
1931 if (quantum_info == (QuantumInfo *) NULL)
1932 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1933 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1934 quantum_info->min_is_white=MagickTrue;
1935 pixels=GetQuantumPixels(quantum_info);
1936 for (y=0; y < (ssize_t) image->rows; y++)
1938 register const Quantum
1941 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1942 if (p == (const Quantum *) NULL)
1944 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1945 GrayQuantum,pixels,exception);
1946 count=WriteBlob(image,extent,pixels);
1947 if (count != (ssize_t) extent)
1949 if (image->previous == (Image *) NULL)
1951 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1953 if (status == MagickFalse)
1957 quantum_info=DestroyQuantumInfo(quantum_info);
1963 Convert image to a PGM image.
1965 if (image->depth > 32)
1967 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
1968 ((MagickOffsetType) GetQuantumRange(image->depth)));
1969 (void) WriteBlobString(image,buffer);
1970 quantum_info=AcquireQuantumInfo(image_info,image);
1971 if (quantum_info == (QuantumInfo *) NULL)
1972 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1973 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1974 quantum_info->min_is_white=MagickTrue;
1975 pixels=GetQuantumPixels(quantum_info);
1976 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1977 for (y=0; y < (ssize_t) image->rows; y++)
1979 register const Quantum
1985 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1986 if (p == (const Quantum *) NULL)
1989 switch (image->depth)
1995 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1996 GrayQuantum,pixels,exception);
2001 if (image->depth <= 8)
2003 for (x=0; x < (ssize_t) image->columns; x++)
2005 if (IsPixelGray(image,p) == MagickFalse)
2006 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2007 image,p)),max_value);
2010 if (image->depth == 8)
2011 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
2013 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2016 q=PopCharPixel((unsigned char) pixel,q);
2017 p+=GetPixelChannels(image);
2019 extent=(size_t) (q-pixels);
2022 if (image->depth <= 16)
2024 for (x=0; x < (ssize_t) image->columns; x++)
2026 if (IsPixelGray(image,p) == MagickFalse)
2027 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2031 if (image->depth == 16)
2032 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2034 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2037 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2038 p+=GetPixelChannels(image);
2040 extent=(size_t) (q-pixels);
2043 for (x=0; x < (ssize_t) image->columns; x++)
2045 if (IsPixelGray(image,p) == MagickFalse)
2046 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2050 if (image->depth == 16)
2051 pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2053 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2055 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2056 p+=GetPixelChannels(image);
2058 extent=(size_t) (q-pixels);
2062 count=WriteBlob(image,extent,pixels);
2063 if (count != (ssize_t) extent)
2065 if (image->previous == (Image *) NULL)
2067 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2069 if (status == MagickFalse)
2073 quantum_info=DestroyQuantumInfo(quantum_info);
2079 Convert image to a PNM image.
2081 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2082 if (image->depth > 32)
2084 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2085 ((MagickOffsetType) GetQuantumRange(image->depth)));
2086 (void) WriteBlobString(image,buffer);
2087 quantum_info=AcquireQuantumInfo(image_info,image);
2088 if (quantum_info == (QuantumInfo *) NULL)
2089 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2090 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2091 pixels=GetQuantumPixels(quantum_info);
2092 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2093 for (y=0; y < (ssize_t) image->rows; y++)
2095 register const Quantum
2101 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2102 if (p == (const Quantum *) NULL)
2105 switch (image->depth)
2111 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2112 quantum_type,pixels,exception);
2117 if (image->depth <= 8)
2119 for (x=0; x < (ssize_t) image->columns; x++)
2121 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2122 q=PopCharPixel((unsigned char) pixel,q);
2123 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2124 q=PopCharPixel((unsigned char) pixel,q);
2125 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2126 q=PopCharPixel((unsigned char) pixel,q);
2127 p+=GetPixelChannels(image);
2129 extent=(size_t) (q-pixels);
2132 if (image->depth <= 16)
2134 for (x=0; x < (ssize_t) image->columns; x++)
2136 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2137 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2138 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2139 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2140 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2141 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2142 p+=GetPixelChannels(image);
2144 extent=(size_t) (q-pixels);
2147 for (x=0; x < (ssize_t) image->columns; x++)
2149 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2150 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2151 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2152 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2153 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2154 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2155 p+=GetPixelChannels(image);
2157 extent=(size_t) (q-pixels);
2161 count=WriteBlob(image,extent,pixels);
2162 if (count != (ssize_t) extent)
2164 if (image->previous == (Image *) NULL)
2166 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2168 if (status == MagickFalse)
2172 quantum_info=DestroyQuantumInfo(quantum_info);
2178 Convert image to a PAM.
2180 if (image->depth > 32)
2182 quantum_info=AcquireQuantumInfo(image_info,image);
2183 if (quantum_info == (QuantumInfo *) NULL)
2184 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2185 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2186 pixels=GetQuantumPixels(quantum_info);
2187 for (y=0; y < (ssize_t) image->rows; y++)
2189 register const Quantum
2195 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2196 if (p == (const Quantum *) NULL)
2199 switch (image->depth)
2205 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2206 quantum_type,pixels,exception);
2211 switch (quantum_type)
2214 case GrayAlphaQuantum:
2216 if (image->depth <= 8)
2218 for (x=0; x < (ssize_t) image->columns; x++)
2220 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2221 image,p)),max_value);
2222 q=PopCharPixel((unsigned char) pixel,q);
2223 if (image->alpha_trait != UndefinedPixelTrait)
2225 pixel=(unsigned char) ScaleQuantumToAny(
2226 GetPixelAlpha(image,p),max_value);
2227 q=PopCharPixel((unsigned char) pixel,q);
2229 p+=GetPixelChannels(image);
2233 if (image->depth <= 16)
2235 for (x=0; x < (ssize_t) image->columns; x++)
2237 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2238 image,p)),max_value);
2239 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2240 if (image->alpha_trait != UndefinedPixelTrait)
2242 pixel=(unsigned char) ScaleQuantumToAny(
2243 GetPixelAlpha(image,p),max_value);
2244 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2246 p+=GetPixelChannels(image);
2250 for (x=0; x < (ssize_t) image->columns; x++)
2252 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2254 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2255 if (image->alpha_trait != UndefinedPixelTrait)
2257 pixel=(unsigned char) ScaleQuantumToAny(
2258 GetPixelAlpha(image,p),max_value);
2259 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2261 p+=GetPixelChannels(image);
2268 if (image->depth <= 8)
2270 for (x=0; x < (ssize_t) image->columns; x++)
2272 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2273 q=PopCharPixel((unsigned char) pixel,q);
2274 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2276 q=PopCharPixel((unsigned char) pixel,q);
2277 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2279 q=PopCharPixel((unsigned char) pixel,q);
2280 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2282 q=PopCharPixel((unsigned char) pixel,q);
2283 if (image->alpha_trait != UndefinedPixelTrait)
2285 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2287 q=PopCharPixel((unsigned char) pixel,q);
2289 p+=GetPixelChannels(image);
2293 if (image->depth <= 16)
2295 for (x=0; x < (ssize_t) image->columns; x++)
2297 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2298 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2299 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2301 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2302 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2304 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2305 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2307 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2308 if (image->alpha_trait != UndefinedPixelTrait)
2310 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2312 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2314 p+=GetPixelChannels(image);
2318 for (x=0; x < (ssize_t) image->columns; x++)
2320 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2321 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2322 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2323 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2324 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2325 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2326 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2327 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2328 if (image->alpha_trait != UndefinedPixelTrait)
2330 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2332 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2334 p+=GetPixelChannels(image);
2340 if (image->depth <= 8)
2342 for (x=0; x < (ssize_t) image->columns; x++)
2344 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2345 q=PopCharPixel((unsigned char) pixel,q);
2346 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2348 q=PopCharPixel((unsigned char) pixel,q);
2349 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2351 q=PopCharPixel((unsigned char) pixel,q);
2352 if (image->alpha_trait != UndefinedPixelTrait)
2354 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2356 q=PopCharPixel((unsigned char) pixel,q);
2358 p+=GetPixelChannels(image);
2362 if (image->depth <= 16)
2364 for (x=0; x < (ssize_t) image->columns; x++)
2366 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2367 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2368 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2370 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2371 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2373 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2374 if (image->alpha_trait != UndefinedPixelTrait)
2376 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2378 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2380 p+=GetPixelChannels(image);
2384 for (x=0; x < (ssize_t) image->columns; x++)
2386 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2387 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2388 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2389 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2390 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2391 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2392 if (image->alpha_trait != UndefinedPixelTrait)
2394 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2396 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2398 p+=GetPixelChannels(image);
2403 extent=(size_t) (q-pixels);
2407 count=WriteBlob(image,extent,pixels);
2408 if (count != (ssize_t) extent)
2410 if (image->previous == (Image *) NULL)
2412 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2414 if (status == MagickFalse)
2418 quantum_info=DestroyQuantumInfo(quantum_info);
2424 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2427 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2428 quantum_info=AcquireQuantumInfo(image_info,image);
2429 if (quantum_info == (QuantumInfo *) NULL)
2430 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2431 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2432 if (status == MagickFalse)
2433 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2434 pixels=GetQuantumPixels(quantum_info);
2435 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2437 register const Quantum
2440 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2441 if (p == (const Quantum *) NULL)
2443 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2444 quantum_type,pixels,exception);
2445 (void) WriteBlob(image,extent,pixels);
2446 if (image->previous == (Image *) NULL)
2448 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2450 if (status == MagickFalse)
2454 quantum_info=DestroyQuantumInfo(quantum_info);
2458 if (GetNextImageInList(image) == (Image *) NULL)
2460 image=SyncNextImageInList(image);
2461 status=SetImageProgress(image,SaveImagesTag,scene++,
2462 GetImageListLength(image));
2463 if (status == MagickFalse)
2465 } while (image_info->adjoin != MagickFalse);
2466 (void) CloseBlob(image);