]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/compare.c
(no commit message)
[imagemagick] / MagickCore / compare.c
index 9439aceaf80daed9c24404001024a65e0708632f..f255bfeeb7099c820af4185428648268087dfa7a 100644 (file)
@@ -17,7 +17,7 @@
 %                               December 2003                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2013 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  %
@@ -64,6 +64,7 @@
 #include "MagickCore/resource_.h"
 #include "MagickCore/string_.h"
 #include "MagickCore/statistic.h"
+#include "MagickCore/thread-private.h"
 #include "MagickCore/transform.h"
 #include "MagickCore/utility.h"
 #include "MagickCore/version.h"
@@ -79,7 +80,7 @@
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  CompareImages() compares one or more pixel channels of an image to a 
+%  CompareImages() compares one or more pixel channels of an image to a
 %  reconstructed image and returns the difference image.
 %
 %  The format of the CompareImages method is:
@@ -115,16 +116,15 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
     *difference_image,
     *highlight_image;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
   PixelInfo
     highlight,
-    lowlight,
-    zero;
+    lowlight;
+
+  ssize_t
+    y;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -154,56 +154,48 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
       difference_image=DestroyImage(difference_image);
       return((Image *) NULL);
     }
-  if (SetImageStorageClass(highlight_image,DirectClass,exception) == MagickFalse)
+  status=SetImageStorageClass(highlight_image,DirectClass,exception);
+  if (status == MagickFalse)
     {
       difference_image=DestroyImage(difference_image);
       highlight_image=DestroyImage(highlight_image);
       return((Image *) NULL);
     }
   (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
-  (void) QueryMagickColor("#f1001ecc",&highlight,exception);
+  (void) QueryColorCompliance("#f1001e33",AllCompliance,&highlight,exception);
   artifact=GetImageArtifact(image,"highlight-color");
   if (artifact != (const char *) NULL)
-    (void) QueryMagickColor(artifact,&highlight,exception);
-  (void) QueryMagickColor("#ffffffcc",&lowlight,exception);
+    (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
+  (void) QueryColorCompliance("#ffffff33",AllCompliance,&lowlight,exception);
   artifact=GetImageArtifact(image,"lowlight-color");
   if (artifact != (const char *) NULL)
-    (void) QueryMagickColor(artifact,&lowlight,exception);
-  if (highlight_image->colorspace == CMYKColorspace)
-    {
-      ConvertRGBToCMYK(&highlight);
-      ConvertRGBToCMYK(&lowlight);
-    }
+    (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
   /*
     Generate difference image.
   */
   status=MagickTrue;
-  GetPixelInfo(image,&zero);
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
-  highlight_view=AcquireCacheView(highlight_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(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,highlight_image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     MagickBooleanType
       sync;
 
-    PixelInfo
-      pixel,
-      reconstruct_pixel;
-
     register const Quantum
       *restrict p,
       *restrict q;
 
-    register ssize_t
-      x;
-
     register Quantum
       *restrict r;
 
+    register ssize_t
+      x;
+
     if (status == MagickFalse)
       continue;
     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
@@ -211,52 +203,56 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
       1,exception);
     r=QueueCacheViewAuthenticPixels(highlight_view,0,y,highlight_image->columns,
       1,exception);
-    if ((p == (const Quantum *) NULL) ||
-        (q == (Quantum *) NULL) || (r == (Quantum *) NULL))
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) ||
+        (r == (Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
-    pixel=zero;
-    reconstruct_pixel=zero;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       MagickStatusType
         difference;
 
-      SetPixelInfo(image,p,&pixel);
-      SetPixelInfo(reconstruct_image,q,&reconstruct_pixel);
-      difference=MagickFalse;
-      if (image->sync == MagickTrue)
-        {
-          if (IsFuzzyEquivalencePixelInfo(&pixel,&reconstruct_pixel) == MagickFalse)
-            difference=MagickTrue;
-        }
-      else
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,p) != 0)
         {
-          if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
-              fabs(GetPixelRed(image,p)-GetPixelRed(reconstruct_image,q)) >= MagickEpsilon)
-            difference=MagickTrue;
-          if (((GetPixelGreenTraits(image) & UpdatePixelTrait)-0) &&
-              fabs(GetPixelGreen(image,p)-GetPixelGreen(reconstruct_image,q)) >= MagickEpsilon)
-            difference=MagickTrue;
-          if (((GetPixelBlueTraits(image) & UpdatePixelTrait)-0) &&
-              fabs(GetPixelBlue(image,p)-GetPixelBlue(reconstruct_image,q)) >= MagickEpsilon)
-            difference=MagickTrue;
-          if ((((GetPixelBlackTraits(image) & UpdatePixelTrait)-0) &&
-               (image->colorspace == CMYKColorspace) &&
-               (reconstruct_image->colorspace == CMYKColorspace)) &&
-              fabs(GetPixelBlack(image,p)-GetPixelBlack(reconstruct_image,q)) >= MagickEpsilon)
-            difference=MagickTrue;
-          if (((GetPixelAlphaTraits(image) & UpdatePixelTrait)-0) &&
-              (image->matte-MagickFalse) &&
-              fabs(GetPixelAlpha(image,p)-GetPixelAlpha(reconstruct_image,q)) >= MagickEpsilon)
-            difference=MagickTrue;
+          SetPixelInfoPixel(highlight_image,&lowlight,r);
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          r+=GetPixelChannels(highlight_image);
+          continue;
         }
-      if (difference-MagickFalse)
-        SetPixelPixelInfo(highlight_image,&highlight,r);
+      difference=MagickFalse;
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=p[i]-(double) GetPixelChannel(reconstruct_image,channel,q);
+        if (fabs((double) distance) >= MagickEpsilon)
+          difference=MagickTrue;
+      }
+      if (difference == MagickFalse)
+        SetPixelInfoPixel(highlight_image,&lowlight,r);
       else
-        SetPixelPixelInfo(highlight_image,&lowlight,r);
+        SetPixelInfoPixel(highlight_image,&highlight,r);
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
       r+=GetPixelChannels(highlight_image);
@@ -268,7 +264,8 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
   highlight_view=DestroyCacheView(highlight_view);
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  (void) CompositeImage(difference_image,image->compose,highlight_image,0,0);
+  (void) CompositeImage(difference_image,highlight_image,image->compose,
+    MagickTrue,0,0,exception);
   highlight_image=DestroyImage(highlight_image);
   if (status == MagickFalse)
     difference_image=DestroyImage(difference_image);
@@ -286,10 +283,10 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetImageDistortion() compares one or more pixel channels of an image to a 
+%  GetImageDistortion() compares one or more pixel channels of an image to a
 %  reconstructed image and returns the specified distortion metric.
 %
-%  The format of the CompareImages method is:
+%  The format of the GetImageDistortion method is:
 %
 %      MagickBooleanType GetImageDistortion(const Image *image,
 %        const Image *reconstruct_image,const MetricType metric,
@@ -319,9 +316,6 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   MagickBooleanType
     status;
 
-  PixelInfo
-    zero;
-
   ssize_t
     y;
 
@@ -329,20 +323,16 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
     Compute the absolute difference in pixels between two images.
   */
   status=MagickTrue;
-  GetPixelInfo(image,&zero);
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[CompositeChannels+1];
-
-    PixelInfo
-      pixel,
-      reconstruct_pixel;
+      channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
       *restrict p,
@@ -357,41 +347,58 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
       1,exception);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
-    pixel=zero;
-    reconstruct_pixel=pixel;
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      SetPixelInfo(image,p,&pixel);
-      SetPixelInfo(reconstruct_image,q,&reconstruct_pixel);
-      if (IsFuzzyEquivalencePixelInfo(&pixel,&reconstruct_pixel) == MagickFalse)
+      MagickBooleanType
+        difference;
+
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,p) != 0)
+        {
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
+        }
+      difference=MagickFalse;
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        if (p[i] != GetPixelChannel(reconstruct_image,channel,q))
+          difference=MagickTrue;
+      }
+      if (difference != MagickFalse)
         {
-          if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-            channel_distortion[RedChannel]++;
-          if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-            channel_distortion[GreenChannel]++;
-          if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-            channel_distortion[BlueChannel]++;
-          if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-              (image->matte != MagickFalse))
-            channel_distortion[OpacityChannel]++;
-          if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-              (image->colorspace == CMYKColorspace))
-            channel_distortion[BlackChannel]++;
-          channel_distortion[CompositeChannels]++;
+          channel_distortion[i]++;
+          channel_distortion[CompositePixelChannel]++;
         }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GetAbsoluteError)
