Commit eb7c94189 for imagemagick.org
commit eb7c941898ca3c13401ab15f4226aebab2c4c1ea
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sat Dec 5 10:53:24 2026 -0500
hex color parsing is now 64-bit QuantumAny + GetFuzzyColorDistance() is now symmetric
diff --git a/MagickCore/color-private.h b/MagickCore/color-private.h
index 8ce7999da..e6d3924fd 100644
--- a/MagickCore/color-private.h
+++ b/MagickCore/color-private.h
@@ -28,6 +28,9 @@
extern "C" {
#endif
+extern MagickExport const ColorInfo
+ *GetColorCompliance(const char *,const ComplianceType,ExceptionInfo *);
+
extern MagickPrivate MagickBooleanType
ColorComponentGenesis(void),
IsEquivalentAlpha(const Image *,const PixelInfo *,const PixelInfo *),
@@ -74,14 +77,19 @@ static inline MagickBooleanType GetColorRange(const char *color,
static inline double GetFuzzyColorDistance(const Image *p,const Image *q)
{
+ double
+ fuzz;
+
/*
Variance-style combined fuzz: the squared fuzz of each image is summed.
+ Either image may be NULL; the result is symmetric in p and q.
*/
- if (p == (const Image *) NULL)
- return(0.0);
- if (q == (const Image *) NULL)
- return(p->fuzz*p->fuzz);
- return(p->fuzz*p->fuzz+q->fuzz*q->fuzz);
+ fuzz=0.0;
+ if (p != (const Image *) NULL)
+ fuzz+=p->fuzz*p->fuzz;
+ if (q != (const Image *) NULL)
+ fuzz+=q->fuzz*q->fuzz;
+ return(fuzz);
}
#if defined(__cplusplus) || defined(c_plusplus)
diff --git a/MagickCore/color.c b/MagickCore/color.c
index ce9733443..01b3b8b4b 100644
--- a/MagickCore/color.c
+++ b/MagickCore/color.c
@@ -1256,7 +1256,7 @@ MagickExport void ConcatenateColorComponent(const PixelInfo *pixel,
}
if ((pixel->colorspace == OklchColorspace) && (channel == BluePixelChannel))
(void) FormatLocaleString(component,MagickPathExtent,"%.*g",
- GetMagickPrecision(),RadiansToDegrees(scale*QuantumScale*color));
+ GetMagickPrecision(),360.0*scale*QuantumScale*color);
else
if ((scale != 100.0) ||
(IsLabCompatibleColorspace(pixel->colorspace) != MagickFalse))
@@ -1789,7 +1789,7 @@ MagickExport MagickBooleanType IsEquivalentImage(const Image *image,
target_view=AcquireVirtualCacheView(target_image,exception);
for (y=(*y_offset); y < (ssize_t) image->rows; y++)
{
- for (x=y == 0 ? *x_offset : 0; x < (ssize_t) image->columns; x++)
+ for (x=(y == *y_offset) ? *x_offset : 0; x < (ssize_t) image->columns; x++)
{
for (j=0; j < (ssize_t) target_image->rows; j++)
{
@@ -2179,6 +2179,8 @@ static MagickBooleanType LoadColorCache(LinkedListInfo *cache,const char *xml,
{
if (LocaleCompare((char *) keyword,"name") == 0)
{
+ if (color_info->name != (char *) NULL)
+ color_info->name=DestroyString(color_info->name);
color_info->name=ConstantString(token);
break;
}
@@ -2353,7 +2355,7 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
name=BackgroundColor;
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
- if (strlen(name) > MagickPathExtent)
+ if (strlen(name) >= MagickPathExtent)
{
(void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
"UnrecognizedColor","`%s'",name);
@@ -2367,11 +2369,12 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
char
c;
- PixelPacket
- pixel = { 0, 0, 0, 0, 0 };
-
QuantumAny
- range;
+ alpha = 0,
+ blue = 0,
+ green = 0,
+ range,
+ red = 0;
size_t
depth,
@@ -2385,23 +2388,27 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
if ((n == 3) || (n == 6) || (n == 9) || (n == 12) || (n == 24) ||
(n == 48))
{
+ /*
+ Accumulate in 64-bit QuantumAny: 48-digit colors carry 64 bits
+ per channel, which overflowed the 32-bit PixelPacket fields.
+ */
do
{
- pixel.red=pixel.green;
- pixel.green=pixel.blue;
- pixel.blue=0;
+ red=green;
+ green=blue;
+ blue=0;
for (i=(ssize_t) (n/3-1); i >= 0; i--)
{
c=(*name++);
- pixel.blue<<=4;
+ blue<<=4;
if ((c >= '0') && (c <= '9'))
- pixel.blue|=(unsigned int) (c-'0');
+ blue|=(QuantumAny) (c-'0');
else
if ((c >= 'A') && (c <= 'F'))
- pixel.blue|=(unsigned int) (c-('A'-10));
+ blue|=(QuantumAny) (c-('A'-10));
else
if ((c >= 'a') && (c <= 'f'))
- pixel.blue|=(unsigned int) (c-('a'-10));
+ blue|=(QuantumAny) (c-('a'-10));
else
return(MagickFalse);
}
@@ -2418,22 +2425,22 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
}
do
{
- pixel.red=pixel.green;
- pixel.green=pixel.blue;
- pixel.blue=pixel.alpha;
- pixel.alpha=0;
+ red=green;
+ green=blue;
+ blue=alpha;
+ alpha=0;
for (i=(ssize_t) (n/4-1); i >= 0; i--)
{
c=(*name++);
- pixel.alpha<<=4;
+ alpha<<=4;
if ((c >= '0') && (c <= '9'))
- pixel.alpha|=(unsigned int) (c-'0');
+ alpha|=(QuantumAny) (c-'0');
else
if ((c >= 'A') && (c <= 'F'))
- pixel.alpha|=(unsigned int) c-('A'-10);
+ alpha|=(QuantumAny) (c-('A'-10));
else
if ((c >= 'a') && (c <= 'f'))
- pixel.alpha|=(unsigned int) c-('a'-10);
+ alpha|=(QuantumAny) (c-('a'-10));
else
return(MagickFalse);
}
@@ -2444,14 +2451,14 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
color->depth=depth;
color->alpha_trait=UndefinedPixelTrait;
range=GetQuantumRange(depth);
- color->red=(double) ScaleAnyToQuantum(pixel.red,range);
- color->green=(double) ScaleAnyToQuantum(pixel.green,range);
- color->blue=(double) ScaleAnyToQuantum(pixel.blue,range);
+ color->red=(double) ScaleAnyToQuantum(red,range);
+ color->green=(double) ScaleAnyToQuantum(green,range);
+ color->blue=(double) ScaleAnyToQuantum(blue,range);
color->alpha=(double) OpaqueAlpha;
if ((n % 3) != 0)
{
color->alpha_trait=BlendPixelTrait;
- color->alpha=(double) ScaleAnyToQuantum(pixel.alpha,range);
+ color->alpha=(double) ScaleAnyToQuantum(alpha,range);
}
color->black=0.0;
return(MagickTrue);
@@ -2562,7 +2569,7 @@ MagickExport MagickBooleanType QueryColorCompliance(const char *name,
geometry_info.sigma);
if ((flags & XiValue) != 0)
color->blue=(MagickRealType) ClampToQuantum((double) QuantumRange*
- DegreesToRadians(geometry_info.xi));
+ fmod(fmod(geometry_info.xi,360.0)+360.0,360.0)/360.0);
}
else
if (IsLabCompatibleColorspace(color->colorspace) != MagickFalse)
diff --git a/MagickCore/colormap-private.h b/MagickCore/colormap-private.h
index 175c8866f..c9de9ec16 100644
--- a/MagickCore/colormap-private.h
+++ b/MagickCore/colormap-private.h
@@ -26,12 +26,16 @@
extern "C" {
#endif
-static inline ssize_t ConstrainColormapIndex(Image *image,const ssize_t index,
- ExceptionInfo *exception)
+static inline ssize_t ConstrainColormapIndex(const Image *image,
+ const ssize_t index,ExceptionInfo *exception)
{
if ((index < 0) || (index >= (ssize_t) image->colors))
{
- if (exception->severity != CorruptImageError)
+ /*
+ Report once: skip if an error at least this severe is already
+ recorded, so a corrupt image cannot queue one exception per pixel.
+ */
+ if (exception->severity < CorruptImageError)
(void) ThrowMagickException(exception,GetMagickModule(),
CorruptImageError,"InvalidColormapIndex","`%s'",image->filename);
return(0);
@@ -39,8 +43,8 @@ static inline ssize_t ConstrainColormapIndex(Image *image,const ssize_t index,
return(index);
}
-static inline void ValidateColormapValue(Image *image,const ssize_t index,Quantum *target,
- ExceptionInfo *exception)
+static inline void ValidateColormapValue(const Image *image,
+ const ssize_t index,Quantum *target,ExceptionInfo *exception)
{
*target=(Quantum) ConstrainColormapIndex(image,index,exception);
}
diff --git a/MagickCore/colormap.c b/MagickCore/colormap.c
index 03440d068..953a9fe8d 100644
--- a/MagickCore/colormap.c
+++ b/MagickCore/colormap.c
@@ -254,8 +254,12 @@ MagickExport MagickBooleanType CycleColormapImage(Image *image,
}
for (x=0; x < (ssize_t) image->columns; x++)
{
- index=((ssize_t) GetPixelIndex(image,q)+shift) % (ssize_t)
- image->colors;
+ /*
+ Constrain first: a negative or NaN HDRI index would otherwise yield a
+ negative remainder and an out-of-bounds colormap read.
+ */
+ index=(ConstrainColormapIndex(image,(ssize_t) GetPixelIndex(image,q),
+ exception)+shift) % (ssize_t) image->colors;
SetPixelIndex(image,(Quantum) index,q);
SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
q+=(ptrdiff_t) GetPixelChannels(image);
@@ -364,7 +368,7 @@ MagickExport MagickBooleanType SortColormapByIntensity(Image *image,
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
if ((image->storage_class != PseudoClass) ||
- (image->colormap == (PixelInfo *) NULL))
+ (image->colormap == (PixelInfo *) NULL) || (image->colors == 0))
return(MagickTrue);
/*
Allocate memory for the sort keys, the old->new index map, and a scratch
@@ -374,7 +378,7 @@ MagickExport MagickBooleanType SortColormapByIntensity(Image *image,
sizeof(*entries));
remap=(size_t *) AcquireQuantumMemory((size_t) image->colors,
sizeof(*remap));
- colormap=(PixelInfo *) AcquireQuantumMemory((size_t) image->colors+1,
+ colormap=(PixelInfo *) AcquireQuantumMemory((size_t) image->colors,
sizeof(*colormap));
if ((entries == (IntensityEntry *) NULL) || (remap == (size_t *) NULL) ||
(colormap == (PixelInfo *) NULL))
@@ -412,9 +416,13 @@ MagickExport MagickBooleanType SortColormapByIntensity(Image *image,
colormap[j]=image->colormap[entries[j].index];
remap[entries[j].index]=(size_t) j;
}
- colormap[image->colors]=image->colormap[image->colors];
+ /*
+ Only the first image->colors entries are guaranteed to exist: colormaps
+ not built by AcquireImageColormap() may lack the spare trailing slot, so
+ reading or writing image->colormap[image->colors] could overrun the heap.
+ */
entries=(IntensityEntry *) RelinquishMagickMemory(entries);
- (void) memcpy(image->colormap,colormap,((size_t) image->colors+1)*
+ (void) memcpy(image->colormap,colormap,(size_t) image->colors*
sizeof(*colormap));
colormap=(PixelInfo *) RelinquishMagickMemory(colormap);
/*
diff --git a/MagickCore/colorspace-private.h b/MagickCore/colorspace-private.h
index f3adb4d43..b7ba453f2 100644
--- a/MagickCore/colorspace-private.h
+++ b/MagickCore/colorspace-private.h
@@ -1553,19 +1553,23 @@ static inline void ConvertOklchToRGB(const double L,const double C,
double
a,
b,
+ h,
l,
m,
s;
/*
Convert Oklch to RGB colorspace. Unlike Oklab, a and b carry no 0.5
- offset, and the hue is in radians.
+ offset. The hue is normalized to a fraction of a turn [0,1) so that it
+ fits in a [0..QuantumRange] channel (radians would clamp above ~57.3
+ degrees in non-HDRI builds).
*/
assert(red != (double *) NULL);
assert(green != (double *) NULL);
assert(blue != (double *) NULL);
- a=C*cos(H); /* OKLCH -> OKLab */
- b=C*sin(H);
+ h=2.0*MagickPI*(H-floor(H));
+ a=C*cos(h); /* OKLCH -> OKLab */
+ b=C*sin(h);
l=L+Oklab_la*a+Oklab_lb*b; /* OKLab -> nonlinear LMS */
m=L+Oklab_ma*a+Oklab_mb*b;
s=L+Oklab_sa*a+Oklab_sb*b;
@@ -1638,7 +1642,8 @@ static inline void ConvertRGBToOklch(const double red,const double green,
/*
Convert RGB to Oklch colorspace. Unlike Oklab, a and b carry no 0.5
- offset, and the hue is in radians [0, 2*pi).
+ offset. The hue is returned as a fraction of a turn [0,1) so that it
+ survives storage in a [0..QuantumRange] channel.
*/
assert(L != (double *) NULL);
assert(C != (double *) NULL);
@@ -1653,9 +1658,11 @@ static inline void ConvertRGBToOklch(const double red,const double green,
a=Oklab_al*l+Oklab_am*m+Oklab_as*s;
b=Oklab_bl*l+Oklab_bm*m+Oklab_bs*s;
*C=sqrt(a*a+b*b); /* OKLab -> OKLCH */
- *H=atan2(b,a);
+ *H=atan2(b,a)/(2.0*MagickPI);
if (*H < 0.0)
- *H+=2.0*MagickPI;
+ *H+=1.0;
+ if (*H >= 1.0)
+ *H=0.0;
}
static inline void ConvertRGBToYDbDr(const double red,const double green,
diff --git a/MagickCore/colorspace.c b/MagickCore/colorspace.c
index 158f64d26..5f2adb440 100644
--- a/MagickCore/colorspace.c
+++ b/MagickCore/colorspace.c
@@ -139,8 +139,13 @@ static IlluminantType GetColorspaceIlluminant(const Image *image)
return(D65Illuminant);
illuminant_type=ParseCommandOption(MagickIlluminantOptions,MagickFalse,
value);
- if (illuminant_type < 0)
- return(UndefinedIlluminant);
+ /*
+ The result indexes illuminant_tristimulus[]: reject anything outside the
+ table (A..F11) so a stray option value cannot read past its end.
+ */
+ if ((illuminant_type < (ssize_t) AIlluminant) ||
+ (illuminant_type > (ssize_t) F11Illuminant))
+ return(D65Illuminant);
return((IlluminantType) illuminant_type);
}
diff --git a/MagickCore/colorspace.h b/MagickCore/colorspace.h
index 91c7c61e8..03dfffac6 100644
--- a/MagickCore/colorspace.h
+++ b/MagickCore/colorspace.h
@@ -46,7 +46,7 @@ typedef enum
Rec601YCbCrColorspace,
Rec709YCbCrColorspace,
RGBColorspace, /* Linear RGB colorspace */
- scRGBColorspace, /* ??? */
+ scRGBColorspace, /* linear scene-referred RGB (IEC 61966-2-2) */
sRGBColorspace, /* Default: non-linear sRGB colorspace */
TransparentColorspace,
xyYColorspace,
diff --git a/MagickCore/gem-private.h b/MagickCore/gem-private.h
index 15d570c9e..4045e3148 100644
--- a/MagickCore/gem-private.h
+++ b/MagickCore/gem-private.h
@@ -19,6 +19,7 @@
#define MAGICKCORE_GEM_PRIVATE_H
#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/random_.h"
#include "MagickCore/visual-effects.h"
#if defined(__cplusplus) || defined(c_plusplus)
diff --git a/MagickCore/gem.c b/MagickCore/gem.c
index 780d8fc85..06fc5ed9f 100644
--- a/MagickCore/gem.c
+++ b/MagickCore/gem.c
@@ -162,8 +162,12 @@ MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info,
gamma=sqrt(-2.0*log(alpha));
sigma=gamma*cos((double) (2.0*MagickPI*beta));
tau=gamma*sin((double) (2.0*MagickPI*beta));
- noise=(double) pixel+sqrt((double) pixel)*SigmaGaussian*sigma+
- (double) QuantumRange*TauGaussian*tau;
+ /*
+ HDRI pixels may be negative; sqrt() of a negative value is NaN and
+ would poison the whole channel, so clamp the signal term at zero.
+ */
+ noise=(double) pixel+sqrt(MagickMax((double) pixel,0.0))*SigmaGaussian*
+ sigma+(double) QuantumRange*TauGaussian*tau;
break;
}
case ImpulseNoise:
@@ -214,6 +218,14 @@ MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info,
ssize_t
i;
+ if (SigmaPoisson <= MagickEpsilon)
+ {
+ /*
+ Zero attenuation: no noise (the estimator below would yield 0).
+ */
+ noise=(double) pixel;
+ break;
+ }
poisson=exp(-SigmaPoisson*QuantumScale*(double) pixel);
for (i=0; alpha > poisson; i++)
{
@@ -290,7 +302,9 @@ static size_t GetGaussianKernelWidth(const double radius,const double sigma,
width;
if (IsEventLogging() != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
+ (void) LogMagickEvent(TraceEvent,GetMagickModule(),
+ "radius=%g sigma=%g %s",radius,sigma,two_dimensional != MagickFalse ?
+ "2D" : "1D");
if (isnan(radius) || isnan(sigma))
return(3UL);
if (radius > MagickEpsilon)