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 ssize_t ConstrainPixel(Image *image,const ssize_t offset,
140 if ((offset < 0) || (offset > (ssize_t) extent))
142 (void) ThrowMagickException(&image->exception,GetMagickModule(),
143 CorruptImageError,"InvalidPixel","`%s'",image->filename);
149 static size_t 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((size_t) (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 < (ssize_t) image->rows; y++)
437 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
438 if (q == (PixelPacket *) NULL)
440 for (x=0; x < (ssize_t) 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,(MagickOffsetType) y,
453 if (status == MagickFalse)
457 image->type=BilevelType;
466 Convert PGM image to pixel packets.
468 scale=(Quantum *) NULL;
469 if (max_value != (1U*QuantumRange))
472 Compute pixel scaling table.
474 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
476 if (scale == (Quantum *) NULL)
477 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
478 for (i=0; i <= (ssize_t) max_value; i++)
479 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
481 for (y=0; y < (ssize_t) image->rows; y++)
489 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
490 if (q == (PixelPacket *) NULL)
492 for (x=0; x < (ssize_t) image->columns; x++)
494 intensity=PNMInteger(image,10);
495 q->red=(Quantum) intensity;
496 if (scale != (Quantum *) NULL)
497 q->red=scale[ConstrainPixel(image,(ssize_t) intensity,max_value)];
502 if (SyncAuthenticPixels(image,exception) == MagickFalse)
504 if (image->previous == (Image *) NULL)
506 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
508 if (status == MagickFalse)
512 image->type=GrayscaleType;
513 if (scale != (Quantum *) NULL)
514 scale=(Quantum *) RelinquishMagickMemory(scale);
523 Convert PNM image to pixel packets.
525 scale=(Quantum *) NULL;
526 if (max_value != (1U*QuantumRange))
529 Compute pixel scaling table.
531 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
533 if (scale == (Quantum *) NULL)
534 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
535 for (i=0; i <= (ssize_t) max_value; i++)
536 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
538 for (y=0; y < (ssize_t) image->rows; y++)
546 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
547 if (q == (PixelPacket *) NULL)
549 for (x=0; x < (ssize_t) image->columns; x++)
551 pixel.red=(MagickRealType) PNMInteger(image,10);
552 pixel.green=(MagickRealType) PNMInteger(image,10);
553 pixel.blue=(MagickRealType) PNMInteger(image,10);
554 if (scale != (Quantum *) NULL)
556 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
557 pixel.red,max_value)];
558 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
559 (ssize_t) pixel.green,max_value)];
560 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
561 pixel.blue,max_value)];
563 q->red=(Quantum) pixel.red;
564 q->green=(Quantum) pixel.green;
565 q->blue=(Quantum) pixel.blue;
568 if (SyncAuthenticPixels(image,exception) == MagickFalse)
570 if (image->previous == (Image *) NULL)
572 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
574 if (status == MagickFalse)
578 if (scale != (Quantum *) NULL)
579 scale=(Quantum *) RelinquishMagickMemory(scale);
585 Convert PBM raw image to pixel packets.
587 quantum_type=GrayQuantum;
588 if (image->storage_class == PseudoClass)
589 quantum_type=IndexQuantum;
590 quantum_info=AcquireQuantumInfo(image_info,image);
591 if (quantum_info == (QuantumInfo *) NULL)
592 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
593 SetQuantumMinIsWhite(quantum_info,MagickTrue);
594 extent=GetQuantumExtent(image,quantum_info,quantum_type);
595 for (y=0; y < (ssize_t) image->rows; y++)
615 if (status == MagickFalse)
617 pixels=GetQuantumPixels(quantum_info);
619 count=ReadBlob(image,extent,pixels);
620 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
621 (image->previous == (Image *) NULL))
626 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
628 if (proceed == MagickFalse)
633 if (count != (ssize_t) extent)
635 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
636 if (q == (PixelPacket *) NULL)
641 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
642 quantum_type,pixels,exception);
643 if (length != extent)
645 sync=SyncAuthenticPixels(image,exception);
646 if (sync == MagickFalse)
649 quantum_info=DestroyQuantumInfo(quantum_info);
650 if (status == MagickFalse)
651 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
652 SetQuantumImageType(image,quantum_type);
661 Convert PGM raw image to pixel packets.
663 range=GetQuantumRange(image->depth);
664 quantum_type=GrayQuantum;
665 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
666 quantum_info=AcquireQuantumInfo(image_info,image);
667 if (quantum_info == (QuantumInfo *) NULL)
668 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
669 for (y=0; y < (ssize_t) image->rows; y++)
677 register const unsigned char
692 if (status == MagickFalse)
694 pixels=GetQuantumPixels(quantum_info);
696 count=ReadBlob(image,extent,pixels);
697 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
698 (image->previous == (Image *) NULL))
703 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
705 if (proceed == MagickFalse)
710 if (count != (ssize_t) extent)
712 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
713 if (q == (PixelPacket *) NULL)
719 if ((image->depth == 8) || (image->depth == 16))
720 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
721 quantum_type,pixels,exception);
723 if (image->depth <= 8)
728 for (x=0; x < (ssize_t) image->columns; x++)
730 p=PushCharPixel(p,&pixel);
731 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
742 for (x=0; x < (ssize_t) image->columns; x++)
744 p=PushShortPixel(MSBEndian,p,&pixel);
745 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
751 sync=SyncAuthenticPixels(image,exception);
752 if (sync == MagickFalse)
755 quantum_info=DestroyQuantumInfo(quantum_info);
756 if (status == MagickFalse)
757 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
758 SetQuantumImageType(image,quantum_type);
770 Convert PNM raster image to pixel packets.
773 quantum_type=RGBQuantum;
774 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
775 range=GetQuantumRange(image->depth);
776 quantum_info=AcquireQuantumInfo(image_info,image);
777 if (quantum_info == (QuantumInfo *) NULL)
778 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
779 for (y=0; y < (ssize_t) image->rows; y++)
787 register const unsigned char
802 if (status == MagickFalse)
804 pixels=GetQuantumPixels(quantum_info);
806 count=ReadBlob(image,extent,pixels);
807 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
808 (image->previous == (Image *) NULL))
813 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
815 if (proceed == MagickFalse)
820 if (count != (ssize_t) extent)
822 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
823 if (q == (PixelPacket *) NULL)
829 if (image->depth == 8)
830 for (x=0; x < (ssize_t) image->columns; x++)
832 q->red=ScaleCharToQuantum(*p++);
833 q->green=ScaleCharToQuantum(*p++);
834 q->blue=ScaleCharToQuantum(*p++);
835 q->opacity=OpaqueOpacity;
839 if (image->depth == 16)
844 for (x=0; x < (ssize_t) image->columns; x++)
846 p=PushShortPixel(MSBEndian,p,&pixel);
847 q->red=ScaleShortToQuantum(pixel);
848 p=PushShortPixel(MSBEndian,p,&pixel);
849 q->green=ScaleShortToQuantum(pixel);
850 p=PushShortPixel(MSBEndian,p,&pixel);
851 q->blue=ScaleShortToQuantum(pixel);
852 q->opacity=OpaqueOpacity;
857 if (image->depth <= 8)
862 for (x=0; x < (ssize_t) image->columns; x++)
864 p=PushCharPixel(p,&pixel);
865 q->red=ScaleAnyToQuantum(pixel,range);
866 p=PushCharPixel(p,&pixel);
867 q->green=ScaleAnyToQuantum(pixel,range);
868 p=PushCharPixel(p,&pixel);
869 q->blue=ScaleAnyToQuantum(pixel,range);
870 q->opacity=OpaqueOpacity;
879 for (x=0; x < (ssize_t) image->columns; x++)
881 p=PushShortPixel(MSBEndian,p,&pixel);
882 q->red=ScaleAnyToQuantum(pixel,range);
883 p=PushShortPixel(MSBEndian,p,&pixel);
884 q->green=ScaleAnyToQuantum(pixel,range);
885 p=PushShortPixel(MSBEndian,p,&pixel);
886 q->blue=ScaleAnyToQuantum(pixel,range);
887 q->opacity=OpaqueOpacity;
891 if ((type == BilevelType) || (type == GrayscaleType))
893 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
894 for (x=0; x < (ssize_t) image->columns; x++)
896 if ((type == BilevelType) &&
897 (IsMonochromePixel(q) == MagickFalse))
898 type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
900 if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
902 if ((type != BilevelType) && (type != GrayscaleType))
907 sync=SyncAuthenticPixels(image,exception);
908 if (sync == MagickFalse)
911 quantum_info=DestroyQuantumInfo(quantum_info);
912 if (status == MagickFalse)
913 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
914 if (type != UndefinedType)
930 Convert PAM raster image to pixel packets.
932 range=GetQuantumRange(image->depth);
933 switch (quantum_type)
936 case GrayAlphaQuantum:
953 if (image->matte != MagickFalse)
955 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
956 quantum_info=AcquireQuantumInfo(image_info,image);
957 if (quantum_info == (QuantumInfo *) NULL)
958 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
959 for (y=0; y < (ssize_t) image->rows; y++)
967 register const unsigned char
982 if (status == MagickFalse)
984 pixels=GetQuantumPixels(quantum_info);
986 count=ReadBlob(image,extent,pixels);
987 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
988 (image->previous == (Image *) NULL))
993 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
995 if (proceed == MagickFalse)
1000 if (count != (ssize_t) extent)
1002 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1003 if (q == (PixelPacket *) NULL)
1008 indexes=GetAuthenticIndexQueue(image);
1010 if ((image->depth == 8) || (image->depth == 16))
1011 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1012 quantum_type,pixels,exception);
1014 switch (quantum_type)
1017 case GrayAlphaQuantum:
1019 if (image->depth <= 8)
1024 for (x=0; x < (ssize_t) image->columns; x++)
1026 p=PushCharPixel(p,&pixel);
1027 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1030 SetOpacityPixelComponent(q,OpaqueOpacity);
1031 if (image->matte != MagickFalse)
1033 p=PushCharPixel(p,&pixel);
1034 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1045 for (x=0; x < (ssize_t) image->columns; x++)
1047 p=PushShortPixel(MSBEndian,p,&pixel);
1048 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1051 SetOpacityPixelComponent(q,OpaqueOpacity);
1052 if (image->matte != MagickFalse)
1054 p=PushShortPixel(MSBEndian,p,&pixel);
1055 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1066 if (image->depth <= 8)
1071 for (x=0; x < (ssize_t) image->columns; x++)
1073 p=PushCharPixel(p,&pixel);
1074 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1075 p=PushCharPixel(p,&pixel);
1076 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1077 p=PushCharPixel(p,&pixel);
1078 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1079 p=PushCharPixel(p,&pixel);
1080 indexes[x]=ScaleAnyToQuantum(pixel,range);
1081 SetOpacityPixelComponent(q,OpaqueOpacity);
1082 if (image->matte != MagickFalse)
1084 p=PushCharPixel(p,&pixel);
1085 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1096 for (x=0; x < (ssize_t) image->columns; x++)
1098 p=PushShortPixel(MSBEndian,p,&pixel);
1099 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1100 p=PushShortPixel(MSBEndian,p,&pixel);
1101 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1102 p=PushShortPixel(MSBEndian,p,&pixel);
1103 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1104 p=PushShortPixel(MSBEndian,p,&pixel);
1105 indexes[x]=ScaleAnyToQuantum(pixel,range);
1106 SetOpacityPixelComponent(q,OpaqueOpacity);
1107 if (image->matte != MagickFalse)
1109 p=PushShortPixel(MSBEndian,p,&pixel);
1110 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1120 if (image->depth <= 8)
1125 for (x=0; x < (ssize_t) image->columns; x++)
1127 p=PushCharPixel(p,&pixel);
1128 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1129 p=PushCharPixel(p,&pixel);
1130 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1131 p=PushCharPixel(p,&pixel);
1132 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1133 SetOpacityPixelComponent(q,OpaqueOpacity);
1134 if (image->matte != MagickFalse)
1136 p=PushCharPixel(p,&pixel);
1137 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1148 for (x=0; x < (ssize_t) image->columns; x++)
1150 p=PushShortPixel(MSBEndian,p,&pixel);
1151 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1152 p=PushShortPixel(MSBEndian,p,&pixel);
1153 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1154 p=PushShortPixel(MSBEndian,p,&pixel);
1155 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1156 SetOpacityPixelComponent(q,OpaqueOpacity);
1157 if (image->matte != MagickFalse)
1159 p=PushShortPixel(MSBEndian,p,&pixel);
1160 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1169 sync=SyncAuthenticPixels(image,exception);
1170 if (sync == MagickFalse)
1173 quantum_info=DestroyQuantumInfo(quantum_info);
1174 if (status == MagickFalse)
1175 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1176 SetQuantumImageType(image,quantum_type);
1183 Convert PFM raster image to pixel packets.
1185 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1186 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1188 quantum_info=AcquireQuantumInfo(image_info,image);
1189 if (quantum_info == (QuantumInfo *) NULL)
1190 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1191 status=SetQuantumDepth(image,quantum_info,32);
1192 if (status == MagickFalse)
1193 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1194 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1195 if (status == MagickFalse)
1196 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1197 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1198 fabs(quantum_scale));
1199 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1200 for (y=0; y < (ssize_t) image->rows; y++)
1208 register PixelPacket
1220 if (status == MagickFalse)
1222 pixels=GetQuantumPixels(quantum_info);
1224 count=ReadBlob(image,extent,pixels);
1225 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1226 (image->previous == (Image *) NULL))
1231 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1233 if (proceed == MagickFalse)
1238 if ((size_t) count != extent)
1240 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1241 image->columns,1,exception);
1242 if (q == (PixelPacket *) NULL)
1247 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1248 quantum_type,pixels,exception);
1249 if (length != extent)
1251 sync=SyncAuthenticPixels(image,exception);
1252 if (sync == MagickFalse)
1255 quantum_info=DestroyQuantumInfo(quantum_info);
1256 if (status == MagickFalse)
1257 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1258 SetQuantumImageType(image,quantum_type);
1262 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1264 if (EOFBlob(image) != MagickFalse)
1265 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1266 "UnexpectedEndOfFile","`%s'",image->filename);
1268 Proceed to next image.
1270 if (image_info->number_scenes != 0)
1271 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1273 if ((format == '1') || (format == '2') || (format == '3'))
1277 Skip to end of line.
1279 count=ReadBlob(image,1,(unsigned char *) &format);
1282 if ((count != 0) && (format == 'P'))
1284 } while (format != '\n');
1285 count=ReadBlob(image,1,(unsigned char *) &format);
1286 if ((count == 1) && (format == 'P'))
1289 Allocate next image structure.
1291 AcquireNextImage(image_info,image);
1292 if (GetNextImageInList(image) == (Image *) NULL)
1294 image=DestroyImageList(image);
1295 return((Image *) NULL);
1297 image=SyncNextImageInList(image);
1298 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1299 GetBlobSize(image));
1300 if (status == MagickFalse)
1303 } while ((count == 1) && (format == 'P'));
1304 (void) CloseBlob(image);
1305 return(GetFirstImageInList(image));
1309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1313 % R e g i s t e r P N M I m a g e %
1317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1319 % RegisterPNMImage() adds properties for the PNM image format to
1320 % the list of supported formats. The properties include the image format
1321 % tag, a method to read and/or write the format, whether the format
1322 % supports the saving of more than one frame to the same file or blob,
1323 % whether the format supports native in-memory I/O, and a brief
1324 % description of the format.
1326 % The format of the RegisterPNMImage method is:
1328 % size_t RegisterPNMImage(void)
1331 ModuleExport size_t RegisterPNMImage(void)
1336 entry=SetMagickInfo("PAM");
1337 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1338 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1339 entry->description=ConstantString("Common 2-dimensional bitmap format");
1340 entry->module=ConstantString("PNM");
1341 (void) RegisterMagickInfo(entry);
1342 entry=SetMagickInfo("PBM");
1343 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1344 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1345 entry->description=ConstantString("Portable bitmap format (black and white)");
1346 entry->module=ConstantString("PNM");
1347 (void) RegisterMagickInfo(entry);
1348 entry=SetMagickInfo("PFM");
1349 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1350 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1351 entry->endian_support=MagickTrue;
1352 entry->description=ConstantString("Portable float format");
1353 entry->module=ConstantString("PFM");
1354 (void) RegisterMagickInfo(entry);
1355 entry=SetMagickInfo("PGM");
1356 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1357 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1358 entry->description=ConstantString("Portable graymap format (gray scale)");
1359 entry->module=ConstantString("PNM");
1360 (void) RegisterMagickInfo(entry);
1361 entry=SetMagickInfo("PNM");
1362 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1363 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1364 entry->magick=(IsImageFormatHandler *) IsPNM;
1365 entry->description=ConstantString("Portable anymap");
1366 entry->module=ConstantString("PNM");
1367 (void) RegisterMagickInfo(entry);
1368 entry=SetMagickInfo("PPM");
1369 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1370 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1371 entry->description=ConstantString("Portable pixmap format (color)");
1372 entry->module=ConstantString("PNM");
1373 (void) RegisterMagickInfo(entry);
1374 return(MagickImageCoderSignature);
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % U n r e g i s t e r P N M I m a g e %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % UnregisterPNMImage() removes format registrations made by the
1389 % PNM module from the list of supported formats.
1391 % The format of the UnregisterPNMImage method is:
1393 % UnregisterPNMImage(void)
1396 ModuleExport void UnregisterPNMImage(void)
1398 (void) UnregisterMagickInfo("PAM");
1399 (void) UnregisterMagickInfo("PBM");
1400 (void) UnregisterMagickInfo("PGM");
1401 (void) UnregisterMagickInfo("PNM");
1402 (void) UnregisterMagickInfo("PPM");
1406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % W r i t e P N M I m a g e %
1414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1418 % The format of the WritePNMImage method is:
1420 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1422 % A description of each parameter follows.
1424 % o image_info: the image info.
1426 % o image: The image.
1429 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1432 buffer[MaxTextExtent],
1434 magick[MaxTextExtent];
1463 register unsigned char
1475 Open output image file.
1477 assert(image_info != (const ImageInfo *) NULL);
1478 assert(image_info->signature == MagickSignature);
1479 assert(image != (Image *) NULL);
1480 assert(image->signature == MagickSignature);
1481 if (image->debug != MagickFalse)
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1483 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1484 if (status == MagickFalse)
1490 Write PNM file header.
1493 quantum_type=RGBQuantum;
1494 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1507 if (image_info->compression == NoCompression)
1515 if (IsGrayImage(image,&image->exception) != MagickFalse)
1523 if (image_info->compression == NoCompression)
1530 if ((image_info->type != TrueColorType) &&
1531 (IsGrayImage(image,&image->exception) != MagickFalse))
1534 if (image_info->compression == NoCompression)
1536 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1539 if (image_info->compression == NoCompression)
1548 if (image_info->compression == NoCompression)
1553 (void) FormatMagickString(buffer,MaxTextExtent,"P%c\n",format);
1554 (void) WriteBlobString(image,buffer);
1555 value=GetImageProperty(image,"comment");
1556 if (value != (const char *) NULL)
1562 Write comments to file.
1564 (void) WriteBlobByte(image,'#');
1565 for (p=value; *p != '\0'; p++)
1567 (void) WriteBlobByte(image,(unsigned char) *p);
1568 if ((*p == '\r') && (*(p+1) != '\0'))
1569 (void) WriteBlobByte(image,'#');
1570 if ((*p == '\n') && (*(p+1) != '\0'))
1571 (void) WriteBlobByte(image,'#');
1573 (void) WriteBlobByte(image,'\n');
1577 if (image->colorspace != RGBColorspace)
1578 (void) TransformImageColorspace(image,RGBColorspace);
1579 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n",
1580 (double) image->columns,(double) image->rows);
1581 (void) WriteBlobString(image,buffer);
1586 type[MaxTextExtent];
1591 (void) FormatMagickString(buffer,MaxTextExtent,
1592 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1594 (void) WriteBlobString(image,buffer);
1595 quantum_type=GetQuantumType(image,&image->exception);
1596 switch (quantum_type)
1602 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1606 case GrayAlphaQuantum:
1609 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1614 quantum_type=RGBQuantum;
1615 if (image->matte != MagickFalse)
1616 quantum_type=RGBAQuantum;
1618 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1622 if (image->matte != MagickFalse)
1625 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1627 if (image->depth > 16)
1629 (void) FormatMagickString(buffer,MaxTextExtent,
1630 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1631 GetQuantumRange(image->depth));
1632 (void) WriteBlobString(image,buffer);
1633 (void) FormatMagickString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1635 (void) WriteBlobString(image,buffer);
1638 Convert runextent encoded to PNM raster pixels.
1648 Convert image to a PBM image.
1651 for (y=0; y < (ssize_t) image->rows; y++)
1653 register const IndexPacket
1656 register const PixelPacket
1662 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1663 if (p == (const PixelPacket *) NULL)
1665 indexes=GetVirtualIndexQueue(image);
1666 for (x=0; x < (ssize_t) image->columns; x++)
1668 pixel=PixelIntensityToQuantum(p);
1669 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1672 if ((q-pixels+2) >= 80)
1675 (void) WriteBlob(image,q-pixels,pixels);
1681 if (image->previous == (Image *) NULL)
1683 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1685 if (status == MagickFalse)
1692 (void) WriteBlob(image,q-pixels,pixels);
1702 Convert image to a PGM image.
1704 if (image->depth <= 8)
1705 (void) WriteBlobString(image,"255\n");
1707 (void) WriteBlobString(image,"65535\n");
1709 for (y=0; y < (ssize_t) image->rows; y++)
1711 register const PixelPacket
1717 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1718 if (p == (const PixelPacket *) NULL)
1720 for (x=0; x < (ssize_t) image->columns; x++)
1722 index=PixelIntensityToQuantum(p);
1723 if (image->depth <= 8)
1724 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1725 ScaleQuantumToChar(index));
1727 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1728 ScaleQuantumToShort(index));
1729 extent=(size_t) count;
1730 (void) strncpy((char *) q,buffer,extent);
1732 if ((q-pixels+extent) >= 80)
1735 (void) WriteBlob(image,q-pixels,pixels);
1740 if (image->previous == (Image *) NULL)
1742 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1744 if (status == MagickFalse)
1751 (void) WriteBlob(image,q-pixels,pixels);
1761 Convert image to a PNM image.
1763 if (image->depth <= 8)
1764 (void) WriteBlobString(image,"255\n");
1766 (void) WriteBlobString(image,"65535\n");
1768 for (y=0; y < (ssize_t) image->rows; y++)
1770 register const PixelPacket
1776 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1777 if (p == (const PixelPacket *) NULL)
1779 for (x=0; x < (ssize_t) image->columns; x++)
1781 if (image->depth <= 8)
1782 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1783 "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
1784 ScaleQuantumToChar(GetGreenPixelComponent(p)),
1785 ScaleQuantumToChar(GetBluePixelComponent(p)));
1787 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1788 "%u %u %u ",ScaleQuantumToShort(GetRedPixelComponent(p)),
1789 ScaleQuantumToShort(GetGreenPixelComponent(p)),
1790 ScaleQuantumToShort(GetBluePixelComponent(p)));
1791 extent=(size_t) count;
1792 (void) strncpy((char *) q,buffer,extent);
1794 if ((q-pixels+extent) >= 80)
1797 (void) WriteBlob(image,q-pixels,pixels);
1802 if (image->previous == (Image *) NULL)
1804 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1806 if (status == MagickFalse)
1813 (void) WriteBlob(image,q-pixels,pixels);
1820 Convert image to a PBM image.
1823 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1824 if (quantum_info == (QuantumInfo *) NULL)
1825 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1826 quantum_info->min_is_white=MagickTrue;
1827 pixels=GetQuantumPixels(quantum_info);
1828 for (y=0; y < (ssize_t) image->rows; y++)
1830 register const PixelPacket
1833 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1834 if (p == (const PixelPacket *) NULL)
1836 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1837 quantum_info,GrayQuantum,pixels,&image->exception);
1838 count=WriteBlob(image,extent,pixels);
1839 if (count != (ssize_t) extent)
1841 if (image->previous == (Image *) NULL)
1843 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1845 if (status == MagickFalse)
1849 quantum_info=DestroyQuantumInfo(quantum_info);
1858 Convert image to a PGM image.
1860 if (image->depth > 8)
1862 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
1863 GetQuantumRange(image->depth));
1864 (void) WriteBlobString(image,buffer);
1865 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1866 if (quantum_info == (QuantumInfo *) NULL)
1867 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1868 quantum_info->min_is_white=MagickTrue;
1869 pixels=GetQuantumPixels(quantum_info);
1870 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1871 range=GetQuantumRange(image->depth);
1872 for (y=0; y < (ssize_t) image->rows; y++)
1874 register const PixelPacket
1880 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1881 if (p == (const PixelPacket *) NULL)
1884 if ((image->depth == 8) || (image->depth == 16))
1885 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1886 quantum_info,GrayQuantum,pixels,&image->exception);
1889 if (image->depth <= 8)
1890 for (x=0; x < (ssize_t) image->columns; x++)
1892 if (IsGrayPixel(p) == MagickFalse)
1893 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1896 if (image->depth == 8)
1897 pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
1899 pixel=ScaleQuantumToAny(p->red,range);
1901 q=PopCharPixel((unsigned char) pixel,q);
1905 for (x=0; x < (ssize_t) image->columns; x++)
1907 if (IsGrayPixel(p) == MagickFalse)
1908 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1911 if (image->depth == 16)
1912 pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
1914 pixel=ScaleQuantumToAny(p->red,range);
1916 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1919 extent=(size_t) (q-pixels);
1921 count=WriteBlob(image,extent,pixels);
1922 if (count != (ssize_t) extent)
1924 if (image->previous == (Image *) NULL)
1926 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1928 if (status == MagickFalse)
1932 quantum_info=DestroyQuantumInfo(quantum_info);
1941 Convert image to a PNM image.
1943 if (image->depth > 8)
1945 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
1946 GetQuantumRange(image->depth));
1947 (void) WriteBlobString(image,buffer);
1948 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1949 if (quantum_info == (QuantumInfo *) NULL)
1950 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1951 pixels=GetQuantumPixels(quantum_info);
1952 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1953 range=GetQuantumRange(image->depth);
1954 for (y=0; y < (ssize_t) image->rows; y++)
1956 register const PixelPacket
1962 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1963 if (p == (const PixelPacket *) NULL)
1966 if ((image->depth == 8) || (image->depth == 16))
1967 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1968 quantum_info,quantum_type,pixels,&image->exception);
1971 if (image->depth <= 8)
1972 for (x=0; x < (ssize_t) image->columns; x++)
1974 pixel=ScaleQuantumToAny(p->red,range);
1975 q=PopCharPixel((unsigned char) pixel,q);
1976 pixel=ScaleQuantumToAny(p->green,range);
1977 q=PopCharPixel((unsigned char) pixel,q);
1978 pixel=ScaleQuantumToAny(p->blue,range);
1979 q=PopCharPixel((unsigned char) pixel,q);
1983 for (x=0; x < (ssize_t) image->columns; x++)
1985 pixel=ScaleQuantumToAny(p->red,range);
1986 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1987 pixel=ScaleQuantumToAny(p->green,range);
1988 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1989 pixel=ScaleQuantumToAny(p->blue,range);
1990 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1993 extent=(size_t) (q-pixels);
1995 count=WriteBlob(image,extent,pixels);
1996 if (count != (ssize_t) extent)
1998 if (image->previous == (Image *) NULL)
2000 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2002 if (status == MagickFalse)
2006 quantum_info=DestroyQuantumInfo(quantum_info);
2015 Convert image to a PAM.
2017 if (image->depth > 16)
2019 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2020 pixels=GetQuantumPixels(quantum_info);
2021 range=GetQuantumRange(image->depth);
2022 for (y=0; y < (ssize_t) image->rows; y++)
2024 register const IndexPacket
2027 register const PixelPacket
2033 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2034 if (p == (const PixelPacket *) NULL)
2036 indexes=GetVirtualIndexQueue(image);
2038 if ((image->depth == 8) || (image->depth == 16))
2039 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2040 quantum_info,quantum_type,pixels,&image->exception);
2043 switch (quantum_type)
2046 case GrayAlphaQuantum:
2048 if (image->depth <= 8)
2049 for (x=0; x < (ssize_t) image->columns; x++)
2051 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2052 q=PopCharPixel((unsigned char) pixel,q);
2053 if (image->matte != MagickFalse)
2055 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2057 q=PopCharPixel((unsigned char) pixel,q);
2062 for (x=0; x < (ssize_t) image->columns; x++)
2064 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2065 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2066 if (image->matte != MagickFalse)
2068 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2070 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2079 if (image->depth <= 8)
2080 for (x=0; x < (ssize_t) image->columns; x++)
2082 pixel=ScaleQuantumToAny(p->red,range);
2083 q=PopCharPixel((unsigned char) pixel,q);
2084 pixel=ScaleQuantumToAny(p->green,range);
2085 q=PopCharPixel((unsigned char) pixel,q);
2086 pixel=ScaleQuantumToAny(p->blue,range);
2087 q=PopCharPixel((unsigned char) pixel,q);
2088 pixel=ScaleQuantumToAny(indexes[x],range);
2089 q=PopCharPixel((unsigned char) pixel,q);
2090 if (image->matte != MagickFalse)
2092 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2093 GetOpacityPixelComponent(p)),range);
2094 q=PopCharPixel((unsigned char) pixel,q);
2099 for (x=0; x < (ssize_t) image->columns; x++)
2101 pixel=ScaleQuantumToAny(p->red,range);
2102 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2103 pixel=ScaleQuantumToAny(p->green,range);
2104 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2105 pixel=ScaleQuantumToAny(p->blue,range);
2106 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2107 pixel=ScaleQuantumToAny(indexes[x],range);
2108 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2109 if (image->matte != MagickFalse)
2111 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2112 GetOpacityPixelComponent(p)),range);
2113 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2121 if (image->depth <= 8)
2122 for (x=0; x < (ssize_t) image->columns; x++)
2124 pixel=ScaleQuantumToAny(p->red,range);
2125 q=PopCharPixel((unsigned char) pixel,q);
2126 pixel=ScaleQuantumToAny(p->green,range);
2127 q=PopCharPixel((unsigned char) pixel,q);
2128 pixel=ScaleQuantumToAny(p->blue,range);
2129 q=PopCharPixel((unsigned char) pixel,q);
2130 if (image->matte != MagickFalse)
2132 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2133 GetOpacityPixelComponent(p)),range);
2134 q=PopCharPixel((unsigned char) pixel,q);
2139 for (x=0; x < (ssize_t) image->columns; x++)
2141 pixel=ScaleQuantumToAny(p->red,range);
2142 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2143 pixel=ScaleQuantumToAny(p->green,range);
2144 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2145 pixel=ScaleQuantumToAny(p->blue,range);
2146 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2147 if (image->matte != MagickFalse)
2149 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2150 GetOpacityPixelComponent(p)),range);
2151 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2158 extent=(size_t) (q-pixels);
2160 count=WriteBlob(image,extent,pixels);
2161 if (count != (ssize_t) extent)
2163 if (image->previous == (Image *) NULL)
2165 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2167 if (status == MagickFalse)
2171 quantum_info=DestroyQuantumInfo(quantum_info);
2177 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2180 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2181 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2182 if (quantum_info == (QuantumInfo *) NULL)
2183 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2184 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2185 if (status == MagickFalse)
2186 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2187 pixels=GetQuantumPixels(quantum_info);
2188 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2190 register const PixelPacket
2193 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2194 if (p == (const PixelPacket *) NULL)
2196 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2197 quantum_info,quantum_type,pixels,&image->exception);
2198 (void) WriteBlob(image,extent,pixels);
2199 if (image->previous == (Image *) NULL)
2201 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2203 if (status == MagickFalse)
2207 quantum_info=DestroyQuantumInfo(quantum_info);
2211 if (GetNextImageInList(image) == (Image *) NULL)
2213 image=SyncNextImageInList(image);
2214 status=SetImageProgress(image,SaveImagesTag,scene++,
2215 GetImageListLength(image));
2216 if (status == MagickFalse)
2218 } while (image_info->adjoin != MagickFalse);
2219 (void) CloseBlob(image);