+    #pragma omp critical (MagickCore_GetAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) CompositeChannels; i++)
+    for (i=0; i <= MaxPixelChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
@@ -399,24 +406,28 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   return(status);
 }
 
-static size_t GetNumberChannels(const Image *image)
+static size_t GetImageChannels(const Image *image)
 {
+  register ssize_t
+    i;
+
   size_t
     channels;
 
   channels=0;
-  if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-    channels++;
-  if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-    channels++;
-  if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-    channels++;
-  if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->colorspace == CMYKColorspace))
-    channels++;
-  if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-       (image->matte != MagickFalse))
-    channels++;
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    PixelChannel
+      channel;
+
+    PixelTrait
+      traits;
+
+    channel=GetPixelChannelChannel(image,i);
+    traits=GetPixelChannelTraits(image,channel);
+    if ((traits & UpdatePixelTrait) != 0)
+      channels++;
+  }
   return(channels);
 }
 
@@ -437,15 +448,16 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
     y;
 
   status=MagickTrue;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[CompositeChannels+1];
+      channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
       *restrict p,
@@ -468,68 +480,55 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
+      register ssize_t
+        i;
 
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*(GetPixelRed(image,p)-(MagickRealType)
-            GetPixelRed(reconstruct_image,q));
-          channel_distortion[RedChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*(GetPixelGreen(image,p)-(MagickRealType)
-            GetPixelGreen(reconstruct_image,q));
-          channel_distortion[GreenChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*(GetPixelBlue(image,p)-(MagickRealType)
-            GetPixelBlue(reconstruct_image,q));
-          channel_distortion[BlueChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && ((image->matte != MagickFalse) ||
-          (reconstruct_image->matte != MagickFalse)))
+      if (GetPixelMask(image,p) != 0)
         {
-          distance=QuantumScale*((image->matte != MagickFalse ?
-            GetPixelAlpha(image,p) : OpaqueAlpha)-(reconstruct_image->matte !=
-            MagickFalse ? GetPixelAlpha(reconstruct_image,q): OpaqueAlpha));
-          channel_distortion[OpacityChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        {
-          distance=QuantumScale*(GetPixelBlack(image,p)-(MagickRealType)
-            GetPixelBlack(reconstruct_image,q));
-          channel_distortion[BlackChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=QuantumScale*(p[i]-(double) GetPixelChannel(reconstruct_image,
+          channel,q));
+        distance*=distance;
+        channel_distortion[i]+=distance;
+        channel_distortion[CompositePixelChannel]+=distance;
+      }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GetMeanSquaredError)
+    #pragma omp critical (MagickCore_GetFuzzDistortion)
 #endif
-    for (i=0; i <= (ssize_t) CompositeChannels; i++)
+    for (i=0; i <= MaxPixelChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) CompositeChannels; i++)
+  for (i=0; i <= MaxPixelChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
-  if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-      ((image->matte != MagickFalse) ||
-      (reconstruct_image->matte != MagickFalse)))
-    distortion[CompositeChannels]/=(double) (GetNumberChannels(image)-1);
-  else
-    distortion[CompositeChannels]/=(double) GetNumberChannels(image);
-  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]);
+  distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
+  distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]);
   return(status);
 }
 
