]> granicus.if.org Git - imagemagick/commitdiff
More improvements to perceptual hash
authorCristy <urban-warrior@imagemagick.org>
Sat, 3 Sep 2016 15:31:13 +0000 (11:31 -0400)
committerCristy <urban-warrior@imagemagick.org>
Sat, 3 Sep 2016 15:31:13 +0000 (11:31 -0400)
MagickCore/compare.c
MagickCore/identify.c
coders/json.c

index c899dfd6b036bed5b358eabe865fca8dde9fad7b..7ca7487e794aab62ff6e51fe06bf0323c59ba381 100644 (file)
@@ -1153,7 +1153,7 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   const Image *reconstruct_image,double *distortion,ExceptionInfo *exception)
 {
   ChannelPerceptualHash
-    *image_phash,
+    *channel_phash,
     *reconstruct_phash;
 
   ssize_t
@@ -1162,13 +1162,14 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   /*
     Compute perceptual hash in the sRGB colorspace.
   */
-  image_phash=GetImagePerceptualHash(image,exception);
-  if (image_phash == (ChannelPerceptualHash *) NULL)
+  channel_phash=GetImagePerceptualHash(image,exception);
+  if (channel_phash == (ChannelPerceptualHash *) NULL)
     return(MagickFalse);
   reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
   if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
     {
-      image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
+      channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
+        channel_phash);
       return(MagickFalse);
     }
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
@@ -1189,40 +1190,15 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
         alpha,
         beta;
 
-      alpha=image_phash[channel].srgb_hu_phash[i];
-      beta=reconstruct_phash[channel].srgb_hu_phash[i];
-      difference+=(beta-alpha)*(beta-alpha);
-    }
-    distortion[channel]+=difference;
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
-    #pragma omp critical (MagickCore_GetPerceptualHashDistortion)
-#endif
-    distortion[CompositePixelChannel]+=difference;
-  }
-  /*
-    Compute perceptual hash in the HCLP colorspace.
-  */
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(static,4)
-#endif
-  for (channel=0; channel < MaxPixelChannels; channel++)
-  {
-    double
-      difference;
-
-    register ssize_t
-      i;
-
-    difference=0.0;
-    for (i=0; i < MaximumNumberOfImageMoments; i++)
-    {
-      double
-        alpha,
-        beta;
+      register ssize_t
+        j;
 
-      alpha=image_phash[channel].hclp_hu_phash[i];
-      beta=reconstruct_phash[channel].hclp_hu_phash[i];
-      difference+=(beta-alpha)*(beta-alpha);
+      for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
+      {
+        alpha=channel_phash[channel].phash[j][i];
+        beta=reconstruct_phash[channel].phash[j][i];
+        difference+=(beta-alpha)*(beta-alpha);
+      }
     }
     distortion[channel]+=difference;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
@@ -1235,7 +1211,7 @@ static MagickBooleanType GetPerceptualHashDistortion(const Image *image,
   */
   reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
     reconstruct_phash);
-  image_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(image_phash);
+  channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
   return(MagickTrue);
 }
 
