Commit 7888ec61b for imagemagick.org

commit 7888ec61b5629f609649d944261211a2ea07eda2
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 22:14:57 2026 -0500

    the reverse pass of Voronoi used the wrong kernel weights for neighbours to the right of the pixel

diff --git a/MagickCore/distort.c b/MagickCore/distort.c
index 2cf4663e3..86c25947e 100644
--- a/MagickCore/distort.c
+++ b/MagickCore/distort.c
@@ -1283,6 +1283,14 @@ static double *GenerateCoefficients(const Image *image,
           coeff[3] *= arguments[2]/coeff[2];
         coeff[2] = arguments[2];
       }
+      if ( number_arguments >= 4 && fabs(coeff[3]) < MagickEpsilon ) {
+        /* top and bottom radius are equal: radial scale would divide by 0 */
+        coeff = (double *) RelinquishMagickMemory(coeff);
+        (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+            "InvalidArgument","%s : 'Inner and Outer Radius Are Equal'",
+            CommandOptionToMnemonic(MagickDistortOptions, *method) );
+        return((double *) NULL);
+      }
       coeff[4] = ((double)image->columns-1.0)/2.0;

       return(coeff);
@@ -1642,7 +1650,7 @@ MagickExport Image *DistortResizeImage(const Image *image,const size_t columns,
       */
       (void) SetImageAlphaChannel(tmp_image,SetAlphaChannel,exception);
       resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
-        MagickTrue,exception),
+        MagickTrue,exception);
       tmp_image=DestroyImage(tmp_image);
       if (resize_image == (Image *) NULL)
         return((Image *) NULL);
@@ -1663,7 +1671,7 @@ MagickExport Image *DistortResizeImage(const Image *image,const size_t columns,
       (void) SetImageAlphaChannel(tmp_image,ExtractAlphaChannel,exception);
       (void) SetImageAlphaChannel(tmp_image,OpaqueAlphaChannel,exception);
       resize_alpha=DistortImage(tmp_image,AffineDistortion,12,distort_args,
-        MagickTrue,exception),
+        MagickTrue,exception);
       tmp_image=DestroyImage(tmp_image);
       if (resize_alpha == (Image *) NULL)
         return((Image *) NULL);
