% 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"
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++)
{
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)
{
(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;
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)
{
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
(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;
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);
}
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
/*
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);
MagickBooleanType
status;
+ MemoryInfo
+ *buffer_info,
+ *pixel_info;
+
Quantum
*restrict buffer,
*restrict pixels;
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.
*/
}
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);
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);
(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);
}
MagickExport Image *EmbossImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
{
+ double
+ gamma,
+ normalize;
+
Image
*emboss_image;
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--;
}
- emboss_image=MorphologyImage(image,ConvolveMorphology,1,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);
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);
}
(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;
% %
% %
% %
-% 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:
% 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
(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;
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)
(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;
(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;
MagickExport Image *SharpenImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
{
- char
- geometry[MaxTextExtent];
+ double
+ gamma,
+ normalize;
+
+ Image
+ *sharp_image;
KernelInfo
*kernel_info;
- Image
- *sharp_image;
+ register ssize_t
+ i;
+
+ size_t
+ width;
+
+ ssize_t
+ j,
+ u,
+ v;
assert(image != (const Image *) NULL);
assert(image->signature == MagickSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- (void) FormatLocaleString(geometry,MaxTextExtent,"LoG:%.20gx%.20g",
- radius,sigma);
- kernel_info=AcquireKernelInfo(geometry);
+ width=GetOptimalKernelWidth2D(radius,sigma);
+ kernel_info=AcquireKernelInfo((const char *) NULL);
if (kernel_info == (KernelInfo *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- ScaleGeometryKernelInfo(kernel_info,"56!,100%");
- sharp_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) (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->height*
+ 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-1)/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]=(double) ((-2.0)*normalize);
+ 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);
}
const int
id = GetOpenMPThreadId();
- register const Quantum
- *restrict p;
-
register Quantum
*restrict q;
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;
% The format of the UnsharpMaskImage method is:
%
% Image *UnsharpMaskImage(const Image *image,const double radius,
-% const double sigma,const double gain,ExceptionInfo *exception)
+% const double sigma,const double amount,const double threshold,
+% ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% 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 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 gain,ExceptionInfo *exception)
+ const double sigma,const double gain,const double threshold,
+ ExceptionInfo *exception)
{
- char
- geometry[MaxTextExtent];
+#define SharpenImageTag "Sharpen/Image"
- KernelInfo
- *kernel_info;
+ CacheView
+ *image_view,
+ *unsharp_view;
Image
*unsharp_image;
+ MagickBooleanType
+ status;
+
+ MagickOffsetType
+ progress;
+
+ double
+ quantum_threshold;
+
+ ssize_t
+ 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);
- (void) FormatLocaleString(geometry,MaxTextExtent,"Blur:%.20gx%.20g>",
- radius,sigma);
- kernel_info=AcquireKernelInfo(geometry);
- if (kernel_info == (KernelInfo *) NULL)
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- (void) FormatLocaleString(geometry,MaxTextExtent,"%.20g,%.20g%%",
- -100.0+gain*100.0,200.0-gain*100.0);
- ScaleGeometryKernelInfo(kernel_info,geometry);
- unsharp_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
- exception);
- kernel_info=DestroyKernelInfo(kernel_info);
+ unsharp_image=BlurImage(image,radius,sigma,exception);
+ if (unsharp_image == (Image *) NULL)
+ return((Image *) NULL);
+ quantum_threshold=(double) QuantumRange*threshold;
+ /*
+ Unsharp-mask image.
+ */
+ status=MagickTrue;
+ progress=0;
+ image_view=AcquireVirtualCacheView(image,exception);
+ unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+ #pragma omp parallel for schedule(static,4) shared(progress,status) \
+ magick_threads(image,unsharp_image,image->rows,1)
+#endif
+ for (y=0; y < (ssize_t) image->rows; y++)
+ {
+ register const Quantum
+ *restrict p;
+
+ register Quantum
+ *restrict q;
+
+ register ssize_t
+ x;
+
+ if (status == MagickFalse)
+ continue;
+ p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
+ q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
+ exception);
+ if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
+ {
+ status=MagickFalse;
+ continue;
+ }
+ for (x=0; x < (ssize_t) image->columns; x++)
+ {
+ register ssize_t
+ i;
+
+ for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+ {
+ double
+ pixel;
+
+ PixelChannel
+ channel;
+
+ PixelTrait
+ traits,
+ unsharp_traits;
+
+ channel=GetPixelChannelChannel(image,i);
+ traits=GetPixelChannelTraits(image,channel);
+ unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
+ if ((traits == UndefinedPixelTrait) ||
+ (unsharp_traits == UndefinedPixelTrait))
+ continue;
+ if (((unsharp_traits & CopyPixelTrait) != 0) ||
+ (GetPixelReadMask(image,p) != 0))
+ {
+ SetPixelChannel(unsharp_image,channel,p[i],q);
+ continue;
+ }
+ pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
+ if (fabs(2.0*pixel) < quantum_threshold)
+ pixel=(double) p[i];
+ else
+ pixel=(double) p[i]+gain*pixel;
+ SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
+ }
+ p+=GetPixelChannels(image);
+ q+=GetPixelChannels(unsharp_image);
+ }
+ if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
+ status=MagickFalse;
+ if (image->progress_monitor != (MagickProgressMonitor) NULL)
+ {
+ MagickBooleanType
+ proceed;
+
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+ #pragma omp critical (MagickCore_UnsharpMaskImage)
+#endif
+ proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
+ if (proceed == MagickFalse)
+ status=MagickFalse;
+ }
+ }
+ unsharp_image->type=image->type;
+ unsharp_view=DestroyCacheView(unsharp_view);
+ image_view=DestroyCacheView(image_view);
+ if (status == MagickFalse)
+ unsharp_image=DestroyImage(unsharp_image);
return(unsharp_image);
}