Commit 359f0483e for imagemagick.org
commit 359f0483ecf5bad3e65698f44813cc33e705798a
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sat Nov 28 15:32:00 2026 -0500
is_gray and is_mono are now reset per image
diff --git a/coders/pnm.c b/coders/pnm.c
index 8f3089789..ab09411fe 100644
--- a/coders/pnm.c
+++ b/coders/pnm.c
@@ -310,7 +310,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
size_t
depth,
- extent;
+ extent,
+ read_depth;
ssize_t
count,
@@ -356,14 +357,26 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
colorspace=UndefinedColorspace;
quantum_type=UndefinedQuantum;
quantum_scale=1.0;
+ is_gray=MagickTrue;
+ is_mono=MagickTrue;
format=(char) ReadBlobByte(image);
+ if ((format == '\0') ||
+ (strchr("1234567FfHh",(int) ((unsigned char) format)) == (char *) NULL))
+ ThrowPNMException(CorruptImageError,"ImproperImageHeader");
if (format != '7')
{
+ int
+ c;
+
/*
PBM, PGM, PPM, and PNM.
*/
- if (ReadBlobByte(image) == '4')
- image->alpha_trait=BlendPixelTrait;
+ c=ReadBlobByte(image);
+ if (c == (int) '#')
+ c=PNMComment(image,&comment_info);
+ else
+ if ((c == (int) '4') && ((format == 'F') || (format == 'H')))
+ image->alpha_trait=BlendPixelTrait;
image->columns=(size_t) PNMInteger(image,&comment_info,10);
image->rows=(size_t) PNMInteger(image,&comment_info,10);
if ((format == 'f') || (format == 'F') || (format == 'h') ||
@@ -373,7 +386,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
scale[32];
if (PNMString(image,scale,sizeof(scale)) != (char *) NULL)
- quantum_scale=StringToDouble(scale,(char **) NULL);
+ {
+ quantum_scale=StringToDouble(scale,(char **) NULL);
+ if ((quantum_scale != quantum_scale) ||
+ (fabs(quantum_scale) > DBL_MAX) || (quantum_scale == 0.0))
+ ThrowPNMException(CorruptImageError,"ImproperImageHeader");
+ }
}
else
{
@@ -395,6 +413,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
char
*p;
+ MagickBooleanType
+ end_header = MagickFalse,
+ has_alpha = MagickFalse;
+
+ size_t
+ pam_depth = 0;
+
/*
PAM.
*/
@@ -425,6 +450,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
break;
}
}
+ if (c == EOF)
+ break;
p=keyword;
do
{
@@ -436,7 +463,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
} while (isalnum((int) ((unsigned char) c)));
*p='\0';
if (LocaleCompare(keyword,"endhdr") == 0)
- break;
+ {
+ end_header=MagickTrue;
+ break;
+ }
while (isspace((int) ((unsigned char) c)) != 0)
{
c=ReadBlobByte(image);
@@ -457,14 +487,19 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
Assign a value to the specified keyword.
*/
if (LocaleCompare(keyword,"depth") == 0)
- (void) StringToUnsignedLong(value);
+ pam_depth=(size_t) StringToUnsignedLong(value);
if (LocaleCompare(keyword,"height") == 0)
image->rows=StringToUnsignedLong(value);
if (LocaleCompare(keyword,"maxval") == 0)
max_value=StringToUnsignedLong(value);
+ if ((LocaleCompare(keyword,"TUPLTYPE") == 0) &&
+ (LocaleCompare(value,"ALPHA") == 0))
+ has_alpha=MagickTrue;
if ((quantum_type == UndefinedQuantum) &&
(LocaleCompare(keyword,"TUPLTYPE") == 0))
{
+ if (LocaleCompare(value,"RGB") == 0)
+ quantum_type=RGBQuantum;
if (LocaleCompare(value,"BLACKANDWHITE") == 0)
{
colorspace=GRAYColorspace;
@@ -507,6 +542,62 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (LocaleCompare(keyword,"width") == 0)
image->columns=StringToUnsignedLong(value);
}
+ if (end_header == MagickFalse)
+ ThrowPNMException(CorruptImageError,"ImproperImageHeader");
+ if (quantum_type == UndefinedQuantum)
+ switch (pam_depth)
+ {
+ case 1:
+ {
+ colorspace=GRAYColorspace;
+ quantum_type=GrayQuantum;
+ break;
+ }
+ case 2:
+ {
+ colorspace=GRAYColorspace;
+ quantum_type=GrayAlphaQuantum;
+ has_alpha=MagickTrue;
+ break;
+ }
+ case 4:
+ {
+ quantum_type=RGBAQuantum;
+ has_alpha=MagickTrue;
+ break;
+ }
+ default:
+ break;
+ }
+ if (has_alpha != MagickFalse)
+ {
+ image->alpha_trait=BlendPixelTrait;
+ if (quantum_type == GrayQuantum)
+ quantum_type=GrayAlphaQuantum;
+ else
+ if (quantum_type == CMYKQuantum)
+ quantum_type=CMYKAQuantum;
+ else
+ if ((quantum_type == RGBQuantum) || (quantum_type == UndefinedQuantum))
+ quantum_type=RGBAQuantum;
+ }
+ if (pam_depth != 0)
+ {
+ size_t
+ expected;
+
+ switch (quantum_type)
+ {
+ case GrayQuantum: expected=1; break;
+ case GrayAlphaQuantum: expected=2; break;
+ case CMYKQuantum: expected=4; break;
+ case CMYKAQuantum: expected=5; break;
+ case RGBAQuantum: expected=4; break;
+ default: expected=3; break;
+ }
+ if (expected != pam_depth)
+ ThrowPNMException(CorruptImageError,"ImproperImageHeader");
+ }
}
if (quantum_type == UndefinedQuantum)
quantum_type=RGBQuantum;
@@ -514,10 +605,23 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
ThrowPNMException(CorruptImageError,"ImproperImageHeader");
for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
image->depth=depth;
+ /*
+ The raw readers import samples directly only when maxval is exactly
+ 2^depth-1; otherwise samples must be rescaled by maxval.
+ */
+ read_depth=depth;
+ if (max_value != GetQuantumRange(depth))
+ switch (depth)
+ {
+ case 8: read_depth=7; break;
+ case 16: read_depth=15; break;
+ case 32: read_depth=31; break;
+ default: break;
+ }
if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
if (image->scene >= (image_info->scene+image_info->number_scenes-1))
break;
- if ((MagickSizeType) (image->columns*image->rows/8) > GetBlobSize(image))
+ if (((MagickSizeType) image->columns*image->rows/8) > GetBlobSize(image))
ThrowPNMException(CorruptImageError,"InsufficientImageDataInFile");
status=SetImageExtent(image,image->columns,image->rows,exception);
if (status == MagickFalse)
@@ -747,6 +851,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
quantum_info=AcquireQuantumInfo(image_info,image);
if (quantum_info == (QuantumInfo *) NULL)
ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
+ (void) SetQuantumEndian(image,quantum_info,MSBEndian);
pixels=GetQuantumPixels(quantum_info);
for (y=0; y < (ssize_t) image->rows; y++)
{
@@ -782,7 +887,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (q == (Quantum *) NULL)
break;
p=(unsigned char *) stream;
- switch (image->depth)
+ switch (read_depth)
{
case 8:
case 16:
@@ -794,7 +899,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
default:
{
- switch (image->depth)
+ switch (read_depth)
{
case 1:
case 2:
@@ -906,7 +1011,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (q == (Quantum *) NULL)
break;
p=(unsigned char *) stream;
- switch (image->depth)
+ switch (read_depth)
{
case 1:
case 2:
@@ -1105,6 +1210,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
quantum_info=AcquireQuantumInfo(image_info,image);
if (quantum_info == (QuantumInfo *) NULL)
ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
+ (void) SetQuantumEndian(image,quantum_info,MSBEndian);
pixels=GetQuantumPixels(quantum_info);
for (y=0; y < (ssize_t) image->rows; y++)
{
@@ -1140,7 +1246,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (q == (Quantum *) NULL)
break;
p=(unsigned char *) stream;
- switch (image->depth)
+ switch (read_depth)
{
case 8:
case 16:
@@ -1157,7 +1263,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
case GrayQuantum:
case GrayAlphaQuantum:
{
- switch (image->depth)
+ switch (read_depth)
{
case 1:
case 2:
@@ -1241,7 +1347,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
case CMYKQuantum:
case CMYKAQuantum:
{
- switch (image->depth)
+ switch (read_depth)
{
case 1:
case 2:
@@ -1350,7 +1456,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
default:
{
- switch (image->depth)
+ switch (read_depth)
{
case 1:
case 2:
@@ -1628,7 +1734,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (format == 'P')
break;
} while (format != '\n');
- count=ReadBlob(image,1,(unsigned char *) &format);
+ if ((count != 1) || (format != 'P'))
+ count=ReadBlob(image,1,(unsigned char *) &format);
if ((count == 1) && (format == 'P'))
{
/*
@@ -1755,7 +1862,9 @@ ModuleExport void UnregisterPNMImage(void)
{
(void) UnregisterMagickInfo("PAM");
(void) UnregisterMagickInfo("PBM");
+ (void) UnregisterMagickInfo("PFM");
(void) UnregisterMagickInfo("PGM");
+ (void) UnregisterMagickInfo("PHM");
(void) UnregisterMagickInfo("PNM");
(void) UnregisterMagickInfo("PPM");
}
@@ -1856,6 +1965,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
packet_size=3;
quantum_type=RGBQuantum;
(void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
+ if (image->depth > 32)
+ image->depth=32;
max_value=GetQuantumRange(image->depth);
switch (magick[1])
{
@@ -2577,7 +2688,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
q=PopCharPixel((unsigned char) pixel,q);
if (image->alpha_trait != UndefinedPixelTrait)
{
- pixel=(unsigned char) ScaleQuantumToAny(
+ pixel=ScaleQuantumToAny(
GetPixelAlpha(image,p),max_value);
q=PopCharPixel((unsigned char) pixel,q);
}
@@ -2594,7 +2705,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
if (image->alpha_trait != UndefinedPixelTrait)
{
- pixel=(unsigned char) ScaleQuantumToAny(
+ pixel=ScaleQuantumToAny(
GetPixelAlpha(image,p),max_value);
q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
}
@@ -2609,7 +2720,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
if (image->alpha_trait != UndefinedPixelTrait)
{
- pixel=(unsigned char) ScaleQuantumToAny(
+ pixel=ScaleQuantumToAny(
GetPixelAlpha(image,p),max_value);
q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
}