]> granicus.if.org Git - imagemagick/blobdiff - MagickWand/compare.c
Fixed compiler warning.
[imagemagick] / MagickWand / compare.c
index 0531d4bc0ff8af8f5dfa927197b733a8af5bbc3e..1a49cb5a669f817e992eb35e8faad70e545fdd70 100644 (file)
 %                         Image Comparison Methods                            %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                               December 2003                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 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",
@@ -172,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) \
@@ -210,7 +215,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
   double
     dissimilarity_threshold,
     distortion,
-    similarity_metric;
+    similarity_metric,
+    similarity_threshold;
 
   Image
     *difference_image,
@@ -266,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;
@@ -812,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 == '+')
@@ -918,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
@@ -947,6 +977,12 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             &distortion,exception);
         else
           {
+            Image
+              *distort_image;
+
+            RectangleInfo
+              page;
+
             (void) CompositeImage(composite_image,reconstruct_image,
               CopyCompositeOp,MagickTrue,offset.x,offset.y,exception);
             difference_image=CompareImages(image,composite_image,metric,
@@ -957,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)
           {
@@ -980,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
         {
@@ -1039,8 +1080,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             case FuzzErrorMetric:
             case MeanAbsoluteErrorMetric:
             case MeanSquaredErrorMetric:
-            case RootMeanSquaredErrorMetric:
             case PeakAbsoluteErrorMetric:
+            case RootMeanSquaredErrorMetric:
             {
               switch (image->colorspace)
               {
@@ -1101,7 +1142,8 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             }
             case AbsoluteErrorMetric:
             case NormalizedCrossCorrelationErrorMetric:
-            case PeakSignalToNoiseRatioMetric:
+            case PeakSignalToNoiseRatioErrorMetric:
+            case PerceptualHashErrorMetric:
             {
               switch (image->colorspace)
               {
@@ -1148,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],
@@ -1156,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))
@@ -1174,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();
-  return((status != 0) || (distortion != 0.0) ? MagickTrue : MagickFalse);
+  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);
 }