@@ -550,15 +549,16 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
     y;
 
   status=MagickTrue;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[CompositeChannels+1];
+      channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
       *restrict p,
@@ -571,9 +571,9 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
     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);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
+      1,exception);
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
@@ -581,60 +581,53 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
+      register ssize_t
+        i;
 
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
+      if (GetPixelMask(image,p) != 0)
         {
-          distance=QuantumScale*fabs(GetPixelRed(image,p)-(double)
-            GetPixelRed(reconstruct_image,q));
-          channel_distortion[RedChannel]+=distance;
-          channel_distortion[CompositeChannels]+=distance;
-        }
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*fabs(GetPixelGreen(image,p)-(double)
-            GetPixelGreen(reconstruct_image,q));
-          channel_distortion[GreenChannel]+=distance;
-          channel_distortion[CompositeChannels]+=distance;
-        }
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*fabs(GetPixelBlue(image,p)-(double)
-            GetPixelBlue(reconstruct_image,q));
-          channel_distortion[BlueChannel]+=distance;
-          channel_distortion[CompositeChannels]+=distance;
-        }
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace))
-        {
-          distance=QuantumScale*fabs(GetPixelBlack(image,p)-(double)
-            GetPixelBlack(reconstruct_image,q));
-          channel_distortion[BlackChannel]+=distance;
-          channel_distortion[CompositeChannels]+=distance;
-        }
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte != MagickFalse))
-        {
-          distance=QuantumScale*fabs(GetPixelAlpha(image,p)-(double)
-            GetPixelAlpha(reconstruct_image,q));
-          channel_distortion[OpacityChannel]+=distance;
-          channel_distortion[CompositeChannels]+=distance;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=QuantumScale*fabs(p[i]-(double) GetPixelChannel(
+          reconstruct_image,channel,q));
+        channel_distortion[i]+=distance;
+        channel_distortion[CompositePixelChannel]+=distance;
+      }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GetMeanAbsoluteError)
+    #pragma omp critical (MagickCore_GetMeanAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) CompositeChannels; i++)
+    for (i=0; i <= MaxPixelChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) CompositeChannels; i++)
+  for (i=0; i <= MaxPixelChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
-  distortion[CompositeChannels]/=(double) GetNumberChannels(image);
+  distortion[CompositePixelChannel]/=(double) GetImageChannels(image);
   return(status);
 }
 
@@ -648,7 +641,7 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
   MagickBooleanType
     status;
 
-  MagickRealType
+  double
     alpha,
     area,
     beta,
@@ -664,8 +657,8 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
   area=0.0;
   maximum_error=0.0;
   mean_error=0.0;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -678,90 +671,57 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
       1,exception);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         break;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
+      register ssize_t
+        i;
 
