]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/compare.c
...
[imagemagick] / MagickCore / compare.c
index cf5bcf3692039471b7adb2fcf4b1442d4c2f78b3..8917bf8afbc972a57a60d80ebdb63457e748c357 100644 (file)
@@ -17,7 +17,7 @@
 %                               December 2003                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2017 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  %
 %
 */
 
-static inline MagickBooleanType ValidateImageMorphology(
-  const Image *restrict image,const Image *restrict reconstruct_image)
+static size_t GetImageChannels(const Image *image)
 {
-  /*
-    Does the image match the reconstructed image morphology?
-  */
-  if ((image->storage_class != reconstruct_image->storage_class) ||
-      (image->colorspace != reconstruct_image->colorspace) ||
-      (image->alpha_trait != reconstruct_image->alpha_trait) ||
-      (image->number_channels != reconstruct_image->number_channels) ||
-      (image->columns != reconstruct_image->columns) ||
-      (image->rows != reconstruct_image->rows))
-    return(MagickFalse);
-  return(MagickTrue);
+  register ssize_t
+    i;
+
+  size_t
+    channels;
+
+  channels=0;
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    PixelTrait traits=GetPixelChannelTraits(image,channel);
+    if ((traits & UpdatePixelTrait) != 0)
+      channels++;
+  }
+  return(channels == 0 ? (size_t) 1 : channels);
 }
 
 MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
@@ -129,10 +132,14 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
     *image_view,
     *reconstruct_view;
 
+  double
+    fuzz;
+
   const char
     *artifact;
 
   Image
+    *clone_image,
     *difference_image,
     *highlight_image;
 
@@ -141,39 +148,54 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
 
   PixelInfo
     highlight,
-    lowlight;
+    lowlight,
+    masklight;
+
+  RectangleInfo
+    geometry;
+
+  size_t
+    columns,
+    rows;
 
   ssize_t
     y;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(reconstruct_image != (const Image *) NULL);
-  assert(reconstruct_image->signature == MagickSignature);
+  assert(reconstruct_image->signature == MagickCoreSignature);
   assert(distortion != (double *) NULL);
   *distortion=0.0;
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (metric != PerceptualHashErrorMetric)
-    if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
-      ThrowImageException(ImageError,"ImageMorphologyDiffers");
   status=GetImageDistortion(image,reconstruct_image,metric,distortion,
     exception);
   if (status == MagickFalse)
     return((Image *) NULL);
