]> granicus.if.org Git - imagemagick/blobdiff - magick/compare.c
(no commit message)
[imagemagick] / magick / compare.c
index 987b2ba791c11822a0a60f54d8c11e9b5c70cc2e..a48fcf80484e3d5db100380ee0320870c8d80175 100644 (file)
@@ -110,7 +110,7 @@ MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
   Image
     *highlight_image;
 
-  highlight_image=CompareImageChannels(image,reconstruct_image,AllChannels,
+  highlight_image=CompareImageChannels(image,reconstruct_image,CompositeChannels,
     metric,distortion,exception);
   return(highlight_image);
 }
@@ -255,26 +255,31 @@ MagickExport Image *CompareImageChannels(Image *image,
       SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes+x,
         &reconstruct_pixel);
       difference=MagickFalse;
-      if (channel == AllChannels)
+      if (channel == CompositeChannels)
         {
           if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
             difference=MagickTrue;
         }
       else
         {
-          if (((channel & RedChannel) != 0) && (p->red != q->red))
+          if (((channel & RedChannel) != 0) &&
+              (GetRedPixelComponent(p) != GetRedPixelComponent(q)))
             difference=MagickTrue;
-          if (((channel & GreenChannel) != 0) && (p->green != q->green))
+          if (((channel & GreenChannel) != 0) &&
+              (GetGreenPixelComponent(p) != GetGreenPixelComponent(q)))
             difference=MagickTrue;
-          if (((channel & BlueChannel) != 0) && (p->blue != q->blue))
+          if (((channel & BlueChannel) != 0) &&
+              (GetBluePixelComponent(p) != GetBluePixelComponent(q)))
             difference=MagickTrue;
           if (((channel & OpacityChannel) != 0) &&
-              (image->matte != MagickFalse) && (p->opacity != q->opacity))
+              (image->matte != MagickFalse) &&
+              (GetOpacityPixelComponent(p) != GetOpacityPixelComponent(q)))
             difference=MagickTrue;
           if ((((channel & IndexChannel) != 0) &&
                (image->colorspace == CMYKColorspace) &&
                (reconstruct_image->colorspace == CMYKColorspace)) &&
-              (indexes[x] != reconstruct_indexes[x]))
+              (GetIndexPixelComponent(indexes+x) !=
+               GetIndexPixelComponent(reconstruct_indexes+x)))
             difference=MagickTrue;
         }
       if (difference != MagickFalse)
@@ -342,7 +347,7 @@ MagickExport MagickBooleanType GetImageDistortion(Image *image,
   MagickBooleanType
     status;
 
-  status=GetImageChannelDistortion(image,reconstruct_image,AllChannels,
+  status=GetImageChannelDistortion(image,reconstruct_image,CompositeChannels,
     metric,distortion,exception);
   return(status);
 }
@@ -355,15 +360,15 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
   MagickPixelPacket
     zero;
 
+  ssize_t
+    y;
+
   /*
     Compute the absolute difference in pixels between two images.
   */
@@ -377,7 +382,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[AllChannels+1];
+      channel_distortion[CompositeChannels+1];
 
     MagickPixelPacket
       pixel,
@@ -429,7 +434,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
           if (((channel & IndexChannel) != 0) &&
               (image->colorspace == CMYKColorspace))
             channel_distortion[BlackChannel]++;
-          channel_distortion[AllChannels]++;
+          channel_distortion[CompositeChannels]++;
         }
       p++;
       q++;