-      if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
+      if (GetPixelMask(image,p) != 0)
         {
-          if (image->matte != MagickFalse)
-            alpha=(MagickRealType) (QuantumScale*(
-              GetPixelAlpha(reconstruct_image,p)));
-          if (reconstruct_image->matte != MagickFalse)
-            beta=(MagickRealType) (QuantumScale*
-              GetPixelAlpha(reconstruct_image,q));
-        }
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=fabs(alpha*GetPixelRed(image,p)-beta*
-            GetPixelRed(reconstruct_image,q));
-          distortion[RedChannel]+=distance;
-          distortion[CompositeChannels]+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
-        }
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=fabs(alpha*GetPixelGreen(image,p)-beta*
-            GetPixelGreen(reconstruct_image,q));
-          distortion[GreenChannel]+=distance;
-          distortion[CompositeChannels]+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
-        }
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=fabs(alpha*GetPixelBlue(image,p)-beta*
-            GetPixelBlue(reconstruct_image,q));
-          distortion[BlueChannel]+=distance;
-          distortion[CompositeChannels]+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
-        }
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        {
-          distance=fabs(alpha*GetPixelBlack(image,p)-beta*
-            GetPixelBlack(reconstruct_image,q));
-          distortion[BlackChannel]+=distance;
-          distortion[CompositeChannels]+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
-        }
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte != MagickFalse))
-        {
-          distance=fabs((double) GetPixelAlpha(image,p)-
-            GetPixelAlpha(reconstruct_image,q));
-          distortion[OpacityChannel]+=distance;
-          distortion[CompositeChannels]+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=fabs((double) (alpha*p[i]-beta*GetPixelChannel(
+          reconstruct_image,channel,q)));
+        distortion[i]+=distance;
+        distortion[CompositePixelChannel]+=distance;
+        mean_error+=distance*distance;
+        if (distance > maximum_error)
+          maximum_error=distance;
+        area++;
+      }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  image->error.mean_error_per_pixel=distortion[CompositeChannels]/area;
+  image->error.mean_error_per_pixel=distortion[CompositePixelChannel]/area;
   image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area;
   image->error.normalized_maximum_error=QuantumScale*maximum_error;
   return(status);
@@ -784,15 +744,16 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
     y;
 
   status=MagickTrue;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[CompositeChannels+1];
+      channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
       *restrict p,
@@ -805,9 +766,9 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
     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);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
+      1,exception);
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
@@ -815,61 +776,54 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
+      register ssize_t
+        i;
 
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*(GetPixelRed(image,p)-(MagickRealType)
-             GetPixelRed(reconstruct_image,q));
-          channel_distortion[RedChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
+      if (GetPixelMask(image,p) != 0)
         {
-          distance=QuantumScale*(GetPixelGreen(image,p)-(MagickRealType)
-            GetPixelGreen(reconstruct_image,q));
-          channel_distortion[GreenChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*(GetPixelBlue(image,p)-(MagickRealType)
-            GetPixelBlue(reconstruct_image,q));
-          channel_distortion[BlueChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        {
-          distance=QuantumScale*(GetPixelBlack(image,p)-(MagickRealType)
-            GetPixelBlack(reconstruct_image,q));
-          channel_distortion[BlackChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
-        }
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte != MagickFalse))
-        {
-          distance=QuantumScale*(GetPixelAlpha(image,p)-(MagickRealType)
-            GetPixelAlpha(reconstruct_image,q));
-          channel_distortion[OpacityChannel]+=distance*distance;
-          channel_distortion[CompositeChannels]+=distance*distance;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=QuantumScale*(p[i]-(double) GetPixelChannel(
+          reconstruct_image,channel,q));
+        distance*=distance;
+        channel_distortion[i]+=distance;
+        channel_distortion[CompositePixelChannel]+=distance;
+      }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GetMeanSquaredError)
+    #pragma omp critical (MagickCore_GetMeanSquaredError)
 #endif
-    for (i=0; i <= (ssize_t) CompositeChannels; i++)
+    for (i=0; i <= MaxPixelChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) CompositeChannels; i++)
+  for (i=0; i <= MaxPixelChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
-  distortion[CompositeChannels]/=(double) GetNumberChannels(image);
+  distortion[CompositePixelChannel]/=GetImageChannels(image);
   return(status);
 }
 
@@ -893,7 +847,7 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   MagickOffsetType
     progress;
 
-  MagickRealType
+  double
     area;
 
   register ssize_t
@@ -909,11 +863,11 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
   status=MagickTrue;
   progress=0;
-  for (i=0; i <= (ssize_t) CompositeChannels; i++)
+  for (i=0; i <= MaxPixelChannels; i++)
     distortion[i]=0.0;
-  area=1.0/((MagickRealType) image->columns*image->rows);
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  area=1.0/((double) image->columns*image->rows-1);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -928,43 +882,44 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
       1,exception);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        distortion[RedChannel]+=area*QuantumScale*(GetPixelRed(image,p)-
-          image_statistics[RedChannel].mean)*(
-          GetPixelRed(reconstruct_image,q)-
-          reconstruct_statistics[RedChannel].mean);
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        distortion[GreenChannel]+=area*QuantumScale*(GetPixelGreen(image,p)-
-          image_statistics[GreenChannel].mean)*(
-          GetPixelGreen(reconstruct_image,q)-
-          reconstruct_statistics[GreenChannel].mean);
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        distortion[BlueChannel]+=area*QuantumScale*(GetPixelBlue(image,p)-
-          image_statistics[BlueChannel].mean)*(
-          GetPixelBlue(reconstruct_image,q)-
-          reconstruct_statistics[BlueChannel].mean);
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        distortion[BlackChannel]+=area*QuantumScale*(GetPixelBlack(image,p)-
-          image_statistics[OpacityChannel].mean)*(
-          GetPixelBlack(reconstruct_image,q)-
-          reconstruct_statistics[OpacityChannel].mean);
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte != MagickFalse))
-        distortion[OpacityChannel]+=area*QuantumScale*(GetPixelAlpha(image,p)-
-          image_statistics[OpacityChannel].mean)*
-          (GetPixelAlpha(reconstruct_image,q)-
-          reconstruct_statistics[OpacityChannel].mean);
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,p) != 0)
+        {
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distortion[i]+=area*QuantumScale*(p[i]-image_statistics[i].mean)*
+          (GetPixelChannel(reconstruct_image,channel,q)-
+          reconstruct_statistics[channel].mean);
+      }
       p+=GetPixelChannels(image);