-  difference_image=CloneImage(image,0,0,MagickTrue,exception);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  SetGeometry(image,&geometry);
+  geometry.width=columns;
+  geometry.height=rows;
+  clone_image=CloneImage(image,0,0,MagickTrue,exception);
+  if (clone_image == (Image *) NULL)
+    return((Image *) NULL);
+  (void) SetImageMask(clone_image,ReadPixelMask,(Image *) NULL,exception);
+  difference_image=ExtentImage(clone_image,&geometry,exception);
+  clone_image=DestroyImage(clone_image);
   if (difference_image == (Image *) NULL)
     return((Image *) NULL);
   (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
-  highlight_image=CloneImage(image,image->columns,image->rows,MagickTrue,
-    exception);
+  highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
   if (highlight_image == (Image *) NULL)
     {
       difference_image=DestroyImage(difference_image);
       return((Image *) NULL);
     }
+  (void) SetImageMask(highlight_image,ReadPixelMask,(Image *) NULL,exception);
   status=SetImageStorageClass(highlight_image,DirectClass,exception);
   if (status == MagickFalse)
     {
@@ -190,46 +212,49 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
   artifact=GetImageArtifact(image,"lowlight-color");
   if (artifact != (const char *) NULL)
     (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
+  (void) QueryColorCompliance("#888888cc",AllCompliance,&masklight,exception);
+  artifact=GetImageArtifact(image,"masklight-color");
+  if (artifact != (const char *) NULL)
+    (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
   /*
     Generate difference image.
   */
   status=MagickTrue;
+  fuzz=GetFuzzyColorDistance(image,reconstruct_image);
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
   highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,highlight_image,image->rows,1)
+    magick_threads(image,highlight_image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     MagickBooleanType
       sync;
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register Quantum
-      *restrict r;
+      *magick_restrict r;
 
     register ssize_t
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,image->columns,1,
-      exception);
-    r=QueueCacheViewAuthenticPixels(highlight_view,0,y,highlight_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
+    r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) ||
         (r == (Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -241,9 +266,10 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
-          SetPixelInfoPixel(highlight_image,&lowlight,r);
+          SetPixelViaPixelInfo(highlight_image,&masklight,r);
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
           r+=GetPixelChannels(highlight_image);
@@ -266,13 +292,16 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
             ((reconstruct_traits & UpdatePixelTrait) == 0))
           continue;
         distance=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
-        if (fabs((double) distance) >= MagickEpsilon)
-          difference=MagickTrue;
+        if ((distance*distance) > fuzz)
+          {
+            difference=MagickTrue;
+            break;
+          }
       }
       if (difference == MagickFalse)
-        SetPixelInfoPixel(highlight_image,&lowlight,r);
+        SetPixelViaPixelInfo(highlight_image,&lowlight,r);
       else
-        SetPixelInfoPixel(highlight_image,&highlight,r);
+        SetPixelViaPixelInfo(highlight_image,&highlight,r);
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
       r+=GetPixelChannels(highlight_image);
@@ -286,6 +315,7 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
   image_view=DestroyCacheView(image_view);
   (void) CompositeImage(difference_image,highlight_image,image->compose,
     MagickTrue,0,0,exception);
+  (void) SetImageAlphaChannel(difference_image,OffAlphaChannel,exception);
   highlight_image=DestroyImage(highlight_image);
   if (status == MagickFalse)
     difference_image=DestroyImage(difference_image);
@@ -326,13 +356,6 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
 %
 */
 
-static inline double MagickMax(const double x,const double y)
-{
-  if (x > y)
-    return(x);
-  return(y);
-}
-
 static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
@@ -346,6 +369,10 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   MagickBooleanType
     status;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -353,47 +380,39 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
     Compute the absolute difference in pixels between two images.
   */
   status=MagickTrue;
-  if (image->fuzz == 0.0)
-    fuzz=MagickMax(reconstruct_image->fuzz,MagickSQ1_2)*
-      MagickMax(reconstruct_image->fuzz,MagickSQ1_2);
-  else
-    if (reconstruct_image->fuzz == 0.0)
-      fuzz=MagickMax(image->fuzz,MagickSQ1_2)*
-        MagickMax(image->fuzz,MagickSQ1_2);
-    else
-      fuzz=MagickMax(image->fuzz,MagickSQ1_2)*
-        MagickMax(reconstruct_image->fuzz,MagickSQ1_2);
+  fuzz=GetFuzzyColorDistance(image,reconstruct_image);
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,image,image->rows,1)
+    magick_threads(image,image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     double
       channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
-      i,
+      j,
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -405,7 +424,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if (GetPixelWriteMask(image,p) == 0)
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -430,48 +449,26 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
         distance=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
         if ((distance*distance) > fuzz)
           {
+            channel_distortion[i]++;
             difference=MagickTrue;
-            break;
           }
       }
       if (difference != MagickFalse)
-        {
-          channel_distortion[i]++;
-          channel_distortion[CompositePixelChannel]++;
-        }
+        channel_distortion[CompositePixelChannel]++;
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
     #pragma omp critical (MagickCore_GetAbsoluteError)
 #endif
-    for (i=0; i <= MaxPixelChannels; i++)
-      distortion[i]+=channel_distortion[i];
+    for (j=0; j <= MaxPixelChannels; j++)
+      distortion[j]+=channel_distortion[j];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
   return(status);
 }
 
-static size_t GetImageChannels(const Image *image)
-{
-  register ssize_t
-    i;
-
-  size_t
-    channels;
-
-  channels=0;
-  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
-  {
-    PixelChannel channel=GetPixelChannelChannel(image,i);
-    PixelTrait traits=GetPixelChannelTraits(image,channel);
-    if ((traits & UpdatePixelTrait) != 0)
-      channels++;
-  }
-  return(channels == 0 ? 1 : channels);
-}
-
 static MagickBooleanType GetFuzzDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
@@ -479,47 +476,55 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
+  double
+    area;
+
   MagickBooleanType
     status;
 
   register ssize_t
-    i;
+    j;
+
+  size_t
+    columns,
+    rows;
 
   ssize_t
     y;
 
   status=MagickTrue;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  area=0.0;
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,image,image->rows,1)
+    magick_threads(image,image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     double
       channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
-      i,
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -528,7 +533,8 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -554,19 +560,21 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
         channel_distortion[i]+=distance*distance;
         channel_distortion[CompositePixelChannel]+=distance*distance;
       }
