]> granicus.if.org Git - imagemagick/commitdiff
Adding a binomial kernel (for Fred)
authoranthony <anthony@git.imagemagick.org>
Thu, 2 Aug 2012 13:23:28 +0000 (13:23 +0000)
committeranthony <anthony@git.imagemagick.org>
Thu, 2 Aug 2012 13:23:28 +0000 (13:23 +0000)
MagickCore/morphology.c
MagickCore/morphology.h
MagickCore/option.c

index d87a32342170b7ff732f324cb39b8bee9b218bdb..b54949db0d919a862bbb405bbc10f014c2b5e589 100644 (file)
@@ -114,6 +114,21 @@ static inline double MagickMax(const double x,const double y)
 #define Minimize(assign,value) assign=MagickMin(assign,value)
 #define Maximize(assign,value) assign=MagickMax(assign,value)
 
+/* Integer Factorial Function - for a Binomial kernel */
+#if 1
+static inline size_t fact(size_t n)
+{
+  size_t l,f;
+  for(f=1, l=2; l <= n; f=f*l, l++);
+  return(f);
+}
+#elif 1 /* glibc floating point alternatives */
+#define fact(n) ((size_t)tgamma((double)n+1))
+#else
+#define fact(n) ((size_t)lgamma((double)n+1))
+#endif
+
+
 /* Currently these are only internal to this module */
 static void
   CalcKernelMetaData(KernelInfo *),
@@ -633,6 +648,10 @@ MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
 %       Note that the first argument is the width of the kernel and not the
 %       radius of the kernel.
 %
+%    Binomial:[{radius}]
+%       Generate a discrete kernel using a 2 dimentional Pascel's Triangle
+%       of values.
+%
 %    # Still to be implemented...
 %    #
 %    # Filter2D
@@ -995,6 +1014,7 @@ MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
     case LoGKernel:
     case BlurKernel:
     case CometKernel:
+    case BinomialKernel:
     case DiamondKernel:
     case SquareKernel:
     case RectangleKernel:
@@ -1289,6 +1309,37 @@ MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
         RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
         break;
       }
+    case BinomialKernel:
+      {
+        size_t
+          order_f;
+
+        if (args->rho < 1.0)
+          kernel->width = kernel->height = 3;  /* default radius = 1 */
+        else
+          kernel->width = kernel->height = ((size_t)args->rho)*2+1;
+        kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
+
+        order_f = fact(kernel->width-1);
+
+        kernel->values=(double *) AcquireAlignedMemory(kernel->width,
+          kernel->height*sizeof(*kernel->values));
+        if (kernel->values == (double *) NULL)
+          return(DestroyKernelInfo(kernel));
+
+        /* set all kernel values within diamond area to scale given */
+        for ( i=0, v=0; v < (ssize_t)kernel->height; v++)
+          { size_t
+              alpha = order_f / ( fact(v) * fact(kernel->height-v-1) );
+            for ( u=0; u < (ssize_t)kernel->width; u++, i++)
+              kernel->positive_range += kernel->values[i] = (double)
+                (alpha * order_f / ( fact(u) * fact(kernel->height-u-1) ));
+          }
+        kernel->minimum = 1.0;
+        kernel->maximum = kernel->values[kernel->x+kernel->y*kernel->width];
+        kernel->negative_range = 0.0;
+        break;
+      }
 
     /*
       Convolution Kernels - Well Known Named Constant Kernels
index 62311845f550c856eaa4195e53301e5ed8b2440b..dd359596aa0fb967c3a9e48aa87141374df35f29 100644 (file)
@@ -33,6 +33,7 @@ typedef enum
   LoGKernel,
   BlurKernel,
   CometKernel,
+  BinomialKernel,
   LaplacianKernel,    /* Convolution Kernels, by Name */
   SobelKernel,
   FreiChenKernel,
index a34446ba88360b246b7ab9941e471564949d8095..9b5283d83145728beea92e13ef370d26b08b5eb4 100644 (file)
@@ -1117,6 +1117,7 @@ static const OptionInfo
     { "LoG", LoGKernel, UndefinedOptionFlag, MagickFalse },
     { "Blur", BlurKernel, UndefinedOptionFlag, MagickFalse },
     { "Comet", CometKernel, UndefinedOptionFlag, MagickFalse },