-      q+=GetPixelChannels(image);
+      q+=GetPixelChannels(reconstruct_image);
     }
     if (image->progress_monitor != (MagickProgressMonitor) NULL)
       {
@@ -982,35 +937,24 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   /*
     Divide by the standard deviation.
   */
-  for (i=0; i < (ssize_t) CompositeChannels; i++)
+  distortion[CompositePixelChannel]=0.0;
+  for (i=0; i < MaxPixelChannels; i++)
   {
-    MagickRealType
+    double
       gamma;
 
+    PixelChannel
+      channel;
+
+    channel=GetPixelChannelChannel(image,i);
     gamma=image_statistics[i].standard_deviation*
-      reconstruct_statistics[i].standard_deviation;
-    gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
+      reconstruct_statistics[channel].standard_deviation;
+    gamma=PerceptibleReciprocal(gamma);
     distortion[i]=QuantumRange*gamma*distortion[i];
+    distortion[CompositePixelChannel]+=distortion[i]*distortion[i];
   }
-  distortion[CompositeChannels]=0.0;
-  if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-    distortion[CompositeChannels]+=distortion[RedChannel]*
-      distortion[RedChannel];
-  if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-    distortion[CompositeChannels]+=distortion[GreenChannel]*
-      distortion[GreenChannel];
-  if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-    distortion[CompositeChannels]+=distortion[BlueChannel]*
-      distortion[BlueChannel];
-  if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && (image->matte != MagickFalse))
-    distortion[CompositeChannels]+=distortion[OpacityChannel]*
-      distortion[OpacityChannel];
-  if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->colorspace == CMYKColorspace))
-    distortion[CompositeChannels]+=distortion[BlackChannel]*
-      distortion[BlackChannel];
-  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]/
-    GetNumberChannels(image));
+  distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]/
+    GetImageChannels(image));
   /*
     Free resources.
   */
@@ -1035,15 +979,16 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
     y;
 
   status=MagickTrue;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[CompositeChannels+1];
+      channel_distortion[MaxPixelChannels+1];
 
     register const Quantum
       *restrict p,
@@ -1056,9 +1001,9 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
     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);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
+      1,exception);
+    if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
       {
         status=MagickFalse;
         continue;
@@ -1066,64 +1011,48 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
     (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
+      register ssize_t
+        i;
 
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
+      if (GetPixelMask(image,p) != 0)
         {
-          distance=QuantumScale*fabs(GetPixelRed(image,p)-(double)
-            GetPixelRed(reconstruct_image,q));
-          if (distance > channel_distortion[RedChannel])
-            channel_distortion[RedChannel]=distance;
-          if (distance > channel_distortion[CompositeChannels])
-            channel_distortion[CompositeChannels]=distance;
-        }
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*fabs(GetPixelGreen(image,p)-(double)
-            GetPixelGreen(reconstruct_image,q));
-          if (distance > channel_distortion[GreenChannel])
-            channel_distortion[GreenChannel]=distance;
-          if (distance > channel_distortion[CompositeChannels])
-            channel_distortion[CompositeChannels]=distance;
-        }
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        {
-          distance=QuantumScale*fabs(GetPixelBlue(image,p)-(double)
-            GetPixelBlue(reconstruct_image,q));
-          if (distance > channel_distortion[BlueChannel])
-            channel_distortion[BlueChannel]=distance;
-          if (distance > channel_distortion[CompositeChannels])
-            channel_distortion[CompositeChannels]=distance;
-        }
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        {
-          distance=QuantumScale*fabs(GetPixelBlack(image,p)-(double)
-            GetPixelBlack(reconstruct_image,q));
-          if (distance > channel_distortion[BlackChannel])
-            channel_distortion[BlackChannel]=distance;
-          if (distance > channel_distortion[CompositeChannels])
-            channel_distortion[CompositeChannels]=distance;
-        }
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte != MagickFalse))
-        {
-          distance=QuantumScale*fabs(GetPixelAlpha(image,p)-(double)
-            GetPixelAlpha(reconstruct_image,q));
-          if (distance > channel_distortion[OpacityChannel])
-            channel_distortion[OpacityChannel]=distance;
-          if (distance > channel_distortion[CompositeChannels])
-            channel_distortion[CompositeChannels]=distance;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (reconstruct_traits == UndefinedPixelTrait) ||
+            ((reconstruct_traits & UpdatePixelTrait) == 0))
+          continue;
+        distance=QuantumScale*fabs(p[i]-(double) GetPixelChannel(
+          reconstruct_image,channel,q));
+        if (distance > channel_distortion[i])
+          channel_distortion[i]=distance;
+        if (distance > channel_distortion[CompositePixelChannel])
+          channel_distortion[CompositePixelChannel]=distance;
+      }
       p+=GetPixelChannels(image);
