Commit 04864dfca for imagemagick.org

commit 04864dfca459965285ac7204749f800c5c9b8cf4
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Wed Nov 4 19:31:20 2026 -0500

    https://github.com/ImageMagick/ImageMagick/issues/9097

diff --git a/MagickCore/compare.c b/MagickCore/compare.c
index 4c080ae9e..3b8a414db 100644
--- a/MagickCore/compare.c
+++ b/MagickCore/compare.c
@@ -2487,7 +2487,7 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
     default: break;
   }
   channel_similarity=(double *) RelinquishMagickMemory(channel_similarity);
-  if (fabs(*distortion) < MagickEpsilon)
+  if ((IsNaN(*distortion) != 0) || (fabs(*distortion) < MagickEpsilon))
     *distortion=0.0;
   (void) FormatImageProperty(image,"distortion","%.*g",GetMagickPrecision(),
     *distortion);
diff --git a/MagickWand/compare.c b/MagickWand/compare.c
index 50f196bd5..a6e6af5ba 100644
--- a/MagickWand/compare.c
+++ b/MagickWand/compare.c
@@ -1325,8 +1325,25 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
     default:
       break;
   }
-  if (fabs(distortion) > CompareEpsilon)
-    similar=MagickFalse;
+  switch (metric)
+  {
+    case DotProductCorrelationErrorMetric:
+    case PhaseCorrelationErrorMetric:
+    case NormalizedCrossCorrelationErrorMetric:
+    case PeakSignalToNoiseRatioErrorMetric:
+    case StructuralSimilarityErrorMetric:
+    {
+      if (fabs(distortion) < (1.0-CompareEpsilon))
+        similar=MagickFalse;
+      break;
+    }
+    default:
+    {
+      if (fabs(distortion) > CompareEpsilon)
+        similar=MagickFalse;
+      break;
+    }
+  }
   if (difference_image == (Image *) NULL)
     status=0;
   else
@@ -1406,8 +1423,9 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                   if (image->alpha_trait != UndefinedPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %.*g (%.*g)\n",
                       GetMagickPrecision(),scale*
-                      channel_distortion[AlphaPixelChannel],GetMagickPrecision(),
-                      normal_scale*channel_distortion[AlphaPixelChannel]);
+                      channel_distortion[AlphaPixelChannel],
+                      GetMagickPrecision(),normal_scale*
+                      channel_distortion[AlphaPixelChannel]);
                   break;
                 }
                 case CMYKColorspace:
@@ -1455,7 +1473,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
               }
               (void) FormatLocaleFile(stderr,"    all: %.*g (%.*g)\n",
                 GetMagickPrecision(),scale*channel_distortion[MaxPixelChannels],
-                GetMagickPrecision(),normal_scale*channel_distortion[MaxPixelChannels]);
+                GetMagickPrecision(),normal_scale*
+                channel_distortion[MaxPixelChannels]);
               break;
             }
             case DotProductCorrelationErrorMetric: