2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
72 static MagickBooleanType
73 WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsPNM() returns MagickTrue if the image format type, identified by the
87 % magick string, is PNM.
89 % The format of the IsPNM method is:
91 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o extent: Specifies the extent of the magick string.
100 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
104 if ((*magick == (unsigned char) 'P') &&
105 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
106 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
107 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d P N M I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % ReadPNMImage() reads a Portable Anymap image file and returns it.
124 % It allocates the memory necessary for the new Image structure and returns
125 % a pointer to the new image.
127 % The format of the ReadPNMImage method is:
129 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image_info: the image info.
135 % o exception: return any errors or warnings in this structure.
139 static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 const size_t extent,ExceptionInfo *exception)
142 if ((offset < 0) || (offset > (ssize_t) extent))
144 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
145 "InvalidPixel","`%s'",image->filename);
151 static 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->alpha_trait=BlendPixelTrait;
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->alpha_trait=BlendPixelTrait;
394 quantum_type=GrayAlphaQuantum;
396 if (LocaleCompare(value,"RGB_ALPHA") == 0)
398 image->alpha_trait=BlendPixelTrait;
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->alpha_trait=BlendPixelTrait;
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=(double) PNMInteger(image,10,exception);
561 pixel.green=(double) PNMInteger(image,10,exception);
562 pixel.blue=(double) PNMInteger(image,10,exception);
563 if (scale != (Quantum *) NULL)
565 pixel.red=(double) scale[ConstrainPixel(image,(ssize_t)
566 pixel.red,max_value,exception)];
567 pixel.green=(double) scale[ConstrainPixel(image,
568 (ssize_t) pixel.green,max_value,exception)];
569 pixel.blue=(double) 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 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
779 for (y=0; y < (ssize_t) image->rows; y++)
784 register const unsigned char
800 if (status == MagickFalse)
802 pixels=GetQuantumPixels(quantum_info);
804 count=ReadBlob(image,extent,pixels);
805 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
806 (image->previous == (Image *) NULL))
811 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
813 if (proceed == MagickFalse)
818 if (count != (ssize_t) extent)
820 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
821 if (q == (Quantum *) NULL)
827 if (image->depth == 8)
828 for (x=0; x < (ssize_t) image->columns; x++)
830 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
831 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
832 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
833 SetPixelAlpha(image,OpaqueAlpha,q);
834 q+=GetPixelChannels(image);
837 if (image->depth == 16)
842 for (x=0; x < (ssize_t) image->columns; x++)
844 p=PushShortPixel(MSBEndian,p,&pixel);
845 SetPixelRed(image,ScaleShortToQuantum(pixel),q);
846 p=PushShortPixel(MSBEndian,p,&pixel);
847 SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
848 p=PushShortPixel(MSBEndian,p,&pixel);
849 SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
850 SetPixelAlpha(image,OpaqueAlpha,q);
851 q+=GetPixelChannels(image);
855 if (image->depth <= 8)
860 for (x=0; x < (ssize_t) image->columns; x++)
862 p=PushCharPixel(p,&pixel);
863 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
864 p=PushCharPixel(p,&pixel);
865 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
866 p=PushCharPixel(p,&pixel);
867 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
868 SetPixelAlpha(image,OpaqueAlpha,q);
869 q+=GetPixelChannels(image);
877 for (x=0; x < (ssize_t) image->columns; x++)
879 p=PushShortPixel(MSBEndian,p,&pixel);
880 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
881 p=PushShortPixel(MSBEndian,p,&pixel);
882 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
883 p=PushShortPixel(MSBEndian,p,&pixel);
884 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
885 SetPixelAlpha(image,OpaqueAlpha,q);
886 q+=GetPixelChannels(image);
889 sync=SyncAuthenticPixels(image,exception);
890 if (sync == MagickFalse)
893 quantum_info=DestroyQuantumInfo(quantum_info);
894 if (status == MagickFalse)
895 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
907 Convert PAM raster image to pixel packets.
909 range=GetQuantumRange(image->depth);
910 switch (quantum_type)
913 case GrayAlphaQuantum:
930 if (image->alpha_trait == BlendPixelTrait)
932 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
933 quantum_info=AcquireQuantumInfo(image_info,image);
934 if (quantum_info == (QuantumInfo *) NULL)
935 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
936 for (y=0; y < (ssize_t) image->rows; y++)
941 register const unsigned char
957 if (status == MagickFalse)
959 pixels=GetQuantumPixels(quantum_info);
961 count=ReadBlob(image,extent,pixels);
962 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
963 (image->previous == (Image *) NULL))
968 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
970 if (proceed == MagickFalse)
975 if (count != (ssize_t) extent)
977 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
978 if (q == (Quantum *) NULL)
984 if ((image->depth == 8) || (image->depth == 16))
985 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
986 quantum_type,pixels,exception);
988 switch (quantum_type)
991 case GrayAlphaQuantum:
993 if (image->depth <= 8)
998 for (x=0; x < (ssize_t) image->columns; x++)
1000 p=PushCharPixel(p,&pixel);
1001 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1002 SetPixelAlpha(image,OpaqueAlpha,q);
1003 if (image->alpha_trait == BlendPixelTrait)
1005 p=PushCharPixel(p,&pixel);
1006 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1008 q+=GetPixelChannels(image);
1016 for (x=0; x < (ssize_t) image->columns; x++)
1018 p=PushShortPixel(MSBEndian,p,&pixel);
1019 SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1020 SetPixelAlpha(image,OpaqueAlpha,q);
1021 if (image->alpha_trait == BlendPixelTrait)
1023 p=PushShortPixel(MSBEndian,p,&pixel);
1024 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1026 q+=GetPixelChannels(image);
1034 if (image->depth <= 8)
1039 for (x=0; x < (ssize_t) image->columns; x++)
1041 p=PushCharPixel(p,&pixel);
1042 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1043 p=PushCharPixel(p,&pixel);
1044 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1045 p=PushCharPixel(p,&pixel);
1046 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1047 p=PushCharPixel(p,&pixel);
1048 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1049 SetPixelAlpha(image,OpaqueAlpha,q);
1050 if (image->alpha_trait == BlendPixelTrait)
1052 p=PushCharPixel(p,&pixel);
1053 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1055 q+=GetPixelChannels(image);
1063 for (x=0; x < (ssize_t) image->columns; x++)
1065 p=PushShortPixel(MSBEndian,p,&pixel);
1066 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1067 p=PushShortPixel(MSBEndian,p,&pixel);
1068 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1069 p=PushShortPixel(MSBEndian,p,&pixel);
1070 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1071 p=PushShortPixel(MSBEndian,p,&pixel);
1072 SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1073 SetPixelAlpha(image,OpaqueAlpha,q);
1074 if (image->alpha_trait == BlendPixelTrait)
1076 p=PushShortPixel(MSBEndian,p,&pixel);
1077 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1079 q+=GetPixelChannels(image);
1086 if (image->depth <= 8)
1091 for (x=0; x < (ssize_t) image->columns; x++)
1093 p=PushCharPixel(p,&pixel);
1094 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1095 p=PushCharPixel(p,&pixel);
1096 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1097 p=PushCharPixel(p,&pixel);
1098 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1099 SetPixelAlpha(image,OpaqueAlpha,q);
1100 if (image->alpha_trait == BlendPixelTrait)
1102 p=PushCharPixel(p,&pixel);
1103 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1105 q+=GetPixelChannels(image);
1113 for (x=0; x < (ssize_t) image->columns; x++)
1115 p=PushShortPixel(MSBEndian,p,&pixel);
1116 SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1117 p=PushShortPixel(MSBEndian,p,&pixel);
1118 SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1119 p=PushShortPixel(MSBEndian,p,&pixel);
1120 SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1121 SetPixelAlpha(image,OpaqueAlpha,q);
1122 if (image->alpha_trait == BlendPixelTrait)
1124 p=PushShortPixel(MSBEndian,p,&pixel);
1125 SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1127 q+=GetPixelChannels(image);
1133 sync=SyncAuthenticPixels(image,exception);
1134 if (sync == MagickFalse)
1137 quantum_info=DestroyQuantumInfo(quantum_info);
1138 if (status == MagickFalse)
1139 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1140 SetQuantumImageType(image,quantum_type);
1147 Convert PFM raster image to pixel packets.
1150 SetImageColorspace(image,GRAYColorspace,exception);
1151 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1152 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1154 quantum_info=AcquireQuantumInfo(image_info,image);
1155 if (quantum_info == (QuantumInfo *) NULL)
1156 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1157 status=SetQuantumDepth(image,quantum_info,32);
1158 if (status == MagickFalse)
1159 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1160 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1161 if (status == MagickFalse)
1162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1163 SetQuantumScale(quantum_info,(double) QuantumRange*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->alpha_trait == BlendPixelTrait)
1581 quantum_type=RGBAQuantum;
1583 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1587 if (image->alpha_trait == BlendPixelTrait)
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.
1616 for (y=0; y < (ssize_t) image->rows; y++)
1618 register const Quantum
1624 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1625 if (p == (const Quantum *) NULL)
1627 for (x=0; x < (ssize_t) image->columns; x++)
1629 pixel=GetPixelIntensity(image,p);
1630 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1633 if ((q-pixels+2) >= 80)
1636 (void) WriteBlob(image,q-pixels,pixels);
1639 p+=GetPixelChannels(image);
1641 if (image->previous == (Image *) NULL)
1643 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1645 if (status == MagickFalse)
1652 (void) WriteBlob(image,q-pixels,pixels);
1662 Convert image to a PGM image.
1664 if (image->depth <= 8)
1665 (void) WriteBlobString(image,"255\n");
1667 (void) WriteBlobString(image,"65535\n");
1669 for (y=0; y < (ssize_t) image->rows; y++)
1671 register const Quantum
1677 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1678 if (p == (const Quantum *) NULL)
1680 for (x=0; x < (ssize_t) image->columns; x++)
1682 index=GetPixelIntensity(image,p);
1683 if (image->depth <= 8)
1684 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1685 ScaleQuantumToChar(index));
1687 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1688 ScaleQuantumToShort(index));
1689 extent=(size_t) count;
1690 (void) strncpy((char *) q,buffer,extent);
1692 if ((q-pixels+extent) >= 80)
1695 (void) WriteBlob(image,q-pixels,pixels);
1698 p+=GetPixelChannels(image);
1700 if (image->previous == (Image *) NULL)
1702 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1704 if (status == MagickFalse)
1711 (void) WriteBlob(image,q-pixels,pixels);
1721 Convert image to a PNM image.
1723 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1724 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1725 if (image->depth <= 8)
1726 (void) WriteBlobString(image,"255\n");
1728 (void) WriteBlobString(image,"65535\n");
1730 for (y=0; y < (ssize_t) image->rows; y++)
1732 register const Quantum
1738 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1739 if (p == (const Quantum *) NULL)
1741 for (x=0; x < (ssize_t) image->columns; x++)
1743 if (image->depth <= 8)
1744 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1745 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
1746 ScaleQuantumToChar(GetPixelGreen(image,p)),
1747 ScaleQuantumToChar(GetPixelBlue(image,p)));
1749 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1750 "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
1751 ScaleQuantumToShort(GetPixelGreen(image,p)),
1752 ScaleQuantumToShort(GetPixelBlue(image,p)));
1753 extent=(size_t) count;
1754 (void) strncpy((char *) q,buffer,extent);
1756 if ((q-pixels+extent) >= 80)
1759 (void) WriteBlob(image,q-pixels,pixels);
1762 p+=GetPixelChannels(image);
1764 if (image->previous == (Image *) NULL)
1766 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1768 if (status == MagickFalse)
1775 (void) WriteBlob(image,q-pixels,pixels);
1782 Convert image to a PBM image.
1785 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1786 if (quantum_info == (QuantumInfo *) NULL)
1787 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1788 quantum_info->min_is_white=MagickTrue;
1789 pixels=GetQuantumPixels(quantum_info);
1790 for (y=0; y < (ssize_t) image->rows; y++)
1792 register const Quantum
1795 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1796 if (p == (const Quantum *) NULL)
1798 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1799 GrayQuantum,pixels,exception);
1800 count=WriteBlob(image,extent,pixels);
1801 if (count != (ssize_t) extent)
1803 if (image->previous == (Image *) NULL)
1805 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1807 if (status == MagickFalse)
1811 quantum_info=DestroyQuantumInfo(quantum_info);
1820 Convert image to a PGM image.
1822 if (image->depth > 8)
1824 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1825 ((MagickOffsetType) GetQuantumRange(image->depth)));
1826 (void) WriteBlobString(image,buffer);
1827 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1828 if (quantum_info == (QuantumInfo *) NULL)
1829 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1830 quantum_info->min_is_white=MagickTrue;
1831 pixels=GetQuantumPixels(quantum_info);
1832 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1833 range=GetQuantumRange(image->depth);
1834 for (y=0; y < (ssize_t) image->rows; y++)
1836 register const Quantum
1842 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1843 if (p == (const Quantum *) NULL)
1846 if ((image->depth == 8) || (image->depth == 16))
1847 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1848 GrayQuantum,pixels,exception);
1851 if (image->depth <= 8)
1852 for (x=0; x < (ssize_t) image->columns; x++)
1854 if (IsPixelGray(image,p) == MagickFalse)
1855 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1858 if (image->depth == 8)
1859 pixel=ScaleQuantumToChar(GetPixelRed(image,p));
1861 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1863 q=PopCharPixel((unsigned char) pixel,q);
1864 p+=GetPixelChannels(image);
1867 for (x=0; x < (ssize_t) image->columns; x++)
1869 if (IsPixelGray(image,p) == MagickFalse)
1870 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
1873 if (image->depth == 16)
1874 pixel=ScaleQuantumToShort(GetPixelRed(image,p));
1876 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1878 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1879 p+=GetPixelChannels(image);
1881 extent=(size_t) (q-pixels);
1883 count=WriteBlob(image,extent,pixels);
1884 if (count != (ssize_t) extent)
1886 if (image->previous == (Image *) NULL)
1888 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1890 if (status == MagickFalse)
1894 quantum_info=DestroyQuantumInfo(quantum_info);
1903 Convert image to a PNM image.
1905 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1906 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1907 if (image->depth > 8)
1909 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1910 ((MagickOffsetType) GetQuantumRange(image->depth)));
1911 (void) WriteBlobString(image,buffer);
1912 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1913 if (quantum_info == (QuantumInfo *) NULL)
1914 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1915 (void) SetQuantumEndian(image,quantum_info,MSBEndian);
1916 pixels=GetQuantumPixels(quantum_info);
1917 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1918 range=GetQuantumRange(image->depth);
1919 for (y=0; y < (ssize_t) image->rows; y++)
1921 register const Quantum
1927 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1928 if (p == (const Quantum *) NULL)
1931 if ((image->depth == 8) || (image->depth == 16))
1932 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1933 quantum_type,pixels,exception);
1936 if (image->depth <= 8)
1937 for (x=0; x < (ssize_t) image->columns; x++)
1939 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1940 q=PopCharPixel((unsigned char) pixel,q);
1941 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1942 q=PopCharPixel((unsigned char) pixel,q);
1943 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1944 q=PopCharPixel((unsigned char) pixel,q);
1945 p+=GetPixelChannels(image);
1948 for (x=0; x < (ssize_t) image->columns; x++)
1950 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
1951 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1952 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
1953 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1954 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
1955 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1956 p+=GetPixelChannels(image);
1958 extent=(size_t) (q-pixels);
1960 count=WriteBlob(image,extent,pixels);
1961 if (count != (ssize_t) extent)
1963 if (image->previous == (Image *) NULL)
1965 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1967 if (status == MagickFalse)
1971 quantum_info=DestroyQuantumInfo(quantum_info);
1980 Convert image to a PAM.
1982 if (image->depth > 16)
1984 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1985 pixels=GetQuantumPixels(quantum_info);
1986 range=GetQuantumRange(image->depth);
1987 for (y=0; y < (ssize_t) image->rows; y++)
1989 register const Quantum
1995 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1996 if (p == (const Quantum *) NULL)
1999 if ((image->depth == 8) || (image->depth == 16))
2000 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2001 quantum_type,pixels,exception);
2004 switch (quantum_type)
2007 case GrayAlphaQuantum:
2009 if (image->depth <= 8)
2010 for (x=0; x < (ssize_t) image->columns; x++)
2012 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2013 q=PopCharPixel((unsigned char) pixel,q);
2014 if (image->alpha_trait == BlendPixelTrait)
2016 pixel=(unsigned char) ScaleQuantumToAny(
2017 GetPixelAlpha(image,p),range);
2018 q=PopCharPixel((unsigned char) pixel,q);
2020 p+=GetPixelChannels(image);
2023 for (x=0; x < (ssize_t) image->columns; x++)
2025 pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
2026 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2027 if (image->alpha_trait == BlendPixelTrait)
2029 pixel=(unsigned char) ScaleQuantumToAny(
2030 GetPixelAlpha(image,p),range);
2031 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2033 p+=GetPixelChannels(image);
2040 if (image->depth <= 8)
2041 for (x=0; x < (ssize_t) image->columns; x++)
2043 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2044 q=PopCharPixel((unsigned char) pixel,q);
2045 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2046 q=PopCharPixel((unsigned char) pixel,q);
2047 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2048 q=PopCharPixel((unsigned char) pixel,q);
2049 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2050 q=PopCharPixel((unsigned char) pixel,q);
2051 if (image->alpha_trait == BlendPixelTrait)
2053 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2054 q=PopCharPixel((unsigned char) pixel,q);
2056 p+=GetPixelChannels(image);
2059 for (x=0; x < (ssize_t) image->columns; x++)
2061 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2062 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2063 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2064 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2065 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2066 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2067 pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
2068 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2069 if (image->alpha_trait == BlendPixelTrait)
2071 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2072 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2074 p+=GetPixelChannels(image);
2080 if (image->depth <= 8)
2081 for (x=0; x < (ssize_t) image->columns; x++)
2083 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2084 q=PopCharPixel((unsigned char) pixel,q);
2085 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2086 q=PopCharPixel((unsigned char) pixel,q);
2087 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2088 q=PopCharPixel((unsigned char) pixel,q);
2089 if (image->alpha_trait == BlendPixelTrait)
2091 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2092 q=PopCharPixel((unsigned char) pixel,q);
2094 p+=GetPixelChannels(image);
2097 for (x=0; x < (ssize_t) image->columns; x++)
2099 pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
2100 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2101 pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
2102 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2103 pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
2104 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2105 if (image->alpha_trait == BlendPixelTrait)
2107 pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
2108 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2110 p+=GetPixelChannels(image);
2115 extent=(size_t) (q-pixels);
2117 count=WriteBlob(image,extent,pixels);
2118 if (count != (ssize_t) extent)
2120 if (image->previous == (Image *) NULL)
2122 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2124 if (status == MagickFalse)
2128 quantum_info=DestroyQuantumInfo(quantum_info);
2134 (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2137 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2138 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2139 if (quantum_info == (QuantumInfo *) NULL)
2140 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2141 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2142 if (status == MagickFalse)
2143 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2144 pixels=GetQuantumPixels(quantum_info);
2145 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2147 register const Quantum
2150 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2151 if (p == (const Quantum *) NULL)
2153 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2154 quantum_type,pixels,exception);
2155 (void) WriteBlob(image,extent,pixels);
2156 if (image->previous == (Image *) NULL)
2158 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2160 if (status == MagickFalse)
2164 quantum_info=DestroyQuantumInfo(quantum_info);
2168 if (GetNextImageInList(image) == (Image *) NULL)
2170 image=SyncNextImageInList(image);
2171 status=SetImageProgress(image,SaveImagesTag,scene++,
2172 GetImageListLength(image));
2173 if (status == MagickFalse)
2175 } while (image_info->adjoin != MagickFalse);
2176 (void) CloseBlob(image);