2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2011 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);
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)
375 quantum_type=GrayQuantum;
376 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
378 quantum_type=GrayAlphaQuantum;
379 image->matte=MagickTrue;
381 if (LocaleCompare(value,"GRAYSCALE") == 0)
382 quantum_type=GrayQuantum;
383 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
385 quantum_type=GrayAlphaQuantum;
386 image->matte=MagickTrue;
388 if (LocaleCompare(value,"RGB_ALPHA") == 0)
390 quantum_type=RGBAQuantum;
391 image->matte=MagickTrue;
393 if (LocaleCompare(value,"CMYK") == 0)
395 quantum_type=CMYKQuantum;
396 image->colorspace=CMYKColorspace;
398 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
400 quantum_type=CMYKAQuantum;
401 image->colorspace=CMYKColorspace;
402 image->matte=MagickTrue;
405 if (LocaleCompare(keyword,"width") == 0)
406 image->columns=StringToUnsignedLong(value);
409 if ((image->columns == 0) || (image->rows == 0))
410 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
411 if (max_value >= 65536)
412 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
413 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
415 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
416 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
419 Convert PNM pixels to runextent-encoded MIFF packets.
428 Convert PBM image to pixel packets.
430 for (y=0; y < (ssize_t) image->rows; y++)
438 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
439 if (q == (PixelPacket *) NULL)
441 for (x=0; x < (ssize_t) image->columns; x++)
443 q->red=(Quantum) (PNMInteger(image,2) == 0 ? QuantumRange : 0);
448 if (SyncAuthenticPixels(image,exception) == MagickFalse)
450 if (image->previous == (Image *) NULL)
452 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
454 if (status == MagickFalse)
458 image->type=BilevelType;
467 Convert PGM image to pixel packets.
469 scale=(Quantum *) NULL;
470 if (max_value != (1U*QuantumRange))
473 Compute pixel scaling table.
475 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
477 if (scale == (Quantum *) NULL)
478 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
479 for (i=0; i <= (ssize_t) max_value; i++)
480 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
482 for (y=0; y < (ssize_t) image->rows; y++)
490 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
491 if (q == (PixelPacket *) NULL)
493 for (x=0; x < (ssize_t) image->columns; x++)
495 intensity=PNMInteger(image,10);
496 q->red=(Quantum) intensity;
497 if (scale != (Quantum *) NULL)
498 q->red=scale[ConstrainPixel(image,(ssize_t) intensity,max_value)];
503 if (SyncAuthenticPixels(image,exception) == MagickFalse)
505 if (image->previous == (Image *) NULL)
507 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
509 if (status == MagickFalse)
513 image->type=GrayscaleType;
514 if (scale != (Quantum *) NULL)
515 scale=(Quantum *) RelinquishMagickMemory(scale);
524 Convert PNM image to pixel packets.
526 scale=(Quantum *) NULL;
527 if (max_value != (1U*QuantumRange))
530 Compute pixel scaling table.
532 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
534 if (scale == (Quantum *) NULL)
535 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
536 for (i=0; i <= (ssize_t) max_value; i++)
537 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
539 for (y=0; y < (ssize_t) image->rows; y++)
547 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
548 if (q == (PixelPacket *) NULL)
550 for (x=0; x < (ssize_t) image->columns; x++)
552 pixel.red=(MagickRealType) PNMInteger(image,10);
553 pixel.green=(MagickRealType) PNMInteger(image,10);
554 pixel.blue=(MagickRealType) PNMInteger(image,10);
555 if (scale != (Quantum *) NULL)
557 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
558 pixel.red,max_value)];
559 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
560 (ssize_t) pixel.green,max_value)];
561 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
562 pixel.blue,max_value)];
564 q->red=(Quantum) pixel.red;
565 q->green=(Quantum) pixel.green;
566 q->blue=(Quantum) pixel.blue;
569 if (SyncAuthenticPixels(image,exception) == MagickFalse)
571 if (image->previous == (Image *) NULL)
573 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
575 if (status == MagickFalse)
579 if (scale != (Quantum *) NULL)
580 scale=(Quantum *) RelinquishMagickMemory(scale);
586 Convert PBM raw image to pixel packets.
588 quantum_type=GrayQuantum;
589 if (image->storage_class == PseudoClass)
590 quantum_type=IndexQuantum;
591 quantum_info=AcquireQuantumInfo(image_info,image);
592 if (quantum_info == (QuantumInfo *) NULL)
593 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
594 SetQuantumMinIsWhite(quantum_info,MagickTrue);
595 extent=GetQuantumExtent(image,quantum_info,quantum_type);
596 for (y=0; y < (ssize_t) image->rows; y++)
616 if (status == MagickFalse)
618 pixels=GetQuantumPixels(quantum_info);
620 count=ReadBlob(image,extent,pixels);
621 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
622 (image->previous == (Image *) NULL))
627 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
629 if (proceed == MagickFalse)
634 if (count != (ssize_t) extent)
636 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
637 if (q == (PixelPacket *) NULL)
642 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
643 quantum_type,pixels,exception);
644 if (length != extent)
646 sync=SyncAuthenticPixels(image,exception);
647 if (sync == MagickFalse)
650 quantum_info=DestroyQuantumInfo(quantum_info);
651 if (status == MagickFalse)
652 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
653 SetQuantumImageType(image,quantum_type);
662 Convert PGM raw image to pixel packets.
664 range=GetQuantumRange(image->depth);
665 quantum_type=GrayQuantum;
666 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
667 quantum_info=AcquireQuantumInfo(image_info,image);
668 if (quantum_info == (QuantumInfo *) NULL)
669 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
670 for (y=0; y < (ssize_t) image->rows; y++)
678 register const unsigned char
693 if (status == MagickFalse)
695 pixels=GetQuantumPixels(quantum_info);
697 count=ReadBlob(image,extent,pixels);
698 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
699 (image->previous == (Image *) NULL))
704 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
706 if (proceed == MagickFalse)
711 if (count != (ssize_t) extent)
713 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
714 if (q == (PixelPacket *) NULL)
720 if ((image->depth == 8) || (image->depth == 16))
721 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
722 quantum_type,pixels,exception);
724 if (image->depth <= 8)
729 for (x=0; x < (ssize_t) image->columns; x++)
731 p=PushCharPixel(p,&pixel);
732 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
743 for (x=0; x < (ssize_t) image->columns; x++)
745 p=PushShortPixel(MSBEndian,p,&pixel);
746 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
752 sync=SyncAuthenticPixels(image,exception);
753 if (sync == MagickFalse)
756 quantum_info=DestroyQuantumInfo(quantum_info);
757 if (status == MagickFalse)
758 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
759 SetQuantumImageType(image,quantum_type);
771 Convert PNM raster image to pixel packets.
774 quantum_type=RGBQuantum;
775 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
776 range=GetQuantumRange(image->depth);
777 quantum_info=AcquireQuantumInfo(image_info,image);
778 if (quantum_info == (QuantumInfo *) NULL)
779 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
780 for (y=0; y < (ssize_t) image->rows; y++)
788 register const unsigned char
803 if (status == MagickFalse)
805 pixels=GetQuantumPixels(quantum_info);
807 count=ReadBlob(image,extent,pixels);
808 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
809 (image->previous == (Image *) NULL))
814 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
816 if (proceed == MagickFalse)
821 if (count != (ssize_t) extent)
823 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
824 if (q == (PixelPacket *) NULL)
830 if (image->depth == 8)
831 for (x=0; x < (ssize_t) image->columns; x++)
833 q->red=ScaleCharToQuantum(*p++);
834 q->green=ScaleCharToQuantum(*p++);
835 q->blue=ScaleCharToQuantum(*p++);
836 q->opacity=OpaqueOpacity;
840 if (image->depth == 16)
845 for (x=0; x < (ssize_t) image->columns; x++)
847 p=PushShortPixel(MSBEndian,p,&pixel);
848 q->red=ScaleShortToQuantum(pixel);
849 p=PushShortPixel(MSBEndian,p,&pixel);
850 q->green=ScaleShortToQuantum(pixel);
851 p=PushShortPixel(MSBEndian,p,&pixel);
852 q->blue=ScaleShortToQuantum(pixel);
853 q->opacity=OpaqueOpacity;
858 if (image->depth <= 8)
863 for (x=0; x < (ssize_t) image->columns; x++)
865 p=PushCharPixel(p,&pixel);
866 q->red=ScaleAnyToQuantum(pixel,range);
867 p=PushCharPixel(p,&pixel);
868 q->green=ScaleAnyToQuantum(pixel,range);
869 p=PushCharPixel(p,&pixel);
870 q->blue=ScaleAnyToQuantum(pixel,range);
871 q->opacity=OpaqueOpacity;
880 for (x=0; x < (ssize_t) image->columns; x++)
882 p=PushShortPixel(MSBEndian,p,&pixel);
883 q->red=ScaleAnyToQuantum(pixel,range);
884 p=PushShortPixel(MSBEndian,p,&pixel);
885 q->green=ScaleAnyToQuantum(pixel,range);
886 p=PushShortPixel(MSBEndian,p,&pixel);
887 q->blue=ScaleAnyToQuantum(pixel,range);
888 q->opacity=OpaqueOpacity;
892 if ((type == BilevelType) || (type == GrayscaleType))
894 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
895 for (x=0; x < (ssize_t) image->columns; x++)
897 if ((type == BilevelType) &&
898 (IsMonochromePixel(q) == MagickFalse))
899 type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
901 if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
903 if ((type != BilevelType) && (type != GrayscaleType))
908 sync=SyncAuthenticPixels(image,exception);
909 if (sync == MagickFalse)
912 quantum_info=DestroyQuantumInfo(quantum_info);
913 if (status == MagickFalse)
914 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
915 if (type != UndefinedType)
931 Convert PAM raster image to pixel packets.
933 range=GetQuantumRange(image->depth);
934 switch (quantum_type)
937 case GrayAlphaQuantum:
954 if (image->matte != MagickFalse)
956 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
957 quantum_info=AcquireQuantumInfo(image_info,image);
958 if (quantum_info == (QuantumInfo *) NULL)
959 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
960 for (y=0; y < (ssize_t) image->rows; y++)
968 register const unsigned char
983 if (status == MagickFalse)
985 pixels=GetQuantumPixels(quantum_info);
987 count=ReadBlob(image,extent,pixels);
988 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
989 (image->previous == (Image *) NULL))
994 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
996 if (proceed == MagickFalse)
1001 if (count != (ssize_t) extent)
1003 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1004 if (q == (PixelPacket *) NULL)
1009 indexes=GetAuthenticIndexQueue(image);
1011 if ((image->depth == 8) || (image->depth == 16))
1012 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1013 quantum_type,pixels,exception);
1015 switch (quantum_type)
1018 case GrayAlphaQuantum:
1020 if (image->depth <= 8)
1025 for (x=0; x < (ssize_t) image->columns; x++)
1027 p=PushCharPixel(p,&pixel);
1028 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1031 SetOpacityPixelComponent(q,OpaqueOpacity);
1032 if (image->matte != MagickFalse)
1034 p=PushCharPixel(p,&pixel);
1035 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1046 for (x=0; x < (ssize_t) image->columns; x++)
1048 p=PushShortPixel(MSBEndian,p,&pixel);
1049 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1052 SetOpacityPixelComponent(q,OpaqueOpacity);
1053 if (image->matte != MagickFalse)
1055 p=PushShortPixel(MSBEndian,p,&pixel);
1056 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1067 if (image->depth <= 8)
1072 for (x=0; x < (ssize_t) image->columns; x++)
1074 p=PushCharPixel(p,&pixel);
1075 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1076 p=PushCharPixel(p,&pixel);
1077 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1078 p=PushCharPixel(p,&pixel);
1079 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1080 p=PushCharPixel(p,&pixel);
1081 indexes[x]=ScaleAnyToQuantum(pixel,range);
1082 SetOpacityPixelComponent(q,OpaqueOpacity);
1083 if (image->matte != MagickFalse)
1085 p=PushCharPixel(p,&pixel);
1086 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1097 for (x=0; x < (ssize_t) image->columns; x++)
1099 p=PushShortPixel(MSBEndian,p,&pixel);
1100 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1101 p=PushShortPixel(MSBEndian,p,&pixel);
1102 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1103 p=PushShortPixel(MSBEndian,p,&pixel);
1104 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1105 p=PushShortPixel(MSBEndian,p,&pixel);
1106 indexes[x]=ScaleAnyToQuantum(pixel,range);
1107 SetOpacityPixelComponent(q,OpaqueOpacity);
1108 if (image->matte != MagickFalse)
1110 p=PushShortPixel(MSBEndian,p,&pixel);
1111 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1121 if (image->depth <= 8)
1126 for (x=0; x < (ssize_t) image->columns; x++)
1128 p=PushCharPixel(p,&pixel);
1129 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1130 p=PushCharPixel(p,&pixel);
1131 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1132 p=PushCharPixel(p,&pixel);
1133 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1134 SetOpacityPixelComponent(q,OpaqueOpacity);
1135 if (image->matte != MagickFalse)
1137 p=PushCharPixel(p,&pixel);
1138 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1149 for (x=0; x < (ssize_t) image->columns; x++)
1151 p=PushShortPixel(MSBEndian,p,&pixel);
1152 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1153 p=PushShortPixel(MSBEndian,p,&pixel);
1154 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1155 p=PushShortPixel(MSBEndian,p,&pixel);
1156 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1157 SetOpacityPixelComponent(q,OpaqueOpacity);
1158 if (image->matte != MagickFalse)
1160 p=PushShortPixel(MSBEndian,p,&pixel);
1161 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1170 sync=SyncAuthenticPixels(image,exception);
1171 if (sync == MagickFalse)
1174 quantum_info=DestroyQuantumInfo(quantum_info);
1175 if (status == MagickFalse)
1176 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1177 SetQuantumImageType(image,quantum_type);
1184 Convert PFM raster image to pixel packets.
1186 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1187 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1189 quantum_info=AcquireQuantumInfo(image_info,image);
1190 if (quantum_info == (QuantumInfo *) NULL)
1191 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1192 status=SetQuantumDepth(image,quantum_info,32);
1193 if (status == MagickFalse)
1194 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1195 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1196 if (status == MagickFalse)
1197 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1198 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1199 fabs(quantum_scale));
1200 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1201 for (y=0; y < (ssize_t) image->rows; y++)
1209 register PixelPacket
1221 if (status == MagickFalse)
1223 pixels=GetQuantumPixels(quantum_info);
1225 count=ReadBlob(image,extent,pixels);
1226 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1227 (image->previous == (Image *) NULL))
1232 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1234 if (proceed == MagickFalse)
1239 if ((size_t) count != extent)
1241 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1242 image->columns,1,exception);
1243 if (q == (PixelPacket *) NULL)
1248 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1249 quantum_type,pixels,exception);
1250 if (length != extent)
1252 sync=SyncAuthenticPixels(image,exception);
1253 if (sync == MagickFalse)
1256 quantum_info=DestroyQuantumInfo(quantum_info);
1257 if (status == MagickFalse)
1258 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1259 SetQuantumImageType(image,quantum_type);
1263 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1265 if (EOFBlob(image) != MagickFalse)
1266 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1267 "UnexpectedEndOfFile","`%s'",image->filename);
1269 Proceed to next image.
1271 if (image_info->number_scenes != 0)
1272 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1274 if ((format == '1') || (format == '2') || (format == '3'))
1278 Skip to end of line.
1280 count=ReadBlob(image,1,(unsigned char *) &format);
1283 if ((count != 0) && (format == 'P'))
1285 } while (format != '\n');
1286 count=ReadBlob(image,1,(unsigned char *) &format);
1287 if ((count == 1) && (format == 'P'))
1290 Allocate next image structure.
1292 AcquireNextImage(image_info,image);
1293 if (GetNextImageInList(image) == (Image *) NULL)
1295 image=DestroyImageList(image);
1296 return((Image *) NULL);
1298 image=SyncNextImageInList(image);
1299 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1300 GetBlobSize(image));
1301 if (status == MagickFalse)
1304 } while ((count == 1) && (format == 'P'));
1305 (void) CloseBlob(image);
1306 return(GetFirstImageInList(image));
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314 % R e g i s t e r P N M I m a g e %
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % RegisterPNMImage() adds properties for the PNM image format to
1321 % the list of supported formats. The properties include the image format
1322 % tag, a method to read and/or write the format, whether the format
1323 % supports the saving of more than one frame to the same file or blob,
1324 % whether the format supports native in-memory I/O, and a brief
1325 % description of the format.
1327 % The format of the RegisterPNMImage method is:
1329 % size_t RegisterPNMImage(void)
1332 ModuleExport size_t RegisterPNMImage(void)
1337 entry=SetMagickInfo("PAM");
1338 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1339 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1340 entry->description=ConstantString("Common 2-dimensional bitmap format");
1341 entry->module=ConstantString("PNM");
1342 (void) RegisterMagickInfo(entry);
1343 entry=SetMagickInfo("PBM");
1344 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1345 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1346 entry->description=ConstantString("Portable bitmap format (black and white)");
1347 entry->module=ConstantString("PNM");
1348 (void) RegisterMagickInfo(entry);
1349 entry=SetMagickInfo("PFM");
1350 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1351 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1352 entry->endian_support=MagickTrue;
1353 entry->description=ConstantString("Portable float format");
1354 entry->module=ConstantString("PFM");
1355 (void) RegisterMagickInfo(entry);
1356 entry=SetMagickInfo("PGM");
1357 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1358 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1359 entry->description=ConstantString("Portable graymap format (gray scale)");
1360 entry->module=ConstantString("PNM");
1361 (void) RegisterMagickInfo(entry);
1362 entry=SetMagickInfo("PNM");
1363 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1364 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1365 entry->magick=(IsImageFormatHandler *) IsPNM;
1366 entry->description=ConstantString("Portable anymap");
1367 entry->module=ConstantString("PNM");
1368 (void) RegisterMagickInfo(entry);
1369 entry=SetMagickInfo("PPM");
1370 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1371 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1372 entry->description=ConstantString("Portable pixmap format (color)");
1373 entry->module=ConstantString("PNM");
1374 (void) RegisterMagickInfo(entry);
1375 return(MagickImageCoderSignature);
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383 % U n r e g i s t e r P N M I m a g e %
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 % UnregisterPNMImage() removes format registrations made by the
1390 % PNM module from the list of supported formats.
1392 % The format of the UnregisterPNMImage method is:
1394 % UnregisterPNMImage(void)
1397 ModuleExport void UnregisterPNMImage(void)
1399 (void) UnregisterMagickInfo("PAM");
1400 (void) UnregisterMagickInfo("PBM");
1401 (void) UnregisterMagickInfo("PGM");
1402 (void) UnregisterMagickInfo("PNM");
1403 (void) UnregisterMagickInfo("PPM");
1407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 % W r i t e P N M I m a g e %
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1419 % The format of the WritePNMImage method is:
1421 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1423 % A description of each parameter follows.
1425 % o image_info: the image info.
1427 % o image: The image.
1430 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1433 buffer[MaxTextExtent],
1435 magick[MaxTextExtent];
1461 register unsigned char
1473 Open output image file.
1475 assert(image_info != (const ImageInfo *) NULL);
1476 assert(image_info->signature == MagickSignature);
1477 assert(image != (Image *) NULL);
1478 assert(image->signature == MagickSignature);
1479 if (image->debug != MagickFalse)
1480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1481 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1482 if (status == MagickFalse)
1488 Write PNM file header.
1491 quantum_type=RGBQuantum;
1492 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1505 if (image_info->compression == NoCompression)
1513 if (IsGrayImage(image,&image->exception) != MagickFalse)
1521 if (image_info->compression == NoCompression)
1528 if ((image_info->type != TrueColorType) &&
1529 (IsGrayImage(image,&image->exception) != MagickFalse))
1532 if (image_info->compression == NoCompression)
1534 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1537 if (image_info->compression == NoCompression)
1546 if (image_info->compression == NoCompression)
1551 (void) FormatMagickString(buffer,MaxTextExtent,"P%c\n",format);
1552 (void) WriteBlobString(image,buffer);
1553 value=GetImageProperty(image,"comment");
1554 if (value != (const char *) NULL)
1560 Write comments to file.
1562 (void) WriteBlobByte(image,'#');
1563 for (p=value; *p != '\0'; p++)
1565 (void) WriteBlobByte(image,(unsigned char) *p);
1566 if ((*p == '\r') && (*(p+1) != '\0'))
1567 (void) WriteBlobByte(image,'#');
1568 if ((*p == '\n') && (*(p+1) != '\0'))
1569 (void) WriteBlobByte(image,'#');
1571 (void) WriteBlobByte(image,'\n');
1575 if (image->colorspace != RGBColorspace)
1576 (void) TransformImageColorspace(image,RGBColorspace);
1577 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n",
1578 (double) image->columns,(double) image->rows);
1579 (void) WriteBlobString(image,buffer);
1584 type[MaxTextExtent];
1589 (void) FormatMagickString(buffer,MaxTextExtent,
1590 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1592 (void) WriteBlobString(image,buffer);
1593 quantum_type=GetQuantumType(image,&image->exception);
1594 switch (quantum_type)
1600 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1604 case GrayAlphaQuantum:
1607 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1612 quantum_type=RGBQuantum;
1613 if (image->matte != MagickFalse)
1614 quantum_type=RGBAQuantum;
1616 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1620 if (image->matte != MagickFalse)
1623 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1625 if (image->depth > 16)
1627 (void) FormatMagickString(buffer,MaxTextExtent,
1628 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1629 GetQuantumRange(image->depth));
1630 (void) WriteBlobString(image,buffer);
1631 (void) FormatMagickString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1633 (void) WriteBlobString(image,buffer);
1636 Convert runextent encoded to PNM raster pixels.
1646 Convert image to a PBM image.
1649 for (y=0; y < (ssize_t) image->rows; y++)
1651 register const PixelPacket
1657 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1658 if (p == (const PixelPacket *) NULL)
1660 for (x=0; x < (ssize_t) image->columns; x++)
1662 pixel=PixelIntensityToQuantum(p);
1663 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1666 if ((q-pixels+2) >= 80)
1669 (void) WriteBlob(image,q-pixels,pixels);
1674 if (image->previous == (Image *) NULL)
1676 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1678 if (status == MagickFalse)
1685 (void) WriteBlob(image,q-pixels,pixels);
1695 Convert image to a PGM image.
1697 if (image->depth <= 8)
1698 (void) WriteBlobString(image,"255\n");
1700 (void) WriteBlobString(image,"65535\n");
1702 for (y=0; y < (ssize_t) image->rows; y++)
1704 register const PixelPacket
1710 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1711 if (p == (const PixelPacket *) NULL)
1713 for (x=0; x < (ssize_t) image->columns; x++)
1715 index=PixelIntensityToQuantum(p);
1716 if (image->depth <= 8)
1717 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1718 ScaleQuantumToChar(index));
1720 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1721 ScaleQuantumToShort(index));
1722 extent=(size_t) count;
1723 (void) strncpy((char *) q,buffer,extent);
1725 if ((q-pixels+extent) >= 80)
1728 (void) WriteBlob(image,q-pixels,pixels);
1733 if (image->previous == (Image *) NULL)
1735 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1737 if (status == MagickFalse)
1744 (void) WriteBlob(image,q-pixels,pixels);
1754 Convert image to a PNM image.
1756 if (image->depth <= 8)
1757 (void) WriteBlobString(image,"255\n");
1759 (void) WriteBlobString(image,"65535\n");
1761 for (y=0; y < (ssize_t) image->rows; y++)
1763 register const PixelPacket
1769 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1770 if (p == (const PixelPacket *) NULL)
1772 for (x=0; x < (ssize_t) image->columns; x++)
1774 if (image->depth <= 8)
1775 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1776 "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
1777 ScaleQuantumToChar(GetGreenPixelComponent(p)),
1778 ScaleQuantumToChar(GetBluePixelComponent(p)));
1780 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1781 "%u %u %u ",ScaleQuantumToShort(GetRedPixelComponent(p)),
1782 ScaleQuantumToShort(GetGreenPixelComponent(p)),
1783 ScaleQuantumToShort(GetBluePixelComponent(p)));
1784 extent=(size_t) count;
1785 (void) strncpy((char *) q,buffer,extent);
1787 if ((q-pixels+extent) >= 80)
1790 (void) WriteBlob(image,q-pixels,pixels);
1795 if (image->previous == (Image *) NULL)
1797 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1799 if (status == MagickFalse)
1806 (void) WriteBlob(image,q-pixels,pixels);
1813 Convert image to a PBM image.
1816 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1817 if (quantum_info == (QuantumInfo *) NULL)
1818 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1819 quantum_info->min_is_white=MagickTrue;
1820 pixels=GetQuantumPixels(quantum_info);
1821 for (y=0; y < (ssize_t) image->rows; y++)
1823 register const PixelPacket
1826 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1827 if (p == (const PixelPacket *) NULL)
1829 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1830 quantum_info,GrayQuantum,pixels,&image->exception);
1831 count=WriteBlob(image,extent,pixels);
1832 if (count != (ssize_t) extent)
1834 if (image->previous == (Image *) NULL)
1836 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1838 if (status == MagickFalse)
1842 quantum_info=DestroyQuantumInfo(quantum_info);
1851 Convert image to a PGM image.
1853 if (image->depth > 8)
1855 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
1856 GetQuantumRange(image->depth));
1857 (void) WriteBlobString(image,buffer);
1858 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1859 if (quantum_info == (QuantumInfo *) NULL)
1860 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1861 quantum_info->min_is_white=MagickTrue;
1862 pixels=GetQuantumPixels(quantum_info);
1863 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1864 range=GetQuantumRange(image->depth);
1865 for (y=0; y < (ssize_t) image->rows; y++)
1867 register const PixelPacket
1873 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1874 if (p == (const PixelPacket *) NULL)
1877 if ((image->depth == 8) || (image->depth == 16))
1878 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1879 quantum_info,GrayQuantum,pixels,&image->exception);
1882 if (image->depth <= 8)
1883 for (x=0; x < (ssize_t) image->columns; x++)
1885 if (IsGrayPixel(p) == MagickFalse)
1886 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1889 if (image->depth == 8)
1890 pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
1892 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1894 q=PopCharPixel((unsigned char) pixel,q);
1898 for (x=0; x < (ssize_t) image->columns; x++)
1900 if (IsGrayPixel(p) == MagickFalse)
1901 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1904 if (image->depth == 16)
1905 pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
1907 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1909 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1912 extent=(size_t) (q-pixels);
1914 count=WriteBlob(image,extent,pixels);
1915 if (count != (ssize_t) extent)
1917 if (image->previous == (Image *) NULL)
1919 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1921 if (status == MagickFalse)
1925 quantum_info=DestroyQuantumInfo(quantum_info);
1934 Convert image to a PNM image.
1936 if (image->depth > 8)
1938 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
1939 GetQuantumRange(image->depth));
1940 (void) WriteBlobString(image,buffer);
1941 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1942 if (quantum_info == (QuantumInfo *) NULL)
1943 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1944 pixels=GetQuantumPixels(quantum_info);
1945 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1946 range=GetQuantumRange(image->depth);
1947 for (y=0; y < (ssize_t) image->rows; y++)
1949 register const PixelPacket
1955 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1956 if (p == (const PixelPacket *) NULL)
1959 if ((image->depth == 8) || (image->depth == 16))
1960 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1961 quantum_info,quantum_type,pixels,&image->exception);
1964 if (image->depth <= 8)
1965 for (x=0; x < (ssize_t) image->columns; x++)
1967 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1968 q=PopCharPixel((unsigned char) pixel,q);
1969 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
1970 q=PopCharPixel((unsigned char) pixel,q);
1971 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
1972 q=PopCharPixel((unsigned char) pixel,q);
1976 for (x=0; x < (ssize_t) image->columns; x++)
1978 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1979 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1980 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
1981 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1982 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
1983 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1986 extent=(size_t) (q-pixels);
1988 count=WriteBlob(image,extent,pixels);
1989 if (count != (ssize_t) extent)
1991 if (image->previous == (Image *) NULL)
1993 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1995 if (status == MagickFalse)
1999 quantum_info=DestroyQuantumInfo(quantum_info);
2008 Convert image to a PAM.
2010 if (image->depth > 16)
2012 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2013 pixels=GetQuantumPixels(quantum_info);
2014 range=GetQuantumRange(image->depth);
2015 for (y=0; y < (ssize_t) image->rows; y++)
2017 register const IndexPacket
2020 register const PixelPacket
2026 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2027 if (p == (const PixelPacket *) NULL)
2029 indexes=GetVirtualIndexQueue(image);
2031 if ((image->depth == 8) || (image->depth == 16))
2032 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2033 quantum_info,quantum_type,pixels,&image->exception);
2036 switch (quantum_type)
2039 case GrayAlphaQuantum:
2041 if (image->depth <= 8)
2042 for (x=0; x < (ssize_t) image->columns; x++)
2044 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2045 q=PopCharPixel((unsigned char) pixel,q);
2046 if (image->matte != MagickFalse)
2048 pixel=(unsigned char) ScaleQuantumToAny(GetOpacityPixelComponent(p),
2050 q=PopCharPixel((unsigned char) pixel,q);
2055 for (x=0; x < (ssize_t) image->columns; x++)
2057 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2058 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2059 if (image->matte != MagickFalse)
2061 pixel=(unsigned char) ScaleQuantumToAny(GetOpacityPixelComponent(p),
2063 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2072 if (image->depth <= 8)
2073 for (x=0; x < (ssize_t) image->columns; x++)
2075 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2076 q=PopCharPixel((unsigned char) pixel,q);
2077 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2078 q=PopCharPixel((unsigned char) pixel,q);
2079 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2080 q=PopCharPixel((unsigned char) pixel,q);
2081 pixel=ScaleQuantumToAny(GetIndexPixelComponent(indexes+x),range);
2082 q=PopCharPixel((unsigned char) pixel,q);
2083 if (image->matte != MagickFalse)
2085 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2086 GetOpacityPixelComponent(p)),range);
2087 q=PopCharPixel((unsigned char) pixel,q);
2092 for (x=0; x < (ssize_t) image->columns; x++)
2094 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2095 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2096 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2097 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2098 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2099 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2100 pixel=ScaleQuantumToAny(GetIndexPixelComponent(indexes+x),range);
2101 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2102 if (image->matte != MagickFalse)
2104 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2105 GetOpacityPixelComponent(p)),range);
2106 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2114 if (image->depth <= 8)
2115 for (x=0; x < (ssize_t) image->columns; x++)
2117 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2118 q=PopCharPixel((unsigned char) pixel,q);
2119 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2120 q=PopCharPixel((unsigned char) pixel,q);
2121 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2122 q=PopCharPixel((unsigned char) pixel,q);
2123 if (image->matte != MagickFalse)
2125 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2126 GetOpacityPixelComponent(p)),range);
2127 q=PopCharPixel((unsigned char) pixel,q);
2132 for (x=0; x < (ssize_t) image->columns; x++)
2134 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2135 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2136 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2137 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2138 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2139 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2140 if (image->matte != MagickFalse)
2142 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2143 GetOpacityPixelComponent(p)),range);
2144 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2151 extent=(size_t) (q-pixels);
2153 count=WriteBlob(image,extent,pixels);
2154 if (count != (ssize_t) extent)
2156 if (image->previous == (Image *) NULL)
2158 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2160 if (status == MagickFalse)
2164 quantum_info=DestroyQuantumInfo(quantum_info);
2170 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2173 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2174 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2175 if (quantum_info == (QuantumInfo *) NULL)
2176 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2177 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2178 if (status == MagickFalse)
2179 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2180 pixels=GetQuantumPixels(quantum_info);
2181 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2183 register const PixelPacket
2186 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2187 if (p == (const PixelPacket *) NULL)
2189 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2190 quantum_info,quantum_type,pixels,&image->exception);
2191 (void) WriteBlob(image,extent,pixels);
2192 if (image->previous == (Image *) NULL)
2194 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2196 if (status == MagickFalse)
2200 quantum_info=DestroyQuantumInfo(quantum_info);
2204 if (GetNextImageInList(image) == (Image *) NULL)
2206 image=SyncNextImageInList(image);
2207 status=SetImageProgress(image,SaveImagesTag,scene++,
2208 GetImageListLength(image));
2209 if (status == MagickFalse)
2211 } while (image_info->adjoin != MagickFalse);
2212 (void) CloseBlob(image);