+      area++;
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
     #pragma omp critical (MagickCore_GetFuzzDistortion)
 #endif
-    for (i=0; i <= MaxPixelChannels; i++)
-      distortion[i]+=channel_distortion[i];
+    for (j=0; j <= MaxPixelChannels; j++)
+      distortion[j]+=channel_distortion[j];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= MaxPixelChannels; i++)
-    distortion[i]/=((double) image->columns*image->rows);
+  area=PerceptibleReciprocal(area);
+  for (j=0; j <= MaxPixelChannels; j++)
+    distortion[j]*=area;
   distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
   distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]);
   return(status);
@@ -579,47 +587,55 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
+  double
+    area;
+
   MagickBooleanType
     status;
 
   register ssize_t
-    i;
+    j;
+
+  size_t
+    columns,
+    rows;
 
   ssize_t
     y;
 
   status=MagickTrue;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  area=0.0;
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,image,image->rows,1)
+    magick_threads(image,image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     double
       channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
-      i,
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -628,7 +644,8 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -654,19 +671,21 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
         channel_distortion[i]+=distance;
         channel_distortion[CompositePixelChannel]+=distance;
       }
+      area++;
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
     #pragma omp critical (MagickCore_GetMeanAbsoluteError)
 #endif
-    for (i=0; i <= MaxPixelChannels; i++)
-      distortion[i]+=channel_distortion[i];
+    for (j=0; j <= MaxPixelChannels; j++)
+      distortion[j]+=channel_distortion[j];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= MaxPixelChannels; i++)
-    distortion[i]/=((double) image->columns*image->rows);
+  area=PerceptibleReciprocal(area);
+  for (j=0; j <= MaxPixelChannels; j++)
+    distortion[j]*=area;
   distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
   return(status);
 }
@@ -686,6 +705,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
     maximum_error,
     mean_error;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -693,26 +716,27 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
   area=0.0;
   maximum_error=0.0;
   mean_error=0.0;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
       x;
 
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         break;
       }
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -721,7 +745,8 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -742,8 +767,7 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
             (reconstruct_traits == UndefinedPixelTrait) ||
             ((reconstruct_traits & UpdatePixelTrait) == 0))
           continue;
-        distance=fabs((double) (Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
-          channel,q)));
+        distance=fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q));
         distortion[i]+=distance;
         distortion[CompositePixelChannel]+=distance;
         mean_error+=distance*distance;
@@ -770,47 +794,55 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
+  double
+    area;
+
   MagickBooleanType
     status;
 
   register ssize_t
-    i;
+    j;
+
+  size_t
+    columns,
+    rows;
 
   ssize_t
     y;
 
   status=MagickTrue;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  area=0.0;
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,image,image->rows,1)
+    magick_threads(image,image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     double
       channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
-      i,
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -819,7 +851,8 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -845,19 +878,21 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
         channel_distortion[i]+=distance*distance;
         channel_distortion[CompositePixelChannel]+=distance*distance;
       }
+      area++;
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
     #pragma omp critical (MagickCore_GetMeanSquaredError)
 #endif
-    for (i=0; i <= MaxPixelChannels; i++)
-      distortion[i]+=channel_distortion[i];
+    for (j=0; j <= MaxPixelChannels; j++)
+      distortion[j]+=channel_distortion[j];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= MaxPixelChannels; i++)
-    distortion[i]/=((double) image->columns*image->rows);
+  area=PerceptibleReciprocal(area);
+  for (j=0; j <= MaxPixelChannels; j++)
+    distortion[j]*=area;
   distortion[CompositePixelChannel]/=GetImageChannels(image);
   return(status);
 }
