2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
72 static MagickBooleanType
73 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsPNM() returns MagickTrue if the image format type, identified by the
87 % magick string, is PNM.
89 % The format of the IsPNM method is:
91 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o extent: Specifies the extent of the magick string.
100 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
104 if ((*magick == (unsigned char) 'P') &&
105 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
106 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
107 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d P N M I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % ReadPNMImage() reads a Portable Anymap image file and returns it.
124 % It allocates the memory necessary for the new Image structure and returns
125 % a pointer to the new image.
127 % The format of the ReadPNMImage method is:
129 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image_info: the image info.
135 % o exception: return any errors or warnings in this structure.
139 static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 const size_t extent,ExceptionInfo *exception)
142 if ((offset < 0) || (offset > (ssize_t) extent))
144 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
145 "InvalidPixel","`%s'",image->filename);
151 static size_t PNMInteger(Image *image,const unsigned int base,
152 ExceptionInfo *exception)
168 Skip any leading whitespace.
170 extent=MaxTextExtent;
171 comment=(char *) NULL;
175 c=ReadBlobByte(image);
183 if (comment == (char *) NULL)
184 comment=AcquireString((char *) NULL);
185 p=comment+strlen(comment);
186 for ( ; (c != EOF) && (c != (int) '\n'); p++)
188 if ((size_t) (p-comment+1) >= extent)
191 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
193 if (comment == (char *) NULL)
195 p=comment+strlen(comment);
197 c=ReadBlobByte(image);
204 if (comment == (char *) NULL)
208 } while (isdigit(c) == MagickFalse);
209 if (comment != (char *) NULL)
211 (void) SetImageProperty(image,"comment",comment,exception);
212 comment=DestroyString(comment);
215 return((size_t) (c-(int) '0'));
224 c=ReadBlobByte(image);
227 } while (isdigit(c) != MagickFalse);
231 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
271 assert(image_info != (const ImageInfo *) NULL);
272 assert(image_info->signature == MagickSignature);
273 if (image_info->debug != MagickFalse)
274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
275 image_info->filename);
276 assert(exception != (ExceptionInfo *) NULL);
277 assert(exception->signature == MagickSignature);
278 image=AcquireImage(image_info,exception);
279 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
280 if (status == MagickFalse)
282 image=DestroyImageList(image);
283 return((Image *) NULL);
288 count=ReadBlob(image,1,(unsigned char *) &format);
292 Initialize image structure.
294 if ((count != 1) || (format != 'P'))
295 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
297 quantum_type=RGBQuantum;
299 format=(char) ReadBlobByte(image);
303 PBM, PGM, PPM, and PNM.
305 image->columns=PNMInteger(image,10,exception);
306 image->rows=PNMInteger(image,10,exception);
307 if ((format == 'f') || (format == 'F'))
310 scale[MaxTextExtent];
312 (void) ReadBlobString(image,scale);
313 quantum_scale=StringToDouble(scale,(char **) NULL);
317 if ((format == '1') || (format == '4'))
318 max_value=1; /* bitmap */
320 max_value=PNMInteger(image,10,exception);
326 keyword[MaxTextExtent],
327 value[MaxTextExtent];
338 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
340 while (isspace((int) ((unsigned char) c)) != 0)
341 c=ReadBlobByte(image);
345 if ((size_t) (p-keyword) < (MaxTextExtent-1))
347 c=ReadBlobByte(image);
348 } while (isalnum(c));
350 if (LocaleCompare(keyword,"endhdr") == 0)
352 while (isspace((int) ((unsigned char) c)) != 0)
353 c=ReadBlobByte(image);
355 while (isalnum(c) || (c == '_'))
357 if ((size_t) (p-value) < (MaxTextExtent-1))
359 c=ReadBlobByte(image);
363 Assign a value to the specified keyword.
365 if (LocaleCompare(keyword,"depth") == 0)
366 packet_size=StringToUnsignedLong(value);
368 if (LocaleCompare(keyword,"height") == 0)
369 image->rows=StringToUnsignedLong(value);
370 if (LocaleCompare(keyword,"maxval") == 0)
371 max_value=StringToUnsignedLong(value);
372 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
374 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
376 SetImageColorspace(image,GRAYColorspace,exception);
377 quantum_type=GrayQuantum;
379 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
381 SetImageColorspace(image,GRAYColorspace,exception);
382 image->matte=MagickTrue;
383 quantum_type=GrayAlphaQuantum;
385 if (LocaleCompare(value,"GRAYSCALE") == 0)
387 quantum_type=GrayQuantum;
388 SetImageColorspace(image,GRAYColorspace,exception);
390 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
392 SetImageColorspace(image,GRAYColorspace,exception);
393 image->matte=MagickTrue;
394 quantum_type=GrayAlphaQuantum;
396 if (LocaleCompare(value,"RGB_ALPHA") == 0)
398 image->matte=MagickTrue;
399 quantum_type=RGBAQuantum;
401 if (LocaleCompare(value,"CMYK") == 0)
403 SetImageColorspace(image,CMYKColorspace,exception);
404 quantum_type=CMYKQuantum;
406 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
408 SetImageColorspace(image,CMYKColorspace,exception);
409 image->matte=MagickTrue;
410 quantum_type=CMYKAQuantum;
413 if (LocaleCompare(keyword,"width") == 0)
414 image->columns=StringToUnsignedLong(value);
417 if ((image->columns == 0) || (image->rows == 0))
418 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
419 if (max_value >= 65536)
420 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
421 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
423 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
424 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
427 Convert PNM pixels to runextent-encoded MIFF packets.
436 Convert PBM image to pixel packets.
438 SetImageColorspace(image,GRAYColorspace,exception);
439 for (y=0; y < (ssize_t) image->rows; y++)
447 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
448 if (q == (Quantum *) NULL)
450 for (x=0; x < (ssize_t) image->columns; x++)
452 SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
454 q+=GetPixelChannels(image);
456 if (SyncAuthenticPixels(image,exception) == MagickFalse)
458 if (image->previous == (Image *) NULL)
460 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
462 if (status == MagickFalse)
466 image->type=BilevelType;
475 Convert PGM image to pixel packets.
477 SetImageColorspace(image,GRAYColorspace,exception);
478 scale=(Quantum *) NULL;
479 if (max_value != (1U*QuantumRange))
482 Compute pixel scaling table.
484 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
486 if (scale == (Quantum *) NULL)
487 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
488 for (i=0; i <= (ssize_t) max_value; i++)
489 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
491 for (y=0; y < (ssize_t) image->rows; y++)
499 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
500 if (q == (Quantum *) NULL)
502 for (x=0; x < (ssize_t) image->columns; x++)
504 intensity=PNMInteger(image,10,exception);
505 SetPixelGray(image,intensity,q);
506 if (scale != (Quantum *) NULL)
507 SetPixelGray(image,scale[ConstrainPixel(image,(ssize_t) intensity,
508 max_value,exception)],q);
509 q+=GetPixelChannels(image);
511 if (SyncAuthenticPixels(image,exception) == MagickFalse)
513 if (image->previous == (Image *) NULL)
515 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
517 if (status == MagickFalse)
521 image->type=GrayscaleType;
522 if (scale != (Quantum *) NULL)
523 scale=(Quantum *) RelinquishMagickMemory(scale);
532 Convert PNM image to pixel packets.
534 scale=(Quantum *) NULL;
535 if (max_value != (1U*QuantumRange))
538 Compute pixel scaling table.
540 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
542 if (scale == (Quantum *) NULL)
543 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
544 for (i=0; i <= (ssize_t) max_value; i++)
545 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
547 for (y=0; y < (ssize_t) image->rows; y++)
555 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
556 if (q == (Quantum *) NULL)
558 for (x=0; x < (ssize_t) image->columns; x++)
560 pixel.red=(MagickRealType) PNMInteger(image,10,exception);
561 pixel.green=(MagickRealType) PNMInteger(image,10,exception);
562 pixel.blue=(MagickRealType) PNMInteger(image,10,exception);
563 if (scale != (Quantum *) NULL)
565 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
566 pixel.red,max_value,exception)];
567 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
568 (ssize_t) pixel.green,max_value,exception)];
569 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
570 pixel.blue,max_value,exception)];
572 SetPixelRed(image,ClampToQuantum(pixel.red),q);
573 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
574 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
575 q+=GetPixelChannels(image);
577 if (SyncAuthenticPixels(image,exception) == MagickFalse)
579 if (image->previous == (Image *) NULL)
581 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
583 if (status == MagickFalse)
587 if (scale != (Quantum *) NULL)
588 scale=(Quantum *) RelinquishMagickMemory(scale);
594 Convert PBM raw image to pixel packets.
596 SetImageColorspace(image,GRAYColorspace,exception);
597 quantum_type=GrayQuantum;
598 if (image->storage_class == PseudoClass)
599 quantum_type=IndexQuantum;
600 quantum_info=AcquireQuantumInfo(image_info,image);
601 if (quantum_info == (QuantumInfo *) NULL)
602 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
603 SetQuantumMinIsWhite(quantum_info,MagickTrue);
604 extent=GetQuantumExtent(image,quantum_info,quantum_type);
605 for (y=0; y < (ssize_t) image->rows; y++)
623 if (status == MagickFalse)
625 pixels=GetQuantumPixels(quantum_info);
627 count=ReadBlob(image,extent,pixels);
628 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
629 (image->previous == (Image *) NULL))
634 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
636 if (proceed == MagickFalse)
641 if (count != (ssize_t) extent)
643 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
644 if (q == (Quantum *) NULL)
649 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
650 quantum_type,pixels,exception);
651 if (length != extent)
653 sync=SyncAuthenticPixels(image,exception);
654 if (sync == MagickFalse)
657 quantum_info=DestroyQuantumInfo(quantum_info);
658 if (status == MagickFalse)
659 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
660 SetQuantumImageType(image,quantum_type);
669 Convert PGM raw image to pixel packets.
671 SetImageColorspace(image,GRAYColorspace,exception);
672 range=GetQuantumRange(image->depth);
673 quantum_type=GrayQuantum;
674 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
675 quantum_info=AcquireQuantumInfo(image_info,image);
676 if (quantum_info == (QuantumInfo *) NULL)
677 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
678 for (y=0; y < (ssize_t) image->rows; y++)
683 register const unsigned char
699 if (status == MagickFalse)
701 pixels=GetQuantumPixels(quantum_info);
703 count=ReadBlob(image,extent,pixels);
704 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
705 (image->previous == (Image *) NULL))
710 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
712 if (proceed == MagickFalse)
717 if (count != (ssize_t) extent)
719 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
720 if (q == (Quantum *) NULL)
726 if ((image->depth == 8) || (image->depth == 16))
727 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
728 quantum_type,pixels,exception);
730 if (image->depth <= 8)
735 for (x=0; x < (ssize_t) image->columns; x++)
737 p=PushCharPixel(p,&pixel);
738 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
739 q+=GetPixelChannels(image);
747 for (x=0; x < (ssize_t) image->columns; x++)
749 p=PushShortPixel(MSBEndian,p,&pixel);
750 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
751 q+=GetPixelChannels(image);
754 sync=SyncAuthenticPixels(image,exception);
755 if (sync == MagickFalse)
758 quantum_info=DestroyQuantumInfo(quantum_info);
759 if (status == MagickFalse)
760 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
761 SetQuantumImageType(image,quantum_type);
770 Convert PNM raster image to pixel packets.
772 quantum_type=RGBQuantum;
773 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
774 range=GetQuantumRange(image->depth);
775 quantum_info=AcquireQuantumInfo(image_info,image);
776 if (quantum_info == (QuantumInfo *) NULL)
777 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
778 for (y=0; y < (ssize_t) image->rows; y++)
783 register const unsigned char
799 if (status == MagickFalse)
801 pixels=GetQuantumPixels(quantum_info);
803 count=ReadBlob(image,extent,pixels);
804 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
805 (image->previous == (Image *) NULL))
810 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
812 if (proceed == MagickFalse)
817 if (count != (ssize_t) extent)
819 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
820 if (q == (Quantum *) NULL)
826 if (image->depth == 8)
827 for (x=0; x < (ssize_t) image->columns; x++)
829 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
830 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
831 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
832 SetPixelAlpha(image,OpaqueAlpha,q);
833 q+=GetPixelChannels(image);
836 if (image->depth == 16)
841 for (x=0; x < (ssize_t) image->columns; x++)
843 p=PushShortPixel(MSBEndian,p,&pixel);
844 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
845 p=PushShortPixel(MSBEndian,p,&pixel);
846 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
847 p=PushShortPixel(MSBEndian,p,&pixel);
848 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
849 SetPixelAlpha(image,OpaqueAlpha,q);
850 q+=GetPixelChannels(image);
854 if (image->depth <= 8)
859 for (x=0; x < (ssize_t) image->columns; x++)
861 p=PushCharPixel(p,&pixel);
862 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
863 p=PushCharPixel(p,&pixel);
864 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
865 p=PushCharPixel(p,&pixel);
866 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
867 SetPixelAlpha(image,OpaqueAlpha,q);
868 q+=GetPixelChannels(image);
876 for (x=0; x < (ssize_t) image->columns; x++)
878 p=PushShortPixel(MSBEndian,p,&pixel);
879 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
880 p=PushShortPixel(MSBEndian,p,&pixel);
881 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
882 p=PushShortPixel(MSBEndian,p,&pixel);
883 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
884 SetPixelAlpha(image,OpaqueAlpha,q);
885 q+=GetPixelChannels(image);
888 sync=SyncAuthenticPixels(image,exception);
889 if (sync == MagickFalse)
892 quantum_info=DestroyQuantumInfo(quantum_info);
893 if (status == MagickFalse)
894 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
906 Convert PAM raster image to pixel packets.
908 range=GetQuantumRange(image->depth);
909 switch (quantum_type)
912 case GrayAlphaQuantum:
929 if (image->matte != MagickFalse)
931 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
932 quantum_info=AcquireQuantumInfo(image_info,image);
933 if (quantum_info == (QuantumInfo *) NULL)
934 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
935 for (y=0; y < (ssize_t) image->rows; y++)
940 register const unsigned char
956 if (status == MagickFalse)
958 pixels=GetQuantumPixels(quantum_info);
960 count=ReadBlob(image,extent,pixels);
961 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
962 (image->previous == (Image *) NULL))
967 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
969 if (proceed == MagickFalse)
974 if (count != (ssize_t) extent)
976 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
977 if (q == (Quantum *) NULL)
983 if ((image->depth == 8) || (image->depth == 16))
984 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
985 quantum_type,pixels,exception);
987 switch (quantum_type)
990 case GrayAlphaQuantum:
992 if (image->depth <= 8)
997 for (x=0; x < (ssize_t) image->columns; x++)
999 p=PushCharPixel(p,&pixel);
1000 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1001 SetPixelAlpha(image,OpaqueAlpha,q);
1002 if (image->matte != MagickFalse)
1004 p=PushCharPixel(p,&pixel);
1005 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1007 q+=GetPixelChannels(image);
1015 for (x=0; x < (ssize_t) image->columns; x++)
1017 p=PushShortPixel(MSBEndian,p,&pixel);
1018 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1019 SetPixelAlpha(image,OpaqueAlpha,q);
1020 if (image->matte != MagickFalse)
1022 p=PushShortPixel(MSBEndian,p,&pixel);
1023 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1025 q+=GetPixelChannels(image);
1033 if (image->depth <= 8)
1038 for (x=0; x < (ssize_t) image->columns; x++)
1040 p=PushCharPixel(p,&pixel);
1041 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1042 p=PushCharPixel(p,&pixel);
1043 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1044 p=PushCharPixel(p,&pixel);
1045 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1046 p=PushCharPixel(p,&pixel);
1047 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1048 SetPixelAlpha(image,OpaqueAlpha,q);
1049 if (image->matte != MagickFalse)
1051 p=PushCharPixel(p,&pixel);
1052 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1054 q+=GetPixelChannels(image);
1062 for (x=0; x < (ssize_t) image->columns; x++)
1064 p=PushShortPixel(MSBEndian,p,&pixel);
1065 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1066 p=PushShortPixel(MSBEndian,p,&pixel);
1067 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1068 p=PushShortPixel(MSBEndian,p,&pixel);
1069 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1070 p=PushShortPixel(MSBEndian,p,&pixel);
1071 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1072 SetPixelAlpha(image,OpaqueAlpha,q);
1073 if (image->matte != MagickFalse)
1075 p=PushShortPixel(MSBEndian,p,&pixel);
1076 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1078 q+=GetPixelChannels(image);
1085 if (image->depth <= 8)
1090 for (x=0; x < (ssize_t) image->columns; x++)
1092 p=PushCharPixel(p,&pixel);
1093 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1094 p=PushCharPixel(p,&pixel);
1095 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1096 p=PushCharPixel(p,&pixel);
1097 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1098 SetPixelAlpha(image,OpaqueAlpha,q);
1099 if (image->matte != MagickFalse)
1101 p=PushCharPixel(p,&pixel);
1102 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1104 q+=GetPixelChannels(image);
1112 for (x=0; x < (ssize_t) image->columns; x++)
1114 p=PushShortPixel(MSBEndian,p,&pixel);
1115 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1116 p=PushShortPixel(MSBEndian,p,&pixel);
1117 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1118 p=PushShortPixel(MSBEndian,p,&pixel);
1119 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1120 SetPixelAlpha(image,OpaqueAlpha,q);
1121 if (image->matte != MagickFalse)
1123 p=PushShortPixel(MSBEndian,p,&pixel);
1124 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1126 q+=GetPixelChannels(image);
1132 sync=SyncAuthenticPixels(image,exception);
1133 if (sync == MagickFalse)
1136 quantum_info=DestroyQuantumInfo(quantum_info);
1137 if (status == MagickFalse)
1138 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1139 SetQuantumImageType(image,quantum_type);
1146 Convert PFM raster image to pixel packets.
1149 SetImageColorspace(image,GRAYColorspace,exception);
1150 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1151 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1153 quantum_info=AcquireQuantumInfo(image_info,image);
1154 if (quantum_info == (QuantumInfo *) NULL)
1155 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1156 status=SetQuantumDepth(image,quantum_info,32);
1157 if (status == MagickFalse)
1158 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1159 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1160 if (status == MagickFalse)
1161 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1162 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1163 fabs(quantum_scale));
1164 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1165 for (y=0; y < (ssize_t) image->rows; y++)
1183 if (status == MagickFalse)
1185 pixels=GetQuantumPixels(quantum_info);
1187 count=ReadBlob(image,extent,pixels);
1188 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1189 (image->previous == (Image *) NULL))
1194 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1196 if (proceed == MagickFalse)
1201 if ((size_t) count != extent)
1203 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1204 image->columns,1,exception);
1205 if (q == (Quantum *) NULL)
1210 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1211 quantum_type,pixels,exception);
1212 if (length != extent)
1214 sync=SyncAuthenticPixels(image,exception);
1215 if (sync == MagickFalse)
1218 quantum_info=DestroyQuantumInfo(quantum_info);
1219 if (status == MagickFalse)
1220 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1221 SetQuantumImageType(image,quantum_type);
1225 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1227 if (EOFBlob(image) != MagickFalse)
1229 (void) ThrowMagickException(exception,GetMagickModule(),
1230 CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1234 Proceed to next image.
1236 if (image_info->number_scenes != 0)
1237 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1239 if ((format == '1') || (format == '2') || (format == '3'))
1243 Skip to end of line.
1245 count=ReadBlob(image,1,(unsigned char *) &format);
1248 if ((count != 0) && (format == 'P'))
1250 } while (format != '\n');
1251 count=ReadBlob(image,1,(unsigned char *) &format);
1252 if ((count == 1) && (format == 'P'))
1255 Allocate next image structure.
1257 AcquireNextImage(image_info,image,exception);
1258 if (GetNextImageInList(image) == (Image *) NULL)
1260 image=DestroyImageList(image);
1261 return((Image *) NULL);
1263 image=SyncNextImageInList(image);
1264 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1265 GetBlobSize(image));
1266 if (status == MagickFalse)
1269 } while ((count == 1) && (format == 'P'));
1270 (void) CloseBlob(image);
1271 return(GetFirstImageInList(image));
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1279 % R e g i s t e r P N M I m a g e %
1283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1285 % RegisterPNMImage() adds properties for the PNM image format to
1286 % the list of supported formats. The properties include the image format
1287 % tag, a method to read and/or write the format, whether the format
1288 % supports the saving of more than one frame to the same file or blob,
1289 % whether the format supports native in-memory I/O, and a brief
1290 % description of the format.
1292 % The format of the RegisterPNMImage method is:
1294 % size_t RegisterPNMImage(void)
1297 ModuleExport size_t RegisterPNMImage(void)
1302 entry=SetMagickInfo("PAM");
1303 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1304 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1305 entry->description=ConstantString("Common 2-dimensional bitmap format");
1306 entry->module=ConstantString("PNM");
1307 (void) RegisterMagickInfo(entry);
1308 entry=SetMagickInfo("PBM");
1309 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1310 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1311 entry->description=ConstantString("Portable bitmap format (black and white)");
1312 entry->module=ConstantString("PNM");
1313 (void) RegisterMagickInfo(entry);
1314 entry=SetMagickInfo("PFM");
1315 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1316 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1317 entry->endian_support=MagickTrue;
1318 entry->description=ConstantString("Portable float format");
1319 entry->module=ConstantString("PFM");
1320 (void) RegisterMagickInfo(entry);
1321 entry=SetMagickInfo("PGM");
1322 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1323 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1324 entry->description=ConstantString("Portable graymap format (gray scale)");
1325 entry->module=ConstantString("PNM");
1326 (void) RegisterMagickInfo(entry);
1327 entry=SetMagickInfo("PNM");
1328 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1329 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1330 entry->magick=(IsImageFormatHandler *) IsPNM;
1331 entry->description=ConstantString("Portable anymap");
1332 entry->module=ConstantString("PNM");
1333 (void) RegisterMagickInfo(entry);
1334 entry=SetMagickInfo("PPM");
1335 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1336 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1337 entry->description=ConstantString("Portable pixmap format (color)");
1338 entry->module=ConstantString("PNM");
1339 (void) RegisterMagickInfo(entry);
1340 return(MagickImageCoderSignature);
1344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348 % U n r e g i s t e r P N M I m a g e %
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % UnregisterPNMImage() removes format registrations made by the
1355 % PNM module from the list of supported formats.
1357 % The format of the UnregisterPNMImage method is:
1359 % UnregisterPNMImage(void)
1362 ModuleExport void UnregisterPNMImage(void)
1364 (void) UnregisterMagickInfo("PAM");
1365 (void) UnregisterMagickInfo("PBM");
1366 (void) UnregisterMagickInfo("PGM");
1367 (void) UnregisterMagickInfo("PNM");
1368 (void) UnregisterMagickInfo("PPM");
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % W r i t e P N M I m a g e %
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1384 % The format of the WritePNMImage method is:
1386 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1387 % Image *image,ExceptionInfo *exception)
1389 % A description of each parameter follows.
1391 % o image_info: the image info.
1393 % o image: The image.
1395 % o exception: return any errors or warnings in this structure.
1398 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1399 ExceptionInfo *exception)
1402 buffer[MaxTextExtent],
1404 magick[MaxTextExtent];
1427 register unsigned char
1440 Open output image file.
1442 assert(image_info != (const ImageInfo *) NULL);
1443 assert(image_info->signature == MagickSignature);
1444 assert(image != (Image *) NULL);
1445 assert(image->signature == MagickSignature);
1446 if (image->debug != MagickFalse)
1447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1448 assert(exception != (ExceptionInfo *) NULL);
1449 assert(exception->signature == MagickSignature);
1450 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1451 if (status == MagickFalse)
1457 Write PNM file header.
1460 quantum_type=RGBQuantum;
1461 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1474 if (image_info->compression == NoCompression)
1482 if (IsImageGray(image,exception) != MagickFalse)
1490 if (image_info->compression == NoCompression)
1497 if ((image_info->type != TrueColorType) &&
1498 (IsImageGray(image,exception) != MagickFalse))
1501 if (image_info->compression == NoCompression)
1503 if (IsImageMonochrome(image,exception) != MagickFalse)
1506 if (image_info->compression == NoCompression)
1515 if (image_info->compression == NoCompression)
1520 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1521 (void) WriteBlobString(image,buffer);
1522 value=GetImageProperty(image,"comment",exception);
1523 if (value != (const char *) NULL)
1529 Write comments to file.
1531 (void) WriteBlobByte(image,'#');
1532 for (p=value; *p != '\0'; p++)
1534 (void) WriteBlobByte(image,(unsigned char) *p);
1535 if ((*p == '\r') && (*(p+1) != '\0'))
1536 (void) WriteBlobByte(image,'#');
1537 if ((*p == '\n') && (*(p+1) != '\0'))
1538 (void) WriteBlobByte(image,'#');
1540 (void) WriteBlobByte(image,'\n');
1544 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1545 (double) image->columns,(double) image->rows);
1546 (void) WriteBlobString(image,buffer);
1551 type[MaxTextExtent];
1556 (void) FormatLocaleString(buffer,MaxTextExtent,
1557 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1559 (void) WriteBlobString(image,buffer);
1560 quantum_type=GetQuantumType(image,exception);
1561 switch (quantum_type)
1567 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1571 case GrayAlphaQuantum:
1574 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1579 quantum_type=RGBQuantum;
1580 if (image->matte != MagickFalse)
1581 quantum_type=RGBAQuantum;
1583 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1587 if (image->matte != MagickFalse)
1590 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1592 if (image->depth > 16)
1594 (void) FormatLocaleString(buffer,MaxTextExtent,
1595 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1596 ((MagickOffsetType) GetQuantumRange(image->depth)));
1597 (void) WriteBlobString(image,buffer);
1598 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1600 (void) WriteBlobString(image,buffer);
1603 Convert runextent encoded to PNM raster pixels.
1613 Convert image to a PBM image.
1615 if (IsImageGray(image,exception) == MagickFalse)
1616 (void) TransformImageColorspace(image,GRAYColorspace,exception);
1618 for (y=0; y < (ssize_t) image->rows; y++)
1620 register const Quantum
1626 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1627 if (p == (const Quantum *) NULL)
1629 for (x=0; x < (ssize_t) image->columns; x++)
1631 pixel=GetPixelIntensity(image,p);
1632 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1635 if ((q-pixels+2) >= 80)
1638 (void) WriteBlob(image,q-pixels,pixels);
1641 p+=GetPixelChannels(image);
1643 if (image->previous == (Image *) NULL)
1645 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1647 if (status == MagickFalse)
1654 (void) WriteBlob(image,q-pixels,pixels);
1664 Convert image to a PGM image.
1666 if (IsImageGray(image,exception) == MagickFalse)
1667 (void) TransformImageColorspace(image,GRAYColorspace,exception);
1668 if (image->depth <= 8)
1669 (void) WriteBlobString(image,"255\n");
1671 (void) WriteBlobString(image,"65535\n");
1673 for (y=0; y < (ssize_t) image->rows; y++)
1675 register const Quantum
1681 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1682 if (p == (const Quantum *) NULL)
1684 for (x=0; x < (ssize_t) image->columns; x++)
1686 index=GetPixelIntensity(image,p);
1687 if (image->depth <= 8)
1688 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1689 ScaleQuantumToChar(index));
1691 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1692 ScaleQuantumToShort(index));
1693 extent=(size_t) count;
1694 (void) strncpy((char *) q,buffer,extent);
1696 if ((q-pixels+extent) >= 80)
1699 (void) WriteBlob(image,q-pixels,pixels);
1702 p+=GetPixelChannels(image);
1704 if (image->previous == (Image *) NULL)
1706 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1708 if (status == MagickFalse)
1715 (void) WriteBlob(image,q-pixels,pixels);
1725 Convert image to a PNM image.
1727 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1728 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1729 if (image->depth <= 8)
1730 (void) WriteBlobString(image,"255\n");
1732 (void) WriteBlobString(image,"65535\n");
1734 for (y=0; y < (ssize_t) image->rows; y++)
1736 register const Quantum
1742 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1743 if (p == (const Quantum *) NULL)
1745 for (x=0; x < (ssize_t) image->columns; x++)
1747 if (image->depth <= 8)
1748 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1749 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1750 ScaleQuantumToChar(GetPixelGreen(image,p)),
1751 ScaleQuantumToChar(GetPixelBlue(image,p)));
1753 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1754 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1755 ScaleQuantumToShort(GetPixelGreen(image,p)),
1756 ScaleQuantumToShort(GetPixelBlue(image,p)));
1757 extent=(size_t) count;
1758 (void) strncpy((char *) q,buffer,extent);
1760 if ((q-pixels+extent) >= 80)
1763 (void) WriteBlob(image,q-pixels,pixels);
1766 p+=GetPixelChannels(image);
1768 if (image->previous == (Image *) NULL)
1770 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1772 if (status == MagickFalse)
1779 (void) WriteBlob(image,q-pixels,pixels);
1786 Convert image to a PBM image.
1788 if (IsImageGray(image,exception) == MagickFalse)
1789 (void) TransformImageColorspace(image,GRAYColorspace,exception);
1791 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1792 if (quantum_info == (QuantumInfo *) NULL)
1793 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1794 quantum_info->min_is_white=MagickTrue;
1795 pixels=GetQuantumPixels(quantum_info);
1796 for (y=0; y < (ssize_t) image->rows; y++)
1798 register const Quantum
1801 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1802 if (p == (const Quantum *) NULL)
1804 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1805 GrayQuantum,pixels,exception);
1806 count=WriteBlob(image,extent,pixels);
1807 if (count != (ssize_t) extent)
1809 if (image->previous == (Image *) NULL)
1811 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1813 if (status == MagickFalse)
1817 quantum_info=DestroyQuantumInfo(quantum_info);
1826 Convert image to a PGM image.
1828 if (IsImageGray(image,exception) == MagickFalse)
1829 (void) TransformImageColorspace(image,GRAYColorspace,exception);
1830 if (image->depth > 8)
1832 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1833 ((MagickOffsetType) GetQuantumRange(image->depth)));
1834 (void) WriteBlobString(image,buffer);
1835 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1836 if (quantum_info == (QuantumInfo *) NULL)
1837 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1838 quantum_info->min_is_white=MagickTrue;
1839 pixels=GetQuantumPixels(quantum_info);
1840 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1841 range=GetQuantumRange(image->depth);
1842 for (y=0; y < (ssize_t) image->rows; y++)
1844 register const Quantum
1850 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1851 if (p == (const Quantum *) NULL)
1854 if ((image->depth == 8) || (image->depth == 16))
1855 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1856 GrayQuantum,pixels,exception);
1859 if (image->depth <= 8)
1860 for (x=0; x < (ssize_t) image->columns; x++)
1862 if (IsPixelGray(image,p) == MagickFalse)
1863 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1866 if (image->depth == 8)
1867 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1869 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1871 q=PopCharPixel((unsigned char) pixel,q);
1872 p+=GetPixelChannels(image);
1875 for (x=0; x < (ssize_t) image->columns; x++)
1877 if (IsPixelGray(image,p) == MagickFalse)
1878 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1881 if (image->depth == 16)
1882 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
1884 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1886 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1887 p+=GetPixelChannels(image);
1889 extent=(size_t) (q-pixels);
1891 count=WriteBlob(image,extent,pixels);
1892 if (count != (ssize_t) extent)
1894 if (image->previous == (Image *) NULL)
1896 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1898 if (status == MagickFalse)
1902 quantum_info=DestroyQuantumInfo(quantum_info);
1911 Convert image to a PNM image.
1913 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1914 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1915 if (image->depth > 8)
1917 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1918 ((MagickOffsetType) GetQuantumRange(image->depth)));
1919 (void) WriteBlobString(image,buffer);
1920 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1921 if (quantum_info == (QuantumInfo *) NULL)
1922 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1923 pixels=GetQuantumPixels(quantum_info);
1924 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1925 range=GetQuantumRange(image->depth);
1926 for (y=0; y < (ssize_t) image->rows; y++)
1928 register const Quantum
1934 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1935 if (p == (const Quantum *) NULL)
1938 if ((image->depth == 8) || (image->depth == 16))
1939 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1940 quantum_type,pixels,exception);
1943 if (image->depth <= 8)
1944 for (x=0; x < (ssize_t) image->columns; x++)
1946 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1947 q=PopCharPixel((unsigned char) pixel,q);
1948 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1949 q=PopCharPixel((unsigned char) pixel,q);
1950 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1951 q=PopCharPixel((unsigned char) pixel,q);
1952 p+=GetPixelChannels(image);
1955 for (x=0; x < (ssize_t) image->columns; x++)
1957 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1958 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1959 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1960 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1961 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1962 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1963 p+=GetPixelChannels(image);
1965 extent=(size_t) (q-pixels);
1967 count=WriteBlob(image,extent,pixels);
1968 if (count != (ssize_t) extent)
1970 if (image->previous == (Image *) NULL)
1972 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1974 if (status == MagickFalse)
1978 quantum_info=DestroyQuantumInfo(quantum_info);
1987 Convert image to a PAM.
1989 if (image->depth > 16)
1991 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1992 pixels=GetQuantumPixels(quantum_info);
1993 range=GetQuantumRange(image->depth);
1994 for (y=0; y < (ssize_t) image->rows; y++)
1996 register const Quantum
2002 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2003 if (p == (const Quantum *) NULL)
2006 if ((image->depth == 8) || (image->depth == 16))
2007 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2008 quantum_type,pixels,exception);
2011 switch (quantum_type)
2014 case GrayAlphaQuantum:
2016 if (image->depth <= 8)
2017 for (x=0; x < (ssize_t) image->columns; x++)
2019 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2020 q=PopCharPixel((unsigned char) pixel,q);
2021 if (image->matte != MagickFalse)
2023 pixel=(unsigned char) ScaleQuantumToAny(
2024 GetPixelAlpha(image,p),range);
2025 q=PopCharPixel((unsigned char) pixel,q);
2027 p+=GetPixelChannels(image);
2030 for (x=0; x < (ssize_t) image->columns; x++)
2032 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2033 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2034 if (image->matte != MagickFalse)
2036 pixel=(unsigned char) ScaleQuantumToAny(
2037 GetPixelAlpha(image,p),range);
2038 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2040 p+=GetPixelChannels(image);
2047 if (image->depth <= 8)
2048 for (x=0; x < (ssize_t) image->columns; x++)
2050 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2051 q=PopCharPixel((unsigned char) pixel,q);
2052 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2053 q=PopCharPixel((unsigned char) pixel,q);
2054 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2055 q=PopCharPixel((unsigned char) pixel,q);
2056 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2057 q=PopCharPixel((unsigned char) pixel,q);
2058 if (image->matte != MagickFalse)
2060 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2061 q=PopCharPixel((unsigned char) pixel,q);
2063 p+=GetPixelChannels(image);
2066 for (x=0; x < (ssize_t) image->columns; x++)
2068 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2069 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2070 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2071 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2072 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2073 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2074 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2075 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2076 if (image->matte != MagickFalse)
2078 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2079 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2081 p+=GetPixelChannels(image);
2087 if (image->depth <= 8)
2088 for (x=0; x < (ssize_t) image->columns; x++)
2090 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2091 q=PopCharPixel((unsigned char) pixel,q);
2092 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2093 q=PopCharPixel((unsigned char) pixel,q);
2094 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2095 q=PopCharPixel((unsigned char) pixel,q);
2096 if (image->matte != MagickFalse)
2098 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2099 q=PopCharPixel((unsigned char) pixel,q);
2101 p+=GetPixelChannels(image);
2104 for (x=0; x < (ssize_t) image->columns; x++)
2106 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2107 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2108 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2109 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2110 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2111 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2112 if (image->matte != MagickFalse)
2114 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2115 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2117 p+=GetPixelChannels(image);
2122 extent=(size_t) (q-pixels);
2124 count=WriteBlob(image,extent,pixels);
2125 if (count != (ssize_t) extent)
2127 if (image->previous == (Image *) NULL)
2129 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2131 if (status == MagickFalse)
2135 quantum_info=DestroyQuantumInfo(quantum_info);
2141 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2144 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2145 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2146 if (quantum_info == (QuantumInfo *) NULL)
2147 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2148 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2149 if (status == MagickFalse)
2150 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2151 pixels=GetQuantumPixels(quantum_info);
2152 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2154 register const Quantum
2157 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2158 if (p == (const Quantum *) NULL)
2160 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2161 quantum_type,pixels,exception);
2162 (void) WriteBlob(image,extent,pixels);
2163 if (image->previous == (Image *) NULL)
2165 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2167 if (status == MagickFalse)
2171 quantum_info=DestroyQuantumInfo(quantum_info);
2175 if (GetNextImageInList(image) == (Image *) NULL)
2177 image=SyncNextImageInList(image);
2178 status=SetImageProgress(image,SaveImagesTag,scene++,
2179 GetImageListLength(image));
2180 if (status == MagickFalse)
2182 } while (image_info->adjoin != MagickFalse);
2183 (void) CloseBlob(image);