X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=coders%2Fpnm.c;h=5d79b2d5d26d7af726f61580db27bccebe0d317f;hb=fbcb8172d4de27d663753e66a0127e124bf34500;hp=0fa96fb2f482e64fad144e616b6687f758c69019;hpb=5d89a73e47bd5f056f435668bc5d59f0026a1169;p=imagemagick diff --git a/coders/pnm.c b/coders/pnm.c index 0fa96fb2f..5d79b2d5d 100644 --- a/coders/pnm.c +++ b/coders/pnm.c @@ -13,11 +13,11 @@ % Read/Write PBMPlus Portable Anymap Image Format % % % % Software Design % -% John Cristy % +% Cristy % % July 1992 % % % % % -% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % +% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % % dedicated to making software imaging solutions freely available. % % % % You may not use this file except in compliance with the License. You may % @@ -148,69 +148,70 @@ static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset, return(offset); } -static size_t PNMInteger(Image *image,const unsigned int base, - ExceptionInfo *exception) +static void PNMComment(Image *image,ExceptionInfo *exception) { - char - *comment; - int c; + char + *comment; + register char *p; size_t - extent, + extent; + + /* + Read comment. + */ + comment=AcquireString(GetImageProperty(image,"comment",exception)); + extent=strlen(comment); + p=comment+strlen(comment); + for (c='#'; (c != EOF) && (c != (int) '\n'); p++) + { + if ((size_t) (p-comment+1) >= extent) + { + extent<<=1; + comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent, + sizeof(*comment)); + if (comment == (char *) NULL) + break; + p=comment+strlen(comment); + } + c=ReadBlobByte(image); + if (c != EOF) + { + *p=(char) c; + *(p+1)='\0'; + } + } + if (comment == (char *) NULL) + return; + (void) SetImageProperty(image,"comment",comment,exception); + comment=DestroyString(comment); +} + +static size_t PNMInteger(Image *image,const unsigned int base, + ExceptionInfo *exception) +{ + int + c; + + size_t value; /* Skip any leading whitespace. */ - extent=MaxTextExtent; - comment=(char *) NULL; - p=comment; do { c=ReadBlobByte(image); if (c == EOF) return(0); if (c == (int) '#') - { - /* - Read comment. - */ - if (comment == (char *) NULL) - comment=AcquireString((char *) NULL); - p=comment+strlen(comment); - for ( ; (c != EOF) && (c != (int) '\n'); p++) - { - if ((size_t) (p-comment+1) >= extent) - { - extent<<=1; - comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent, - sizeof(*comment)); - if (comment == (char *) NULL) - break; - p=comment+strlen(comment); - } - c=ReadBlobByte(image); - if (c != (int) '\n') - { - *p=(char) c; - *(p+1)='\0'; - } - } - if (comment == (char *) NULL) - return(0); - continue; - } + PNMComment(image,exception); } while (isdigit(c) == MagickFalse); - if (comment != (char *) NULL) - { - (void) SetImageProperty(image,"comment",comment,exception); - comment=DestroyString(comment); - } if (base == 2) return((size_t) (c-(int) '0')); /* @@ -242,8 +243,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) MagickBooleanType status; - Quantum - *scale; + QuantumAny + max_value; QuantumInfo *quantum_info; @@ -251,13 +252,9 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) QuantumType quantum_type; - register ssize_t - i; - size_t depth, extent, - max_value, packet_size; ssize_t @@ -339,6 +336,16 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) { while (isspace((int) ((unsigned char) c)) != 0) c=ReadBlobByte(image); + if (c == '#') + { + /* + Comment. + */ + PNMComment(image,exception); + c=ReadBlobByte(image); + while (isspace((int) ((unsigned char) c)) != 0) + c=ReadBlobByte(image); + } p=keyword; do { @@ -373,23 +380,23 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) { if (LocaleCompare(value,"BLACKANDWHITE") == 0) { - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); quantum_type=GrayQuantum; } if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0) { - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); image->alpha_trait=BlendPixelTrait; quantum_type=GrayAlphaQuantum; } if (LocaleCompare(value,"GRAYSCALE") == 0) { quantum_type=GrayQuantum; - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); } if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0) { - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); image->alpha_trait=BlendPixelTrait; quantum_type=GrayAlphaQuantum; } @@ -400,12 +407,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } if (LocaleCompare(value,"CMYK") == 0) { - SetImageColorspace(image,CMYKColorspace,exception); + (void) SetImageColorspace(image,CMYKColorspace,exception); quantum_type=CMYKQuantum; } if (LocaleCompare(value,"CMYK_ALPHA") == 0) { - SetImageColorspace(image,CMYKColorspace,exception); + (void) SetImageColorspace(image,CMYKColorspace,exception); image->alpha_trait=BlendPixelTrait; quantum_type=CMYKAQuantum; } @@ -416,7 +423,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); - if (max_value >= 65536) + if (max_value > 4294967295) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); for (depth=1; GetQuantumRange(depth) < max_value; depth++) ; image->depth=depth; @@ -435,7 +442,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) /* Convert PBM image to pixel packets. */ - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); for (y=0; y < (ssize_t) image->rows; y++) { register ssize_t @@ -474,20 +481,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) /* Convert PGM image to pixel packets. */ - SetImageColorspace(image,GRAYColorspace,exception); - scale=(Quantum *) NULL; - if (max_value != (1U*QuantumRange)) - { - /* - Compute pixel scaling table. - */ - scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL, - sizeof(*scale)); - if (scale == (Quantum *) NULL) - ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); - for (i=0; i <= (ssize_t) max_value; i++) - scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5); - } + (void) SetImageColorspace(image,GRAYColorspace,exception); for (y=0; y < (ssize_t) image->rows; y++) { register ssize_t @@ -501,11 +495,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) break; for (x=0; x < (ssize_t) image->columns; x++) { - intensity=PNMInteger(image,10,exception); + intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value); SetPixelGray(image,intensity,q); - if (scale != (Quantum *) NULL) - SetPixelGray(image,scale[ConstrainPixel(image,(ssize_t) intensity, - max_value,exception)],q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) @@ -519,31 +510,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } } image->type=GrayscaleType; - if (scale != (Quantum *) NULL) - scale=(Quantum *) RelinquishMagickMemory(scale); break; } case '3': { - PixelInfo - pixel; - /* Convert PNM image to pixel packets. */ - scale=(Quantum *) NULL; - if (max_value != (1U*QuantumRange)) - { - /* - Compute pixel scaling table. - */ - scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL, - sizeof(*scale)); - if (scale == (Quantum *) NULL) - ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); - for (i=0; i <= (ssize_t) max_value; i++) - scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5); - } for (y=0; y < (ssize_t) image->rows; y++) { register ssize_t @@ -557,21 +530,15 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) break; for (x=0; x < (ssize_t) image->columns; x++) { - pixel.red=(double) PNMInteger(image,10,exception); - pixel.green=(double) PNMInteger(image,10,exception); - pixel.blue=(double) PNMInteger(image,10,exception); - if (scale != (Quantum *) NULL) - { - pixel.red=(double) scale[ConstrainPixel(image,(ssize_t) - pixel.red,max_value,exception)]; - pixel.green=(double) scale[ConstrainPixel(image, - (ssize_t) pixel.green,max_value,exception)]; - pixel.blue=(double) scale[ConstrainPixel(image,(ssize_t) - pixel.blue,max_value,exception)]; - } - SetPixelRed(image,ClampToQuantum(pixel.red),q); - SetPixelGreen(image,ClampToQuantum(pixel.green),q); - SetPixelBlue(image,ClampToQuantum(pixel.blue),q); + double + pixel; + + pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value); + SetPixelRed(image,pixel,q); + pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value); + SetPixelGreen(image,pixel,q); + pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value); + SetPixelBlue(image,pixel,q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) @@ -584,8 +551,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) break; } } - if (scale != (Quantum *) NULL) - scale=(Quantum *) RelinquishMagickMemory(scale); break; } case '4': @@ -593,7 +558,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) /* Convert PBM raw image to pixel packets. */ - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); quantum_type=GrayQuantum; if (image->storage_class == PseudoClass) quantum_type=IndexQuantum; @@ -662,16 +627,18 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } case '5': { - QuantumAny - range; - /* Convert PGM raw image to pixel packets. */ - SetImageColorspace(image,GRAYColorspace,exception); - range=GetQuantumRange(image->depth); + (void) SetImageColorspace(image,GRAYColorspace,exception); quantum_type=GrayQuantum; - extent=(image->depth <= 8 ? 1 : 2)*image->columns; + if (image->depth <= 8) + extent=image->columns; + else + if (image->depth <= 16) + extent=2*image->columns; + else + extent=4*image->columns; quantum_info=AcquireQuantumInfo(image_info,image); if (quantum_info == (QuantumInfo *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); @@ -723,34 +690,56 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) continue; } p=pixels; - if ((image->depth == 8) || (image->depth == 16)) - (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, - quantum_type,pixels,exception); - else - if (image->depth <= 8) - { - unsigned char - pixel; + switch (image->depth) + { + case 8: + case 16: + case 32: + { + (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, + quantum_type,pixels,exception); + break; + } + default: + { + unsigned int + pixel; - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 8) { - p=PushCharPixel(p,&pixel); - SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q); - q+=GetPixelChannels(image); - } - } - else - { - unsigned short - pixel; + unsigned char + pixel; - for (x=0; x < (ssize_t) image->columns; x++) + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushCharPixel(p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q); + q+=GetPixelChannels(image); + } + break; + } + if (image->depth <= 16) { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q); - q+=GetPixelChannels(image); + unsigned short + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q); + q+=GetPixelChannels(image); + } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q); + q+=GetPixelChannels(image); } + break; + } + } sync=SyncAuthenticPixels(image,exception); if (sync == MagickFalse) status=MagickFalse; @@ -763,15 +752,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } case '6': { - QuantumAny - range; - /* Convert PNM raster image to pixel packets. */ quantum_type=RGBQuantum; extent=3*(image->depth <= 8 ? 1 : 2)*image->columns; - range=GetQuantumRange(image->depth); quantum_info=AcquireQuantumInfo(image_info,image); if (quantum_info == (QuantumInfo *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); @@ -824,34 +809,61 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) continue; } p=pixels; - if (image->depth == 8) - for (x=0; x < (ssize_t) image->columns; x++) + switch (image->depth) + { + case 8: { - SetPixelRed(image,ScaleCharToQuantum(*p++),q); - SetPixelGreen(image,ScaleCharToQuantum(*p++),q); - SetPixelBlue(image,ScaleCharToQuantum(*p++),q); - SetPixelAlpha(image,OpaqueAlpha,q); - q+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + SetPixelRed(image,ScaleCharToQuantum(*p++),q); + SetPixelGreen(image,ScaleCharToQuantum(*p++),q); + SetPixelBlue(image,ScaleCharToQuantum(*p++),q); + SetPixelAlpha(image,OpaqueAlpha,q); + q+=GetPixelChannels(image); + } + break; } - else - if (image->depth == 16) + case 16: + { + unsigned short + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) { - unsigned short - pixel; + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleShortToQuantum(pixel),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleShortToQuantum(pixel),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleShortToQuantum(pixel),q); + SetPixelAlpha(image,OpaqueAlpha,q); + q+=GetPixelChannels(image); + } + break; + } + case 32: + { + unsigned int + pixel; - for (x=0; x < (ssize_t) image->columns; x++) - { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelRed(image,ScaleShortToQuantum(pixel),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGreen(image,ScaleShortToQuantum(pixel),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelBlue(image,ScaleShortToQuantum(pixel),q); - SetPixelAlpha(image,OpaqueAlpha,q); - q+=GetPixelChannels(image); - } + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleLongToQuantum(pixel),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleLongToQuantum(pixel),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleLongToQuantum(pixel),q); + SetPixelAlpha(image,OpaqueAlpha,q); + q+=GetPixelChannels(image); } - else + break; + } + default: + { + unsigned int + pixel; + if (image->depth <= 8) { unsigned char @@ -860,16 +872,17 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) for (x=0; x < (ssize_t) image->columns; x++) { p=PushCharPixel(p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); p=PushCharPixel(p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); p=PushCharPixel(p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); SetPixelAlpha(image,OpaqueAlpha,q); q+=GetPixelChannels(image); } + break; } - else + if (image->depth <= 16) { unsigned short pixel; @@ -877,15 +890,30 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) for (x=0; x < (ssize_t) image->columns; x++) { p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); SetPixelAlpha(image,OpaqueAlpha,q); q+=GetPixelChannels(image); } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + q+=GetPixelChannels(image); + } + break; + } + } sync=SyncAuthenticPixels(image,exception); if (sync == MagickFalse) status=MagickFalse; @@ -897,16 +925,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } case '7': { - QuantumAny - range; - size_t channels; /* Convert PAM raster image to pixel packets. */ - range=GetQuantumRange(image->depth); switch (quantum_type) { case GrayQuantum: @@ -929,7 +953,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) } if (image->alpha_trait == BlendPixelTrait) channels++; - extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns; + if (image->depth <= 8) + extent=channels*image->columns; + else + if (image->depth <= 16) + extent=2*channels*image->columns; + else + extent=4*channels*image->columns; quantum_info=AcquireQuantumInfo(image_info,image); if (quantum_info == (QuantumInfo *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); @@ -981,155 +1011,235 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) continue; } p=pixels; - if ((image->depth == 8) || (image->depth == 16)) - (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, - quantum_type,pixels,exception); - else - switch (quantum_type) + switch (image->depth) + { + case 8: + case 16: + case 32: + { + (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, + quantum_type,pixels,exception); + break; + } + default: { - case GrayQuantum: - case GrayAlphaQuantum: + switch (quantum_type) { - if (image->depth <= 8) - { - unsigned char - pixel; + case GrayQuantum: + case GrayAlphaQuantum: + { + unsigned int + pixel; - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 8) { - p=PushCharPixel(p,&pixel); - SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushCharPixel(p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned char + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushCharPixel(p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushCharPixel(p,&pixel); + if (image->depth != 1) + SetPixelAlpha(image,ScaleAnyToQuantum(pixel, + max_value),q); + else + SetPixelAlpha(image,QuantumRange- + ScaleAnyToQuantum(pixel,max_value),q); + } + q+=GetPixelChannels(image); + } + break; } - } - else - { - unsigned short - pixel; - - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned short + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value), + q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel, + max_value),q); + } + q+=GetPixelChannels(image); + } + break; } - } - break; - } - case CMYKQuantum: - case CMYKAQuantum: - { - if (image->depth <= 8) + for (x=0; x < (ssize_t) image->columns; x++) { - unsigned char - pixel; + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q); + } + q+=GetPixelChannels(image); + } + break; + } + case CMYKQuantum: + case CMYKAQuantum: + { + unsigned int + pixel; - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 8) { - p=PushCharPixel(p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); - p=PushCharPixel(p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); - p=PushCharPixel(p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); - p=PushCharPixel(p,&pixel); - SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushCharPixel(p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned char + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushCharPixel(p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushCharPixel(p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushCharPixel(p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushCharPixel(p,&pixel); + SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushCharPixel(p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel, + max_value),q); + } + q+=GetPixelChannels(image); + } + break; } - } - else - { - unsigned short - pixel; - - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned short + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel, + max_value),q); + } + q+=GetPixelChannels(image); + } } - } - break; - } - default: - { - if (image->depth <= 8) + for (x=0; x < (ssize_t) image->columns; x++) { - unsigned char - pixel; + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q); + } + q+=GetPixelChannels(image); + } + break; + } + default: + { + unsigned int + pixel; - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 8) { - p=PushCharPixel(p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); - p=PushCharPixel(p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); - p=PushCharPixel(p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushCharPixel(p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned char + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushCharPixel(p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushCharPixel(p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushCharPixel(p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushCharPixel(p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q); + } + q+=GetPixelChannels(image); + } + break; } - } - else - { - unsigned short - pixel; - - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q); - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q); - SetPixelAlpha(image,OpaqueAlpha,q); - if (image->alpha_trait == BlendPixelTrait) - { - p=PushShortPixel(MSBEndian,p,&pixel); - SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q); - } - q+=GetPixelChannels(image); + unsigned short + pixel; + + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushShortPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel, + max_value),q); + } + q+=GetPixelChannels(image); + } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q); + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q); + SetPixelAlpha(image,OpaqueAlpha,q); + if (image->alpha_trait == BlendPixelTrait) + { + p=PushLongPixel(MSBEndian,p,&pixel); + SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q); + } + q+=GetPixelChannels(image); } - break; + break; + } } } + } sync=SyncAuthenticPixels(image,exception); if (sync == MagickFalse) status=MagickFalse; @@ -1147,7 +1257,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception) Convert PFM raster image to pixel packets. */ if (format == 'f') - SetImageColorspace(image,GRAYColorspace,exception); + (void) SetImageColorspace(image,GRAYColorspace,exception); quantum_type=format == 'f' ? GrayQuantum : RGBQuantum; image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian; image->depth=32; @@ -1303,12 +1413,14 @@ ModuleExport size_t RegisterPNMImage(void) entry->decoder=(DecodeImageHandler *) ReadPNMImage; entry->encoder=(EncodeImageHandler *) WritePNMImage; entry->description=ConstantString("Common 2-dimensional bitmap format"); + entry->mime_type=ConstantString("image/x-portable-pixmap"); entry->module=ConstantString("PNM"); (void) RegisterMagickInfo(entry); entry=SetMagickInfo("PBM"); entry->decoder=(DecodeImageHandler *) ReadPNMImage; entry->encoder=(EncodeImageHandler *) WritePNMImage; entry->description=ConstantString("Portable bitmap format (black and white)"); + entry->mime_type=ConstantString("image/x-portable-bitmap"); entry->module=ConstantString("PNM"); (void) RegisterMagickInfo(entry); entry=SetMagickInfo("PFM"); @@ -1322,6 +1434,7 @@ ModuleExport size_t RegisterPNMImage(void) entry->decoder=(DecodeImageHandler *) ReadPNMImage; entry->encoder=(EncodeImageHandler *) WritePNMImage; entry->description=ConstantString("Portable graymap format (gray scale)"); + entry->mime_type=ConstantString("image/x-portable-greymap"); entry->module=ConstantString("PNM"); (void) RegisterMagickInfo(entry); entry=SetMagickInfo("PNM"); @@ -1329,12 +1442,14 @@ ModuleExport size_t RegisterPNMImage(void) entry->encoder=(EncodeImageHandler *) WritePNMImage; entry->magick=(IsImageFormatHandler *) IsPNM; entry->description=ConstantString("Portable anymap"); + entry->mime_type=ConstantString("image/x-portable-pixmap"); entry->module=ConstantString("PNM"); (void) RegisterMagickInfo(entry); entry=SetMagickInfo("PPM"); entry->decoder=(DecodeImageHandler *) ReadPNMImage; entry->encoder=(EncodeImageHandler *) WritePNMImage; entry->description=ConstantString("Portable pixmap format (color)"); + entry->mime_type=ConstantString("image/x-portable-pixmap"); entry->module=ConstantString("PNM"); (void) RegisterMagickInfo(entry); return(MagickImageCoderSignature); @@ -1453,12 +1568,16 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, scene=0; do { + QuantumAny + max_value; + /* Write PNM file header. */ packet_size=3; quantum_type=RGBQuantum; (void) CopyMagickString(magick,image_info->magick,MaxTextExtent); + max_value=GetQuantumRange(image->depth); switch (magick[1]) { case 'A': @@ -1532,9 +1651,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, for (p=value; *p != '\0'; p++) { (void) WriteBlobByte(image,(unsigned char) *p); - if ((*p == '\r') && (*(p+1) != '\0')) - (void) WriteBlobByte(image,'#'); - if ((*p == '\n') && (*(p+1) != '\0')) + if ((*p == '\n') || (*p == '\r')) (void) WriteBlobByte(image,'#'); } (void) WriteBlobByte(image,'\n'); @@ -1589,8 +1706,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, packet_size++; (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent); } - if (image->depth > 16) - image->depth=16; + if (image->depth > 32) + image->depth=32; (void) FormatLocaleString(buffer,MaxTextExtent, "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double) ((MagickOffsetType) GetQuantumRange(image->depth))); @@ -1612,6 +1729,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, /* Convert image to a PBM image. */ + (void) SetImageType(image,BilevelType,exception); q=pixels; for (y=0; y < (ssize_t) image->rows; y++) { @@ -1626,8 +1744,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, break; for (x=0; x < (ssize_t) image->columns; x++) { - pixel=GetPixelIntensity(image,p); - *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ? + *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ? '0' : '1'); *q++=' '; if ((q-pixels+2) >= 80) @@ -1664,7 +1781,10 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, if (image->depth <= 8) (void) WriteBlobString(image,"255\n"); else - (void) WriteBlobString(image,"65535\n"); + if (image->depth <= 16) + (void) WriteBlobString(image,"65535\n"); + else + (void) WriteBlobString(image,"4294967295\n"); q=pixels; for (y=0; y < (ssize_t) image->rows; y++) { @@ -1679,13 +1799,17 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, break; for (x=0; x < (ssize_t) image->columns; x++) { - index=GetPixelIntensity(image,p); + index=ClampToQuantum(GetPixelLuma(image,p)); if (image->depth <= 8) count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ", ScaleQuantumToChar(index)); else - count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ", - ScaleQuantumToShort(index)); + if (image->depth <= 16) + count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ", + ScaleQuantumToShort(index)); + else + count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ", + ScaleQuantumToLong(index)); extent=(size_t) count; (void) strncpy((char *) q,buffer,extent); q+=extent; @@ -1720,12 +1844,14 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, /* Convert image to a PNM image. */ - if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) - (void) TransformImageColorspace(image,sRGBColorspace,exception); + (void) TransformImageColorspace(image,sRGBColorspace,exception); if (image->depth <= 8) (void) WriteBlobString(image,"255\n"); else - (void) WriteBlobString(image,"65535\n"); + if (image->depth <= 16) + (void) WriteBlobString(image,"65535\n"); + else + (void) WriteBlobString(image,"4294967295\n"); q=pixels; for (y=0; y < (ssize_t) image->rows; y++) { @@ -1746,10 +1872,16 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, ScaleQuantumToChar(GetPixelGreen(image,p)), ScaleQuantumToChar(GetPixelBlue(image,p))); else - count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent, - "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)), - ScaleQuantumToShort(GetPixelGreen(image,p)), - ScaleQuantumToShort(GetPixelBlue(image,p))); + if (image->depth <= 16) + count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent, + "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)), + ScaleQuantumToShort(GetPixelGreen(image,p)), + ScaleQuantumToShort(GetPixelBlue(image,p))); + else + count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent, + "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)), + ScaleQuantumToLong(GetPixelGreen(image,p)), + ScaleQuantumToLong(GetPixelBlue(image,p))); extent=(size_t) count; (void) strncpy((char *) q,buffer,extent); q+=extent; @@ -1781,6 +1913,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, /* Convert image to a PBM image. */ + (void) SetImageType(image,BilevelType,exception); image->depth=1; quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image); if (quantum_info == (QuantumInfo *) NULL) @@ -1813,14 +1946,11 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, } case '5': { - QuantumAny - range; - /* Convert image to a PGM image. */ - if (image->depth > 8) - image->depth=16; + if (image->depth > 32) + image->depth=32; (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) ((MagickOffsetType) GetQuantumRange(image->depth))); (void) WriteBlobString(image,buffer); @@ -1830,7 +1960,6 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, quantum_info->min_is_white=MagickTrue; pixels=GetQuantumPixels(quantum_info); extent=GetQuantumExtent(image,quantum_info,GrayQuantum); - range=GetQuantumRange(image->depth); for (y=0; y < (ssize_t) image->rows; y++) { register const Quantum @@ -1843,43 +1972,79 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, if (p == (const Quantum *) NULL) break; q=pixels; - if ((image->depth == 8) || (image->depth == 16)) - extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, - GrayQuantum,pixels,exception); - else + switch (image->depth) + { + case 8: + case 16: + case 32: + { + extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, + GrayQuantum,pixels,exception); + break; + } + default: { if (image->depth <= 8) - for (x=0; x < (ssize_t) image->columns; x++) { - if (IsPixelGray(image,p) == MagickFalse) - pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range); - else - { - if (image->depth == 8) - pixel=ScaleQuantumToChar(GetPixelRed(image,p)); - else - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - } - q=PopCharPixel((unsigned char) pixel,q); - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + if (IsPixelGray(image,p) == MagickFalse) + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma( + image,p)),max_value); + else + { + if (image->depth == 8) + pixel=ScaleQuantumToChar(GetPixelRed(image,p)); + else + pixel=ScaleQuantumToAny(GetPixelRed(image,p), + max_value); + } + q=PopCharPixel((unsigned char) pixel,q); + p+=GetPixelChannels(image); + } + extent=(size_t) (q-pixels); + break; } - else - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - if (IsPixelGray(image,p) == MagickFalse) - pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range); - else - { - if (image->depth == 16) - pixel=ScaleQuantumToShort(GetPixelRed(image,p)); - else - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - } - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + if (IsPixelGray(image,p) == MagickFalse) + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image, + p)),max_value); + else + { + if (image->depth == 16) + pixel=ScaleQuantumToShort(GetPixelRed(image,p)); + else + pixel=ScaleQuantumToAny(GetPixelRed(image,p), + max_value); + } + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + p+=GetPixelChannels(image); + } + extent=(size_t) (q-pixels); + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + if (IsPixelGray(image,p) == MagickFalse) + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)), + max_value); + else + { + if (image->depth == 16) + pixel=ScaleQuantumToLong(GetPixelRed(image,p)); + else + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + } + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + p+=GetPixelChannels(image); + } extent=(size_t) (q-pixels); + break; } + } count=WriteBlob(image,extent,pixels); if (count != (ssize_t) extent) break; @@ -1896,16 +2061,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, } case '6': { - QuantumAny - range; - /* Convert image to a PNM image. */ - if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) - (void) TransformImageColorspace(image,sRGBColorspace,exception); - if (image->depth > 8) - image->depth=16; + (void) TransformImageColorspace(image,sRGBColorspace,exception); + if (image->depth > 32) + image->depth=32; (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) ((MagickOffsetType) GetQuantumRange(image->depth))); (void) WriteBlobString(image,buffer); @@ -1915,7 +2076,6 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, (void) SetQuantumEndian(image,quantum_info,MSBEndian); pixels=GetQuantumPixels(quantum_info); extent=GetQuantumExtent(image,quantum_info,quantum_type); - range=GetQuantumRange(image->depth); for (y=0; y < (ssize_t) image->rows; y++) { register const Quantum @@ -1928,35 +2088,62 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, if (p == (const Quantum *) NULL) break; q=pixels; - if ((image->depth == 8) || (image->depth == 16)) - extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, - quantum_type,pixels,exception); - else + switch (image->depth) + { + case 8: + case 16: + case 32: + { + extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, + quantum_type,pixels,exception); + break; + } + default: { if (image->depth <= 8) - for (x=0; x < (ssize_t) image->columns; x++) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + p+=GetPixelChannels(image); + } + extent=(size_t) (q-pixels); + break; } - else - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + p+=GetPixelChannels(image); + } + extent=(size_t) (q-pixels); + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + p+=GetPixelChannels(image); + } extent=(size_t) (q-pixels); + break; } + } count=WriteBlob(image,extent,pixels); if (count != (ssize_t) extent) break; @@ -1973,17 +2160,13 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, } case '7': { - QuantumAny - range; - /* Convert image to a PAM. */ - if (image->depth > 16) - image->depth=16; + if (image->depth > 32) + image->depth=32; quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image); pixels=GetQuantumPixels(quantum_info); - range=GetQuantumRange(image->depth); for (y=0; y < (ssize_t) image->rows; y++) { register const Quantum @@ -1996,10 +2179,17 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, if (p == (const Quantum *) NULL) break; q=pixels; - if ((image->depth == 8) || (image->depth == 16)) - extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, - quantum_type,pixels,exception); - else + switch (image->depth) + { + case 8: + case 16: + case 32: + { + extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, + quantum_type,pixels,exception); + break; + } + default: { switch (quantum_type) { @@ -2007,113 +2197,196 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image, case GrayAlphaQuantum: { if (image->depth <= 8) - for (x=0; x < (ssize_t) image->columns; x++) { - pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=(unsigned char) ScaleQuantumToAny( - GetPixelAlpha(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma( + image,p)),max_value); + q=PopCharPixel((unsigned char) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=(unsigned char) ScaleQuantumToAny( + GetPixelAlpha(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } - else - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=(unsigned char) ScaleQuantumToAny( - GetPixelAlpha(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma( + image,p)),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=(unsigned char) ScaleQuantumToAny( + GetPixelAlpha(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image, + p)),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=(unsigned char) ScaleQuantumToAny( + GetPixelAlpha(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + } + p+=GetPixelChannels(image); + } break; } case CMYKQuantum: case CMYKAQuantum: { if (image->depth <= 8) - for (x=0; x < (ssize_t) image->columns; x++) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlack(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } - else - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlack(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + } + p+=GetPixelChannels(image); + } break; } default: { if (image->depth <= 8) - for (x=0; x < (ssize_t) image->columns; x++) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range); - q=PopCharPixel((unsigned char) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopCharPixel((unsigned char) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } - else - for (x=0; x < (ssize_t) image->columns; x++) + if (image->depth <= 16) { - pixel=ScaleQuantumToAny(GetPixelRed(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - if (image->alpha_trait == BlendPixelTrait) - { - pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range); - q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); - } - p+=GetPixelChannels(image); + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopShortPixel(MSBEndian,(unsigned short) pixel,q); + } + p+=GetPixelChannels(image); + } + break; } + for (x=0; x < (ssize_t) image->columns; x++) + { + pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + if (image->alpha_trait == BlendPixelTrait) + { + pixel=ScaleQuantumToAny(GetPixelAlpha(image,p), + max_value); + q=PopLongPixel(MSBEndian,(unsigned int) pixel,q); + } + p+=GetPixelChannels(image); + } break; } } extent=(size_t) (q-pixels); + break; } + } count=WriteBlob(image,extent,pixels); if (count != (ssize_t) extent) break;