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)
165 Skip any leading whitespace.
167 extent=MaxTextExtent;
168 comment=(char *) NULL;
172 c=ReadBlobByte(image);
180 if (comment == (char *) NULL)
181 comment=AcquireString((char *) NULL);
182 p=comment+strlen(comment);
183 for ( ; (c != EOF) && (c != (int) '\n'); p++)
185 if ((size_t) (p-comment+1) >= extent)
188 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
190 if (comment == (char *) NULL)
192 p=comment+strlen(comment);
194 c=ReadBlobByte(image);
198 if (comment == (char *) NULL)
202 } while (isdigit(c) == MagickFalse);
203 if (comment != (char *) NULL)
205 (void) SetImageProperty(image,"comment",comment);
206 comment=DestroyString(comment);
209 return((size_t) (c-(int) '0'));
218 c=ReadBlobByte(image);
221 } while (isdigit(c) != MagickFalse);
225 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
265 assert(image_info != (const ImageInfo *) NULL);
266 assert(image_info->signature == MagickSignature);
267 if (image_info->debug != MagickFalse)
268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
269 image_info->filename);
270 assert(exception != (ExceptionInfo *) NULL);
271 assert(exception->signature == MagickSignature);
272 image=AcquireImage(image_info);
273 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
274 if (status == MagickFalse)
276 image=DestroyImageList(image);
277 return((Image *) NULL);
282 count=ReadBlob(image,1,(unsigned char *) &format);
286 Initialize image structure.
288 if ((count != 1) || (format != 'P'))
289 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
291 quantum_type=RGBQuantum;
293 format=(char) ReadBlobByte(image);
297 PBM, PGM, PPM, and PNM.
299 image->columns=PNMInteger(image,10);
300 image->rows=PNMInteger(image,10);
301 if ((format == 'f') || (format == 'F'))
304 scale[MaxTextExtent];
306 (void) ReadBlobString(image,scale);
307 quantum_scale=InterpretLocaleValue(scale,(char **) NULL);
311 if ((format == '1') || (format == '4'))
312 max_value=1; /* bitmap */
314 max_value=PNMInteger(image,10);
320 keyword[MaxTextExtent],
321 value[MaxTextExtent];
332 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
334 while (isspace((int) ((unsigned char) c)) != 0)
335 c=ReadBlobByte(image);
339 if ((size_t) (p-keyword) < (MaxTextExtent-1))
341 c=ReadBlobByte(image);
342 } while (isalnum(c));
344 if (LocaleCompare(keyword,"endhdr") == 0)
346 while (isspace((int) ((unsigned char) c)) != 0)
347 c=ReadBlobByte(image);
349 while (isalnum(c) || (c == '_'))
351 if ((size_t) (p-value) < (MaxTextExtent-1))
353 c=ReadBlobByte(image);
357 Assign a value to the specified keyword.
359 if (LocaleCompare(keyword,"depth") == 0)
360 packet_size=StringToUnsignedLong(value);
362 if (LocaleCompare(keyword,"height") == 0)
363 image->rows=StringToUnsignedLong(value);
364 if (LocaleCompare(keyword,"maxval") == 0)
365 max_value=StringToUnsignedLong(value);
366 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
368 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
369 quantum_type=GrayQuantum;
370 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
372 quantum_type=GrayAlphaQuantum;
373 image->matte=MagickTrue;
375 if (LocaleCompare(value,"GRAYSCALE") == 0)
376 quantum_type=GrayQuantum;
377 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
379 quantum_type=GrayAlphaQuantum;
380 image->matte=MagickTrue;
382 if (LocaleCompare(value,"RGB_ALPHA") == 0)
384 quantum_type=RGBAQuantum;
385 image->matte=MagickTrue;
387 if (LocaleCompare(value,"CMYK") == 0)
389 quantum_type=CMYKQuantum;
390 image->colorspace=CMYKColorspace;
392 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
394 quantum_type=CMYKAQuantum;
395 image->colorspace=CMYKColorspace;
396 image->matte=MagickTrue;
399 if (LocaleCompare(keyword,"width") == 0)
400 image->columns=StringToUnsignedLong(value);
403 if ((image->columns == 0) || (image->rows == 0))
404 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
405 if (max_value >= 65536)
406 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
407 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
409 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
410 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
413 Convert PNM pixels to runextent-encoded MIFF packets.
422 Convert PBM image to pixel packets.
424 for (y=0; y < (ssize_t) image->rows; y++)
432 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
433 if (q == (PixelPacket *) NULL)
435 for (x=0; x < (ssize_t) image->columns; x++)
437 SetRedPixelComponent(q,PNMInteger(image,2) == 0 ? QuantumRange : 0);
438 SetGreenPixelComponent(q,GetRedPixelComponent(q));
439 SetBluePixelComponent(q,GetRedPixelComponent(q));
442 if (SyncAuthenticPixels(image,exception) == MagickFalse)
444 if (image->previous == (Image *) NULL)
446 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
448 if (status == MagickFalse)
452 image->type=BilevelType;
461 Convert PGM image to pixel packets.
463 scale=(Quantum *) NULL;
464 if (max_value != (1U*QuantumRange))
467 Compute pixel scaling table.
469 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
471 if (scale == (Quantum *) NULL)
472 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
473 for (i=0; i <= (ssize_t) max_value; i++)
474 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
476 for (y=0; y < (ssize_t) image->rows; y++)
484 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
485 if (q == (PixelPacket *) NULL)
487 for (x=0; x < (ssize_t) image->columns; x++)
489 intensity=PNMInteger(image,10);
490 SetRedPixelComponent(q,intensity);
491 if (scale != (Quantum *) NULL)
492 SetRedPixelComponent(q,scale[ConstrainPixel(image,(ssize_t)
493 intensity,max_value)]);
494 SetGreenPixelComponent(q,GetRedPixelComponent(q));
495 SetBluePixelComponent(q,GetRedPixelComponent(q));
498 if (SyncAuthenticPixels(image,exception) == MagickFalse)
500 if (image->previous == (Image *) NULL)
502 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
504 if (status == MagickFalse)
508 image->type=GrayscaleType;
509 if (scale != (Quantum *) NULL)
510 scale=(Quantum *) RelinquishMagickMemory(scale);
519 Convert PNM image to pixel packets.
521 scale=(Quantum *) NULL;
522 if (max_value != (1U*QuantumRange))
525 Compute pixel scaling table.
527 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
529 if (scale == (Quantum *) NULL)
530 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
531 for (i=0; i <= (ssize_t) max_value; i++)
532 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
534 for (y=0; y < (ssize_t) image->rows; y++)
542 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
543 if (q == (PixelPacket *) NULL)
545 for (x=0; x < (ssize_t) image->columns; x++)
547 pixel.red=(MagickRealType) PNMInteger(image,10);
548 pixel.green=(MagickRealType) PNMInteger(image,10);
549 pixel.blue=(MagickRealType) PNMInteger(image,10);
550 if (scale != (Quantum *) NULL)
552 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
553 pixel.red,max_value)];
554 pixel.green=(MagickRealType) scale[ConstrainPixel(image,
555 (ssize_t) pixel.green,max_value)];
556 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
557 pixel.blue,max_value)];
559 SetRedPixelComponent(q,pixel.red);
560 SetGreenPixelComponent(q,pixel.green);
561 SetBluePixelComponent(q,pixel.blue);
564 if (SyncAuthenticPixels(image,exception) == MagickFalse)
566 if (image->previous == (Image *) NULL)
568 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
570 if (status == MagickFalse)
574 if (scale != (Quantum *) NULL)
575 scale=(Quantum *) RelinquishMagickMemory(scale);
581 Convert PBM raw image to pixel packets.
583 quantum_type=GrayQuantum;
584 if (image->storage_class == PseudoClass)
585 quantum_type=IndexQuantum;
586 quantum_info=AcquireQuantumInfo(image_info,image);
587 if (quantum_info == (QuantumInfo *) NULL)
588 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
589 SetQuantumMinIsWhite(quantum_info,MagickTrue);
590 extent=GetQuantumExtent(image,quantum_info,quantum_type);
591 for (y=0; y < (ssize_t) image->rows; y++)
609 if (status == MagickFalse)
611 pixels=GetQuantumPixels(quantum_info);
613 count=ReadBlob(image,extent,pixels);
614 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
615 (image->previous == (Image *) NULL))
620 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
622 if (proceed == MagickFalse)
627 if (count != (ssize_t) extent)
629 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
630 if (q == (PixelPacket *) NULL)
635 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
636 quantum_type,pixels,exception);
637 if (length != extent)
639 sync=SyncAuthenticPixels(image,exception);
640 if (sync == MagickFalse)
643 quantum_info=DestroyQuantumInfo(quantum_info);
644 if (status == MagickFalse)
645 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
646 SetQuantumImageType(image,quantum_type);
655 Convert PGM raw image to pixel packets.
657 range=GetQuantumRange(image->depth);
658 quantum_type=GrayQuantum;
659 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
660 quantum_info=AcquireQuantumInfo(image_info,image);
661 if (quantum_info == (QuantumInfo *) NULL)
662 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
663 for (y=0; y < (ssize_t) image->rows; y++)
668 register const unsigned char
684 if (status == MagickFalse)
686 pixels=GetQuantumPixels(quantum_info);
688 count=ReadBlob(image,extent,pixels);
689 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
690 (image->previous == (Image *) NULL))
695 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
697 if (proceed == MagickFalse)
702 if (count != (ssize_t) extent)
704 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
705 if (q == (PixelPacket *) NULL)
711 if ((image->depth == 8) || (image->depth == 16))
712 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
713 quantum_type,pixels,exception);
715 if (image->depth <= 8)
720 for (x=0; x < (ssize_t) image->columns; x++)
722 p=PushCharPixel(p,&pixel);
723 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
724 SetGreenPixelComponent(q,GetRedPixelComponent(q));
725 SetBluePixelComponent(q,GetRedPixelComponent(q));
734 for (x=0; x < (ssize_t) image->columns; x++)
736 p=PushShortPixel(MSBEndian,p,&pixel);
737 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
738 SetGreenPixelComponent(q,GetRedPixelComponent(q));
739 SetBluePixelComponent(q,GetRedPixelComponent(q));
743 sync=SyncAuthenticPixels(image,exception);
744 if (sync == MagickFalse)
747 quantum_info=DestroyQuantumInfo(quantum_info);
748 if (status == MagickFalse)
749 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
750 SetQuantumImageType(image,quantum_type);
762 Convert PNM raster image to pixel packets.
765 quantum_type=RGBQuantum;
766 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
767 range=GetQuantumRange(image->depth);
768 quantum_info=AcquireQuantumInfo(image_info,image);
769 if (quantum_info == (QuantumInfo *) NULL)
770 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
771 for (y=0; y < (ssize_t) image->rows; y++)
776 register const unsigned char
792 if (status == MagickFalse)
794 pixels=GetQuantumPixels(quantum_info);
796 count=ReadBlob(image,extent,pixels);
797 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
798 (image->previous == (Image *) NULL))
803 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
805 if (proceed == MagickFalse)
810 if (count != (ssize_t) extent)
812 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
813 if (q == (PixelPacket *) NULL)
819 if (image->depth == 8)
820 for (x=0; x < (ssize_t) image->columns; x++)
822 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
823 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
824 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
825 q->opacity=OpaqueOpacity;
829 if (image->depth == 16)
834 for (x=0; x < (ssize_t) image->columns; x++)
836 p=PushShortPixel(MSBEndian,p,&pixel);
837 SetRedPixelComponent(q,ScaleShortToQuantum(pixel));
838 p=PushShortPixel(MSBEndian,p,&pixel);
839 SetGreenPixelComponent(q,ScaleShortToQuantum(pixel));
840 p=PushShortPixel(MSBEndian,p,&pixel);
841 SetBluePixelComponent(q,ScaleShortToQuantum(pixel));
842 SetOpacityPixelComponent(q,OpaqueOpacity);
847 if (image->depth <= 8)
852 for (x=0; x < (ssize_t) image->columns; x++)
854 p=PushCharPixel(p,&pixel);
855 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
856 p=PushCharPixel(p,&pixel);
857 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
858 p=PushCharPixel(p,&pixel);
859 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
860 SetOpacityPixelComponent(q,OpaqueOpacity);
869 for (x=0; x < (ssize_t) image->columns; x++)
871 p=PushShortPixel(MSBEndian,p,&pixel);
872 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
873 p=PushShortPixel(MSBEndian,p,&pixel);
874 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
875 p=PushShortPixel(MSBEndian,p,&pixel);
876 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
877 SetOpacityPixelComponent(q,OpaqueOpacity);
881 if ((type == BilevelType) || (type == GrayscaleType))
883 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
884 for (x=0; x < (ssize_t) image->columns; x++)
886 if ((type == BilevelType) &&
887 (IsMonochromePixel(q) == MagickFalse))
888 type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
890 if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
892 if ((type != BilevelType) && (type != GrayscaleType))
897 sync=SyncAuthenticPixels(image,exception);
898 if (sync == MagickFalse)
901 quantum_info=DestroyQuantumInfo(quantum_info);
902 if (status == MagickFalse)
903 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
904 if (type != UndefinedType)
920 Convert PAM raster image to pixel packets.
922 range=GetQuantumRange(image->depth);
923 switch (quantum_type)
926 case GrayAlphaQuantum:
943 if (image->matte != MagickFalse)
945 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
946 quantum_info=AcquireQuantumInfo(image_info,image);
947 if (quantum_info == (QuantumInfo *) NULL)
948 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
949 for (y=0; y < (ssize_t) image->rows; y++)
954 register const unsigned char
970 if (status == MagickFalse)
972 pixels=GetQuantumPixels(quantum_info);
974 count=ReadBlob(image,extent,pixels);
975 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
976 (image->previous == (Image *) NULL))
981 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
983 if (proceed == MagickFalse)
988 if (count != (ssize_t) extent)
990 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
991 if (q == (PixelPacket *) NULL)
996 indexes=GetAuthenticIndexQueue(image);
998 if ((image->depth == 8) || (image->depth == 16))
999 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1000 quantum_type,pixels,exception);
1002 switch (quantum_type)
1005 case GrayAlphaQuantum:
1007 if (image->depth <= 8)
1012 for (x=0; x < (ssize_t) image->columns; x++)
1014 p=PushCharPixel(p,&pixel);
1015 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1016 SetGreenPixelComponent(q,GetRedPixelComponent(q));
1017 SetBluePixelComponent(q,GetRedPixelComponent(q));
1018 SetOpacityPixelComponent(q,OpaqueOpacity);
1019 if (image->matte != MagickFalse)
1021 p=PushCharPixel(p,&pixel);
1022 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1033 for (x=0; x < (ssize_t) image->columns; x++)
1035 p=PushShortPixel(MSBEndian,p,&pixel);
1036 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1037 SetGreenPixelComponent(q,GetRedPixelComponent(q));
1038 SetBluePixelComponent(q,GetRedPixelComponent(q));
1039 SetOpacityPixelComponent(q,OpaqueOpacity);
1040 if (image->matte != MagickFalse)
1042 p=PushShortPixel(MSBEndian,p,&pixel);
1043 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1054 if (image->depth <= 8)
1059 for (x=0; x < (ssize_t) image->columns; x++)
1061 p=PushCharPixel(p,&pixel);
1062 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1063 p=PushCharPixel(p,&pixel);
1064 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1065 p=PushCharPixel(p,&pixel);
1066 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1067 p=PushCharPixel(p,&pixel);
1068 SetIndexPixelComponent(indexes+x,ScaleAnyToQuantum(pixel,
1070 SetOpacityPixelComponent(q,OpaqueOpacity);
1071 if (image->matte != MagickFalse)
1073 p=PushCharPixel(p,&pixel);
1074 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1085 for (x=0; x < (ssize_t) image->columns; x++)
1087 p=PushShortPixel(MSBEndian,p,&pixel);
1088 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1089 p=PushShortPixel(MSBEndian,p,&pixel);
1090 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1091 p=PushShortPixel(MSBEndian,p,&pixel);
1092 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1093 p=PushShortPixel(MSBEndian,p,&pixel);
1094 SetIndexPixelComponent(indexes+x,ScaleAnyToQuantum(pixel,
1096 SetOpacityPixelComponent(q,OpaqueOpacity);
1097 if (image->matte != MagickFalse)
1099 p=PushShortPixel(MSBEndian,p,&pixel);
1100 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1110 if (image->depth <= 8)
1115 for (x=0; x < (ssize_t) image->columns; x++)
1117 p=PushCharPixel(p,&pixel);
1118 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1119 p=PushCharPixel(p,&pixel);
1120 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1121 p=PushCharPixel(p,&pixel);
1122 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1123 SetOpacityPixelComponent(q,OpaqueOpacity);
1124 if (image->matte != MagickFalse)
1126 p=PushCharPixel(p,&pixel);
1127 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1138 for (x=0; x < (ssize_t) image->columns; x++)
1140 p=PushShortPixel(MSBEndian,p,&pixel);
1141 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1142 p=PushShortPixel(MSBEndian,p,&pixel);
1143 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1144 p=PushShortPixel(MSBEndian,p,&pixel);
1145 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1146 SetOpacityPixelComponent(q,OpaqueOpacity);
1147 if (image->matte != MagickFalse)
1149 p=PushShortPixel(MSBEndian,p,&pixel);
1150 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
1159 sync=SyncAuthenticPixels(image,exception);
1160 if (sync == MagickFalse)
1163 quantum_info=DestroyQuantumInfo(quantum_info);
1164 if (status == MagickFalse)
1165 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1166 SetQuantumImageType(image,quantum_type);
1173 Convert PFM raster image to pixel packets.
1175 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1176 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1178 quantum_info=AcquireQuantumInfo(image_info,image);
1179 if (quantum_info == (QuantumInfo *) NULL)
1180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1181 status=SetQuantumDepth(image,quantum_info,32);
1182 if (status == MagickFalse)
1183 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1184 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1185 if (status == MagickFalse)
1186 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1187 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1188 fabs(quantum_scale));
1189 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1190 for (y=0; y < (ssize_t) image->rows; y++)
1195 register PixelPacket
1208 if (status == MagickFalse)
1210 pixels=GetQuantumPixels(quantum_info);
1212 count=ReadBlob(image,extent,pixels);
1213 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1214 (image->previous == (Image *) NULL))
1219 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1221 if (proceed == MagickFalse)
1226 if ((size_t) count != extent)
1228 q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
1229 image->columns,1,exception);
1230 if (q == (PixelPacket *) NULL)
1235 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1236 quantum_type,pixels,exception);
1237 if (length != extent)
1239 sync=SyncAuthenticPixels(image,exception);
1240 if (sync == MagickFalse)
1243 quantum_info=DestroyQuantumInfo(quantum_info);
1244 if (status == MagickFalse)
1245 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1246 SetQuantumImageType(image,quantum_type);
1250 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1252 if (EOFBlob(image) != MagickFalse)
1253 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1254 "UnexpectedEndOfFile","`%s'",image->filename);
1256 Proceed to next image.
1258 if (image_info->number_scenes != 0)
1259 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1261 if ((format == '1') || (format == '2') || (format == '3'))
1265 Skip to end of line.
1267 count=ReadBlob(image,1,(unsigned char *) &format);
1270 if ((count != 0) && (format == 'P'))
1272 } while (format != '\n');
1273 count=ReadBlob(image,1,(unsigned char *) &format);
1274 if ((count == 1) && (format == 'P'))
1277 Allocate next image structure.
1279 AcquireNextImage(image_info,image);
1280 if (GetNextImageInList(image) == (Image *) NULL)
1282 image=DestroyImageList(image);
1283 return((Image *) NULL);
1285 image=SyncNextImageInList(image);
1286 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1287 GetBlobSize(image));
1288 if (status == MagickFalse)
1291 } while ((count == 1) && (format == 'P'));
1292 (void) CloseBlob(image);
1293 return(GetFirstImageInList(image));
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % R e g i s t e r P N M I m a g e %
1305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307 % RegisterPNMImage() adds properties for the PNM image format to
1308 % the list of supported formats. The properties include the image format
1309 % tag, a method to read and/or write the format, whether the format
1310 % supports the saving of more than one frame to the same file or blob,
1311 % whether the format supports native in-memory I/O, and a brief
1312 % description of the format.
1314 % The format of the RegisterPNMImage method is:
1316 % size_t RegisterPNMImage(void)
1319 ModuleExport size_t RegisterPNMImage(void)
1324 entry=SetMagickInfo("PAM");
1325 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1326 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1327 entry->description=ConstantString("Common 2-dimensional bitmap format");
1328 entry->module=ConstantString("PNM");
1329 (void) RegisterMagickInfo(entry);
1330 entry=SetMagickInfo("PBM");
1331 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1332 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1333 entry->description=ConstantString("Portable bitmap format (black and white)");
1334 entry->module=ConstantString("PNM");
1335 (void) RegisterMagickInfo(entry);
1336 entry=SetMagickInfo("PFM");
1337 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1338 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1339 entry->endian_support=MagickTrue;
1340 entry->description=ConstantString("Portable float format");
1341 entry->module=ConstantString("PFM");
1342 (void) RegisterMagickInfo(entry);
1343 entry=SetMagickInfo("PGM");
1344 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1345 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1346 entry->description=ConstantString("Portable graymap format (gray scale)");
1347 entry->module=ConstantString("PNM");
1348 (void) RegisterMagickInfo(entry);
1349 entry=SetMagickInfo("PNM");
1350 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1351 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1352 entry->magick=(IsImageFormatHandler *) IsPNM;
1353 entry->description=ConstantString("Portable anymap");
1354 entry->module=ConstantString("PNM");
1355 (void) RegisterMagickInfo(entry);
1356 entry=SetMagickInfo("PPM");
1357 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1358 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1359 entry->description=ConstantString("Portable pixmap format (color)");
1360 entry->module=ConstantString("PNM");
1361 (void) RegisterMagickInfo(entry);
1362 return(MagickImageCoderSignature);
1366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % U n r e g i s t e r P N M I m a g e %
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % UnregisterPNMImage() removes format registrations made by the
1377 % PNM module from the list of supported formats.
1379 % The format of the UnregisterPNMImage method is:
1381 % UnregisterPNMImage(void)
1384 ModuleExport void UnregisterPNMImage(void)
1386 (void) UnregisterMagickInfo("PAM");
1387 (void) UnregisterMagickInfo("PBM");
1388 (void) UnregisterMagickInfo("PGM");
1389 (void) UnregisterMagickInfo("PNM");
1390 (void) UnregisterMagickInfo("PPM");
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 % W r i t e P N M I m a g e %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % WritePNMImage() writes an image to a file in the PNM rasterfile format.
1406 % The format of the WritePNMImage method is:
1408 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1410 % A description of each parameter follows.
1412 % o image_info: the image info.
1414 % o image: The image.
1417 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1420 buffer[MaxTextExtent],
1422 magick[MaxTextExtent];
1445 register unsigned char
1458 Open output image file.
1460 assert(image_info != (const ImageInfo *) NULL);
1461 assert(image_info->signature == MagickSignature);
1462 assert(image != (Image *) NULL);
1463 assert(image->signature == MagickSignature);
1464 if (image->debug != MagickFalse)
1465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1466 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1467 if (status == MagickFalse)
1473 Write PNM file header.
1476 quantum_type=RGBQuantum;
1477 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1490 if (image_info->compression == NoCompression)
1498 if (IsGrayImage(image,&image->exception) != MagickFalse)
1506 if (image_info->compression == NoCompression)
1513 if ((image_info->type != TrueColorType) &&
1514 (IsGrayImage(image,&image->exception) != MagickFalse))
1517 if (image_info->compression == NoCompression)
1519 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1522 if (image_info->compression == NoCompression)
1531 if (image_info->compression == NoCompression)
1536 (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
1537 (void) WriteBlobString(image,buffer);
1538 value=GetImageProperty(image,"comment");
1539 if (value != (const char *) NULL)
1545 Write comments to file.
1547 (void) WriteBlobByte(image,'#');
1548 for (p=value; *p != '\0'; p++)
1550 (void) WriteBlobByte(image,(unsigned char) *p);
1551 if ((*p == '\r') && (*(p+1) != '\0'))
1552 (void) WriteBlobByte(image,'#');
1553 if ((*p == '\n') && (*(p+1) != '\0'))
1554 (void) WriteBlobByte(image,'#');
1556 (void) WriteBlobByte(image,'\n');
1560 if (image->colorspace != RGBColorspace)
1561 (void) TransformImageColorspace(image,RGBColorspace);
1562 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
1563 (double) image->columns,(double) image->rows);
1564 (void) WriteBlobString(image,buffer);
1569 type[MaxTextExtent];
1574 (void) FormatLocaleString(buffer,MaxTextExtent,
1575 "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1577 (void) WriteBlobString(image,buffer);
1578 quantum_type=GetQuantumType(image,&image->exception);
1579 switch (quantum_type)
1585 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1589 case GrayAlphaQuantum:
1592 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1597 quantum_type=RGBQuantum;
1598 if (image->matte != MagickFalse)
1599 quantum_type=RGBAQuantum;
1601 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1605 if (image->matte != MagickFalse)
1608 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1610 if (image->depth > 16)
1612 (void) FormatLocaleString(buffer,MaxTextExtent,
1613 "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1614 GetQuantumRange(image->depth));
1615 (void) WriteBlobString(image,buffer);
1616 (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1618 (void) WriteBlobString(image,buffer);
1621 Convert runextent encoded to PNM raster pixels.
1631 Convert image to a PBM image.
1634 for (y=0; y < (ssize_t) image->rows; y++)
1636 register const PixelPacket
1642 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1643 if (p == (const PixelPacket *) NULL)
1645 for (x=0; x < (ssize_t) image->columns; x++)
1647 pixel=PixelIntensityToQuantum(p);
1648 *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
1651 if ((q-pixels+2) >= 80)
1654 (void) WriteBlob(image,q-pixels,pixels);
1659 if (image->previous == (Image *) NULL)
1661 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1663 if (status == MagickFalse)
1670 (void) WriteBlob(image,q-pixels,pixels);
1680 Convert image to a PGM image.
1682 if (image->depth <= 8)
1683 (void) WriteBlobString(image,"255\n");
1685 (void) WriteBlobString(image,"65535\n");
1687 for (y=0; y < (ssize_t) image->rows; y++)
1689 register const PixelPacket
1695 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1696 if (p == (const PixelPacket *) NULL)
1698 for (x=0; x < (ssize_t) image->columns; x++)
1700 index=PixelIntensityToQuantum(p);
1701 if (image->depth <= 8)
1702 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1703 ScaleQuantumToChar(index));
1705 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
1706 ScaleQuantumToShort(index));
1707 extent=(size_t) count;
1708 (void) strncpy((char *) q,buffer,extent);
1710 if ((q-pixels+extent) >= 80)
1713 (void) WriteBlob(image,q-pixels,pixels);
1718 if (image->previous == (Image *) NULL)
1720 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1722 if (status == MagickFalse)
1729 (void) WriteBlob(image,q-pixels,pixels);
1739 Convert image to a PNM image.
1741 if (image->depth <= 8)
1742 (void) WriteBlobString(image,"255\n");
1744 (void) WriteBlobString(image,"65535\n");
1746 for (y=0; y < (ssize_t) image->rows; y++)
1748 register const PixelPacket
1754 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1755 if (p == (const PixelPacket *) NULL)
1757 for (x=0; x < (ssize_t) image->columns; x++)
1759 if (image->depth <= 8)
1760 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1761 "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
1762 ScaleQuantumToChar(GetGreenPixelComponent(p)),
1763 ScaleQuantumToChar(GetBluePixelComponent(p)));
1765 count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
1766 "%u %u %u ",ScaleQuantumToShort(GetRedPixelComponent(p)),
1767 ScaleQuantumToShort(GetGreenPixelComponent(p)),
1768 ScaleQuantumToShort(GetBluePixelComponent(p)));
1769 extent=(size_t) count;
1770 (void) strncpy((char *) q,buffer,extent);
1772 if ((q-pixels+extent) >= 80)
1775 (void) WriteBlob(image,q-pixels,pixels);
1780 if (image->previous == (Image *) NULL)
1782 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1784 if (status == MagickFalse)
1791 (void) WriteBlob(image,q-pixels,pixels);
1798 Convert image to a PBM image.
1801 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1802 if (quantum_info == (QuantumInfo *) NULL)
1803 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1804 quantum_info->min_is_white=MagickTrue;
1805 pixels=GetQuantumPixels(quantum_info);
1806 for (y=0; y < (ssize_t) image->rows; y++)
1808 register const PixelPacket
1811 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1812 if (p == (const PixelPacket *) NULL)
1814 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1815 quantum_info,GrayQuantum,pixels,&image->exception);
1816 count=WriteBlob(image,extent,pixels);
1817 if (count != (ssize_t) extent)
1819 if (image->previous == (Image *) NULL)
1821 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1823 if (status == MagickFalse)
1827 quantum_info=DestroyQuantumInfo(quantum_info);
1836 Convert image to a PGM image.
1838 if (image->depth > 8)
1840 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1841 GetQuantumRange(image->depth));
1842 (void) WriteBlobString(image,buffer);
1843 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1844 if (quantum_info == (QuantumInfo *) NULL)
1845 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1846 quantum_info->min_is_white=MagickTrue;
1847 pixels=GetQuantumPixels(quantum_info);
1848 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1849 range=GetQuantumRange(image->depth);
1850 for (y=0; y < (ssize_t) image->rows; y++)
1852 register const PixelPacket
1858 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1859 if (p == (const PixelPacket *) NULL)
1862 if ((image->depth == 8) || (image->depth == 16))
1863 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1864 quantum_info,GrayQuantum,pixels,&image->exception);
1867 if (image->depth <= 8)
1868 for (x=0; x < (ssize_t) image->columns; x++)
1870 if (IsGrayPixel(p) == MagickFalse)
1871 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1874 if (image->depth == 8)
1875 pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
1877 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1879 q=PopCharPixel((unsigned char) pixel,q);
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 == 16)
1890 pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
1892 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1894 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1897 extent=(size_t) (q-pixels);
1899 count=WriteBlob(image,extent,pixels);
1900 if (count != (ssize_t) extent)
1902 if (image->previous == (Image *) NULL)
1904 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1906 if (status == MagickFalse)
1910 quantum_info=DestroyQuantumInfo(quantum_info);
1919 Convert image to a PNM image.
1921 if (image->depth > 8)
1923 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1924 GetQuantumRange(image->depth));
1925 (void) WriteBlobString(image,buffer);
1926 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1927 if (quantum_info == (QuantumInfo *) NULL)
1928 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1929 pixels=GetQuantumPixels(quantum_info);
1930 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1931 range=GetQuantumRange(image->depth);
1932 for (y=0; y < (ssize_t) image->rows; y++)
1934 register const PixelPacket
1940 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1941 if (p == (const PixelPacket *) NULL)
1944 if ((image->depth == 8) || (image->depth == 16))
1945 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1946 quantum_info,quantum_type,pixels,&image->exception);
1949 if (image->depth <= 8)
1950 for (x=0; x < (ssize_t) image->columns; x++)
1952 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1953 q=PopCharPixel((unsigned char) pixel,q);
1954 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
1955 q=PopCharPixel((unsigned char) pixel,q);
1956 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
1957 q=PopCharPixel((unsigned char) pixel,q);
1961 for (x=0; x < (ssize_t) image->columns; x++)
1963 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
1964 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1965 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
1966 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1967 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
1968 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1971 extent=(size_t) (q-pixels);
1973 count=WriteBlob(image,extent,pixels);
1974 if (count != (ssize_t) extent)
1976 if (image->previous == (Image *) NULL)
1978 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1980 if (status == MagickFalse)
1984 quantum_info=DestroyQuantumInfo(quantum_info);
1993 Convert image to a PAM.
1995 if (image->depth > 16)
1997 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1998 pixels=GetQuantumPixels(quantum_info);
1999 range=GetQuantumRange(image->depth);
2000 for (y=0; y < (ssize_t) image->rows; y++)
2002 register const IndexPacket
2005 register const PixelPacket
2011 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2012 if (p == (const PixelPacket *) NULL)
2014 indexes=GetVirtualIndexQueue(image);
2016 if ((image->depth == 8) || (image->depth == 16))
2017 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2018 quantum_info,quantum_type,pixels,&image->exception);
2021 switch (quantum_type)
2024 case GrayAlphaQuantum:
2026 if (image->depth <= 8)
2027 for (x=0; x < (ssize_t) image->columns; x++)
2029 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2030 q=PopCharPixel((unsigned char) pixel,q);
2031 if (image->matte != MagickFalse)
2033 pixel=(unsigned char) ScaleQuantumToAny(
2034 GetOpacityPixelComponent(p),range);
2035 q=PopCharPixel((unsigned char) pixel,q);
2040 for (x=0; x < (ssize_t) image->columns; x++)
2042 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2043 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2044 if (image->matte != MagickFalse)
2046 pixel=(unsigned char) ScaleQuantumToAny(
2047 GetOpacityPixelComponent(p),range);
2048 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2057 if (image->depth <= 8)
2058 for (x=0; x < (ssize_t) image->columns; x++)
2060 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2061 q=PopCharPixel((unsigned char) pixel,q);
2062 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2063 q=PopCharPixel((unsigned char) pixel,q);
2064 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2065 q=PopCharPixel((unsigned char) pixel,q);
2066 pixel=ScaleQuantumToAny(
2067 GetIndexPixelComponent(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 < (ssize_t) image->columns; x++)
2080 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2081 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2082 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2083 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2084 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2085 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2086 pixel=ScaleQuantumToAny(
2087 GetIndexPixelComponent(indexes+x),range);
2088 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2089 if (image->matte != MagickFalse)
2091 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2092 GetOpacityPixelComponent(p)),range);
2093 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2101 if (image->depth <= 8)
2102 for (x=0; x < (ssize_t) image->columns; x++)
2104 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2105 q=PopCharPixel((unsigned char) pixel,q);
2106 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2107 q=PopCharPixel((unsigned char) pixel,q);
2108 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2109 q=PopCharPixel((unsigned char) pixel,q);
2110 if (image->matte != MagickFalse)
2112 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2113 GetOpacityPixelComponent(p)),range);
2114 q=PopCharPixel((unsigned char) pixel,q);
2119 for (x=0; x < (ssize_t) image->columns; x++)
2121 pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
2122 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2123 pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
2124 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2125 pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
2126 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2127 if (image->matte != MagickFalse)
2129 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2130 GetOpacityPixelComponent(p)),range);
2131 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2138 extent=(size_t) (q-pixels);
2140 count=WriteBlob(image,extent,pixels);
2141 if (count != (ssize_t) extent)
2143 if (image->previous == (Image *) NULL)
2145 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2147 if (status == MagickFalse)
2151 quantum_info=DestroyQuantumInfo(quantum_info);
2157 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2160 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2161 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2162 if (quantum_info == (QuantumInfo *) NULL)
2163 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2164 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2165 if (status == MagickFalse)
2166 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2167 pixels=GetQuantumPixels(quantum_info);
2168 for (y=(ssize_t) image->rows-1; y >= 0; y--)
2170 register const PixelPacket
2173 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2174 if (p == (const PixelPacket *) NULL)
2176 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2177 quantum_info,quantum_type,pixels,&image->exception);
2178 (void) WriteBlob(image,extent,pixels);
2179 if (image->previous == (Image *) NULL)
2181 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2183 if (status == MagickFalse)
2187 quantum_info=DestroyQuantumInfo(quantum_info);
2191 if (GetNextImageInList(image) == (Image *) NULL)
2193 image=SyncNextImageInList(image);
2194 status=SetImageProgress(image,SaveImagesTag,scene++,
2195 GetImageListLength(image));
2196 if (status == MagickFalse)
2198 } while (image_info->adjoin != MagickFalse);
2199 (void) CloseBlob(image);