-      q+=GetPixelChannels(image);
+      q+=GetPixelChannels(reconstruct_image);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GetPeakAbsoluteError)
+    #pragma omp critical (MagickCore_GetPeakAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) CompositeChannels; i++)
+    for (i=0; i <= MaxPixelChannels; i++)
       if (channel_distortion[i] > distortion[i])
         distortion[i]=channel_distortion[i];
   }
@@ -1138,26 +1067,12 @@ static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
   MagickBooleanType
     status;
 
+  register ssize_t
+    i;
+
   status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
-  if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-    distortion[RedChannel]=20.0*log10((double) 1.0/sqrt(
-      distortion[RedChannel]));
-  if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-    distortion[GreenChannel]=20.0*log10((double) 1.0/sqrt(
-      distortion[GreenChannel]));
-  if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-    distortion[BlueChannel]=20.0*log10((double) 1.0/sqrt(
-      distortion[BlueChannel]));
-  if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->matte != MagickFalse))
-    distortion[OpacityChannel]=20.0*log10((double) 1.0/sqrt(
-      distortion[OpacityChannel]));
-  if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->colorspace == CMYKColorspace))
-    distortion[BlackChannel]=20.0*log10((double) 1.0/sqrt(
-      distortion[BlackChannel]));
-  distortion[CompositeChannels]=20.0*log10((double) 1.0/sqrt(
-    distortion[CompositeChannels]));
+  for (i=0; i <= MaxPixelChannels; i++)
+    distortion[i]=20.0*log10((double) 1.0/sqrt(distortion[i]));
   return(status);
 }
 
@@ -1167,20 +1082,12 @@ static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
   MagickBooleanType
     status;
 
+  register ssize_t
+    i;
+
   status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception);
-  if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-    distortion[RedChannel]=sqrt(distortion[RedChannel]);
-  if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-    distortion[GreenChannel]=sqrt(distortion[GreenChannel]);
-  if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-    distortion[BlueChannel]=sqrt(distortion[BlueChannel]);
-  if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->colorspace == CMYKColorspace))
-    distortion[BlackChannel]=sqrt(distortion[BlackChannel]);
-  if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-      (image->matte != MagickFalse))
-    distortion[OpacityChannel]=sqrt(distortion[OpacityChannel]);
-  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]);
+  for (i=0; i <= MaxPixelChannels; i++)
+    distortion[i]=sqrt(distortion[i]);
   return(status);
 }
 
@@ -1213,7 +1120,7 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
   /*
     Get image distortion.
   */
-  length=CompositeChannels+1UL;
+  length=MaxPixelChannels+1;
   channel_distortion=(double *) AcquireQuantumMemory(length,
     sizeof(*channel_distortion));
   if (channel_distortion == (double *) NULL)
@@ -1278,7 +1185,7 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
       break;
     }
   }
-  *distortion=channel_distortion[CompositeChannels];
+  *distortion=channel_distortion[CompositePixelChannel];
   channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
   return(status);
 }
@@ -1294,11 +1201,11 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetImageDistrortion() compares the pixel channels of an image to a
+%  GetImageDistortions() compares the pixel channels of an image to a
 %  reconstructed image and returns the specified distortion metric for each
 %  channel.
 %
-%  The format of the CompareImages method is:
+%  The format of the GetImageDistortions method is:
 %
 %      double *GetImageDistortions(const Image *image,
 %        const Image *reconstruct_image,const MetricType metric,
@@ -1339,14 +1246,14 @@ MagickExport double *GetImageDistortions(Image *image,
   if ((reconstruct_image->columns != image->columns) ||
       (reconstruct_image->rows != image->rows))
     {
-      (void) ThrowMagickException(&image->exception,GetMagickModule(),
-        ImageError,"ImageSizeDiffers","`%s'",image->filename);
+      (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
+        "ImageSizeDiffers","`%s'",image->filename);
       return((double *) NULL);
     }
   /*
     Get image distortion.
   */
-  length=CompositeChannels+1UL;
+  length=MaxPixelChannels+1UL;
   channel_distortion=(double *) AcquireQuantumMemory(length,
     sizeof(*channel_distortion));
   if (channel_distortion == (double *) NULL)
@@ -1479,7 +1386,7 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
   MagickBooleanType
     status;
 
-  MagickRealType
+  double
     area,
     maximum_error,
     mean_error,
@@ -1499,8 +1406,8 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
   maximum_error=0.0;
   mean_error_per_pixel=0.0;
   mean_error=0.0;
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -1517,51 +1424,42 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
       break;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        distance;
-
-      distance=fabs(GetPixelRed(image,p)-(double)
-        GetPixelRed(reconstruct_image,q));
-      mean_error_per_pixel+=distance;
-      mean_error+=distance*distance;
-      if (distance > maximum_error)
-        maximum_error=distance;
-      area++;
-      distance=fabs(GetPixelGreen(image,p)-(double)
-        GetPixelGreen(reconstruct_image,q));
-      mean_error_per_pixel+=distance;
-      mean_error+=distance*distance;
-      if (distance > maximum_error)
-        maximum_error=distance;
-      area++;
-      distance=fabs(GetPixelBlue(image,p)-(double)
-        GetPixelBlue(reconstruct_image,q));
-      mean_error_per_pixel+=distance;
-      mean_error+=distance*distance;
-      if (distance > maximum_error)
-        maximum_error=distance;
-      area++;
-      if (image->matte != MagickFalse)
-        {
-          distance=fabs(GetPixelAlpha(image,p)-(double)
-            GetPixelAlpha(reconstruct_image,q));
-          mean_error_per_pixel+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
-        }
-      if ((image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,p) != 0)
         {
-          distance=fabs(GetPixelBlack(image,p)-(double)
-            GetPixelBlack(reconstruct_image,q));
-          mean_error_per_pixel+=distance;
-          mean_error+=distance*distance;
-          if (distance > maximum_error)
-            maximum_error=distance;
-          area++;
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(reconstruct_image);
+          continue;
         }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        double
+          distance;
+
+        PixelChannel
+          channel;
+
+        PixelTrait
+          reconstruct_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        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));
+        mean_error_per_pixel+=distance;
+        mean_error+=distance*distance;
+        if (distance > maximum_error)
+          maximum_error=distance;
+        area++;
+      }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(reconstruct_image);
     }
