]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/compare.c
...
[imagemagick] / MagickCore / compare.c
index 7763cd1ee37134f3f0ed9eda03dd84ab3365829b..8917bf8afbc972a57a60d80ebdb63457e748c357 100644 (file)
 %                      MagickCore Image Comparison Methods                    %
 %                                                                             %
 %                              Software Design                                %
-%                                   Cristy                                    %
+%                                  Cristy                                     %
 %                               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  %
 %    o exception: return any errors or warnings in this structure.
 %
 */
+
+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 ? (size_t) 1 : channels);
+}
+
 MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
   const MetricType metric,double *distortion,ExceptionInfo *exception)
 {
@@ -112,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;
 
@@ -124,40 +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 ((reconstruct_image->columns != image->columns) ||
-        (reconstruct_image->rows != image->rows))
-      ThrowImageException(ImageError,"ImageSizeDiffers");
   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)
     {
@@ -174,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,
@@ -225,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);
@@ -250,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);
@@ -270,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);
@@ -310,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)
 {
@@ -330,6 +369,10 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   MagickBooleanType
     status;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -337,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,
@@ -389,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);
@@ -414,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);
-}
-
 static MagickBooleanType GetFuzzDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
@@ -463,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,
@@ -512,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);
@@ -538,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);
@@ -563,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,
@@ -612,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);
@@ -638,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);
 }
@@ -670,6 +705,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
     maximum_error,
     mean_error;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -677,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,
@@ -705,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);
@@ -726,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;
@@ -754,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,
@@ -803,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);
@@ -829,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);
 }
@@ -872,6 +923,10 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   register ssize_t
     i;
 
+  size_t
+    columns,
+    rows;
+
   ssize_t
     y;
 
@@ -895,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) 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) image->columns; x++)
+    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);
@@ -944,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);
@@ -956,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);
@@ -968,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];
@@ -1002,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,
@@ -1045,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);
@@ -1079,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);
@@ -1090,9 +1194,11 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
 
 static inline double MagickLog10(const double x)
 {
-#define Log10Epsilon  (1.0e-20)
+#define Log10Epsilon  (1.0e-11)
 
- return(log10(fabs(x)+Log10Epsilon));
+ if (fabs(x) < Log10Epsilon)
+   return(log10(Log10Epsilon));
+ return(log10(fabs(x)));
 }
 
 static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
@@ -1106,149 +1212,90 @@ 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);
 }
 
-static Image *PerceptualImageHash(const Image *image,
-  const ColorspaceType colorspace,ExceptionInfo *exception)
-{
-  Image
-    *phash_image;
-
-  MagickBooleanType
-    status;
-
-  /*
-    Transform colorspace then blur perceptual hash image.
-  */
-  phash_image=BlurImage(image,0.0,1.0,exception);
-  if (phash_image == (Image *) NULL)
-    return((Image *) NULL);
-  phash_image->depth=8;
-  status=TransformImageColorspace(phash_image,colorspace,exception);
-  if (status == MagickFalse)
-    phash_image=DestroyImage(phash_image);
-  return(phash_image);
-}
-
 static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
-  ChannelMoments
-    *image_moments,
-    *reconstruct_moments;
+  ChannelPerceptualHash
+    *channel_phash,
+    *reconstruct_phash;
 
-  double
-    alpha,
-    beta,
-    difference;
-
-  Image
-    *phash_image;
+  const char
+    *artifact;
 
   MagickBooleanType
-    grayscale;
+    normalize;
 
-  register ssize_t
-    i;
+  ssize_t
+    channel;
 
   /*
-    Compute perceptual hash in the native image colorspace.
+    Compute perceptual hash in the sRGB colorspace.
   */
-  phash_image=PerceptualImageHash(image,image->colorspace,exception);
-  if (phash_image == (Image *) NULL)
+  channel_phash=GetImagePerceptualHash(image,exception);
+  if (channel_phash == (ChannelPerceptualHash *) NULL)
     return(MagickFalse);
-  image_moments=GetImageMoments(phash_image,exception);
-  phash_image=DestroyImage(phash_image);
-  if (image_moments == (ChannelMoments *) NULL)
-    return(MagickFalse);
-  phash_image=PerceptualImageHash(reconstruct_image,
-    reconstruct_image->colorspace,exception);
-  if (phash_image == (Image *) NULL)
-    {
-      image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
-      return(MagickFalse);
-    }
-  reconstruct_moments=GetImageMoments(phash_image,exception);
-  phash_image=DestroyImage(phash_image);
-  if (reconstruct_moments == (ChannelMoments *) NULL)
+  reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
+  if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
     {
-      image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
+      channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
+        channel_phash);
       return(MagickFalse);
     }