@@ -888,6 +923,10 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   register ssize_t
     i;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -911,45 +950,75 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   progress=0;
   for (i=0; i <= MaxPixelChannels; i++)
     distortion[i]=0.0;
-  area=1.0/((double) image->columns*image->rows);
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  area=0.0;
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
       x;
 
-    if (status == MagickFalse)
-      continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
-        continue;
+        break;
       }
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
+    {
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
+        {
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
+        }
+      area++;
+      p+=GetPixelChannels(image);
+      q+=GetPixelChannels(reconstruct_image);
+    }
+  }
+  area=PerceptibleReciprocal(area);
+  for (y=0; y < (ssize_t) rows; y++)
+  {
+    register const Quantum
+      *magick_restrict p,
+      *magick_restrict q;
+
+    register ssize_t
+      x;
+
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
+      {
+        status=MagickFalse;
+        break;
+      }
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
         Sa;
 
-      register ssize_t
-        i;
-
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
           continue;
         }
-      Sa=QuantumScale*GetPixelAlpha(image,p);
-      Da=QuantumScale*GetPixelAlpha(reconstruct_image,q);
+      Sa=QuantumScale*(image->alpha_trait != UndefinedPixelTrait ?
+        GetPixelAlpha(image,p) : OpaqueAlpha);
+      Da=QuantumScale*(reconstruct_image->alpha_trait != UndefinedPixelTrait ?
+        GetPixelAlpha(reconstruct_image,q) : OpaqueAlpha);
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
         PixelChannel channel=GetPixelChannelChannel(image,i);
@@ -960,9 +1029,20 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
             (reconstruct_traits == UndefinedPixelTrait) ||
             ((reconstruct_traits & UpdatePixelTrait) == 0))
           continue;
-        distortion[i]+=area*QuantumScale*(Sa*p[i]-image_statistics[i].mean)*
-          (Da*GetPixelChannel(reconstruct_image,channel,q)-
-          reconstruct_statistics[channel].mean);
+        if (channel == AlphaPixelChannel)
+          {
+            distortion[i]+=area*QuantumScale*(p[i]-
+              image_statistics[channel].mean)*(GetPixelChannel(
+              reconstruct_image,channel,q)-
+              reconstruct_statistics[channel].mean);
+          }
+        else
+          {
+            distortion[i]+=area*QuantumScale*(Sa*p[i]-
+              image_statistics[channel].mean)*(Da*GetPixelChannel(
+              reconstruct_image,channel,q)-
+              reconstruct_statistics[channel].mean);
+          }
       }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
@@ -972,10 +1052,12 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
         MagickBooleanType
           proceed;
 
-        proceed=SetImageProgress(image,SimilarityImageTag,progress++,
-          image->rows);
+        proceed=SetImageProgress(image,SimilarityImageTag,progress++,rows);
         if (proceed == MagickFalse)
-          status=MagickFalse;
+          {
+            status=MagickFalse;
+            break;
+          }
       }
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
@@ -984,13 +1066,13 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
     Divide by the standard deviation.
   */
   distortion[CompositePixelChannel]=0.0;
-  for (i=0; i < MaxPixelChannels; i++)
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
   {
     double
       gamma;
 
     PixelChannel channel=GetPixelChannelChannel(image,i);
-    gamma=image_statistics[i].standard_deviation*
+    gamma=image_statistics[channel].standard_deviation*
       reconstruct_statistics[channel].standard_deviation;
     gamma=PerceptibleReciprocal(gamma);
     distortion[i]=QuantumRange*gamma*distortion[i];
@@ -1018,41 +1100,46 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
   MagickBooleanType
     status;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
   status=MagickTrue;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4) shared(status) \
-    magick_threads(image,image,image->rows,1)
+    magick_threads(image,image,rows,1)
 #endif
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     double
       channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
-      i,
+      j,
       x;
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       double
         Da,
