blob: 3edf892d7c6fdbd9493abc8519460fc22ac65412 [file] [log] [blame]
cristy701db312009-11-20 03:14:08 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M OOO RRRR PPPP H H OOO L OOO GGGG Y Y %
7% MM MM O O R R P P H H O O L O O G Y Y %
8% M M M O O RRRR PPPP HHHHH O O L O O G GGG Y %
9% M M O O R R P H H O O L O O G G Y %
10% M M OOO R R P H H OOO LLLLL OOO GGG Y %
11% %
12% %
13% MagickCore Morphology Methods %
14% %
15% Software Design %
16% Anthony Thyssen %
anthonyc94cdb02010-01-06 08:15:29 +000017% January 2010 %
cristy701db312009-11-20 03:14:08 +000018% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy701db312009-11-20 03:14:08 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
anthony602ab9b2010-01-05 08:06:50 +000036% Morpology is the the application of various kernals, of any size and even
37% shape, to a image in various ways (typically binary, but not always).
cristy701db312009-11-20 03:14:08 +000038%
anthony602ab9b2010-01-05 08:06:50 +000039% Convolution (weighted sum or average) is just one specific type of
40% morphology. Just one that is very common for image bluring and sharpening
41% effects. Not only 2D Gaussian blurring, but also 2-pass 1D Blurring.
42%
43% This module provides not only a general morphology function, and the ability
44% to apply more advanced or iterative morphologies, but also functions for the
45% generation of many different types of kernel arrays from user supplied
46% arguments. Prehaps even the generation of a kernel from a small image.
cristy701db312009-11-20 03:14:08 +000047*/
48
49/*
50 Include declarations.
51*/
52#include "magick/studio.h"
anthony602ab9b2010-01-05 08:06:50 +000053#include "magick/artifact.h"
cristy701db312009-11-20 03:14:08 +000054#include "magick/cache-view.h"
55#include "magick/color-private.h"
56#include "magick/enhance.h"
57#include "magick/exception.h"
58#include "magick/exception-private.h"
anthony602ab9b2010-01-05 08:06:50 +000059#include "magick/gem.h"
cristy701db312009-11-20 03:14:08 +000060#include "magick/hashmap.h"
61#include "magick/image.h"
cristybba804b2010-01-05 15:39:59 +000062#include "magick/image-private.h"
cristy701db312009-11-20 03:14:08 +000063#include "magick/list.h"
anthony29188a82010-01-22 10:12:34 +000064#include "magick/magick.h"
cristy701db312009-11-20 03:14:08 +000065#include "magick/memory_.h"
66#include "magick/monitor-private.h"
67#include "magick/morphology.h"
anthony602ab9b2010-01-05 08:06:50 +000068#include "magick/option.h"
cristy701db312009-11-20 03:14:08 +000069#include "magick/pixel-private.h"
70#include "magick/prepress.h"
71#include "magick/quantize.h"
72#include "magick/registry.h"
73#include "magick/semaphore.h"
74#include "magick/splay-tree.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
anthony602ab9b2010-01-05 08:06:50 +000077#include "magick/string-private.h"
78#include "magick/token.h"
cristya29d45f2010-03-05 21:14:54 +000079
anthony602ab9b2010-01-05 08:06:50 +000080/*
cristya29d45f2010-03-05 21:14:54 +000081 The following test is for special floating point numbers of value NaN (not
82 a number), that may be used within a Kernel Definition. NaN's are defined
83 as part of the IEEE standard for floating point number representation.
84
85 These are used a Kernel value of NaN means that that kernal position is not
86 part of the normal convolution or morphology process, and thus allowing the
87 use of 'shaped' kernels.
88
89 Special properities two NaN's are never equal, even if they are from the
90 same variable That is the IsNaN() macro is only true if the value is NaN.
91*/
anthony602ab9b2010-01-05 08:06:50 +000092#define IsNan(a) ((a)!=(a))
93
anthony29188a82010-01-22 10:12:34 +000094/*
cristya29d45f2010-03-05 21:14:54 +000095 Other global definitions used by module.
96*/
anthony29188a82010-01-22 10:12:34 +000097static inline double MagickMin(const double x,const double y)
98{
99 return( x < y ? x : y);
100}
101static inline double MagickMax(const double x,const double y)
102{
103 return( x > y ? x : y);
104}
105#define Minimize(assign,value) assign=MagickMin(assign,value)
106#define Maximize(assign,value) assign=MagickMax(assign,value)
107
anthonyc4c86e02010-01-27 09:30:32 +0000108/* Currently these are only internal to this module */
109static void
cristyef656912010-03-05 19:54:59 +0000110 RotateKernelInfo(KernelInfo *, double);
anthony602ab9b2010-01-05 08:06:50 +0000111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
anthony83ba99b2010-01-24 08:48:15 +0000117% A c q u i r e K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
cristy2be15382010-01-21 02:38:03 +0000123% AcquireKernelInfo() takes the given string (generally supplied by the
anthony602ab9b2010-01-05 08:06:50 +0000124% user) and converts it into a Morphology/Convolution Kernel. This allows
125% users to specify a kernel from a number of pre-defined kernels, or to fully
126% specify their own kernel for a specific Convolution or Morphology
127% Operation.
128%
129% The kernel so generated can be any rectangular array of floating point
130% values (doubles) with the 'control point' or 'pixel being affected'
131% anywhere within that array of values.
132%
anthony83ba99b2010-01-24 08:48:15 +0000133% Previously IM was restricted to a square of odd size using the exact
134% center as origin, this is no longer the case, and any rectangular kernel
135% with any value being declared the origin. This in turn allows the use of
136% highly asymmetrical kernels.
anthony602ab9b2010-01-05 08:06:50 +0000137%
138% The floating point values in the kernel can also include a special value
anthony83ba99b2010-01-24 08:48:15 +0000139% known as 'nan' or 'not a number' to indicate that this value is not part
140% of the kernel array. This allows you to shaped the kernel within its
141% rectangular area. That is 'nan' values provide a 'mask' for the kernel
142% shape. However at least one non-nan value must be provided for correct
143% working of a kernel.
anthony602ab9b2010-01-05 08:06:50 +0000144%
anthony83ba99b2010-01-24 08:48:15 +0000145% The returned kernel should be free using the DestroyKernelInfo() when you
146% are finished with it.
anthony602ab9b2010-01-05 08:06:50 +0000147%
148% Input kernel defintion strings can consist of any of three types.
149%
anthony29188a82010-01-22 10:12:34 +0000150% "name:args"
151% Select from one of the built in kernels, using the name and
152% geometry arguments supplied. See AcquireKernelBuiltIn()
anthony602ab9b2010-01-05 08:06:50 +0000153%
154% "WxH[+X+Y]:num, num, num ..."
155% a kernal of size W by H, with W*H floating point numbers following.
156% the 'center' can be optionally be defined at +X+Y (such that +0+0
anthony29188a82010-01-22 10:12:34 +0000157% is top left corner). If not defined the pixel in the center, for
158% odd sizes, or to the immediate top or left of center for even sizes
159% is automatically selected.
anthony602ab9b2010-01-05 08:06:50 +0000160%
anthony29188a82010-01-22 10:12:34 +0000161% "num, num, num, num, ..."
162% list of floating point numbers defining an 'old style' odd sized
163% square kernel. At least 9 values should be provided for a 3x3
164% square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
165% Values can be space or comma separated. This is not recommended.
anthony602ab9b2010-01-05 08:06:50 +0000166%
anthony83ba99b2010-01-24 08:48:15 +0000167% Note that 'name' kernels will start with an alphabetic character while the
168% new kernel specification has a ':' character in its specification string.
169% If neither is the case, it is assumed an old style of a simple list of
170% numbers generating a odd-sized square kernel has been given.
anthony602ab9b2010-01-05 08:06:50 +0000171%
172% The format of the AcquireKernal method is:
173%
cristy2be15382010-01-21 02:38:03 +0000174% KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000175%
176% A description of each parameter follows:
177%
178% o kernel_string: the Morphology/Convolution kernel wanted.
179%
180*/
181
cristy2be15382010-01-21 02:38:03 +0000182MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000183{
cristy2be15382010-01-21 02:38:03 +0000184 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000185 *kernel;
186
187 char
188 token[MaxTextExtent];
189
cristy150989e2010-02-01 14:59:39 +0000190 register long
anthony602ab9b2010-01-05 08:06:50 +0000191 i;
192
193 const char
194 *p;
195
196 MagickStatusType
197 flags;
198
199 GeometryInfo
200 args;
201
anthony29188a82010-01-22 10:12:34 +0000202 double
203 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
204
cristya2175d32010-04-03 02:25:17 +0000205 if (kernel_string == (const char *) NULL)
206 {
207 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
208 if (kernel == (KernelInfo *)NULL)
209 return(kernel);
210 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
211 kernel->type=UserDefinedKernel;
212 kernel->signature=MagickSignature;
213 return(kernel);
214 }
anthony602ab9b2010-01-05 08:06:50 +0000215 SetGeometryInfo(&args);
216
217 /* does it start with an alpha - Return a builtin kernel */
218 GetMagickToken(kernel_string,&p,token);
cristya2175d32010-04-03 02:25:17 +0000219 if (isalpha((int) ((unsigned char) *token)) != 0)
anthony602ab9b2010-01-05 08:06:50 +0000220 {
221 long
222 type;
223
anthony29188a82010-01-22 10:12:34 +0000224 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
anthony602ab9b2010-01-05 08:06:50 +0000225 if ( type < 0 || type == UserDefinedKernel )
cristy2be15382010-01-21 02:38:03 +0000226 return((KernelInfo *)NULL);
anthony602ab9b2010-01-05 08:06:50 +0000227
228 while (((isspace((int) ((unsigned char) *p)) != 0) ||
229 (*p == ',') || (*p == ':' )) && (*p != '\0'))
230 p++;
231 flags = ParseGeometry(p, &args);
232
233 /* special handling of missing values in input string */
anthony4fd27e22010-02-07 08:17:18 +0000234 switch( type ) {
235 case RectangleKernel:
anthony602ab9b2010-01-05 08:06:50 +0000236 if ( (flags & WidthValue) == 0 ) /* if no width then */
237 args.rho = args.sigma; /* then width = height */
238 if ( args.rho < 1.0 ) /* if width too small */
239 args.rho = 3; /* then width = 3 */
240 if ( args.sigma < 1.0 ) /* if height too small */
241 args.sigma = args.rho; /* then height = width */
242 if ( (flags & XValue) == 0 ) /* center offset if not defined */
243 args.xi = (double)(((long)args.rho-1)/2);
244 if ( (flags & YValue) == 0 )
245 args.psi = (double)(((long)args.sigma-1)/2);
anthony4fd27e22010-02-07 08:17:18 +0000246 break;
247 case SquareKernel:
248 case DiamondKernel:
249 case DiskKernel:
250 case PlusKernel:
251 if ( (flags & HeightValue) == 0 ) /* if no scale */
252 args.sigma = 1.0; /* then scale = 1.0 */
253 break;
254 default:
255 break;
anthony602ab9b2010-01-05 08:06:50 +0000256 }
257
cristy2be15382010-01-21 02:38:03 +0000258 return(AcquireKernelBuiltIn((KernelInfoType)type, &args));
anthony602ab9b2010-01-05 08:06:50 +0000259 }
260
cristy2be15382010-01-21 02:38:03 +0000261 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
262 if (kernel == (KernelInfo *)NULL)
anthony602ab9b2010-01-05 08:06:50 +0000263 return(kernel);
264 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
265 kernel->type = UserDefinedKernel;
cristyd43a46b2010-01-21 02:13:41 +0000266 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000267
268 /* Has a ':' in argument - New user kernel specification */
269 p = strchr(kernel_string, ':');
270 if ( p != (char *) NULL)
271 {
anthony602ab9b2010-01-05 08:06:50 +0000272 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
cristy150989e2010-02-01 14:59:39 +0000273 memcpy(token, kernel_string, (size_t) (p-kernel_string));
anthony602ab9b2010-01-05 08:06:50 +0000274 token[p-kernel_string] = '\0';
275 flags = ParseGeometry(token, &args);
anthony602ab9b2010-01-05 08:06:50 +0000276
anthony29188a82010-01-22 10:12:34 +0000277 /* Size handling and checks of geometry settings */
anthony602ab9b2010-01-05 08:06:50 +0000278 if ( (flags & WidthValue) == 0 ) /* if no width then */
279 args.rho = args.sigma; /* then width = height */
280 if ( args.rho < 1.0 ) /* if width too small */
281 args.rho = 1.0; /* then width = 1 */
282 if ( args.sigma < 1.0 ) /* if height too small */
283 args.sigma = args.rho; /* then height = width */
284 kernel->width = (unsigned long)args.rho;
285 kernel->height = (unsigned long)args.sigma;
286
287 /* Offset Handling and Checks */
288 if ( args.xi < 0.0 || args.psi < 0.0 )
anthony83ba99b2010-01-24 08:48:15 +0000289 return(DestroyKernelInfo(kernel));
cristyc99304f2010-02-01 15:26:27 +0000290 kernel->x = ((flags & XValue)!=0) ? (long)args.xi
cristy150989e2010-02-01 14:59:39 +0000291 : (long) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000292 kernel->y = ((flags & YValue)!=0) ? (long)args.psi
cristy150989e2010-02-01 14:59:39 +0000293 : (long) (kernel->height-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000294 if ( kernel->x >= (long) kernel->width ||
295 kernel->y >= (long) kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000296 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000297
298 p++; /* advance beyond the ':' */
299 }
300 else
301 { /* ELSE - Old old kernel specification, forming odd-square kernel */
302 /* count up number of values given */
303 p=(const char *) kernel_string;
cristya699b172010-01-06 16:48:49 +0000304 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
anthony29188a82010-01-22 10:12:34 +0000305 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000306 for (i=0; *p != '\0'; i++)
307 {
308 GetMagickToken(p,&p,token);
309 if (*token == ',')
310 GetMagickToken(p,&p,token);
311 }
312 /* set the size of the kernel - old sized square */
313 kernel->width = kernel->height= (unsigned long) sqrt((double) i+1.0);
cristyc99304f2010-02-01 15:26:27 +0000314 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000315 p=(const char *) kernel_string;
anthony29188a82010-01-22 10:12:34 +0000316 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
317 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000318 }
319
320 /* Read in the kernel values from rest of input string argument */
321 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
322 kernel->height*sizeof(double));
323 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000324 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000325
cristyc99304f2010-02-01 15:26:27 +0000326 kernel->minimum = +MagickHuge;
327 kernel->maximum = -MagickHuge;
328 kernel->negative_range = kernel->positive_range = 0.0;
cristy150989e2010-02-01 14:59:39 +0000329 for (i=0; (i < (long) (kernel->width*kernel->height)) && (*p != '\0'); i++)
anthony602ab9b2010-01-05 08:06:50 +0000330 {
331 GetMagickToken(p,&p,token);
332 if (*token == ',')
333 GetMagickToken(p,&p,token);
anthony29188a82010-01-22 10:12:34 +0000334 if ( LocaleCompare("nan",token) == 0
335 || LocaleCompare("-",token) == 0 ) {
336 kernel->values[i] = nan; /* do not include this value in kernel */
337 }
338 else {
339 kernel->values[i] = StringToDouble(token);
340 ( kernel->values[i] < 0)
cristyc99304f2010-02-01 15:26:27 +0000341 ? ( kernel->negative_range += kernel->values[i] )
342 : ( kernel->positive_range += kernel->values[i] );
343 Minimize(kernel->minimum, kernel->values[i]);
344 Maximize(kernel->maximum, kernel->values[i]);
anthony29188a82010-01-22 10:12:34 +0000345 }
anthony602ab9b2010-01-05 08:06:50 +0000346 }
anthonycc6c8362010-01-25 04:14:01 +0000347 /* check that we recieved at least one real (non-nan) value! */
cristyc99304f2010-02-01 15:26:27 +0000348 if ( kernel->minimum == MagickHuge )
anthonycc6c8362010-01-25 04:14:01 +0000349 return(DestroyKernelInfo(kernel));
anthony29188a82010-01-22 10:12:34 +0000350
anthonycc6c8362010-01-25 04:14:01 +0000351 /* This should not be needed for a fully defined kernel
anthony29188a82010-01-22 10:12:34 +0000352 * Perhaps an error should be reported instead!
anthonycc6c8362010-01-25 04:14:01 +0000353 * Kept for backward compatibility.
anthony29188a82010-01-22 10:12:34 +0000354 */
cristy150989e2010-02-01 14:59:39 +0000355 if ( i < (long) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000356 Minimize(kernel->minimum, kernel->values[i]);
357 Maximize(kernel->maximum, kernel->values[i]);
cristy150989e2010-02-01 14:59:39 +0000358 for ( ; i < (long) (kernel->width*kernel->height); i++)
anthony29188a82010-01-22 10:12:34 +0000359 kernel->values[i]=0.0;
360 }
anthony602ab9b2010-01-05 08:06:50 +0000361
362 return(kernel);
363}
364
365/*
366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367% %
368% %
369% %
370% A c q u i r e K e r n e l B u i l t I n %
371% %
372% %
373% %
374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375%
376% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
377% kernels used for special purposes such as gaussian blurring, skeleton
378% pruning, and edge distance determination.
379%
380% They take a KernelType, and a set of geometry style arguments, which were
381% typically decoded from a user supplied string, or from a more complex
382% Morphology Method that was requested.
383%
384% The format of the AcquireKernalBuiltIn method is:
385%
cristy2be15382010-01-21 02:38:03 +0000386% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000387% const GeometryInfo args)
388%
389% A description of each parameter follows:
390%
391% o type: the pre-defined type of kernel wanted
392%
393% o args: arguments defining or modifying the kernel
394%
395% Convolution Kernels
396%
anthony4fd27e22010-02-07 08:17:18 +0000397% Gaussian "{radius},{sigma}"
anthony602ab9b2010-01-05 08:06:50 +0000398% Generate a two-dimentional gaussian kernel, as used by -gaussian
399% A sigma is required, (with the 'x'), due to historical reasons.
400%
401% NOTE: that the 'radius' is optional, but if provided can limit (clip)
402% the final size of the resulting kernel to a square 2*radius+1 in size.
403% The radius should be at least 2 times that of the sigma value, or
404% sever clipping and aliasing may result. If not given or set to 0 the
405% radius will be determined so as to produce the best minimal error
406% result, which is usally much larger than is normally needed.
407%
anthony4fd27e22010-02-07 08:17:18 +0000408% Blur "{radius},{sigma},{angle}"
anthony602ab9b2010-01-05 08:06:50 +0000409% As per Gaussian, but generates a 1 dimensional or linear gaussian
410% blur, at the angle given (current restricted to orthogonal angles).
411% If a 'radius' is given the kernel is clipped to a width of 2*radius+1.
412%
413% NOTE that two such blurs perpendicular to each other is equivelent to
414% -blur and the previous gaussian, but is often 10 or more times faster.
415%
anthony4fd27e22010-02-07 08:17:18 +0000416% Comet "{width},{sigma},{angle}"
anthony602ab9b2010-01-05 08:06:50 +0000417% Blur in one direction only, mush like how a bright object leaves
418% a comet like trail. The Kernel is actually half a gaussian curve,
419% Adding two such blurs in oppiste directions produces a Linear Blur.
420%
421% NOTE: that the first argument is the width of the kernel and not the
422% radius of the kernel.
423%
424% # Still to be implemented...
425% #
anthony4fd27e22010-02-07 08:17:18 +0000426% # Sharpen "{radius},{sigma}
427% # Negated Gaussian (center zeroed and re-normalized),
428% # with a 2 unit positive peak. -- Check On line documentation
429% #
430% # Laplacian "{radius},{sigma}"
anthony602ab9b2010-01-05 08:06:50 +0000431% # Laplacian (a mexican hat like) Function
432% #
433% # LOG "{radius},{sigma1},{sigma2}
434% # Laplacian of Gaussian
435% #
436% # DOG "{radius},{sigma1},{sigma2}
anthony4fd27e22010-02-07 08:17:18 +0000437% # Difference of two Gaussians
438% #
439% # Filter2D
440% # Filter1D
441% # Set kernel values using a resize filter, and given scale (sigma)
442% # Cylindrical or Linear. Is this posible with an image?
443% #
anthony602ab9b2010-01-05 08:06:50 +0000444%
445% Boolean Kernels
446%
447% Rectangle "{geometry}"
448% Simply generate a rectangle of 1's with the size given. You can also
449% specify the location of the 'control point', otherwise the closest
450% pixel to the center of the rectangle is selected.
451%
452% Properly centered and odd sized rectangles work the best.
453%
anthony4fd27e22010-02-07 08:17:18 +0000454% Diamond "[{radius}[,{scale}]]"
anthony602ab9b2010-01-05 08:06:50 +0000455% Generate a diamond shaped kernal with given radius to the points.
456% Kernel size will again be radius*2+1 square and defaults to radius 1,
457% generating a 3x3 kernel that is slightly larger than a square.
458%
anthony4fd27e22010-02-07 08:17:18 +0000459% Square "[{radius}[,{scale}]]"
anthony602ab9b2010-01-05 08:06:50 +0000460% Generate a square shaped kernel of size radius*2+1, and defaulting
461% to a 3x3 (radius 1).
462%
463% Note that using a larger radius for the "Square" or the "Diamond"
464% is also equivelent to iterating the basic morphological method
465% that many times. However However iterating with the smaller radius 1
466% default is actually faster than using a larger kernel radius.
467%
anthony4fd27e22010-02-07 08:17:18 +0000468% Disk "[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000469% Generate a binary disk of the radius given, radius may be a float.
470% Kernel size will be ceil(radius)*2+1 square.
471% NOTE: Here are some disk shapes of specific interest
472% "disk:1" => "diamond" or "cross:1"
473% "disk:1.5" => "square"
474% "disk:2" => "diamond:2"
anthony83ba99b2010-01-24 08:48:15 +0000475% "disk:2.5" => a general disk shape of radius 2
anthony602ab9b2010-01-05 08:06:50 +0000476% "disk:2.9" => "square:2"
anthony83ba99b2010-01-24 08:48:15 +0000477% "disk:3.5" => default - octagonal/disk shape of radius 3
anthony602ab9b2010-01-05 08:06:50 +0000478% "disk:4.2" => roughly octagonal shape of radius 4
anthony83ba99b2010-01-24 08:48:15 +0000479% "disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000480% After this all the kernel shape becomes more and more circular.
481%
482% Because a "disk" is more circular when using a larger radius, using a
483% larger radius is preferred over iterating the morphological operation.
484%
anthony4fd27e22010-02-07 08:17:18 +0000485% Plus "[{radius}[,{scale}]]"
anthony602ab9b2010-01-05 08:06:50 +0000486% Generate a kernel in the shape of a 'plus' sign. The length of each
487% arm is also the radius, which defaults to 2.
488%
489% This kernel is not a good general morphological kernel, but is used
490% more for highlighting and marking any single pixels in an image using,
491% a "Dilate" or "Erode" method as appropriate.
anthonyc94cdb02010-01-06 08:15:29 +0000492%
anthony602ab9b2010-01-05 08:06:50 +0000493% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
494%
495% Note that unlike other kernels iterating a plus does not produce the
496% same result as using a larger radius for the cross.
497%
498% Distance Measuring Kernels
499%
500% Chebyshev "[{radius}][x{scale}]" largest x or y distance (default r=1)
501% Manhatten "[{radius}][x{scale}]" square grid distance (default r=1)
anthonyc94cdb02010-01-06 08:15:29 +0000502% Euclidean "[{radius}][x{scale}]" direct distance (default r=1)
anthony602ab9b2010-01-05 08:06:50 +0000503%
504% Different types of distance measuring methods, which are used with the
505% a 'Distance' morphology method for generating a gradient based on
506% distance from an edge of a binary shape, though there is a technique
507% for handling a anti-aliased shape.
508%
anthonyc94cdb02010-01-06 08:15:29 +0000509% Chebyshev Distance (also known as Tchebychev Distance) is a value of
510% one to any neighbour, orthogonal or diagonal. One why of thinking of
511% it is the number of squares a 'King' or 'Queen' in chess needs to
512% traverse reach any other position on a chess board. It results in a
513% 'square' like distance function, but one where diagonals are closer
514% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000515%
anthonyc94cdb02010-01-06 08:15:29 +0000516% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
517% Cab metric), is the distance needed when you can only travel in
518% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
519% in chess would travel. It results in a diamond like distances, where
520% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000521%
anthonyc94cdb02010-01-06 08:15:29 +0000522% Euclidean Distance is the 'direct' or 'as the crow flys distance.
523% However by default the kernel size only has a radius of 1, which
524% limits the distance to 'Knight' like moves, with only orthogonal and
525% diagonal measurements being correct. As such for the default kernel
526% you will get octagonal like distance function, which is reasonally
527% accurate.
528%
529% However if you use a larger radius such as "Euclidean:4" you will
530% get a much smoother distance gradient from the edge of the shape.
531% Of course a larger kernel is slower to use, and generally not needed.
532%
533% To allow the use of fractional distances that you get with diagonals
534% the actual distance is scaled by a fixed value which the user can
535% provide. This is not actually nessary for either ""Chebyshev" or
536% "Manhatten" distance kernels, but is done for all three distance
537% kernels. If no scale is provided it is set to a value of 100,
538% allowing for a maximum distance measurement of 655 pixels using a Q16
539% version of IM, from any edge. However for small images this can
540% result in quite a dark gradient.
541%
542% See the 'Distance' Morphological Method, for information of how it is
543% applied.
anthony602ab9b2010-01-05 08:06:50 +0000544%
anthony4fd27e22010-02-07 08:17:18 +0000545% # Hit-n-Miss Kernel-Lists -- Still to be implemented
546% #
547% # specifically for Pruning, Thinning, Thickening
548% #
anthony602ab9b2010-01-05 08:06:50 +0000549*/
550
cristy2be15382010-01-21 02:38:03 +0000551MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000552 const GeometryInfo *args)
553{
cristy2be15382010-01-21 02:38:03 +0000554 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000555 *kernel;
556
cristy150989e2010-02-01 14:59:39 +0000557 register long
anthony602ab9b2010-01-05 08:06:50 +0000558 i;
559
560 register long
561 u,
562 v;
563
564 double
565 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
566
cristy2be15382010-01-21 02:38:03 +0000567 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
568 if (kernel == (KernelInfo *) NULL)
anthony602ab9b2010-01-05 08:06:50 +0000569 return(kernel);
570 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
cristyc99304f2010-02-01 15:26:27 +0000571 kernel->minimum = kernel->maximum = 0.0;
572 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000573 kernel->type = type;
cristyd43a46b2010-01-21 02:13:41 +0000574 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000575
576 switch(type) {
577 /* Convolution Kernels */
578 case GaussianKernel:
579 { double
580 sigma = fabs(args->sigma);
581
582 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
583
584 kernel->width = kernel->height =
585 GetOptimalKernelWidth2D(args->rho,sigma);
cristyc99304f2010-02-01 15:26:27 +0000586 kernel->x = kernel->y = (long) (kernel->width-1)/2;
587 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000588 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
589 kernel->height*sizeof(double));
590 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000591 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000592
593 sigma = 2.0*sigma*sigma; /* simplify the expression */
cristyc99304f2010-02-01 15:26:27 +0000594 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
595 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
596 kernel->positive_range += (
anthony602ab9b2010-01-05 08:06:50 +0000597 kernel->values[i] =
598 exp(-((double)(u*u+v*v))/sigma)
599 /* / (MagickPI*sigma) */ );
cristyc99304f2010-02-01 15:26:27 +0000600 kernel->minimum = 0;
601 kernel->maximum = kernel->values[
602 kernel->y*kernel->width+kernel->x ];
anthony602ab9b2010-01-05 08:06:50 +0000603
anthony999bb2c2010-02-18 12:38:01 +0000604 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
anthony602ab9b2010-01-05 08:06:50 +0000605
606 break;
607 }
608 case BlurKernel:
609 { double
610 sigma = fabs(args->sigma);
611
612 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
613
614 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
cristyc99304f2010-02-01 15:26:27 +0000615 kernel->x = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000616 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +0000617 kernel->y = 0;
618 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000619 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
620 kernel->height*sizeof(double));
621 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000622 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000623
624#if 1
625#define KernelRank 3
626 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
627 ** It generates a gaussian 3 times the width, and compresses it into
628 ** the expected range. This produces a closer normalization of the
629 ** resulting kernel, especially for very low sigma values.
630 ** As such while wierd it is prefered.
631 **
632 ** I am told this method originally came from Photoshop.
633 */
634 sigma *= KernelRank; /* simplify expanded curve */
cristy150989e2010-02-01 14:59:39 +0000635 v = (long) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony602ab9b2010-01-05 08:06:50 +0000636 (void) ResetMagickMemory(kernel->values,0, (size_t)
637 kernel->width*sizeof(double));
638 for ( u=-v; u <= v; u++) {
639 kernel->values[(u+v)/KernelRank] +=
640 exp(-((double)(u*u))/(2.0*sigma*sigma))
641 /* / (MagickSQ2PI*sigma/KernelRank) */ ;
642 }
cristy150989e2010-02-01 14:59:39 +0000643 for (i=0; i < (long) kernel->width; i++)
cristyc99304f2010-02-01 15:26:27 +0000644 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +0000645#else
cristyc99304f2010-02-01 15:26:27 +0000646 for ( i=0, u=-kernel->x; i < kernel->width; i++, u++)
647 kernel->positive_range += (
anthony602ab9b2010-01-05 08:06:50 +0000648 kernel->values[i] =
649 exp(-((double)(u*u))/(2.0*sigma*sigma))
650 /* / (MagickSQ2PI*sigma) */ );
651#endif
cristyc99304f2010-02-01 15:26:27 +0000652 kernel->minimum = 0;
653 kernel->maximum = kernel->values[ kernel->x ];
anthonycc6c8362010-01-25 04:14:01 +0000654 /* Note that neither methods above generate a normalized kernel,
655 ** though it gets close. The kernel may be 'clipped' by a user defined
656 ** radius, producing a smaller (darker) kernel. Also for very small
657 ** sigma's (> 0.1) the central value becomes larger than one, and thus
658 ** producing a very bright kernel.
anthony602ab9b2010-01-05 08:06:50 +0000659 */
anthonycc6c8362010-01-25 04:14:01 +0000660
anthony602ab9b2010-01-05 08:06:50 +0000661 /* Normalize the 1D Gaussian Kernel
662 **
663 ** Because of this the divisor in the above kernel generator is
anthonyc94cdb02010-01-06 08:15:29 +0000664 ** not needed, so is not done above.
anthony602ab9b2010-01-05 08:06:50 +0000665 */
anthony999bb2c2010-02-18 12:38:01 +0000666 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
anthonycc6c8362010-01-25 04:14:01 +0000667
anthony602ab9b2010-01-05 08:06:50 +0000668 /* rotate the kernel by given angle */
anthony4fd27e22010-02-07 08:17:18 +0000669 RotateKernelInfo(kernel, args->xi);
anthony602ab9b2010-01-05 08:06:50 +0000670 break;
671 }
672 case CometKernel:
673 { double
674 sigma = fabs(args->sigma);
675
676 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
677
678 if ( args->rho < 1.0 )
679 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
680 else
681 kernel->width = (unsigned long)args->rho;
cristyc99304f2010-02-01 15:26:27 +0000682 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +0000683 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +0000684 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000685 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
686 kernel->height*sizeof(double));
687 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000688 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000689
690 /* A comet blur is half a gaussian curve, so that the object is
691 ** blurred in one direction only. This may not be quite the right
692 ** curve so may change in the future. The function must be normalised.
693 */
694#if 1
695#define KernelRank 3
696 sigma *= KernelRank; /* simplify expanded curve */
cristy150989e2010-02-01 14:59:39 +0000697 v = (long) kernel->width*KernelRank; /* start/end points to fit range */
anthony602ab9b2010-01-05 08:06:50 +0000698 (void) ResetMagickMemory(kernel->values,0, (size_t)
699 kernel->width*sizeof(double));
700 for ( u=0; u < v; u++) {
701 kernel->values[u/KernelRank] +=
702 exp(-((double)(u*u))/(2.0*sigma*sigma))
703 /* / (MagickSQ2PI*sigma/KernelRank) */ ;
704 }
cristy150989e2010-02-01 14:59:39 +0000705 for (i=0; i < (long) kernel->width; i++)
cristyc99304f2010-02-01 15:26:27 +0000706 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +0000707#else
cristy150989e2010-02-01 14:59:39 +0000708 for ( i=0; i < (long) kernel->width; i++)
cristyc99304f2010-02-01 15:26:27 +0000709 kernel->positive_range += (
anthony602ab9b2010-01-05 08:06:50 +0000710 kernel->values[i] =
711 exp(-((double)(i*i))/(2.0*sigma*sigma))
712 /* / (MagickSQ2PI*sigma) */ );
713#endif
cristyc99304f2010-02-01 15:26:27 +0000714 kernel->minimum = 0;
715 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +0000716
anthony999bb2c2010-02-18 12:38:01 +0000717 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
718 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +0000719 break;
720 }
721 /* Boolean Kernels */
722 case RectangleKernel:
723 case SquareKernel:
724 {
anthony4fd27e22010-02-07 08:17:18 +0000725 double scale;
anthony602ab9b2010-01-05 08:06:50 +0000726 if ( type == SquareKernel )
727 {
728 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000729 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +0000730 else
cristy150989e2010-02-01 14:59:39 +0000731 kernel->width = kernel->height = (unsigned long) (2*args->rho+1);
cristyc99304f2010-02-01 15:26:27 +0000732 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +0000733 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +0000734 }
735 else {
cristy2be15382010-01-21 02:38:03 +0000736 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +0000737 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +0000738 return(DestroyKernelInfo(kernel)); /* invalid args given */
anthony602ab9b2010-01-05 08:06:50 +0000739 kernel->width = (unsigned long)args->rho;
740 kernel->height = (unsigned long)args->sigma;
741 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
742 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000743 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristyc99304f2010-02-01 15:26:27 +0000744 kernel->x = (long) args->xi;
745 kernel->y = (long) args->psi;
anthony4fd27e22010-02-07 08:17:18 +0000746 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +0000747 }
748 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
749 kernel->height*sizeof(double));
750 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000751 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000752
anthonycc6c8362010-01-25 04:14:01 +0000753 /* set all kernel values to 1.0 */
cristy150989e2010-02-01 14:59:39 +0000754 u=(long) kernel->width*kernel->height;
755 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +0000756 kernel->values[i] = scale;
757 kernel->minimum = kernel->maximum = scale; /* a flat shape */
758 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +0000759 break;
anthony602ab9b2010-01-05 08:06:50 +0000760 }
761 case DiamondKernel:
762 {
763 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000764 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +0000765 else
766 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000767 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000768
769 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
770 kernel->height*sizeof(double));
771 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000772 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000773
anthony4fd27e22010-02-07 08:17:18 +0000774 /* set all kernel values within diamond area to scale given */
cristyc99304f2010-02-01 15:26:27 +0000775 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
776 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
777 if ((labs(u)+labs(v)) <= (long)kernel->x)
anthony4fd27e22010-02-07 08:17:18 +0000778 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +0000779 else
780 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +0000781 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +0000782 break;
783 }
784 case DiskKernel:
785 {
786 long
787 limit;
788
789 limit = (long)(args->rho*args->rho);
anthony83ba99b2010-01-24 08:48:15 +0000790 if (args->rho < 0.1) /* default radius approx 3.5 */
791 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +0000792 else
793 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000794 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000795
796 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
797 kernel->height*sizeof(double));
798 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000799 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000800
anthonycc6c8362010-01-25 04:14:01 +0000801 /* set all kernel values within disk area to 1.0 */
cristyc99304f2010-02-01 15:26:27 +0000802 for ( i=0, v= -kernel->y; v <= (long)kernel->y; v++)
803 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +0000804 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +0000805 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +0000806 else
807 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +0000808 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +0000809 break;
810 }
811 case PlusKernel:
812 {
813 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000814 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +0000815 else
816 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000817 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000818
819 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
820 kernel->height*sizeof(double));
821 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000822 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000823
anthonycc6c8362010-01-25 04:14:01 +0000824 /* set all kernel values along axises to 1.0 */
cristyc99304f2010-02-01 15:26:27 +0000825 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
826 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +0000827 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
828 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
829 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +0000830 break;
831 }
832 /* Distance Measuring Kernels */
833 case ChebyshevKernel:
834 {
835 double
836 scale;
837
838 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000839 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +0000840 else
841 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000842 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000843
844 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
845 kernel->height*sizeof(double));
846 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000847 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000848
849 scale = (args->sigma < 1.0) ? 100.0 : args->sigma;
cristyc99304f2010-02-01 15:26:27 +0000850 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
851 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
852 kernel->positive_range += ( kernel->values[i] =
anthony602ab9b2010-01-05 08:06:50 +0000853 scale*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +0000854 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +0000855 break;
856 }
857 case ManhattenKernel:
858 {
859 double
860 scale;
861
862 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000863 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +0000864 else
865 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000866 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000867
868 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
869 kernel->height*sizeof(double));
870 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000871 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000872
873 scale = (args->sigma < 1.0) ? 100.0 : args->sigma;
cristyc99304f2010-02-01 15:26:27 +0000874 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
875 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
876 kernel->positive_range += ( kernel->values[i] =
anthony602ab9b2010-01-05 08:06:50 +0000877 scale*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +0000878 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +0000879 break;
880 }
881 case EuclideanKernel:
882 {
883 double
884 scale;
885
886 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +0000887 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +0000888 else
889 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +0000890 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000891
892 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
893 kernel->height*sizeof(double));
894 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000895 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000896
897 scale = (args->sigma < 1.0) ? 100.0 : args->sigma;
cristyc99304f2010-02-01 15:26:27 +0000898 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
899 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
900 kernel->positive_range += ( kernel->values[i] =
anthony602ab9b2010-01-05 08:06:50 +0000901 scale*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +0000902 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +0000903 break;
904 }
905 /* Undefined Kernels */
906 case LaplacianKernel:
907 case LOGKernel:
908 case DOGKernel:
cristy150989e2010-02-01 14:59:39 +0000909 perror("Kernel Type has not been defined yet");
anthony602ab9b2010-01-05 08:06:50 +0000910 /* FALL THRU */
911 default:
912 /* Generate a No-Op minimal kernel - 1x1 pixel */
913 kernel->values=(double *)AcquireQuantumMemory((size_t)1,sizeof(double));
914 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000915 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000916 kernel->width = kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +0000917 kernel->x = kernel->x = 0;
anthony602ab9b2010-01-05 08:06:50 +0000918 kernel->type = UndefinedKernel;
cristyc99304f2010-02-01 15:26:27 +0000919 kernel->maximum =
920 kernel->positive_range =
anthonyc94cdb02010-01-06 08:15:29 +0000921 kernel->values[0] = 1.0; /* a flat single-point no-op kernel! */
anthony602ab9b2010-01-05 08:06:50 +0000922 break;
923 }
924
925 return(kernel);
926}
anthonyc94cdb02010-01-06 08:15:29 +0000927
anthony602ab9b2010-01-05 08:06:50 +0000928/*
929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930% %
931% %
932% %
cristy6771f1e2010-03-05 19:43:39 +0000933% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +0000934% %
935% %
936% %
937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938%
939% CloneKernelInfo() creates a new clone of the given Kernel so that its can
940% be modified without effecting the original. The cloned kernel should be
941% destroyed using DestoryKernelInfo() when no longer needed.
942%
cristye6365592010-04-02 17:31:23 +0000943% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +0000944%
anthony930be612010-02-08 04:26:15 +0000945% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +0000946%
947% A description of each parameter follows:
948%
949% o kernel: the Morphology/Convolution kernel to be cloned
950%
951*/
cristyef656912010-03-05 19:54:59 +0000952MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +0000953{
954 register long
955 i;
956
957 KernelInfo *
958 new;
959
960 assert(kernel != (KernelInfo *) NULL);
961
962 new=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
963 if (new == (KernelInfo *) NULL)
964 return(new);
965 *new = *kernel; /* copy values in structure */
966
967 new->values=(double *) AcquireQuantumMemory(kernel->width,
968 kernel->height*sizeof(double));
969 if (new->values == (double *) NULL)
970 return(DestroyKernelInfo(new));
971
972 for (i=0; i < (long) (kernel->width*kernel->height); i++)
973 new->values[i] = kernel->values[i];
974
975 return(new);
976}
977
978/*
979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980% %
981% %
982% %
anthony83ba99b2010-01-24 08:48:15 +0000983% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000984% %
985% %
986% %
987%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988%
anthony83ba99b2010-01-24 08:48:15 +0000989% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
990% kernel.
anthony602ab9b2010-01-05 08:06:50 +0000991%
anthony83ba99b2010-01-24 08:48:15 +0000992% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +0000993%
anthony83ba99b2010-01-24 08:48:15 +0000994% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +0000995%
996% A description of each parameter follows:
997%
998% o kernel: the Morphology/Convolution kernel to be destroyed
999%
1000*/
1001
anthony83ba99b2010-01-24 08:48:15 +00001002MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001003{
cristy2be15382010-01-21 02:38:03 +00001004 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001005
1006 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1007 kernel->height*sizeof(double));
anthony602ab9b2010-01-05 08:06:50 +00001008 kernel->values=(double *)RelinquishMagickMemory(kernel->values);
cristy2be15382010-01-21 02:38:03 +00001009 kernel=(KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001010 return(kernel);
1011}
anthonyc94cdb02010-01-06 08:15:29 +00001012
1013/*
1014%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015% %
1016% %
1017% %
anthony29188a82010-01-22 10:12:34 +00001018% M o r p h o l o g y I m a g e C h a n n e l %
anthony602ab9b2010-01-05 08:06:50 +00001019% %
1020% %
1021% %
1022%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023%
anthony29188a82010-01-22 10:12:34 +00001024% MorphologyImageChannel() applies a user supplied kernel to the image
1025% according to the given mophology method.
anthony602ab9b2010-01-05 08:06:50 +00001026%
1027% The given kernel is assumed to have been pre-scaled appropriatally, usally
1028% by the kernel generator.
1029%
1030% The format of the MorphologyImage method is:
1031%
cristyef656912010-03-05 19:54:59 +00001032% Image *MorphologyImage(const Image *image,MorphologyMethod method,
1033% const long iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony29188a82010-01-22 10:12:34 +00001034% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristyef656912010-03-05 19:54:59 +00001035% channel,MorphologyMethod method,const long iterations,
1036% KernelInfo *kernel,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00001037%
1038% A description of each parameter follows:
1039%
1040% o image: the image.
1041%
1042% o method: the morphology method to be applied.
1043%
1044% o iterations: apply the operation this many times (or no change).
1045% A value of -1 means loop until no change found.
1046% How this is applied may depend on the morphology method.
1047% Typically this is a value of 1.
1048%
1049% o channel: the channel type.
1050%
1051% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00001052% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00001053%
1054% o exception: return any errors or warnings in this structure.
1055%
1056%
1057% TODO: bias and auto-scale handling of the kernel for convolution
1058% The given kernel is assumed to have been pre-scaled appropriatally, usally
1059% by the kernel generator.
1060%
1061*/
1062
anthony930be612010-02-08 04:26:15 +00001063
anthony602ab9b2010-01-05 08:06:50 +00001064/* Internal function
anthony930be612010-02-08 04:26:15 +00001065 * Apply the Low-Level Morphology Method using the given Kernel
1066 * Returning the number of pixels that changed.
1067 * Two pre-created images must be provided, no image is created.
anthony602ab9b2010-01-05 08:06:50 +00001068 */
1069static unsigned long MorphologyApply(const Image *image, Image
1070 *result_image, const MorphologyMethod method, const ChannelType channel,
cristy2be15382010-01-21 02:38:03 +00001071 const KernelInfo *kernel, ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00001072{
cristy2be15382010-01-21 02:38:03 +00001073#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00001074
1075 long
cristy150989e2010-02-01 14:59:39 +00001076 progress,
anthony29188a82010-01-22 10:12:34 +00001077 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00001078 changed;
1079
1080 MagickBooleanType
1081 status;
1082
1083 MagickPixelPacket
1084 bias;
1085
1086 CacheView
1087 *p_view,
1088 *q_view;
1089
anthony4fd27e22010-02-07 08:17:18 +00001090 /* Only the most basic morphology is actually performed by this routine */
anthony4fd27e22010-02-07 08:17:18 +00001091
anthony602ab9b2010-01-05 08:06:50 +00001092 /*
anthony4fd27e22010-02-07 08:17:18 +00001093 Apply Basic Morphology to Image.
anthony602ab9b2010-01-05 08:06:50 +00001094 */
1095 status=MagickTrue;
1096 changed=0;
1097 progress=0;
1098
1099 GetMagickPixelPacket(image,&bias);
1100 SetMagickPixelPacketBias(image,&bias);
anthonycc6c8362010-01-25 04:14:01 +00001101 /* Future: handle auto-bias from user, based on kernel input */
anthony602ab9b2010-01-05 08:06:50 +00001102
1103 p_view=AcquireCacheView(image);
1104 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00001105
anthonycc6c8362010-01-25 04:14:01 +00001106 /* Some methods (including convolve) needs use a reflected kernel.
1107 * Adjust 'origin' offsets for this reflected kernel.
anthony29188a82010-01-22 10:12:34 +00001108 */
cristyc99304f2010-02-01 15:26:27 +00001109 offx = kernel->x;
1110 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00001111 switch(method) {
1112 case ErodeMorphology:
1113 case ErodeIntensityMorphology:
anthony999bb2c2010-02-18 12:38:01 +00001114 /* kernel is user as is, without reflection */
anthony29188a82010-01-22 10:12:34 +00001115 break;
anthony930be612010-02-08 04:26:15 +00001116 case ConvolveMorphology:
1117 case DilateMorphology:
1118 case DilateIntensityMorphology:
1119 case DistanceMorphology:
anthony999bb2c2010-02-18 12:38:01 +00001120 /* kernel needs to used with reflection */
cristy150989e2010-02-01 14:59:39 +00001121 offx = (long) kernel->width-offx-1;
1122 offy = (long) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00001123 break;
anthony930be612010-02-08 04:26:15 +00001124 default:
1125 perror("Not a low level Morpholgy Method");
1126 break;
anthony29188a82010-01-22 10:12:34 +00001127 }
1128
anthony602ab9b2010-01-05 08:06:50 +00001129#if defined(MAGICKCORE_OPENMP_SUPPORT)
1130 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1131#endif
cristy150989e2010-02-01 14:59:39 +00001132 for (y=0; y < (long) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00001133 {
1134 MagickBooleanType
1135 sync;
1136
1137 register const PixelPacket
1138 *restrict p;
1139
1140 register const IndexPacket
1141 *restrict p_indexes;
1142
1143 register PixelPacket
1144 *restrict q;
1145
1146 register IndexPacket
1147 *restrict q_indexes;
1148
cristy150989e2010-02-01 14:59:39 +00001149 register long
anthony602ab9b2010-01-05 08:06:50 +00001150 x;
1151
anthony29188a82010-01-22 10:12:34 +00001152 unsigned long
anthony602ab9b2010-01-05 08:06:50 +00001153 r;
1154
1155 if (status == MagickFalse)
1156 continue;
anthony29188a82010-01-22 10:12:34 +00001157 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
1158 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00001159 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
1160 exception);
1161 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1162 {
1163 status=MagickFalse;
1164 continue;
1165 }
1166 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
1167 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00001168 r = (image->columns+kernel->width)*offy+offx; /* constant */
1169
cristy150989e2010-02-01 14:59:39 +00001170 for (x=0; x < (long) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00001171 {
cristy150989e2010-02-01 14:59:39 +00001172 long
anthony602ab9b2010-01-05 08:06:50 +00001173 v;
1174
cristy150989e2010-02-01 14:59:39 +00001175 register long
anthony602ab9b2010-01-05 08:06:50 +00001176 u;
1177
1178 register const double
1179 *restrict k;
1180
1181 register const PixelPacket
1182 *restrict k_pixels;
1183
1184 register const IndexPacket
1185 *restrict k_indexes;
1186
1187 MagickPixelPacket
1188 result;
1189
anthony29188a82010-01-22 10:12:34 +00001190 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00001191 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00001192 */
anthony602ab9b2010-01-05 08:06:50 +00001193 *q = p[r];
1194 if (image->colorspace == CMYKColorspace)
1195 q_indexes[x] = p_indexes[r];
1196
cristy5ee247a2010-02-12 15:42:34 +00001197 result.green=(MagickRealType) 0;
1198 result.blue=(MagickRealType) 0;
1199 result.opacity=(MagickRealType) 0;
1200 result.index=(MagickRealType) 0;
anthony602ab9b2010-01-05 08:06:50 +00001201 switch (method) {
1202 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00001203 /* Set the user defined bias of the weighted average output
1204 **
1205 ** FUTURE: provide some way for internal functions to disable
1206 ** user defined bias and scaling effects.
1207 */
anthony602ab9b2010-01-05 08:06:50 +00001208 result=bias;
anthony930be612010-02-08 04:26:15 +00001209 break;
anthony83ba99b2010-01-24 08:48:15 +00001210 case DilateMorphology:
anthony29188a82010-01-22 10:12:34 +00001211 result.red =
1212 result.green =
1213 result.blue =
1214 result.opacity =
1215 result.index = -MagickHuge;
1216 break;
1217 case ErodeMorphology:
1218 result.red =
1219 result.green =
1220 result.blue =
1221 result.opacity =
1222 result.index = +MagickHuge;
1223 break;
anthony4fd27e22010-02-07 08:17:18 +00001224 case DilateIntensityMorphology:
1225 case ErodeIntensityMorphology:
1226 result.red = 0.0; /* flag indicating first match found */
1227 break;
anthony602ab9b2010-01-05 08:06:50 +00001228 default:
anthony29188a82010-01-22 10:12:34 +00001229 /* Otherwise just start with the original pixel value */
cristy150989e2010-02-01 14:59:39 +00001230 result.red = (MagickRealType) p[r].red;
1231 result.green = (MagickRealType) p[r].green;
1232 result.blue = (MagickRealType) p[r].blue;
1233 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
anthony602ab9b2010-01-05 08:06:50 +00001234 if ( image->colorspace == CMYKColorspace)
cristy150989e2010-02-01 14:59:39 +00001235 result.index = (MagickRealType) p_indexes[r];
anthony602ab9b2010-01-05 08:06:50 +00001236 break;
1237 }
1238
1239 switch ( method ) {
1240 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00001241 /* Weighted Average of pixels using reflected kernel
1242 **
1243 ** NOTE for correct working of this operation for asymetrical
1244 ** kernels, the kernel needs to be applied in its reflected form.
1245 ** That is its values needs to be reversed.
1246 **
1247 ** Correlation is actually the same as this but without reflecting
1248 ** the kernel, and thus 'lower-level' that Convolution. However
1249 ** as Convolution is the more common method used, and it does not
1250 ** really cost us much in terms of processing to use a reflected
1251 ** kernel it is Convolution that is implemented.
1252 **
1253 ** Correlation will have its kernel reflected before calling
1254 ** this function to do a Convolve.
1255 **
1256 ** For more details of Correlation vs Convolution see
1257 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
1258 */
anthony602ab9b2010-01-05 08:06:50 +00001259 if (((channel & OpacityChannel) == 0) ||
1260 (image->matte == MagickFalse))
1261 {
anthony930be612010-02-08 04:26:15 +00001262 /* Convolution without transparency effects */
anthony29188a82010-01-22 10:12:34 +00001263 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00001264 k_pixels = p;
1265 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001266 for (v=0; v < (long) kernel->height; v++) {
1267 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00001268 if ( IsNan(*k) ) continue;
1269 result.red += (*k)*k_pixels[u].red;
1270 result.green += (*k)*k_pixels[u].green;
1271 result.blue += (*k)*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00001272 /* result.opacity += not involved here */
anthony602ab9b2010-01-05 08:06:50 +00001273 if ( image->colorspace == CMYKColorspace)
1274 result.index += (*k)*k_indexes[u];
1275 }
1276 k_pixels += image->columns+kernel->width;
1277 k_indexes += image->columns+kernel->width;
1278 }
anthony602ab9b2010-01-05 08:06:50 +00001279 }
1280 else
1281 { /* Kernel & Alpha weighted Convolution */
1282 MagickRealType
1283 alpha, /* alpha value * kernel weighting */
1284 gamma; /* weighting divisor */
1285
1286 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00001287 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00001288 k_pixels = p;
1289 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001290 for (v=0; v < (long) kernel->height; v++) {
1291 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00001292 if ( IsNan(*k) ) continue;
1293 alpha=(*k)*(QuantumScale*(QuantumRange-
1294 k_pixels[u].opacity));
1295 gamma += alpha;
1296 result.red += alpha*k_pixels[u].red;
1297 result.green += alpha*k_pixels[u].green;
1298 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00001299 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00001300 if ( image->colorspace == CMYKColorspace)
1301 result.index += alpha*k_indexes[u];
1302 }
1303 k_pixels += image->columns+kernel->width;
1304 k_indexes += image->columns+kernel->width;
1305 }
1306 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00001307 result.red *= gamma;
1308 result.green *= gamma;
1309 result.blue *= gamma;
1310 result.opacity *= gamma;
1311 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00001312 }
1313 break;
1314
anthony4fd27e22010-02-07 08:17:18 +00001315 case ErodeMorphology:
anthony930be612010-02-08 04:26:15 +00001316 /* Minimize Value within kernel neighbourhood
1317 **
1318 ** NOTE that the kernel is not reflected for this operation!
1319 **
1320 ** NOTE: in normal Greyscale Morphology, the kernel value should
1321 ** be added to the real value, this is currently not done, due to
1322 ** the nature of the boolean kernels being used.
1323 */
anthony4fd27e22010-02-07 08:17:18 +00001324 k = kernel->values;
1325 k_pixels = p;
1326 k_indexes = p_indexes;
1327 for (v=0; v < (long) kernel->height; v++) {
1328 for (u=0; u < (long) kernel->width; u++, k++) {
1329 if ( IsNan(*k) || (*k) < 0.5 ) continue;
1330 Minimize(result.red, (double) k_pixels[u].red);
1331 Minimize(result.green, (double) k_pixels[u].green);
1332 Minimize(result.blue, (double) k_pixels[u].blue);
1333 Minimize(result.opacity, QuantumRange-(double) k_pixels[u].opacity);
1334 if ( image->colorspace == CMYKColorspace)
1335 Minimize(result.index, (double) k_indexes[u]);
1336 }
1337 k_pixels += image->columns+kernel->width;
1338 k_indexes += image->columns+kernel->width;
1339 }
1340 break;
1341
anthony83ba99b2010-01-24 08:48:15 +00001342 case DilateMorphology:
anthony930be612010-02-08 04:26:15 +00001343 /* Maximize Value within kernel neighbourhood
1344 **
1345 ** NOTE for correct working of this operation for asymetrical
1346 ** kernels, the kernel needs to be applied in its reflected form.
1347 ** That is its values needs to be reversed.
1348 **
1349 ** NOTE: in normal Greyscale Morphology, the kernel value should
1350 ** be added to the real value, this is currently not done, due to
1351 ** the nature of the boolean kernels being used.
1352 **
1353 */
anthony29188a82010-01-22 10:12:34 +00001354 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00001355 k_pixels = p;
1356 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001357 for (v=0; v < (long) kernel->height; v++) {
1358 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00001359 if ( IsNan(*k) || (*k) < 0.5 ) continue;
cristy150989e2010-02-01 14:59:39 +00001360 Maximize(result.red, (double) k_pixels[u].red);
1361 Maximize(result.green, (double) k_pixels[u].green);
1362 Maximize(result.blue, (double) k_pixels[u].blue);
1363 Maximize(result.opacity, QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00001364 if ( image->colorspace == CMYKColorspace)
cristy150989e2010-02-01 14:59:39 +00001365 Maximize(result.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00001366 }
1367 k_pixels += image->columns+kernel->width;
1368 k_indexes += image->columns+kernel->width;
1369 }
anthony602ab9b2010-01-05 08:06:50 +00001370 break;
1371
anthony4fd27e22010-02-07 08:17:18 +00001372 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00001373 /* Select Pixel with Minimum Intensity within kernel neighbourhood
1374 **
1375 ** WARNING: the intensity test fails for CMYK and does not
1376 ** take into account the moderating effect of teh alpha channel
1377 ** on the intensity.
1378 **
1379 ** NOTE that the kernel is not reflected for this operation!
1380 */
anthony602ab9b2010-01-05 08:06:50 +00001381 k = kernel->values;
1382 k_pixels = p;
1383 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001384 for (v=0; v < (long) kernel->height; v++) {
1385 for (u=0; u < (long) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00001386 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00001387 if ( result.red == 0.0 ||
1388 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
1389 /* copy the whole pixel - no channel selection */
1390 *q = k_pixels[u];
1391 if ( result.red > 0.0 ) changed++;
1392 result.red = 1.0;
1393 }
anthony602ab9b2010-01-05 08:06:50 +00001394 }
1395 k_pixels += image->columns+kernel->width;
1396 k_indexes += image->columns+kernel->width;
1397 }
anthony602ab9b2010-01-05 08:06:50 +00001398 break;
1399
anthony83ba99b2010-01-24 08:48:15 +00001400 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00001401 /* Select Pixel with Maximum Intensity within kernel neighbourhood
1402 **
1403 ** WARNING: the intensity test fails for CMYK and does not
1404 ** take into account the moderating effect of teh alpha channel
1405 ** on the intensity.
1406 **
1407 ** NOTE for correct working of this operation for asymetrical
1408 ** kernels, the kernel needs to be applied in its reflected form.
1409 ** That is its values needs to be reversed.
1410 */
anthony29188a82010-01-22 10:12:34 +00001411 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00001412 k_pixels = p;
1413 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001414 for (v=0; v < (long) kernel->height; v++) {
1415 for (u=0; u < (long) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00001416 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
1417 if ( result.red == 0.0 ||
1418 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
1419 /* copy the whole pixel - no channel selection */
1420 *q = k_pixels[u];
1421 if ( result.red > 0.0 ) changed++;
1422 result.red = 1.0;
1423 }
anthony602ab9b2010-01-05 08:06:50 +00001424 }
1425 k_pixels += image->columns+kernel->width;
1426 k_indexes += image->columns+kernel->width;
1427 }
anthony602ab9b2010-01-05 08:06:50 +00001428 break;
1429
anthony602ab9b2010-01-05 08:06:50 +00001430 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00001431 /* Add kernel Value and select the minimum value found.
1432 ** The result is a iterative distance from edge of image shape.
1433 **
1434 ** All Distance Kernels are symetrical, but that may not always
1435 ** be the case. For example how about a distance from left edges?
1436 ** To work correctly with asymetrical kernels the reflected kernel
1437 ** needs to be applied.
1438 */
anthony602ab9b2010-01-05 08:06:50 +00001439#if 0
anthony930be612010-02-08 04:26:15 +00001440 /* No need to do distance morphology if original value is zero
1441 ** Unfortunatally I have not been able to get this right
1442 ** when channel selection also becomes involved. -- Arrgghhh
1443 */
1444 if ( ((channel & RedChannel) == 0 && p[r].red == 0)
1445 || ((channel & GreenChannel) == 0 && p[r].green == 0)
1446 || ((channel & BlueChannel) == 0 && p[r].blue == 0)
1447 || ((channel & OpacityChannel) == 0 && p[r].opacity == 0)
1448 || (( (channel & IndexChannel) == 0
1449 || image->colorspace != CMYKColorspace
1450 ) && p_indexes[x] ==0 )
1451 )
1452 break;
anthony602ab9b2010-01-05 08:06:50 +00001453#endif
anthony29188a82010-01-22 10:12:34 +00001454 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00001455 k_pixels = p;
1456 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00001457 for (v=0; v < (long) kernel->height; v++) {
1458 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00001459 if ( IsNan(*k) ) continue;
1460 Minimize(result.red, (*k)+k_pixels[u].red);
1461 Minimize(result.green, (*k)+k_pixels[u].green);
1462 Minimize(result.blue, (*k)+k_pixels[u].blue);
1463 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
1464 if ( image->colorspace == CMYKColorspace)
1465 Minimize(result.index, (*k)+k_indexes[u]);
1466 }
1467 k_pixels += image->columns+kernel->width;
1468 k_indexes += image->columns+kernel->width;
1469 }
anthony602ab9b2010-01-05 08:06:50 +00001470 break;
1471
1472 case UndefinedMorphology:
1473 default:
1474 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00001475 }
1476 switch ( method ) {
1477 case UndefinedMorphology:
1478 case DilateIntensityMorphology:
1479 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00001480 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00001481 default:
1482 /* Assign the results */
1483 if ((channel & RedChannel) != 0)
1484 q->red = ClampToQuantum(result.red);
1485 if ((channel & GreenChannel) != 0)
1486 q->green = ClampToQuantum(result.green);
1487 if ((channel & BlueChannel) != 0)
1488 q->blue = ClampToQuantum(result.blue);
1489 if ((channel & OpacityChannel) != 0
1490 && image->matte == MagickTrue )
1491 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
1492 if ((channel & IndexChannel) != 0
1493 && image->colorspace == CMYKColorspace)
1494 q_indexes[x] = ClampToQuantum(result.index);
1495 break;
1496 }
1497 if ( ( p[r].red != q->red )
1498 || ( p[r].green != q->green )
1499 || ( p[r].blue != q->blue )
1500 || ( p[r].opacity != q->opacity )
1501 || ( image->colorspace == CMYKColorspace &&
1502 p_indexes[r] != q_indexes[x] ) )
1503 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00001504 p++;
1505 q++;
anthony83ba99b2010-01-24 08:48:15 +00001506 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00001507 sync=SyncCacheViewAuthenticPixels(q_view,exception);
1508 if (sync == MagickFalse)
1509 status=MagickFalse;
1510 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1511 {
1512 MagickBooleanType
1513 proceed;
1514
1515#if defined(MAGICKCORE_OPENMP_SUPPORT)
1516 #pragma omp critical (MagickCore_MorphologyImage)
1517#endif
1518 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
1519 if (proceed == MagickFalse)
1520 status=MagickFalse;
1521 }
anthony83ba99b2010-01-24 08:48:15 +00001522 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00001523 result_image->type=image->type;
1524 q_view=DestroyCacheView(q_view);
1525 p_view=DestroyCacheView(p_view);
cristy150989e2010-02-01 14:59:39 +00001526 return(status ? (unsigned long) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00001527}
1528
anthony4fd27e22010-02-07 08:17:18 +00001529
1530MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
anthony930be612010-02-08 04:26:15 +00001531 method, const long iterations,const KernelInfo *kernel, ExceptionInfo
1532 *exception)
cristy2be15382010-01-21 02:38:03 +00001533{
1534 Image
1535 *morphology_image;
1536
1537 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
1538 iterations,kernel,exception);
1539 return(morphology_image);
1540}
1541
anthony4fd27e22010-02-07 08:17:18 +00001542
cristyef656912010-03-05 19:54:59 +00001543MagickExport Image *MorphologyImageChannel(const Image *image,
1544 const ChannelType channel,const MorphologyMethod method,
1545 const long iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00001546{
cristy150989e2010-02-01 14:59:39 +00001547 long
1548 count;
anthony602ab9b2010-01-05 08:06:50 +00001549
1550 Image
1551 *new_image,
anthony4fd27e22010-02-07 08:17:18 +00001552 *old_image,
1553 *grad_image;
anthony602ab9b2010-01-05 08:06:50 +00001554
anthonycc6c8362010-01-25 04:14:01 +00001555 const char
1556 *artifact;
1557
cristy150989e2010-02-01 14:59:39 +00001558 unsigned long
1559 changed,
1560 limit;
1561
anthony4fd27e22010-02-07 08:17:18 +00001562 KernelInfo
1563 *curr_kernel;
1564
1565 MorphologyMethod
1566 curr_method;
1567
anthony602ab9b2010-01-05 08:06:50 +00001568 assert(image != (Image *) NULL);
1569 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00001570 assert(kernel != (KernelInfo *) NULL);
1571 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00001572 assert(exception != (ExceptionInfo *) NULL);
1573 assert(exception->signature == MagickSignature);
1574
anthony602ab9b2010-01-05 08:06:50 +00001575 if ( iterations == 0 )
1576 return((Image *)NULL); /* null operation - nothing to do! */
1577
1578 /* kernel must be valid at this point
1579 * (except maybe for posible future morphology methods like "Prune"
1580 */
cristy2be15382010-01-21 02:38:03 +00001581 assert(kernel != (KernelInfo *)NULL);
anthony602ab9b2010-01-05 08:06:50 +00001582
anthony4fd27e22010-02-07 08:17:18 +00001583 count = 0; /* interation count */
1584 changed = 1; /* if compound method assume image was changed */
anthony930be612010-02-08 04:26:15 +00001585 curr_kernel = (KernelInfo *)kernel; /* allow kernel and method */
1586 curr_method = method; /* to be changed as nessary */
anthony4fd27e22010-02-07 08:17:18 +00001587
cristy150989e2010-02-01 14:59:39 +00001588 limit = (unsigned long) iterations;
anthony602ab9b2010-01-05 08:06:50 +00001589 if ( iterations < 0 )
1590 limit = image->columns > image->rows ? image->columns : image->rows;
1591
anthony4fd27e22010-02-07 08:17:18 +00001592 /* Third-level morphology methods */
cristy5ee247a2010-02-12 15:42:34 +00001593 grad_image=(Image *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00001594 switch( curr_method ) {
1595 case EdgeMorphology:
1596 grad_image = MorphologyImageChannel(image, channel,
1597 DilateMorphology, iterations, curr_kernel, exception);
1598 /* FALL-THRU */
1599 case EdgeInMorphology:
1600 curr_method = ErodeMorphology;
anthony602ab9b2010-01-05 08:06:50 +00001601 break;
anthony4fd27e22010-02-07 08:17:18 +00001602 case EdgeOutMorphology:
1603 curr_method = DilateMorphology;
anthony602ab9b2010-01-05 08:06:50 +00001604 break;
anthony4fd27e22010-02-07 08:17:18 +00001605 case TopHatMorphology:
1606 curr_method = OpenMorphology;
1607 break;
1608 case BottomHatMorphology:
1609 curr_method = CloseMorphology;
1610 break;
1611 default:
anthony930be612010-02-08 04:26:15 +00001612 break; /* not a third-level method */
anthony4fd27e22010-02-07 08:17:18 +00001613 }
1614
1615 /* Second-level morphology methods */
1616 switch( curr_method ) {
anthony930be612010-02-08 04:26:15 +00001617 case OpenMorphology:
1618 /* Open is a Erode then a Dilate without reflection */
anthony4fd27e22010-02-07 08:17:18 +00001619 new_image = MorphologyImageChannel(image, channel,
1620 ErodeMorphology, iterations, curr_kernel, exception);
anthony602ab9b2010-01-05 08:06:50 +00001621 if (new_image == (Image *) NULL)
1622 return((Image *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001623 curr_method = DilateMorphology;
1624 break;
anthony602ab9b2010-01-05 08:06:50 +00001625 case OpenIntensityMorphology:
anthony4fd27e22010-02-07 08:17:18 +00001626 new_image = MorphologyImageChannel(image, channel,
1627 ErodeIntensityMorphology, iterations, curr_kernel, exception);
anthony602ab9b2010-01-05 08:06:50 +00001628 if (new_image == (Image *) NULL)
1629 return((Image *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001630 curr_method = DilateIntensityMorphology;
1631 break;
anthony930be612010-02-08 04:26:15 +00001632
1633 case CloseMorphology:
1634 /* Close is a Dilate then Erode using reflected kernel */
1635 /* A reflected kernel is needed for a Close */
1636 if ( curr_kernel == kernel )
1637 curr_kernel = CloneKernelInfo(kernel);
1638 RotateKernelInfo(curr_kernel,180);
1639 new_image = MorphologyImageChannel(image, channel,
1640 DilateMorphology, iterations, curr_kernel, exception);
1641 if (new_image == (Image *) NULL)
1642 return((Image *) NULL);
1643 curr_method = ErodeMorphology;
1644 break;
anthony4fd27e22010-02-07 08:17:18 +00001645 case CloseIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00001646 /* A reflected kernel is needed for a Close */
1647 if ( curr_kernel == kernel )
1648 curr_kernel = CloneKernelInfo(kernel);
anthony4fd27e22010-02-07 08:17:18 +00001649 RotateKernelInfo(curr_kernel,180);
1650 new_image = MorphologyImageChannel(image, channel,
1651 DilateIntensityMorphology, iterations, curr_kernel, exception);
1652 if (new_image == (Image *) NULL)
1653 return((Image *) NULL);
1654 curr_method = ErodeIntensityMorphology;
anthony602ab9b2010-01-05 08:06:50 +00001655 break;
1656
anthony930be612010-02-08 04:26:15 +00001657 case CorrelateMorphology:
1658 /* A Correlation is actually a Convolution with a reflected kernel.
1659 ** However a Convolution is a weighted sum with a reflected kernel.
1660 ** It may seem stange to convert a Correlation into a Convolution
1661 ** as the Correleation is the simplier method, but Convolution is
1662 ** much more commonly used, and it makes sense to implement it directly
1663 ** so as to avoid the need to duplicate the kernel when it is not
1664 ** required (which is typically the default).
1665 */
1666 if ( curr_kernel == kernel )
1667 curr_kernel = CloneKernelInfo(kernel);
1668 RotateKernelInfo(curr_kernel,180);
1669 curr_method = ConvolveMorphology;
1670 /* FALL-THRU into Correlate (weigthed sum without reflection) */
1671
anthonyc94cdb02010-01-06 08:15:29 +00001672 case ConvolveMorphology:
anthony4fd27e22010-02-07 08:17:18 +00001673 /* Scale or Normalize kernel, according to user wishes
anthony930be612010-02-08 04:26:15 +00001674 ** before using it for the Convolve/Correlate method.
1675 **
1676 ** FUTURE: provide some way for internal functions to disable
1677 ** user bias and scaling effects.
anthonycc6c8362010-01-25 04:14:01 +00001678 */
1679 artifact = GetImageArtifact(image,"convolve:scale");
anthony4fd27e22010-02-07 08:17:18 +00001680 if ( artifact != (char *)NULL ) {
anthony999bb2c2010-02-18 12:38:01 +00001681 MagickStatusType
1682 flags;
1683 GeometryInfo
1684 args;
1685
anthony930be612010-02-08 04:26:15 +00001686 if ( curr_kernel == kernel )
1687 curr_kernel = CloneKernelInfo(kernel);
anthony999bb2c2010-02-18 12:38:01 +00001688
1689 args.rho = 1.0;
1690 flags = ParseGeometry(artifact, &args);
1691 ScaleKernelInfo(curr_kernel, args.rho, flags);
anthony4fd27e22010-02-07 08:17:18 +00001692 }
anthony930be612010-02-08 04:26:15 +00001693 /* FALL-THRU to do the first, and typically the only iteration */
anthony4fd27e22010-02-07 08:17:18 +00001694
anthony602ab9b2010-01-05 08:06:50 +00001695 default:
anthony930be612010-02-08 04:26:15 +00001696 /* Do a single iteration using the Low-Level Morphology method!
1697 ** This ensures a "new_image" has been generated, but allows us to skip
1698 ** the creation of 'old_image' if no more iterations are needed.
1699 **
1700 ** The "curr_method" should also be set to a low-level method that is
1701 ** understood by the MorphologyApply() internal function.
anthony602ab9b2010-01-05 08:06:50 +00001702 */
1703 new_image=CloneImage(image,0,0,MagickTrue,exception);
1704 if (new_image == (Image *) NULL)
1705 return((Image *) NULL);
1706 if (SetImageStorageClass(new_image,DirectClass) == MagickFalse)
1707 {
1708 InheritException(exception,&new_image->exception);
1709 new_image=DestroyImage(new_image);
1710 return((Image *) NULL);
1711 }
anthony4fd27e22010-02-07 08:17:18 +00001712 changed = MorphologyApply(image,new_image,curr_method,channel,curr_kernel,
anthony602ab9b2010-01-05 08:06:50 +00001713 exception);
1714 count++;
1715 if ( GetImageArtifact(image,"verbose") != (const char *) NULL )
cristy150989e2010-02-01 14:59:39 +00001716 fprintf(stderr, "Morphology %s:%ld => Changed %lu\n",
anthony4fd27e22010-02-07 08:17:18 +00001717 MagickOptionToMnemonic(MagickMorphologyOptions, curr_method),
anthony602ab9b2010-01-05 08:06:50 +00001718 count, changed);
anthony930be612010-02-08 04:26:15 +00001719 break;
anthony602ab9b2010-01-05 08:06:50 +00001720 }
1721
anthony930be612010-02-08 04:26:15 +00001722 /* At this point the "curr_method" should not only be set to a low-level
1723 ** method that is understood by the MorphologyApply() internal function,
1724 ** but "new_image" should now be defined, as the image to apply the
1725 ** "curr_method" to.
1726 */
1727
1728 /* Repeat the low-level morphology until count or no change reached */
cristy150989e2010-02-01 14:59:39 +00001729 if ( count < (long) limit && changed > 0 ) {
anthony602ab9b2010-01-05 08:06:50 +00001730 old_image = CloneImage(new_image,0,0,MagickTrue,exception);
1731 if (old_image == (Image *) NULL)
1732 return(DestroyImage(new_image));
1733 if (SetImageStorageClass(old_image,DirectClass) == MagickFalse)
1734 {
1735 InheritException(exception,&old_image->exception);
1736 old_image=DestroyImage(old_image);
1737 return(DestroyImage(new_image));
1738 }
cristy150989e2010-02-01 14:59:39 +00001739 while( count < (long) limit && changed != 0 )
anthony602ab9b2010-01-05 08:06:50 +00001740 {
1741 Image *tmp = old_image;
1742 old_image = new_image;
1743 new_image = tmp;
anthony4fd27e22010-02-07 08:17:18 +00001744 changed = MorphologyApply(old_image,new_image,curr_method,channel,
1745 curr_kernel, exception);
anthony602ab9b2010-01-05 08:06:50 +00001746 count++;
1747 if ( GetImageArtifact(image,"verbose") != (const char *) NULL )
cristy150989e2010-02-01 14:59:39 +00001748 fprintf(stderr, "Morphology %s:%ld => Changed %lu\n",
anthony4fd27e22010-02-07 08:17:18 +00001749 MagickOptionToMnemonic(MagickMorphologyOptions, curr_method),
anthony602ab9b2010-01-05 08:06:50 +00001750 count, changed);
1751 }
cristy150989e2010-02-01 14:59:39 +00001752 old_image=DestroyImage(old_image);
anthony602ab9b2010-01-05 08:06:50 +00001753 }
anthony930be612010-02-08 04:26:15 +00001754
1755 /* We are finished with kernel - destroy it if we made a clone */
anthony4fd27e22010-02-07 08:17:18 +00001756 if ( curr_kernel != kernel )
1757 curr_kernel=DestroyKernelInfo(curr_kernel);
1758
anthony930be612010-02-08 04:26:15 +00001759 /* Third-level Subtractive methods post-processing */
anthony4fd27e22010-02-07 08:17:18 +00001760 switch( method ) {
1761 case EdgeOutMorphology:
1762 case EdgeInMorphology:
1763 case TopHatMorphology:
1764 case BottomHatMorphology:
anthony930be612010-02-08 04:26:15 +00001765 /* Get Difference relative to the original image */
cristy04ffdba2010-02-18 14:34:47 +00001766 (void) CompositeImageChannel(new_image, channel, DifferenceCompositeOp,
anthony4fd27e22010-02-07 08:17:18 +00001767 image, 0, 0);
1768 break;
anthony930be612010-02-08 04:26:15 +00001769 case EdgeMorphology: /* subtract the Erode from a Dilate */
cristy04ffdba2010-02-18 14:34:47 +00001770 (void) CompositeImageChannel(new_image, channel, DifferenceCompositeOp,
anthony4fd27e22010-02-07 08:17:18 +00001771 grad_image, 0, 0);
1772 grad_image=DestroyImage(grad_image);
1773 break;
1774 default:
1775 break;
1776 }
anthony602ab9b2010-01-05 08:06:50 +00001777
1778 return(new_image);
1779}
anthony83ba99b2010-01-24 08:48:15 +00001780
1781/*
1782%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1783% %
1784% %
1785% %
anthony4fd27e22010-02-07 08:17:18 +00001786+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00001787% %
1788% %
1789% %
1790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791%
anthony4fd27e22010-02-07 08:17:18 +00001792% RotateKernelInfo() rotates the kernel by the angle given. Currently it is
anthony83ba99b2010-01-24 08:48:15 +00001793% restricted to 90 degree angles, but this may be improved in the future.
1794%
anthony4fd27e22010-02-07 08:17:18 +00001795% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00001796%
anthony4fd27e22010-02-07 08:17:18 +00001797% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00001798%
1799% A description of each parameter follows:
1800%
1801% o kernel: the Morphology/Convolution kernel
1802%
1803% o angle: angle to rotate in degrees
1804%
anthonyc4c86e02010-01-27 09:30:32 +00001805% This function is only internel to this module, as it is not finalized,
1806% especially with regard to non-orthogonal angles, and rotation of larger
1807% 2D kernels.
anthony83ba99b2010-01-24 08:48:15 +00001808*/
anthony4fd27e22010-02-07 08:17:18 +00001809static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00001810{
1811 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
1812 **
1813 ** TODO: expand beyond simple 90 degree rotates, flips and flops
1814 */
1815
1816 /* Modulus the angle */
1817 angle = fmod(angle, 360.0);
1818 if ( angle < 0 )
1819 angle += 360.0;
1820
1821 if ( 315.0 < angle || angle <= 45.0 )
1822 return; /* no change! - At least at this time */
1823
1824 switch (kernel->type) {
1825 /* These built-in kernels are cylindrical kernels, rotating is useless */
1826 case GaussianKernel:
1827 case LaplacianKernel:
1828 case LOGKernel:
1829 case DOGKernel:
1830 case DiskKernel:
1831 case ChebyshevKernel:
1832 case ManhattenKernel:
1833 case EuclideanKernel:
1834 return;
1835
1836 /* These may be rotatable at non-90 angles in the future */
1837 /* but simply rotating them in multiples of 90 degrees is useless */
1838 case SquareKernel:
1839 case DiamondKernel:
1840 case PlusKernel:
1841 return;
1842
1843 /* These only allows a +/-90 degree rotation (by transpose) */
1844 /* A 180 degree rotation is useless */
1845 case BlurKernel:
1846 case RectangleKernel:
1847 if ( 135.0 < angle && angle <= 225.0 )
1848 return;
1849 if ( 225.0 < angle && angle <= 315.0 )
1850 angle -= 180;
1851 break;
1852
1853 /* these are freely rotatable in 90 degree units */
1854 case CometKernel:
1855 case UndefinedKernel:
1856 case UserDefinedKernel:
1857 break;
1858 }
1859 if ( 135.0 < angle && angle <= 225.0 )
1860 {
1861 /* For a 180 degree rotation - also know as a reflection */
1862 /* This is actually a very very common operation! */
1863 /* Basically all that is needed is a reversal of the kernel data! */
1864 unsigned long
1865 i,j;
1866 register double
1867 *k,t;
1868
1869 k=kernel->values;
1870 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
1871 t=k[i], k[i]=k[j], k[j]=t;
1872
anthony930be612010-02-08 04:26:15 +00001873 kernel->x = (long) kernel->width - kernel->x - 1;
1874 kernel->y = (long) kernel->height - kernel->y - 1;
anthony83ba99b2010-01-24 08:48:15 +00001875 angle = fmod(angle+180.0, 360.0);
1876 }
1877 if ( 45.0 < angle && angle <= 135.0 )
1878 { /* Do a transpose and a flop, of the image, which results in a 90
1879 * degree rotation using two mirror operations.
1880 *
1881 * WARNING: this assumes the original image was a 1 dimentional image
1882 * but currently that is the only built-ins it is applied to.
1883 */
cristy150989e2010-02-01 14:59:39 +00001884 long
anthony83ba99b2010-01-24 08:48:15 +00001885 t;
cristy150989e2010-02-01 14:59:39 +00001886 t = (long) kernel->width;
anthony83ba99b2010-01-24 08:48:15 +00001887 kernel->width = kernel->height;
cristy150989e2010-02-01 14:59:39 +00001888 kernel->height = (unsigned long) t;
cristyc99304f2010-02-01 15:26:27 +00001889 t = kernel->x;
1890 kernel->x = kernel->y;
1891 kernel->y = t;
anthony83ba99b2010-01-24 08:48:15 +00001892 angle = fmod(450.0 - angle, 360.0);
1893 }
1894 /* At this point angle should be between -45 (315) and +45 degrees
1895 * In the future some form of non-orthogonal angled rotates could be
1896 * performed here, posibily with a linear kernel restriction.
1897 */
1898
1899#if 0
1900 Not currently in use!
1901 { /* Do a flop, this assumes kernel is horizontally symetrical.
1902 * Each row of the kernel needs to be reversed!
1903 */
1904 unsigned long
1905 y;
cristy150989e2010-02-01 14:59:39 +00001906 register long
anthony83ba99b2010-01-24 08:48:15 +00001907 x,r;
1908 register double
1909 *k,t;
1910
1911 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
1912 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
1913 t=k[x], k[x]=k[r], k[r]=t;
1914
cristyc99304f2010-02-01 15:26:27 +00001915 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00001916 angle = fmod(angle+180.0, 360.0);
1917 }
1918#endif
1919 return;
1920}
1921
1922/*
1923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924% %
1925% %
1926% %
cristy6771f1e2010-03-05 19:43:39 +00001927% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00001928% %
1929% %
1930% %
1931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932%
anthony999bb2c2010-02-18 12:38:01 +00001933% ScaleKernelInfo() scales the kernel by the given amount, with or without
1934% normalization of the sum of the kernel values.
anthonycc6c8362010-01-25 04:14:01 +00001935%
anthony999bb2c2010-02-18 12:38:01 +00001936% By default (no flags given) the values within the kernel is scaled
1937% according the given scaling factor.
anthonycc6c8362010-01-25 04:14:01 +00001938%
anthony999bb2c2010-02-18 12:38:01 +00001939% If any 'normalize_flags' are given the kernel will be normalized and then
1940% further scaled by the scaleing factor value given. A 'PercentValue' flag
1941% will cause the given scaling factor to be divided by one hundred percent.
1942%
1943% Kernel normalization ('normalize_flags' given) is designed to ensure that
1944% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
1945% morphology methods will fall into -1.0 to +1.0 range. Note however that
1946% for non-HDRI versions of IM this may cause images to have any negative
1947% results clipped, unless some 'clip' any negative output from 'Convolve'
1948% with the use of some kernels.
1949%
1950% More specifically. Kernels which only contain positive values (such as a
1951% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
1952% ensuring a 0.0 to +1.0 convolution output range for non-HDRI images.
1953%
1954% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
1955% the kernel will be scaled by the absolute of the sum of kernel values, so
1956% that it will generally fall within the +/- 1.0 range.
1957%
1958% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
1959% will be scaled by just the sum of the postive values, so that its output
1960% range will again fall into the +/- 1.0 range.
1961%
1962% For special kernels designed for locating shapes using 'Correlate', (often
1963% only containing +1 and -1 values, representing foreground/brackground
1964% matching) a special normalization method is provided to scale the positive
1965% values seperatally to those of the negative values, so the kernel will be
1966% forced to become a zero-sum kernel better suited to such searches.
1967%
1968% WARNING: Correct normalization of the kernal assumes that the '*_range'
1969% attributes within the kernel structure have been correctly set during the
1970% kernels creation.
1971%
1972% NOTE: The values used for 'normalize_flags' have been selected specifically
1973% to match the use of geometry options, so that '!' means NormalizeValue, '^'
1974% means CorrelateNormalizeValue, and '%' means PercentValue. All other
1975% GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00001976%
anthony4fd27e22010-02-07 08:17:18 +00001977% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00001978%
anthony999bb2c2010-02-18 12:38:01 +00001979% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
1980% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00001981%
1982% A description of each parameter follows:
1983%
1984% o kernel: the Morphology/Convolution kernel
1985%
anthony999bb2c2010-02-18 12:38:01 +00001986% o scaling_factor:
1987% multiply all values (after normalization) by this factor if not
1988% zero. If the kernel is normalized regardless of any flags.
1989%
1990% o normalize_flags:
1991% GeometryFlags defining normalization method to use.
1992% specifically: NormalizeValue, CorrelateNormalizeValue,
1993% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00001994%
anthonyc4c86e02010-01-27 09:30:32 +00001995% This function is internal to this module only at this time, but can be
1996% exported to other modules if needed.
anthonycc6c8362010-01-25 04:14:01 +00001997*/
cristy6771f1e2010-03-05 19:43:39 +00001998MagickExport void ScaleKernelInfo(KernelInfo *kernel,
1999 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00002000{
cristy150989e2010-02-01 14:59:39 +00002001 register long
anthonycc6c8362010-01-25 04:14:01 +00002002 i;
2003
anthony999bb2c2010-02-18 12:38:01 +00002004 register double
2005 pos_scale,
2006 neg_scale;
2007
2008 pos_scale = 1.0;
2009 if ( (normalize_flags&NormalizeValue) != 0 ) {
2010 /* normalize kernel appropriately */
2011 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
2012 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00002013 else
anthony999bb2c2010-02-18 12:38:01 +00002014 pos_scale = kernel->positive_range; /* special zero-summing kernel */
2015 }
2016 /* force kernel into being a normalized zero-summing kernel */
2017 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
2018 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
2019 ? kernel->positive_range : 1.0;
2020 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
2021 ? -kernel->negative_range : 1.0;
2022 }
2023 else
2024 neg_scale = pos_scale;
2025
2026 /* finialize scaling_factor for positive and negative components */
2027 pos_scale = scaling_factor/pos_scale;
2028 neg_scale = scaling_factor/neg_scale;
2029 if ( (normalize_flags&PercentValue) != 0 ) {
2030 pos_scale /= 100.0;
2031 neg_scale /= 100.0;
anthonycc6c8362010-01-25 04:14:01 +00002032 }
2033
cristy150989e2010-02-01 14:59:39 +00002034 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00002035 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00002036 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00002037
anthony999bb2c2010-02-18 12:38:01 +00002038 /* convolution output range */
2039 kernel->positive_range *= pos_scale;
2040 kernel->negative_range *= neg_scale;
2041 /* maximum and minimum values in kernel */
2042 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
2043 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
2044
2045 /* swap kernel settings if user scaling factor is negative */
2046 if ( scaling_factor < MagickEpsilon ) {
2047 double t;
2048 t = kernel->positive_range;
2049 kernel->positive_range = kernel->negative_range;
2050 kernel->negative_range = t;
2051 t = kernel->maximum;
2052 kernel->maximum = kernel->minimum;
2053 kernel->minimum = 1;
2054 }
anthonycc6c8362010-01-25 04:14:01 +00002055
2056 return;
2057}
2058
2059/*
2060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2061% %
2062% %
2063% %
anthony4fd27e22010-02-07 08:17:18 +00002064+ S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00002065% %
2066% %
2067% %
2068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069%
anthony4fd27e22010-02-07 08:17:18 +00002070% ShowKernelInfo() outputs the details of the given kernel defination to
2071% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00002072%
2073% The format of the ShowKernel method is:
2074%
anthony4fd27e22010-02-07 08:17:18 +00002075% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00002076%
2077% A description of each parameter follows:
2078%
2079% o kernel: the Morphology/Convolution kernel
2080%
anthonyc4c86e02010-01-27 09:30:32 +00002081% This function is internal to this module only at this time. That may change
2082% in the future.
anthony83ba99b2010-01-24 08:48:15 +00002083*/
anthony4fd27e22010-02-07 08:17:18 +00002084MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00002085{
cristy150989e2010-02-01 14:59:39 +00002086 long
anthony83ba99b2010-01-24 08:48:15 +00002087 i, u, v;
2088
2089 fprintf(stderr,
anthonycc6c8362010-01-25 04:14:01 +00002090 "Kernel \"%s\" of size %lux%lu%+ld%+ld with values from %.*lg to %.*lg\n",
anthony83ba99b2010-01-24 08:48:15 +00002091 MagickOptionToMnemonic(MagickKernelOptions, kernel->type),
2092 kernel->width, kernel->height,
cristyc99304f2010-02-01 15:26:27 +00002093 kernel->x, kernel->y,
2094 GetMagickPrecision(), kernel->minimum,
2095 GetMagickPrecision(), kernel->maximum);
anthonycc6c8362010-01-25 04:14:01 +00002096 fprintf(stderr, "Forming convolution output range from %.*lg to %.*lg%s\n",
cristyc99304f2010-02-01 15:26:27 +00002097 GetMagickPrecision(), kernel->negative_range,
2098 GetMagickPrecision(), kernel->positive_range,
anthonycc6c8362010-01-25 04:14:01 +00002099 /*kernel->normalized == MagickTrue ? " (normalized)" : */ "" );
cristy150989e2010-02-01 14:59:39 +00002100 for (i=v=0; v < (long) kernel->height; v++) {
anthony83ba99b2010-01-24 08:48:15 +00002101 fprintf(stderr,"%2ld:",v);
cristy150989e2010-02-01 14:59:39 +00002102 for (u=0; u < (long) kernel->width; u++, i++)
anthony83ba99b2010-01-24 08:48:15 +00002103 if ( IsNan(kernel->values[i]) )
2104 fprintf(stderr," %*s", GetMagickPrecision()+2, "nan");
2105 else
2106 fprintf(stderr," %*.*lg", GetMagickPrecision()+2,
2107 GetMagickPrecision(), kernel->values[i]);
2108 fprintf(stderr,"\n");
2109 }
2110}
anthonycc6c8362010-01-25 04:14:01 +00002111
2112/*
2113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2114% %
2115% %
2116% %
anthony4fd27e22010-02-07 08:17:18 +00002117+ Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00002118% %
2119% %
2120% %
2121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2122%
2123% ZeroKernelNans() replaces any special 'nan' value that may be present in
2124% the kernel with a zero value. This is typically done when the kernel will
2125% be used in special hardware (GPU) convolution processors, to simply
2126% matters.
2127%
2128% The format of the ZeroKernelNans method is:
2129%
2130% voidZeroKernelNans (KernelInfo *kernel)
2131%
2132% A description of each parameter follows:
2133%
2134% o kernel: the Morphology/Convolution kernel
2135%
2136% FUTURE: return the information in a string for API usage.
2137*/
anthonyc4c86e02010-01-27 09:30:32 +00002138MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00002139{
cristy150989e2010-02-01 14:59:39 +00002140 register long
anthonycc6c8362010-01-25 04:14:01 +00002141 i;
2142
cristy150989e2010-02-01 14:59:39 +00002143 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00002144 if ( IsNan(kernel->values[i]) )
2145 kernel->values[i] = 0.0;
2146
2147 return;
2148}