@@ -1595,7 +1493,8 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
 %  The format of the SimilarityImageImage method is:
 %
 %      Image *SimilarityImage(const Image *image,const Image *reference,
-%        RectangleInfo *offset,double *similarity,ExceptionInfo *exception)
+%        const MetricType metric,RectangleInfo *offset,double *similarity,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -1603,6 +1502,8 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
 %
 %    o reference: find an area of the image that closely resembles this image.
 %
+%    o metric: the metric.
+%
 %    o the best match offset of the reference image within the image.
 %
 %    o similarity: the computed similarity between the images.
@@ -1611,118 +1512,9 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
 %
 */
 
-static double GetNCCDistortion(const Image *image,
-  const Image *reconstruct_image,
-  const ChannelStatistics *reconstruct_statistics,ExceptionInfo *exception)
-{
-#define SimilarityImageTag  "Similarity/Image"
-
-  CacheView
-    *image_view,
-    *reconstruct_view;
-
-  ChannelStatistics
-    *image_statistics;
-
-  double
-    distortion;
-
-  MagickBooleanType
-    status;
-
-  MagickRealType
-    area,
-    gamma;
-
-  ssize_t
-    y;
-
-  unsigned long
-    number_channels;
-  
-  /*
-    Normalize to account for variation due to lighting and exposure condition.
-  */
-  image_statistics=GetImageStatistics(image,exception);
-  status=MagickTrue;
-  distortion=0.0;
-  area=1.0/((MagickRealType) image->columns*image->rows);
-  image_view=AcquireCacheView(image);
-  reconstruct_view=AcquireCacheView(reconstruct_image);
-  for (y=0; y < (ssize_t) image->rows; y++)
-  {
-    register const Quantum
-      *restrict p,
-      *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);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
-      {
-        status=MagickFalse;
-        continue;
-      }
-    for (x=0; x < (ssize_t) image->columns; x++)
-    {
-      distortion+=area*QuantumScale*(GetPixelRed(image,p)-
-        image_statistics[RedChannel].mean)*(
-        GetPixelRed(reconstruct_image,q)-
-        reconstruct_statistics[RedChannel].mean);
-      distortion+=area*QuantumScale*(GetPixelGreen(image,p)-
-        image_statistics[GreenChannel].mean)*(
-        GetPixelGreen(reconstruct_image,q)-
-        reconstruct_statistics[GreenChannel].mean);
-      distortion+=area*QuantumScale*(GetPixelBlue(image,p)-
-        GetPixelBlue(reconstruct_image,q)-
-        image_statistics[BlueChannel].mean)*(
-        reconstruct_statistics[BlueChannel].mean);
-      if ((image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        distortion+=area*QuantumScale*(GetPixelBlack(image,p)-
-          image_statistics[BlackChannel].mean)*(
-          GetPixelBlack(reconstruct_image,q)-
-          reconstruct_statistics[BlackChannel].mean);
-      if (image->matte != MagickFalse)
-        distortion+=area*QuantumScale*(GetPixelAlpha(image,p)-
-          image_statistics[OpacityChannel].mean)*(
-          GetPixelAlpha(reconstruct_image,q)-
-          reconstruct_statistics[OpacityChannel].mean);
-      p+=GetPixelChannels(image);
-      q+=GetPixelChannels(reconstruct_image);
-    }
-  }
-  reconstruct_view=DestroyCacheView(reconstruct_view);
-  image_view=DestroyCacheView(image_view);
-  /*
-    Divide by the standard deviation.
-  */
-  gamma=image_statistics[CompositeChannels].standard_deviation*
-    reconstruct_statistics[CompositeChannels].standard_deviation;
-  gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
-  distortion=QuantumRange*gamma*distortion;
-  number_channels=3;
-  if (image->matte != MagickFalse)
-    number_channels++;
-  if (image->colorspace == CMYKColorspace)
-    number_channels++;
-  distortion=sqrt(distortion/number_channels);
-  /*
-    Free resources.
-  */
-  image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
-    image_statistics);
-  return(1.0-distortion);
-}
-
 static double GetSimilarityMetric(const Image *image,const Image *reference,
-  const ChannelStatistics *reference_statistics,const ssize_t x_offset,
-  const ssize_t y_offset,ExceptionInfo *exception)
+  const MetricType metric,const ssize_t x_offset,const ssize_t y_offset,
+  ExceptionInfo *exception)
 {
   double
     distortion;
@@ -1730,6 +1522,9 @@ static double GetSimilarityMetric(const Image *image,const Image *reference,
   Image
     *similarity_image;
 
+  MagickBooleanType
+    status;
+
   RectangleInfo
     geometry;
 
@@ -1739,23 +1534,24 @@ static double GetSimilarityMetric(const Image *image,const Image *reference,
   similarity_image=CropImage(image,&geometry,exception);
   if (similarity_image == (Image *) NULL)
     return(0.0);
-  distortion=GetNCCDistortion(reference,similarity_image,reference_statistics,
+  distortion=0.0;
+  status=GetImageDistortion(similarity_image,reference,metric,&distortion,
     exception);
   similarity_image=DestroyImage(similarity_image);
+  if (status == MagickFalse)
+    return(0.0);
   return(distortion);
 }
 
 MagickExport Image *SimilarityImage(Image *image,const Image *reference,
-  RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
+  const MetricType metric,RectangleInfo *offset,double *similarity_metric,
+  ExceptionInfo *exception)
 {
 #define SimilarityImageTag  "Similarity/Image"
 
   CacheView
     *similarity_view;
 
-  ChannelStatistics
-    *reference_statistics;
-
   Image
     *similarity_image;
 
@@ -1783,7 +1579,8 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
     image->rows-reference->rows+1,MagickTrue,exception);
   if (similarity_image == (Image *) NULL)
     return((Image *) NULL);
-  if (SetImageStorageClass(similarity_image,DirectClass,exception) == MagickFalse)
+  status=SetImageStorageClass(similarity_image,DirectClass,exception);
+  if (status == MagickFalse)
     {
       similarity_image=DestroyImage(similarity_image);
       return((Image *) NULL);
@@ -1793,22 +1590,22 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
   */
   status=MagickTrue;
   progress=0;
-  reference_statistics=GetImageStatistics(reference,exception);
-  similarity_view=AcquireCacheView(similarity_image);
+  similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) (image->rows-reference->rows+1); y++)
   {
     double
       similarity;
 
-    register ssize_t
-      x;
-
     register Quantum
       *restrict q;
 
+    register ssize_t
+      x;
+
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(similarity_view,0,y,similarity_image->columns,
@@ -1820,10 +1617,12 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
       }
     for (x=0; x < (ssize_t) (image->columns-reference->columns+1); x++)
     {
-      similarity=GetSimilarityMetric(image,reference,reference_statistics,x,y,
-        exception);
+      register ssize_t
+        i;
+
+      similarity=GetSimilarityMetric(image,reference,metric,x,y,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_SimilarityImage)
+      #pragma omp critical (MagickCore_SimilarityImage)
 #endif
       if (similarity < *similarity_metric)
         {
@@ -1831,30 +1630,46 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
           offset->x=x;
           offset->y=y;
         }
-      SetPixelRed(similarity_image,ClampToQuantum(QuantumRange-
-        QuantumRange*similarity),q);
-      SetPixelGreen(similarity_image,GetPixelRed(image,q),q);
-      SetPixelBlue(similarity_image,GetPixelRed(image,q),q);
+      if (GetPixelMask(similarity_image,q) != 0)
+        {
+          q+=GetPixelChannels(similarity_image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(similarity_image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          similarity_traits,
+          traits;
+
+        channel=GetPixelChannelChannel(image,i);
+        traits=GetPixelChannelTraits(image,channel);
+        similarity_traits=GetPixelChannelTraits(similarity_image,channel);
+        if ((traits == UndefinedPixelTrait) ||
+            (similarity_traits == UndefinedPixelTrait) ||
+            ((similarity_traits & UpdatePixelTrait) == 0))
+          continue;
+        SetPixelChannel(similarity_image,channel,ClampToQuantum(QuantumRange-
+          QuantumRange*similarity),q);
+      }
       q+=GetPixelChannels(similarity_image);
     }
-    if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
+    if (IfMagickFalse( SyncCacheViewAuthenticPixels(similarity_view,exception) ))
       status=MagickFalse;
     if (image->progress_monitor != (MagickProgressMonitor) NULL)
       {
-        MagickBooleanType
-          proceed;
-
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_SimilarityImage)
+        #pragma omp critical (MagickCore_SimilarityImage)
 #endif
-        proceed=SetImageProgress(image,SimilarityImageTag,progress++,
-          image->rows);
-        if (proceed == MagickFalse)
+        if (IfMagickFalse(SetImageProgress(image,SimilarityImageTag,
+                 progress++,image->rows) ))
           status=MagickFalse;
       }
   }
   similarity_view=DestroyCacheView(similarity_view);
-  reference_statistics=(ChannelStatistics *) RelinquishMagickMemory(
-    reference_statistics);
+  if (status == MagickFalse)
+    similarity_image=DestroyImage(similarity_image);
   return(similarity_image);
 }