@@ -1061,7 +1148,8 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if ((GetPixelReadMask(image,p) == 0) ||
+          (GetPixelReadMask(reconstruct_image,q) == 0))
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -1095,9 +1183,9 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
     #pragma omp critical (MagickCore_GetPeakAbsoluteError)
 #endif
-    for (i=0; i <= MaxPixelChannels; i++)
-      if (channel_distortion[i] > distortion[i])
-        distortion[i]=channel_distortion[i];
+    for (j=0; j <= MaxPixelChannels; j++)
+      if (channel_distortion[j] > distortion[j])
+        distortion[j]=channel_distortion[j];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
@@ -1124,7 +1212,8 @@ static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
 
   status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
   for (i=0; i <= MaxPixelChannels; i++)
-    distortion[i]=20.0*MagickLog10((double) 1.0/sqrt(distortion[i]));
+    if (fabs(distortion[i]) >= MagickEpsilon)
+      distortion[i]=20.0*MagickLog10((double) 1.0/sqrt(distortion[i]));
   return(status);
 }
 
@@ -1132,24 +1221,34 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
   ChannelPerceptualHash
-    *image_phash,
+    *channel_phash,
     *reconstruct_phash;
 
+  const char
+    *artifact;
+
+  MagickBooleanType
+    normalize;
+
   ssize_t
     channel;
 
   /*
     Compute perceptual hash in the sRGB colorspace.
   */
-  image_phash=GetImagePerceptualHash(image,exception);
-  if (image_phash == (ChannelPerceptualHash *) NULL)
+  channel_phash=GetImagePerceptualHash(image,exception);
+  if (channel_phash == (ChannelPerceptualHash *) NULL)
     return(MagickFalse);
   reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
   if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
     {
-      image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
+      channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
+        channel_phash);
       return(MagickFalse);
     }
+  artifact=GetImageArtifact(image,"phash:normalize");
+  normalize=(artifact == (const char *) NULL) ||
+    (IsStringTrue(artifact) == MagickFalse) ? MagickFalse : MagickTrue;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp parallel for schedule(static,4)
 #endif
@@ -1162,46 +1261,25 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
       i;
 
     difference=0.0;
-    for (i=0; i < 7; i++)
+    for (i=0; i < MaximumNumberOfImageMoments; i++)
     {
       double
         alpha,
         beta;
 
-      alpha=image_phash[channel].P[i];
-      beta=reconstruct_phash[channel].P[i];
-      difference+=(beta-alpha)*(beta-alpha);
-    }
-    distortion[channel]+=difference;
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
-    #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
-#endif
-    distortion[CompositePixelChannel]+=difference;
-  }
-  /*
-    Compute perceptual hash in the HCLP colorspace.
-  */
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(static,4)
-#endif
-  for (channel=0; channel < MaxPixelChannels; channel++)
-  {
-    double
-      difference;
-
-    register ssize_t
-      i;
-
-    difference=0.0;
-    for (i=0; i < 7; i++)
-    {
-      double
-        alpha,
-        beta;
+      register ssize_t
+        j;
 
-      alpha=image_phash[channel].Q[i];
-      beta=reconstruct_phash[channel].Q[i];
-      difference+=(beta-alpha)*(beta-alpha);
+      for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
+      {
+        alpha=channel_phash[channel].phash[j][i];
+        beta=reconstruct_phash[channel].phash[j][i];
+        if (normalize == MagickFalse)
+          difference+=(beta-alpha)*(beta-alpha);
+        else
+          difference=sqrt((beta-alpha)*(beta-alpha)/
+            channel_phash[0].number_channels);
+      }
     }
     distortion[channel]+=difference;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
@@ -1214,12 +1292,10 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   */
   reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
     reconstruct_phash);
-  image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
+  channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
   return(MagickTrue);
 }
 
-
-
 static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
@@ -1249,18 +1325,15 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
     length;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(reconstruct_image != (const Image *) NULL);
-  assert(reconstruct_image->signature == MagickSignature);
+  assert(reconstruct_image->signature == MagickCoreSignature);
   assert(distortion != (double *) NULL);
   *distortion=0.0;
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (metric != PerceptualHashErrorMetric)
-    if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
-      ThrowBinaryException(ImageError,"ImageMorphologyDiffers",image->filename);
   /*
     Get image distortion.
   */
