]> granicus.if.org Git - imagemagick/blobdiff - MagickWand/compare.c
Fixed compiler warning.
[imagemagick] / MagickWand / compare.c
index 4141bb09bf0c9fdbb868214afd7d97a5ec705083..1a49cb5a669f817e992eb35e8faad70e545fdd70 100644 (file)
 %                         Image Comparison Methods                            %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                               December 2003                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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  %
@@ -124,7 +124,6 @@ static MagickBooleanType CompareUsage(void)
       "                     de-emphasize pixel differences with this color",
       "-metric type         measure differences between images with this metric",
       "-monitor             monitor progress",
-      "-passphrase filename get the passphrase from this file",
       "-profile filename    add, delete, or apply an image profile",
       "-quality value       JPEG/MIFF/PNG compression level",
       "-quiet               suppress all warning messages",
@@ -136,8 +135,12 @@ static MagickBooleanType CompareUsage(void)
       "-seed value          seed a new sequence of pseudo-random numbers",
       "-set attribute value set an image attribute",
       "-quality value       JPEG/MIFF/PNG compression level",
+      "-similarity-threshold value",
+      "                     minimum distortion for (sub)image match",
       "-size geometry       width and height of image",
       "-subimage-search     search for subimage",
+      "-synchronize         synchronize image to storage device",
+      "-taint               declare the image as modified",
       "-transparent-color color",
       "                     transparent color",
       "-type type           image type",
@@ -148,9 +151,7 @@ static MagickBooleanType CompareUsage(void)
       (char *) NULL
     };
 
-  (void) printf("Version: %s\n",GetMagickVersion((size_t *) NULL));
-  (void) printf("Copyright: %s\n",GetMagickCopyright());
-  (void) printf("Features: %s\n\n",GetMagickFeatures());
+  ListMagickVersion(stdout);
   (void) printf("Usage: %s [options ...] image reconstruct difference\n",
     GetClientName());
   (void) printf("\nImage Settings:\n");
@@ -160,7 +161,7 @@ static MagickBooleanType CompareUsage(void)
   for (p=miscellaneous; *p != (char *) NULL; p++)
     (void) printf("  %s\n",*p);
   (void) printf(
-    "\nBy default, the image format of `file' is determined by its magic\n");
+    "\nBy default, the image format of 'file' is determined by its magic\n");
   (void) printf(
     "number.  To specify a particular image format, precede the filename\n");
   (void) printf(
@@ -174,7 +175,9 @@ static MagickBooleanType CompareUsage(void)
 WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
   int argc,char **argv,char **metadata,ExceptionInfo *exception)
 {
+#define CompareEpsilon  (1.0e-06)
 #define DefaultDissimilarityThreshold  0.31830988618379067154
+#define DefaultSimilarityThreshold  (-1.0)
 #define DestroyCompare() \
 { \
   if (similarity_image != (Image *) NULL) \
@@ -197,7 +200,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
 #define ThrowCompareInvalidArgumentException(option,argument) \
 { \
   (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
-    "InvalidArgument","`%s': %s",option,argument); \
+    "InvalidArgument","'%s': %s",option,argument); \
   DestroyCompare(); \
   return(MagickFalse); \
 }
@@ -212,7 +215,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
   double
     dissimilarity_threshold,
     distortion,
-    similarity_metric;
+    similarity_metric,
+    similarity_threshold;
 
   Image
     *difference_image,
@@ -259,12 +263,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
       if ((LocaleCompare("version",option+1) == 0) ||
           (LocaleCompare("-version",option+1) == 0))
         {
-          (void) FormatLocaleFile(stdout,"Version: %s\n",
-            GetMagickVersion((size_t *) NULL));
-          (void) FormatLocaleFile(stdout,"Copyright: %s\n",
-            GetMagickCopyright());
-          (void) FormatLocaleFile(stdout,"Features: %s\n\n",
-            GetMagickFeatures());
+          ListMagickVersion(stdout);
           return(MagickFalse);
         }
     }
@@ -273,11 +272,12 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
   difference_image=NewImageList();
   similarity_image=NewImageList();
   dissimilarity_threshold=DefaultDissimilarityThreshold;
