]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/effect.c
(no commit message)
[imagemagick] / MagickCore / effect.c
index 864bfc090ecba748090465f5b46c51c92bed34d6..7ba273c2858ba724375776c71dc3fa402592607b 100644 (file)
 %                       MagickCore Image Effects Methods                      %
 %                                                                             %
 %                               Software Design                               %
-%                                 John Cristy                                 %
+%                                    Cristy                                   %
 %                                 October 1996                                %
 %                                                                             %
 %                                                                             %
-%  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  %
 #include "MagickCore/image-private.h"
 #include "MagickCore/list.h"
 #include "MagickCore/log.h"
+#include "MagickCore/matrix.h"
 #include "MagickCore/memory_.h"
 #include "MagickCore/memory-private.h"
 #include "MagickCore/monitor.h"
 #include "MagickCore/monitor-private.h"
 #include "MagickCore/montage.h"
 #include "MagickCore/morphology.h"
+#include "MagickCore/morphology-private.h"
 #include "MagickCore/paint.h"
 #include "MagickCore/pixel-accessor.h"
 #include "MagickCore/pixel-private.h"
@@ -263,7 +265,7 @@ MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
     if (kernel[i] == (MagickRealType *) NULL)
       break;
     normalize=0.0;
-    j=(ssize_t) (width-i)/2;
+    j=(ssize_t) (width-i-1)/2;
     k=0;
     for (v=(-j); v <= j; v++)
     {
@@ -275,11 +277,9 @@ MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
         k++;
       }
     }
-    if (fabs(normalize) < MagickEpsilon)
-      normalize=MagickEpsilon;
-    normalize=PerceptibleReciprocal(normalize);
-    for (k=0; k < (j*j); k++)
-      kernel[i][k]=normalize*kernel[i][k];
+    kernel[i][(j-1)/2]+=(1.0-normalize);
+    if (sigma < MagickEpsilon)
+      kernel[i][(j-1)/2]=1.0;
   }
   if (i < (ssize_t) width)
     {
@@ -383,7 +383,7 @@ MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
             (blur_traits == UndefinedPixelTrait))
           continue;
         if (((blur_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p+center) == 0))
+            (GetPixelReadMask(image,p+center) == 0))
           {
             SetPixelChannel(blur_image,channel,p[center+i],q);
             continue;
@@ -600,11 +600,9 @@ MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
         k++;
       }
     }
-    if (fabs(normalize) < MagickEpsilon)
-      normalize=MagickEpsilon;
-    normalize=PerceptibleReciprocal(normalize);
-    for (k=0; k < (j*j); k++)
-      kernel[i][k]=normalize*kernel[i][k];
+    kernel[i][(k-1)/2]=(double) ((-2.0)*normalize);
+    if (sigma < MagickEpsilon)
+      kernel[i][(k-1)/2]=1.0;
   }
   if (i < (ssize_t) width)
     {
@@ -660,8 +658,8 @@ MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
         center,
         j;
 
-      j=(ssize_t) ceil((double) width*QuantumScale*
-        GetPixelIntensity(edge_image,r)-0.5);
+      j=(ssize_t) ceil((double) width*(1.0-QuantumScale*
+        GetPixelIntensity(edge_image,r))-0.5);
       if (j < 0)
         j=0;
       else
@@ -708,7 +706,7 @@ MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
             (sharp_traits == UndefinedPixelTrait))
           continue;
         if (((sharp_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p+center) == 0))
