#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"
/*
Edge detect the image brighness channel, level, blur, and level again.
*/
- edge_image=EdgeImage(image,radius,sigma,exception);
+ edge_image=EdgeImage(image,radius,exception);
if (edge_image == (Image *) NULL)
{
blur_image=DestroyImage(blur_image);
return((Image *) NULL);
}
(void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
- gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
+ gaussian_image=BlurImage(edge_image,radius,sigma,exception);
if (gaussian_image != (Image *) NULL)
{
edge_image=DestroyImage(edge_image);
(blur_traits == UndefinedPixelTrait))
continue;
if (((blur_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(image,p) != 0))
+ (GetPixelMask(image,p+center) == 0))
{
SetPixelChannel(blur_image,channel,p[center+i],q);
continue;
/*
Edge detect the image brighness channel, level, sharp, and level again.
*/
- edge_image=EdgeImage(image,radius,sigma,exception);
+ edge_image=EdgeImage(image,radius,exception);
if (edge_image == (Image *) NULL)
{
sharp_image=DestroyImage(sharp_image);
return((Image *) NULL);
}
(void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
- gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
+ gaussian_image=BlurImage(edge_image,radius,sigma,exception);
if (gaussian_image != (Image *) NULL)
{
edge_image=DestroyImage(edge_image);
(sharp_traits == UndefinedPixelTrait))
continue;
if (((sharp_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(image,p) != 0))
+ (GetPixelMask(image,p+center) == 0))
{
SetPixelChannel(sharp_image,channel,p[center+i],q);
continue;
MagickExport Image *BlurImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
{
- double
- normalize;
-
- Image
- *blur_image;
+ char
+ geometry[MaxTextExtent];
KernelInfo
*kernel_info;
- register ssize_t
- i;
-
- size_t
- width;
-
- ssize_t
- j,
- u,
- v;
+ Image
+ *blur_image;
assert(image != (const Image *) NULL);
assert(image->signature == MagickSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- width=GetOptimalKernelWidth2D(radius,sigma);
- kernel_info=AcquireKernelInfo((const char *) NULL);
+ (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");
- (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
- kernel_info->width=width;
- kernel_info->height=width;
- kernel_info->x=(ssize_t) width/2;
- kernel_info->y=(ssize_t) width/2;
- kernel_info->signature=MagickSignature;
- kernel_info->values=(MagickRealType *) MagickAssumeAligned(
- AcquireAlignedMemory(kernel_info->width,kernel_info->width*
- sizeof(*kernel_info->values)));
- if (kernel_info->values == (MagickRealType *) NULL)
- {
- kernel_info=DestroyKernelInfo(kernel_info);
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- }
- normalize=0.0;
- j=(ssize_t) kernel_info->width/2;
- i=0;
- for (v=(-j); v <= j; v++)
- {
- for (u=(-j); u <= j; u++)
- {
- kernel_info->values[i]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*
- MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
- normalize+=kernel_info->values[i];
- i++;
- }
- }
- kernel_info->values[i/2]+=(1.0-normalize);
- blur_image=ConvolveImage(image,kernel_info,exception);
+ blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+ UndefinedCompositeOp,0.0,exception);
kernel_info=DestroyKernelInfo(kernel_info);
return(blur_image);
}
MagickExport Image *ConvolveImage(const Image *image,
const KernelInfo *kernel_info,ExceptionInfo *exception)
{
- return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
+ Image
+ *convolve_image;
+
+ convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+ UndefinedCompositeOp,0.0,exception);
+ return(convolve_image);
}
\f
/*
% The format of the EdgeImage method is:
%
% Image *EdgeImage(const Image *image,const double radius,
-% const double sigma,ExceptionInfo *exception)
+% ExceptionInfo *exception)
%
% A description of each parameter follows:
%
%
% o radius: the radius of the pixel neighborhood.
%
-% o sigma: the standard deviation of the Gaussian, in pixels.
-%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *EdgeImage(const Image *image,const double radius,
- const double sigma,ExceptionInfo *exception)
+ ExceptionInfo *exception)
{
- Image
- *edge_image;
+ char
+ geometry[MaxTextExtent];
KernelInfo
*kernel_info;
- register ssize_t
- i;
-
- size_t
- width;
-
- ssize_t
- j,
- u,
- v;
+ Image
+ *edge_image;
assert(image != (const Image *) NULL);
assert(image->signature == MagickSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- width=GetOptimalKernelWidth1D(radius,sigma);
- kernel_info=AcquireKernelInfo((const char *) NULL);
+ (void) FormatLocaleString(geometry,MaxTextExtent,"laplacian:%.20g",radius);
+ kernel_info=AcquireKernelInfo(geometry);
if (kernel_info == (KernelInfo *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- kernel_info->width=width;
- kernel_info->height=width;
- kernel_info->x=(ssize_t) width/2;
- kernel_info->y=(ssize_t) width/2;
- kernel_info->values=(MagickRealType *) MagickAssumeAligned(
- AcquireAlignedMemory(kernel_info->width,kernel_info->width*
- sizeof(*kernel_info->values)));
- if (kernel_info->values == (MagickRealType *) NULL)
- {
- kernel_info=DestroyKernelInfo(kernel_info);
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- }
- j=(ssize_t) kernel_info->width/2;
- i=0;
- for (v=(-j); v <= j; v++)
- {
- for (u=(-j); u <= j; u++)
- {
- kernel_info->values[i]=(MagickRealType) (-1.0);
- i++;
- }
- }
- kernel_info->values[i/2]=(MagickRealType) (width*width-1.0);
- edge_image=ConvolveImage(image,kernel_info,exception);
+ edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+ UndefinedCompositeOp,0.0,exception);
kernel_info=DestroyKernelInfo(kernel_info);
return(edge_image);
}
MagickExport Image *EmbossImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
{
+ double
+ gamma,
+ normalize;
+
Image
*emboss_image;
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
kernel_info->width=width;
kernel_info->height=width;
- kernel_info->x=(ssize_t) width/2;
- kernel_info->y=(ssize_t) width/2;
+ kernel_info->x=(ssize_t) (width-1)/2;
+ kernel_info->y=(ssize_t) (width-1)/2;
kernel_info->values=(MagickRealType *) MagickAssumeAligned(
AcquireAlignedMemory(kernel_info->width,kernel_info->width*
sizeof(*kernel_info->values)));
kernel_info=DestroyKernelInfo(kernel_info);
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
}
- j=(ssize_t) kernel_info->width/2;
+ j=(ssize_t) (kernel_info->width-1)/2;
k=j;
i=0;
for (v=(-j); v <= j; v++)
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]=0.0;
+ if (u == k)
+ kernel_info->values[i]=v == k ? 1.0 : 0.0;
i++;
}
k--;
}
- emboss_image=ConvolveImage(image,kernel_info,exception);
+ normalize=0.0;
+ for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
+ normalize+=kernel_info->values[i];
+ gamma=PerceptibleReciprocal(normalize);
+ for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
+ kernel_info->values[i]*=gamma;
+ 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);
MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
{
- Image
- *blur_image;
+ char
+ geometry[MaxTextExtent];
KernelInfo
*kernel_info;
- register ssize_t
- i;
-
- size_t
- width;
-
- ssize_t
- j,
- u,
- v;
+ Image
+ *blur_image;
assert(image != (const Image *) NULL);
assert(image->signature == MagickSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- width=GetOptimalKernelWidth2D(radius,sigma);
- kernel_info=AcquireKernelInfo((const char *) NULL);
+ (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
+ radius,sigma);
+ kernel_info=AcquireKernelInfo(geometry);
if (kernel_info == (KernelInfo *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
- kernel_info->width=width;
- kernel_info->height=width;
- kernel_info->x=(ssize_t) width/2;
- kernel_info->y=(ssize_t) width/2;
- kernel_info->signature=MagickSignature;
- kernel_info->values=(MagickRealType *) MagickAssumeAligned(
- AcquireAlignedMemory(kernel_info->width,kernel_info->width*
- sizeof(*kernel_info->values)));
- if (kernel_info->values == (MagickRealType *) NULL)
- {
- kernel_info=DestroyKernelInfo(kernel_info);
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- }
- j=(ssize_t) kernel_info->width/2;
- i=0;
- for (v=(-j); v <= j; v++)
- {
- for (u=(-j); u <= j; u++)
- {
- kernel_info->values[i]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*
- MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
- i++;
- }
- }
- blur_image=ConvolveImage(image,kernel_info,exception);
+ blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+ UndefinedCompositeOp,0.0,exception);
kernel_info=DestroyKernelInfo(kernel_info);
return(blur_image);
}
(blur_traits == UndefinedPixelTrait))
continue;
if (((blur_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(image,p) != 0))
+ (GetPixelMask(image,p) == 0))
{
SetPixelChannel(blur_image,channel,p[i],q);
continue;
}
case EdgeDetectPreview:
{
- preview_image=EdgeImage(thumbnail,radius,sigma,exception);
+ preview_image=EdgeImage(thumbnail,radius,exception);
(void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
break;
}
break;
geometry.width=(size_t) (2*i+2);
geometry.height=(size_t) (2*i+2);
- geometry.x=i/2;
- geometry.y=i/2;
+ geometry.x=(i-1)/2;
+ geometry.y=(i-1)/2;
(void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
(void) FormatLocaleString(label,MaxTextExtent,
"raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
blur_image=DestroyImage(blur_image);
return((Image *) NULL);
}
- blur_center.x=(double) image->columns/2.0;
- blur_center.y=(double) image->rows/2.0;
+ blur_center.x=(double) (image->columns-1)/2.0;
+ blur_center.y=(double) (image->rows-1)/2.0;
blur_radius=hypot(blur_center.x,blur_center.y);
n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
theta=DegreesToRadians(angle)/(double) (n-1);
(blur_traits == UndefinedPixelTrait))
continue;
if (((blur_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(image,p) != 0))
+ (GetPixelMask(image,p) == 0))
{
SetPixelChannel(blur_image,channel,p[i],q);
continue;
width,width*sizeof(*kernel)));
if (kernel == (MagickRealType *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- j=(ssize_t) width/2;
+ j=(ssize_t) (width-1)/2;
i=0;
for (v=(-j); v <= j; v++)
{
*/
status=MagickTrue;
progress=0;
- center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
- GetPixelChannels(image)*(width/2L));
+ center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
+ ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
image_view=AcquireVirtualCacheView(image,exception);
luminance_view=AcquireVirtualCacheView(luminance_image,exception);
blur_view=AcquireAuthenticCacheView(blur_image,exception);
if (status == MagickFalse)
continue;
- p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
- (width/2L),image->columns+width,width,exception);
- l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) width/2L),y-(ssize_t)
- (width/2L),luminance_image->columns+width,width,exception);
+ p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
+ ((width-1)/2L),image->columns+width,width,exception);
+ l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
+ (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
(blur_traits == UndefinedPixelTrait))
continue;
if (((blur_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(image,p) != 0))
+ (GetPixelMask(image,p+center) == 0))
{
SetPixelChannel(blur_image,channel,p[center+i],q);
continue;
pixels+=GetPixelChannels(image);
luminance_pixels+=GetPixelChannels(luminance_image);
}
- pixels+=image->columns*GetPixelChannels(image);
+ pixels+=(image->columns-1)*GetPixelChannels(image);
luminance_pixels+=luminance_image->columns*
GetPixelChannels(luminance_image);
}
pixels+=GetPixelChannels(image);
luminance_pixels+=GetPixelChannels(luminance_image);
}
- pixels+=image->columns*GetPixelChannels(image);
+ pixels+=(image->columns-1)*GetPixelChannels(image);
luminance_pixels+=luminance_image->columns*
GetPixelChannels(luminance_image);
}
shade_image=DestroyImage(shade_image);
return((Image *) NULL);
}
- if (image->colorspace == sRGBColorspace)
- (void) TransformImageColorspace(linear_image,RGBColorspace,exception);
if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
{
linear_image=DestroyImage(linear_image);
(shade_traits == UndefinedPixelTrait))
continue;
if (((shade_traits & CopyPixelTrait) != 0) ||
- (GetPixelMask(linear_image,p) != 0))
+ (GetPixelMask(linear_image,center) == 0))
{
SetPixelChannel(shade_image,channel,center[i],q);
continue;
shade_view=DestroyCacheView(shade_view);
image_view=DestroyCacheView(image_view);
linear_image=DestroyImage(linear_image);
- if (image->colorspace == sRGBColorspace)
- (void) TransformImageColorspace(shade_image,sRGBColorspace,exception);
if (status == MagickFalse)
shade_image=DestroyImage(shade_image);
return(shade_image);
const double sigma,ExceptionInfo *exception)
{
double
+ gamma,
normalize;
Image
(void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
kernel_info->width=width;
kernel_info->height=width;
- kernel_info->x=(ssize_t) width/2;
- kernel_info->y=(ssize_t) width/2;
+ kernel_info->x=(ssize_t) (width-1)/2;
+ 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)
{
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
}
normalize=0.0;
- j=(ssize_t) kernel_info->width/2;
+ j=(ssize_t) (kernel_info->width-1)/2;
i=0;
for (v=(-j); v <= j; v++)
{
}
}
kernel_info->values[i/2]=(double) ((-2.0)*normalize);
- sharp_image=ConvolveImage(image,kernel_info,exception);
+ normalize=0.0;
+ for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
+ normalize+=kernel_info->values[i];
+ gamma=PerceptibleReciprocal(normalize);
+ for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
+ kernel_info->values[i]*=gamma;
+ sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
+ UndefinedCompositeOp,0.0,exception);
kernel_info=DestroyKernelInfo(kernel_info);
return(sharp_image);
}
%
% o sigma: the standard deviation of the Gaussian, in pixels.
%
-% o amount: the percentage of the difference between the original and the
+% o gain: the percentage of the difference between the original and the
% blur image that is added back into the original.
%
-% o threshold: the threshold in pixels needed to apply the diffence amount.
+% o threshold: the threshold in pixels needed to apply the diffence gain.
%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
- const double sigma,const double amount,const double threshold,
+ const double sigma,const double gain,const double threshold,
ExceptionInfo *exception)
{
#define SharpenImageTag "Sharpen/Image"
if (fabs(2.0*pixel) < quantum_threshold)
pixel=(double) p[i];
else
- pixel=(double) p[i]+amount*pixel;
+ pixel=(double) p[i]+gain*pixel;
SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
}
p+=GetPixelChannels(image);