+    { "Binomial", BinomialKernel, UndefinedOptionFlag, MagickFalse },
     { "Laplacian", LaplacianKernel, UndefinedOptionFlag, MagickFalse },
     { "Sobel", SobelKernel, UndefinedOptionFlag, MagickFalse },
     { "FreiChen", FreiChenKernel, UndefinedOptionFlag, MagickFalse },
@@ -1709,7 +1710,7 @@ static const OptionInfo *GetOptionInfo(const CommandOption option)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  CloneImageOptions() clones one or more image options.
+%  CloneImageOptions() clones all global image options, to another image_info
 %
 %  The format of the CloneImageOptions method is:
 %
@@ -1718,9 +1719,9 @@ static const OptionInfo *GetOptionInfo(const CommandOption option)
 %
 %  A description of each parameter follows:
 %
-%    o image_info: the image info.
+%    o image_info: the image info to recieve the cloned options.
 %
-%    o clone_info: the clone image info.
+%    o clone_info: the source image info for options to clone.
 %
 */
 MagickExport MagickBooleanType CloneImageOptions(ImageInfo *image_info,
@@ -1751,7 +1752,8 @@ MagickExport MagickBooleanType CloneImageOptions(ImageInfo *image_info,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %  DefineImageOption() associates an assignment string of the form
-%  "key=value" with an image option. It is equivelent to SetImageOption().
+%  "key=value" with a global image option. It is equivelent to
+%  SetImageOption().
 %
 %  The format of the DefineImageOption method is:
 %
@@ -1799,7 +1801,7 @@ MagickExport MagickBooleanType DefineImageOption(ImageInfo *image_info,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  DeleteImageOption() deletes an key from the image map.
+%  DeleteImageOption() deletes an key from the global image options.
 %
 %  Returns MagickTrue is the option is found and deleted from the Options.
 %
@@ -1839,7 +1841,8 @@ MagickExport MagickBooleanType DeleteImageOption(ImageInfo *image_info,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  DestroyImageOptions() releases memory associated with image option values.
+%  DestroyImageOptions() destroys all global options and associated memory
+%  attached to the given image_info image list.
 %
 %  The format of the DestroyDefines method is:
 %
@@ -1872,7 +1875,10 @@ MagickExport void DestroyImageOptions(ImageInfo *image_info)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetImageOption() gets a value associated with an image option.
+%  GetImageOption() gets a value associated with the global image options.
+%
+%  The returned string is a constant string in the tree and should NOT be
+%  freed by the caller.
 %
 %  The format of the GetImageOption method is:
 %
@@ -2151,7 +2157,7 @@ MagickExport char **GetCommandOptions(const CommandOption value)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetNextImageOption() gets the next image option value.
+%  GetNextImageOption() gets the next global option value.
 %
 %  The format of the GetNextImageOption method is:
 %
@@ -2595,6 +2601,9 @@ MagickExport ssize_t ParsePixelChannelOption(const char *channels)
 %
 %  RemoveImageOption() removes an option from the image and returns its value.
 %
+%  In this case the ConstantString() value returned should be freed by the
+%  caller when finished.
+%
 %  The format of the RemoveImageOption method is:
 %
 %      char *RemoveImageOption(ImageInfo *image_info,const char *option)
@@ -2635,7 +2644,7 @@ MagickExport char *RemoveImageOption(ImageInfo *image_info,const char *option)
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %  ResetImageOptions() resets the image_info option.  That is, it deletes
-%  all options associated with the image_info structure.
+%  all global options associated with the image_info structure.
 %
 %  The format of the ResetImageOptions method is:
 %
@@ -2739,7 +2748,7 @@ MagickExport MagickBooleanType SetImageOption(ImageInfo *image_info,
     image_info->options=NewSplayTree(CompareSplayTreeString,
       RelinquishMagickMemory,RelinquishMagickMemory);
 
-  /* Delete Option if NULL */
+  /* Delete Option if NULL --  empty string values are valid! */
   if (value == (const char *) NULL)
     return(DeleteImageOption(image_info,option));