index 1071353411936bf2e22565cd497a29d86f39626d..038dd0100e8df397619482e9abe5ee5658a1c66f 100644 (file)
@@ -403,9 +403,6 @@ static ssize_t PrintChannelPerceptualHash(Image *image,FILE *file,
   (void) FormatLocaleFile(file,"\n");
   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
   {
-    const char
-      *name;
-
     register ssize_t
       j;
 
@@ -413,67 +410,7 @@ static ssize_t PrintChannelPerceptualHash(Image *image,FILE *file,
     PixelTrait traits=GetPixelChannelTraits(image,channel);
     if (traits == UndefinedPixelTrait)
       continue;
-    switch (channel)
-    {
-      case RedPixelChannel:
-      {
-        name="Red";
-        if (image->colorspace == CMYKColorspace)
-          name="Cyan";
-        if (image->colorspace == GRAYColorspace)
-          name="Gray";
-        break;
-      }
-      case GreenPixelChannel:
-      {
-        name="Green";
-        if (image->colorspace == CMYKColorspace)
-          name="Magenta";
-        break;
-      }
-      case BluePixelChannel:
-      {
-        name="Blue";
-        if (image->colorspace == CMYKColorspace)
-          name="Yellow";
-        break;
-      }
-      case BlackPixelChannel:
-      {
-        name="Black";
-        if (image->storage_class == PseudoClass)
-          name="Index";
-        break;
-      }
-      case IndexPixelChannel:
-      {
-        name="Index";
-        break;
-      }
-      case AlphaPixelChannel:
-      {
-        name="Alpha";
-        break;
-      }
-      case ReadMaskPixelChannel:
-      {
-        name="ReadMask";
-        break;
-      }
-      case WriteMaskPixelChannel:
-      {
-        name="WriteMask";
-        break;
-      }
-      case MetaPixelChannel:
-      {
-        name="Meta";
-        break;
-      }
-      default:
-        name="Undefined";
-    }
-    n=FormatLocaleFile(file,"    %s:\n",name);
+    n=FormatLocaleFile(file,"    Channel %.20g:\n",(double) channel);
     for (j=0; j < MaximumNumberOfPerceptualHashes; j++)
     {
       register ssize_t
index c6d335a0f9a84e85155d22be84baaa6ed88c9d8e..ee43d7d722bf71a906b77d9ea285d08512e30a4c 100644 (file)
@@ -608,8 +608,7 @@ static ssize_t PrintChannelMoments(FILE *file,const PixelChannel channel,
   return(n);
 }
 
-static ssize_t PrintChannelPerceptualHash(FILE *file,const ChannelType channel,
-  const char *name,const MagickBooleanType separator,
+static ssize_t PrintChannelPerceptualHash(Image *image,FILE *file,
   const ChannelPerceptualHash *channel_phash)
 {
   register ssize_t
@@ -618,20 +617,46 @@ static ssize_t PrintChannelPerceptualHash(FILE *file,const ChannelType channel,
   ssize_t
     n;
 
-  n=FormatLocaleFile(file,"      \"%s\": {\n",name);
-  for (i=0; i < 6; i++)
-    n+=FormatLocaleFile(file,
-      "        \"PH%.20g\": [ \"%.*g\", \"%.*g\" ],\n",i+1.0,
-      GetMagickPrecision(),channel_phash[channel].srgb_hu_phash[i],
-      GetMagickPrecision(),channel_phash[channel].hclp_hu_phash[i]);
-  n+=FormatLocaleFile(file,
-    "        \"PH%.20g\": [ \"%.*g\", \"%.*g\" ]\n",i+1.0,
-    GetMagickPrecision(),channel_phash[channel].srgb_hu_phash[i],
-    GetMagickPrecision(),channel_phash[channel].hclp_hu_phash[i]);
-  (void) FormatLocaleFile(file,"      }");
-  if (separator != MagickFalse)
-    (void) FormatLocaleFile(file,",");
-  (void) FormatLocaleFile(file,"\n");
+  (void) FormatLocaleFile(file,"      \"colorspaces\": [ ");
+  for (i=0; i < (ssize_t) channel_phash[0].number_colorspaces; i++)
+  {
+    (void) FormatLocaleFile(file,"\"%s\"",CommandOptionToMnemonic(
+      MagickColorspaceOptions,(ssize_t) channel_phash[0].colorspace[i]));
+    if (i < (ssize_t) (channel_phash[0].number_colorspaces-1))
+      (void) FormatLocaleFile(file,", ");
+  }
+  (void) FormatLocaleFile(file,"],\n");
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    register ssize_t
+      j;
+
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    PixelTrait traits=GetPixelChannelTraits(image,channel);
+    if (traits == UndefinedPixelTrait)
+      continue;
+    n=FormatLocaleFile(file,"      \"Channel%.20g\": {\n",(double) channel);
+    for (j=0; j < MaximumNumberOfPerceptualHashes; j++)
+    {
+      register ssize_t
+        k;
+
+      n+=FormatLocaleFile(file,"        \"PH%.20g\": [",(double) j+1);
+      for (k=0; k < (ssize_t) channel_phash[0].number_colorspaces; k++)
+      {
+        n+=FormatLocaleFile(file,"\"%.*g\"",GetMagickPrecision(),
+          channel_phash[channel].phash[k][j]);
+        if (k < (ssize_t) (channel_phash[0].number_colorspaces-1))
+          n+=FormatLocaleFile(file,", ");
+      }
+      n+=FormatLocaleFile(file,"]");
+      if (j < (MaximumNumberOfPerceptualHashes-1))
+        n+=FormatLocaleFile(file,",\n");
+    }
+    if (i < (GetPixelChannels(image)-1))
+      n+=FormatLocaleFile(file,"\n      },\n");
+  }
+  n+=FormatLocaleFile(file,"\n      }\n");
   return(n);
 }
 
@@ -1029,15 +1054,7 @@ static MagickBooleanType EncodeImageAttributes(Image *image,FILE *file,
   if (channel_phash != (ChannelPerceptualHash *) NULL)
     {
       (void) FormatLocaleFile(file,"    \"channelPerceptualHash\": {\n");
-      if (image->alpha_trait != UndefinedPixelTrait)
-        (void) PrintChannelPerceptualHash(file,AlphaChannel,"alphaAlpha",
-          MagickTrue,channel_phash);
-      (void) PrintChannelPerceptualHash(file,RedChannel,"redHue",MagickTrue,
-        channel_phash);
-      (void) PrintChannelPerceptualHash(file,GreenChannel,"greenChroma",
-        MagickTrue,channel_phash);
-      (void) PrintChannelPerceptualHash(file,BlueChannel,"blueLuma",MagickFalse,
-        channel_phash);
+      (void) PrintChannelPerceptualHash(image,file,channel_phash);
       (void) FormatLocaleFile(file,"    },\n");
       channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
         channel_phash);