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.
438 Convert PBM image to pixel packets.
440 (void) SetImageColorspace(image,GRAYColorspace,exception);
441 for (y=0; y < (ssize_t) image->rows; y++)
449 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
450 if (q == (Quantum *) NULL)
452 for (x=0; x < (ssize_t) image->columns; x++)
454 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
456 if (EOFBlob(image) != MagickFalse)
458 q+=GetPixelChannels(image);
460 if (SyncAuthenticPixels(image,exception) == MagickFalse)
462 if (image->previous == (Image *) NULL)
464 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
466 if (status == MagickFalse)
469 if (EOFBlob(image) != MagickFalse)
472 image->type=BilevelType;
481 Convert PGM image to pixel packets.
483 (void) SetImageColorspace(image,GRAYColorspace,exception);
484 for (y=0; y < (ssize_t) image->rows; y++)
492 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
493 if (q == (Quantum *) NULL)
495 for (x=0; x < (ssize_t) image->columns; x++)
497 intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),
499 if (EOFBlob(image) != MagickFalse)
501 SetPixelGray(image,intensity,q);
502 q+=GetPixelChannels(image);
504 if (SyncAuthenticPixels(image,exception) == MagickFalse)
506 if (image->previous == (Image *) NULL)
508 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
510 if (status == MagickFalse)
513 if (EOFBlob(image) != MagickFalse)
516 image->type=GrayscaleType;
522 Convert PNM image to pixel packets.
524 for (y=0; y < (ssize_t) image->rows; y++)
532 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
533 if (q == (Quantum *) NULL)
535 for (x=0; x < (ssize_t) image->columns; x++)
540 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
541 if (EOFBlob(image) != MagickFalse)
543 SetPixelRed(image,pixel,q);
544 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
545 SetPixelGreen(image,pixel,q);
546 pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
547 SetPixelBlue(image,pixel,q);
548 q+=GetPixelChannels(image);
550 if (SyncAuthenticPixels(image,exception) == MagickFalse)
552 if (image->previous == (Image *) NULL)
554 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
556 if (status == MagickFalse)
559 if (EOFBlob(image) != MagickFalse)
567 Convert PBM raw image to pixel packets.
569 (void) SetImageColorspace(image,GRAYColorspace,exception);
570 quantum_type=GrayQuantum;
571 if (image->storage_class == PseudoClass)
572 quantum_type=IndexQuantum;
573 quantum_info=AcquireQuantumInfo(image_info,image);
574 if (quantum_info == (QuantumInfo *) NULL)
575 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
576 SetQuantumMinIsWhite(quantum_info,MagickTrue);
577 extent=GetQuantumExtent(image,quantum_info,quantum_type);
578 for (y=0; y < (ssize_t) image->rows; y++)
596 pixels=(unsigned char *) ReadBlobStream(image,extent,
597 GetQuantumPixels(quantum_info),&count);
598 if (count != (ssize_t) extent)
600 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
601 (image->previous == (Image *) NULL))
606 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
608 if (proceed == MagickFalse)
612 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
613 if (q == (Quantum *) NULL)
615 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
616 quantum_type,pixels,exception);
617 if (length != extent)
619 sync=SyncAuthenticPixels(image,exception);
620 if (sync == MagickFalse)
623 quantum_info=DestroyQuantumInfo(quantum_info);
624 SetQuantumImageType(image,quantum_type);
630 Convert PGM raw image to pixel packets.
632 (void) SetImageColorspace(image,GRAYColorspace,exception);
633 quantum_type=GrayQuantum;
634 if (image->depth <= 8)
635 extent=image->columns;
637 if (image->depth <= 16)
638 extent=2*image->columns;
640 extent=4*image->columns;
641 quantum_info=AcquireQuantumInfo(image_info,image);
642 if (quantum_info == (QuantumInfo *) NULL)
643 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
644 for (y=0; y < (ssize_t) image->rows; y++)
652 register const unsigned char
665 pixels=(unsigned char *) ReadBlobStream(image,extent,
666 GetQuantumPixels(quantum_info),&count);
667 if (count != (ssize_t) extent)
669 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
670 (image->previous == (Image *) NULL))
675 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
677 if (proceed == MagickFalse)
681 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
682 if (q == (Quantum *) NULL)
685 switch (image->depth)
691 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
692 quantum_type,pixels,exception);
700 if (image->depth <= 8)
705 for (x=0; x < (ssize_t) image->columns; x++)
707 p=PushCharPixel(p,&pixel);
708 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
709 q+=GetPixelChannels(image);
713 if (image->depth <= 16)
718 for (x=0; x < (ssize_t) image->columns; x++)
720 p=PushShortPixel(MSBEndian,p,&pixel);
721 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
722 q+=GetPixelChannels(image);
726 for (x=0; x < (ssize_t) image->columns; x++)
728 p=PushLongPixel(MSBEndian,p,&pixel);
729 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
730 q+=GetPixelChannels(image);
735 sync=SyncAuthenticPixels(image,exception);
736 if (sync == MagickFalse)
739 quantum_info=DestroyQuantumInfo(quantum_info);
740 SetQuantumImageType(image,quantum_type);
746 Convert PNM raster image to pixel packets.
748 quantum_type=RGBQuantum;
749 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
750 quantum_info=AcquireQuantumInfo(image_info,image);
751 if (quantum_info == (QuantumInfo *) NULL)
752 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
753 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
754 for (y=0; y < (ssize_t) image->rows; y++)
762 register const unsigned char
775 pixels=(unsigned char *) ReadBlobStream(image,extent,
776 GetQuantumPixels(quantum_info),&count);
777 if (count != (ssize_t) extent)
779 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
780 (image->previous == (Image *) NULL))
785 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
787 if (proceed == MagickFalse)
791 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
792 if (q == (Quantum *) NULL)
795 switch (image->depth)
799 for (x=0; x < (ssize_t) image->columns; x++)
801 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
802 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
803 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
804 SetPixelAlpha(image,OpaqueAlpha,q);
805 q+=GetPixelChannels(image);
814 for (x=0; x < (ssize_t) image->columns; x++)
816 p=PushShortPixel(MSBEndian,p,&pixel);
817 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
818 p=PushShortPixel(MSBEndian,p,&pixel);
819 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
820 p=PushShortPixel(MSBEndian,p,&pixel);
821 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
822 SetPixelAlpha(image,OpaqueAlpha,q);
823 q+=GetPixelChannels(image);
832 for (x=0; x < (ssize_t) image->columns; x++)
834 p=PushLongPixel(MSBEndian,p,&pixel);
835 SetPixelRed(image,ScaleLongToQuantum(pixel),q);
836 p=PushLongPixel(MSBEndian,p,&pixel);
837 SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
838 p=PushLongPixel(MSBEndian,p,&pixel);
839 SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
840 SetPixelAlpha(image,OpaqueAlpha,q);
841 q+=GetPixelChannels(image);
850 if (image->depth <= 8)
855 for (x=0; x < (ssize_t) image->columns; x++)
857 p=PushCharPixel(p,&pixel);
858 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
859 p=PushCharPixel(p,&pixel);
860 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
861 p=PushCharPixel(p,&pixel);
862 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
863 SetPixelAlpha(image,OpaqueAlpha,q);
864 q+=GetPixelChannels(image);
868 if (image->depth <= 16)
873 for (x=0; x < (ssize_t) image->columns; x++)
875 p=PushShortPixel(MSBEndian,p,&pixel);
876 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
877 p=PushShortPixel(MSBEndian,p,&pixel);
878 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
879 p=PushShortPixel(MSBEndian,p,&pixel);
880 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
881 SetPixelAlpha(image,OpaqueAlpha,q);
882 q+=GetPixelChannels(image);
886 for (x=0; x < (ssize_t) image->columns; x++)
888 p=PushLongPixel(MSBEndian,p,&pixel);
889 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
890 p=PushLongPixel(MSBEndian,p,&pixel);
891 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
892 p=PushLongPixel(MSBEndian,p,&pixel);
893 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
894 SetPixelAlpha(image,OpaqueAlpha,q);
895 q+=GetPixelChannels(image);
900 sync=SyncAuthenticPixels(image,exception);
901 if (sync == MagickFalse)
904 quantum_info=DestroyQuantumInfo(quantum_info);
913 Convert PAM raster image to pixel packets.
915 switch (quantum_type)
918 case GrayAlphaQuantum:
935 if (image->alpha_trait != UndefinedPixelTrait)
937 if (image->depth <= 8)
938 extent=channels*image->columns;
940 if (image->depth <= 16)
941 extent=2*channels*image->columns;
943 extent=4*channels*image->columns;
944 quantum_info=AcquireQuantumInfo(image_info,image);
945 if (quantum_info == (QuantumInfo *) NULL)
946 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
947 for (y=0; y < (ssize_t) image->rows; y++)
955 register const unsigned char
968 pixels=(unsigned char *) ReadBlobStream(image,extent,
969 GetQuantumPixels(quantum_info),&count);
970 if (count != (ssize_t) extent)
972 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
973 (image->previous == (Image *) NULL))
978 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
980 if (proceed == MagickFalse)
984 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
985 if (q == (Quantum *) NULL)
988 switch (image->depth)
994 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
995 quantum_type,pixels,exception);
1000 switch (quantum_type)
1003 case GrayAlphaQuantum:
1008 if (image->depth <= 8)
1013 for (x=0; x < (ssize_t) image->columns; x++)
1015 p=PushCharPixel(p,&pixel);
1016 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1018 SetPixelAlpha(image,OpaqueAlpha,q);
1019 if (image->alpha_trait != UndefinedPixelTrait)
1021 p=PushCharPixel(p,&pixel);
1022 if (image->depth != 1)
1023 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1026 SetPixelAlpha(image,QuantumRange-
1027 ScaleAnyToQuantum(pixel,max_value),q);
1029 q+=GetPixelChannels(image);
1033 if (image->depth <= 16)
1038 for (x=0; x < (ssize_t) image->columns; x++)
1040 p=PushShortPixel(MSBEndian,p,&pixel);
1041 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1043 SetPixelAlpha(image,OpaqueAlpha,q);
1044 if (image->alpha_trait != UndefinedPixelTrait)
1046 p=PushShortPixel(MSBEndian,p,&pixel);
1047 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1050 q+=GetPixelChannels(image);
1054 for (x=0; x < (ssize_t) image->columns; x++)
1056 p=PushLongPixel(MSBEndian,p,&pixel);
1057 SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
1058 SetPixelAlpha(image,OpaqueAlpha,q);
1059 if (image->alpha_trait != UndefinedPixelTrait)
1061 p=PushLongPixel(MSBEndian,p,&pixel);
1062 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1065 q+=GetPixelChannels(image);
1075 if (image->depth <= 8)
1080 for (x=0; x < (ssize_t) image->columns; x++)
1082 p=PushCharPixel(p,&pixel);
1083 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1084 p=PushCharPixel(p,&pixel);
1085 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1087 p=PushCharPixel(p,&pixel);
1088 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1090 p=PushCharPixel(p,&pixel);
1091 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1093 SetPixelAlpha(image,OpaqueAlpha,q);
1094 if (image->alpha_trait != UndefinedPixelTrait)
1096 p=PushCharPixel(p,&pixel);
1097 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1100 q+=GetPixelChannels(image);
1104 if (image->depth <= 16)
1109 for (x=0; x < (ssize_t) image->columns; x++)
1111 p=PushShortPixel(MSBEndian,p,&pixel);
1112 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1113 p=PushShortPixel(MSBEndian,p,&pixel);
1114 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1116 p=PushShortPixel(MSBEndian,p,&pixel);
1117 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1119 p=PushShortPixel(MSBEndian,p,&pixel);
1120 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1122 SetPixelAlpha(image,OpaqueAlpha,q);
1123 if (image->alpha_trait != UndefinedPixelTrait)
1125 p=PushShortPixel(MSBEndian,p,&pixel);
1126 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1129 q+=GetPixelChannels(image);
1132 for (x=0; x < (ssize_t) image->columns; x++)
1134 p=PushLongPixel(MSBEndian,p,&pixel);
1135 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1136 p=PushLongPixel(MSBEndian,p,&pixel);
1137 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1138 p=PushLongPixel(MSBEndian,p,&pixel);
1139 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1140 p=PushLongPixel(MSBEndian,p,&pixel);
1141 SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
1142 SetPixelAlpha(image,OpaqueAlpha,q);
1143 if (image->alpha_trait != UndefinedPixelTrait)
1145 p=PushLongPixel(MSBEndian,p,&pixel);
1146 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1149 q+=GetPixelChannels(image);
1158 if (image->depth <= 8)
1163 for (x=0; x < (ssize_t) image->columns; x++)
1165 p=PushCharPixel(p,&pixel);
1166 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1167 p=PushCharPixel(p,&pixel);
1168 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1170 p=PushCharPixel(p,&pixel);
1171 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1173 SetPixelAlpha(image,OpaqueAlpha,q);
1174 if (image->alpha_trait != UndefinedPixelTrait)
1176 p=PushCharPixel(p,&pixel);
1177 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1180 q+=GetPixelChannels(image);
1184 if (image->depth <= 16)
1189 for (x=0; x < (ssize_t) image->columns; x++)
1191 p=PushShortPixel(MSBEndian,p,&pixel);
1192 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1193 p=PushShortPixel(MSBEndian,p,&pixel);
1194 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1196 p=PushShortPixel(MSBEndian,p,&pixel);
1197 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1199 SetPixelAlpha(image,OpaqueAlpha,q);
1200 if (image->alpha_trait != UndefinedPixelTrait)
1202 p=PushShortPixel(MSBEndian,p,&pixel);
1203 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1206 q+=GetPixelChannels(image);
1210 for (x=0; x < (ssize_t) image->columns; x++)
1212 p=PushLongPixel(MSBEndian,p,&pixel);
1213 SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1214 p=PushLongPixel(MSBEndian,p,&pixel);
1215 SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1216 p=PushLongPixel(MSBEndian,p,&pixel);
1217 SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1218 SetPixelAlpha(image,OpaqueAlpha,q);
1219 if (image->alpha_trait != UndefinedPixelTrait)
1221 p=PushLongPixel(MSBEndian,p,&pixel);
1222 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
1225 q+=GetPixelChannels(image);
1232 sync=SyncAuthenticPixels(image,exception);
1233 if (sync == MagickFalse)
1236 quantum_info=DestroyQuantumInfo(quantum_info);
1237 SetQuantumImageType(image,quantum_type);
1244 Convert PFM raster image to pixel packets.
1247 (void) SetImageColorspace(image,GRAYColorspace,exception);
1248 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1249 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1251 quantum_info=AcquireQuantumInfo(image_info,image);
1252 if (quantum_info == (QuantumInfo *) NULL)
1253 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1254 status=SetQuantumDepth(image,quantum_info,32);
1255 if (status == MagickFalse)
1256 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1257 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1258 if (status == MagickFalse)
1259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1260 SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1261 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1262 for (y=0; y < (ssize_t) image->rows; y++)
1280 pixels=(unsigned char *) ReadBlobStream(image,extent,
1281 GetQuantumPixels(quantum_info),&count);
1282 if ((size_t) count != extent)
1284 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1285 (image->previous == (Image *) NULL))
1290 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1292 if (proceed == MagickFalse)
1296 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1297 image->columns,1,exception);
1298 if (q == (Quantum *) NULL)
1300 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1301 quantum_type,pixels,exception);
1302 if (length != extent)
1304 sync=SyncAuthenticPixels(image,exception);
1305 if (sync == MagickFalse)
1308 quantum_info=DestroyQuantumInfo(quantum_info);
1309 SetQuantumImageType(image,quantum_type);
1313 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1315 if (y < (ssize_t) image->rows)
1316 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1317 if (EOFBlob(image) != MagickFalse)
1319 (void) ThrowMagickException(exception,GetMagickModule(),
1320 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1324 Proceed to next image.
1326 if (image_info->number_scenes != 0)
1327 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1329 if ((format == '1') || (format == '2') || (format == '3'))
1333 Skip to end of line.
1335 count=ReadBlob(image,1,(unsigned char *) &format);
1340 } while (format != '\n');
1341 count=ReadBlob(image,1,(unsigned char *) &format);
1342 if ((count == 1) && (format == 'P'))
1345 Allocate next image structure.
1347 AcquireNextImage(image_info,image,exception);
1348 if (GetNextImageInList(image) == (Image *) NULL)
1350 image=DestroyImageList(image);
1351 return((Image *) NULL);
1353 image=SyncNextImageInList(image);
1354 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1355 GetBlobSize(image));
1356 if (status == MagickFalse)
1359 } while ((count == 1) && (format == 'P'));
1360 (void) CloseBlob(image);
1361 return(GetFirstImageInList(image));
1365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % R e g i s t e r P N M I m a g e %
1373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375 % RegisterPNMImage() adds properties for the PNM image format to
1376 % the list of supported formats. The properties include the image format
1377 % tag, a method to read and/or write the format, whether the format
1378 % supports the saving of more than one frame to the same file or blob,
1379 % whether the format supports native in-memory I/O, and a brief
1380 % description of the format.
1382 % The format of the RegisterPNMImage method is:
1384 % size_t RegisterPNMImage(void)
1387 ModuleExport size_t RegisterPNMImage(void)
1392 entry=AcquireMagickInfo("PNM","PAM","Common 2-dimensional bitmap format");
1393 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1394 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1395 entry->mime_type=ConstantString("image/x-portable-pixmap");
1396 (void) RegisterMagickInfo(entry);
1397 entry=AcquireMagickInfo("PNM","PBM",
1398 "Portable bitmap format (black and white)");
1399 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1400 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1401 entry->mime_type=ConstantString("image/x-portable-bitmap");
1402 (void) RegisterMagickInfo(entry);
1403 entry=AcquireMagickInfo("PNM","PFM","Portable float format");
1404 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1405 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1406 entry->flags|=CoderEndianSupportFlag;
1407 (void) RegisterMagickInfo(entry);
1408 entry=AcquireMagickInfo("PNM","PGM","Portable graymap format (gray scale)");
1409 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1410 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1411 entry->mime_type=ConstantString("image/x-portable-greymap");
1412 (void) RegisterMagickInfo(entry);
1413 entry=AcquireMagickInfo("PNM","PNM","Portable anymap");
1414 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1415 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1416 entry->magick=(IsImageFormatHandler *) IsPNM;
1417 entry->mime_type=ConstantString("image/x-portable-pixmap");
1418 (void) RegisterMagickInfo(entry);
1419 entry=AcquireMagickInfo("PNM","PPM","Portable pixmap format (color)");
1420 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1421 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1422 entry->mime_type=ConstantString("image/x-portable-pixmap");
1423 (void) RegisterMagickInfo(entry);
1424 return(MagickImageCoderSignature);
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % U n r e g i s t e r P N M I m a g e %
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438 % UnregisterPNMImage() removes format registrations made by the
1439 % PNM module from the list of supported formats.
1441 % The format of the UnregisterPNMImage method is:
1443 % UnregisterPNMImage(void)
1446 ModuleExport void UnregisterPNMImage(void)
1448 (void) UnregisterMagickInfo("PAM");
1449 (void) UnregisterMagickInfo("PBM");
1450 (void) UnregisterMagickInfo("PGM");
1451 (void) UnregisterMagickInfo("PNM");
1452 (void) UnregisterMagickInfo("PPM");
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460 % W r i t e P N M I m a g e %
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1468 % The format of the WritePNMImage method is:
1470 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1471 % Image *image,ExceptionInfo *exception)
1473 % A description of each parameter follows.
1475 % o image_info: the image info.
1477 % o image: The image.
1479 % o exception: return any errors or warnings in this structure.
1482 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1483 ExceptionInfo *exception)
1486 buffer[MagickPathExtent],
1488 magick[MagickPathExtent];
1511 register unsigned char
1524 Open output image file.
1526 assert(image_info != (const ImageInfo *) NULL);
1527 assert(image_info->signature == MagickCoreSignature);
1528 assert(image != (Image *) NULL);
1529 assert(image->signature == MagickCoreSignature);
1530 if (image->debug != MagickFalse)
1531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1532 assert(exception != (ExceptionInfo *) NULL);
1533 assert(exception->signature == MagickCoreSignature);
1534 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1535 if (status == MagickFalse)
1544 Write PNM file header.
1547 quantum_type=RGBQuantum;
1548 (void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
1549 max_value=GetQuantumRange(image->depth);
1562 if (image_info->compression == NoCompression)
1570 if (SetImageGray(image,exception) != MagickFalse)
1578 if (image_info->compression == NoCompression)
1585 if ((image_info->type != TrueColorType) &&
1586 (SetImageGray(image,exception) != MagickFalse))
1589 if (image_info->compression == NoCompression)
1591 if (SetImageMonochrome(image,exception) != MagickFalse)
1594 if (image_info->compression == NoCompression)
1603 if (image_info->compression == NoCompression)
1608 (void) FormatLocaleString(buffer,MagickPathExtent,"P%c\n",format);
1609 (void) WriteBlobString(image,buffer);
1610 value=GetImageProperty(image,"comment",exception);
1611 if (value != (const char *) NULL)
1617 Write comments to file.
1619 (void) WriteBlobByte(image,'#');
1620 for (p=value; *p != '\0'; p++)
1622 (void) WriteBlobByte(image,(unsigned char) *p);
1623 if ((*p == '\n') || (*p == '\r'))
1624 (void) WriteBlobByte(image,'#');
1626 (void) WriteBlobByte(image,'\n');
1630 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",
1631 (double) image->columns,(double) image->rows);
1632 (void) WriteBlobString(image,buffer);
1637 type[MagickPathExtent];
1642 (void) FormatLocaleString(buffer,MagickPathExtent,
1643 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1645 (void) WriteBlobString(image,buffer);
1646 quantum_type=GetQuantumType(image,exception);
1647 switch (quantum_type)
1653 (void) CopyMagickString(type,"CMYK",MagickPathExtent);
1657 case GrayAlphaQuantum:
1660 (void) CopyMagickString(type,"GRAYSCALE",MagickPathExtent);
1661 if (IsImageMonochrome(image) != MagickFalse)
1662 (void) CopyMagickString(type,"BLACKANDWHITE",MagickPathExtent);
1667 quantum_type=RGBQuantum;
1668 if (image->alpha_trait != UndefinedPixelTrait)
1669 quantum_type=RGBAQuantum;
1671 (void) CopyMagickString(type,"RGB",MagickPathExtent);
1675 if (image->alpha_trait != UndefinedPixelTrait)
1678 (void) ConcatenateMagickString(type,"_ALPHA",MagickPathExtent);
1680 if (image->depth > 32)
1682 (void) FormatLocaleString(buffer,MagickPathExtent,
1683 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1684 ((MagickOffsetType) GetQuantumRange(image->depth)));
1685 (void) WriteBlobString(image,buffer);
1686 (void) FormatLocaleString(buffer,MagickPathExtent,
1687 "TUPLTYPE %s\nENDHDR\n",type);
1688 (void) WriteBlobString(image,buffer);
1691 Convert runextent encoded to PNM raster pixels.
1701 Convert image to a PBM image.
1703 (void) SetImageType(image,BilevelType,exception);
1705 for (y=0; y < (ssize_t) image->rows; y++)
1707 register const Quantum
1713 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1714 if (p == (const Quantum *) NULL)
1716 for (x=0; x < (ssize_t) image->columns; x++)
1718 *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
1721 if ((q-pixels+1) >= (ssize_t) sizeof(pixels))
1724 (void) WriteBlob(image,q-pixels,pixels);
1727 p+=GetPixelChannels(image);
1730 (void) WriteBlob(image,q-pixels,pixels);
1732 if (image->previous == (Image *) NULL)
1734 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1736 if (status == MagickFalse)
1743 (void) WriteBlob(image,q-pixels,pixels);
1753 Convert image to a PGM image.
1755 if (image->depth <= 8)
1756 (void) WriteBlobString(image,"255\n");
1758 if (image->depth <= 16)
1759 (void) WriteBlobString(image,"65535\n");
1761 (void) WriteBlobString(image,"4294967295\n");
1763 for (y=0; y < (ssize_t) image->rows; y++)
1765 register const Quantum
1771 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1772 if (p == (const Quantum *) NULL)
1774 for (x=0; x < (ssize_t) image->columns; x++)
1776 index=ClampToQuantum(GetPixelLuma(image,p));
1777 if (image->depth <= 8)
1778 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,"%u ",
1779 ScaleQuantumToChar(index));
1781 if (image->depth <= 16)
1782 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1783 "%u ",ScaleQuantumToShort(index));
1785 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1786 "%u ",ScaleQuantumToLong(index));
1787 extent=(size_t) count;
1788 (void) strncpy((char *) q,buffer,extent);
1790 if ((q-pixels+extent+1) >= sizeof(pixels))
1793 (void) WriteBlob(image,q-pixels,pixels);
1796 p+=GetPixelChannels(image);
1799 (void) WriteBlob(image,q-pixels,pixels);
1801 if (image->previous == (Image *) NULL)
1803 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1805 if (status == MagickFalse)
1812 (void) WriteBlob(image,q-pixels,pixels);
1822 Convert image to a PNM image.
1824 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1825 if (image->depth <= 8)
1826 (void) WriteBlobString(image,"255\n");
1828 if (image->depth <= 16)
1829 (void) WriteBlobString(image,"65535\n");
1831 (void) WriteBlobString(image,"4294967295\n");
1833 for (y=0; y < (ssize_t) image->rows; y++)
1835 register const Quantum
1841 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1842 if (p == (const Quantum *) NULL)
1844 for (x=0; x < (ssize_t) image->columns; x++)
1846 if (image->depth <= 8)
1847 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1848 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1849 ScaleQuantumToChar(GetPixelGreen(image,p)),
1850 ScaleQuantumToChar(GetPixelBlue(image,p)));
1852 if (image->depth <= 16)
1853 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1854 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1855 ScaleQuantumToShort(GetPixelGreen(image,p)),
1856 ScaleQuantumToShort(GetPixelBlue(image,p)));
1858 count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
1859 "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
1860 ScaleQuantumToLong(GetPixelGreen(image,p)),
1861 ScaleQuantumToLong(GetPixelBlue(image,p)));
1862 extent=(size_t) count;
1863 (void) strncpy((char *) q,buffer,extent);
1865 if ((q-pixels+extent+1) >= sizeof(pixels))
1868 (void) WriteBlob(image,q-pixels,pixels);
1871 p+=GetPixelChannels(image);
1874 (void) WriteBlob(image,q-pixels,pixels);
1876 if (image->previous == (Image *) NULL)
1878 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1880 if (status == MagickFalse)
1887 (void) WriteBlob(image,q-pixels,pixels);
1894 Convert image to a PBM image.
1896 (void) SetImageType(image,BilevelType,exception);
1898 quantum_info=AcquireQuantumInfo(image_info,image);
1899 if (quantum_info == (QuantumInfo *) NULL)
1900 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1901 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1902 quantum_info->min_is_white=MagickTrue;
1903 pixels=GetQuantumPixels(quantum_info);
1904 for (y=0; y < (ssize_t) image->rows; y++)
1906 register const Quantum
1909 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1910 if (p == (const Quantum *) NULL)
1912 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1913 GrayQuantum,pixels,exception);
1914 count=WriteBlob(image,extent,pixels);
1915 if (count != (ssize_t) extent)
1917 if (image->previous == (Image *) NULL)
1919 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1921 if (status == MagickFalse)
1925 quantum_info=DestroyQuantumInfo(quantum_info);
1931 Convert image to a PGM image.
1933 if (image->depth > 32)
1935 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
1936 ((MagickOffsetType) GetQuantumRange(image->depth)));
1937 (void) WriteBlobString(image,buffer);
1938 quantum_info=AcquireQuantumInfo(image_info,image);
1939 if (quantum_info == (QuantumInfo *) NULL)
1940 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1941 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1942 quantum_info->min_is_white=MagickTrue;
1943 pixels=GetQuantumPixels(quantum_info);
1944 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1945 for (y=0; y < (ssize_t) image->rows; y++)
1947 register const Quantum
1953 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1954 if (p == (const Quantum *) NULL)
1957 switch (image->depth)
1963 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1964 GrayQuantum,pixels,exception);
1969 if (image->depth <= 8)
1971 for (x=0; x < (ssize_t) image->columns; x++)
1973 if (IsPixelGray(image,p) == MagickFalse)
1974 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
1975 image,p)),max_value);
1978 if (image->depth == 8)
1979 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1981 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
1984 q=PopCharPixel((unsigned char) pixel,q);
1985 p+=GetPixelChannels(image);
1987 extent=(size_t) (q-pixels);
1990 if (image->depth <= 16)
1992 for (x=0; x < (ssize_t) image->columns; x++)
1994 if (IsPixelGray(image,p) == MagickFalse)
1995 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
1999 if (image->depth == 16)
2000 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2002 pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2005 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2006 p+=GetPixelChannels(image);
2008 extent=(size_t) (q-pixels);
2011 for (x=0; x < (ssize_t) image->columns; x++)
2013 if (IsPixelGray(image,p) == MagickFalse)
2014 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2018 if (image->depth == 16)
2019 pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2021 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2023 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2024 p+=GetPixelChannels(image);
2026 extent=(size_t) (q-pixels);
2030 count=WriteBlob(image,extent,pixels);
2031 if (count != (ssize_t) extent)
2033 if (image->previous == (Image *) NULL)
2035 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2037 if (status == MagickFalse)
2041 quantum_info=DestroyQuantumInfo(quantum_info);
2047 Convert image to a PNM image.
2049 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2050 if (image->depth > 32)
2052 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2053 ((MagickOffsetType) GetQuantumRange(image->depth)));
2054 (void) WriteBlobString(image,buffer);
2055 quantum_info=AcquireQuantumInfo(image_info,image);
2056 if (quantum_info == (QuantumInfo *) NULL)
2057 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2058 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2059 pixels=GetQuantumPixels(quantum_info);
2060 extent=GetQuantumExtent(image,quantum_info,quantum_type);
2061 for (y=0; y < (ssize_t) image->rows; y++)
2063 register const Quantum
2069 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2070 if (p == (const Quantum *) NULL)
2073 switch (image->depth)
2079 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2080 quantum_type,pixels,exception);
2085 if (image->depth <= 8)
2087 for (x=0; x < (ssize_t) image->columns; x++)
2089 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2090 q=PopCharPixel((unsigned char) pixel,q);
2091 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2092 q=PopCharPixel((unsigned char) pixel,q);
2093 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2094 q=PopCharPixel((unsigned char) pixel,q);
2095 p+=GetPixelChannels(image);
2097 extent=(size_t) (q-pixels);
2100 if (image->depth <= 16)
2102 for (x=0; x < (ssize_t) image->columns; x++)
2104 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2105 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2106 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2107 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2108 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2109 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2110 p+=GetPixelChannels(image);
2112 extent=(size_t) (q-pixels);
2115 for (x=0; x < (ssize_t) image->columns; x++)
2117 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2118 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2119 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2120 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2121 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2122 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2123 p+=GetPixelChannels(image);
2125 extent=(size_t) (q-pixels);
2129 count=WriteBlob(image,extent,pixels);
2130 if (count != (ssize_t) extent)
2132 if (image->previous == (Image *) NULL)
2134 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2136 if (status == MagickFalse)
2140 quantum_info=DestroyQuantumInfo(quantum_info);
2146 Convert image to a PAM.
2148 if (image->depth > 32)
2150 quantum_info=AcquireQuantumInfo(image_info,image);
2151 if (quantum_info == (QuantumInfo *) NULL)
2152 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2153 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2154 pixels=GetQuantumPixels(quantum_info);
2155 for (y=0; y < (ssize_t) image->rows; y++)
2157 register const Quantum
2163 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2164 if (p == (const Quantum *) NULL)
2167 switch (image->depth)
2173 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2174 quantum_type,pixels,exception);
2179 switch (quantum_type)
2182 case GrayAlphaQuantum:
2184 if (image->depth <= 8)
2186 for (x=0; x < (ssize_t) image->columns; x++)
2188 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2189 image,p)),max_value);
2190 q=PopCharPixel((unsigned char) pixel,q);
2191 if (image->alpha_trait != UndefinedPixelTrait)
2193 pixel=(unsigned char) ScaleQuantumToAny(
2194 GetPixelAlpha(image,p),max_value);
2195 q=PopCharPixel((unsigned char) pixel,q);
2197 p+=GetPixelChannels(image);
2201 if (image->depth <= 16)
2203 for (x=0; x < (ssize_t) image->columns; x++)
2205 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2206 image,p)),max_value);
2207 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2208 if (image->alpha_trait != UndefinedPixelTrait)
2210 pixel=(unsigned char) ScaleQuantumToAny(
2211 GetPixelAlpha(image,p),max_value);
2212 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2214 p+=GetPixelChannels(image);
2218 for (x=0; x < (ssize_t) image->columns; x++)
2220 pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2222 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2223 if (image->alpha_trait != UndefinedPixelTrait)
2225 pixel=(unsigned char) ScaleQuantumToAny(
2226 GetPixelAlpha(image,p),max_value);
2227 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2229 p+=GetPixelChannels(image);
2236 if (image->depth <= 8)
2238 for (x=0; x < (ssize_t) image->columns; x++)
2240 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2241 q=PopCharPixel((unsigned char) pixel,q);
2242 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2244 q=PopCharPixel((unsigned char) pixel,q);
2245 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2247 q=PopCharPixel((unsigned char) pixel,q);
2248 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2250 q=PopCharPixel((unsigned char) pixel,q);
2251 if (image->alpha_trait != UndefinedPixelTrait)
2253 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2255 q=PopCharPixel((unsigned char) pixel,q);
2257 p+=GetPixelChannels(image);
2261 if (image->depth <= 16)
2263 for (x=0; x < (ssize_t) image->columns; x++)
2265 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2266 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2267 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2269 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2270 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2272 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2273 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2275 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2276 if (image->alpha_trait != UndefinedPixelTrait)
2278 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2280 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2282 p+=GetPixelChannels(image);
2286 for (x=0; x < (ssize_t) image->columns; x++)
2288 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2289 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2290 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2291 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2292 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2293 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2294 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2295 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2296 if (image->alpha_trait != UndefinedPixelTrait)
2298 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2300 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2302 p+=GetPixelChannels(image);
2308 if (image->depth <= 8)
2310 for (x=0; x < (ssize_t) image->columns; x++)
2312 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2313 q=PopCharPixel((unsigned char) pixel,q);
2314 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2316 q=PopCharPixel((unsigned char) pixel,q);
2317 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2319 q=PopCharPixel((unsigned char) pixel,q);
2320 if (image->alpha_trait != UndefinedPixelTrait)
2322 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2324 q=PopCharPixel((unsigned char) pixel,q);
2326 p+=GetPixelChannels(image);
2330 if (image->depth <= 16)
2332 for (x=0; x < (ssize_t) image->columns; x++)
2334 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2335 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2336 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2338 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2339 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2341 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2342 if (image->alpha_trait != UndefinedPixelTrait)
2344 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2346 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2348 p+=GetPixelChannels(image);
2352 for (x=0; x < (ssize_t) image->columns; x++)
2354 pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2355 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2356 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2357 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2358 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2359 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2360 if (image->alpha_trait != UndefinedPixelTrait)
2362 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2364 q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2366 p+=GetPixelChannels(image);
2371 extent=(size_t) (q-pixels);
2375 count=WriteBlob(image,extent,pixels);
2376 if (count != (ssize_t) extent)
2378 if (image->previous == (Image *) NULL)
2380 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2382 if (status == MagickFalse)
2386 quantum_info=DestroyQuantumInfo(quantum_info);
2392 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2395 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2396 quantum_info=AcquireQuantumInfo(image_info,image);
2397 if (quantum_info == (QuantumInfo *) NULL)
2398 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2399 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2400 if (status == MagickFalse)
2401 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2402 pixels=GetQuantumPixels(quantum_info);
2403 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2405 register const Quantum
2408 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2409 if (p == (const Quantum *) NULL)
2411 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2412 quantum_type,pixels,exception);
2413 (void) WriteBlob(image,extent,pixels);
2414 if (image->previous == (Image *) NULL)
2416 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2418 if (status == MagickFalse)
2422 quantum_info=DestroyQuantumInfo(quantum_info);
2426 if (GetNextImageInList(image) == (Image *) NULL)
2428 image=SyncNextImageInList(image);
2429 status=SetImageProgress(image,SaveImagesTag,scene++,
2430 GetImageListLength(image));
2431 if (status == MagickFalse)
2433 } while (image_info->adjoin != MagickFalse);
2434 (void) CloseBlob(image);