-  for (i=0; i < 7; i++)
+  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
+  for (channel=0; channel < MaxPixelChannels; channel++)
   {
-    ssize_t
-      channel;
+    double
+      difference;
 
-    /*
-      Compute sum of moment differences squared.
-    */
-    for (channel=0; channel < MaxPixelChannels; channel++)
+    register ssize_t
+      i;
+
+    difference=0.0;
+    for (i=0; i < MaximumNumberOfImageMoments; i++)
     {
-      alpha=MagickLog10(image_moments[channel].I[i]);
-      beta=MagickLog10(reconstruct_moments[channel].I[i]);
-      difference=beta-alpha;
-      distortion[channel]+=difference*difference;
-      distortion[CompositePixelChannel]+=difference*difference;
+      double
+        alpha,
+        beta;
+
+      register ssize_t
+        j;
+
+      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)
+    #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
+#endif
+    distortion[CompositePixelChannel]+=difference;
   }
-  image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
-  reconstruct_moments=(ChannelMoments *) RelinquishMagickMemory(
-    reconstruct_moments);
-  grayscale=(IsImageGray(image,exception) != MagickFalse) &&
-    (IsImageGray(reconstruct_image,exception) != MagickFalse) ? MagickTrue :
-    MagickFalse;
-  if (grayscale != MagickFalse)
-    return(MagickTrue);
   /*
-    Compute perceptual hash in the HCLP colorspace.
+    Free resources.
   */
-  phash_image=PerceptualImageHash(image,HCLpColorspace,exception);
-  if (phash_image == (Image *) NULL)
-    return(MagickFalse);
-  image_moments=GetImageMoments(phash_image,exception);
-  phash_image=DestroyImage(phash_image);
-  if (image_moments == (ChannelMoments *) NULL)
-    return(MagickFalse);
-  phash_image=PerceptualImageHash(reconstruct_image,HCLpColorspace,exception);
-  if (phash_image == (Image *) NULL)
-    {
-      image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
-      return(MagickFalse);
-    }
-  reconstruct_moments=GetImageMoments(phash_image,exception);
-  phash_image=DestroyImage(phash_image);
-  if (reconstruct_moments == (ChannelMoments *) NULL)
-    {
-      image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
-      return(MagickFalse);
-    }
-  for (i=0; i < 7; i++)
-  {
-    ssize_t
-      channel;
-
-    /*
-      Compute sum of moment differences squared.
-    */
-    for (channel=0; channel < MaxPixelChannels; channel++)
-    {
-      alpha=MagickLog10(image_moments[channel].I[i]);
-      beta=MagickLog10(reconstruct_moments[channel].I[i]);
-      difference=beta-alpha;
-      distortion[channel]+=difference*difference;
-      distortion[CompositePixelChannel]+=difference*difference;
-    }
-  }
-  image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments);
-  reconstruct_moments=(ChannelMoments *) RelinquishMagickMemory(
-    reconstruct_moments);
+  reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
+    reconstruct_phash);
+  channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
   return(MagickTrue);
 }
 
-
 static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
@@ -1278,19 +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 ((reconstruct_image->columns != image->columns) ||
-        (reconstruct_image->rows != image->rows))
-      ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
   /*
     Get image distortion.
   */
@@ -1418,21 +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 ((reconstruct_image->columns != image->columns) ||
-        (reconstruct_image->rows != image->rows))
-      {
-        (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
-          "ImageSizeDiffers","`%s'",image->filename);
-        return((double *) NULL);
-      }
   /*
     Get image distortion.
   */
@@ -1527,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
@@ -1551,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.
@@ -1565,58 +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 ((reconstruct_image->columns != image->columns) ||
-      (reconstruct_image->rows != image->rows))
-    ThrowBinaryException(ImageError,"ImageSizeDiffers",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);
@@ -1637,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);
@@ -1728,7 +1873,7 @@ static double GetSimilarityMetric(const Image *image,const Image *reference,
   return(distortion);
 }
 
-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)
 {
@@ -1750,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=1.0;
-  if ((reference->columns > image->columns) || (reference->rows > image->rows))
-    ThrowImageException(ImageError,"ImageSizeDiffers");
+  *similarity_metric=MagickMaximumValue;
   similarity_image=CloneImage(image,image->columns-reference->columns+1,
     image->rows-reference->rows+1,MagickTrue,exception);
   if (similarity_image == (Image *) NULL)
@@ -1789,7 +1932,7 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
       similarity;
 
     register Quantum
-      *restrict q;
+      *magick_restrict q;
 
     register ssize_t
       x;
@@ -1822,19 +1965,24 @@ 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;
           offset->y=y;
           *similarity_metric=similarity;
         }
-      if (GetPixelReadMask(similarity_image,q) == 0)
+      if (metric == PerceptualHashErrorMetric)
+        similarity=MagickMin(0.01*similarity,1.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);