+  similarity_threshold=DefaultSimilarityThreshold;
   distortion=0.0;
   format=(char *) NULL;
   j=1;
   k=0;
-  metric=UndefinedMetric;
+  metric=UndefinedErrorMetric;
   NewImageStack();
   option=(char *) NULL;
   pend=MagickFalse;
@@ -348,9 +348,9 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             i++;
             if (i == (ssize_t) argc)
               ThrowCompareException(OptionError,"MissingArgument",option);
-            type=ParseCommandOption(MagickAlphaOptions,MagickFalse,argv[i]);
+            type=ParseCommandOption(MagickAlphaChannelOptions,MagickFalse,argv[i]);
             if (type < 0)
-              ThrowCompareException(OptionError,"UnrecognizedAlphaChannelType",
+              ThrowCompareException(OptionError,"UnrecognizedAlphaChannelOption",
                 argv[i]);
             break;
           }
@@ -392,7 +392,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             if (channel < 0)
               ThrowCompareException(OptionError,"UnrecognizedChannelType",
                 argv[i]);
-            SetPixelChannelMapMask(image,(ChannelType) channel);
+            SetPixelChannelMask(image,(ChannelType) channel);
             break;
           }
         if (LocaleCompare("colorspace",option+1) == 0)
@@ -819,6 +819,21 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
               ThrowCompareException(OptionError,"MissingArgument",option);
             break;
           }
+        if (LocaleCompare("similarity-threshold",option+1) == 0)
+          {
+            if (*option == '+')
+              break;
+            i++;
+            if (i == (ssize_t) argc)
+              ThrowCompareException(OptionError,"MissingArgument",option);
+            if (IsGeometry(argv[i]) == MagickFalse)
+              ThrowCompareInvalidArgumentException(option,argv[i]);
+            if (*option == '+')
+              similarity_threshold=DefaultSimilarityThreshold;
+            else
+              similarity_threshold=StringToDouble(argv[i],(char **) NULL);
+            break;
+          }
         if (LocaleCompare("size",option+1) == 0)
           {
             if (*option == '+')
@@ -882,12 +897,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
         if ((LocaleCompare("version",option+1) == 0) ||
             (LocaleCompare("-version",option+1) == 0))
           {
-            (void) FormatLocaleFile(stdout,"Version: %s\n",
-              GetMagickVersion((size_t *) NULL));
-            (void) FormatLocaleFile(stdout,"Copyright: %s\n",
-              GetMagickCopyright());
-            (void) FormatLocaleFile(stdout,"Features: %s\n\n",
-              GetMagickFeatures());
+            ListMagickVersion(stdout);
             break;
           }
         if (LocaleCompare("virtual-pixel",option+1) == 0)
@@ -930,21 +940,29 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
     ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]);
   image=GetImageFromList(image,0);
   reconstruct_image=GetImageFromList(image,1);
+  offset.x=0;
+  offset.y=0;
   if (subimage_search != MagickFalse)
     {
-      similarity_image=SimilarityImage(image,reconstruct_image,metric,&offset,
-        &similarity_metric,exception);
+      similarity_image=SimilarityImage(image,reconstruct_image,metric,
+        similarity_threshold,&offset,&similarity_metric,exception);
       if (similarity_metric > dissimilarity_threshold)
         ThrowCompareException(ImageError,"ImagesTooDissimilar",image->filename);
     }
   if ((reconstruct_image->columns == image->columns) &&
-      (reconstruct_image->rows == image->rows))
+       (reconstruct_image->rows == image->rows))
     difference_image=CompareImages(image,reconstruct_image,metric,&distortion,
       exception);
   else
     if (similarity_image == (Image *) NULL)