@@ -1388,20 +1461,13 @@ MagickExport double *GetImageDistortions(Image *image,
     length;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(reconstruct_image != (const Image *) NULL);
-  assert(reconstruct_image->signature == MagickSignature);
+  assert(reconstruct_image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (metric != PerceptualHashErrorMetric)
-    if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
-      {
-        (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
-          "ImageMorphologyDiffers","`%s'",image->filename);
-        return((double *) NULL);
-      }
   /*
     Get image distortion.
   */
@@ -1496,7 +1562,112 @@ MagickExport double *GetImageDistortions(Image *image,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  IsImagesEqual() measures the difference between colors at each pixel
+%  IsImagesEqual() compare the pixels of two images and returns immediately
+%  if any pixel is not identical.
+%
+%  The format of the IsImagesEqual method is:
+%
+%      MagickBooleanType IsImagesEqual(const Image *image,
+%        const Image *reconstruct_image,ExceptionInfo *exception)
+%
+%  A description of each parameter follows.
+%
+%    o image: the image.
+%
+%    o reconstruct_image: the reconstruct image.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+MagickExport MagickBooleanType IsImagesEqual(const Image *image,
+  const Image *reconstruct_image,ExceptionInfo *exception)
+{
+  CacheView
+    *image_view,
+    *reconstruct_view;
+
+  size_t
+    columns,
+    rows;
+
+  ssize_t
+    y;
+
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickCoreSignature);
+  assert(reconstruct_image != (const Image *) NULL);
+  assert(reconstruct_image->signature == MagickCoreSignature);
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
+  for (y=0; y < (ssize_t) rows; y++)
+  {
+    register const Quantum
+      *magick_restrict p,
+      *magick_restrict q;
+
+    register ssize_t
+      x;
+
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
+    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+      break;
+    for (x=0; x < (ssize_t) columns; x++)
+    {
+      register ssize_t
+        i;
+
+      if (GetPixelWriteMask(image,p) == 0)
+        {
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel channel=GetPixelChannelChannel(image,i);
+        PixelTrait traits=GetPixelChannelTraits(image,channel);
+        PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image,
+          channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=fabs(p[i]-(double) GetPixelChannel(reconstruct_image,
+          channel,q));
+        if (distance >= MagickEpsilon)
+          break;
+      }
+      if (i < (ssize_t) GetPixelChannels(image))
+        break;
+      p+=GetPixelChannels(image);
+      q+=GetPixelChannels(reconstruct_image);
+    }
+    if (x < (ssize_t) columns)
+      break;
+  }
+  reconstruct_view=DestroyCacheView(reconstruct_view);
+  image_view=DestroyCacheView(image_view);
+  return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%  S e t I m a g e C o l o r M e t r i c                                      %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  SetImageColorMetric() measures the difference between colors at each pixel
 %  location of two images.  A value other than 0 means the colors match
 %  exactly.  Otherwise an error measure is computed by summing over all
 %  pixels in an image the distance squared in RGB space between each image
@@ -1520,9 +1691,9 @@ MagickExport double *GetImageDistortions(Image *image,
 %  image->normalized_mean_error, suggests the images are very similar in
 %  spatial layout and color.
 %
-%  The format of the IsImagesEqual method is:
+%  The format of the SetImageColorMetric method is:
 %
-%      MagickBooleanType IsImagesEqual(Image *image,
+%      MagickBooleanType SetImageColorMetric(Image *image,
 %        const Image *reconstruct_image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
@@ -1534,57 +1705,60 @@ MagickExport double *GetImageDistortions(Image *image,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport MagickBooleanType IsImagesEqual(Image *image,
+MagickExport MagickBooleanType SetImageColorMetric(Image *image,
   const Image *reconstruct_image,ExceptionInfo *exception)
 {
   CacheView
     *image_view,
     *reconstruct_view;
 
-  MagickBooleanType
-    status;
-
   double
     area,
     maximum_error,
     mean_error,
     mean_error_per_pixel;
 
+  MagickBooleanType
+    status;
+
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   assert(reconstruct_image != (const Image *) NULL);
-  assert(reconstruct_image->signature == MagickSignature);
-  if (ValidateImageMorphology(image,reconstruct_image) == MagickFalse)
-    ThrowBinaryException(ImageError,"ImageMorphologyDiffers",image->filename);
+  assert(reconstruct_image->signature == MagickCoreSignature);
   area=0.0;
   maximum_error=0.0;
   mean_error_per_pixel=0.0;
   mean_error=0.0;
+  rows=MagickMax(image->rows,reconstruct_image->rows);
+  columns=MagickMax(image->columns,reconstruct_image->columns);
   image_view=AcquireVirtualCacheView(image,exception);
   reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; y < (ssize_t) rows; y++)
   {
     register const Quantum
-      *restrict p,
-      *restrict q;
+      *magick_restrict p,
+      *magick_restrict q;
 
     register ssize_t
       x;
 
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
-    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
-      1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
       break;
-    for (x=0; x < (ssize_t) image->columns; x++)
+    for (x=0; x < (ssize_t) columns; x++)
     {
       register ssize_t
         i;
 
-      if (GetPixelReadMask(image,p) == 0)
+      if (GetPixelWriteMask(image,p) == 0)
         {
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(reconstruct_image);
@@ -1605,10 +1779,13 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
           continue;
         distance=fabs(p[i]-(double) GetPixelChannel(reconstruct_image,
           channel,q));
-        mean_error_per_pixel+=distance;
-        mean_error+=distance*distance;
-        if (distance > maximum_error)
-          maximum_error=distance;
+        if (distance >= MagickEpsilon)
+          {
+            mean_error_per_pixel+=distance;
+            mean_error+=distance*distance;
+            if (distance > maximum_error)
+              maximum_error=distance;
+          }
         area++;
       }
       p+=GetPixelChannels(image);
@@ -1696,14 +1873,7 @@ static double GetSimilarityMetric(const Image *image,const Image *reference,
   return(distortion);
 }
 
-static inline double MagickMin(const double x,const double y)
-{
-  if (x < y)
-    return(x);
-  return(y);
-}
-
-MagickExport Image *SimilarityImage(Image *image,const Image *reference,
+MagickExport Image *SimilarityImage(const Image *image,const Image *reference,
   const MetricType metric,const double similarity_threshold,
   RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
 {
@@ -1725,16 +1895,14 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
     y;
 
   assert(image != (const Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   assert(offset != (RectangleInfo *) NULL);
   SetGeometry(reference,offset);
   *similarity_metric=MagickMaximumValue;
-  if (ValidateImageMorphology(image,reference) == MagickFalse)
-    ThrowImageException(ImageError,"ImageMorphologyDiffers");
   similarity_image=CloneImage(image,image->columns-reference->columns+1,
     image->rows-reference->rows+1,MagickTrue,exception);
   if (similarity_image == (Image *) NULL)
@@ -1764,7 +1932,7 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
       similarity;
 
     register Quantum
-      *restrict q;
+      *magick_restrict q;
 
     register ssize_t
       x;
@@ -1797,6 +1965,9 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
       #pragma omp critical (MagickCore_SimilarityImage)
 #endif
+      if ((metric == NormalizedCrossCorrelationErrorMetric) ||
+          (metric == UndefinedErrorMetric))
+        similarity=1.0-similarity;
       if (similarity < *similarity_metric)
         {
           offset->x=x;
@@ -1805,13 +1976,13 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
         }
       if (metric == PerceptualHashErrorMetric)
         similarity=MagickMin(0.01*similarity,1.0);
-      if (GetPixelReadMask(similarity_image,q) == 0)
+      if (GetPixelWriteMask(similarity_image,q) == 0)
         {
           SetPixelBackgoundColor(similarity_image,q);
           q+=GetPixelChannels(similarity_image);
           continue;
         }
-      for (i=0; i < (ssize_t) GetPixelChannels(similarity_image); i++)
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
         PixelChannel channel=GetPixelChannelChannel(image,i);
         PixelTrait traits=GetPixelChannelTraits(image,channel);