2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write PBMPlus Portable Anymap Image Format %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/module.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/pixel-private.h"
60 #include "magick/property.h"
61 #include "magick/quantum-private.h"
62 #include "magick/static.h"
63 #include "magick/statistic.h"
64 #include "magick/string_.h"
65 #include "magick/string-private.h"
70 static MagickBooleanType
71 WritePNMImage(const ImageInfo *,Image *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % IsPNM() returns MagickTrue if the image format type, identified by the
85 % magick string, is PNM.
87 % The format of the IsPNM method is:
89 % MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
91 % A description of each parameter follows:
93 % o magick: compare image format pattern against these bytes.
95 % o extent: Specifies the extent of the magick string.
98 static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
102 if ((*magick == (unsigned char) 'P') &&
103 ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
104 (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
105 (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f')))
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % R e a d P N M I m a g e %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % ReadPNMImage() reads a Portable Anymap image file and returns it.
122 % It allocates the memory necessary for the new Image structure and returns
123 % a pointer to the new image.
125 % The format of the ReadPNMImage method is:
127 % Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 % A description of each parameter follows:
131 % o image_info: the image info.
133 % o exception: return any errors or warnings in this structure.
137 static inline long ConstrainPixel(Image *image,const long offset,
138 const unsigned long extent)
140 if ((offset < 0) || (offset > (long) extent))
142 (void) ThrowMagickException(&image->exception,GetMagickModule(),
143 CorruptImageError,"InvalidPixel","`%s'",image->filename);
149 static unsigned long PNMInteger(Image *image,const unsigned int base)
167 Skip any leading whitespace.
169 extent=MaxTextExtent;
170 comment=(char *) NULL;
174 c=ReadBlobByte(image);
182 if (comment == (char *) NULL)
183 comment=AcquireString((char *) NULL);
184 p=comment+strlen(comment);
185 for ( ; (c != EOF) && (c != (int) '\n'); p++)
187 if ((size_t) (p-comment+1) >= extent)
190 comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
192 if (comment == (char *) NULL)
194 p=comment+strlen(comment);
196 c=ReadBlobByte(image);
200 if (comment == (char *) NULL)
204 } while (isdigit(c) == MagickFalse);
205 if (comment != (char *) NULL)
207 (void) SetImageProperty(image,"comment",comment);
208 comment=DestroyString(comment);
211 return((unsigned long) (c-(int) '0'));
220 c=ReadBlobByte(image);
223 } while (isdigit(c) != MagickFalse);
227 static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
274 assert(image_info != (const ImageInfo *) NULL);
275 assert(image_info->signature == MagickSignature);
276 if (image_info->debug != MagickFalse)
277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
278 image_info->filename);
279 assert(exception != (ExceptionInfo *) NULL);
280 assert(exception->signature == MagickSignature);
281 image=AcquireImage(image_info);
282 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
283 if (status == MagickFalse)
285 image=DestroyImageList(image);
286 return((Image *) NULL);
291 count=ReadBlob(image,1,(unsigned char *) &format);
295 Initialize image structure.
297 if ((count != 1) || (format != 'P'))
298 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
300 quantum_type=RGBQuantum;
302 format=(char) ReadBlobByte(image);
306 PBM, PGM, PPM, and PNM.
308 image->columns=PNMInteger(image,10);
309 image->rows=PNMInteger(image,10);
310 if ((format == 'f') || (format == 'F'))
313 scale[MaxTextExtent];
315 (void) ReadBlobString(image,scale);
316 quantum_scale=StringToDouble(scale);
320 if ((format == '1') || (format == '4'))
321 max_value=1; /* bitmap */
323 max_value=PNMInteger(image,10);
329 keyword[MaxTextExtent],
330 value[MaxTextExtent];
341 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
343 while (isspace((int) ((unsigned char) c)) != 0)
344 c=ReadBlobByte(image);
348 if ((size_t) (p-keyword) < (MaxTextExtent-1))
350 c=ReadBlobByte(image);
351 } while (isalnum(c));
353 if (LocaleCompare(keyword,"endhdr") == 0)
355 while (isspace((int) ((unsigned char) c)) != 0)
356 c=ReadBlobByte(image);
358 while (isalnum(c) || (c == '_'))
360 if ((size_t) (p-value) < (MaxTextExtent-1))
362 c=ReadBlobByte(image);
366 Assign a value to the specified keyword.
368 if (LocaleCompare(keyword,"depth") == 0)
369 packet_size=StringToUnsignedLong(value);
370 if (LocaleCompare(keyword,"height") == 0)
371 image->rows=StringToUnsignedLong(value);
372 if (LocaleCompare(keyword,"maxval") == 0)
373 max_value=StringToUnsignedLong(value);
374 if (LocaleCompare(keyword,"TUPLTYPE") == 0)
376 if (LocaleCompare(value,"BLACKANDWHITE") == 0)
377 quantum_type=GrayQuantum;
378 if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
380 quantum_type=GrayAlphaQuantum;
381 image->matte=MagickTrue;
383 if (LocaleCompare(value,"GRAYSCALE") == 0)
384 quantum_type=GrayQuantum;
385 if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
387 quantum_type=GrayAlphaQuantum;
388 image->matte=MagickTrue;
390 if (LocaleCompare(value,"RGB_ALPHA") == 0)
392 quantum_type=RGBAQuantum;
393 image->matte=MagickTrue;
395 if (LocaleCompare(value,"CMYK") == 0)
397 quantum_type=CMYKQuantum;
398 image->colorspace=CMYKColorspace;
400 if (LocaleCompare(value,"CMYK_ALPHA") == 0)
402 quantum_type=CMYKAQuantum;
403 image->colorspace=CMYKColorspace;
404 image->matte=MagickTrue;
407 if (LocaleCompare(keyword,"width") == 0)
408 image->columns=StringToUnsignedLong(value);
411 if ((image->columns == 0) || (image->rows == 0))
412 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
413 if (max_value >= 65536)
414 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
415 for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
417 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
418 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
421 Convert PNM pixels to runextent-encoded MIFF packets.
430 Convert PBM image to pixel packets.
432 for (y=0; y < (long) image->rows; y++)
440 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
441 if (q == (PixelPacket *) NULL)
443 for (x=0; x < (long) image->columns; x++)
445 q->red=(Quantum) (PNMInteger(image,2) == 0 ? QuantumRange : 0);
450 if (SyncAuthenticPixels(image,exception) == MagickFalse)
452 if (image->previous == (Image *) NULL)
454 status=SetImageProgress(image,LoadImageTag,y,image->rows);
455 if (status == MagickFalse)
459 image->type=BilevelType;
468 Convert PGM image to pixel packets.
470 scale=(Quantum *) NULL;
471 if (max_value != (1U*QuantumRange))
474 Compute pixel scaling table.
476 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
478 if (scale == (Quantum *) NULL)
479 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
480 for (i=0; i <= (long) max_value; i++)
481 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
483 for (y=0; y < (long) image->rows; y++)
491 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
492 if (q == (PixelPacket *) NULL)
494 for (x=0; x < (long) image->columns; x++)
496 intensity=PNMInteger(image,10);
497 if (scale != (Quantum *) NULL)
498 intensity=(unsigned long) scale[ConstrainPixel(image,(long)
499 intensity,max_value)];
500 q->red=(Quantum) intensity;
505 if (SyncAuthenticPixels(image,exception) == MagickFalse)
507 if (image->previous == (Image *) NULL)
509 status=SetImageProgress(image,LoadImageTag,y,image->rows);
510 if (status == MagickFalse)
514 image->type=GrayscaleType;
515 if (scale != (Quantum *) NULL)
516 scale=(Quantum *) RelinquishMagickMemory(scale);
525 Convert PNM image to pixel packets.
527 scale=(Quantum *) NULL;
528 if (max_value != (1U*QuantumRange))
531 Compute pixel scaling table.
533 scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
535 if (scale == (Quantum *) NULL)
536 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
537 for (i=0; i <= (long) max_value; i++)
538 scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
540 for (y=0; y < (long) image->rows; y++)
548 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
549 if (q == (PixelPacket *) NULL)
551 for (x=0; x < (long) image->columns; x++)
553 pixel.red=(MagickRealType) PNMInteger(image,10);
554 pixel.green=(MagickRealType) PNMInteger(image,10);
555 pixel.blue=(MagickRealType) PNMInteger(image,10);
556 if (scale != (Quantum *) NULL)
558 pixel.red=(MagickRealType) scale[ConstrainPixel(image,(long)
559 pixel.red,max_value)];
560 pixel.green=(MagickRealType) scale[ConstrainPixel(image,(long)
561 pixel.green,max_value)];
562 pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(long)
563 pixel.blue,max_value)];
565 q->red=(Quantum) pixel.red;
566 q->green=(Quantum) pixel.green;
567 q->blue=(Quantum) pixel.blue;
570 if (SyncAuthenticPixels(image,exception) == MagickFalse)
572 if (image->previous == (Image *) NULL)
574 status=SetImageProgress(image,LoadImageTag,y,image->rows);
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 image_view=AcquireCacheView(image);
597 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
598 #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
600 for (y=0; y < (long) image->rows; y++)
620 if (status == MagickFalse)
622 pixels=GetQuantumPixels(quantum_info);
623 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
624 #pragma omp critical (MagickCore_ReadPNMImage)
627 count=ReadBlob(image,extent,pixels);
628 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
629 (image->previous == (Image *) NULL))
634 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
635 if (proceed == MagickFalse)
640 if (count != (ssize_t) extent)
642 q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
644 if (q == (PixelPacket *) NULL)
649 length=ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
651 if (length != extent)
653 sync=SyncCacheViewAuthenticPixels(image_view,exception);
654 if (sync == MagickFalse)
657 image_view=DestroyCacheView(image_view);
658 quantum_info=DestroyQuantumInfo(quantum_info);
659 if (status == MagickFalse)
660 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
661 SetQuantumImageType(image,quantum_type);
670 Convert PGM raw image to pixel packets.
672 range=GetQuantumRange(image->depth);
673 quantum_type=GrayQuantum;
674 extent=(image->depth <= 8 ? 1 : 2)*image->columns;
675 quantum_info=AcquireQuantumInfo(image_info,image);
676 if (quantum_info == (QuantumInfo *) NULL)
677 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
678 image_view=AcquireCacheView(image);
679 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
680 #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
682 for (y=0; y < (long) image->rows; y++)
690 register const unsigned char
705 if (status == MagickFalse)
707 pixels=GetQuantumPixels(quantum_info);
708 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
709 #pragma omp critical (MagickCore_ReadPNMImage)
712 count=ReadBlob(image,extent,pixels);
713 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
714 (image->previous == (Image *) NULL))
719 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
720 if (proceed == MagickFalse)
725 if (count != (ssize_t) extent)
727 q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
729 if (q == (PixelPacket *) NULL)
735 if ((image->depth == 8) || (image->depth == 16))
736 (void) ImportQuantumPixels(image,image_view,quantum_info,
737 quantum_type,pixels,exception);
739 if (image->depth <= 8)
744 for (x=0; x < (long) image->columns; x++)
746 p=PushCharPixel(p,&pixel);
747 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
758 for (x=0; x < (long) image->columns; x++)
760 p=PushShortPixel(MSBEndian,p,&pixel);
761 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
767 sync=SyncCacheViewAuthenticPixels(image_view,exception);
768 if (sync == MagickFalse)
771 image_view=DestroyCacheView(image_view);
772 quantum_info=DestroyQuantumInfo(quantum_info);
773 if (status == MagickFalse)
774 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
775 SetQuantumImageType(image,quantum_type);
787 Convert PNM raster image to pixel packets.
790 quantum_type=RGBQuantum;
791 extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
792 range=GetQuantumRange(image->depth);
793 quantum_info=AcquireQuantumInfo(image_info,image);
794 if (quantum_info == (QuantumInfo *) NULL)
795 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
796 image_view=AcquireCacheView(image);
797 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
798 #pragma omp parallel for schedule(static,1) shared(row,status,type)
800 for (y=0; y < (long) image->rows; y++)
808 register const unsigned char
826 if (status == MagickFalse)
828 pixels=GetQuantumPixels(quantum_info);
829 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
830 #pragma omp critical (MagickCore_ReadPNMImage)
833 count=ReadBlob(image,extent,pixels);
834 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
835 (image->previous == (Image *) NULL))
840 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
841 if (proceed == MagickFalse)
846 if (count != (ssize_t) extent)
848 q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
850 if (q == (PixelPacket *) NULL)
856 if ((image->depth == 8) || (image->depth == 16))
858 length=ImportQuantumPixels(image,image_view,quantum_info,
859 quantum_type,pixels,exception);
860 if (length != extent)
864 if (image->depth <= 8)
873 for (x=0; x < (long) image->columns; x++)
875 p=PushCharPixel(p,&pixel);
876 r->red=ScaleAnyToQuantum(pixel,range);
877 p=PushCharPixel(p,&pixel);
878 r->green=ScaleAnyToQuantum(pixel,range);
879 p=PushCharPixel(p,&pixel);
880 r->blue=ScaleAnyToQuantum(pixel,range);
893 for (x=0; x < (long) image->columns; x++)
895 p=PushShortPixel(MSBEndian,p,&pixel);
896 r->red=ScaleAnyToQuantum(pixel,range);
897 p=PushShortPixel(MSBEndian,p,&pixel);
898 r->green=ScaleAnyToQuantum(pixel,range);
899 p=PushShortPixel(MSBEndian,p,&pixel);
900 r->blue=ScaleAnyToQuantum(pixel,range);
904 if ((type == BilevelType) || (type == GrayscaleType))
905 for (x=0; x < (long) image->columns; x++)
907 if ((type == BilevelType) &&
908 (IsMonochromePixel(q) == MagickFalse))
909 type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
911 if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
913 if ((type != BilevelType) && (type != GrayscaleType))
917 sync=SyncCacheViewAuthenticPixels(image_view,exception);
918 if (sync == MagickFalse)
921 image_view=DestroyCacheView(image_view);
922 quantum_info=DestroyQuantumInfo(quantum_info);
923 if (status == MagickFalse)
924 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
925 if (type != UndefinedType)
941 Convert PAM raster image to pixel packets.
943 range=GetQuantumRange(image->depth);
944 switch (quantum_type)
947 case GrayAlphaQuantum:
964 if (image->matte != MagickFalse)
966 extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
967 quantum_info=AcquireQuantumInfo(image_info,image);
968 if (quantum_info == (QuantumInfo *) NULL)
969 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
970 image_view=AcquireCacheView(image);
971 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
972 #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
974 for (y=0; y < (long) image->rows; y++)
982 register const unsigned char
997 if (status == MagickFalse)
999 pixels=GetQuantumPixels(quantum_info);
1000 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
1001 #pragma omp critical (MagickCore_ReadPNMImage)
1004 count=ReadBlob(image,extent,pixels);
1005 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1006 (image->previous == (Image *) NULL))
1011 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
1012 if (proceed == MagickFalse)
1017 if (count != (ssize_t) extent)
1019 q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
1021 if (q == (PixelPacket *) NULL)
1026 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1028 if ((image->depth == 8) || (image->depth == 16))
1029 (void) ImportQuantumPixels(image,image_view,quantum_info,
1030 quantum_type,pixels,exception);
1032 switch (quantum_type)
1035 case GrayAlphaQuantum:
1037 if (image->depth <= 8)
1042 for (x=0; x < (long) image->columns; x++)
1044 p=PushCharPixel(p,&pixel);
1045 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1048 SetOpacityPixelComponent(q,OpaqueOpacity);
1049 if (image->matte != MagickFalse)
1051 p=PushCharPixel(p,&pixel);
1052 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1062 for (x=0; x < (long) image->columns; x++)
1064 p=PushShortPixel(MSBEndian,p,&pixel);
1065 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1068 SetOpacityPixelComponent(q,OpaqueOpacity);
1069 if (image->matte != MagickFalse)
1071 p=PushShortPixel(MSBEndian,p,&pixel);
1072 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1082 if (image->depth <= 8)
1087 for (x=0; x < (long) image->columns; x++)
1089 p=PushCharPixel(p,&pixel);
1090 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1091 p=PushCharPixel(p,&pixel);
1092 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1093 p=PushCharPixel(p,&pixel);
1094 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1095 p=PushCharPixel(p,&pixel);
1096 indexes[x]=ScaleAnyToQuantum(pixel,range);
1097 SetOpacityPixelComponent(q,OpaqueOpacity);
1098 if (image->matte != MagickFalse)
1100 p=PushCharPixel(p,&pixel);
1101 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1111 for (x=0; x < (long) image->columns; x++)
1113 p=PushShortPixel(MSBEndian,p,&pixel);
1114 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1115 p=PushShortPixel(MSBEndian,p,&pixel);
1116 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1117 p=PushShortPixel(MSBEndian,p,&pixel);
1118 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1119 p=PushShortPixel(MSBEndian,p,&pixel);
1120 indexes[x]=ScaleAnyToQuantum(pixel,range);
1121 SetOpacityPixelComponent(q,OpaqueOpacity);
1122 if (image->matte != MagickFalse)
1124 p=PushShortPixel(MSBEndian,p,&pixel);
1125 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1134 if (image->depth <= 8)
1139 for (x=0; x < (long) image->columns; x++)
1141 p=PushCharPixel(p,&pixel);
1142 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1143 p=PushCharPixel(p,&pixel);
1144 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1145 p=PushCharPixel(p,&pixel);
1146 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1147 SetOpacityPixelComponent(q,OpaqueOpacity);
1148 if (image->matte != MagickFalse)
1150 p=PushCharPixel(p,&pixel);
1151 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1161 for (x=0; x < (long) image->columns; x++)
1163 p=PushShortPixel(MSBEndian,p,&pixel);
1164 SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1165 p=PushShortPixel(MSBEndian,p,&pixel);
1166 SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1167 p=PushShortPixel(MSBEndian,p,&pixel);
1168 SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
1169 SetOpacityPixelComponent(q,OpaqueOpacity);
1170 if (image->matte != MagickFalse)
1172 p=PushShortPixel(MSBEndian,p,&pixel);
1173 SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,range));
1181 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1182 if (sync == MagickFalse)
1185 image_view=DestroyCacheView(image_view);
1186 quantum_info=DestroyQuantumInfo(quantum_info);
1187 if (status == MagickFalse)
1188 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1189 SetQuantumImageType(image,quantum_type);
1196 Convert PFM raster image to pixel packets.
1198 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
1199 image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1201 quantum_info=AcquireQuantumInfo(image_info,image);
1202 if (quantum_info == (QuantumInfo *) NULL)
1203 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1204 status=SetQuantumDepth(image,quantum_info,32);
1205 if (status == MagickFalse)
1206 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1207 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1208 if (status == MagickFalse)
1209 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1210 SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
1211 fabs(quantum_scale));
1212 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1213 image_view=AcquireCacheView(image);
1214 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
1215 #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
1217 for (y=0; y < (long) image->rows; y++)
1225 register PixelPacket
1237 if (status == MagickFalse)
1239 pixels=GetQuantumPixels(quantum_info);
1240 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
1241 #pragma omp critical (MagickCore_ReadPNMImage)
1244 count=ReadBlob(image,extent,pixels);
1245 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1246 (image->previous == (Image *) NULL))
1251 proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
1252 if (proceed == MagickFalse)
1257 if ((size_t) count != extent)
1259 q=QueueCacheViewAuthenticPixels(image_view,0,(long) (image->rows-
1260 offset-1),image->columns,1,exception);
1261 if (q == (PixelPacket *) NULL)
1266 length=ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
1268 if (length != extent)
1270 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1271 if (sync == MagickFalse)
1274 image_view=DestroyCacheView(image_view);
1275 quantum_info=DestroyQuantumInfo(quantum_info);
1276 if (status == MagickFalse)
1277 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1278 SetQuantumImageType(image,quantum_type);
1282 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1284 if (EOFBlob(image) != MagickFalse)
1285 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1286 "UnexpectedEndOfFile","`%s'",image->filename);
1288 Proceed to next image.
1290 if (image_info->number_scenes != 0)
1291 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1293 if ((format == '1') || (format == '2') || (format == '3'))
1297 Skip to end of line.
1299 count=ReadBlob(image,1,(unsigned char *) &format);
1302 if ((count != 0) && (format == 'P'))
1304 } while (format != '\n');
1305 count=ReadBlob(image,1,(unsigned char *) &format);
1306 if ((count == 1) && (format == 'P'))
1309 Allocate next image structure.
1311 AcquireNextImage(image_info,image);
1312 if (GetNextImageInList(image) == (Image *) NULL)
1314 image=DestroyImageList(image);
1315 return((Image *) NULL);
1317 image=SyncNextImageInList(image);
1318 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1319 GetBlobSize(image));
1320 if (status == MagickFalse)
1323 } while ((count == 1) && (format == 'P'));
1324 (void) CloseBlob(image);
1325 return(GetFirstImageInList(image));
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1333 % R e g i s t e r P N M I m a g e %
1337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1339 % RegisterPNMImage() adds properties for the PNM image format to
1340 % the list of supported formats. The properties include the image format
1341 % tag, a method to read and/or write the format, whether the format
1342 % supports the saving of more than one frame to the same file or blob,
1343 % whether the format supports native in-memory I/O, and a brief
1344 % description of the format.
1346 % The format of the RegisterPNMImage method is:
1348 % unsigned long RegisterPNMImage(void)
1351 ModuleExport unsigned long RegisterPNMImage(void)
1356 entry=SetMagickInfo("PAM");
1357 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1358 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1359 entry->description=ConstantString("Common 2-dimensional bitmap format");
1360 entry->module=ConstantString("PNM");
1361 (void) RegisterMagickInfo(entry);
1362 entry=SetMagickInfo("PBM");
1363 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1364 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1365 entry->description=ConstantString("Portable bitmap format (black and white)");
1366 entry->module=ConstantString("PNM");
1367 (void) RegisterMagickInfo(entry);
1368 entry=SetMagickInfo("PFM");
1369 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1370 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1371 entry->description=ConstantString("Portable float format");
1372 entry->module=ConstantString("PFM");
1373 (void) RegisterMagickInfo(entry);
1374 entry=SetMagickInfo("PGM");
1375 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1376 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1377 entry->description=ConstantString("Portable graymap format (gray scale)");
1378 entry->module=ConstantString("PNM");
1379 (void) RegisterMagickInfo(entry);
1380 entry=SetMagickInfo("PNM");
1381 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1382 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1383 entry->magick=(IsImageFormatHandler *) IsPNM;
1384 entry->description=ConstantString("Portable anymap");
1385 entry->module=ConstantString("PNM");
1386 (void) RegisterMagickInfo(entry);
1387 entry=SetMagickInfo("PPM");
1388 entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1389 entry->encoder=(EncodeImageHandler *) WritePNMImage;
1390 entry->description=ConstantString("Portable pixmap format (color)");
1391 entry->module=ConstantString("PNM");
1392 (void) RegisterMagickInfo(entry);
1393 return(MagickImageCoderSignature);
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 % U n r e g i s t e r P N M I m a g e %
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 % UnregisterPNMImage() removes format registrations made by the
1408 % PNM module from the list of supported formats.
1410 % The format of the UnregisterPNMImage method is:
1412 % UnregisterPNMImage(void)
1415 ModuleExport void UnregisterPNMImage(void)
1417 (void) UnregisterMagickInfo("PAM");
1418 (void) UnregisterMagickInfo("PBM");
1419 (void) UnregisterMagickInfo("PGM");
1420 (void) UnregisterMagickInfo("PNM");
1421 (void) UnregisterMagickInfo("PPM");
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429 % W r i t e P N M I m a g e %
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 % Procedure WritePNMImage() writes an image to a file in the PNM rasterfile
1438 % The format of the WritePNMImage method is:
1440 % MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1442 % A description of each parameter follows.
1444 % o image_info: the image info.
1446 % o image: The image.
1449 static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
1452 buffer[MaxTextExtent],
1454 magick[MaxTextExtent];
1480 register unsigned char
1492 Open output image file.
1494 assert(image_info != (const ImageInfo *) NULL);
1495 assert(image_info->signature == MagickSignature);
1496 assert(image != (Image *) NULL);
1497 assert(image->signature == MagickSignature);
1498 if (image->debug != MagickFalse)
1499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1500 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1501 if (status == MagickFalse)
1507 Write PNM file header.
1510 quantum_type=RGBQuantum;
1511 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
1524 if (image_info->compression == NoCompression)
1532 if (IsGrayImage(image,&image->exception) != MagickFalse)
1540 if (image_info->compression == NoCompression)
1547 if ((image_info->type != TrueColorType) &&
1548 (IsGrayImage(image,&image->exception) != MagickFalse))
1551 if (image_info->compression == NoCompression)
1553 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1556 if (image_info->compression == NoCompression)
1565 if (image_info->compression == NoCompression)
1570 (void) FormatMagickString(buffer,MaxTextExtent,"P%c\n",format);
1571 (void) WriteBlobString(image,buffer);
1572 value=GetImageProperty(image,"comment");
1573 if (value != (const char *) NULL)
1579 Write comments to file.
1581 (void) WriteBlobByte(image,'#');
1582 for (p=value; *p != '\0'; p++)
1584 (void) WriteBlobByte(image,(unsigned char) *p);
1585 if ((*p == '\r') && (*(p+1) != '\0'))
1586 (void) WriteBlobByte(image,'#');
1587 if ((*p == '\n') && (*(p+1) != '\0'))
1588 (void) WriteBlobByte(image,'#');
1590 (void) WriteBlobByte(image,'\n');
1594 if (image->colorspace != RGBColorspace)
1595 (void) TransformImageColorspace(image,RGBColorspace);
1596 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n",
1597 image->columns,image->rows);
1598 (void) WriteBlobString(image,buffer);
1603 type[MaxTextExtent];
1608 (void) FormatMagickString(buffer,MaxTextExtent,
1609 "WIDTH %lu\nHEIGHT %lu\n",image->columns,image->rows);
1610 (void) WriteBlobString(image,buffer);
1611 quantum_type=GetQuantumType(image,&image->exception);
1612 switch (quantum_type)
1618 (void) CopyMagickString(type,"CMYK",MaxTextExtent);
1622 case GrayAlphaQuantum:
1625 (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
1630 quantum_type=RGBQuantum;
1631 if (image->matte != MagickFalse)
1632 quantum_type=RGBAQuantum;
1634 (void) CopyMagickString(type,"RGB",MaxTextExtent);
1638 if (image->matte != MagickFalse)
1641 (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
1643 if (image->depth > 16)
1645 (void) FormatMagickString(buffer,MaxTextExtent,
1646 "DEPTH %lu\nMAXVAL %lu\n",(unsigned long) packet_size,(unsigned long)
1647 GetQuantumRange(image->depth));
1648 (void) WriteBlobString(image,buffer);
1649 (void) FormatMagickString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
1651 (void) WriteBlobString(image,buffer);
1654 Convert runextent encoded to PNM raster pixels.
1664 Convert image to a PBM image.
1666 pixels=AcquireString((const char *) NULL);
1667 for (y=0; y < (long) image->rows; y++)
1669 register const IndexPacket
1672 register const PixelPacket
1678 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1679 if (p == (const PixelPacket *) NULL)
1681 indexes=GetVirtualIndexQueue(image);
1683 for (x=0; x < (long) image->columns; x++)
1685 pixel=PixelIntensityToQuantum(p);
1686 (void) ConcatenateString(&pixels,
1687 pixel >= (Quantum) (QuantumRange/2) ? "0 " : "1 ");
1690 (void) ConcatenateString(&pixels,"\n");
1691 (void) WriteBlob(image,strlen(pixels),(unsigned char *) pixels);
1692 if (image->previous == (Image *) NULL)
1694 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1695 if (status == MagickFalse)
1699 pixels=DestroyString(pixels);
1708 Convert image to a PGM image.
1710 if (image->depth <= 8)
1711 (void) WriteBlobString(image,"255\n");
1713 (void) WriteBlobString(image,"65535\n");
1714 pixels=AcquireString((const char *) NULL);
1715 for (y=0; y < (long) image->rows; y++)
1717 register const PixelPacket
1723 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1724 if (p == (const PixelPacket *) NULL)
1727 for (x=0; x < (long) image->columns; x++)
1729 index=PixelIntensityToQuantum(p);
1730 if (image->depth <= 8)
1731 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1732 ScaleQuantumToChar(index));
1734 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
1735 ScaleQuantumToShort(index));
1736 (void) ConcatenateString(&pixels,buffer);
1739 (void) ConcatenateString(&pixels,"\n");
1740 (void) WriteBlob(image,strlen(pixels),(unsigned char *) pixels);
1741 if (image->previous == (Image *) NULL)
1743 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1744 if (status == MagickFalse)
1748 pixels=DestroyString(pixels);
1757 Convert image to a PNM image.
1759 if (image->depth <= 8)
1760 (void) WriteBlobString(image,"255\n");
1762 (void) WriteBlobString(image,"65535\n");
1763 pixels=AcquireString((const char *) NULL);
1764 for (y=0; y < (long) image->rows; y++)
1766 register const PixelPacket
1772 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1773 if (p == (const PixelPacket *) NULL)
1776 for (x=0; x < (long) image->columns; x++)
1778 if (image->depth <= 8)
1779 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1780 "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
1781 ScaleQuantumToChar(GetGreenPixelComponent(p)),
1782 ScaleQuantumToChar(GetBluePixelComponent(p)));
1784 count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
1785 "%u %u %u ",ScaleQuantumToShort(GetRedPixelComponent(p)),
1786 ScaleQuantumToShort(GetGreenPixelComponent(p)),
1787 ScaleQuantumToShort(GetBluePixelComponent(p)));
1788 (void) ConcatenateString(&pixels,buffer);
1791 (void) ConcatenateString(&pixels,"\n");
1792 (void) WriteBlob(image,strlen(pixels),(unsigned char *) pixels);
1793 if (image->previous == (Image *) NULL)
1795 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1796 if (status == MagickFalse)
1800 pixels=DestroyString(pixels);
1806 Convert image to a PBM image.
1809 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1810 if (quantum_info == (QuantumInfo *) NULL)
1811 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1812 quantum_info->min_is_white=MagickTrue;
1813 pixels=GetQuantumPixels(quantum_info);
1814 for (y=0; y < (long) image->rows; y++)
1816 register const PixelPacket
1819 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1820 if (p == (const PixelPacket *) NULL)
1822 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1823 quantum_info,GrayQuantum,pixels,&image->exception);
1824 count=WriteBlob(image,extent,pixels);
1825 if (count != (ssize_t) extent)
1827 if (image->previous == (Image *) NULL)
1829 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1830 if (status == MagickFalse)
1834 quantum_info=DestroyQuantumInfo(quantum_info);
1843 Convert image to a PGM image.
1845 if (image->depth > 8)
1847 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
1848 GetQuantumRange(image->depth));
1849 (void) WriteBlobString(image,buffer);
1850 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1851 if (quantum_info == (QuantumInfo *) NULL)
1852 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1853 quantum_info->min_is_white=MagickTrue;
1854 pixels=GetQuantumPixels(quantum_info);
1855 extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
1856 range=GetQuantumRange(image->depth);
1857 for (y=0; y < (long) image->rows; y++)
1859 register const PixelPacket
1865 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1866 if (p == (const PixelPacket *) NULL)
1869 if ((image->depth == 8) || (image->depth == 16))
1870 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1871 quantum_info,GrayQuantum,pixels,&image->exception);
1874 if (image->depth <= 8)
1875 for (x=0; x < (long) image->columns; x++)
1877 if (IsGrayPixel(p) == MagickFalse)
1878 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1881 if (image->depth == 8)
1882 pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
1884 pixel=ScaleQuantumToAny(p->red,range);
1886 q=PopCharPixel((unsigned char) pixel,q);
1890 for (x=0; x < (long) image->columns; x++)
1892 if (IsGrayPixel(p) == MagickFalse)
1893 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
1896 if (image->depth == 16)
1897 pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
1899 pixel=ScaleQuantumToAny(p->red,range);
1901 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1904 extent=(size_t) (q-pixels);
1906 count=WriteBlob(image,extent,pixels);
1907 if (count != (ssize_t) extent)
1909 if (image->previous == (Image *) NULL)
1911 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1912 if (status == MagickFalse)
1916 quantum_info=DestroyQuantumInfo(quantum_info);
1925 Convert image to a PNM image.
1927 if (image->depth > 8)
1929 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
1930 GetQuantumRange(image->depth));
1931 (void) WriteBlobString(image,buffer);
1932 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
1933 if (quantum_info == (QuantumInfo *) NULL)
1934 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1935 pixels=GetQuantumPixels(quantum_info);
1936 extent=GetQuantumExtent(image,quantum_info,quantum_type);
1937 range=GetQuantumRange(image->depth);
1938 for (y=0; y < (long) image->rows; y++)
1940 register const PixelPacket
1946 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1947 if (p == (const PixelPacket *) NULL)
1950 if ((image->depth == 8) || (image->depth == 16))
1951 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
1952 quantum_info,quantum_type,pixels,&image->exception);
1955 if (image->depth <= 8)
1956 for (x=0; x < (long) image->columns; x++)
1958 pixel=ScaleQuantumToAny(p->red,range);
1959 q=PopCharPixel((unsigned char) pixel,q);
1960 pixel=ScaleQuantumToAny(p->green,range);
1961 q=PopCharPixel((unsigned char) pixel,q);
1962 pixel=ScaleQuantumToAny(p->blue,range);
1963 q=PopCharPixel((unsigned char) pixel,q);
1964 if (image->matte != MagickFalse)
1966 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
1967 GetOpacityPixelComponent(p)),range);
1968 q=PopCharPixel((unsigned char) pixel,q);
1973 for (x=0; x < (long) image->columns; x++)
1975 pixel=ScaleQuantumToAny(p->red,range);
1976 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1977 pixel=ScaleQuantumToAny(p->green,range);
1978 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1979 pixel=ScaleQuantumToAny(p->blue,range);
1980 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1981 if (image->matte != MagickFalse)
1983 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
1984 GetOpacityPixelComponent(p)),range);
1985 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
1989 extent=(size_t) (q-pixels);
1991 count=WriteBlob(image,extent,pixels);
1992 if (count != (ssize_t) extent)
1994 if (image->previous == (Image *) NULL)
1996 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1997 if (status == MagickFalse)
2001 quantum_info=DestroyQuantumInfo(quantum_info);
2010 Convert image to a PAM.
2012 if (image->depth > 16)
2014 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2015 pixels=GetQuantumPixels(quantum_info);
2016 range=GetQuantumRange(image->depth);
2017 for (y=0; y < (long) image->rows; y++)
2019 register const IndexPacket
2022 register const PixelPacket
2028 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2029 if (p == (const PixelPacket *) NULL)
2031 indexes=GetVirtualIndexQueue(image);
2033 if ((image->depth == 8) || (image->depth == 16))
2034 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2035 quantum_info,quantum_type,pixels,&image->exception);
2038 switch (quantum_type)
2041 case GrayAlphaQuantum:
2043 if (image->depth <= 8)
2044 for (x=0; x < (long) image->columns; x++)
2046 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2047 q=PopCharPixel((unsigned char) pixel,q);
2048 if (image->matte != MagickFalse)
2050 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2052 q=PopCharPixel((unsigned char) pixel,q);
2057 for (x=0; x < (long) image->columns; x++)
2059 pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
2060 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2061 if (image->matte != MagickFalse)
2063 pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
2065 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2074 if (image->depth <= 8)
2075 for (x=0; x < (long) image->columns; x++)
2077 pixel=ScaleQuantumToAny(p->red,range);
2078 q=PopCharPixel((unsigned char) pixel,q);
2079 pixel=ScaleQuantumToAny(p->green,range);
2080 q=PopCharPixel((unsigned char) pixel,q);
2081 pixel=ScaleQuantumToAny(p->blue,range);
2082 q=PopCharPixel((unsigned char) pixel,q);
2083 pixel=ScaleQuantumToAny(indexes[x],range);
2084 q=PopCharPixel((unsigned char) pixel,q);
2085 if (image->matte != MagickFalse)
2087 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2088 GetOpacityPixelComponent(p)),range);
2089 q=PopCharPixel((unsigned char) pixel,q);
2094 for (x=0; x < (long) image->columns; x++)
2096 pixel=ScaleQuantumToAny(p->red,range);
2097 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2098 pixel=ScaleQuantumToAny(p->green,range);
2099 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2100 pixel=ScaleQuantumToAny(p->blue,range);
2101 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2102 pixel=ScaleQuantumToAny(indexes[x],range);
2103 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2104 if (image->matte != MagickFalse)
2106 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2107 GetOpacityPixelComponent(p)),range);
2108 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2116 if (image->depth <= 8)
2117 for (x=0; x < (long) image->columns; x++)
2119 pixel=ScaleQuantumToAny(p->red,range);
2120 q=PopCharPixel((unsigned char) pixel,q);
2121 pixel=ScaleQuantumToAny(p->green,range);
2122 q=PopCharPixel((unsigned char) pixel,q);
2123 pixel=ScaleQuantumToAny(p->blue,range);
2124 q=PopCharPixel((unsigned char) pixel,q);
2125 if (image->matte != MagickFalse)
2127 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2128 GetOpacityPixelComponent(p)),range);
2129 q=PopCharPixel((unsigned char) pixel,q);
2134 for (x=0; x < (long) image->columns; x++)
2136 pixel=ScaleQuantumToAny(p->red,range);
2137 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2138 pixel=ScaleQuantumToAny(p->green,range);
2139 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2140 pixel=ScaleQuantumToAny(p->blue,range);
2141 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2142 if (image->matte != MagickFalse)
2144 pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
2145 GetOpacityPixelComponent(p)),range);
2146 q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2153 extent=(size_t) (q-pixels);
2155 count=WriteBlob(image,extent,pixels);
2156 if (count != (ssize_t) extent)
2158 if (image->previous == (Image *) NULL)
2160 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2161 if (status == MagickFalse)
2165 quantum_info=DestroyQuantumInfo(quantum_info);
2171 (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
2174 quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2175 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
2176 if (quantum_info == (QuantumInfo *) NULL)
2177 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2178 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2179 if (status == MagickFalse)
2180 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2181 pixels=GetQuantumPixels(quantum_info);
2182 for (y=(long) image->rows-1; y >= 0; y--)
2184 register const PixelPacket
2187 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2188 if (p == (const PixelPacket *) NULL)
2190 extent=ExportQuantumPixels(image,(const CacheView *) NULL,
2191 quantum_info,quantum_type,pixels,&image->exception);
2192 (void) WriteBlob(image,extent,pixels);
2193 if (image->previous == (Image *) NULL)
2195 status=SetImageProgress(image,SaveImageTag,y,image->rows);
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);