-      ThrowCompareException(OptionError,"ImageWidthsOrHeightsDiffer",
-        image->filename)
+      {
+        if (metric == PerceptualHashErrorMetric)
+          difference_image=CompareImages(image,reconstruct_image,metric,
+            &distortion,exception);
+        else
+          ThrowCompareException(OptionError,"ImageWidthsOrHeightsDiffer",
+            image->filename);
+      }
     else
       {
         Image
@@ -959,8 +977,14 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             &distortion,exception);
         else
           {
+            Image
+              *distort_image;
+
+            RectangleInfo
+              page;
+
             (void) CompositeImage(composite_image,reconstruct_image,
-              CopyCompositeOp,MagickFalse,offset.x,offset.y,exception);
+              CopyCompositeOp,MagickTrue,offset.x,offset.y,exception);
             difference_image=CompareImages(image,composite_image,metric,
               &distortion,exception);
             if (difference_image != (Image *) NULL)
@@ -969,6 +993,22 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                 difference_image->page.y=offset.y;
               }
             composite_image=DestroyImage(composite_image);
+            page.width=reconstruct_image->columns;
+            page.height=reconstruct_image->rows;
+            page.x=offset.x;
+            page.y=offset.y;
+            distort_image=CropImage(image,&page,exception);
+            if (distort_image != (Image *) NULL)
+              {
+                Image
+                  *sans_image;
+
+                sans_image=CompareImages(distort_image,reconstruct_image,metric,
+                  &distortion,exception);
+                distort_image=DestroyImage(distort_image);
+                if (sans_image != (Image *) NULL)
+                  sans_image=DestroyImage(sans_image);
+              }
           }
         if (difference_image != (Image *) NULL)
           {
@@ -992,45 +1032,34 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             case FuzzErrorMetric:
             case MeanAbsoluteErrorMetric:
             case MeanSquaredErrorMetric:
-            case RootMeanSquaredErrorMetric:
             case PeakAbsoluteErrorMetric:
+            case RootMeanSquaredErrorMetric:
             {
               (void) FormatLocaleFile(stderr,"%g (%g)",QuantumRange*distortion,
                 (double) distortion);
-              if ((reconstruct_image->columns != image->columns) ||
-                  (reconstruct_image->rows != image->rows))
-                (void) FormatLocaleFile(stderr," @ %.20g,%.20g",(double)
-                  difference_image->page.x,(double) difference_image->page.y);
-              (void) FormatLocaleFile(stderr,"\n");
               break;
             }
             case AbsoluteErrorMetric:
             case NormalizedCrossCorrelationErrorMetric:
-            case PeakSignalToNoiseRatioMetric:
+            case PeakSignalToNoiseRatioErrorMetric:
+            case PerceptualHashErrorMetric:
             {
               (void) FormatLocaleFile(stderr,"%g",distortion);
-              if ((reconstruct_image->columns != image->columns) ||
-                  (reconstruct_image->rows != image->rows))
-                (void) FormatLocaleFile(stderr," @ %.20g,%.20g",(double)
-                  difference_image->page.x,(double) difference_image->page.y);
-              (void) FormatLocaleFile(stderr,"\n");
               break;
             }
-            case MeanErrorPerPixelMetric:
+            case MeanErrorPerPixelErrorMetric:
             {
               (void) FormatLocaleFile(stderr,"%g (%g, %g)",distortion,
                 image->error.normalized_mean_error,
                 image->error.normalized_maximum_error);
-              if ((reconstruct_image->columns != image->columns) ||
-                  (reconstruct_image->rows != image->rows))
-                (void) FormatLocaleFile(stderr," @ %.20g,%.20g",(double)
-                  difference_image->page.x,(double) difference_image->page.y);
-              (void) FormatLocaleFile(stderr,"\n");
               break;
             }
-            case UndefinedMetric:
+            case UndefinedErrorMetric:
               break;
           }