@@ -437,7 +442,7 @@ static MagickBooleanType GetAbsoluteDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GetAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) AllChannels; i++)
+    for (i=0; i <= (ssize_t) CompositeChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
@@ -475,15 +480,15 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
   register ssize_t
     i;
 
+  ssize_t
+    y;
+
   status=MagickTrue;
   image_view=AcquireCacheView(image);
   reconstruct_view=AcquireCacheView(reconstruct_image);
@@ -493,7 +498,7 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[AllChannels+1];
+      channel_distortion[CompositeChannels+1];
 
     register const IndexPacket
       *restrict indexes,
@@ -510,8 +515,8 @@ static MagickBooleanType GetFuzzDistortion(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);
+    q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
+      1,exception);
     if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
       {
         status=MagickFalse;
@@ -523,59 +528,47 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       MagickRealType
-        distance,
-        scale;
+        distance;
 
-      scale=1.0;
-      if ((channel & OpacityChannel) != 0)
-        {
-          if (image->matte != MagickFalse)
-            scale*=QuantumScale*GetAlphaPixelComponent(p);
-          if (reconstruct_image->matte != MagickFalse)
-            scale*=QuantumScale*GetAlphaPixelComponent(q);
-        }
-      if (((channel & IndexChannel) != 0) &&
-          (image->colorspace == CMYKColorspace) &&
-          (reconstruct_image->colorspace == CMYKColorspace))
-        {
-          scale*=(MagickRealType) (QuantumScale*(QuantumRange-indexes[x]));
-          scale*=(MagickRealType) (QuantumScale*(QuantumRange-
-            reconstruct_indexes[x]));
-        }
       if ((channel & RedChannel) != 0)
         {
-          distance=QuantumScale*(p->red-(MagickRealType) q->red);
-          channel_distortion[RedChannel]+=scale*distance*distance;
-          channel_distortion[AllChannels]+=scale*distance*distance;
+          distance=QuantumScale*(GetRedPixelComponent(p)-(MagickRealType)
+            GetRedPixelComponent(q));
+          channel_distortion[RedChannel]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if ((channel & GreenChannel) != 0)
         {
-          distance=QuantumScale*(p->green-(MagickRealType) q->green);
-          channel_distortion[GreenChannel]+=scale*distance*distance;
-          channel_distortion[AllChannels]+=scale*distance*distance;
+          distance=QuantumScale*(GetGreenPixelComponent(p)-(MagickRealType)
+            GetGreenPixelComponent(q));
+          channel_distortion[GreenChannel]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if ((channel & BlueChannel) != 0)
         {
-          distance=QuantumScale*(p->blue-(MagickRealType) q->blue);
-          channel_distortion[BlueChannel]+=scale*distance*distance;
-          channel_distortion[AllChannels]+=scale*distance*distance;
+          distance=QuantumScale*(GetBluePixelComponent(p)-(MagickRealType)
+            GetBluePixelComponent(q));
+          channel_distortion[BlueChannel]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
-      if (((channel & OpacityChannel) != 0) && ((image->matte != MagickFalse) ||          (reconstruct_image->matte != MagickFalse)))
+      if (((channel & OpacityChannel) != 0) && ((image->matte != MagickFalse) ||
+          (reconstruct_image->matte != MagickFalse)))
         {
-          distance=QuantumScale*((image->matte != MagickFalse ? p->opacity :
-            OpaqueOpacity)-(reconstruct_image->matte != MagickFalse ?
-            q->opacity : OpaqueOpacity));
-          channel_distortion[OpacityChannel]+=QuantumScale*distance*distance;
-          channel_distortion[AllChannels]+=QuantumScale*distance*distance;
+          distance=QuantumScale*((image->matte != MagickFalse ?
+            GetOpacityPixelComponent(p) : OpaqueOpacity)-
+            (reconstruct_image->matte != MagickFalse ?
+            GetOpacityPixelComponent(q): OpaqueOpacity));
+          channel_distortion[OpacityChannel]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
         {
-          distance=QuantumScale*(indexes[x]-(MagickRealType)
-            reconstruct_indexes[x]);
-          channel_distortion[BlackChannel]+=scale*distance*distance;
-          channel_distortion[AllChannels]+=scale*distance*distance;
+          distance=QuantumScale*(GetIndexPixelComponent(indexes+x)-
+            (MagickRealType) GetIndexPixelComponent(reconstruct_indexes+x));
+          channel_distortion[BlackChannel]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       p++;
       q++;
@@ -583,13 +576,19 @@ static MagickBooleanType GetFuzzDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GetMeanSquaredError)
 #endif
-    for (i=0; i <= (ssize_t) AllChannels; i++)
+    for (i=0; i <= (ssize_t) CompositeChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) AllChannels; i++)
+  for (i=0; i <= (ssize_t) CompositeChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
+  if (((channel & OpacityChannel) != 0) && ((image->matte != MagickFalse) ||
+      (reconstruct_image->matte != MagickFalse)))
+    distortion[CompositeChannels]/=(double) (GetNumberChannels(image,channel)-1);
+  else
+    distortion[CompositeChannels]/=(double) GetNumberChannels(image,channel);
+  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]);
   return(status);
 }
 
@@ -601,15 +600,15 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
   register ssize_t
     i;
 
+  ssize_t
+    y;
+
   status=MagickTrue;
   image_view=AcquireCacheView(image);
   reconstruct_view=AcquireCacheView(reconstruct_image);
@@ -619,7 +618,7 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[AllChannels+1];
+      channel_distortion[CompositeChannels+1];
 
     register const IndexPacket
       *restrict indexes,
@@ -653,36 +652,40 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
 
       if ((channel & RedChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->red-(double) q->red);
+          distance=QuantumScale*fabs(GetRedPixelComponent(p)-(double)
+            GetRedPixelComponent(q));
           channel_distortion[RedChannel]+=distance;
-          channel_distortion[AllChannels]+=distance;
+          channel_distortion[CompositeChannels]+=distance;
         }
       if ((channel & GreenChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->green-(double) q->green);
+          distance=QuantumScale*fabs(GetGreenPixelComponent(p)-(double)
+            GetGreenPixelComponent(q));
           channel_distortion[GreenChannel]+=distance;
-          channel_distortion[AllChannels]+=distance;
+          channel_distortion[CompositeChannels]+=distance;
         }
       if ((channel & BlueChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->blue-(double) q->blue);
+          distance=QuantumScale*fabs(GetBluePixelComponent(p)-(double)
+            GetBluePixelComponent(q));
           channel_distortion[BlueChannel]+=distance;
-          channel_distortion[AllChannels]+=distance;
+          channel_distortion[CompositeChannels]+=distance;
         }
       if (((channel & OpacityChannel) != 0) &&
           (image->matte != MagickFalse))
         {
-          distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
+          distance=QuantumScale*fabs(GetOpacityPixelComponent(p)-(double)
+            GetOpacityPixelComponent(q));
           channel_distortion[OpacityChannel]+=distance;
-          channel_distortion[AllChannels]+=distance;
+          channel_distortion[CompositeChannels]+=distance;
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
         {
-          distance=QuantumScale*fabs(indexes[x]-(double)
-            reconstruct_indexes[x]);
+          distance=QuantumScale*fabs(GetIndexPixelComponent(indexes+x)-(double)
+            GetIndexPixelComponent(reconstruct_indexes+x));
           channel_distortion[BlackChannel]+=distance;
-          channel_distortion[AllChannels]+=distance;
+          channel_distortion[CompositeChannels]+=distance;
         }
       p++;
       q++;
@@ -690,14 +693,14 @@ static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GetMeanAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) AllChannels; i++)
+    for (i=0; i <= (ssize_t) CompositeChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) AllChannels; i++)
+  for (i=0; i <= (ssize_t) CompositeChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
-  distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
+  distortion[CompositeChannels]/=(double) GetNumberChannels(image,channel);
   return(status);
 }
 