+            (GetPixelReadMask(image,p+center) == 0))
           {
             SetPixelChannel(sharp_image,channel,p[center+i],q);
             continue;
@@ -828,8 +826,7 @@ MagickExport Image *BlurImage(const Image *image,const double radius,
     *kernel_info;
 
   Image
-    *blur_image,
-    *morphology_image;
+    *blur_image;
 
   assert(image != (const Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -837,29 +834,508 @@ MagickExport Image *BlurImage(const Image *image,const double radius,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g",radius,
-    sigma);
+  (void) FormatLocaleString(geometry,MaxTextExtent,
+    "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
   kernel_info=AcquireKernelInfo(geometry);
   if (kernel_info == (KernelInfo *) NULL)
     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
-  morphology_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
-    exception);
+  blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
   kernel_info=DestroyKernelInfo(kernel_info);
-  if (morphology_image == (Image *) NULL)
-    return((Image *) NULL);
-  (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g+90",radius,
-    sigma);
+  return(blur_image);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%     C a n n y E d g e I m a g e                                             %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of
+%  edges in images.
+%
+%  The format of the EdgeImage method is:
+%
+%      Image *CannyEdgeImage(const Image *image,const double radius,
+%        const double sigma,const double lower_precent,
+%        const double upper_percent,const size_t threshold,
+%        ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+%    o channel: the channel type.
+%
+%    o radius: the radius of the gaussian smoothing filter.
+%
+%    o sigma: the sigma of the gaussian smoothing filter.
+%
+%    o lower_precent: percentage of edge pixels in the lower threshold.
+%
+%    o upper_percent: percentage of edge pixels in the upper threshold.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+
+typedef struct _CannyInfo
+{
+  double
+    magnitude,
+    intensity;
+
+  int
+    orientation;
+
+  ssize_t
+    x,
+    y;
+} CannyInfo;
+
+static inline MagickBooleanType IsAuthenticPixel(const Image *image,
+  const ssize_t x,const ssize_t y)
+{
+  if ((x < 0) || (x >= (ssize_t) image->columns))
+    return(MagickFalse);
+  if ((y < 0) || (y >= (ssize_t) image->rows))
+    return(MagickFalse);
+  return(MagickTrue);
+}
+
+static MagickBooleanType TraceEdges(Image *edge_image,CacheView *trace_view,
+  MatrixInfo *pixel_cache,const ssize_t x,const ssize_t y,
+  const double lower_threshold,ExceptionInfo *exception)
+{
+  CannyInfo
+    pixel;
+
+  size_t
+    number_edges;
+
+  if (GetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
+    return(MagickFalse);
+  pixel.x=x;
+  pixel.y=y;
+  if (SetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
+    return(MagickFalse);
+  number_edges=1;
+  do
+  {
+    MagickBooleanType
+      status;
+
+    ssize_t
+      v,
+      x_offset,
+      y_offset;
+
+    number_edges--;
+    status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,&pixel);
+    if (status == MagickFalse)
+      return(MagickFalse);
+    x_offset=pixel.x;
+    y_offset=pixel.y;
+    for (v=(-1); v <= 1; v++)
+    {
+      ssize_t
+        u;
+
+      for (u=(-1); u <= 1; u++)
+      {
+        Quantum
+          *q;
+
+        if ((u == 0) && (v == 0))
+          continue;
+        if (IsAuthenticPixel(edge_image,x_offset+u,y_offset+v) == MagickFalse)
+          continue;
+        /*
+          Not an edge if gradient value is below the lower threshold.
+        */
+        q=GetCacheViewAuthenticPixels(trace_view,x_offset+u,y_offset+v,1,1,
+          exception);
+        if (q == (Quantum *) NULL)
+          return(MagickFalse);
+        status=GetMatrixElement(pixel_cache,x_offset+u,y_offset+v,&pixel);
+        if (status == MagickFalse)
+          return(MagickFalse);
+        if ((pixel.intensity >= lower_threshold) &&
+            (GetPixelIntensity(edge_image,q) == 0))
+          {
+            *q=QuantumRange;
+            status=SyncCacheViewAuthenticPixels(trace_view,exception);
+            if (status == MagickFalse)
+              return(MagickFalse);
+            status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
+              &pixel);
+            if (status == MagickFalse)
+              return(MagickFalse);
+            pixel.x=x_offset+u;
+            pixel.y=y_offset+v;
+            status=SetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
+              &pixel);
+            if (status == MagickFalse)
+              return(MagickFalse);
+            number_edges++;
+          }
+      }
+    }
+  } while (number_edges != 0);
+  return(MagickTrue);
+}
+
+
+MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
+  const double sigma,const double lower_precent,const double upper_percent,
+  ExceptionInfo *exception)
+{
+  CacheView
+    *edge_view,
+    *trace_view;
+
+  char
+    geometry[MaxTextExtent];
+
+  double
+    upper_threshold,
+    lower_threshold;
+
+  Image
+    *edge_image;
+
+  KernelInfo
+    *kernel_info;
+
+  MagickBooleanType
+    status;
+
+  MatrixInfo
+    *pixel_cache;
+
+  register ssize_t
+    i;
+
+  size_t
+    *histogram,
+    number_pixels;
+
+  ssize_t
+    count,
+    y;
+
+  assert(image != (const Image *) NULL);
+  assert(image->signature == MagickSignature);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  /*
+    Filter out noise.
+  */
+  (void) FormatLocaleString(geometry,MaxTextExtent,
+    "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
   kernel_info=AcquireKernelInfo(geometry);
   if (kernel_info == (KernelInfo *) NULL)
+    ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+  edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
+  kernel_info=DestroyKernelInfo(kernel_info);
+  if (edge_image == (Image *) NULL)
+    return((Image *) NULL);
+  if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
     {
-      morphology_image=DestroyImage(morphology_image);
-      ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+      edge_image=DestroyImage(edge_image);
+      return((Image *) NULL);
     }
-  blur_image=MorphologyImage(morphology_image,ConvolveMorphology,1,kernel_info,
-    exception);
-  morphology_image=DestroyImage(morphology_image);
-  kernel_info=DestroyKernelInfo(kernel_info);
-  return(blur_image);
+  /*
+    Find the intensity gradient of the image.
+  */
+  pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
+    sizeof(CannyInfo),exception);
+  if (pixel_cache == (MatrixInfo *) NULL)
+    {
+      edge_image=DestroyImage(edge_image);
+      return((Image *) NULL);
+    }
+  status=MagickTrue;
+  edge_view=AcquireVirtualCacheView(edge_image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(edge_image,edge_image,edge_image->rows,1)
+#endif
+  for (y=0; y < (ssize_t) edge_image->rows; y++)
+  {
+    register const Quantum
+      *restrict p;
+
+    register ssize_t
+      x;
+
+    if (status == MagickFalse)
+      continue;
+    p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
+      exception);
+    if (p == (const Quantum *) NULL)
+      {
+        status=MagickFalse;
+        continue;
+      }
+    for (x=0; x < (ssize_t) edge_image->columns; x++)
+    {
+      CannyInfo
+        pixel;
+
+      double
+        dx,
+        dy;
+
+      register const Quantum
+        *restrict kernel_pixels;
+
+      ssize_t
+        v;
+
+      static double
+        Gx[2][2] =
+        {
+          { -1.0,  +1.0 },
+          { -1.0,  +1.0 }
+        },
+        Gy[2][2] =
+        {
+          { +1.0, +1.0 },
+          { -1.0, -1.0 }
+        };
+
+      (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
+      dx=0.0;
+      dy=0.0;
+      kernel_pixels=p;
+      for (v=0; v < 2; v++)
+      {
+        ssize_t
+          u;
+
+        for (u=0; u < 2; u++)
+        {
+          double
+            intensity;
+
+          intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
+          dx+=3.0*Gx[v][u]*intensity/2.0;
+          dy+=3.0*Gy[v][u]*intensity/2.0;
+        }
+        kernel_pixels+=edge_image->columns+1;
+      }
+      pixel.magnitude=sqrt(dx*dx+dy*dy);
+      pixel.orientation=0;
+      if (dx != 0.0)
+        {
+          double
+            theta;
+
+          theta=dy/dx;
+          if (theta < 0.0)
+            {
+              if (theta < -2.41421356237)
+                pixel.orientation=2;
+              else
+                if (theta < -0.414213562373)
+                  pixel.orientation=1;
+                else
+                  pixel.orientation=0;
+            }
+          else
+            {
+              if (theta > 2.41421356237)
+                pixel.orientation=2;
+              else
+                if (theta > 0.414213562373)
+                  pixel.orientation=3;
+                else
+                  pixel.orientation=0;
+            }
+        }
+      if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
+        continue;
+      p+=GetPixelChannels(edge_image);
+    }
+  }
+  edge_view=DestroyCacheView(edge_view);
+  /*
+    Non-maxima suppression, remove pixels that are not considered to be part
+    of an edge.
+  */
+  histogram=(size_t *) AcquireQuantumMemory(65536,sizeof(*histogram));
+  if (histogram == (size_t *) NULL)
+    {
+      pixel_cache=DestroyMatrixInfo(pixel_cache);
+      edge_image=DestroyImage(edge_image);
+      return((Image *) NULL);
+    }
+  (void) ResetMagickMemory(histogram,0,65536*sizeof(*histogram));
+  edge_view=AcquireAuthenticCacheView(edge_image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(edge_image,edge_image,edge_image->rows,1)
+#endif
+  for (y=0; y < (ssize_t) edge_image->rows; y++)
+  {
+    register Quantum
+      *restrict q;
+
+    register ssize_t
+      x;
+
+    if (status == MagickFalse)
+      continue;
+    q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
+      exception);
+    if (q == (Quantum *) NULL)
+      {
+        status=MagickFalse;
+        continue;
+      }
+    for (x=0; x < (ssize_t) edge_image->columns; x++)
+    {
+      CannyInfo
+        alpha_pixel,
+        beta_pixel,
+        pixel;
+
+      (void) GetMatrixElement(pixel_cache,x,y,&pixel);
+      switch (pixel.orientation)
+      {
+        case 0:
+        {
+          /*
+            0 degrees.
+          */
+          (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
+          (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
+          break;
+        }
+        case 1:
+        {
+          /*
+            45 degrees.
+          */
+          (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
+          (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
+          break;
+        }
+        case 2:
+        {
+          /*
+            90 degrees.
+          */
+          (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
+          (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
+          break;
+        }
+        case 3:
+        {
+          /*
+            135 degrees.
+          */
+          (void) GetMatrixElement(pixel_cache,x+1,y-1,&alpha_pixel);
+          (void) GetMatrixElement(pixel_cache,x-1,y+1,&beta_pixel);
+          break;
+        }
+      }
+      pixel.intensity=pixel.magnitude;
+      if ((pixel.magnitude < alpha_pixel.magnitude) ||
+          (pixel.magnitude < beta_pixel.magnitude))
+        pixel.intensity=0;
+      else
+        if (pixel.magnitude > QuantumRange)
+          pixel.intensity=QuantumRange;
+      (void) SetMatrixElement(pixel_cache,x,y,&pixel);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+      #pragma omp critical (MagickCore_CannyEdgeImage)
+#endif
+      histogram[ScaleQuantumToShort(ClampToQuantum(pixel.intensity))]++;
+      *q=0;
+      q+=GetPixelChannels(edge_image);
+    }
+    if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
+      status=MagickFalse;
+  }
+  edge_view=DestroyCacheView(edge_view);
+  /*
+    Estimate hysteresis threshold.
+  */
+  number_pixels=(size_t) (lower_precent*(image->columns*image->rows-
+    histogram[0]));
+  count=0;
+  for (i=65535; count < (ssize_t) number_pixels; i--)
+    count+=histogram[i];
+  upper_threshold=(double) ScaleShortToQuantum((unsigned short) i);
+  for (i=0; histogram[i] == 0; i++) ;
+  lower_threshold=upper_percent*(upper_threshold+
+    ScaleShortToQuantum((unsigned short) i));
+  histogram=(size_t *) RelinquishMagickMemory(histogram);
+  /*
+    Hysteresis threshold.
+  */
+  edge_view=AcquireAuthenticCacheView(edge_image,exception);
+  trace_view=AcquireAuthenticCacheView(edge_image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(edge_image,edge_image,edge_image->rows,1)
+#endif
+  for (y=0; y < (ssize_t) edge_image->rows; y++)
+  {
+    register Quantum
+      *restrict q;
+
+    register ssize_t
+      x;
+
+    if (status == MagickFalse)
+      continue;
+    q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
+      exception);
+    if (q == (Quantum *) NULL)
+      {
+        status=MagickFalse;
+        continue;
+      }
+    for (x=0; x < (ssize_t) edge_image->columns; x++)
+    {
+      CannyInfo
+        pixel;
+
+      /*
+        Edge if pixel gradient higher than upper threshold.
+      */
+      status=GetMatrixElement(pixel_cache,x,y,&pixel);
+      if (status == MagickFalse)
+        break;
+      if ((pixel.intensity >= upper_threshold) &&
+          (GetPixelIntensity(edge_image,q) == 0))
+        {
+          *q=QuantumRange;
+          status=TraceEdges(edge_image,trace_view,pixel_cache,x,y,
+            lower_threshold,exception);
+          if (status == MagickFalse)
+            break;
+        }
+      q+=GetPixelChannels(edge_image);
+    }
+    if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
+      status=MagickFalse;
+  }
+  trace_view=DestroyCacheView(trace_view);
+  edge_view=DestroyCacheView(edge_view);
+  pixel_cache=DestroyMatrixInfo(pixel_cache);
+  return(edge_image);
 }
 \f
 /*
@@ -892,7 +1368,12 @@ MagickExport Image *BlurImage(const Image *image,const double radius,
 MagickExport Image *ConvolveImage(const Image *image,
   const KernelInfo *kernel_info,ExceptionInfo *exception)
 {
-  return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
+  Image
+    *convolve_image;
+
+  convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
+  return(convolve_image);
 }
 \f
 /*
@@ -937,6 +1418,10 @@ static void Hull(const Image *image,const ssize_t x_offset,
   ssize_t
     y;
 
+  assert(image != (const Image *) NULL);
+  assert(image->signature == MagickSignature);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(f != (Quantum *) NULL);
   assert(g != (Quantum *) NULL);
   p=f+(columns+2);
@@ -1030,6 +1515,10 @@ MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
   MagickBooleanType
     status;
 
+  MemoryInfo
+    *buffer_info,
+    *pixel_info;
+
   Quantum
     *restrict buffer,
     *restrict pixels;
@@ -1066,17 +1555,20 @@ MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
     Allocate image buffer.
   */
   length=(size_t) ((image->columns+2)*(image->rows+2));
-  pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
-  buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
-  if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
+  pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
+  buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
+  if ((pixel_info == (MemoryInfo *) NULL) ||
+      (buffer_info == (MemoryInfo *) NULL))
     {
-      if (buffer != (Quantum *) NULL)
-        buffer=(Quantum *) RelinquishMagickMemory(buffer);
-      if (pixels != (Quantum *) NULL)
-        pixels=(Quantum *) RelinquishMagickMemory(pixels);
+      if (buffer_info != (MemoryInfo *) NULL)
+        buffer_info=RelinquishVirtualMemory(buffer_info);
+      if (pixel_info != (MemoryInfo *) NULL)
+        pixel_info=RelinquishVirtualMemory(pixel_info);
       despeckle_image=DestroyImage(despeckle_image);
       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
     }
+  pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
+  buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
   /*
     Reduce speckle in the image.
   */
@@ -1179,8 +1671,8 @@ MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
   }
   despeckle_view=DestroyCacheView(despeckle_view);
   image_view=DestroyCacheView(image_view);
-  buffer=(Quantum *) RelinquishMagickMemory(buffer);
-  pixels=(Quantum *) RelinquishMagickMemory(pixels);
+  buffer_info=RelinquishVirtualMemory(buffer_info);
+  pixel_info=RelinquishVirtualMemory(pixel_info);
   despeckle_image->type=image->type;
   if (status == MagickFalse)
     despeckle_image=DestroyImage(despeckle_image);
@@ -1219,14 +1711,17 @@ MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
 MagickExport Image *EdgeImage(const Image *image,const double radius,
   ExceptionInfo *exception)
 {
-  char
-    geometry[MaxTextExtent];
+  Image
+    *edge_image;
 
   KernelInfo
     *kernel_info;
 
-  Image
-    *edge_image;
+  register ssize_t
+    i;
+
+  size_t
+    width;
 
   assert(image != (const Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -1234,11 +1729,29 @@ MagickExport Image *EdgeImage(const Image *image,const double radius,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  (void) FormatLocaleString(geometry,MaxTextExtent,"laplacian:%.20g",radius);
-  kernel_info=AcquireKernelInfo(geometry);
+  width=GetOptimalKernelWidth1D(radius,0.5);
+  kernel_info=AcquireKernelInfo((const char *) NULL);
   if (kernel_info == (KernelInfo *) NULL)
     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
-  edge_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
+  (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
+  kernel_info->width=width;
+  kernel_info->height=width;
+  kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
+  kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
+  kernel_info->signature=MagickSignature;
+  kernel_info->values=(MagickRealType *) MagickAssumeAligned(
+    AcquireAlignedMemory(kernel_info->width,kernel_info->height*
+    sizeof(*kernel_info->values)));
+  if (kernel_info->values == (MagickRealType *) NULL)
+    {
+      kernel_info=DestroyKernelInfo(kernel_info);
+      ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+    }
+  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
+    kernel_info->values[i]=(-1.0);
+  kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
+  edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
   kernel_info=DestroyKernelInfo(kernel_info);
   return(edge_image);
 }
@@ -1333,8 +1846,8 @@ MagickExport Image *EmbossImage(const Image *image,const double radius,
       kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
         8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
         (2.0*MagickPI*MagickSigma*MagickSigma));
-      if (u == k)
-        kernel_info->values[i]=v == k ? 1.0 : 0.0;
+      if (u != k)
+        kernel_info->values[i]=0.0;
       i++;
     }
     k--;
@@ -1345,8 +1858,8 @@ MagickExport Image *EmbossImage(const Image *image,const double radius,
   gamma=PerceptibleReciprocal(normalize);
   for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
     kernel_info->values[i]*=gamma;
-  emboss_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
-    exception);
+  emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
   kernel_info=DestroyKernelInfo(kernel_info);
   if (emboss_image != (Image *) NULL)
     (void) EqualizeImage(emboss_image,exception);
@@ -1409,7 +1922,8 @@ MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
   kernel_info=AcquireKernelInfo(geometry);
   if (kernel_info == (KernelInfo *) NULL)
     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
-  blur_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
+  blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
   kernel_info=DestroyKernelInfo(kernel_info);
   return(blur_image);
 }
@@ -1624,7 +2138,7 @@ MagickExport Image *MotionBlurImage(const Image *image,const double radius,
             (blur_traits == UndefinedPixelTrait))
           continue;
         if (((blur_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p) == 0))
+            (GetPixelReadMask(image,p) == 0))
           {
             SetPixelChannel(blur_image,channel,p[i],q);
             continue;
@@ -2230,19 +2744,19 @@ MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
 %                                                                             %
 %                                                                             %
 %                                                                             %
-%     R a d i a l B l u r I m a g e                                           %
+%     R o t a t i o n a l B l u r I m a g e                                   %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  RadialBlurImage() applies a radial blur to the image.
+%  RotationalBlurImage() applies a radial blur to the image.
 %
 %  Andrew Protano contributed this effect.
 %
-%  The format of the RadialBlurImage method is:
+%  The format of the RotationalBlurImage method is:
 %
-%    Image *RadialBlurImage(const Image *image,const double angle,
+%    Image *RotationalBlurImage(const Image *image,const double angle,
 %      ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
@@ -2256,7 +2770,7 @@ MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport Image *RadialBlurImage(const Image *image,const double angle,
+MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
   ExceptionInfo *exception)
 {
   CacheView
@@ -2417,7 +2931,7 @@ MagickExport Image *RadialBlurImage(const Image *image,const double angle,
             (blur_traits == UndefinedPixelTrait))
           continue;
         if (((blur_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p) == 0))
+            (GetPixelReadMask(image,p) == 0))
           {
             SetPixelChannel(blur_image,channel,p[i],q);
             continue;
@@ -2472,7 +2986,7 @@ MagickExport Image *RadialBlurImage(const Image *image,const double angle,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-        #pragma omp critical (MagickCore_RadialBlurImage)
+        #pragma omp critical (MagickCore_RotationalBlurImage)
 #endif
         proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -2726,7 +3240,7 @@ MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
             (blur_traits == UndefinedPixelTrait))
           continue;
         if (((blur_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p+center) == 0))
+            (GetPixelReadMask(image,p+center) == 0))
           {
             SetPixelChannel(blur_image,channel,p[center+i],q);
             continue;
@@ -3017,7 +3531,7 @@ MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
             (shade_traits == UndefinedPixelTrait))
           continue;
         if (((shade_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(linear_image,center) == 0))
+            (GetPixelReadMask(linear_image,center) == 0))
           {
             SetPixelChannel(shade_image,channel,center[i],q);
             continue;
@@ -3137,7 +3651,7 @@ MagickExport Image *SharpenImage(const Image *image,const double radius,
   kernel_info->y=(ssize_t) (width-1)/2;
   kernel_info->signature=MagickSignature;
   kernel_info->values=(MagickRealType *) MagickAssumeAligned(
-    AcquireAlignedMemory(kernel_info->width,kernel_info->width*
+    AcquireAlignedMemory(kernel_info->width,kernel_info->height*
     sizeof(*kernel_info->values)));
   if (kernel_info->values == (MagickRealType *) NULL)
     {
@@ -3164,7 +3678,8 @@ MagickExport Image *SharpenImage(const Image *image,const double radius,
   gamma=PerceptibleReciprocal(normalize);
   for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
     kernel_info->values[i]*=gamma;
-  sharp_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
+  sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+    UndefinedCompositeOp,0.0,exception);
   kernel_info=DestroyKernelInfo(kernel_info);
   return(sharp_image);
 }
@@ -3268,9 +3783,6 @@ MagickExport Image *SpreadImage(const Image *image,const double radius,
     const int
       id = GetOpenMPThreadId();
 
-    register const Quantum
-      *restrict p;
-
     register Quantum
       *restrict q;
 
@@ -3279,10 +3791,9 @@ MagickExport Image *SpreadImage(const Image *image,const double radius,
 
     if (status == MagickFalse)
       continue;
-    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
       exception);
-    if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
@@ -3450,7 +3961,7 @@ MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
             (unsharp_traits == UndefinedPixelTrait))
           continue;
         if (((unsharp_traits & CopyPixelTrait) != 0) ||
-            (GetPixelMask(image,p) != 0))
+            (GetPixelReadMask(image,p) != 0))
           {
             SetPixelChannel(unsharp_image,channel,p[i],q);
             continue;