+          if (subimage_search != MagickFalse)
+            (void) FormatLocaleFile(stderr," @ %.20g,%.20g",(double)
+              difference_image->page.x,(double) difference_image->page.y);
         }
       else
         {
@@ -1051,8 +1080,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             case FuzzErrorMetric:
             case MeanAbsoluteErrorMetric:
             case MeanSquaredErrorMetric:
-            case RootMeanSquaredErrorMetric:
             case PeakAbsoluteErrorMetric:
+            case RootMeanSquaredErrorMetric:
             {
               switch (image->colorspace)
               {
@@ -1068,7 +1097,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                   (void) FormatLocaleFile(stderr,"    blue: %g (%g)\n",
                     QuantumRange*channel_distortion[BluePixelChannel],
                     channel_distortion[BluePixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g (%g)\n",
                       QuantumRange*channel_distortion[AlphaPixelChannel],
                       channel_distortion[AlphaPixelChannel]);
@@ -1088,7 +1117,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                   (void) FormatLocaleFile(stderr,"    black: %g (%g)\n",
                     QuantumRange*channel_distortion[BlackPixelChannel],
                     channel_distortion[BlackPixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g (%g)\n",
                       QuantumRange*channel_distortion[AlphaPixelChannel],
                       channel_distortion[AlphaPixelChannel]);
@@ -1099,7 +1128,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                   (void) FormatLocaleFile(stderr,"    gray: %g (%g)\n",
                     QuantumRange*channel_distortion[GrayPixelChannel],
                     channel_distortion[GrayPixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g (%g)\n",
                       QuantumRange*channel_distortion[AlphaPixelChannel],
                       channel_distortion[AlphaPixelChannel]);
@@ -1113,7 +1142,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             }
             case AbsoluteErrorMetric:
             case NormalizedCrossCorrelationErrorMetric:
-            case PeakSignalToNoiseRatioMetric:
+            case PeakSignalToNoiseRatioErrorMetric:
+            case PerceptualHashErrorMetric:
             {
               switch (image->colorspace)
               {
@@ -1126,7 +1156,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                     channel_distortion[GreenPixelChannel]);
                   (void) FormatLocaleFile(stderr,"    blue: %g\n",
                     channel_distortion[BluePixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g\n",
                       channel_distortion[AlphaPixelChannel]);
                   break;
@@ -1141,7 +1171,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                     channel_distortion[YellowPixelChannel]);
                   (void) FormatLocaleFile(stderr,"    black: %g\n",
                     channel_distortion[BlackPixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g\n",
                       channel_distortion[AlphaPixelChannel]);
                   break;
@@ -1150,7 +1180,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                 {
                   (void) FormatLocaleFile(stderr,"    gray: %g\n",
                     channel_distortion[GrayPixelChannel]);
-                  if (image->matte != MagickFalse)
+                  if (image->alpha_trait == BlendPixelTrait)
                     (void) FormatLocaleFile(stderr,"    alpha: %g\n",
                       channel_distortion[AlphaPixelChannel]);
                   break;
@@ -1160,7 +1190,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                 channel_distortion[MaxPixelChannels]);
               break;
             }
-            case MeanErrorPerPixelMetric:
+            case MeanErrorPerPixelErrorMetric:
             {
               (void) FormatLocaleFile(stderr,"    %g (%g, %g)\n",
                 channel_distortion[MaxPixelChannels],
@@ -1168,11 +1198,14 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
                 image->error.normalized_maximum_error);
               break;
             }
-            case UndefinedMetric:
+            case UndefinedErrorMetric:
               break;
           }
           channel_distortion=(double *) RelinquishMagickMemory(
             channel_distortion);
+          if (subimage_search != MagickFalse)
+            (void) FormatLocaleFile(stderr,"   Offset: %.20g,%.20g\n",(double)
+              difference_image->page.x,(double) difference_image->page.y);
         }
       status&=WriteImages(image_info,difference_image,argv[argc-1],exception);
       if ((metadata != (char **) NULL) && (format != (char *) NULL))
@@ -1186,11 +1219,19 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed",
               GetExceptionMessage(errno));
           (void) ConcatenateString(&(*metadata),text);
-          (void) ConcatenateString(&(*metadata),"\n");
           text=DestroyString(text);
         }
       difference_image=DestroyImageList(difference_image);
     }
   DestroyCompare();
+  if ((metric == NormalizedCrossCorrelationErrorMetric) ||
+      (metric == UndefinedErrorMetric))
+    {
+      if (fabs(distortion-1.0) > CompareEpsilon)
+        (void) SetImageOption(image_info,"compare:dissimilar","true");
+    }
+  else
+    if (fabs(distortion) > CompareEpsilon)
+      (void) SetImageOption(image_info,"compare:dissimilar","true");
   return(status != 0 ? MagickTrue : MagickFalse);
 }