@@ -1678,7 +1686,7 @@ MagickExport Image *DistortResizeImage(const Image *image,const size_t columns,
       (void) SetImageVirtualPixelMethod(tmp_image,
         TransparentVirtualPixelMethod,exception);
       resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
-        MagickTrue,exception),
+        MagickTrue,exception);
       tmp_image=DestroyImage(tmp_image);
       if (resize_image == (Image *) NULL)
         {
@@ -1993,7 +2001,8 @@ MagickExport Image *DistortImage(const Image *image, DistortMethod method,
           in the mapping function.
         */
         coeff[1] = (double) (Magick2PI*image->columns/coeff[1]);
-        coeff[3] = (double)image->rows/coeff[3];
+        coeff[3] = (double)image->rows*MagickSafeReciprocal(coeff[3]);
+                   /* coeff[3] is 0 for a single-row source image */
         break;
       }
       case PolarDistortion:
@@ -2851,7 +2860,7 @@ if ( d.x == 0.5 && d.y == 0.5 ) {
               gy = ((3*coeff[4]*r + 2*coeff[5])*r + coeff[6])/r;
               /* adjust functions and scaling for 'inverse' form */
               if ( method == BarrelInverseDistortion ) {
-                fx = 1/fx;  fy = 1/fy;
+                fx = MagickSafeReciprocal(fx);  fy = MagickSafeReciprocal(fy);
                 gx *= -fx*fx;  gy *= -fy*fy;
               }
               /* Set the source pixel to lookup and EWA derivative vectors */
@@ -2928,10 +2937,19 @@ if ( d.x == 0.5 && d.y == 0.5 ) {
           SetPixelViaPixelInfo(distort_image,&invalid,q);
         }
         else {
-          /* resample the source image to find its correct color */
-          status=ResamplePixelColor(resample_filter[id],s.x,s.y,&pixel,
+          /*
+            Resample the source image to find its correct color.  Use a
+            thread-local result: assigning to the shared 'status' here is a
+            data race under OpenMP, lets one pixel's resample failure destroy
+            the whole output image, and lets a later success hide a real
+            cache-sync failure reported by another row.
+          */
+          MagickBooleanType
+            resampled;
+
+          resampled=ResamplePixelColor(resample_filter[id],s.x,s.y,&pixel,
             exception);
-          if (status == MagickFalse)
+          if (resampled == MagickFalse)
             SetPixelViaPixelInfo(distort_image,&invalid,q);
           else
             {
@@ -2974,7 +2992,8 @@ if ( d.x == 0.5 && d.y == 0.5 ) {
   /* Arc does not return an offset unless 'bestfit' is in effect
      And the user has not provided an overriding 'viewport'.
    */
-  if ( method == ArcDistortion && !bestfit && !viewport_given ) {
+  if ( (distort_image != (Image *) NULL) &&
+       method == ArcDistortion && !bestfit && !viewport_given ) {
     distort_image->page.x = 0;
     distort_image->page.y = 0;
   }
@@ -3140,6 +3159,17 @@ MagickExport Image *SparseColorImage(const Image *image,
   if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
       (image->alpha_trait != UndefinedPixelTrait))
     number_colors++;
+  if (number_colors == 0)
+    {
+      /*
+        GenerateCoefficients() treats number_values == 0 as an image
+        distortion (u,v,x,y control points), not a color interpolation.
+      */
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "InvalidArgument","%s : 'No color channels selected'",
+        CommandOptionToMnemonic(MagickSparseColorOptions,method));
+      return((Image *) NULL);
+    }

   /*
     Convert input arguments into mapping coefficients, in this case
diff --git a/MagickCore/distort.h b/MagickCore/distort.h
index 350ea4d6b..4bf1d7c54 100644
--- a/MagickCore/distort.h
+++ b/MagickCore/distort.h
@@ -53,6 +53,12 @@ typedef enum
   ShepardsDistortion,
   ResizeDistortion,
   SentinelDistortion,
+  /*
+    RigidAffineDistortion was appended after the sentinel to preserve the
+    ABI, so its value equals InverseColorInterpolate.  This is safe only
+    because SparseColorImage() maps every value >= SentinelDistortion to
+    Shepards before generating coefficients; keep it that way.
+  */
   RigidAffineDistortion
 } DistortMethod;

diff --git a/MagickCore/morphology-private.h b/MagickCore/morphology-private.h
index 86e6ff059..8b3d2e9e9 100644
--- a/MagickCore/morphology-private.h
+++ b/MagickCore/morphology-private.h
@@ -15,8 +15,8 @@

   The ImageMagick morphology private methods.
 */
-#ifndef _MAGICK_MORPHOLOGY_PRIVATE_H
-#define _MAGICK_MORPHOLOGY_PRIVATE_H
+#ifndef MAGICKCORE_MORPHOLOGY_PRIVATE_H
+#define MAGICKCORE_MORPHOLOGY_PRIVATE_H

 #include "MagickCore/morphology.h"

diff --git a/MagickCore/morphology.c b/MagickCore/morphology.c
index a79447efa..b4548908b 100644
--- a/MagickCore/morphology.c
+++ b/MagickCore/morphology.c
@@ -3121,8 +3121,12 @@ static ssize_t MorphologyPrimitive(const Image *image,Image *morphology_image,
             /*
               Select pixel with maximum intensity within kernel neighbourhood.

-              The kernel is not reflected for this operation.
+              The kernel is reflected for this operation.
+
+              Start below any possible intensity (mirrors the Erode case), so
+              an all-black neighbourhood still selects a real neighbour.
             */
+            maximum=(-MagickMaximumValue);
             k=(&kernel->values[kernel->width*kernel->height-1]);
             for (v=0; v < (ssize_t) kernel->height; v++)
             {
@@ -3193,6 +3197,12 @@ static ssize_t MorphologyPrimitive(const Image *image,Image *morphology_image,
         }
         if (quantum_pixels != (const Quantum *) NULL)
           {
+            /*
+              Count the change too, otherwise intensity methods always
+              report 0 changes and iterations=-1 stops after one pass.
+            */
+            if (quantum_pixels[i] != p[center+i])
+              changes[id]++;
             SetPixelChannel(morphology_image,channel,quantum_pixels[i],q);
             continue;
           }
@@ -3579,7 +3589,13 @@ static ssize_t MorphologyPrimitiveDirect(Image *image,
               }
               pixels+=(image->columns-1)*GetPixelChannels(image);
             }
-            k=(&kernel->values[(ssize_t) kernel->width*(kernel->y+1)-1]);
+            /*
+              Reflected kernel element for u=offset.x+1 on the origin row is
+              width*y+x-1 (same as the Distance case above); starting at
+              width*(y+1)-1 applied the wrong weights to the right-hand
+              neighbours.
+            */
+            k=(&kernel->values[(ssize_t) kernel->width*kernel->y+kernel->x-1]);
             pixels=q;
             for (u=offset.x+1; u < (ssize_t) kernel->width; u++)
             {
@@ -4628,7 +4644,7 @@ MagickExport void ScaleKernelInfo(KernelInfo *kernel,
   kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;

   /* swap kernel settings if user's scaling factor is negative */
-  if ( scaling_factor < MagickEpsilon ) {
+  if ( scaling_factor < 0.0 ) {  /* tiny positive factors must not swap */
     double t;
     t = kernel->positive_range;
     kernel->positive_range = kernel->negative_range;