blob: 54d35aed47eb2d4396c99990735d6e035299fa0e [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%
anthony1b2bc0a2010-05-12 05:25:22 +000036% Morpology is the the application of various kernels, of any size and even
anthony602ab9b2010-01-05 08:06:50 +000037% 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"
anthony46a369d2010-05-19 02:41:48 +000068#include "magick/morphology-private.h"
anthony602ab9b2010-01-05 08:06:50 +000069#include "magick/option.h"
cristy701db312009-11-20 03:14:08 +000070#include "magick/pixel-private.h"
71#include "magick/prepress.h"
72#include "magick/quantize.h"
73#include "magick/registry.h"
74#include "magick/semaphore.h"
75#include "magick/splay-tree.h"
76#include "magick/statistic.h"
77#include "magick/string_.h"
anthony602ab9b2010-01-05 08:06:50 +000078#include "magick/string-private.h"
79#include "magick/token.h"
cristya29d45f2010-03-05 21:14:54 +000080
anthonyc3cd15b2010-05-27 06:05:40 +000081
anthony602ab9b2010-01-05 08:06:50 +000082/*
anthonyc3cd15b2010-05-27 06:05:40 +000083** The following test is for special floating point numbers of value NaN (not
84** a number), that may be used within a Kernel Definition. NaN's are defined
85** as part of the IEEE standard for floating point number representation.
86**
87** These are used as a Kernel value to mean that this kernel position is not
88** part of the kernel neighbourhood for convolution or morphology processing,
89** and thus should be ignored. This allows the use of 'shaped' kernels.
90**
91** The special properity that two NaN's are never equal, even if they are from
92** the same variable allow you to test if a value is special NaN value.
93**
94** This macro IsNaN() is thus is only true if the value given is NaN.
cristya29d45f2010-03-05 21:14:54 +000095*/
anthony602ab9b2010-01-05 08:06:50 +000096#define IsNan(a) ((a)!=(a))
97
anthony29188a82010-01-22 10:12:34 +000098/*
cristya29d45f2010-03-05 21:14:54 +000099 Other global definitions used by module.
100*/
anthony29188a82010-01-22 10:12:34 +0000101static inline double MagickMin(const double x,const double y)
102{
103 return( x < y ? x : y);
104}
105static inline double MagickMax(const double x,const double y)
106{
107 return( x > y ? x : y);
108}
109#define Minimize(assign,value) assign=MagickMin(assign,value)
110#define Maximize(assign,value) assign=MagickMax(assign,value)
111
anthonyc4c86e02010-01-27 09:30:32 +0000112/* Currently these are only internal to this module */
113static void
anthony46a369d2010-05-19 02:41:48 +0000114 CalcKernelMetaData(KernelInfo *),
cristyeb8db6d2010-05-24 18:34:11 +0000115 ExpandKernelInfo(KernelInfo *, const double),
cristyef656912010-03-05 19:54:59 +0000116 RotateKernelInfo(KernelInfo *, double);
anthony602ab9b2010-01-05 08:06:50 +0000117
anthony3dd0f622010-05-13 12:57:32 +0000118
119/* Quick function to find last kernel in a kernel list */
120static inline KernelInfo *LastKernelInfo(KernelInfo *kernel)
121{
122 while (kernel->next != (KernelInfo *) NULL)
123 kernel = kernel->next;
124 return(kernel);
125}
126
127
anthony602ab9b2010-01-05 08:06:50 +0000128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% %
131% %
132% %
anthony83ba99b2010-01-24 08:48:15 +0000133% A c q u i r e K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000134% %
135% %
136% %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
cristy2be15382010-01-21 02:38:03 +0000139% AcquireKernelInfo() takes the given string (generally supplied by the
anthony602ab9b2010-01-05 08:06:50 +0000140% user) and converts it into a Morphology/Convolution Kernel. This allows
141% users to specify a kernel from a number of pre-defined kernels, or to fully
142% specify their own kernel for a specific Convolution or Morphology
143% Operation.
144%
145% The kernel so generated can be any rectangular array of floating point
146% values (doubles) with the 'control point' or 'pixel being affected'
147% anywhere within that array of values.
148%
anthony83ba99b2010-01-24 08:48:15 +0000149% Previously IM was restricted to a square of odd size using the exact
150% center as origin, this is no longer the case, and any rectangular kernel
151% with any value being declared the origin. This in turn allows the use of
152% highly asymmetrical kernels.
anthony602ab9b2010-01-05 08:06:50 +0000153%
154% The floating point values in the kernel can also include a special value
anthony83ba99b2010-01-24 08:48:15 +0000155% known as 'nan' or 'not a number' to indicate that this value is not part
156% of the kernel array. This allows you to shaped the kernel within its
157% rectangular area. That is 'nan' values provide a 'mask' for the kernel
158% shape. However at least one non-nan value must be provided for correct
159% working of a kernel.
anthony602ab9b2010-01-05 08:06:50 +0000160%
anthony7a01dcf2010-05-11 12:25:52 +0000161% The returned kernel should be freed using the DestroyKernelInfo() when you
162% are finished with it. Do not free this memory yourself.
anthony602ab9b2010-01-05 08:06:50 +0000163%
164% Input kernel defintion strings can consist of any of three types.
165%
anthony29188a82010-01-22 10:12:34 +0000166% "name:args"
167% Select from one of the built in kernels, using the name and
168% geometry arguments supplied. See AcquireKernelBuiltIn()
anthony602ab9b2010-01-05 08:06:50 +0000169%
anthony43c49252010-05-18 10:59:50 +0000170% "WxH[+X+Y][^@]:num, num, num ..."
anthony1b2bc0a2010-05-12 05:25:22 +0000171% a kernel of size W by H, with W*H floating point numbers following.
anthony602ab9b2010-01-05 08:06:50 +0000172% the 'center' can be optionally be defined at +X+Y (such that +0+0
anthony29188a82010-01-22 10:12:34 +0000173% is top left corner). If not defined the pixel in the center, for
174% odd sizes, or to the immediate top or left of center for even sizes
175% is automatically selected.
anthony602ab9b2010-01-05 08:06:50 +0000176%
anthony43c49252010-05-18 10:59:50 +0000177% If a '^' is included the kernel expanded with 90-degree rotations,
178% While a '@' will allow you to expand a 3x3 kernel using 45-degree
179% circular rotates.
180%
anthony29188a82010-01-22 10:12:34 +0000181% "num, num, num, num, ..."
182% list of floating point numbers defining an 'old style' odd sized
183% square kernel. At least 9 values should be provided for a 3x3
184% square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
185% Values can be space or comma separated. This is not recommended.
anthony602ab9b2010-01-05 08:06:50 +0000186%
anthony7a01dcf2010-05-11 12:25:52 +0000187% You can define a 'list of kernels' which can be used by some morphology
188% operators A list is defined as a semi-colon seperated list kernels.
189%
anthonydbc89892010-05-12 07:05:27 +0000190% " kernel ; kernel ; kernel ; "
anthony7a01dcf2010-05-11 12:25:52 +0000191%
anthony1dd091a2010-05-27 06:31:15 +0000192% Any extra ';' characters, at start, end or between kernel defintions are
anthony43c49252010-05-18 10:59:50 +0000193% simply ignored.
194%
195% Note that 'name' kernels will start with an alphabetic character while the
196% new kernel specification has a ':' character in its specification string.
197% If neither is the case, it is assumed an old style of a simple list of
198% numbers generating a odd-sized square kernel has been given.
anthony7a01dcf2010-05-11 12:25:52 +0000199%
anthony602ab9b2010-01-05 08:06:50 +0000200% The format of the AcquireKernal method is:
201%
cristy2be15382010-01-21 02:38:03 +0000202% KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000203%
204% A description of each parameter follows:
205%
206% o kernel_string: the Morphology/Convolution kernel wanted.
207%
208*/
209
anthonyc84dce52010-05-07 05:42:23 +0000210/* This was separated so that it could be used as a separate
anthony5ef8e942010-05-11 06:51:12 +0000211** array input handling function, such as for -color-matrix
anthonyc84dce52010-05-07 05:42:23 +0000212*/
anthony5ef8e942010-05-11 06:51:12 +0000213static KernelInfo *ParseKernelArray(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000214{
cristy2be15382010-01-21 02:38:03 +0000215 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000216 *kernel;
217
218 char
219 token[MaxTextExtent];
220
anthony602ab9b2010-01-05 08:06:50 +0000221 const char
anthony5ef8e942010-05-11 06:51:12 +0000222 *p,
223 *end;
anthony602ab9b2010-01-05 08:06:50 +0000224
anthonyc84dce52010-05-07 05:42:23 +0000225 register long
226 i;
anthony602ab9b2010-01-05 08:06:50 +0000227
anthony29188a82010-01-22 10:12:34 +0000228 double
229 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
230
anthony43c49252010-05-18 10:59:50 +0000231 MagickStatusType
232 flags;
233
234 GeometryInfo
235 args;
236
cristy2be15382010-01-21 02:38:03 +0000237 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
238 if (kernel == (KernelInfo *)NULL)
anthony602ab9b2010-01-05 08:06:50 +0000239 return(kernel);
240 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000241 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthony7a01dcf2010-05-11 12:25:52 +0000242 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000243 kernel->type = UserDefinedKernel;
anthony7a01dcf2010-05-11 12:25:52 +0000244 kernel->next = (KernelInfo *) NULL;
cristyd43a46b2010-01-21 02:13:41 +0000245 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000246
anthony5ef8e942010-05-11 06:51:12 +0000247 /* find end of this specific kernel definition string */
248 end = strchr(kernel_string, ';');
249 if ( end == (char *) NULL )
250 end = strchr(kernel_string, '\0');
251
anthony43c49252010-05-18 10:59:50 +0000252 /* clear flags - for Expanding kernal lists thorugh rotations */
253 flags = NoValue;
254
anthony602ab9b2010-01-05 08:06:50 +0000255 /* Has a ':' in argument - New user kernel specification */
256 p = strchr(kernel_string, ':');
anthony5ef8e942010-05-11 06:51:12 +0000257 if ( p != (char *) NULL && p < end)
anthony602ab9b2010-01-05 08:06:50 +0000258 {
anthony602ab9b2010-01-05 08:06:50 +0000259 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
cristy150989e2010-02-01 14:59:39 +0000260 memcpy(token, kernel_string, (size_t) (p-kernel_string));
anthony602ab9b2010-01-05 08:06:50 +0000261 token[p-kernel_string] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000262 SetGeometryInfo(&args);
anthony602ab9b2010-01-05 08:06:50 +0000263 flags = ParseGeometry(token, &args);
anthony602ab9b2010-01-05 08:06:50 +0000264
anthony29188a82010-01-22 10:12:34 +0000265 /* Size handling and checks of geometry settings */
anthony602ab9b2010-01-05 08:06:50 +0000266 if ( (flags & WidthValue) == 0 ) /* if no width then */
267 args.rho = args.sigma; /* then width = height */
268 if ( args.rho < 1.0 ) /* if width too small */
269 args.rho = 1.0; /* then width = 1 */
270 if ( args.sigma < 1.0 ) /* if height too small */
271 args.sigma = args.rho; /* then height = width */
272 kernel->width = (unsigned long)args.rho;
273 kernel->height = (unsigned long)args.sigma;
274
275 /* Offset Handling and Checks */
276 if ( args.xi < 0.0 || args.psi < 0.0 )
anthony83ba99b2010-01-24 08:48:15 +0000277 return(DestroyKernelInfo(kernel));
cristyc99304f2010-02-01 15:26:27 +0000278 kernel->x = ((flags & XValue)!=0) ? (long)args.xi
cristy150989e2010-02-01 14:59:39 +0000279 : (long) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000280 kernel->y = ((flags & YValue)!=0) ? (long)args.psi
cristy150989e2010-02-01 14:59:39 +0000281 : (long) (kernel->height-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000282 if ( kernel->x >= (long) kernel->width ||
283 kernel->y >= (long) kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000284 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000285
286 p++; /* advance beyond the ':' */
287 }
288 else
anthonyc84dce52010-05-07 05:42:23 +0000289 { /* ELSE - Old old specification, forming odd-square kernel */
anthony602ab9b2010-01-05 08:06:50 +0000290 /* count up number of values given */
291 p=(const char *) kernel_string;
cristya699b172010-01-06 16:48:49 +0000292 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
anthony29188a82010-01-22 10:12:34 +0000293 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony5ef8e942010-05-11 06:51:12 +0000294 for (i=0; p < end; i++)
anthony602ab9b2010-01-05 08:06:50 +0000295 {
296 GetMagickToken(p,&p,token);
297 if (*token == ',')
298 GetMagickToken(p,&p,token);
299 }
300 /* set the size of the kernel - old sized square */
301 kernel->width = kernel->height= (unsigned long) sqrt((double) i+1.0);
cristyc99304f2010-02-01 15:26:27 +0000302 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000303 p=(const char *) kernel_string;
anthony29188a82010-01-22 10:12:34 +0000304 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
305 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000306 }
307
308 /* Read in the kernel values from rest of input string argument */
309 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
310 kernel->height*sizeof(double));
311 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000312 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000313
cristyc99304f2010-02-01 15:26:27 +0000314 kernel->minimum = +MagickHuge;
315 kernel->maximum = -MagickHuge;
316 kernel->negative_range = kernel->positive_range = 0.0;
anthonyc84dce52010-05-07 05:42:23 +0000317
anthony5ef8e942010-05-11 06:51:12 +0000318 for (i=0; (i < (long) (kernel->width*kernel->height)) && (p < end); i++)
anthony602ab9b2010-01-05 08:06:50 +0000319 {
320 GetMagickToken(p,&p,token);
321 if (*token == ',')
322 GetMagickToken(p,&p,token);
anthony29188a82010-01-22 10:12:34 +0000323 if ( LocaleCompare("nan",token) == 0
anthonyc84dce52010-05-07 05:42:23 +0000324 || LocaleCompare("-",token) == 0 ) {
anthony29188a82010-01-22 10:12:34 +0000325 kernel->values[i] = nan; /* do not include this value in kernel */
326 }
327 else {
328 kernel->values[i] = StringToDouble(token);
329 ( kernel->values[i] < 0)
cristyc99304f2010-02-01 15:26:27 +0000330 ? ( kernel->negative_range += kernel->values[i] )
331 : ( kernel->positive_range += kernel->values[i] );
332 Minimize(kernel->minimum, kernel->values[i]);
333 Maximize(kernel->maximum, kernel->values[i]);
anthony29188a82010-01-22 10:12:34 +0000334 }
anthony602ab9b2010-01-05 08:06:50 +0000335 }
anthony29188a82010-01-22 10:12:34 +0000336
anthony5ef8e942010-05-11 06:51:12 +0000337 /* sanity check -- no more values in kernel definition */
338 GetMagickToken(p,&p,token);
339 if ( *token != '\0' && *token != ';' && *token != '\'' )
340 return(DestroyKernelInfo(kernel));
341
anthonyc84dce52010-05-07 05:42:23 +0000342#if 0
343 /* this was the old method of handling a incomplete kernel */
cristy150989e2010-02-01 14:59:39 +0000344 if ( i < (long) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000345 Minimize(kernel->minimum, kernel->values[i]);
346 Maximize(kernel->maximum, kernel->values[i]);
cristy150989e2010-02-01 14:59:39 +0000347 for ( ; i < (long) (kernel->width*kernel->height); i++)
anthony29188a82010-01-22 10:12:34 +0000348 kernel->values[i]=0.0;
349 }
anthonyc84dce52010-05-07 05:42:23 +0000350#else
351 /* Number of values for kernel was not enough - Report Error */
352 if ( i < (long) (kernel->width*kernel->height) )
353 return(DestroyKernelInfo(kernel));
354#endif
355
356 /* check that we recieved at least one real (non-nan) value! */
357 if ( kernel->minimum == MagickHuge )
358 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000359
anthony43c49252010-05-18 10:59:50 +0000360 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel size */
361 ExpandKernelInfo(kernel, 45.0);
362 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel size */
363 ExpandKernelInfo(kernel, 90.0);
364
anthony602ab9b2010-01-05 08:06:50 +0000365 return(kernel);
366}
anthonyc84dce52010-05-07 05:42:23 +0000367
anthony43c49252010-05-18 10:59:50 +0000368static KernelInfo *ParseKernelName(const char *kernel_string)
anthonyc84dce52010-05-07 05:42:23 +0000369{
anthonyf0176c32010-05-23 23:08:57 +0000370 KernelInfo
371 *kernel;
372
anthonyc84dce52010-05-07 05:42:23 +0000373 char
374 token[MaxTextExtent];
375
anthony5ef8e942010-05-11 06:51:12 +0000376 long
377 type;
378
anthonyc84dce52010-05-07 05:42:23 +0000379 const char
anthony7a01dcf2010-05-11 12:25:52 +0000380 *p,
381 *end;
anthonyc84dce52010-05-07 05:42:23 +0000382
383 MagickStatusType
384 flags;
385
386 GeometryInfo
387 args;
388
anthonyc84dce52010-05-07 05:42:23 +0000389 /* Parse special 'named' kernel */
anthony5ef8e942010-05-11 06:51:12 +0000390 GetMagickToken(kernel_string,&p,token);
anthonyc84dce52010-05-07 05:42:23 +0000391 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
392 if ( type < 0 || type == UserDefinedKernel )
anthony5ef8e942010-05-11 06:51:12 +0000393 return((KernelInfo *)NULL); /* not a valid named kernel */
anthonyc84dce52010-05-07 05:42:23 +0000394
395 while (((isspace((int) ((unsigned char) *p)) != 0) ||
anthony5ef8e942010-05-11 06:51:12 +0000396 (*p == ',') || (*p == ':' )) && (*p != '\0') && (*p != ';'))
anthonyc84dce52010-05-07 05:42:23 +0000397 p++;
anthony7a01dcf2010-05-11 12:25:52 +0000398
399 end = strchr(p, ';'); /* end of this kernel defintion */
400 if ( end == (char *) NULL )
401 end = strchr(p, '\0');
402
403 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
404 memcpy(token, p, (size_t) (end-p));
405 token[end-p] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000406 SetGeometryInfo(&args);
anthony7a01dcf2010-05-11 12:25:52 +0000407 flags = ParseGeometry(token, &args);
anthonyc84dce52010-05-07 05:42:23 +0000408
anthony3c10fc82010-05-13 02:40:51 +0000409#if 0
410 /* For Debugging Geometry Input */
anthony46a369d2010-05-19 02:41:48 +0000411 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
anthony3c10fc82010-05-13 02:40:51 +0000412 flags, args.rho, args.sigma, args.xi, args.psi );
413#endif
414
anthonyc84dce52010-05-07 05:42:23 +0000415 /* special handling of missing values in input string */
416 switch( type ) {
anthony5ef8e942010-05-11 06:51:12 +0000417 case RectangleKernel:
418 if ( (flags & WidthValue) == 0 ) /* if no width then */
419 args.rho = args.sigma; /* then width = height */
420 if ( args.rho < 1.0 ) /* if width too small */
421 args.rho = 3; /* then width = 3 */
422 if ( args.sigma < 1.0 ) /* if height too small */
423 args.sigma = args.rho; /* then height = width */
424 if ( (flags & XValue) == 0 ) /* center offset if not defined */
425 args.xi = (double)(((long)args.rho-1)/2);
426 if ( (flags & YValue) == 0 )
427 args.psi = (double)(((long)args.sigma-1)/2);
428 break;
429 case SquareKernel:
430 case DiamondKernel:
431 case DiskKernel:
432 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +0000433 case CrossKernel:
anthony5ef8e942010-05-11 06:51:12 +0000434 /* If no scale given (a 0 scale is valid! - set it to 1.0 */
435 if ( (flags & HeightValue) == 0 )
436 args.sigma = 1.0;
437 break;
anthonyc1061722010-05-14 06:23:49 +0000438 case RingKernel:
439 if ( (flags & XValue) == 0 )
440 args.xi = 1.0;
441 break;
anthony5ef8e942010-05-11 06:51:12 +0000442 case ChebyshevKernel:
443 case ManhattenKernel:
444 case EuclideanKernel:
anthony43c49252010-05-18 10:59:50 +0000445 if ( (flags & HeightValue) == 0 ) /* no distance scale */
446 args.sigma = 100.0; /* default distance scaling */
447 else if ( (flags & AspectValue ) != 0 ) /* '!' flag */
448 args.sigma = QuantumRange/(args.sigma+1); /* maximum pixel distance */
449 else if ( (flags & PercentValue ) != 0 ) /* '%' flag */
450 args.sigma *= QuantumRange/100.0; /* percentage of color range */
anthony5ef8e942010-05-11 06:51:12 +0000451 break;
452 default:
453 break;
anthonyc84dce52010-05-07 05:42:23 +0000454 }
455
anthonyf0176c32010-05-23 23:08:57 +0000456 kernel = AcquireKernelBuiltIn((KernelInfoType)type, &args);
457
458 /* global expand to rotated kernel list - only for single kernels */
459 if ( kernel->next == (KernelInfo *) NULL ) {
460 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel args */
461 ExpandKernelInfo(kernel, 45.0);
462 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel args */
463 ExpandKernelInfo(kernel, 90.0);
464 }
465
466 return(kernel);
anthonyc84dce52010-05-07 05:42:23 +0000467}
468
anthony5ef8e942010-05-11 06:51:12 +0000469MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
470{
anthony7a01dcf2010-05-11 12:25:52 +0000471
472 KernelInfo
anthonydbc89892010-05-12 07:05:27 +0000473 *kernel,
anthony43c49252010-05-18 10:59:50 +0000474 *new_kernel;
anthony7a01dcf2010-05-11 12:25:52 +0000475
anthony5ef8e942010-05-11 06:51:12 +0000476 char
477 token[MaxTextExtent];
478
anthony7a01dcf2010-05-11 12:25:52 +0000479 const char
anthonydbc89892010-05-12 07:05:27 +0000480 *p;
anthony7a01dcf2010-05-11 12:25:52 +0000481
anthonye108a3f2010-05-12 07:24:03 +0000482 unsigned long
483 kernel_number;
484
anthonydbc89892010-05-12 07:05:27 +0000485 p = kernel_string;
anthony43c49252010-05-18 10:59:50 +0000486 kernel = NULL;
anthonye108a3f2010-05-12 07:24:03 +0000487 kernel_number = 0;
anthony5ef8e942010-05-11 06:51:12 +0000488
anthonydbc89892010-05-12 07:05:27 +0000489 while ( GetMagickToken(p,NULL,token), *token != '\0' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000490
anthony43c49252010-05-18 10:59:50 +0000491 /* ignore extra or multiple ';' kernel seperators */
anthonydbc89892010-05-12 07:05:27 +0000492 if ( *token != ';' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000493
anthonydbc89892010-05-12 07:05:27 +0000494 /* tokens starting with alpha is a Named kernel */
anthony43c49252010-05-18 10:59:50 +0000495 if (isalpha((int) *token) != 0)
496 new_kernel = ParseKernelName(p);
anthonydbc89892010-05-12 07:05:27 +0000497 else /* otherwise a user defined kernel array */
anthony43c49252010-05-18 10:59:50 +0000498 new_kernel = ParseKernelArray(p);
anthony7a01dcf2010-05-11 12:25:52 +0000499
anthonye108a3f2010-05-12 07:24:03 +0000500 /* Error handling -- this is not proper error handling! */
501 if ( new_kernel == (KernelInfo *) NULL ) {
502 fprintf(stderr, "Failed to parse kernel number #%lu\n", kernel_number);
503 if ( kernel != (KernelInfo *) NULL )
504 kernel=DestroyKernelInfo(kernel);
505 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000506 }
anthonye108a3f2010-05-12 07:24:03 +0000507
508 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000509 if ( kernel == (KernelInfo *) NULL )
510 kernel = new_kernel;
511 else
anthony43c49252010-05-18 10:59:50 +0000512 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000513 }
514
515 /* look for the next kernel in list */
516 p = strchr(p, ';');
517 if ( p == (char *) NULL )
518 break;
519 p++;
520
521 }
anthony7a01dcf2010-05-11 12:25:52 +0000522 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000523}
524
anthony602ab9b2010-01-05 08:06:50 +0000525
526/*
527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528% %
529% %
530% %
531% A c q u i r e K e r n e l B u i l t I n %
532% %
533% %
534% %
535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536%
537% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
538% kernels used for special purposes such as gaussian blurring, skeleton
539% pruning, and edge distance determination.
540%
541% They take a KernelType, and a set of geometry style arguments, which were
542% typically decoded from a user supplied string, or from a more complex
543% Morphology Method that was requested.
544%
545% The format of the AcquireKernalBuiltIn method is:
546%
cristy2be15382010-01-21 02:38:03 +0000547% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000548% const GeometryInfo args)
549%
550% A description of each parameter follows:
551%
552% o type: the pre-defined type of kernel wanted
553%
554% o args: arguments defining or modifying the kernel
555%
556% Convolution Kernels
557%
anthony46a369d2010-05-19 02:41:48 +0000558% Unity
559% the No-Op kernel, also requivelent to Gaussian of sigma zero.
560% Basically a 3x3 kernel of a 1 surrounded by zeros.
561%
anthony3c10fc82010-05-13 02:40:51 +0000562% Gaussian:{radius},{sigma}
563% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000564% The sigma for the curve is required. The resulting kernel is
565% normalized,
566%
567% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000568%
569% NOTE: that the 'radius' is optional, but if provided can limit (clip)
570% the final size of the resulting kernel to a square 2*radius+1 in size.
571% The radius should be at least 2 times that of the sigma value, or
572% sever clipping and aliasing may result. If not given or set to 0 the
573% radius will be determined so as to produce the best minimal error
574% result, which is usally much larger than is normally needed.
575%
anthonyc1061722010-05-14 06:23:49 +0000576% DOG:{radius},{sigma1},{sigma2}
577% "Difference of Gaussians" Kernel.
578% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
579% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
580% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000581%
anthony9eb4f742010-05-18 02:45:54 +0000582% LOG:{radius},{sigma}
583% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
584% The supposed ideal edge detection, zero-summing kernel.
585%
586% An alturnative to this kernel is to use a "DOG" with a sigma ratio of
587% approx 1.6, which can also be applied as a 2 pass "DOB" (see below).
588%
anthonyc1061722010-05-14 06:23:49 +0000589% Blur:{radius},{sigma}[,{angle}]
590% Generates a 1 dimensional or linear gaussian blur, at the angle given
591% (current restricted to orthogonal angles). If a 'radius' is given the
592% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
593% by a 90 degree angle.
594%
595% If 'sigma' is zero, you get a single pixel on a field of zeros.
596%
597% Note that two convolutions with two "Blur" kernels perpendicular to
598% each other, is equivelent to a far larger "Gaussian" kernel with the
599% same sigma value, However it is much faster to apply. This is how the
600% "-blur" operator actually works.
601%
602% DOB:{radius},{sigma1},{sigma2}[,{angle}]
603% "Difference of Blurs" Kernel.
604% As "Blur" but with the 1D gaussian produced by 'sigma2' subtracted
605% from thethe 1D gaussian produced by 'sigma1'.
606% The result is a zero-summing kernel.
607%
608% This can be used to generate a faster "DOG" convolution, in the same
609% way "Blur" can.
anthony602ab9b2010-01-05 08:06:50 +0000610%
anthony3c10fc82010-05-13 02:40:51 +0000611% Comet:{width},{sigma},{angle}
612% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000613% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000614% Adding two such blurs in opposite directions produces a Blur Kernel.
615% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000616%
anthony3c10fc82010-05-13 02:40:51 +0000617% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000618% radius of the kernel.
619%
620% # Still to be implemented...
621% #
anthony4fd27e22010-02-07 08:17:18 +0000622% # Filter2D
623% # Filter1D
624% # Set kernel values using a resize filter, and given scale (sigma)
625% # Cylindrical or Linear. Is this posible with an image?
626% #
anthony602ab9b2010-01-05 08:06:50 +0000627%
anthony3c10fc82010-05-13 02:40:51 +0000628% Named Constant Convolution Kernels
629%
anthonyc1061722010-05-14 06:23:49 +0000630% All these are unscaled, zero-summing kernels by default. As such for
631% non-HDRI version of ImageMagick some form of normalization, user scaling,
632% and biasing the results is recommended, to prevent the resulting image
633% being 'clipped'.
634%
635% The 3x3 kernels (most of these) can be circularly rotated in multiples of
636% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000637%
638% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000639% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000640% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
641% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000642% Type 2 : 3x3 with center:4 edge:1 corner:-2
643% Type 3 : 3x3 with center:4 edge:-2 corner:1
644% Type 5 : 5x5 laplacian
645% Type 7 : 7x7 laplacian
anthony43c49252010-05-18 10:59:50 +0000646% Type 15 : 5x5 LOG (sigma approx 1.4)
647% Type 19 : 9x9 LOG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000648%
649% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000650% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000651% -1, 0, 1
652% -2, 0,-2
653% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000654%
anthonyc1061722010-05-14 06:23:49 +0000655% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000656% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000657% 0, 0, 0
658% -1, 1, 0
659% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000660% Prewitt:{angle}
661% Prewitt Edge convolution kernel (3x3)
662% -1, 0, 1
663% -1, 0, 1
664% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000665% Compass:{angle}
666% Prewitt's "Compass" convolution kernel (3x3)
667% -1, 1, 1
668% -1,-2, 1
669% -1, 1, 1
670% Kirsch:{angle}
671% Kirsch's "Compass" convolution kernel (3x3)
672% -3,-3, 5
673% -3, 0, 5
674% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000675%
anthonye2a60ce2010-05-19 12:30:40 +0000676% FreiChen:{type},{angle}
677% Frei-Chen Edge Detector is a set of 9 unique convolution kernels that
anthonyc3cd15b2010-05-27 06:05:40 +0000678% are specially weighted.
679%
680% Type 0: | -1, 0, 1 |
681% | -sqrt(2), 0, sqrt(2) |
682% | -1, 0, 1 |
683%
684% This is basically the unnormalized discrete kernel that can be used
685% instead ot a Sobel kernel.
686%
687% The next 9 kernel types are specially pre-weighted. They should not
688% be normalized. After applying each to the original image, the results
689% is then added together. The square root of the resulting image is
690% the cosine of the edge, and the direction of the feature detection.
anthonye2a60ce2010-05-19 12:30:40 +0000691%
692% Type 1: | 1, sqrt(2), 1 |
693% | 0, 0, 0 | / 2*sqrt(2)
694% | -1, -sqrt(2), -1 |
695%
696% Type 2: | 1, 0, 1 |
697% | sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
698% | 1, 0, 1 |
699%
700% Type 3: | 0, -1, sqrt(2) |
701% | 1, 0, -1 | / 2*sqrt(2)
702% | -sqrt(2), 1, 0 |
703%
anthony6915d062010-05-19 12:45:51 +0000704% Type 4: | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000705% | -1, 0, 1 | / 2*sqrt(2)
706% | 0, 1, -sqrt(2) |
707%
708% Type 5: | 0, 1, 0 |
709% | -1, 0, -1 | / 2
710% | 0, 1, 0 |
711%
712% Type 6: | -1, 0, 1 |
713% | 0, 0, 0 | / 2
714% | 1, 0, -1 |
715%
anthonyf4e00312010-05-20 12:06:35 +0000716% Type 7: | 1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000717% | -2, 4, -2 | / 6
718% | 1, -2, 1 |
719%
anthonyf4e00312010-05-20 12:06:35 +0000720% Type 8: | -2, 1, -2 |
721% | 1, 4, 1 | / 6
722% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000723%
anthonyf4e00312010-05-20 12:06:35 +0000724% Type 9: | 1, 1, 1 |
725% | 1, 1, 1 | / 3
726% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000727%
728% The first 4 are for edge detection, the next 4 are for line detection
729% and the last is to add a average component to the results.
730%
anthonyc3cd15b2010-05-27 06:05:40 +0000731% Using a special type of '-1' will return all 9 pre-weighted kernels
732% as a multi-kernel list, so that you can use them directly (without
733% normalization) with the special "-set option:morphology:compose Plus"
734% setting to apply the full FreiChen Edge Detection Technique.
735%
anthony1dd091a2010-05-27 06:31:15 +0000736% If 'type' is large it will be taken to be an actual rotation angle for
737% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
738% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
739%
anthonye2a60ce2010-05-19 12:30:40 +0000740%
anthony602ab9b2010-01-05 08:06:50 +0000741% Boolean Kernels
742%
anthony3c10fc82010-05-13 02:40:51 +0000743% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000744% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000745% Kernel size will again be radius*2+1 square and defaults to radius 1,
746% generating a 3x3 kernel that is slightly larger than a square.
747%
anthony3c10fc82010-05-13 02:40:51 +0000748% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000749% Generate a square shaped kernel of size radius*2+1, and defaulting
750% to a 3x3 (radius 1).
751%
anthonyc1061722010-05-14 06:23:49 +0000752% Note that using a larger radius for the "Square" or the "Diamond" is
753% also equivelent to iterating the basic morphological method that many
754% times. However iterating with the smaller radius is actually faster
755% than using a larger kernel radius.
756%
757% Rectangle:{geometry}
758% Simply generate a rectangle of 1's with the size given. You can also
759% specify the location of the 'control point', otherwise the closest
760% pixel to the center of the rectangle is selected.
761%
762% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000763%
anthony3c10fc82010-05-13 02:40:51 +0000764% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000765% Generate a binary disk of the radius given, radius may be a float.
766% Kernel size will be ceil(radius)*2+1 square.
767% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000768% "Disk:1" => "diamond" or "cross:1"
769% "Disk:1.5" => "square"
770% "Disk:2" => "diamond:2"
771% "Disk:2.5" => a general disk shape of radius 2
772% "Disk:2.9" => "square:2"
773% "Disk:3.5" => default - octagonal/disk shape of radius 3
774% "Disk:4.2" => roughly octagonal shape of radius 4
775% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000776% After this all the kernel shape becomes more and more circular.
777%
778% Because a "disk" is more circular when using a larger radius, using a
779% larger radius is preferred over iterating the morphological operation.
780%
anthonyc1061722010-05-14 06:23:49 +0000781% Symbol Dilation Kernels
782%
783% These kernel is not a good general morphological kernel, but is used
784% more for highlighting and marking any single pixels in an image using,
785% a "Dilate" method as appropriate.
786%
787% For the same reasons iterating these kernels does not produce the
788% same result as using a larger radius for the symbol.
789%
anthony3c10fc82010-05-13 02:40:51 +0000790% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000791% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000792% Generate a kernel in the shape of a 'plus' or a 'cross' with
793% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000794%
795% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000796%
anthonyc1061722010-05-14 06:23:49 +0000797% Ring:{radius1},{radius2}[,{scale}]
798% A ring of the values given that falls between the two radii.
799% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
800% This is the 'edge' pixels of the default "Disk" kernel,
801% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000802%
anthony3dd0f622010-05-13 12:57:32 +0000803% Hit and Miss Kernels
804%
805% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000806% Find any peak larger than the pixels the fall between the two radii.
807% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000808% Edges
anthony1d45eb92010-05-25 11:13:23 +0000809% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000810% Corners
811% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000812% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000813% Find single pixel ridges or thin lines
814% Ridges2
815% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000816% Ridges3
817% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000818% LineEnds
819% Find end points of lines (for pruning a skeletion)
820% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000821% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000822% ConvexHull
823% Octagonal thicken kernel, to generate convex hulls of 45 degrees
824% Skeleton
825% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000826%
827% Distance Measuring Kernels
828%
anthonyc1061722010-05-14 06:23:49 +0000829% Different types of distance measuring methods, which are used with the
830% a 'Distance' morphology method for generating a gradient based on
831% distance from an edge of a binary shape, though there is a technique
832% for handling a anti-aliased shape.
833%
834% See the 'Distance' Morphological Method, for information of how it is
835% applied.
836%
anthony3dd0f622010-05-13 12:57:32 +0000837% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000838% Chebyshev Distance (also known as Tchebychev Distance) is a value of
839% one to any neighbour, orthogonal or diagonal. One why of thinking of
840% it is the number of squares a 'King' or 'Queen' in chess needs to
841% traverse reach any other position on a chess board. It results in a
842% 'square' like distance function, but one where diagonals are closer
843% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000844%
anthonyc1061722010-05-14 06:23:49 +0000845% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000846% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
847% Cab metric), is the distance needed when you can only travel in
848% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
849% in chess would travel. It results in a diamond like distances, where
850% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000851%
anthonyc1061722010-05-14 06:23:49 +0000852% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000853% Euclidean Distance is the 'direct' or 'as the crow flys distance.
854% However by default the kernel size only has a radius of 1, which
855% limits the distance to 'Knight' like moves, with only orthogonal and
856% diagonal measurements being correct. As such for the default kernel
857% you will get octagonal like distance function, which is reasonally
858% accurate.
859%
860% However if you use a larger radius such as "Euclidean:4" you will
861% get a much smoother distance gradient from the edge of the shape.
862% Of course a larger kernel is slower to use, and generally not needed.
863%
864% To allow the use of fractional distances that you get with diagonals
865% the actual distance is scaled by a fixed value which the user can
866% provide. This is not actually nessary for either ""Chebyshev" or
867% "Manhatten" distance kernels, but is done for all three distance
868% kernels. If no scale is provided it is set to a value of 100,
869% allowing for a maximum distance measurement of 655 pixels using a Q16
870% version of IM, from any edge. However for small images this can
871% result in quite a dark gradient.
872%
anthony602ab9b2010-01-05 08:06:50 +0000873*/
874
cristy2be15382010-01-21 02:38:03 +0000875MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000876 const GeometryInfo *args)
877{
cristy2be15382010-01-21 02:38:03 +0000878 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000879 *kernel;
880
cristy150989e2010-02-01 14:59:39 +0000881 register long
anthony602ab9b2010-01-05 08:06:50 +0000882 i;
883
884 register long
885 u,
886 v;
887
888 double
889 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
890
anthonyc1061722010-05-14 06:23:49 +0000891 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000892 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000893 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000894 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000895 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000896 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000897 break;
anthony1dd091a2010-05-27 06:31:15 +0000898 case UnityKernel: /* Named Descrete Convolution Kernels */
899 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000900 case SobelKernel:
901 case RobertsKernel:
902 case PrewittKernel:
903 case CompassKernel:
904 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000905 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000906 case CornersKernel: /* Hit and Miss kernels */
907 case LineEndsKernel:
908 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000909 case EdgesKernel:
910 case RidgesKernel:
911 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000912 case ConvexHullKernel:
913 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000914 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000915 /* A pre-generated kernel is not needed */
916 break;
anthony1dd091a2010-05-27 06:31:15 +0000917#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000918 case GaussianKernel:
919 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000920 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000921 case BlurKernel:
922 case DOBKernel:
923 case CometKernel:
924 case DiamondKernel:
925 case SquareKernel:
926 case RectangleKernel:
927 case DiskKernel:
928 case PlusKernel:
929 case CrossKernel:
930 case RingKernel:
931 case PeaksKernel:
932 case ChebyshevKernel:
933 case ManhattenKernel:
934 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000935#else
anthony9eb4f742010-05-18 02:45:54 +0000936 default:
anthony1dd091a2010-05-27 06:31:15 +0000937#endif
anthony9eb4f742010-05-18 02:45:54 +0000938 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000939 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
940 if (kernel == (KernelInfo *) NULL)
941 return(kernel);
942 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000943 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000944 kernel->negative_range = kernel->positive_range = 0.0;
945 kernel->type = type;
946 kernel->next = (KernelInfo *) NULL;
947 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000948 break;
949 }
anthony602ab9b2010-01-05 08:06:50 +0000950
951 switch(type) {
952 /* Convolution Kernels */
953 case GaussianKernel:
anthonyc1061722010-05-14 06:23:49 +0000954 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000955 case LOGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000956 { double
anthonyc1061722010-05-14 06:23:49 +0000957 sigma = fabs(args->sigma),
958 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000959 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000960
anthonyc1061722010-05-14 06:23:49 +0000961 if ( args->rho >= 1.0 )
962 kernel->width = (unsigned long)args->rho*2+1;
anthony9eb4f742010-05-18 02:45:54 +0000963 else if ( (type != DOGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000964 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
965 else
966 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
967 kernel->height = kernel->width;
cristyc99304f2010-02-01 15:26:27 +0000968 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000969 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
970 kernel->height*sizeof(double));
971 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000972 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000973
anthony46a369d2010-05-19 02:41:48 +0000974 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000975 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000976 *
977 * How to do this is currently not known, but appears to be
978 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000979 */
980
981 if ( type == GaussianKernel || type == DOGKernel )
982 { /* Calculate a Gaussian, OR positive half of a DOG */
983 if ( sigma > MagickEpsilon )
984 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
985 B = 1.0/(Magick2PI*sigma*sigma);
986 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
987 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
988 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
989 }
990 else /* limiting case - a unity (normalized Dirac) kernel */
991 { (void) ResetMagickMemory(kernel->values,0, (size_t)
992 kernel->width*kernel->height*sizeof(double));
993 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
994 }
anthonyc1061722010-05-14 06:23:49 +0000995 }
anthony9eb4f742010-05-18 02:45:54 +0000996
anthonyc1061722010-05-14 06:23:49 +0000997 if ( type == DOGKernel )
998 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
999 if ( sigma2 > MagickEpsilon )
1000 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001001 A = 1.0/(2.0*sigma*sigma);
1002 B = 1.0/(Magick2PI*sigma*sigma);
anthonyc1061722010-05-14 06:23:49 +00001003 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1004 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001005 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001006 }
anthony9eb4f742010-05-18 02:45:54 +00001007 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001008 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1009 }
anthony9eb4f742010-05-18 02:45:54 +00001010
1011 if ( type == LOGKernel )
1012 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1013 if ( sigma > MagickEpsilon )
1014 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1015 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
1016 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1017 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1018 { R = ((double)(u*u+v*v))*A;
1019 kernel->values[i] = (1-R)*exp(-R)*B;
1020 }
1021 }
1022 else /* special case - generate a unity kernel */
1023 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1024 kernel->width*kernel->height*sizeof(double));
1025 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1026 }
1027 }
1028
1029 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001030 ** radius, producing a smaller (darker) kernel. Also for very small
1031 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1032 ** producing a very bright kernel.
1033 **
1034 ** Normalization will still be needed.
1035 */
anthony602ab9b2010-01-05 08:06:50 +00001036
anthony3dd0f622010-05-13 12:57:32 +00001037 /* Normalize the 2D Gaussian Kernel
1038 **
anthonyc1061722010-05-14 06:23:49 +00001039 ** NB: a CorrelateNormalize performs a normal Normalize if
1040 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001041 */
anthony46a369d2010-05-19 02:41:48 +00001042 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001043 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001044
1045 break;
1046 }
1047 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +00001048 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001049 { double
anthonyc1061722010-05-14 06:23:49 +00001050 sigma = fabs(args->sigma),
1051 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001052 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001053
anthonyc1061722010-05-14 06:23:49 +00001054 if ( args->rho >= 1.0 )
1055 kernel->width = (unsigned long)args->rho*2+1;
1056 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1057 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1058 else
1059 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001060 kernel->height = 1;
anthonyc1061722010-05-14 06:23:49 +00001061 kernel->x = (long) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001062 kernel->y = 0;
1063 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001064 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1065 kernel->height*sizeof(double));
1066 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001067 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001068
1069#if 1
1070#define KernelRank 3
1071 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1072 ** It generates a gaussian 3 times the width, and compresses it into
1073 ** the expected range. This produces a closer normalization of the
1074 ** resulting kernel, especially for very low sigma values.
1075 ** As such while wierd it is prefered.
1076 **
1077 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001078 **
1079 ** A properly normalized curve is generated (apart from edge clipping)
1080 ** even though we later normalize the result (for edge clipping)
1081 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001082 */
anthonyc1061722010-05-14 06:23:49 +00001083
1084 /* initialize */
cristy150989e2010-02-01 14:59:39 +00001085 v = (long) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001086 (void) ResetMagickMemory(kernel->values,0, (size_t)
1087 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001088 /* Calculate a Positive 1D Gaussian */
1089 if ( sigma > MagickEpsilon )
1090 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001091 A = 1.0/(2.0*sigma*sigma);
1092 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001093 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001094 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001095 }
1096 }
1097 else /* special case - generate a unity kernel */
1098 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001099
1100 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001101 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001102 {
anthonyc1061722010-05-14 06:23:49 +00001103 if ( sigma2 > MagickEpsilon )
1104 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001105 A = 1.0/(2.0*sigma*sigma);
1106 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001107 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001108 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001109 }
anthony9eb4f742010-05-18 02:45:54 +00001110 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001111 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1112 }
anthony602ab9b2010-01-05 08:06:50 +00001113#else
anthonyc1061722010-05-14 06:23:49 +00001114 /* Direct calculation without curve averaging */
1115
1116 /* Calculate a Positive Gaussian */
1117 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001118 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1119 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001120 for ( i=0, u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001121 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001122 }
1123 else /* special case - generate a unity kernel */
1124 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1125 kernel->width*kernel->height*sizeof(double));
1126 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1127 }
anthony9eb4f742010-05-18 02:45:54 +00001128
1129 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001130 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001131 {
anthonyc1061722010-05-14 06:23:49 +00001132 if ( sigma2 > MagickEpsilon )
1133 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001134 A = 1.0/(2.0*sigma*sigma);
1135 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001136 for ( i=0, u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001137 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001138 }
anthony9eb4f742010-05-18 02:45:54 +00001139 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001140 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1141 }
anthony602ab9b2010-01-05 08:06:50 +00001142#endif
anthonyc1061722010-05-14 06:23:49 +00001143 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001144 ** radius, producing a smaller (darker) kernel. Also for very small
1145 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1146 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001147 **
1148 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001149 */
anthonycc6c8362010-01-25 04:14:01 +00001150
anthony602ab9b2010-01-05 08:06:50 +00001151 /* Normalize the 1D Gaussian Kernel
1152 **
anthonyc1061722010-05-14 06:23:49 +00001153 ** NB: a CorrelateNormalize performs a normal Normalize if
1154 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001155 */
anthony46a369d2010-05-19 02:41:48 +00001156 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1157 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001158
anthonyc1061722010-05-14 06:23:49 +00001159 /* rotate the 1D kernel by given angle */
1160 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001161 break;
1162 }
1163 case CometKernel:
1164 { double
anthony9eb4f742010-05-18 02:45:54 +00001165 sigma = fabs(args->sigma),
1166 A;
anthony602ab9b2010-01-05 08:06:50 +00001167
anthony602ab9b2010-01-05 08:06:50 +00001168 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001169 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001170 else
1171 kernel->width = (unsigned long)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001172 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001173 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001174 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001175 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1176 kernel->height*sizeof(double));
1177 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001178 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001179
anthonyc1061722010-05-14 06:23:49 +00001180 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001181 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001182 ** curve to use so may change in the future. The function must be
1183 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001184 **
1185 ** As we are normalizing and not subtracting gaussians,
1186 ** there is no need for a divisor in the gaussian formula
1187 **
anthony43c49252010-05-18 10:59:50 +00001188 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001189 */
anthony9eb4f742010-05-18 02:45:54 +00001190 if ( sigma > MagickEpsilon )
1191 {
anthony602ab9b2010-01-05 08:06:50 +00001192#if 1
1193#define KernelRank 3
anthony9eb4f742010-05-18 02:45:54 +00001194 v = (long) kernel->width*KernelRank; /* start/end points */
1195 (void) ResetMagickMemory(kernel->values,0, (size_t)
1196 kernel->width*sizeof(double));
1197 sigma *= KernelRank; /* simplify the loop expression */
1198 A = 1.0/(2.0*sigma*sigma);
1199 /* B = 1.0/(MagickSQ2PI*sigma); */
1200 for ( u=0; u < v; u++) {
1201 kernel->values[u/KernelRank] +=
1202 exp(-((double)(u*u))*A);
1203 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1204 }
1205 for (i=0; i < (long) kernel->width; i++)
1206 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001207#else
anthony9eb4f742010-05-18 02:45:54 +00001208 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1209 /* B = 1.0/(MagickSQ2PI*sigma); */
1210 for ( i=0; i < (long) kernel->width; i++)
1211 kernel->positive_range +=
1212 kernel->values[i] =
1213 exp(-((double)(i*i))*A);
1214 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001215#endif
anthony9eb4f742010-05-18 02:45:54 +00001216 }
1217 else /* special case - generate a unity kernel */
1218 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1219 kernel->width*kernel->height*sizeof(double));
1220 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1221 kernel->positive_range = 1.0;
1222 }
anthony46a369d2010-05-19 02:41:48 +00001223
1224 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001225 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001226 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001227
anthony999bb2c2010-02-18 12:38:01 +00001228 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1229 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001230 break;
1231 }
anthonyc1061722010-05-14 06:23:49 +00001232
anthony3c10fc82010-05-13 02:40:51 +00001233 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001234 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001235 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001236 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001237 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001238 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001239 break;
anthony9eb4f742010-05-18 02:45:54 +00001240 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001241 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001242 break;
1243 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001244 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1245 break;
1246 case 3:
anthonyc1061722010-05-14 06:23:49 +00001247 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001248 break;
anthony9eb4f742010-05-18 02:45:54 +00001249 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001250 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001251 "5: -4,-1,0,-1,-4 -1,2,3,2,-1 0,3,4,3,0 -1,2,3,2,-1 -4,-1,0,-1,-4");
anthony3c10fc82010-05-13 02:40:51 +00001252 break;
anthony9eb4f742010-05-18 02:45:54 +00001253 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001254 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001255 "7:-10,-5,-2,-1,-2,-5,-10 -5,0,3,4,3,0,-5 -2,3,6,7,6,3,-2 -1,4,7,8,7,4,-1 -2,3,6,7,6,3,-2 -5,0,3,4,3,0,-5 -10,-5,-2,-1,-2,-5,-10" );
anthony3c10fc82010-05-13 02:40:51 +00001256 break;
anthony43c49252010-05-18 10:59:50 +00001257 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001258 kernel=ParseKernelArray(
1259 "5: 0,0,-1,0,0 0,-1,-2,-1,0 -1,-2,16,-2,-1 0,-1,-2,-1,0 0,0,-1,0,0");
1260 break;
anthony43c49252010-05-18 10:59:50 +00001261 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1262 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1263 kernel=ParseKernelArray(
1264 "9: 0,-1,-1,-2,-2,-2,-1,-1,0 -1,-2,-4,-5,-5,-5,-4,-2,-1 -1,-4,-5,-3,-0,-3,-5,-4,-1 -2,-5,-3,@12,@24,@12,-3,-5,-2 -2,-5,-0,@24,@40,@24,-0,-5,-2 -2,-5,-3,@12,@24,@12,-3,-5,-2 -1,-4,-5,-3,-0,-3,-5,-4,-1 -1,-2,-4,-5,-5,-5,-4,-2,-1 0,-1,-1,-2,-2,-2,-1,-1,0");
1265 break;
anthony3c10fc82010-05-13 02:40:51 +00001266 }
1267 if (kernel == (KernelInfo *) NULL)
1268 return(kernel);
1269 kernel->type = type;
1270 break;
1271 }
anthonyc1061722010-05-14 06:23:49 +00001272 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001273 {
anthonyc1061722010-05-14 06:23:49 +00001274 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1275 if (kernel == (KernelInfo *) NULL)
1276 return(kernel);
1277 kernel->type = type;
1278 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1279 break;
1280 }
1281 case RobertsKernel:
1282 {
1283 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1284 if (kernel == (KernelInfo *) NULL)
1285 return(kernel);
1286 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001287 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001288 break;
1289 }
1290 case PrewittKernel:
1291 {
1292 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1293 if (kernel == (KernelInfo *) NULL)
1294 return(kernel);
1295 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001296 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001297 break;
1298 }
1299 case CompassKernel:
1300 {
1301 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1302 if (kernel == (KernelInfo *) NULL)
1303 return(kernel);
1304 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001305 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001306 break;
1307 }
anthony9eb4f742010-05-18 02:45:54 +00001308 case KirschKernel:
1309 {
1310 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1311 if (kernel == (KernelInfo *) NULL)
1312 return(kernel);
1313 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001314 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001315 break;
1316 }
anthonye2a60ce2010-05-19 12:30:40 +00001317 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001318 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1319 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001320 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001321 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001322 case 0:
1323 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1324 if (kernel == (KernelInfo *) NULL)
1325 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001326 kernel->values[3] = -MagickSQ2;
1327 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001328 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001329 break;
anthonye2a60ce2010-05-19 12:30:40 +00001330 case 1:
1331 kernel=ParseKernelArray("3: 1,2,1 0,0,0 -1,2,-1");
1332 if (kernel == (KernelInfo *) NULL)
1333 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001334 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001335 kernel->values[1] = +MagickSQ2;
1336 kernel->values[7] = -MagickSQ2;
1337 CalcKernelMetaData(kernel); /* recalculate meta-data */
1338 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1339 break;
1340 case 2:
1341 kernel=ParseKernelArray("3: 1,0,1 2,0,2 1,0,1");
1342 if (kernel == (KernelInfo *) NULL)
1343 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001344 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001345 kernel->values[3] = +MagickSQ2;
1346 kernel->values[5] = +MagickSQ2;
1347 CalcKernelMetaData(kernel);
1348 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1349 break;
1350 case 3:
1351 kernel=ParseKernelArray("3: 0,-1,2 1,0,-1 -2,1,0");
1352 if (kernel == (KernelInfo *) NULL)
1353 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001354 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001355 kernel->values[2] = +MagickSQ2;
1356 kernel->values[6] = -MagickSQ2;
1357 CalcKernelMetaData(kernel);
1358 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1359 break;
1360 case 4:
anthony6915d062010-05-19 12:45:51 +00001361 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001362 if (kernel == (KernelInfo *) NULL)
1363 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001364 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001365 kernel->values[0] = +MagickSQ2;
1366 kernel->values[8] = -MagickSQ2;
1367 CalcKernelMetaData(kernel);
1368 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1369 break;
1370 case 5:
1371 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1372 if (kernel == (KernelInfo *) NULL)
1373 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001374 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001375 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1376 break;
1377 case 6:
1378 kernel=ParseKernelArray("3: -1,0,1 0,0,0 1,0,-1");
1379 if (kernel == (KernelInfo *) NULL)
1380 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001381 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001382 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1383 break;
1384 case 7:
anthonyf4e00312010-05-20 12:06:35 +00001385 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001386 if (kernel == (KernelInfo *) NULL)
1387 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001388 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001389 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1390 break;
1391 case 8:
1392 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1393 if (kernel == (KernelInfo *) NULL)
1394 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001395 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001396 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1397 break;
1398 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001399 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001400 if (kernel == (KernelInfo *) NULL)
1401 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001402 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001403 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1404 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001405 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001406 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1407 if (kernel == (KernelInfo *) NULL)
1408 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001409 break;
anthonye2a60ce2010-05-19 12:30:40 +00001410 }
anthonyc3cd15b2010-05-27 06:05:40 +00001411 if ( fabs(args->sigma) > MagickEpsilon )
1412 /* Rotate by correctly supplied 'angle' */
1413 RotateKernelInfo(kernel, args->sigma);
1414 else if ( args->rho > 30.0 || args->rho < -30.0 )
1415 /* Rotate by out of bounds 'type' */
1416 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001417 break;
1418 }
1419
anthonyc1061722010-05-14 06:23:49 +00001420 /* Boolean Kernels */
1421 case DiamondKernel:
1422 {
1423 if (args->rho < 1.0)
1424 kernel->width = kernel->height = 3; /* default radius = 1 */
1425 else
1426 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
1427 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1428
1429 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1430 kernel->height*sizeof(double));
1431 if (kernel->values == (double *) NULL)
1432 return(DestroyKernelInfo(kernel));
1433
1434 /* set all kernel values within diamond area to scale given */
1435 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1436 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1437 if ((labs(u)+labs(v)) <= (long)kernel->x)
1438 kernel->positive_range += kernel->values[i] = args->sigma;
1439 else
1440 kernel->values[i] = nan;
1441 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1442 break;
1443 }
1444 case SquareKernel:
1445 case RectangleKernel:
1446 { double
1447 scale;
anthony602ab9b2010-01-05 08:06:50 +00001448 if ( type == SquareKernel )
1449 {
1450 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001451 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001452 else
cristy150989e2010-02-01 14:59:39 +00001453 kernel->width = kernel->height = (unsigned long) (2*args->rho+1);
cristyc99304f2010-02-01 15:26:27 +00001454 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001455 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001456 }
1457 else {
cristy2be15382010-01-21 02:38:03 +00001458 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001459 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001460 return(DestroyKernelInfo(kernel)); /* invalid args given */
anthony602ab9b2010-01-05 08:06:50 +00001461 kernel->width = (unsigned long)args->rho;
1462 kernel->height = (unsigned long)args->sigma;
1463 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1464 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001465 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristyc99304f2010-02-01 15:26:27 +00001466 kernel->x = (long) args->xi;
1467 kernel->y = (long) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001468 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001469 }
1470 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1471 kernel->height*sizeof(double));
1472 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001473 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001474
anthony3dd0f622010-05-13 12:57:32 +00001475 /* set all kernel values to scale given */
cristy150989e2010-02-01 14:59:39 +00001476 u=(long) kernel->width*kernel->height;
1477 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001478 kernel->values[i] = scale;
1479 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1480 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001481 break;
anthony602ab9b2010-01-05 08:06:50 +00001482 }
anthony602ab9b2010-01-05 08:06:50 +00001483 case DiskKernel:
1484 {
anthonyc1061722010-05-14 06:23:49 +00001485 long limit = (long)(args->rho*args->rho);
anthony83ba99b2010-01-24 08:48:15 +00001486 if (args->rho < 0.1) /* default radius approx 3.5 */
1487 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001488 else
1489 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001490 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001491
1492 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1493 kernel->height*sizeof(double));
1494 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001495 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001496
anthony3dd0f622010-05-13 12:57:32 +00001497 /* set all kernel values within disk area to scale given */
1498 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
cristyc99304f2010-02-01 15:26:27 +00001499 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001500 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001501 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001502 else
1503 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001504 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001505 break;
1506 }
1507 case PlusKernel:
1508 {
1509 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001510 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001511 else
1512 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001513 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001514
1515 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1516 kernel->height*sizeof(double));
1517 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001518 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001519
anthony3dd0f622010-05-13 12:57:32 +00001520 /* set all kernel values along axises to given scale */
cristyc99304f2010-02-01 15:26:27 +00001521 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1522 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001523 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1524 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1525 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001526 break;
1527 }
anthony3dd0f622010-05-13 12:57:32 +00001528 case CrossKernel:
1529 {
1530 if (args->rho < 1.0)
1531 kernel->width = kernel->height = 5; /* default radius 2 */
1532 else
1533 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
1534 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1535
1536 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1537 kernel->height*sizeof(double));
1538 if (kernel->values == (double *) NULL)
1539 return(DestroyKernelInfo(kernel));
1540
1541 /* set all kernel values along axises to given scale */
1542 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1543 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1544 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1545 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1546 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1547 break;
1548 }
1549 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001550 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001551 case PeaksKernel:
1552 {
1553 long
1554 limit1,
anthonyc1061722010-05-14 06:23:49 +00001555 limit2,
1556 scale;
anthony3dd0f622010-05-13 12:57:32 +00001557
1558 if (args->rho < args->sigma)
1559 {
1560 kernel->width = ((unsigned long)args->sigma)*2+1;
1561 limit1 = (long)args->rho*args->rho;
1562 limit2 = (long)args->sigma*args->sigma;
1563 }
1564 else
1565 {
1566 kernel->width = ((unsigned long)args->rho)*2+1;
1567 limit1 = (long)args->sigma*args->sigma;
1568 limit2 = (long)args->rho*args->rho;
1569 }
anthonyc1061722010-05-14 06:23:49 +00001570 if ( limit2 <= 0 )
1571 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1572
anthony3dd0f622010-05-13 12:57:32 +00001573 kernel->height = kernel->width;
1574 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1575 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1576 kernel->height*sizeof(double));
1577 if (kernel->values == (double *) NULL)
1578 return(DestroyKernelInfo(kernel));
1579
anthonyc1061722010-05-14 06:23:49 +00001580 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristye96405a2010-05-19 02:24:31 +00001581 scale = (long) (( type == PeaksKernel) ? 0.0 : args->xi);
anthony3dd0f622010-05-13 12:57:32 +00001582 for ( i=0, v= -kernel->y; v <= (long)kernel->y; v++)
1583 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1584 { long radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001585 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001586 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001587 else
1588 kernel->values[i] = nan;
1589 }
cristye96405a2010-05-19 02:24:31 +00001590 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001591 if ( type == PeaksKernel ) {
1592 /* set the central point in the middle */
1593 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1594 kernel->positive_range = 1.0;
1595 kernel->maximum = 1.0;
1596 }
anthony3dd0f622010-05-13 12:57:32 +00001597 break;
1598 }
anthony43c49252010-05-18 10:59:50 +00001599 case EdgesKernel:
1600 {
1601 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1602 if (kernel == (KernelInfo *) NULL)
1603 return(kernel);
1604 kernel->type = type;
1605 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1606 break;
1607 }
anthony3dd0f622010-05-13 12:57:32 +00001608 case CornersKernel:
1609 {
anthony4f1dcb72010-05-14 08:43:10 +00001610 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001611 if (kernel == (KernelInfo *) NULL)
1612 return(kernel);
1613 kernel->type = type;
1614 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1615 break;
1616 }
anthony47f5d062010-05-23 07:47:50 +00001617 case RidgesKernel:
1618 {
anthony24a19842010-05-27 12:18:34 +00001619 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001620 if (kernel == (KernelInfo *) NULL)
1621 return(kernel);
1622 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001623 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001624 break;
1625 }
anthony1d45eb92010-05-25 11:13:23 +00001626 case Ridges2Kernel:
1627 {
1628 KernelInfo
1629 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001630 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001631 if (kernel == (KernelInfo *) NULL)
1632 return(kernel);
1633 kernel->type = type;
1634 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001635#if 0
1636 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001637 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1638 if (new_kernel == (KernelInfo *) NULL)
1639 return(DestroyKernelInfo(kernel));
1640 new_kernel->type = type;
1641 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1642 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001643#endif
1644 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1645 /* Unfortunatally we can not yet rotate a non-square kernel */
1646 /* But then we can't flip a non-symetrical kernel either */
1647 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1648 if (new_kernel == (KernelInfo *) NULL)
1649 return(DestroyKernelInfo(kernel));
1650 new_kernel->type = type;
1651 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001652 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001653 if (new_kernel == (KernelInfo *) NULL)
1654 return(DestroyKernelInfo(kernel));
1655 new_kernel->type = type;
1656 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001657 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001658 if (new_kernel == (KernelInfo *) NULL)
1659 return(DestroyKernelInfo(kernel));
1660 new_kernel->type = type;
1661 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001662 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001663 if (new_kernel == (KernelInfo *) NULL)
1664 return(DestroyKernelInfo(kernel));
1665 new_kernel->type = type;
1666 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001667 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001668 if (new_kernel == (KernelInfo *) NULL)
1669 return(DestroyKernelInfo(kernel));
1670 new_kernel->type = type;
1671 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001672 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001673 if (new_kernel == (KernelInfo *) NULL)
1674 return(DestroyKernelInfo(kernel));
1675 new_kernel->type = type;
1676 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001677 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001678 if (new_kernel == (KernelInfo *) NULL)
1679 return(DestroyKernelInfo(kernel));
1680 new_kernel->type = type;
1681 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001682 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001683 if (new_kernel == (KernelInfo *) NULL)
1684 return(DestroyKernelInfo(kernel));
1685 new_kernel->type = type;
1686 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001687 break;
1688 }
anthony3dd0f622010-05-13 12:57:32 +00001689 case LineEndsKernel:
1690 {
anthony43c49252010-05-18 10:59:50 +00001691 KernelInfo
1692 *new_kernel;
1693 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001694 if (kernel == (KernelInfo *) NULL)
1695 return(kernel);
1696 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001697 ExpandKernelInfo(kernel, 90.0);
1698 /* append second set of 4 kernels */
1699 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1700 if (new_kernel == (KernelInfo *) NULL)
1701 return(DestroyKernelInfo(kernel));
1702 new_kernel->type = type;
1703 ExpandKernelInfo(new_kernel, 90.0);
1704 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001705 break;
1706 }
1707 case LineJunctionsKernel:
1708 {
1709 KernelInfo
1710 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001711 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001712 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001713 if (kernel == (KernelInfo *) NULL)
1714 return(kernel);
1715 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001716 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001717 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001718 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001719 if (new_kernel == (KernelInfo *) NULL)
1720 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001721 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001722 ExpandKernelInfo(new_kernel, 90.0);
1723 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001724 break;
1725 }
anthony3dd0f622010-05-13 12:57:32 +00001726 case ConvexHullKernel:
1727 {
anthony4f1dcb72010-05-14 08:43:10 +00001728 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001729 if (kernel == (KernelInfo *) NULL)
1730 return(kernel);
1731 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001732 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001733 break;
1734 }
anthony47f5d062010-05-23 07:47:50 +00001735 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001736 { /* what is the best form for skeletonization by thinning? */
anthony47f5d062010-05-23 07:47:50 +00001737#if 0
1738# if 0
1739 kernel=AcquireKernelInfo("Corners;Edges");
1740# else
1741 kernel=AcquireKernelInfo("Edges;Corners");
1742# endif
1743#else
anthony43c49252010-05-18 10:59:50 +00001744 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001745 if (kernel == (KernelInfo *) NULL)
1746 return(kernel);
1747 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001748 ExpandKernelInfo(kernel, 45);
1749 break;
anthony47f5d062010-05-23 07:47:50 +00001750#endif
anthony3dd0f622010-05-13 12:57:32 +00001751 break;
1752 }
anthonya648a302010-05-27 02:14:36 +00001753 case MatKernel: /* experimental - MAT from a Distance Gradient */
1754 {
1755 KernelInfo
1756 *new_kernel;
1757 /* Ridge Kernel but without the diagonal */
1758 kernel=ParseKernelArray("3x1: 0,1,0");
1759 if (kernel == (KernelInfo *) NULL)
1760 return(kernel);
1761 kernel->type = RidgesKernel;
1762 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1763 /* Plus the 2 pixel ridges kernel - no diagonal */
1764 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1765 if (new_kernel == (KernelInfo *) NULL)
1766 return(kernel);
1767 LastKernelInfo(kernel)->next = new_kernel;
1768 break;
1769 }
anthony602ab9b2010-01-05 08:06:50 +00001770 /* Distance Measuring Kernels */
1771 case ChebyshevKernel:
1772 {
anthony602ab9b2010-01-05 08:06:50 +00001773 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001774 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001775 else
1776 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001777 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001778
1779 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1780 kernel->height*sizeof(double));
1781 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001782 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001783
cristyc99304f2010-02-01 15:26:27 +00001784 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1785 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1786 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001787 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001788 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001789 break;
1790 }
1791 case ManhattenKernel:
1792 {
anthony602ab9b2010-01-05 08:06:50 +00001793 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001794 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001795 else
1796 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001797 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001798
1799 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1800 kernel->height*sizeof(double));
1801 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001802 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001803
cristyc99304f2010-02-01 15:26:27 +00001804 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1805 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1806 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001807 args->sigma*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001808 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001809 break;
1810 }
1811 case EuclideanKernel:
1812 {
anthony602ab9b2010-01-05 08:06:50 +00001813 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001814 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001815 else
1816 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001817 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001818
1819 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1820 kernel->height*sizeof(double));
1821 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001822 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001823
cristyc99304f2010-02-01 15:26:27 +00001824 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1825 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1826 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001827 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001828 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001829 break;
1830 }
anthony46a369d2010-05-19 02:41:48 +00001831 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001832 default:
anthonyc1061722010-05-14 06:23:49 +00001833 {
anthony46a369d2010-05-19 02:41:48 +00001834 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1835 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001836 if (kernel == (KernelInfo *) NULL)
1837 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001838 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001839 break;
1840 }
anthony602ab9b2010-01-05 08:06:50 +00001841 break;
1842 }
1843
1844 return(kernel);
1845}
anthonyc94cdb02010-01-06 08:15:29 +00001846
anthony602ab9b2010-01-05 08:06:50 +00001847/*
1848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1849% %
1850% %
1851% %
cristy6771f1e2010-03-05 19:43:39 +00001852% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001853% %
1854% %
1855% %
1856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1857%
anthony1b2bc0a2010-05-12 05:25:22 +00001858% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1859% can be modified without effecting the original. The cloned kernel should
1860% be destroyed using DestoryKernelInfo() when no longer needed.
anthony7a01dcf2010-05-11 12:25:52 +00001861%
cristye6365592010-04-02 17:31:23 +00001862% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001863%
anthony930be612010-02-08 04:26:15 +00001864% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001865%
1866% A description of each parameter follows:
1867%
1868% o kernel: the Morphology/Convolution kernel to be cloned
1869%
1870*/
cristyef656912010-03-05 19:54:59 +00001871MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001872{
1873 register long
1874 i;
1875
cristy19eb6412010-04-23 14:42:29 +00001876 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001877 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001878
1879 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001880 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1881 if (new_kernel == (KernelInfo *) NULL)
1882 return(new_kernel);
1883 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001884
1885 /* replace the values with a copy of the values */
1886 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001887 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001888 if (new_kernel->values == (double *) NULL)
1889 return(DestroyKernelInfo(new_kernel));
anthony4fd27e22010-02-07 08:17:18 +00001890 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001891 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001892
1893 /* Also clone the next kernel in the kernel list */
1894 if ( kernel->next != (KernelInfo *) NULL ) {
1895 new_kernel->next = CloneKernelInfo(kernel->next);
1896 if ( new_kernel->next == (KernelInfo *) NULL )
1897 return(DestroyKernelInfo(new_kernel));
1898 }
1899
anthony7a01dcf2010-05-11 12:25:52 +00001900 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001901}
1902
1903/*
1904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1905% %
1906% %
1907% %
anthony83ba99b2010-01-24 08:48:15 +00001908% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001909% %
1910% %
1911% %
1912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1913%
anthony83ba99b2010-01-24 08:48:15 +00001914% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1915% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001916%
anthony83ba99b2010-01-24 08:48:15 +00001917% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001918%
anthony83ba99b2010-01-24 08:48:15 +00001919% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001920%
1921% A description of each parameter follows:
1922%
1923% o kernel: the Morphology/Convolution kernel to be destroyed
1924%
1925*/
1926
anthony83ba99b2010-01-24 08:48:15 +00001927MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001928{
cristy2be15382010-01-21 02:38:03 +00001929 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001930
anthony7a01dcf2010-05-11 12:25:52 +00001931 if ( kernel->next != (KernelInfo *) NULL )
1932 kernel->next = DestroyKernelInfo(kernel->next);
1933
1934 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1935 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001936 return(kernel);
1937}
anthonyc94cdb02010-01-06 08:15:29 +00001938
1939/*
1940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1941% %
1942% %
1943% %
anthony3c10fc82010-05-13 02:40:51 +00001944% E x p a n d K e r n e l I n f o %
1945% %
1946% %
1947% %
1948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1949%
1950% ExpandKernelInfo() takes a single kernel, and expands it into a list
1951% of kernels each incrementally rotated the angle given.
1952%
1953% WARNING: 45 degree rotations only works for 3x3 kernels.
1954% While 90 degree roatations only works for linear and square kernels
1955%
1956% The format of the RotateKernelInfo method is:
1957%
1958% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1959%
1960% A description of each parameter follows:
1961%
1962% o kernel: the Morphology/Convolution kernel
1963%
1964% o angle: angle to rotate in degrees
1965%
1966% This function is only internel to this module, as it is not finalized,
1967% especially with regard to non-orthogonal angles, and rotation of larger
1968% 2D kernels.
1969*/
anthony47f5d062010-05-23 07:47:50 +00001970
1971/* Internal Routine - Return true if two kernels are the same */
1972static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1973 const KernelInfo *kernel2)
1974{
1975 register unsigned long
1976 i;
anthony1d45eb92010-05-25 11:13:23 +00001977
1978 /* check size and origin location */
1979 if ( kernel1->width != kernel2->width
1980 || kernel1->height != kernel2->height
1981 || kernel1->x != kernel2->x
1982 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001983 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001984
1985 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001986 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001987 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001988 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1989 return MagickFalse;
1990 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
1991 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001992 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00001993 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
1994 return MagickFalse;
1995 }
anthony1d45eb92010-05-25 11:13:23 +00001996
anthony47f5d062010-05-23 07:47:50 +00001997 return MagickTrue;
1998}
1999
2000static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002001{
2002 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002003 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002004 *last;
cristya9a61ad2010-05-13 12:47:41 +00002005
anthony3c10fc82010-05-13 02:40:51 +00002006 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002007 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002008 clone = CloneKernelInfo(last);
2009 RotateKernelInfo(clone, angle);
2010 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002011 break;
cristy84d9b552010-05-24 18:23:54 +00002012 last->next = clone;
2013 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002014 }
cristy84d9b552010-05-24 18:23:54 +00002015 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002016 return;
anthony3c10fc82010-05-13 02:40:51 +00002017}
2018
2019/*
2020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021% %
2022% %
2023% %
anthony46a369d2010-05-19 02:41:48 +00002024+ C a l c M e t a K e r n a l I n f o %
2025% %
2026% %
2027% %
2028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029%
2030% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2031% using the kernel values. This should only ne used if it is not posible to
2032% calculate that meta-data in some easier way.
2033%
2034% It is important that the meta-data is correct before ScaleKernelInfo() is
2035% used to perform kernel normalization.
2036%
2037% The format of the CalcKernelMetaData method is:
2038%
2039% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2040%
2041% A description of each parameter follows:
2042%
2043% o kernel: the Morphology/Convolution kernel to modify
2044%
2045% WARNING: Minimum and Maximum values are assumed to include zero, even if
2046% zero is not part of the kernel (as in Gaussian Derived kernels). This
2047% however is not true for flat-shaped morphological kernels.
2048%
2049% WARNING: Only the specific kernel pointed to is modified, not a list of
2050% multiple kernels.
2051%
2052% This is an internal function and not expected to be useful outside this
2053% module. This could change however.
2054*/
2055static void CalcKernelMetaData(KernelInfo *kernel)
2056{
2057 register unsigned long
2058 i;
2059
2060 kernel->minimum = kernel->maximum = 0.0;
2061 kernel->negative_range = kernel->positive_range = 0.0;
2062 for (i=0; i < (kernel->width*kernel->height); i++)
2063 {
2064 if ( fabs(kernel->values[i]) < MagickEpsilon )
2065 kernel->values[i] = 0.0;
2066 ( kernel->values[i] < 0)
2067 ? ( kernel->negative_range += kernel->values[i] )
2068 : ( kernel->positive_range += kernel->values[i] );
2069 Minimize(kernel->minimum, kernel->values[i]);
2070 Maximize(kernel->maximum, kernel->values[i]);
2071 }
2072
2073 return;
2074}
2075
2076/*
2077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078% %
2079% %
2080% %
anthony9eb4f742010-05-18 02:45:54 +00002081% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002082% %
2083% %
2084% %
2085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2086%
anthony9eb4f742010-05-18 02:45:54 +00002087% MorphologyApply() applies a morphological method, multiple times using
2088% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002089%
anthony9eb4f742010-05-18 02:45:54 +00002090% It is basically equivelent to as MorphologyImageChannel() (see below) but
2091% without user controls, that that function extracts and applies to kernels
2092% and morphology methods.
2093%
2094% More specifically kernels are not normalized/scaled/blended by the
2095% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2096% (-bias setting or image->bias) is passed directly to this function,
2097% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002098%
anthony47f5d062010-05-23 07:47:50 +00002099% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002100%
anthony9eb4f742010-05-18 02:45:54 +00002101% Image *MorphologyApply(const Image *image,MorphologyMethod method,
anthony47f5d062010-05-23 07:47:50 +00002102% const long iterations,const KernelInfo *kernel,
2103% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002104% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002105%
2106% A description of each parameter follows:
2107%
2108% o image: the image.
2109%
2110% o method: the morphology method to be applied.
2111%
2112% o iterations: apply the operation this many times (or no change).
2113% A value of -1 means loop until no change found.
2114% How this is applied may depend on the morphology method.
2115% Typically this is a value of 1.
2116%
2117% o channel: the channel type.
2118%
2119% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002120% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002121%
anthony47f5d062010-05-23 07:47:50 +00002122% o compose: How to handle or merge multi-kernel results.
2123% If 'Undefined' use default of the Morphology method.
2124% If 'No' force image to be re-iterated by each kernel.
2125% Otherwise merge the results using the mathematical compose
2126% method given.
2127%
2128% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002129%
anthony602ab9b2010-01-05 08:06:50 +00002130% o exception: return any errors or warnings in this structure.
2131%
anthony602ab9b2010-01-05 08:06:50 +00002132*/
2133
anthony930be612010-02-08 04:26:15 +00002134
anthony9eb4f742010-05-18 02:45:54 +00002135/* Apply a Morphology Primative to an image using the given kernel.
2136** Two pre-created images must be provided, no image is created.
2137** Returning the number of pixels that changed.
2138*/
anthony46a369d2010-05-19 02:41:48 +00002139static unsigned long MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002140 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002141 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002142{
cristy2be15382010-01-21 02:38:03 +00002143#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002144
2145 long
cristy150989e2010-02-01 14:59:39 +00002146 progress,
anthony29188a82010-01-22 10:12:34 +00002147 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002148 changed;
2149
2150 MagickBooleanType
2151 status;
2152
anthony602ab9b2010-01-05 08:06:50 +00002153 CacheView
2154 *p_view,
2155 *q_view;
2156
anthony602ab9b2010-01-05 08:06:50 +00002157 status=MagickTrue;
2158 changed=0;
2159 progress=0;
2160
anthony602ab9b2010-01-05 08:06:50 +00002161 p_view=AcquireCacheView(image);
2162 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002163
anthonycc6c8362010-01-25 04:14:01 +00002164 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002165 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002166 */
cristyc99304f2010-02-01 15:26:27 +00002167 offx = kernel->x;
2168 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002169 switch(method) {
anthony930be612010-02-08 04:26:15 +00002170 case ConvolveMorphology:
2171 case DilateMorphology:
2172 case DilateIntensityMorphology:
2173 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002174 /* kernel needs to used with reflection about origin */
cristy150989e2010-02-01 14:59:39 +00002175 offx = (long) kernel->width-offx-1;
2176 offy = (long) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002177 break;
anthony5ef8e942010-05-11 06:51:12 +00002178 case ErodeMorphology:
2179 case ErodeIntensityMorphology:
2180 case HitAndMissMorphology:
2181 case ThinningMorphology:
2182 case ThickenMorphology:
2183 /* kernel is user as is, without reflection */
2184 break;
anthony930be612010-02-08 04:26:15 +00002185 default:
anthony9eb4f742010-05-18 02:45:54 +00002186 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002187 break;
anthony29188a82010-01-22 10:12:34 +00002188 }
2189
anthony602ab9b2010-01-05 08:06:50 +00002190#if defined(MAGICKCORE_OPENMP_SUPPORT)
2191 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2192#endif
cristy150989e2010-02-01 14:59:39 +00002193 for (y=0; y < (long) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002194 {
2195 MagickBooleanType
2196 sync;
2197
2198 register const PixelPacket
2199 *restrict p;
2200
2201 register const IndexPacket
2202 *restrict p_indexes;
2203
2204 register PixelPacket
2205 *restrict q;
2206
2207 register IndexPacket
2208 *restrict q_indexes;
2209
cristy150989e2010-02-01 14:59:39 +00002210 register long
anthony602ab9b2010-01-05 08:06:50 +00002211 x;
2212
anthony29188a82010-01-22 10:12:34 +00002213 unsigned long
anthony602ab9b2010-01-05 08:06:50 +00002214 r;
2215
2216 if (status == MagickFalse)
2217 continue;
anthony29188a82010-01-22 10:12:34 +00002218 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2219 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002220 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2221 exception);
2222 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2223 {
2224 status=MagickFalse;
2225 continue;
2226 }
2227 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2228 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002229 r = (image->columns+kernel->width)*offy+offx; /* constant */
2230
cristy150989e2010-02-01 14:59:39 +00002231 for (x=0; x < (long) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002232 {
cristy150989e2010-02-01 14:59:39 +00002233 long
anthony602ab9b2010-01-05 08:06:50 +00002234 v;
2235
cristy150989e2010-02-01 14:59:39 +00002236 register long
anthony602ab9b2010-01-05 08:06:50 +00002237 u;
2238
2239 register const double
2240 *restrict k;
2241
2242 register const PixelPacket
2243 *restrict k_pixels;
2244
2245 register const IndexPacket
2246 *restrict k_indexes;
2247
2248 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002249 result,
2250 min,
2251 max;
anthony602ab9b2010-01-05 08:06:50 +00002252
anthony29188a82010-01-22 10:12:34 +00002253 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002254 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002255 */
anthony602ab9b2010-01-05 08:06:50 +00002256 *q = p[r];
2257 if (image->colorspace == CMYKColorspace)
2258 q_indexes[x] = p_indexes[r];
2259
anthony5ef8e942010-05-11 06:51:12 +00002260 /* Defaults */
2261 min.red =
2262 min.green =
2263 min.blue =
2264 min.opacity =
2265 min.index = (MagickRealType) QuantumRange;
2266 max.red =
2267 max.green =
2268 max.blue =
2269 max.opacity =
2270 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002271 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002272 result.red = (MagickRealType) p[r].red;
2273 result.green = (MagickRealType) p[r].green;
2274 result.blue = (MagickRealType) p[r].blue;
2275 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002276 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002277 if ( image->colorspace == CMYKColorspace)
2278 result.index = (MagickRealType) p_indexes[r];
2279
anthony602ab9b2010-01-05 08:06:50 +00002280 switch (method) {
2281 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002282 /* Set the user defined bias of the weighted average output */
2283 result.red =
2284 result.green =
2285 result.blue =
2286 result.opacity =
2287 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002288 break;
anthony4fd27e22010-02-07 08:17:18 +00002289 case DilateIntensityMorphology:
2290 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002291 /* use a boolean flag indicating when first match found */
2292 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002293 break;
anthony602ab9b2010-01-05 08:06:50 +00002294 default:
anthony602ab9b2010-01-05 08:06:50 +00002295 break;
2296 }
2297
2298 switch ( method ) {
2299 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002300 /* Weighted Average of pixels using reflected kernel
2301 **
2302 ** NOTE for correct working of this operation for asymetrical
2303 ** kernels, the kernel needs to be applied in its reflected form.
2304 ** That is its values needs to be reversed.
2305 **
2306 ** Correlation is actually the same as this but without reflecting
2307 ** the kernel, and thus 'lower-level' that Convolution. However
2308 ** as Convolution is the more common method used, and it does not
2309 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002310 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002311 **
2312 ** Correlation will have its kernel reflected before calling
2313 ** this function to do a Convolve.
2314 **
2315 ** For more details of Correlation vs Convolution see
2316 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2317 */
anthony5ef8e942010-05-11 06:51:12 +00002318 if (((channel & SyncChannels) != 0 ) &&
2319 (image->matte == MagickTrue))
2320 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2321 ** Weight the color channels with Alpha Channel so that
2322 ** transparent pixels are not part of the results.
2323 */
anthony602ab9b2010-01-05 08:06:50 +00002324 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002325 alpha, /* color channel weighting : kernel*alpha */
2326 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002327
2328 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002329 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002330 k_pixels = p;
2331 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002332 for (v=0; v < (long) kernel->height; v++) {
2333 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002334 if ( IsNan(*k) ) continue;
2335 alpha=(*k)*(QuantumScale*(QuantumRange-
2336 k_pixels[u].opacity));
2337 gamma += alpha;
2338 result.red += alpha*k_pixels[u].red;
2339 result.green += alpha*k_pixels[u].green;
2340 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002341 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002342 if ( image->colorspace == CMYKColorspace)
2343 result.index += alpha*k_indexes[u];
2344 }
2345 k_pixels += image->columns+kernel->width;
2346 k_indexes += image->columns+kernel->width;
2347 }
2348 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002349 result.red *= gamma;
2350 result.green *= gamma;
2351 result.blue *= gamma;
2352 result.opacity *= gamma;
2353 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002354 }
anthony5ef8e942010-05-11 06:51:12 +00002355 else
2356 {
2357 /* No 'Sync' flag, or no Alpha involved.
2358 ** Convolution is simple individual channel weigthed sum.
2359 */
2360 k = &kernel->values[ kernel->width*kernel->height-1 ];
2361 k_pixels = p;
2362 k_indexes = p_indexes;
2363 for (v=0; v < (long) kernel->height; v++) {
2364 for (u=0; u < (long) kernel->width; u++, k--) {
2365 if ( IsNan(*k) ) continue;
2366 result.red += (*k)*k_pixels[u].red;
2367 result.green += (*k)*k_pixels[u].green;
2368 result.blue += (*k)*k_pixels[u].blue;
2369 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2370 if ( image->colorspace == CMYKColorspace)
2371 result.index += (*k)*k_indexes[u];
2372 }
2373 k_pixels += image->columns+kernel->width;
2374 k_indexes += image->columns+kernel->width;
2375 }
2376 }
anthony602ab9b2010-01-05 08:06:50 +00002377 break;
2378
anthony4fd27e22010-02-07 08:17:18 +00002379 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002380 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002381 **
2382 ** NOTE that the kernel is not reflected for this operation!
2383 **
2384 ** NOTE: in normal Greyscale Morphology, the kernel value should
2385 ** be added to the real value, this is currently not done, due to
2386 ** the nature of the boolean kernels being used.
2387 */
anthony4fd27e22010-02-07 08:17:18 +00002388 k = kernel->values;
2389 k_pixels = p;
2390 k_indexes = p_indexes;
2391 for (v=0; v < (long) kernel->height; v++) {
2392 for (u=0; u < (long) kernel->width; u++, k++) {
2393 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002394 Minimize(min.red, (double) k_pixels[u].red);
2395 Minimize(min.green, (double) k_pixels[u].green);
2396 Minimize(min.blue, (double) k_pixels[u].blue);
2397 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002398 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002399 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002400 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002401 }
2402 k_pixels += image->columns+kernel->width;
2403 k_indexes += image->columns+kernel->width;
2404 }
2405 break;
2406
anthony5ef8e942010-05-11 06:51:12 +00002407
anthony83ba99b2010-01-24 08:48:15 +00002408 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002409 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002410 **
2411 ** NOTE for correct working of this operation for asymetrical
2412 ** kernels, the kernel needs to be applied in its reflected form.
2413 ** That is its values needs to be reversed.
2414 **
2415 ** NOTE: in normal Greyscale Morphology, the kernel value should
2416 ** be added to the real value, this is currently not done, due to
2417 ** the nature of the boolean kernels being used.
2418 **
2419 */
anthony29188a82010-01-22 10:12:34 +00002420 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002421 k_pixels = p;
2422 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002423 for (v=0; v < (long) kernel->height; v++) {
2424 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002425 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002426 Maximize(max.red, (double) k_pixels[u].red);
2427 Maximize(max.green, (double) k_pixels[u].green);
2428 Maximize(max.blue, (double) k_pixels[u].blue);
2429 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002430 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002431 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002432 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002433 }
2434 k_pixels += image->columns+kernel->width;
2435 k_indexes += image->columns+kernel->width;
2436 }
anthony602ab9b2010-01-05 08:06:50 +00002437 break;
2438
anthony5ef8e942010-05-11 06:51:12 +00002439 case HitAndMissMorphology:
2440 case ThinningMorphology:
2441 case ThickenMorphology:
2442 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2443 **
2444 ** NOTE that the kernel is not reflected for this operation,
2445 ** and consists of both foreground and background pixel
2446 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2447 ** with either Nan or 0.5 values for don't care.
2448 **
2449 ** Note that this can produce negative results, though really
2450 ** only a positive match has any real value.
2451 */
2452 k = kernel->values;
2453 k_pixels = p;
2454 k_indexes = p_indexes;
2455 for (v=0; v < (long) kernel->height; v++) {
2456 for (u=0; u < (long) kernel->width; u++, k++) {
2457 if ( IsNan(*k) ) continue;
2458 if ( (*k) > 0.7 )
2459 { /* minimim of foreground pixels */
2460 Minimize(min.red, (double) k_pixels[u].red);
2461 Minimize(min.green, (double) k_pixels[u].green);
2462 Minimize(min.blue, (double) k_pixels[u].blue);
2463 Minimize(min.opacity,
2464 QuantumRange-(double) k_pixels[u].opacity);
2465 if ( image->colorspace == CMYKColorspace)
2466 Minimize(min.index, (double) k_indexes[u]);
2467 }
2468 else if ( (*k) < 0.3 )
2469 { /* maximum of background pixels */
2470 Maximize(max.red, (double) k_pixels[u].red);
2471 Maximize(max.green, (double) k_pixels[u].green);
2472 Maximize(max.blue, (double) k_pixels[u].blue);
2473 Maximize(max.opacity,
2474 QuantumRange-(double) k_pixels[u].opacity);
2475 if ( image->colorspace == CMYKColorspace)
2476 Maximize(max.index, (double) k_indexes[u]);
2477 }
2478 }
2479 k_pixels += image->columns+kernel->width;
2480 k_indexes += image->columns+kernel->width;
2481 }
2482 /* Pattern Match only if min fg larger than min bg pixels */
2483 min.red -= max.red; Maximize( min.red, 0.0 );
2484 min.green -= max.green; Maximize( min.green, 0.0 );
2485 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2486 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2487 min.index -= max.index; Maximize( min.index, 0.0 );
2488 break;
2489
anthony4fd27e22010-02-07 08:17:18 +00002490 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002491 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2492 **
2493 ** WARNING: the intensity test fails for CMYK and does not
2494 ** take into account the moderating effect of teh alpha channel
2495 ** on the intensity.
2496 **
2497 ** NOTE that the kernel is not reflected for this operation!
2498 */
anthony602ab9b2010-01-05 08:06:50 +00002499 k = kernel->values;
2500 k_pixels = p;
2501 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002502 for (v=0; v < (long) kernel->height; v++) {
2503 for (u=0; u < (long) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002504 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002505 if ( result.red == 0.0 ||
2506 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2507 /* copy the whole pixel - no channel selection */
2508 *q = k_pixels[u];
2509 if ( result.red > 0.0 ) changed++;
2510 result.red = 1.0;
2511 }
anthony602ab9b2010-01-05 08:06:50 +00002512 }
2513 k_pixels += image->columns+kernel->width;
2514 k_indexes += image->columns+kernel->width;
2515 }
anthony602ab9b2010-01-05 08:06:50 +00002516 break;
2517
anthony83ba99b2010-01-24 08:48:15 +00002518 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002519 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2520 **
2521 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002522 ** take into account the moderating effect of the alpha channel
2523 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002524 **
2525 ** NOTE for correct working of this operation for asymetrical
2526 ** kernels, the kernel needs to be applied in its reflected form.
2527 ** That is its values needs to be reversed.
2528 */
anthony29188a82010-01-22 10:12:34 +00002529 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002530 k_pixels = p;
2531 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002532 for (v=0; v < (long) kernel->height; v++) {
2533 for (u=0; u < (long) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002534 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2535 if ( result.red == 0.0 ||
2536 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2537 /* copy the whole pixel - no channel selection */
2538 *q = k_pixels[u];
2539 if ( result.red > 0.0 ) changed++;
2540 result.red = 1.0;
2541 }
anthony602ab9b2010-01-05 08:06:50 +00002542 }
2543 k_pixels += image->columns+kernel->width;
2544 k_indexes += image->columns+kernel->width;
2545 }
anthony602ab9b2010-01-05 08:06:50 +00002546 break;
2547
anthony5ef8e942010-05-11 06:51:12 +00002548
anthony602ab9b2010-01-05 08:06:50 +00002549 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002550 /* Add kernel Value and select the minimum value found.
2551 ** The result is a iterative distance from edge of image shape.
2552 **
2553 ** All Distance Kernels are symetrical, but that may not always
2554 ** be the case. For example how about a distance from left edges?
2555 ** To work correctly with asymetrical kernels the reflected kernel
2556 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002557 **
2558 ** Actually this is really a GreyErode with a negative kernel!
2559 **
anthony930be612010-02-08 04:26:15 +00002560 */
anthony29188a82010-01-22 10:12:34 +00002561 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002562 k_pixels = p;
2563 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002564 for (v=0; v < (long) kernel->height; v++) {
2565 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002566 if ( IsNan(*k) ) continue;
2567 Minimize(result.red, (*k)+k_pixels[u].red);
2568 Minimize(result.green, (*k)+k_pixels[u].green);
2569 Minimize(result.blue, (*k)+k_pixels[u].blue);
2570 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2571 if ( image->colorspace == CMYKColorspace)
2572 Minimize(result.index, (*k)+k_indexes[u]);
2573 }
2574 k_pixels += image->columns+kernel->width;
2575 k_indexes += image->columns+kernel->width;
2576 }
anthony602ab9b2010-01-05 08:06:50 +00002577 break;
2578
2579 case UndefinedMorphology:
2580 default:
2581 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002582 }
anthony5ef8e942010-05-11 06:51:12 +00002583 /* Final mathematics of results (combine with original image?)
2584 **
2585 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2586 ** be done here but works better with iteration as a image difference
2587 ** in the controling function (below). Thicken and Thinning however
2588 ** should be done here so thay can be iterated correctly.
2589 */
2590 switch ( method ) {
2591 case HitAndMissMorphology:
2592 case ErodeMorphology:
2593 result = min; /* minimum of neighbourhood */
2594 break;
2595 case DilateMorphology:
2596 result = max; /* maximum of neighbourhood */
2597 break;
2598 case ThinningMorphology:
2599 /* subtract pattern match from original */
2600 result.red -= min.red;
2601 result.green -= min.green;
2602 result.blue -= min.blue;
2603 result.opacity -= min.opacity;
2604 result.index -= min.index;
2605 break;
2606 case ThickenMorphology:
2607 /* Union with original image (maximize) - or should this be + */
2608 Maximize( result.red, min.red );
2609 Maximize( result.green, min.green );
2610 Maximize( result.blue, min.blue );
2611 Maximize( result.opacity, min.opacity );
2612 Maximize( result.index, min.index );
2613 break;
2614 default:
2615 /* result directly calculated or assigned */
2616 break;
2617 }
2618 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002619 switch ( method ) {
2620 case UndefinedMorphology:
2621 case DilateIntensityMorphology:
2622 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002623 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002624 default:
anthony83ba99b2010-01-24 08:48:15 +00002625 if ((channel & RedChannel) != 0)
2626 q->red = ClampToQuantum(result.red);
2627 if ((channel & GreenChannel) != 0)
2628 q->green = ClampToQuantum(result.green);
2629 if ((channel & BlueChannel) != 0)
2630 q->blue = ClampToQuantum(result.blue);
2631 if ((channel & OpacityChannel) != 0
2632 && image->matte == MagickTrue )
2633 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2634 if ((channel & IndexChannel) != 0
2635 && image->colorspace == CMYKColorspace)
2636 q_indexes[x] = ClampToQuantum(result.index);
2637 break;
2638 }
anthony5ef8e942010-05-11 06:51:12 +00002639 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002640 if ( ( p[r].red != q->red )
2641 || ( p[r].green != q->green )
2642 || ( p[r].blue != q->blue )
2643 || ( p[r].opacity != q->opacity )
2644 || ( image->colorspace == CMYKColorspace &&
2645 p_indexes[r] != q_indexes[x] ) )
2646 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002647 p++;
2648 q++;
anthony83ba99b2010-01-24 08:48:15 +00002649 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002650 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2651 if (sync == MagickFalse)
2652 status=MagickFalse;
2653 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2654 {
2655 MagickBooleanType
2656 proceed;
2657
2658#if defined(MAGICKCORE_OPENMP_SUPPORT)
2659 #pragma omp critical (MagickCore_MorphologyImage)
2660#endif
2661 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2662 if (proceed == MagickFalse)
2663 status=MagickFalse;
2664 }
anthony83ba99b2010-01-24 08:48:15 +00002665 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002666 result_image->type=image->type;
2667 q_view=DestroyCacheView(q_view);
2668 p_view=DestroyCacheView(p_view);
cristy150989e2010-02-01 14:59:39 +00002669 return(status ? (unsigned long) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002670}
2671
anthony4fd27e22010-02-07 08:17:18 +00002672
anthony9eb4f742010-05-18 02:45:54 +00002673MagickExport Image *MorphologyApply(const Image *image, const ChannelType
2674 channel,const MorphologyMethod method, const long iterations,
anthony47f5d062010-05-23 07:47:50 +00002675 const KernelInfo *kernel, const CompositeOperator compose,
2676 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002677{
2678 Image
anthony47f5d062010-05-23 07:47:50 +00002679 *curr_image, /* Image we are working with or iterating */
2680 *work_image, /* secondary image for primative iteration */
2681 *save_image, /* saved image - for 'edge' method only */
2682 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002683
anthony4fd27e22010-02-07 08:17:18 +00002684 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002685 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2686 *norm_kernel, /* the current normal un-reflected kernel */
2687 *rflt_kernel, /* the current reflected kernel (if needed) */
2688 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002689
2690 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002691 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002692
2693 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002694 rslt_compose; /* multi-kernel compose method for results to use */
2695
2696 MagickBooleanType
2697 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002698
anthony1b2bc0a2010-05-12 05:25:22 +00002699 unsigned long
anthony47f5d062010-05-23 07:47:50 +00002700 method_loop, /* Loop 1: number of compound method iterations */
2701 method_limit, /* maximum number of compound method iterations */
2702 kernel_number, /* Loop 2: the kernel number being applied */
2703 stage_loop, /* Loop 3: primative loop for compound morphology */
2704 stage_limit, /* how many primatives in this compound */
2705 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2706 kernel_limit, /* number of times to iterate kernel */
2707 count, /* total count of primative steps applied */
2708 changed, /* number pixels changed by last primative operation */
2709 kernel_changed, /* total count of changed using iterated kernel */
2710 method_changed; /* total count of changed over method iteration */
2711
2712 char
2713 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002714
anthony602ab9b2010-01-05 08:06:50 +00002715 assert(image != (Image *) NULL);
2716 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002717 assert(kernel != (KernelInfo *) NULL);
2718 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002719 assert(exception != (ExceptionInfo *) NULL);
2720 assert(exception->signature == MagickSignature);
2721
anthonyc3e48252010-05-24 12:43:11 +00002722 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002723 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002724 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002725
anthony47f5d062010-05-23 07:47:50 +00002726 kernel_limit = (unsigned long) iterations;
2727 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2728 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002729
cristye96405a2010-05-19 02:24:31 +00002730 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2731 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002732
anthony9eb4f742010-05-18 02:45:54 +00002733 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002734 curr_image = (Image *) image;
2735 work_image = save_image = rslt_image = (Image *) NULL;
2736 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002737
anthony47f5d062010-05-23 07:47:50 +00002738 /* Initialize specific methods
2739 * + which loop should use the given iteratations
2740 * + how many primatives make up the compound morphology
2741 * + multi-kernel compose method to use (by default)
2742 */
2743 method_limit = 1; /* just do method once, unless otherwise set */
2744 stage_limit = 1; /* assume method is not a compount */
2745 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002746 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002747 case SmoothMorphology: /* 4 primative compound morphology */
2748 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002749 break;
anthony47f5d062010-05-23 07:47:50 +00002750 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002751 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002752 case TopHatMorphology:
2753 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002754 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002755 case BottomHatMorphology:
2756 case EdgeMorphology:
2757 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002758 break;
2759 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002760 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002761 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002762 break;
anthonyc3e48252010-05-24 12:43:11 +00002763 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002764 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002765 case DistanceMorphology:
2766 method_limit = kernel_limit; /* iterate method with each kernel */
2767 kernel_limit = 1; /* do not do kernel iteration */
2768 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002769 break;
2770 default:
anthony930be612010-02-08 04:26:15 +00002771 break;
anthony602ab9b2010-01-05 08:06:50 +00002772 }
2773
anthonyc3e48252010-05-24 12:43:11 +00002774 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002775 if ( compose != UndefinedCompositeOp )
2776 rslt_compose = compose; /* override default composition for method */
2777 if ( rslt_compose == UndefinedCompositeOp )
2778 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2779
anthonyc3e48252010-05-24 12:43:11 +00002780 /* Some methods require a reflected kernel to use with primatives.
2781 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002782 switch ( method ) {
2783 case CorrelateMorphology:
2784 case CloseMorphology:
2785 case CloseIntensityMorphology:
2786 case BottomHatMorphology:
2787 case SmoothMorphology:
2788 reflected_kernel = CloneKernelInfo(kernel);
2789 if (reflected_kernel == (KernelInfo *) NULL)
2790 goto error_cleanup;
2791 RotateKernelInfo(reflected_kernel,180);
2792 break;
2793 default:
2794 break;
anthony9eb4f742010-05-18 02:45:54 +00002795 }
anthony7a01dcf2010-05-11 12:25:52 +00002796
anthony47f5d062010-05-23 07:47:50 +00002797 /* Loop 1: iterate the compound method */
2798 method_loop = 0;
2799 method_changed = 1;
2800 while ( method_loop < method_limit && method_changed > 0 ) {
2801 method_loop++;
2802 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002803
anthony47f5d062010-05-23 07:47:50 +00002804 /* Loop 2: iterate over each kernel in a multi-kernel list */
2805 norm_kernel = (KernelInfo *) kernel;
2806 rflt_kernel = reflected_kernel;
2807 kernel_number = 0;
2808 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002809
anthony47f5d062010-05-23 07:47:50 +00002810 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2811 stage_loop = 0; /* the compound morphology stage number */
2812 while ( stage_loop < stage_limit ) {
2813 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002814
anthony47f5d062010-05-23 07:47:50 +00002815 /* Select primative morphology for this stage of compound method */
2816 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002817 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002818 switch( method ) {
2819 case ErodeMorphology: /* just erode */
2820 case EdgeInMorphology: /* erode and image difference */
2821 primative = ErodeMorphology;
2822 break;
2823 case DilateMorphology: /* just dilate */
2824 case EdgeOutMorphology: /* dilate and image difference */
2825 primative = DilateMorphology;
2826 break;
2827 case OpenMorphology: /* erode then dialate */
2828 case TopHatMorphology: /* open and image difference */
2829 primative = ErodeMorphology;
2830 if ( stage_loop == 2 )
2831 primative = DilateMorphology;
2832 break;
2833 case OpenIntensityMorphology:
2834 primative = ErodeIntensityMorphology;
2835 if ( stage_loop == 2 )
2836 primative = DilateIntensityMorphology;
2837 case CloseMorphology: /* dilate, then erode */
2838 case BottomHatMorphology: /* close and image difference */
2839 this_kernel = rflt_kernel; /* use the reflected kernel */
2840 primative = DilateMorphology;
2841 if ( stage_loop == 2 )
2842 primative = ErodeMorphology;
2843 break;
2844 case CloseIntensityMorphology:
2845 this_kernel = rflt_kernel; /* use the reflected kernel */
2846 primative = DilateIntensityMorphology;
2847 if ( stage_loop == 2 )
2848 primative = ErodeIntensityMorphology;
2849 break;
2850 case SmoothMorphology: /* open, close */
2851 switch ( stage_loop ) {
2852 case 1: /* start an open method, which starts with Erode */
2853 primative = ErodeMorphology;
2854 break;
2855 case 2: /* now Dilate the Erode */
2856 primative = DilateMorphology;
2857 break;
2858 case 3: /* Reflect kernel a close */
2859 this_kernel = rflt_kernel; /* use the reflected kernel */
2860 primative = DilateMorphology;
2861 break;
2862 case 4: /* Finish the Close */
2863 this_kernel = rflt_kernel; /* use the reflected kernel */
2864 primative = ErodeMorphology;
2865 break;
2866 }
2867 break;
2868 case EdgeMorphology: /* dilate and erode difference */
2869 primative = DilateMorphology;
2870 if ( stage_loop == 2 ) {
2871 save_image = curr_image; /* save the image difference */
2872 curr_image = (Image *) image;
2873 primative = ErodeMorphology;
2874 }
2875 break;
2876 case CorrelateMorphology:
2877 /* A Correlation is a Convolution with a reflected kernel.
2878 ** However a Convolution is a weighted sum using a reflected
2879 ** kernel. It may seem stange to convert a Correlation into a
2880 ** Convolution as the Correlation is the simplier method, but
2881 ** Convolution is much more commonly used, and it makes sense to
2882 ** implement it directly so as to avoid the need to duplicate the
2883 ** kernel when it is not required (which is typically the
2884 ** default).
2885 */
2886 this_kernel = rflt_kernel; /* use the reflected kernel */
2887 primative = ConvolveMorphology;
2888 break;
2889 default:
anthony47f5d062010-05-23 07:47:50 +00002890 break;
2891 }
anthony9eb4f742010-05-18 02:45:54 +00002892
anthony47f5d062010-05-23 07:47:50 +00002893 /* Extra information for debugging compound operations */
2894 if ( verbose == MagickTrue ) {
2895 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002896 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002897 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2898 method_loop, stage_loop );
2899 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002900 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002901 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2902 method_loop );
2903 else
2904 v_info[0] = '\0';
2905 }
2906
2907 /* Loop 4: Iterate the kernel with primative */
2908 kernel_loop = 0;
2909 kernel_changed = 0;
2910 changed = 1;
2911 while ( kernel_loop < kernel_limit && changed > 0 ) {
2912 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002913
2914 /* Create a destination image, if not yet defined */
2915 if ( work_image == (Image *) NULL )
2916 {
2917 work_image=CloneImage(image,0,0,MagickTrue,exception);
2918 if (work_image == (Image *) NULL)
2919 goto error_cleanup;
2920 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2921 {
2922 InheritException(exception,&work_image->exception);
2923 goto error_cleanup;
2924 }
2925 }
2926
anthony47f5d062010-05-23 07:47:50 +00002927 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002928 count++;
anthony47f5d062010-05-23 07:47:50 +00002929 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002930 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002931 kernel_changed += changed;
2932 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002933
anthony47f5d062010-05-23 07:47:50 +00002934 if ( verbose == MagickTrue ) {
2935 if ( kernel_loop > 1 )
2936 fprintf(stderr, "\n"); /* add end-of-line from previous */
2937 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2938 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2939 ( this_kernel == rflt_kernel ) ? "*" : "",
2940 method_loop+kernel_loop-1, kernel_number, count, changed);
2941 }
anthony9eb4f742010-05-18 02:45:54 +00002942 /* prepare next loop */
2943 { Image *tmp = work_image; /* swap images for iteration */
2944 work_image = curr_image;
2945 curr_image = tmp;
2946 }
2947 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002948 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002949
anthony47f5d062010-05-23 07:47:50 +00002950 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002951
anthony47f5d062010-05-23 07:47:50 +00002952 if ( verbose == MagickTrue && kernel_changed != changed )
2953 fprintf(stderr, " Total %lu", kernel_changed);
2954 if ( verbose == MagickTrue && stage_loop < stage_limit )
2955 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002956
2957#if 0
anthony47f5d062010-05-23 07:47:50 +00002958 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
2959 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
2960 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
2961 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
2962 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002963#endif
2964
anthony47f5d062010-05-23 07:47:50 +00002965 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002966
anthony47f5d062010-05-23 07:47:50 +00002967 /* Final Post-processing for some Compound Methods
2968 **
2969 ** The removal of any 'Sync' channel flag in the Image Compositon
2970 ** below ensures the methematical compose method is applied in a
2971 ** purely mathematical way, and only to the selected channels.
2972 ** Turn off SVG composition 'alpha blending'.
2973 */
2974 switch( method ) {
2975 case EdgeOutMorphology:
2976 case EdgeInMorphology:
2977 case TopHatMorphology:
2978 case BottomHatMorphology:
2979 if ( verbose == MagickTrue )
2980 fprintf(stderr, "\n%s: Difference with original image",
2981 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2982 (void) CompositeImageChannel(curr_image,
2983 (ChannelType) (channel & ~SyncChannels),
2984 DifferenceCompositeOp, image, 0, 0);
2985 break;
2986 case EdgeMorphology:
2987 if ( verbose == MagickTrue )
2988 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
2989 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2990 (void) CompositeImageChannel(curr_image,
2991 (ChannelType) (channel & ~SyncChannels),
2992 DifferenceCompositeOp, save_image, 0, 0);
2993 save_image = DestroyImage(save_image); /* finished with save image */
2994 break;
2995 default:
2996 break;
2997 }
2998
2999 /* multi-kernel handling: re-iterate, or compose results */
3000 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003001 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003002 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003003 { if ( verbose == MagickTrue ) {
3004 if ( this_kernel->next != (KernelInfo *) NULL )
3005 fprintf(stderr, " (re-iterate)");
3006 else
3007 fprintf(stderr, " (done)");
3008 }
3009 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003010 }
anthony47f5d062010-05-23 07:47:50 +00003011 else if ( rslt_image == (Image *) NULL)
3012 { if ( verbose == MagickTrue )
3013 fprintf(stderr, " (save for compose)");
3014 rslt_image = curr_image;
3015 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003016 }
anthony47f5d062010-05-23 07:47:50 +00003017 else
3018 { /* add the new 'current' result to the composition
3019 **
3020 ** The removal of any 'Sync' channel flag in the Image Compositon
3021 ** below ensures the methematical compose method is applied in a
3022 ** purely mathematical way, and only to the selected channels.
3023 ** Turn off SVG composition 'alpha blending'.
3024 */
3025 if ( verbose == MagickTrue )
3026 fprintf(stderr, " (compose \"%s\")",
3027 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3028 (void) CompositeImageChannel(rslt_image,
3029 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3030 curr_image, 0, 0);
3031 curr_image = (Image *) image; /* continue with original image */
3032 }
3033 if ( verbose == MagickTrue )
3034 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003035
anthony47f5d062010-05-23 07:47:50 +00003036 /* loop to the next kernel in a multi-kernel list */
3037 norm_kernel = norm_kernel->next;
3038 if ( rflt_kernel != (KernelInfo *) NULL )
3039 rflt_kernel = rflt_kernel->next;
3040 kernel_number++;
3041 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003042
anthony47f5d062010-05-23 07:47:50 +00003043 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003044
anthony9eb4f742010-05-18 02:45:54 +00003045 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003046
anthony47f5d062010-05-23 07:47:50 +00003047 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003048error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003049 if ( curr_image != (Image *) NULL &&
3050 curr_image != rslt_image &&
3051 curr_image != image )
3052 curr_image = DestroyImage(curr_image);
3053 if ( rslt_image != (Image *) NULL )
3054 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003055exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003056 if ( curr_image != (Image *) NULL &&
3057 curr_image != rslt_image &&
3058 curr_image != image )
3059 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003060 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003061 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003062 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003063 save_image = DestroyImage(save_image);
3064 if ( reflected_kernel != (KernelInfo *) NULL )
3065 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3066 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003067}
3068
3069/*
3070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3071% %
3072% %
3073% %
3074% M o r p h o l o g y I m a g e C h a n n e l %
3075% %
3076% %
3077% %
3078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3079%
3080% MorphologyImageChannel() applies a user supplied kernel to the image
3081% according to the given mophology method.
3082%
3083% This function applies any and all user defined settings before calling
3084% the above internal function MorphologyApply().
3085%
3086% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003087% * Output Bias for Convolution and correlation ("-bias")
3088% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3089% This can also includes the addition of a scaled unity kernel.
3090% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003091%
3092% The format of the MorphologyImage method is:
3093%
3094% Image *MorphologyImage(const Image *image,MorphologyMethod method,
3095% const long iterations,KernelInfo *kernel,ExceptionInfo *exception)
3096%
3097% Image *MorphologyImageChannel(const Image *image, const ChannelType
3098% channel,MorphologyMethod method,const long iterations,
3099% KernelInfo *kernel,ExceptionInfo *exception)
3100%
3101% A description of each parameter follows:
3102%
3103% o image: the image.
3104%
3105% o method: the morphology method to be applied.
3106%
3107% o iterations: apply the operation this many times (or no change).
3108% A value of -1 means loop until no change found.
3109% How this is applied may depend on the morphology method.
3110% Typically this is a value of 1.
3111%
3112% o channel: the channel type.
3113%
3114% o kernel: An array of double representing the morphology kernel.
3115% Warning: kernel may be normalized for the Convolve method.
3116%
3117% o exception: return any errors or warnings in this structure.
3118%
3119*/
3120
3121MagickExport Image *MorphologyImageChannel(const Image *image,
3122 const ChannelType channel,const MorphologyMethod method,
3123 const long iterations,const KernelInfo *kernel,ExceptionInfo *exception)
3124{
3125 const char
3126 *artifact;
3127
3128 KernelInfo
3129 *curr_kernel;
3130
anthony47f5d062010-05-23 07:47:50 +00003131 CompositeOperator
3132 compose;
3133
anthony9eb4f742010-05-18 02:45:54 +00003134 Image
3135 *morphology_image;
3136
3137
anthony46a369d2010-05-19 02:41:48 +00003138 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3139 * This is done BEFORE the ShowKernelInfo() function is called so that
3140 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003141 */
3142 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003143 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003144 {
3145 artifact = GetImageArtifact(image,"convolve:scale");
3146 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003147 if ( curr_kernel == kernel )
3148 curr_kernel = CloneKernelInfo(kernel);
3149 if (curr_kernel == (KernelInfo *) NULL) {
3150 curr_kernel=DestroyKernelInfo(curr_kernel);
3151 return((Image *) NULL);
3152 }
anthony46a369d2010-05-19 02:41:48 +00003153 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003154 }
3155 }
3156
3157 /* display the (normalized) kernel via stderr */
3158 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003159 if ( artifact == (const char *) NULL)
3160 artifact = GetImageArtifact(image,"convolve:showkernel");
3161 if ( artifact == (const char *) NULL)
3162 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003163 if ( artifact != (const char *) NULL)
3164 ShowKernelInfo(curr_kernel);
3165
anthony47f5d062010-05-23 07:47:50 +00003166 /* override the default handling of multi-kernel morphology results
3167 * if 'Undefined' use the default method
3168 * if 'None' (default for 'Convolve') re-iterate previous result
3169 * otherwise merge resulting images using compose method given
3170 */
3171 compose = UndefinedCompositeOp; /* use default for method */
3172 artifact = GetImageArtifact(image,"morphology:compose");
3173 if ( artifact != (const char *) NULL)
3174 compose = (CompositeOperator) ParseMagickOption(
3175 MagickComposeOptions,MagickFalse,artifact);
3176
anthony9eb4f742010-05-18 02:45:54 +00003177 /* Apply the Morphology */
3178 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003179 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003180
3181 /* Cleanup and Exit */
3182 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003183 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003184 return(morphology_image);
3185}
3186
3187MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
3188 method, const long iterations,const KernelInfo *kernel, ExceptionInfo
3189 *exception)
3190{
3191 Image
3192 *morphology_image;
3193
3194 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3195 iterations,kernel,exception);
3196 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003197}
anthony83ba99b2010-01-24 08:48:15 +00003198
3199/*
3200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3201% %
3202% %
3203% %
anthony4fd27e22010-02-07 08:17:18 +00003204+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003205% %
3206% %
3207% %
3208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3209%
anthony46a369d2010-05-19 02:41:48 +00003210% RotateKernelInfo() rotates the kernel by the angle given.
3211%
3212% Currently it is restricted to 90 degree angles, of either 1D kernels
3213% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3214% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003215%
anthony4fd27e22010-02-07 08:17:18 +00003216% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003217%
anthony4fd27e22010-02-07 08:17:18 +00003218% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003219%
3220% A description of each parameter follows:
3221%
3222% o kernel: the Morphology/Convolution kernel
3223%
3224% o angle: angle to rotate in degrees
3225%
anthony46a369d2010-05-19 02:41:48 +00003226% This function is currently internal to this module only, but can be exported
3227% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003228*/
anthony4fd27e22010-02-07 08:17:18 +00003229static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003230{
anthony1b2bc0a2010-05-12 05:25:22 +00003231 /* angle the lower kernels first */
3232 if ( kernel->next != (KernelInfo *) NULL)
3233 RotateKernelInfo(kernel->next, angle);
3234
anthony83ba99b2010-01-24 08:48:15 +00003235 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3236 **
3237 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3238 */
3239
3240 /* Modulus the angle */
3241 angle = fmod(angle, 360.0);
3242 if ( angle < 0 )
3243 angle += 360.0;
3244
anthony3c10fc82010-05-13 02:40:51 +00003245 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003246 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003247
anthony3dd0f622010-05-13 12:57:32 +00003248 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003249 switch (kernel->type) {
3250 /* These built-in kernels are cylindrical kernels, rotating is useless */
3251 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003252 case DOGKernel:
3253 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003254 case PeaksKernel:
3255 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003256 case ChebyshevKernel:
3257 case ManhattenKernel:
3258 case EuclideanKernel:
3259 return;
3260
3261 /* These may be rotatable at non-90 angles in the future */
3262 /* but simply rotating them in multiples of 90 degrees is useless */
3263 case SquareKernel:
3264 case DiamondKernel:
3265 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003266 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003267 return;
3268
3269 /* These only allows a +/-90 degree rotation (by transpose) */
3270 /* A 180 degree rotation is useless */
3271 case BlurKernel:
3272 case RectangleKernel:
3273 if ( 135.0 < angle && angle <= 225.0 )
3274 return;
3275 if ( 225.0 < angle && angle <= 315.0 )
3276 angle -= 180;
3277 break;
3278
anthony3dd0f622010-05-13 12:57:32 +00003279 default:
anthony83ba99b2010-01-24 08:48:15 +00003280 break;
3281 }
anthony3c10fc82010-05-13 02:40:51 +00003282 /* Attempt rotations by 45 degrees */
3283 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3284 {
3285 if ( kernel->width == 3 && kernel->height == 3 )
3286 { /* Rotate a 3x3 square by 45 degree angle */
3287 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003288 kernel->values[0] = kernel->values[3];
3289 kernel->values[3] = kernel->values[6];
3290 kernel->values[6] = kernel->values[7];
3291 kernel->values[7] = kernel->values[8];
3292 kernel->values[8] = kernel->values[5];
3293 kernel->values[5] = kernel->values[2];
3294 kernel->values[2] = kernel->values[1];
3295 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003296 /* rotate non-centered origin */
3297 if ( kernel->x != 1 || kernel->y != 1 ) {
3298 long x,y;
3299 x = (long) kernel->x-1;
3300 y = (long) kernel->y-1;
3301 if ( x == y ) x = 0;
3302 else if ( x == 0 ) x = -y;
3303 else if ( x == -y ) y = 0;
3304 else if ( y == 0 ) y = x;
3305 kernel->x = (unsigned long) x+1;
3306 kernel->y = (unsigned long) y+1;
3307 }
anthony43c49252010-05-18 10:59:50 +00003308 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3309 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003310 }
3311 else
3312 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3313 }
3314 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3315 {
3316 if ( kernel->width == 1 || kernel->height == 1 )
3317 { /* Do a transpose of the image, which results in a 90
3318 ** degree rotation of a 1 dimentional kernel
3319 */
3320 long
3321 t;
3322 t = (long) kernel->width;
3323 kernel->width = kernel->height;
3324 kernel->height = (unsigned long) t;
3325 t = kernel->x;
3326 kernel->x = kernel->y;
3327 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003328 if ( kernel->width == 1 ) {
3329 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3330 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3331 } else {
3332 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3333 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3334 }
anthony3c10fc82010-05-13 02:40:51 +00003335 }
3336 else if ( kernel->width == kernel->height )
3337 { /* Rotate a square array of values by 90 degrees */
anthony1d45eb92010-05-25 11:13:23 +00003338 { register unsigned long
3339 i,j,x,y;
3340 register MagickRealType
3341 *k,t;
3342 k=kernel->values;
3343 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3344 for( j=0, y=kernel->height-1; j<y; j++, y--)
3345 { t = k[i+j*kernel->width];
3346 k[i+j*kernel->width] = k[j+x*kernel->width];
3347 k[j+x*kernel->width] = k[x+y*kernel->width];
3348 k[x+y*kernel->width] = k[y+i*kernel->width];
3349 k[y+i*kernel->width] = t;
3350 }
3351 }
3352 /* rotate the origin - relative to center of array */
3353 { register long x,y;
3354 x = (long) kernel->x*2-kernel->width+1;
3355 y = (long) kernel->y*2-kernel->height+1;
3356 kernel->x = (unsigned long) ( -y +kernel->width-1)/2;
3357 kernel->y = (unsigned long) ( +x +kernel->height-1)/2;
3358 }
anthony43c49252010-05-18 10:59:50 +00003359 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3360 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003361 }
3362 else
3363 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3364 }
anthony83ba99b2010-01-24 08:48:15 +00003365 if ( 135.0 < angle && angle <= 225.0 )
3366 {
anthony43c49252010-05-18 10:59:50 +00003367 /* For a 180 degree rotation - also know as a reflection
3368 * This is actually a very very common operation!
3369 * Basically all that is needed is a reversal of the kernel data!
3370 * And a reflection of the origon
3371 */
anthony83ba99b2010-01-24 08:48:15 +00003372 unsigned long
3373 i,j;
3374 register double
3375 *k,t;
3376
3377 k=kernel->values;
3378 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3379 t=k[i], k[i]=k[j], k[j]=t;
3380
anthony930be612010-02-08 04:26:15 +00003381 kernel->x = (long) kernel->width - kernel->x - 1;
3382 kernel->y = (long) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003383 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3384 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003385 }
anthony3c10fc82010-05-13 02:40:51 +00003386 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003387 * In the future some form of non-orthogonal angled rotates could be
3388 * performed here, posibily with a linear kernel restriction.
3389 */
3390
3391#if 0
anthony3c10fc82010-05-13 02:40:51 +00003392 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003393 */
3394 unsigned long
3395 y;
cristy150989e2010-02-01 14:59:39 +00003396 register long
anthony83ba99b2010-01-24 08:48:15 +00003397 x,r;
3398 register double
3399 *k,t;
3400
3401 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3402 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3403 t=k[x], k[x]=k[r], k[r]=t;
3404
cristyc99304f2010-02-01 15:26:27 +00003405 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003406 angle = fmod(angle+180.0, 360.0);
3407 }
3408#endif
3409 return;
3410}
3411
3412/*
3413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3414% %
3415% %
3416% %
anthony46a369d2010-05-19 02:41:48 +00003417% S c a l e G e o m e t r y K e r n e l I n f o %
3418% %
3419% %
3420% %
3421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3422%
3423% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3424% provided as a "-set option:convolve:scale {geometry}" user setting,
3425% and modifies the kernel according to the parsed arguments of that setting.
3426%
3427% The first argument (and any normalization flags) are passed to
3428% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3429% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3430% into the scaled/normalized kernel.
3431%
3432% The format of the ScaleKernelInfo method is:
3433%
3434% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3435% const MagickStatusType normalize_flags )
3436%
3437% A description of each parameter follows:
3438%
3439% o kernel: the Morphology/Convolution kernel to modify
3440%
3441% o geometry:
3442% The geometry string to parse, typically from the user provided
3443% "-set option:convolve:scale {geometry}" setting.
3444%
3445*/
3446MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3447 const char *geometry)
3448{
3449 GeometryFlags
3450 flags;
3451 GeometryInfo
3452 args;
3453
3454 SetGeometryInfo(&args);
3455 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3456
3457#if 0
3458 /* For Debugging Geometry Input */
3459 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3460 flags, args.rho, args.sigma, args.xi, args.psi );
3461#endif
3462
3463 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3464 args.rho *= 0.01, args.sigma *= 0.01;
3465
3466 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3467 args.rho = 1.0;
3468 if ( (flags & SigmaValue) == 0 )
3469 args.sigma = 0.0;
3470
3471 /* Scale/Normalize the input kernel */
3472 ScaleKernelInfo(kernel, args.rho, flags);
3473
3474 /* Add Unity Kernel, for blending with original */
3475 if ( (flags & SigmaValue) != 0 )
3476 UnityAddKernelInfo(kernel, args.sigma);
3477
3478 return;
3479}
3480/*
3481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3482% %
3483% %
3484% %
cristy6771f1e2010-03-05 19:43:39 +00003485% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003486% %
3487% %
3488% %
3489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3490%
anthony1b2bc0a2010-05-12 05:25:22 +00003491% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3492% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003493%
anthony999bb2c2010-02-18 12:38:01 +00003494% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003495% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003496%
anthony46a369d2010-05-19 02:41:48 +00003497% If either of the two 'normalize_flags' are given the kernel will first be
3498% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003499%
3500% Kernel normalization ('normalize_flags' given) is designed to ensure that
3501% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003502% morphology methods will fall into -1.0 to +1.0 range. Note that for
3503% non-HDRI versions of IM this may cause images to have any negative results
3504% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003505%
3506% More specifically. Kernels which only contain positive values (such as a
3507% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003508% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003509%
3510% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3511% the kernel will be scaled by the absolute of the sum of kernel values, so
3512% that it will generally fall within the +/- 1.0 range.
3513%
3514% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3515% will be scaled by just the sum of the postive values, so that its output
3516% range will again fall into the +/- 1.0 range.
3517%
3518% For special kernels designed for locating shapes using 'Correlate', (often
3519% only containing +1 and -1 values, representing foreground/brackground
3520% matching) a special normalization method is provided to scale the positive
3521% values seperatally to those of the negative values, so the kernel will be
3522% forced to become a zero-sum kernel better suited to such searches.
3523%
anthony1b2bc0a2010-05-12 05:25:22 +00003524% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003525% attributes within the kernel structure have been correctly set during the
3526% kernels creation.
3527%
3528% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003529% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3530% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003531%
anthony4fd27e22010-02-07 08:17:18 +00003532% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003533%
anthony999bb2c2010-02-18 12:38:01 +00003534% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3535% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003536%
3537% A description of each parameter follows:
3538%
3539% o kernel: the Morphology/Convolution kernel
3540%
anthony999bb2c2010-02-18 12:38:01 +00003541% o scaling_factor:
3542% multiply all values (after normalization) by this factor if not
3543% zero. If the kernel is normalized regardless of any flags.
3544%
3545% o normalize_flags:
3546% GeometryFlags defining normalization method to use.
3547% specifically: NormalizeValue, CorrelateNormalizeValue,
3548% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003549%
3550*/
cristy6771f1e2010-03-05 19:43:39 +00003551MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3552 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003553{
cristy150989e2010-02-01 14:59:39 +00003554 register long
anthonycc6c8362010-01-25 04:14:01 +00003555 i;
3556
anthony999bb2c2010-02-18 12:38:01 +00003557 register double
3558 pos_scale,
3559 neg_scale;
3560
anthony46a369d2010-05-19 02:41:48 +00003561 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003562 if ( kernel->next != (KernelInfo *) NULL)
3563 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3564
anthony46a369d2010-05-19 02:41:48 +00003565 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003566 pos_scale = 1.0;
3567 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003568 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003569 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003570 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003571 else
anthonyf4e00312010-05-20 12:06:35 +00003572 /* zero-summing kernel */
3573 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003574 }
anthony46a369d2010-05-19 02:41:48 +00003575 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003576 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3577 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3578 ? kernel->positive_range : 1.0;
3579 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3580 ? -kernel->negative_range : 1.0;
3581 }
3582 else
3583 neg_scale = pos_scale;
3584
3585 /* finialize scaling_factor for positive and negative components */
3586 pos_scale = scaling_factor/pos_scale;
3587 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003588
cristy150989e2010-02-01 14:59:39 +00003589 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003590 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003591 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003592
anthony999bb2c2010-02-18 12:38:01 +00003593 /* convolution output range */
3594 kernel->positive_range *= pos_scale;
3595 kernel->negative_range *= neg_scale;
3596 /* maximum and minimum values in kernel */
3597 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3598 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3599
anthony46a369d2010-05-19 02:41:48 +00003600 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003601 if ( scaling_factor < MagickEpsilon ) {
3602 double t;
3603 t = kernel->positive_range;
3604 kernel->positive_range = kernel->negative_range;
3605 kernel->negative_range = t;
3606 t = kernel->maximum;
3607 kernel->maximum = kernel->minimum;
3608 kernel->minimum = 1;
3609 }
anthonycc6c8362010-01-25 04:14:01 +00003610
3611 return;
3612}
3613
3614/*
3615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3616% %
3617% %
3618% %
anthony46a369d2010-05-19 02:41:48 +00003619% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003620% %
3621% %
3622% %
3623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3624%
anthony4fd27e22010-02-07 08:17:18 +00003625% ShowKernelInfo() outputs the details of the given kernel defination to
3626% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003627%
3628% The format of the ShowKernel method is:
3629%
anthony4fd27e22010-02-07 08:17:18 +00003630% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003631%
3632% A description of each parameter follows:
3633%
3634% o kernel: the Morphology/Convolution kernel
3635%
anthony83ba99b2010-01-24 08:48:15 +00003636*/
anthony4fd27e22010-02-07 08:17:18 +00003637MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003638{
anthony7a01dcf2010-05-11 12:25:52 +00003639 KernelInfo
3640 *k;
anthony83ba99b2010-01-24 08:48:15 +00003641
anthony43c49252010-05-18 10:59:50 +00003642 unsigned long
anthony7a01dcf2010-05-11 12:25:52 +00003643 c, i, u, v;
3644
3645 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3646
anthony46a369d2010-05-19 02:41:48 +00003647 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003648 if ( kernel->next != (KernelInfo *) NULL )
cristye96405a2010-05-19 02:24:31 +00003649 fprintf(stderr, " #%lu", c );
anthony43c49252010-05-18 10:59:50 +00003650 fprintf(stderr, " \"%s",
3651 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3652 if ( fabs(k->angle) > MagickEpsilon )
3653 fprintf(stderr, "@%lg", k->angle);
anthonya648a302010-05-27 02:14:36 +00003654 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",
anthony43c49252010-05-18 10:59:50 +00003655 k->width, k->height,
3656 k->x, k->y );
anthony7a01dcf2010-05-11 12:25:52 +00003657 fprintf(stderr,
3658 " with values from %.*lg to %.*lg\n",
3659 GetMagickPrecision(), k->minimum,
3660 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003661 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003662 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003663 GetMagickPrecision(), k->positive_range);
3664 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3665 fprintf(stderr, " (Zero-Summing)\n");
3666 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3667 fprintf(stderr, " (Normalized)\n");
3668 else
3669 fprintf(stderr, " (Sum %.*lg)\n",
3670 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003671 for (i=v=0; v < k->height; v++) {
anthony46a369d2010-05-19 02:41:48 +00003672 fprintf(stderr, "%2lu:", v );
anthony43c49252010-05-18 10:59:50 +00003673 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003674 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003675 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003676 else
anthonyf4e00312010-05-20 12:06:35 +00003677 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003678 GetMagickPrecision(), k->values[i]);
3679 fprintf(stderr,"\n");
3680 }
anthony83ba99b2010-01-24 08:48:15 +00003681 }
3682}
anthonycc6c8362010-01-25 04:14:01 +00003683
3684/*
3685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3686% %
3687% %
3688% %
anthony43c49252010-05-18 10:59:50 +00003689% U n i t y A d d K e r n a l I n f o %
3690% %
3691% %
3692% %
3693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3694%
3695% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3696% to the given pre-scaled and normalized Kernel. This in effect adds that
3697% amount of the original image into the resulting convolution kernel. This
3698% value is usually provided by the user as a percentage value in the
3699% 'convolve:scale' setting.
3700%
3701% The resulting effect is to either convert a 'zero-summing' edge detection
3702% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3703% kernel.
3704%
3705% Alternativally by using a purely positive kernel, and using a negative
3706% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3707% as a "Gaussian") into a 'unsharp' kernel.
3708%
anthony46a369d2010-05-19 02:41:48 +00003709% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003710%
3711% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3712%
3713% A description of each parameter follows:
3714%
3715% o kernel: the Morphology/Convolution kernel
3716%
3717% o scale:
3718% scaling factor for the unity kernel to be added to
3719% the given kernel.
3720%
anthony43c49252010-05-18 10:59:50 +00003721*/
3722MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3723 const double scale)
3724{
anthony46a369d2010-05-19 02:41:48 +00003725 /* do the other kernels in a multi-kernel list first */
3726 if ( kernel->next != (KernelInfo *) NULL)
3727 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003728
anthony46a369d2010-05-19 02:41:48 +00003729 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003730 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003731 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003732
3733 return;
3734}
3735
3736/*
3737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3738% %
3739% %
3740% %
3741% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003742% %
3743% %
3744% %
3745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3746%
3747% ZeroKernelNans() replaces any special 'nan' value that may be present in
3748% the kernel with a zero value. This is typically done when the kernel will
3749% be used in special hardware (GPU) convolution processors, to simply
3750% matters.
3751%
3752% The format of the ZeroKernelNans method is:
3753%
anthony46a369d2010-05-19 02:41:48 +00003754% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003755%
3756% A description of each parameter follows:
3757%
3758% o kernel: the Morphology/Convolution kernel
3759%
anthonycc6c8362010-01-25 04:14:01 +00003760*/
anthonyc4c86e02010-01-27 09:30:32 +00003761MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003762{
anthony43c49252010-05-18 10:59:50 +00003763 register unsigned long
anthonycc6c8362010-01-25 04:14:01 +00003764 i;
3765
anthony46a369d2010-05-19 02:41:48 +00003766 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003767 if ( kernel->next != (KernelInfo *) NULL)
3768 ZeroKernelNans(kernel->next);
3769
anthony43c49252010-05-18 10:59:50 +00003770 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003771 if ( IsNan(kernel->values[i]) )
3772 kernel->values[i] = 0.0;
3773
3774 return;
3775}