@@ -709,9 +712,6 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
@@ -722,6 +722,9 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
     maximum_error,
     mean_error;
 
+  ssize_t
+    y;
+
   status=MagickTrue;
   alpha=1.0;
   beta=1.0;
@@ -767,9 +770,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
         }
       if ((channel & RedChannel) != 0)
         {
-          distance=fabs(alpha*p->red-beta*q->red);
+          distance=fabs(alpha*GetRedPixelComponent(p)-beta*
+            GetRedPixelComponent(q));
           distortion[RedChannel]+=distance;
-          distortion[AllChannels]+=distance;
+          distortion[CompositeChannels]+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
             maximum_error=distance;
@@ -777,9 +781,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
         }
       if ((channel & GreenChannel) != 0)
         {
-          distance=fabs(alpha*p->green-beta*q->green);
+          distance=fabs(alpha*GetGreenPixelComponent(p)-beta*
+            GetGreenPixelComponent(q));
           distortion[GreenChannel]+=distance;
-          distortion[AllChannels]+=distance;
+          distortion[CompositeChannels]+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
             maximum_error=distance;
@@ -787,9 +792,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
         }
       if ((channel & BlueChannel) != 0)
         {
-          distance=fabs(alpha*p->blue-beta*q->blue);
+          distance=fabs(alpha*GetBluePixelComponent(p)-beta*
+            GetBluePixelComponent(q));
           distortion[BlueChannel]+=distance;
-          distortion[AllChannels]+=distance;
+          distortion[CompositeChannels]+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
             maximum_error=distance;
@@ -798,9 +804,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
       if (((channel & OpacityChannel) != 0) &&
           (image->matte != MagickFalse))
         {
-          distance=fabs((double) p->opacity-q->opacity);
+          distance=fabs((double) GetOpacityPixelComponent(p)-
+            GetOpacityPixelComponent(q));
           distortion[OpacityChannel]+=distance;
-          distortion[AllChannels]+=distance;
+          distortion[CompositeChannels]+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
             maximum_error=distance;
@@ -810,9 +817,10 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
           (image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
         {
-          distance=fabs(alpha*indexes[x]-beta*reconstruct_indexes[x]);
+          distance=fabs(alpha*GetIndexPixelComponent(indexes+x)-beta*
+            GetIndexPixelComponent(reconstruct_indexes+x));
           distortion[BlackChannel]+=distance;
-          distortion[AllChannels]+=distance;
+          distortion[CompositeChannels]+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
             maximum_error=distance;
@@ -824,7 +832,7 @@ static MagickBooleanType GetMeanErrorPerPixel(Image *image,
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  image->error.mean_error_per_pixel=distortion[AllChannels]/area;
+  image->error.mean_error_per_pixel=distortion[CompositeChannels]/area;
   image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area;
   image->error.normalized_maximum_error=QuantumScale*maximum_error;
   return(status);
@@ -838,15 +846,15 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
   register ssize_t
     i;
 
+  ssize_t
+    y;
+
   status=MagickTrue;
   image_view=AcquireCacheView(image);
   reconstruct_view=AcquireCacheView(reconstruct_image);
@@ -856,7 +864,7 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[AllChannels+1];
+      channel_distortion[CompositeChannels+1];
 
     register const IndexPacket
       *restrict indexes,
@@ -890,37 +898,41 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
 
       if ((channel & RedChannel) != 0)
         {
-          distance=QuantumScale*(p->red-(MagickRealType) q->red);
+          distance=QuantumScale*(GetRedPixelComponent(p)-(MagickRealType)
+            GetRedPixelComponent(q));
           channel_distortion[RedChannel]+=distance*distance;
-          channel_distortion[AllChannels]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if ((channel & GreenChannel) != 0)
         {
-          distance=QuantumScale*(p->green-(MagickRealType) q->green);
+          distance=QuantumScale*(GetGreenPixelComponent(p)-(MagickRealType)
+            GetGreenPixelComponent(q));
           channel_distortion[GreenChannel]+=distance*distance;
-          channel_distortion[AllChannels]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if ((channel & BlueChannel) != 0)
         {
-          distance=QuantumScale*(p->blue-(MagickRealType) q->blue);
+          distance=QuantumScale*(GetBluePixelComponent(p)-(MagickRealType)
+            GetBluePixelComponent(q));
           channel_distortion[BlueChannel]+=distance*distance;
-          channel_distortion[AllChannels]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if (((channel & OpacityChannel) != 0) &&
           (image->matte != MagickFalse))
         {
-          distance=QuantumScale*(p->opacity-(MagickRealType) q->opacity);
+          distance=QuantumScale*(GetOpacityPixelComponent(p)-(MagickRealType)
+            GetOpacityPixelComponent(q));
           channel_distortion[OpacityChannel]+=distance*distance;
-          channel_distortion[AllChannels]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
         {
-          distance=QuantumScale*(indexes[x]-(MagickRealType)
-            reconstruct_indexes[x]);
+          distance=QuantumScale*(GetIndexPixelComponent(indexes+x)-
+            (MagickRealType) GetIndexPixelComponent(reconstruct_indexes+x));
           channel_distortion[BlackChannel]+=distance*distance;
-          channel_distortion[AllChannels]+=distance*distance;
+          channel_distortion[CompositeChannels]+=distance*distance;
         }
       p++;
       q++;
@@ -928,14 +940,14 @@ static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GetMeanSquaredError)
 #endif
-    for (i=0; i <= (ssize_t) AllChannels; i++)
+    for (i=0; i <= (ssize_t) CompositeChannels; i++)
       distortion[i]+=channel_distortion[i];
   }
   reconstruct_view=DestroyCacheView(reconstruct_view);
   image_view=DestroyCacheView(image_view);
-  for (i=0; i <= (ssize_t) AllChannels; i++)
+  for (i=0; i <= (ssize_t) CompositeChannels; i++)
     distortion[i]/=((double) image->columns*image->rows);
-  distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
+  distortion[CompositeChannels]/=(double) GetNumberChannels(image,channel);
   return(status);
 }
 
@@ -975,7 +987,7 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   reconstruct_statistics=GetImageChannelStatistics(reconstruct_image,exception);
   status=MagickTrue;
   progress=0;
-  for (i=0; i <= (ssize_t) AllChannels; i++)
+  for (i=0; i <= (ssize_t) CompositeChannels; i++)
     distortion[i]=0.0;
   area=1.0/((MagickRealType) image->columns*image->rows);
   image_view=AcquireCacheView(image);
@@ -1008,27 +1020,30 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        distortion[RedChannel]+=area*QuantumScale*(p->red-
-          image_statistics[RedChannel].mean)*(q->red-
+        distortion[RedChannel]+=area*QuantumScale*(GetRedPixelComponent(p)-
+          image_statistics[RedChannel].mean)*(GetRedPixelComponent(q)-
           reconstruct_statistics[RedChannel].mean);
       if ((channel & GreenChannel) != 0)
-        distortion[GreenChannel]+=area*QuantumScale*(p->green-
-          image_statistics[GreenChannel].mean)*(q->green-
+        distortion[GreenChannel]+=area*QuantumScale*(GetGreenPixelComponent(p)-
+          image_statistics[GreenChannel].mean)*(GetGreenPixelComponent(q)-
           reconstruct_statistics[GreenChannel].mean);
       if ((channel & BlueChannel) != 0)
-        distortion[BlueChannel]+=area*QuantumScale*(p->blue-
-          image_statistics[BlueChannel].mean)*(q->blue-
+        distortion[BlueChannel]+=area*QuantumScale*(GetBluePixelComponent(p)-
+          image_statistics[BlueChannel].mean)*(GetBluePixelComponent(q)-
           reconstruct_statistics[BlueChannel].mean);
       if (((channel & OpacityChannel) != 0) &&
           (image->matte != MagickFalse))
-        distortion[OpacityChannel]+=area*QuantumScale*(p->opacity-
-          image_statistics[OpacityChannel].mean)*(q->opacity-
+        distortion[OpacityChannel]+=area*QuantumScale*(
+          GetOpacityPixelComponent(p)-image_statistics[OpacityChannel].mean)*
+          (GetOpacityPixelComponent(q)-
           reconstruct_statistics[OpacityChannel].mean);
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
-        distortion[BlackChannel]+=area*QuantumScale*(indexes[x]-
-          image_statistics[OpacityChannel].mean)*(reconstruct_indexes[x]-
+        distortion[BlackChannel]+=area*QuantumScale*(
+          GetIndexPixelComponent(indexes+x)-
+          image_statistics[OpacityChannel].mean)*(
+          GetIndexPixelComponent(reconstruct_indexes+x)-
           reconstruct_statistics[OpacityChannel].mean);
       p++;
       q++;
@@ -1049,7 +1064,7 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
   /*
     Divide by the standard deviation.
   */
-  for (i=0; i < (ssize_t) AllChannels; i++)
+  for (i=0; i < (ssize_t) CompositeChannels; i++)
   {
     MagickRealType
       gamma;
@@ -1059,20 +1074,20 @@ static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
     gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
     distortion[i]=QuantumRange*gamma*distortion[i];
   }
-  distortion[AllChannels]=0.0;
+  distortion[CompositeChannels]=0.0;
   if ((channel & RedChannel) != 0)
-    distortion[AllChannels]+=distortion[RedChannel]*distortion[RedChannel];
+    distortion[CompositeChannels]+=distortion[RedChannel]*distortion[RedChannel];
   if ((channel & GreenChannel) != 0)
-    distortion[AllChannels]+=distortion[GreenChannel]*distortion[GreenChannel];
+    distortion[CompositeChannels]+=distortion[GreenChannel]*distortion[GreenChannel];
   if ((channel & BlueChannel) != 0)
-    distortion[AllChannels]+=distortion[BlueChannel]*distortion[BlueChannel];
+    distortion[CompositeChannels]+=distortion[BlueChannel]*distortion[BlueChannel];
   if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
-    distortion[AllChannels]+=distortion[OpacityChannel]*
+    distortion[CompositeChannels]+=distortion[OpacityChannel]*
       distortion[OpacityChannel];
   if (((channel & IndexChannel) != 0) &&
       (image->colorspace == CMYKColorspace))
-    distortion[AllChannels]+=distortion[BlackChannel]*distortion[BlackChannel];
-  distortion[AllChannels]=sqrt(distortion[AllChannels]/GetNumberChannels(image,
+    distortion[CompositeChannels]+=distortion[BlackChannel]*distortion[BlackChannel];
+  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]/GetNumberChannels(image,
     channel));
   /*
     Free resources.
@@ -1092,12 +1107,12 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
     *image_view,
     *reconstruct_view;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
+  ssize_t
+    y;
+
   status=MagickTrue;
   image_view=AcquireCacheView(image);
   reconstruct_view=AcquireCacheView(reconstruct_image);
@@ -1107,7 +1122,7 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
-      channel_distortion[AllChannels+1];
+      channel_distortion[CompositeChannels+1];
 
     register const IndexPacket
       *restrict indexes,
@@ -1141,47 +1156,51 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
 
       if ((channel & RedChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->red-(double) q->red);
+          distance=QuantumScale*fabs(GetRedPixelComponent(p)-(double)
+            GetRedPixelComponent(q));
           if (distance > channel_distortion[RedChannel])
             channel_distortion[RedChannel]=distance;
-          if (distance > channel_distortion[AllChannels])
-            channel_distortion[AllChannels]=distance;
+          if (distance > channel_distortion[CompositeChannels])
+            channel_distortion[CompositeChannels]=distance;
         }
       if ((channel & GreenChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->green-(double) q->green);
+          distance=QuantumScale*fabs(GetGreenPixelComponent(p)-(double)
+            GetGreenPixelComponent(q));
           if (distance > channel_distortion[GreenChannel])
             channel_distortion[GreenChannel]=distance;
-          if (distance > channel_distortion[AllChannels])
-            channel_distortion[AllChannels]=distance;
+          if (distance > channel_distortion[CompositeChannels])
+            channel_distortion[CompositeChannels]=distance;
         }
       if ((channel & BlueChannel) != 0)
         {
-          distance=QuantumScale*fabs(p->blue-(double) q->blue);
+          distance=QuantumScale*fabs(GetBluePixelComponent(p)-(double)
+            GetBluePixelComponent(q));
           if (distance > channel_distortion[BlueChannel])
             channel_distortion[BlueChannel]=distance;
-          if (distance > channel_distortion[AllChannels])
-            channel_distortion[AllChannels]=distance;
+          if (distance > channel_distortion[CompositeChannels])
+            channel_distortion[CompositeChannels]=distance;
         }
       if (((channel & OpacityChannel) != 0) &&
           (image->matte != MagickFalse))
         {
-          distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
+          distance=QuantumScale*fabs(GetOpacityPixelComponent(p)-(double)
+            GetOpacityPixelComponent(q));
           if (distance > channel_distortion[OpacityChannel])
             channel_distortion[OpacityChannel]=distance;
-          if (distance > channel_distortion[AllChannels])
-            channel_distortion[AllChannels]=distance;
+          if (distance > channel_distortion[CompositeChannels])
+            channel_distortion[CompositeChannels]=distance;
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
         {
-          distance=QuantumScale*fabs(indexes[x]-(double)
-            reconstruct_indexes[x]);
+          distance=QuantumScale*fabs(GetIndexPixelComponent(indexes+x)-(double)
+            GetIndexPixelComponent(reconstruct_indexes+x));
           if (distance > channel_distortion[BlackChannel])
             channel_distortion[BlackChannel]=distance;
-          if (distance > channel_distortion[AllChannels])
-            channel_distortion[AllChannels]=distance;
+          if (distance > channel_distortion[CompositeChannels])
+            channel_distortion[CompositeChannels]=distance;
         }
       p++;
       q++;
@@ -1189,7 +1208,7 @@ static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GetPeakAbsoluteError)
 #endif
-    for (i=0; i <= (ssize_t) AllChannels; i++)
+    for (i=0; i <= (ssize_t) CompositeChannels; i++)
       if (channel_distortion[i] > distortion[i])
         distortion[i]=channel_distortion[i];
   }
@@ -1224,8 +1243,8 @@ static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
       (image->colorspace == CMYKColorspace))
     distortion[BlackChannel]=20.0*log10((double) 1.0/sqrt(
       distortion[BlackChannel]));
-  distortion[AllChannels]=20.0*log10((double) 1.0/sqrt(
-    distortion[AllChannels]));
+  distortion[CompositeChannels]=20.0*log10((double) 1.0/sqrt(
+    distortion[CompositeChannels]));
   return(status);
 }
 
@@ -1250,7 +1269,7 @@ static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
   if (((channel & IndexChannel) != 0) &&
       (image->colorspace == CMYKColorspace))
     distortion[BlackChannel]=sqrt(distortion[BlackChannel]);
-  distortion[AllChannels]=sqrt(distortion[AllChannels]);
+  distortion[CompositeChannels]=sqrt(distortion[CompositeChannels]);
   return(status);
 }
 
@@ -1283,7 +1302,7 @@ MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
   /*
     Get image distortion.
   */
-  length=AllChannels+1UL;
+  length=CompositeChannels+1UL;
   channel_distortion=(double *) AcquireQuantumMemory(length,
     sizeof(*channel_distortion));
   if (channel_distortion == (double *) NULL)
@@ -1348,7 +1367,7 @@ MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
       break;
     }
   }
-  *distortion=channel_distortion[AllChannels];
+  *distortion=channel_distortion[CompositeChannels];
   channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
   return(status);
 }
@@ -1416,42 +1435,43 @@ MagickExport double *GetImageChannelDistortions(Image *image,
   /*
     Get image distortion.
   */
-  length=AllChannels+1UL;
+  length=CompositeChannels+1UL;
   channel_distortion=(double *) AcquireQuantumMemory(length,
     sizeof(*channel_distortion));
   if (channel_distortion == (double *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   (void) ResetMagickMemory(channel_distortion,0,length*
     sizeof(*channel_distortion));
+  status=MagickTrue;
   switch (metric)
   {
     case AbsoluteErrorMetric:
     {
-      status=GetAbsoluteDistortion(image,reconstruct_image,AllChannels,
+      status=GetAbsoluteDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case FuzzErrorMetric:
     {
-      status=GetFuzzDistortion(image,reconstruct_image,AllChannels,
+      status=GetFuzzDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case MeanAbsoluteErrorMetric:
     {
-      status=GetMeanAbsoluteDistortion(image,reconstruct_image,AllChannels,
+      status=GetMeanAbsoluteDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case MeanErrorPerPixelMetric:
     {
-      status=GetMeanErrorPerPixel(image,reconstruct_image,AllChannels,
+      status=GetMeanErrorPerPixel(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case MeanSquaredErrorMetric:
     {
-      status=GetMeanSquaredDistortion(image,reconstruct_image,AllChannels,
+      status=GetMeanSquaredDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
@@ -1459,28 +1479,33 @@ MagickExport double *GetImageChannelDistortions(Image *image,
     default:
     {
       status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
-        AllChannels,channel_distortion,exception);
+        CompositeChannels,channel_distortion,exception);
       break;
     }
     case PeakAbsoluteErrorMetric:
     {
-      status=GetPeakAbsoluteDistortion(image,reconstruct_image,AllChannels,
+      status=GetPeakAbsoluteDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case PeakSignalToNoiseRatioMetric:
     {
-      status=GetPeakSignalToNoiseRatio(image,reconstruct_image,AllChannels,
+      status=GetPeakSignalToNoiseRatio(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
     case RootMeanSquaredErrorMetric:
     {
-      status=GetRootMeanSquaredDistortion(image,reconstruct_image,AllChannels,
+      status=GetRootMeanSquaredDistortion(image,reconstruct_image,CompositeChannels,
         channel_distortion,exception);
       break;
     }
   }
+  if (status == MagickFalse)
+    {
+      channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
+      return((double *) NULL);
+    }
   return(channel_distortion);
 }
 \f
@@ -1541,9 +1566,6 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
   ExceptionInfo
     *exception;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
@@ -1553,6 +1575,9 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
     mean_error,
     mean_error_per_pixel;
 
+  ssize_t
+    y;
+
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
   assert(reconstruct_image != (const Image *) NULL);
@@ -1592,19 +1617,22 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
       MagickRealType
         distance;
 
-      distance=fabs(p->red-(double) q->red);
+      distance=fabs(GetRedPixelComponent(p)-(double)
+        GetRedPixelComponent(q));
       mean_error_per_pixel+=distance;
       mean_error+=distance*distance;
       if (distance > maximum_error)
         maximum_error=distance;
       area++;
-      distance=fabs(p->green-(double) q->green);
+      distance=fabs(GetGreenPixelComponent(p)-(double)
+        GetGreenPixelComponent(q));
       mean_error_per_pixel+=distance;
       mean_error+=distance*distance;
       if (distance > maximum_error)
         maximum_error=distance;
       area++;
-      distance=fabs(p->blue-(double) q->blue);
+      distance=fabs(GetBluePixelComponent(p)-(double)
+        GetBluePixelComponent(q));
       mean_error_per_pixel+=distance;
       mean_error+=distance*distance;
       if (distance > maximum_error)
@@ -1612,7 +1640,8 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
       area++;
       if (image->matte != MagickFalse)
         {
-          distance=fabs(p->opacity-(double) q->opacity);
+          distance=fabs(GetOpacityPixelComponent(p)-(double)
+            GetOpacityPixelComponent(q));
           mean_error_per_pixel+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
@@ -1622,7 +1651,8 @@ MagickExport MagickBooleanType IsImagesEqual(Image *image,
       if ((image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
         {
-          distance=fabs(indexes[x]-(double) reconstruct_indexes[x]);
+          distance=fabs(GetIndexPixelComponent(indexes+x)-(double)
+            GetIndexPixelComponent(reconstruct_indexes+x));
           mean_error_per_pixel+=distance;
           mean_error+=distance*distance;
           if (distance > maximum_error)
@@ -1743,24 +1773,24 @@ static double GetNCCDistortion(const Image *image,
     reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      distortion+=area*QuantumScale*(p->red-
-        image_statistics[RedChannel].mean)*(q->red-
+      distortion+=area*QuantumScale*(GetRedPixelComponent(p)-
+        image_statistics[RedChannel].mean)*(GetRedPixelComponent(q)-
         reconstruct_statistics[RedChannel].mean);
-      distortion+=area*QuantumScale*(p->green-
-        image_statistics[GreenChannel].mean)*(q->green-
+      distortion+=area*QuantumScale*(GetGreenPixelComponent(p)-
+        image_statistics[GreenChannel].mean)*(GetGreenPixelComponent(q)-
         reconstruct_statistics[GreenChannel].mean);
-      distortion+=area*QuantumScale*(p->blue-
+      distortion+=area*QuantumScale*(GetBluePixelComponent(p)-
         image_statistics[BlueChannel].mean)*(q->blue-
         reconstruct_statistics[BlueChannel].mean);
       if (image->matte != MagickFalse)
-        distortion+=area*QuantumScale*(p->opacity-
-          image_statistics[OpacityChannel].mean)*(q->opacity-
+        distortion+=area*QuantumScale*(GetOpacityPixelComponent(p)-
+          image_statistics[OpacityChannel].mean)*(GetOpacityPixelComponent(q)-
           reconstruct_statistics[OpacityChannel].mean);
       if ((image->colorspace == CMYKColorspace) &&
           (reconstruct_image->colorspace == CMYKColorspace))
-        distortion+=area*QuantumScale*(indexes[x]-
-          image_statistics[OpacityChannel].mean)*(reconstruct_indexes[x]-
-          reconstruct_statistics[OpacityChannel].mean);
+        distortion+=area*QuantumScale*(GetIndexPixelComponent(indexes+x)-
+          image_statistics[OpacityChannel].mean)*(GetIndexPixelComponent(
+          reconstruct_indexes+x)-reconstruct_statistics[OpacityChannel].mean);
       p++;
       q++;
     }
@@ -1770,8 +1800,8 @@ static double GetNCCDistortion(const Image *image,
   /*
     Divide by the standard deviation.
   */
-  gamma=image_statistics[AllChannels].standard_deviation*
-    reconstruct_statistics[AllChannels].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;
@@ -1900,9 +1930,10 @@ MagickExport Image *SimilarityImage(Image *image,const Image *reference,
           offset->x=x;
           offset->y=y;
         }
-      q->red=ClampToQuantum(QuantumRange-QuantumRange*similarity);
-      q->green=q->red;
-      q->blue=q->red;
+      SetRedPixelComponent(q,ClampToQuantum(QuantumRange-QuantumRange*
+        similarity));
+      SetGreenPixelComponent(q,GetRedPixelComponent(q));
+      SetBluePixelComponent(q,GetRedPixelComponent(q));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)