2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/module.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/pixel-private.h"
60 #include "magick/property.h"
61 #include "magick/quantum-private.h"
62 #include "magick/static.h"
63 #include "magick/statistic.h"
64 #include "magick/string_.h"
65 #include "magick/string-private.h"
70 static MagickBooleanType
71 WritePNMImage(const ImageInfo *,Image *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % IsPNM() returns MagickTrue if the image format type, identified by the
85 % magick string, is PNM.
87 % The format of the IsPNM method is:
89 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
91 % A description of each parameter follows:
93 % o magick: compare image format pattern against these bytes.
95 % o extent: Specifies the extent of the magick string.
98 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
102 if ((*magick == (unsigned char) 'P') &&
103 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
104 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
105 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % R e a d P N M I m a g e %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % ReadPNMImage() reads a Portable Anymap image file and returns it.
122 % It allocates the memory necessary for the new Image structure and returns
123 % a pointer to the new image.
125 % The format of the ReadPNMImage method is:
127 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 % A description of each parameter follows:
131 % o image_info: the image info.
133 % o exception: return any errors or warnings in this structure.
137 static inline long ConstrainPixel(Image *image,const long offset,
138 const unsigned long extent)
140 if ((offset < 0) || (offset > (long) extent))
142 (void) ThrowMagickException(&image->exception,GetMagickModule(),
143 CorruptImageError,"InvalidPixel","`%s'",image->filename);
149 static unsigned long PNMInteger(Image *image,const unsigned int base)
167 Skip any leading whitespace.
169 extent=MaxTextExtent;
170 comment=(char *) NULL;
174 c=ReadBlobByte(image);
182 if (comment == (char *) NULL)
183 comment=AcquireString((char *) NULL);
184 p=comment+strlen(comment);
185 for ( ; (c != EOF) && (c != (int) '\n'); p++)
187 if ((size_t) (p-comment+1) >= extent)
190 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
192 if (comment == (char *) NULL)
194 p=comment+strlen(comment);
196 c=ReadBlobByte(image);
200 if (comment == (char *) NULL)
204 } while (isdigit(c) == MagickFalse);
205 if (comment != (char *) NULL)
207 (void) SetImageProperty(image,"comment",comment);
208 comment=DestroyString(comment);
211 return((unsigned long) (c-(int) '0'));
220 c=ReadBlobByte(image);
223 } while (isdigit(c) != MagickFalse);
227 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);
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);
306 image->rows=PNMInteger(image,10);
307 if ((format == 'f') || (format == 'F'))
310 scale[MaxTextExtent];
312 (void) ReadBlobString(image,scale);
313 quantum_scale=StringToDouble(scale);
317 if ((format == '1') || (format == '4'))
318 max_value=1; /* bitmap */
320 max_value=PNMInteger(image,10);
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);
367 if (LocaleCompare(keyword,"height") == 0)
368 image->rows=StringToUnsignedLong(value);
369 if (LocaleCompare(keyword,"maxval") == 0)
370 max_value=StringToUnsignedLong(value);
371 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
373 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
374 quantum_type=GrayQuantum;
375 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
377 quantum_type=GrayAlphaQuantum;
378 image->matte=MagickTrue;
380 if (LocaleCompare(value,"GRAYSCALE") == 0)
381 quantum_type=GrayQuantum;
382 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
384 quantum_type=GrayAlphaQuantum;
385 image->matte=MagickTrue;
387 if (LocaleCompare(value,"RGB_ALPHA") == 0)
389 quantum_type=RGBAQuantum;
390 image->matte=MagickTrue;
392 if (LocaleCompare(value,"CMYK") == 0)
394 quantum_type=CMYKQuantum;
395 image->colorspace=CMYKColorspace;
397 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
399 quantum_type=CMYKAQuantum;
400 image->colorspace=CMYKColorspace;
401 image->matte=MagickTrue;
404 if (LocaleCompare(keyword,"width") == 0)
405 image->columns=StringToUnsignedLong(value);
408 if ((image->columns == 0) || (image->rows == 0))
409 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
410 if (max_value >= 65536)
411 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
412 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
414 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
415 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
418 Convert PNM pixels to runextent-encoded MIFF packets.
427 Convert PBM image to pixel packets.
429 for (y=0; y < (long) image->rows; y++)
437 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
438 if (q == (PixelPacket *) NULL)
440 for (x=0; x < (long) image->columns; x++)
442 q->red=(Quantum) (PNMInteger(image,2) == 0 ? QuantumRange : 0);
447 if (SyncAuthenticPixels(image,exception) == MagickFalse)
449 if (image->previous == (Image *) NULL)
451 status=SetImageProgress(image,LoadImageTag,y,image->rows);
452 if (status == MagickFalse)
456 image->type=BilevelType;
465 Convert PGM image to pixel packets.
467 scale=(Quantum *) NULL;
468 if (max_value != (1U*QuantumRange))
471 Compute pixel scaling table.
473 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
475 if (scale == (Quantum *) NULL)
476 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
477 for (i=0; i <= (long) max_value; i++)
478 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
480 for (y=0; y < (long) image->rows; y++)
488 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
489 if (q == (PixelPacket *) NULL)
491 for (x=0; x < (long) image->columns; x++)
493 intensity=PNMInteger(image,10);
494 q->red=(Quantum) intensity;
495 if (scale != (Quantum *) NULL)
496 q->red=scale[ConstrainPixel(image,(long) intensity,max_value)];
501 if (SyncAuthenticPixels(image,exception) == MagickFalse)
503 if (image->previous == (Image *) NULL)
505 status=SetImageProgress(image,LoadImageTag,y,image->rows);
506 if (status == MagickFalse)
510 image->type=GrayscaleType;
511 if (scale != (Quantum *) NULL)
512 scale=(Quantum *) RelinquishMagickMemory(scale);
521 Convert PNM image to pixel packets.
523 scale=(Quantum *) NULL;
524 if (max_value != (1U*QuantumRange))
527 Compute pixel scaling table.
529 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
531 if (scale == (Quantum *) NULL)
532 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
533 for (i=0; i <= (long) max_value; i++)
534 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
536 for (y=0; y < (long) image->rows; y++)
544 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
545 if (q == (PixelPacket *) NULL)
547 for (x=0; x < (long) image->columns; x++)
549 pixel.red=(MagickRealType) PNMInteger(image,10);
550 pixel.green=(MagickRealType) PNMInteger(image,10);
551 pixel.blue=(MagickRealType) PNMInteger(image,10);
552 if (scale != (Quantum *) NULL)
554 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(long)
555 pixel.red,max_value)];
556 pixel.green=(MagickRealType) scale[ConstrainPixel(image,(long)
557 pixel.green,max_value)];
558 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(long)
559 pixel.blue,max_value)];
561 q->red=(Quantum) pixel.red;
562 q->green=(Quantum) pixel.green;
563 q->blue=(Quantum) pixel.blue;
566 if (SyncAuthenticPixels(image,exception) == MagickFalse)
568 if (image->previous == (Image *) NULL)
570 status=SetImageProgress(image,LoadImageTag,y,image->rows);
571 if (status == MagickFalse)
575 if (scale != (Quantum *) NULL)
576 scale=(Quantum *) RelinquishMagickMemory(scale);
582 Convert PBM raw image to pixel packets.
584 quantum_type=GrayQuantum;
585 if (image->storage_class == PseudoClass)
586 quantum_type=IndexQuantum;
587 quantum_info=AcquireQuantumInfo(image_info,image);
588 if (quantum_info == (QuantumInfo *) NULL)
589 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
590 SetQuantumMinIsWhite(quantum_info,MagickTrue);
591 extent=GetQuantumExtent(image,quantum_info,quantum_type);
592 for (y=0; y < (long) image->rows; y++)
612 if (status == MagickFalse)
614 pixels=GetQuantumPixels(quantum_info);
616 count=ReadBlob(image,extent,pixels);
617 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
618 (image->previous == (Image *) NULL))
623 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
624 if (proceed == MagickFalse)
629 if (count != (ssize_t) extent)
631 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
632 if (q == (PixelPacket *) NULL)
637 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
638 quantum_type,pixels,exception);
639 if (length != extent)
641 sync=SyncAuthenticPixels(image,exception);
642 if (sync == MagickFalse)
645 quantum_info=DestroyQuantumInfo(quantum_info);
646 if (status == MagickFalse)
647 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
648 SetQuantumImageType(image,quantum_type);
657 Convert PGM raw image to pixel packets.
659 range=GetQuantumRange(image->depth);
660 quantum_type=GrayQuantum;
661 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
662 quantum_info=AcquireQuantumInfo(image_info,image);
663 if (quantum_info == (QuantumInfo *) NULL)
664 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
665 for (y=0; y < (long) image->rows; y++)
673 register const unsigned char
688 if (status == MagickFalse)
690 pixels=GetQuantumPixels(quantum_info);
692 count=ReadBlob(image,extent,pixels);
693 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
694 (image->previous == (Image *) NULL))
699 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
700 if (proceed == MagickFalse)
705 if (count != (ssize_t) extent)
707 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
708 if (q == (PixelPacket *) NULL)
714 if ((image->depth == 8) || (image->depth == 16))
715 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
716 quantum_type,pixels,exception);
718 if (image->depth <= 8)
723 for (x=0; x < (long) image->columns; x++)
725 p=PushCharPixel(p,&pixel);
726 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
737 for (x=0; x < (long) image->columns; x++)
739 p=PushShortPixel(MSBEndian,p,&pixel);
740 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
746 sync=SyncAuthenticPixels(image,exception);
747 if (sync == MagickFalse)
750 quantum_info=DestroyQuantumInfo(quantum_info);
751 if (status == MagickFalse)
752 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
753 SetQuantumImageType(image,quantum_type);
765 Convert PNM raster image to pixel packets.
768 quantum_type=RGBQuantum;
769 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
770 range=GetQuantumRange(image->depth);
771 quantum_info=AcquireQuantumInfo(image_info,image);
772 if (quantum_info == (QuantumInfo *) NULL)
773 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
774 for (y=0; y < (long) image->rows; y++)
782 register const unsigned char
797 if (status == MagickFalse)
799 pixels=GetQuantumPixels(quantum_info);
801 count=ReadBlob(image,extent,pixels);
802 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
803 (image->previous == (Image *) NULL))
808 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
809 if (proceed == MagickFalse)
814 if (count != (ssize_t) extent)
816 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
817 if (q == (PixelPacket *) NULL)
823 if (image->depth == 8)
824 for (x=0; x < (long) image->columns; x++)
826 q->red=ScaleCharToQuantum(*p++);
827 q->green=ScaleCharToQuantum(*p++);
828 q->blue=ScaleCharToQuantum(*p++);
829 q->opacity=OpaqueOpacity;
833 if (image->depth == 16)
838 for (x=0; x < (long) image->columns; x++)
840 p=PushShortPixel(MSBEndian,p,&pixel);
841 q->red=ScaleShortToQuantum(pixel);
842 p=PushShortPixel(MSBEndian,p,&pixel);
843 q->green=ScaleShortToQuantum(pixel);
844 p=PushShortPixel(MSBEndian,p,&pixel);
845 q->blue=ScaleShortToQuantum(pixel);
846 q->opacity=OpaqueOpacity;
851 if (image->depth <= 8)
856 for (x=0; x < (long) image->columns; x++)
858 p=PushCharPixel(p,&pixel);
859 q->red=ScaleAnyToQuantum(pixel,range);
860 p=PushCharPixel(p,&pixel);
861 q->green=ScaleAnyToQuantum(pixel,range);
862 p=PushCharPixel(p,&pixel);
863 q->blue=ScaleAnyToQuantum(pixel,range);
864 q->opacity=OpaqueOpacity;
873 for (x=0; x < (long) image->columns; x++)
875 p=PushShortPixel(MSBEndian,p,&pixel);
876 q->red=ScaleAnyToQuantum(pixel,range);
877 p=PushShortPixel(MSBEndian,p,&pixel);
878 q->green=ScaleAnyToQuantum(pixel,range);
879 p=PushShortPixel(MSBEndian,p,&pixel);
880 q->blue=ScaleAnyToQuantum(pixel,range);
881 q->opacity=OpaqueOpacity;
885 if ((type == BilevelType) || (type == GrayscaleType))
887 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
888 for (x=0; x < (long) image->columns; x++)
890 if ((type == BilevelType) &&
891 (IsMonochromePixel(q) == MagickFalse))
892 type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
894 if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
896 if ((type != BilevelType) && (type != GrayscaleType))
901 sync=SyncAuthenticPixels(image,exception);
902 if (sync == MagickFalse)
905 quantum_info=DestroyQuantumInfo(quantum_info);
906 if (status == MagickFalse)
907 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
908 if (type != UndefinedType)
924 Convert PAM raster image to pixel packets.
926 range=GetQuantumRange(image->depth);
927 switch (quantum_type)
930 case GrayAlphaQuantum:
947 if (image->matte != MagickFalse)
949 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
950 quantum_info=AcquireQuantumInfo(image_info,image);
951 if (quantum_info == (QuantumInfo *) NULL)
952 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
953 for (y=0; y < (long) image->rows; y++)
961 register const unsigned char
976 if (status == MagickFalse)
978 pixels=GetQuantumPixels(quantum_info);
980 count=ReadBlob(image,extent,pixels);
981 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
982 (image->previous == (Image *) NULL))
987 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
988 if (proceed == MagickFalse)
993 if (count != (ssize_t) extent)
995 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
996 if (q == (PixelPacket *) NULL)
1001 indexes=GetAuthenticIndexQueue(image);
1003 if ((image->depth == 8) || (image->depth == 16))
1004 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1005 quantum_type,pixels,exception);
1007 switch (quantum_type)
1010 case GrayAlphaQuantum:
1012 if (image->depth <= 8)
1017 for (x=0; x < (long) image->columns; x++)
1019 p=PushCharPixel(p,&pixel);
1020 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1023 SetOpacityPixelComponent(q,OpaqueOpacity);
1024 if (image->matte != MagickFalse)
1026 p=PushCharPixel(p,&pixel);
1027 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1037 for (x=0; x < (long) image->columns; x++)
1039 p=PushShortPixel(MSBEndian,p,&pixel);
1040 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1043 SetOpacityPixelComponent(q,OpaqueOpacity);
1044 if (image->matte != MagickFalse)
1046 p=PushShortPixel(MSBEndian,p,&pixel);
1047 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1057 if (image->depth <= 8)
1062 for (x=0; x < (long) image->columns; x++)
1064 p=PushCharPixel(p,&pixel);
1065 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1066 p=PushCharPixel(p,&pixel);
1067 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1068 p=PushCharPixel(p,&pixel);
1069 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1070 p=PushCharPixel(p,&pixel);
1071 indexes[x]=ScaleAnyToQuantum(pixel,range);
1072 SetOpacityPixelComponent(q,OpaqueOpacity);
1073 if (image->matte != MagickFalse)
1075 p=PushCharPixel(p,&pixel);
1076 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1086 for (x=0; x < (long) image->columns; x++)
1088 p=PushShortPixel(MSBEndian,p,&pixel);
1089 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1090 p=PushShortPixel(MSBEndian,p,&pixel);
1091 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1092 p=PushShortPixel(MSBEndian,p,&pixel);
1093 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1094 p=PushShortPixel(MSBEndian,p,&pixel);
1095 indexes[x]=ScaleAnyToQuantum(pixel,range);
1096 SetOpacityPixelComponent(q,OpaqueOpacity);
1097 if (image->matte != MagickFalse)
1099 p=PushShortPixel(MSBEndian,p,&pixel);
1100 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1109 if (image->depth <= 8)
1114 for (x=0; x < (long) image->columns; x++)
1116 p=PushCharPixel(p,&pixel);
1117 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1118 p=PushCharPixel(p,&pixel);
1119 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1120 p=PushCharPixel(p,&pixel);
1121 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1122 SetOpacityPixelComponent(q,OpaqueOpacity);
1123 if (image->matte != MagickFalse)
1125 p=PushCharPixel(p,&pixel);
1126 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1136 for (x=0; x < (long) image->columns; x++)
1138 p=PushShortPixel(MSBEndian,p,&pixel);
1139 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1140 p=PushShortPixel(MSBEndian,p,&pixel);
1141 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1142 p=PushShortPixel(MSBEndian,p,&pixel);
1143 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1144 SetOpacityPixelComponent(q,OpaqueOpacity);
1145 if (image->matte != MagickFalse)
1147 p=PushShortPixel(MSBEndian,p,&pixel);
1148 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1156 sync=SyncAuthenticPixels(image,exception);
1157 if (sync == MagickFalse)
1160 quantum_info=DestroyQuantumInfo(quantum_info);
1161 if (status == MagickFalse)
1162 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1163 SetQuantumImageType(image,quantum_type);
1170 Convert PFM raster image to pixel packets.
1172 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1173 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1175 quantum_info=AcquireQuantumInfo(image_info,image);
1176 if (quantum_info == (QuantumInfo *) NULL)
1177 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1178 status=SetQuantumDepth(image,quantum_info,32);
1179 if (status == MagickFalse)
1180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1181 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1182 if (status == MagickFalse)
1183 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1184 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1185 fabs(quantum_scale));
1186 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1187 for (y=0; y < (long) image->rows; y++)
1195 register PixelPacket
1207 if (status == MagickFalse)
1209 pixels=GetQuantumPixels(quantum_info);
1211 count=ReadBlob(image,extent,pixels);
1212 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1213 (image->previous == (Image *) NULL))
1218 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
1219 if (proceed == MagickFalse)
1224 if ((size_t) count != extent)
1226 q=QueueAuthenticPixels(image,0,(long) (image->rows-offset-1),
1227 image->columns,1,exception);
1228 if (q == (PixelPacket *) NULL)
1233 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1234 quantum_type,pixels,exception);
1235 if (length != extent)
1237 sync=SyncAuthenticPixels(image,exception);
1238 if (sync == MagickFalse)
1241 quantum_info=DestroyQuantumInfo(quantum_info);
1242 if (status == MagickFalse)
1243 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1244 SetQuantumImageType(image,quantum_type);
1248 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1250 if (EOFBlob(image) != MagickFalse)
1251 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1252 "UnexpectedEndOfFile","`%s'",image->filename);
1254 Proceed to next image.
1256 if (image_info->number_scenes != 0)
1257 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1259 if ((format == '1') || (format == '2') || (format == '3'))
1263 Skip to end of line.
1265 count=ReadBlob(image,1,(unsigned char *) &format);
1268 if ((count != 0) && (format == 'P'))
1270 } while (format != '\n');
1271 count=ReadBlob(image,1,(unsigned char *) &format);
1272 if ((count == 1) && (format == 'P'))
1275 Allocate next image structure.
1277 AcquireNextImage(image_info,image);
1278 if (GetNextImageInList(image) == (Image *) NULL)
1280 image=DestroyImageList(image);
1281 return((Image *) NULL);
1283 image=SyncNextImageInList(image);
1284 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1285 GetBlobSize(image));
1286 if (status == MagickFalse)
1289 } while ((count == 1) && (format == 'P'));
1290 (void) CloseBlob(image);
1291 return(GetFirstImageInList(image));
1295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1299 % R e g i s t e r P N M I m a g e %
1303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1305 % RegisterPNMImage() adds properties for the PNM image format to
1306 % the list of supported formats. The properties include the image format
1307 % tag, a method to read and/or write the format, whether the format
1308 % supports the saving of more than one frame to the same file or blob,
1309 % whether the format supports native in-memory I/O, and a brief
1310 % description of the format.
1312 % The format of the RegisterPNMImage method is:
1314 % unsigned long RegisterPNMImage(void)
1317 ModuleExport unsigned long RegisterPNMImage(void)
1322 entry=SetMagickInfo("PAM");
1323 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1324 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1325 entry->description=ConstantString("Common 2-dimensional bitmap format");
1326 entry->module=ConstantString("PNM");
1327 (void) RegisterMagickInfo(entry);
1328 entry=SetMagickInfo("PBM");
1329 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1330 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1331 entry->description=ConstantString("Portable bitmap format (black and white)");
1332 entry->module=ConstantString("PNM");
1333 (void) RegisterMagickInfo(entry);
1334 entry=SetMagickInfo("PFM");
1335 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1336 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1337 entry->endian_support=MagickTrue;
1338 entry->description=ConstantString("Portable float format");
1339 entry->module=ConstantString("PFM");
1340 (void) RegisterMagickInfo(entry);
1341 entry=SetMagickInfo("PGM");
1342 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1343 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1344 entry->description=ConstantString("Portable graymap format (gray scale)");
1345 entry->module=ConstantString("PNM");
1346 (void) RegisterMagickInfo(entry);
1347 entry=SetMagickInfo("PNM");
1348 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1349 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1350 entry->magick=(IsImageFormatHandler *) IsPNM;
1351 entry->description=ConstantString("Portable anymap");
1352 entry->module=ConstantString("PNM");
1353 (void) RegisterMagickInfo(entry);
1354 entry=SetMagickInfo("PPM");
1355 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1356 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1357 entry->description=ConstantString("Portable pixmap format (color)");
1358 entry->module=ConstantString("PNM");
1359 (void) RegisterMagickInfo(entry);
1360 return(MagickImageCoderSignature);
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 % U n r e g i s t e r P N M I m a g e %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % UnregisterPNMImage() removes format registrations made by the
1375 % PNM module from the list of supported formats.
1377 % The format of the UnregisterPNMImage method is:
1379 % UnregisterPNMImage(void)
1382 ModuleExport void UnregisterPNMImage(void)
1384 (void) UnregisterMagickInfo("PAM");
1385 (void) UnregisterMagickInfo("PBM");
1386 (void) UnregisterMagickInfo("PGM");
1387 (void) UnregisterMagickInfo("PNM");
1388 (void) UnregisterMagickInfo("PPM");
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % W r i t e P N M I m a g e %
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1404 % The format of the WritePNMImage method is:
1406 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1408 % A description of each parameter follows.
1410 % o image_info: the image info.
1412 % o image: The image.
1415 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1418 buffer[MaxTextExtent],
1420 magick[MaxTextExtent];
1449 register unsigned char
1461 Open output image file.
1463 assert(image_info != (const ImageInfo *) NULL);
1464 assert(image_info->signature == MagickSignature);
1465 assert(image != (Image *) NULL);
1466 assert(image->signature == MagickSignature);
1467 if (image->debug != MagickFalse)
1468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1469 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1470 if (status == MagickFalse)
1476 Write PNM file header.
1479 quantum_type=RGBQuantum;
1480 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1493 if (image_info->compression == NoCompression)
1501 if (IsGrayImage(image,&image->exception) != MagickFalse)
1509 if (image_info->compression == NoCompression)
1516 if ((image_info->type != TrueColorType) &&
1517 (IsGrayImage(image,&image->exception) != MagickFalse))
1520 if (image_info->compression == NoCompression)
1522 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1525 if (image_info->compression == NoCompression)
1534 if (image_info->compression == NoCompression)
1539 (void) FormatMagickString(buffer,MaxTextExtent,"P%c\n",format);
1540 (void) WriteBlobString(image,buffer);
1541 value=GetImageProperty(image,"comment");
1542 if (value != (const char *) NULL)
1548 Write comments to file.
1550 (void) WriteBlobByte(image,'#');
1551 for (p=value; *p != '\0'; p++)
1553 (void) WriteBlobByte(image,(unsigned char) *p);
1554 if ((*p == '\r') && (*(p+1) != '\0'))
1555 (void) WriteBlobByte(image,'#');
1556 if ((*p == '\n') && (*(p+1) != '\0'))
1557 (void) WriteBlobByte(image,'#');
1559 (void) WriteBlobByte(image,'\n');
1563 if (image->colorspace != RGBColorspace)
1564 (void) TransformImageColorspace(image,RGBColorspace);
1565 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n",
1566 image->columns,image->rows);
1567 (void) WriteBlobString(image,buffer);
1572 type[MaxTextExtent];
1577 (void) FormatMagickString(buffer,MaxTextExtent,
1578 "WIDTH %lu\nHEIGHT %lu\n",image->columns,image->rows);
1579 (void) WriteBlobString(image,buffer);
1580 quantum_type=GetQuantumType(image,&image->exception);
1581 switch (quantum_type)
1587 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1591 case GrayAlphaQuantum:
1594 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1599 quantum_type=RGBQuantum;
1600 if (image->matte != MagickFalse)
1601 quantum_type=RGBAQuantum;
1603 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1607 if (image->matte != MagickFalse)
1610 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1612 if (image->depth > 16)
1614 (void) FormatMagickString(buffer,MaxTextExtent,
1615 "DEPTH %lu\nMAXVAL %lu\n",(unsigned long) packet_size,(unsigned long)
1616 GetQuantumRange(image->depth));
1617 (void) WriteBlobString(image,buffer);
1618 (void) FormatMagickString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1620 (void) WriteBlobString(image,buffer);
1623 Convert runextent encoded to PNM raster pixels.
1633 Convert image to a PBM image.
1636 for (y=0; y < (long) image->rows; y++)
1638 register const IndexPacket
1641 register const PixelPacket
1647 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1648 if (p == (const PixelPacket *) NULL)
1650 indexes=GetVirtualIndexQueue(image);
1651 for (x=0; x < (long) image->columns; x++)
1653 pixel=PixelIntensityToQuantum(p);
1654 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1657 if ((q-pixels+2) >= 80)
1660 (void) WriteBlob(image,q-pixels,pixels);
1666 if (image->previous == (Image *) NULL)
1668 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1669 if (status == MagickFalse)
1676 (void) WriteBlob(image,q-pixels,pixels);
1686 Convert image to a PGM image.
1688 if (image->depth <= 8)
1689 (void) WriteBlobString(image,"255\n");
1691 (void) WriteBlobString(image,"65535\n");
1693 for (y=0; y < (long) image->rows; y++)
1695 register const PixelPacket
1701 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1702 if (p == (const PixelPacket *) NULL)
1704 for (x=0; x < (long) image->columns; x++)
1706 index=PixelIntensityToQuantum(p);
1707 if (image->depth <= 8)
1708 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1709 ScaleQuantumToChar(index));
1711 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1712 ScaleQuantumToShort(index));
1713 extent=(size_t) count;
1714 (void) strncpy((char *) q,buffer,extent);
1716 if ((q-pixels+extent) >= 80)
1719 (void) WriteBlob(image,q-pixels,pixels);
1724 if (image->previous == (Image *) NULL)
1726 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1727 if (status == MagickFalse)
1734 (void) WriteBlob(image,q-pixels,pixels);
1744 Convert image to a PNM image.
1746 if (image->depth <= 8)
1747 (void) WriteBlobString(image,"255\n");
1749 (void) WriteBlobString(image,"65535\n");
1751 for (y=0; y < (long) image->rows; y++)
1753 register const PixelPacket
1759 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1760 if (p == (const PixelPacket *) NULL)
1762 for (x=0; x < (long) image->columns; x++)
1764 if (image->depth <= 8)
1765 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1766 "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
1767 ScaleQuantumToChar(GetGreenPixelComponent(p)),
1768 ScaleQuantumToChar(GetBluePixelComponent(p)));
1770 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1771 "%u %u %u ",ScaleQuantumToShort(GetRedPixelComponent(p)),
1772 ScaleQuantumToShort(GetGreenPixelComponent(p)),
1773 ScaleQuantumToShort(GetBluePixelComponent(p)));
1774 extent=(size_t) count;
1775 (void) strncpy((char *) q,buffer,extent);
1777 if ((q-pixels+extent) >= 80)
1780 (void) WriteBlob(image,q-pixels,pixels);
1785 if (image->previous == (Image *) NULL)
1787 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1788 if (status == MagickFalse)
1795 (void) WriteBlob(image,q-pixels,pixels);
1802 Convert image to a PBM image.
1805 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1806 if (quantum_info == (QuantumInfo *) NULL)
1807 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1808 quantum_info->min_is_white=MagickTrue;
1809 pixels=GetQuantumPixels(quantum_info);
1810 for (y=0; y < (long) image->rows; y++)
1812 register const PixelPacket
1815 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1816 if (p == (const PixelPacket *) NULL)
1818 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1819 quantum_info,GrayQuantum,pixels,&image->exception);
1820 count=WriteBlob(image,extent,pixels);
1821 if (count != (ssize_t) extent)
1823 if (image->previous == (Image *) NULL)
1825 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1826 if (status == MagickFalse)
1830 quantum_info=DestroyQuantumInfo(quantum_info);
1839 Convert image to a PGM image.
1841 if (image->depth > 8)
1843 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
1844 GetQuantumRange(image->depth));
1845 (void) WriteBlobString(image,buffer);
1846 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1847 if (quantum_info == (QuantumInfo *) NULL)
1848 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1849 quantum_info->min_is_white=MagickTrue;
1850 pixels=GetQuantumPixels(quantum_info);
1851 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1852 range=GetQuantumRange(image->depth);
1853 for (y=0; y < (long) image->rows; y++)
1855 register const PixelPacket
1861 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1862 if (p == (const PixelPacket *) NULL)
1865 if ((image->depth == 8) || (image->depth == 16))
1866 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1867 quantum_info,GrayQuantum,pixels,&image->exception);
1870 if (image->depth <= 8)
1871 for (x=0; x < (long) image->columns; x++)
1873 if (IsGrayPixel(p) == MagickFalse)
1874 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1877 if (image->depth == 8)
1878 pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
1880 pixel=ScaleQuantumToAny(p->red,range);
1882 q=PopCharPixel((unsigned char) pixel,q);
1886 for (x=0; x < (long) image->columns; x++)
1888 if (IsGrayPixel(p) == MagickFalse)
1889 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1892 if (image->depth == 16)
1893 pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
1895 pixel=ScaleQuantumToAny(p->red,range);
1897 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1900 extent=(size_t) (q-pixels);
1902 count=WriteBlob(image,extent,pixels);
1903 if (count != (ssize_t) extent)
1905 if (image->previous == (Image *) NULL)
1907 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1908 if (status == MagickFalse)
1912 quantum_info=DestroyQuantumInfo(quantum_info);
1921 Convert image to a PNM image.
1923 if (image->depth > 8)
1925 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
1926 GetQuantumRange(image->depth));
1927 (void) WriteBlobString(image,buffer);
1928 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1929 if (quantum_info == (QuantumInfo *) NULL)
1930 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1931 pixels=GetQuantumPixels(quantum_info);
1932 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1933 range=GetQuantumRange(image->depth);
1934 for (y=0; y < (long) image->rows; y++)
1936 register const PixelPacket
1942 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1943 if (p == (const PixelPacket *) NULL)
1946 if ((image->depth == 8) || (image->depth == 16))
1947 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1948 quantum_info,quantum_type,pixels,&image->exception);
1951 if (image->depth <= 8)
1952 for (x=0; x < (long) image->columns; x++)
1954 pixel=ScaleQuantumToAny(p->red,range);
1955 q=PopCharPixel((unsigned char) pixel,q);
1956 pixel=ScaleQuantumToAny(p->green,range);
1957 q=PopCharPixel((unsigned char) pixel,q);
1958 pixel=ScaleQuantumToAny(p->blue,range);
1959 q=PopCharPixel((unsigned char) pixel,q);
1963 for (x=0; x < (long) image->columns; x++)
1965 pixel=ScaleQuantumToAny(p->red,range);
1966 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1967 pixel=ScaleQuantumToAny(p->green,range);
1968 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1969 pixel=ScaleQuantumToAny(p->blue,range);
1970 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1973 extent=(size_t) (q-pixels);
1975 count=WriteBlob(image,extent,pixels);
1976 if (count != (ssize_t) extent)
1978 if (image->previous == (Image *) NULL)
1980 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1981 if (status == MagickFalse)
1985 quantum_info=DestroyQuantumInfo(quantum_info);
1994 Convert image to a PAM.
1996 if (image->depth > 16)
1998 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1999 pixels=GetQuantumPixels(quantum_info);
2000 range=GetQuantumRange(image->depth);
2001 for (y=0; y < (long) image->rows; y++)
2003 register const IndexPacket
2006 register const PixelPacket
2012 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2013 if (p == (const PixelPacket *) NULL)
2015 indexes=GetVirtualIndexQueue(image);
2017 if ((image->depth == 8) || (image->depth == 16))
2018 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2019 quantum_info,quantum_type,pixels,&image->exception);
2022 switch (quantum_type)
2025 case GrayAlphaQuantum:
2027 if (image->depth <= 8)
2028 for (x=0; x < (long) image->columns; x++)
2030 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2031 q=PopCharPixel((unsigned char) pixel,q);
2032 if (image->matte != MagickFalse)
2034 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2036 q=PopCharPixel((unsigned char) pixel,q);
2041 for (x=0; x < (long) image->columns; x++)
2043 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2044 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2045 if (image->matte != MagickFalse)
2047 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2049 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2058 if (image->depth <= 8)
2059 for (x=0; x < (long) image->columns; x++)
2061 pixel=ScaleQuantumToAny(p->red,range);
2062 q=PopCharPixel((unsigned char) pixel,q);
2063 pixel=ScaleQuantumToAny(p->green,range);
2064 q=PopCharPixel((unsigned char) pixel,q);
2065 pixel=ScaleQuantumToAny(p->blue,range);
2066 q=PopCharPixel((unsigned char) pixel,q);
2067 pixel=ScaleQuantumToAny(indexes[x],range);
2068 q=PopCharPixel((unsigned char) pixel,q);
2069 if (image->matte != MagickFalse)
2071 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2072 GetOpacityPixelComponent(p)),range);
2073 q=PopCharPixel((unsigned char) pixel,q);
2078 for (x=0; x < (long) image->columns; x++)
2080 pixel=ScaleQuantumToAny(p->red,range);
2081 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2082 pixel=ScaleQuantumToAny(p->green,range);
2083 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2084 pixel=ScaleQuantumToAny(p->blue,range);
2085 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2086 pixel=ScaleQuantumToAny(indexes[x],range);
2087 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2088 if (image->matte != MagickFalse)
2090 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2091 GetOpacityPixelComponent(p)),range);
2092 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2100 if (image->depth <= 8)
2101 for (x=0; x < (long) image->columns; x++)
2103 pixel=ScaleQuantumToAny(p->red,range);
2104 q=PopCharPixel((unsigned char) pixel,q);
2105 pixel=ScaleQuantumToAny(p->green,range);
2106 q=PopCharPixel((unsigned char) pixel,q);
2107 pixel=ScaleQuantumToAny(p->blue,range);
2108 q=PopCharPixel((unsigned char) pixel,q);
2109 if (image->matte != MagickFalse)
2111 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2112 GetOpacityPixelComponent(p)),range);
2113 q=PopCharPixel((unsigned char) pixel,q);
2118 for (x=0; x < (long) image->columns; x++)
2120 pixel=ScaleQuantumToAny(p->red,range);
2121 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2122 pixel=ScaleQuantumToAny(p->green,range);
2123 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2124 pixel=ScaleQuantumToAny(p->blue,range);
2125 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2126 if (image->matte != MagickFalse)
2128 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2129 GetOpacityPixelComponent(p)),range);
2130 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2137 extent=(size_t) (q-pixels);
2139 count=WriteBlob(image,extent,pixels);
2140 if (count != (ssize_t) extent)
2142 if (image->previous == (Image *) NULL)
2144 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2145 if (status == MagickFalse)
2149 quantum_info=DestroyQuantumInfo(quantum_info);
2155 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2158 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2159 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2160 if (quantum_info == (QuantumInfo *) NULL)
2161 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2162 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2163 if (status == MagickFalse)
2164 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2165 pixels=GetQuantumPixels(quantum_info);
2166 for (y=(long) image->rows-1; y >= 0; y--)
2168 register const PixelPacket
2171 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2172 if (p == (const PixelPacket *) NULL)
2174 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2175 quantum_info,quantum_type,pixels,&image->exception);
2176 (void) WriteBlob(image,extent,pixels);
2177 if (image->previous == (Image *) NULL)
2179 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2180 if (status == MagickFalse)
2184 quantum_info=DestroyQuantumInfo(quantum_info);
2188 if (GetNextImageInList(image) == (Image *) NULL)
2190 image=SyncNextImageInList(image);
2191 status=SetImageProgress(image,SaveImagesTag,scene++,
2192 GetImageListLength(image));
2193 if (status == MagickFalse)
2195 } while (image_info->adjoin != MagickFalse);
2196 (void) CloseBlob(image);