blob: cc2d8100202dceece0ea6e98fc37b1d4201d75d7 [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 {
1728 KernelInfo
1729 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001730 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001731 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001732 if (kernel == (KernelInfo *) NULL)
1733 return(kernel);
1734 kernel->type = type;
1735 ExpandKernelInfo(kernel, 90.0);
1736 /* append second set of 4 kernels */
anthony43c49252010-05-18 10:59:50 +00001737 new_kernel=ParseKernelArray("3: 1,1,1 1,0,0 -,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001738 if (new_kernel == (KernelInfo *) NULL)
1739 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001740 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001741 ExpandKernelInfo(new_kernel, 90.0);
1742 LastKernelInfo(kernel)->next = new_kernel;
1743 break;
1744 }
anthony47f5d062010-05-23 07:47:50 +00001745 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001746 { /* what is the best form for skeletonization by thinning? */
anthony47f5d062010-05-23 07:47:50 +00001747#if 0
1748# if 0
1749 kernel=AcquireKernelInfo("Corners;Edges");
1750# else
1751 kernel=AcquireKernelInfo("Edges;Corners");
1752# endif
1753#else
anthony43c49252010-05-18 10:59:50 +00001754 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001755 if (kernel == (KernelInfo *) NULL)
1756 return(kernel);
1757 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001758 ExpandKernelInfo(kernel, 45);
1759 break;
anthony47f5d062010-05-23 07:47:50 +00001760#endif
anthony3dd0f622010-05-13 12:57:32 +00001761 break;
1762 }
anthonya648a302010-05-27 02:14:36 +00001763 case MatKernel: /* experimental - MAT from a Distance Gradient */
1764 {
1765 KernelInfo
1766 *new_kernel;
1767 /* Ridge Kernel but without the diagonal */
1768 kernel=ParseKernelArray("3x1: 0,1,0");
1769 if (kernel == (KernelInfo *) NULL)
1770 return(kernel);
1771 kernel->type = RidgesKernel;
1772 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1773 /* Plus the 2 pixel ridges kernel - no diagonal */
1774 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1775 if (new_kernel == (KernelInfo *) NULL)
1776 return(kernel);
1777 LastKernelInfo(kernel)->next = new_kernel;
1778 break;
1779 }
anthony602ab9b2010-01-05 08:06:50 +00001780 /* Distance Measuring Kernels */
1781 case ChebyshevKernel:
1782 {
anthony602ab9b2010-01-05 08:06:50 +00001783 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001784 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001785 else
1786 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001787 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001788
1789 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1790 kernel->height*sizeof(double));
1791 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001792 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001793
cristyc99304f2010-02-01 15:26:27 +00001794 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1795 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1796 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001797 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001798 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001799 break;
1800 }
1801 case ManhattenKernel:
1802 {
anthony602ab9b2010-01-05 08:06:50 +00001803 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001804 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001805 else
1806 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001807 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001808
1809 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1810 kernel->height*sizeof(double));
1811 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001812 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001813
cristyc99304f2010-02-01 15:26:27 +00001814 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1815 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1816 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001817 args->sigma*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001818 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001819 break;
1820 }
1821 case EuclideanKernel:
1822 {
anthony602ab9b2010-01-05 08:06:50 +00001823 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001824 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001825 else
1826 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001827 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001828
1829 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1830 kernel->height*sizeof(double));
1831 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001832 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001833
cristyc99304f2010-02-01 15:26:27 +00001834 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1835 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1836 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001837 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001838 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001839 break;
1840 }
anthony46a369d2010-05-19 02:41:48 +00001841 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001842 default:
anthonyc1061722010-05-14 06:23:49 +00001843 {
anthony46a369d2010-05-19 02:41:48 +00001844 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1845 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001846 if (kernel == (KernelInfo *) NULL)
1847 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001848 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001849 break;
1850 }
anthony602ab9b2010-01-05 08:06:50 +00001851 break;
1852 }
1853
1854 return(kernel);
1855}
anthonyc94cdb02010-01-06 08:15:29 +00001856
anthony602ab9b2010-01-05 08:06:50 +00001857/*
1858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859% %
1860% %
1861% %
cristy6771f1e2010-03-05 19:43:39 +00001862% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001863% %
1864% %
1865% %
1866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1867%
anthony1b2bc0a2010-05-12 05:25:22 +00001868% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1869% can be modified without effecting the original. The cloned kernel should
1870% be destroyed using DestoryKernelInfo() when no longer needed.
anthony7a01dcf2010-05-11 12:25:52 +00001871%
cristye6365592010-04-02 17:31:23 +00001872% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001873%
anthony930be612010-02-08 04:26:15 +00001874% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001875%
1876% A description of each parameter follows:
1877%
1878% o kernel: the Morphology/Convolution kernel to be cloned
1879%
1880*/
cristyef656912010-03-05 19:54:59 +00001881MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001882{
1883 register long
1884 i;
1885
cristy19eb6412010-04-23 14:42:29 +00001886 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001887 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001888
1889 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001890 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1891 if (new_kernel == (KernelInfo *) NULL)
1892 return(new_kernel);
1893 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001894
1895 /* replace the values with a copy of the values */
1896 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001897 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001898 if (new_kernel->values == (double *) NULL)
1899 return(DestroyKernelInfo(new_kernel));
anthony4fd27e22010-02-07 08:17:18 +00001900 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001901 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001902
1903 /* Also clone the next kernel in the kernel list */
1904 if ( kernel->next != (KernelInfo *) NULL ) {
1905 new_kernel->next = CloneKernelInfo(kernel->next);
1906 if ( new_kernel->next == (KernelInfo *) NULL )
1907 return(DestroyKernelInfo(new_kernel));
1908 }
1909
anthony7a01dcf2010-05-11 12:25:52 +00001910 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001911}
1912
1913/*
1914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1915% %
1916% %
1917% %
anthony83ba99b2010-01-24 08:48:15 +00001918% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001919% %
1920% %
1921% %
1922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1923%
anthony83ba99b2010-01-24 08:48:15 +00001924% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1925% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001926%
anthony83ba99b2010-01-24 08:48:15 +00001927% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001928%
anthony83ba99b2010-01-24 08:48:15 +00001929% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001930%
1931% A description of each parameter follows:
1932%
1933% o kernel: the Morphology/Convolution kernel to be destroyed
1934%
1935*/
1936
anthony83ba99b2010-01-24 08:48:15 +00001937MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001938{
cristy2be15382010-01-21 02:38:03 +00001939 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001940
anthony7a01dcf2010-05-11 12:25:52 +00001941 if ( kernel->next != (KernelInfo *) NULL )
1942 kernel->next = DestroyKernelInfo(kernel->next);
1943
1944 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1945 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001946 return(kernel);
1947}
anthonyc94cdb02010-01-06 08:15:29 +00001948
1949/*
1950%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951% %
1952% %
1953% %
anthony3c10fc82010-05-13 02:40:51 +00001954% E x p a n d K e r n e l I n f o %
1955% %
1956% %
1957% %
1958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959%
1960% ExpandKernelInfo() takes a single kernel, and expands it into a list
1961% of kernels each incrementally rotated the angle given.
1962%
1963% WARNING: 45 degree rotations only works for 3x3 kernels.
1964% While 90 degree roatations only works for linear and square kernels
1965%
1966% The format of the RotateKernelInfo method is:
1967%
1968% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1969%
1970% A description of each parameter follows:
1971%
1972% o kernel: the Morphology/Convolution kernel
1973%
1974% o angle: angle to rotate in degrees
1975%
1976% This function is only internel to this module, as it is not finalized,
1977% especially with regard to non-orthogonal angles, and rotation of larger
1978% 2D kernels.
1979*/
anthony47f5d062010-05-23 07:47:50 +00001980
1981/* Internal Routine - Return true if two kernels are the same */
1982static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1983 const KernelInfo *kernel2)
1984{
1985 register unsigned long
1986 i;
anthony1d45eb92010-05-25 11:13:23 +00001987
1988 /* check size and origin location */
1989 if ( kernel1->width != kernel2->width
1990 || kernel1->height != kernel2->height
1991 || kernel1->x != kernel2->x
1992 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001993 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001994
1995 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001996 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001997 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001998 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1999 return MagickFalse;
2000 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2001 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002002 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002003 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2004 return MagickFalse;
2005 }
anthony1d45eb92010-05-25 11:13:23 +00002006
anthony47f5d062010-05-23 07:47:50 +00002007 return MagickTrue;
2008}
2009
2010static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002011{
2012 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002013 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002014 *last;
cristya9a61ad2010-05-13 12:47:41 +00002015
anthony3c10fc82010-05-13 02:40:51 +00002016 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002017 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002018 clone = CloneKernelInfo(last);
2019 RotateKernelInfo(clone, angle);
2020 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002021 break;
cristy84d9b552010-05-24 18:23:54 +00002022 last->next = clone;
2023 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002024 }
cristy84d9b552010-05-24 18:23:54 +00002025 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002026 return;
anthony3c10fc82010-05-13 02:40:51 +00002027}
2028
2029/*
2030%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2031% %
2032% %
2033% %
anthony46a369d2010-05-19 02:41:48 +00002034+ C a l c M e t a K e r n a l I n f o %
2035% %
2036% %
2037% %
2038%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039%
2040% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2041% using the kernel values. This should only ne used if it is not posible to
2042% calculate that meta-data in some easier way.
2043%
2044% It is important that the meta-data is correct before ScaleKernelInfo() is
2045% used to perform kernel normalization.
2046%
2047% The format of the CalcKernelMetaData method is:
2048%
2049% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2050%
2051% A description of each parameter follows:
2052%
2053% o kernel: the Morphology/Convolution kernel to modify
2054%
2055% WARNING: Minimum and Maximum values are assumed to include zero, even if
2056% zero is not part of the kernel (as in Gaussian Derived kernels). This
2057% however is not true for flat-shaped morphological kernels.
2058%
2059% WARNING: Only the specific kernel pointed to is modified, not a list of
2060% multiple kernels.
2061%
2062% This is an internal function and not expected to be useful outside this
2063% module. This could change however.
2064*/
2065static void CalcKernelMetaData(KernelInfo *kernel)
2066{
2067 register unsigned long
2068 i;
2069
2070 kernel->minimum = kernel->maximum = 0.0;
2071 kernel->negative_range = kernel->positive_range = 0.0;
2072 for (i=0; i < (kernel->width*kernel->height); i++)
2073 {
2074 if ( fabs(kernel->values[i]) < MagickEpsilon )
2075 kernel->values[i] = 0.0;
2076 ( kernel->values[i] < 0)
2077 ? ( kernel->negative_range += kernel->values[i] )
2078 : ( kernel->positive_range += kernel->values[i] );
2079 Minimize(kernel->minimum, kernel->values[i]);
2080 Maximize(kernel->maximum, kernel->values[i]);
2081 }
2082
2083 return;
2084}
2085
2086/*
2087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2088% %
2089% %
2090% %
anthony9eb4f742010-05-18 02:45:54 +00002091% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002092% %
2093% %
2094% %
2095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2096%
anthony9eb4f742010-05-18 02:45:54 +00002097% MorphologyApply() applies a morphological method, multiple times using
2098% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002099%
anthony9eb4f742010-05-18 02:45:54 +00002100% It is basically equivelent to as MorphologyImageChannel() (see below) but
2101% without user controls, that that function extracts and applies to kernels
2102% and morphology methods.
2103%
2104% More specifically kernels are not normalized/scaled/blended by the
2105% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2106% (-bias setting or image->bias) is passed directly to this function,
2107% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002108%
anthony47f5d062010-05-23 07:47:50 +00002109% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002110%
anthony9eb4f742010-05-18 02:45:54 +00002111% Image *MorphologyApply(const Image *image,MorphologyMethod method,
anthony47f5d062010-05-23 07:47:50 +00002112% const long iterations,const KernelInfo *kernel,
2113% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002114% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002115%
2116% A description of each parameter follows:
2117%
2118% o image: the image.
2119%
2120% o method: the morphology method to be applied.
2121%
2122% o iterations: apply the operation this many times (or no change).
2123% A value of -1 means loop until no change found.
2124% How this is applied may depend on the morphology method.
2125% Typically this is a value of 1.
2126%
2127% o channel: the channel type.
2128%
2129% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002130% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002131%
anthony47f5d062010-05-23 07:47:50 +00002132% o compose: How to handle or merge multi-kernel results.
2133% If 'Undefined' use default of the Morphology method.
2134% If 'No' force image to be re-iterated by each kernel.
2135% Otherwise merge the results using the mathematical compose
2136% method given.
2137%
2138% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002139%
anthony602ab9b2010-01-05 08:06:50 +00002140% o exception: return any errors or warnings in this structure.
2141%
anthony602ab9b2010-01-05 08:06:50 +00002142*/
2143
anthony930be612010-02-08 04:26:15 +00002144
anthony9eb4f742010-05-18 02:45:54 +00002145/* Apply a Morphology Primative to an image using the given kernel.
2146** Two pre-created images must be provided, no image is created.
2147** Returning the number of pixels that changed.
2148*/
anthony46a369d2010-05-19 02:41:48 +00002149static unsigned long MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002150 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002151 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002152{
cristy2be15382010-01-21 02:38:03 +00002153#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002154
2155 long
cristy150989e2010-02-01 14:59:39 +00002156 progress,
anthony29188a82010-01-22 10:12:34 +00002157 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002158 changed;
2159
2160 MagickBooleanType
2161 status;
2162
anthony602ab9b2010-01-05 08:06:50 +00002163 CacheView
2164 *p_view,
2165 *q_view;
2166
anthony602ab9b2010-01-05 08:06:50 +00002167 status=MagickTrue;
2168 changed=0;
2169 progress=0;
2170
anthony602ab9b2010-01-05 08:06:50 +00002171 p_view=AcquireCacheView(image);
2172 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002173
anthonycc6c8362010-01-25 04:14:01 +00002174 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002175 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002176 */
cristyc99304f2010-02-01 15:26:27 +00002177 offx = kernel->x;
2178 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002179 switch(method) {
anthony930be612010-02-08 04:26:15 +00002180 case ConvolveMorphology:
2181 case DilateMorphology:
2182 case DilateIntensityMorphology:
2183 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002184 /* kernel needs to used with reflection about origin */
cristy150989e2010-02-01 14:59:39 +00002185 offx = (long) kernel->width-offx-1;
2186 offy = (long) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002187 break;
anthony5ef8e942010-05-11 06:51:12 +00002188 case ErodeMorphology:
2189 case ErodeIntensityMorphology:
2190 case HitAndMissMorphology:
2191 case ThinningMorphology:
2192 case ThickenMorphology:
2193 /* kernel is user as is, without reflection */
2194 break;
anthony930be612010-02-08 04:26:15 +00002195 default:
anthony9eb4f742010-05-18 02:45:54 +00002196 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002197 break;
anthony29188a82010-01-22 10:12:34 +00002198 }
2199
anthony602ab9b2010-01-05 08:06:50 +00002200#if defined(MAGICKCORE_OPENMP_SUPPORT)
2201 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2202#endif
cristy150989e2010-02-01 14:59:39 +00002203 for (y=0; y < (long) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002204 {
2205 MagickBooleanType
2206 sync;
2207
2208 register const PixelPacket
2209 *restrict p;
2210
2211 register const IndexPacket
2212 *restrict p_indexes;
2213
2214 register PixelPacket
2215 *restrict q;
2216
2217 register IndexPacket
2218 *restrict q_indexes;
2219
cristy150989e2010-02-01 14:59:39 +00002220 register long
anthony602ab9b2010-01-05 08:06:50 +00002221 x;
2222
anthony29188a82010-01-22 10:12:34 +00002223 unsigned long
anthony602ab9b2010-01-05 08:06:50 +00002224 r;
2225
2226 if (status == MagickFalse)
2227 continue;
anthony29188a82010-01-22 10:12:34 +00002228 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2229 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002230 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2231 exception);
2232 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2233 {
2234 status=MagickFalse;
2235 continue;
2236 }
2237 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2238 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002239 r = (image->columns+kernel->width)*offy+offx; /* constant */
2240
cristy150989e2010-02-01 14:59:39 +00002241 for (x=0; x < (long) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002242 {
cristy150989e2010-02-01 14:59:39 +00002243 long
anthony602ab9b2010-01-05 08:06:50 +00002244 v;
2245
cristy150989e2010-02-01 14:59:39 +00002246 register long
anthony602ab9b2010-01-05 08:06:50 +00002247 u;
2248
2249 register const double
2250 *restrict k;
2251
2252 register const PixelPacket
2253 *restrict k_pixels;
2254
2255 register const IndexPacket
2256 *restrict k_indexes;
2257
2258 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002259 result,
2260 min,
2261 max;
anthony602ab9b2010-01-05 08:06:50 +00002262
anthony29188a82010-01-22 10:12:34 +00002263 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002264 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002265 */
anthony602ab9b2010-01-05 08:06:50 +00002266 *q = p[r];
2267 if (image->colorspace == CMYKColorspace)
2268 q_indexes[x] = p_indexes[r];
2269
anthony5ef8e942010-05-11 06:51:12 +00002270 /* Defaults */
2271 min.red =
2272 min.green =
2273 min.blue =
2274 min.opacity =
2275 min.index = (MagickRealType) QuantumRange;
2276 max.red =
2277 max.green =
2278 max.blue =
2279 max.opacity =
2280 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002281 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002282 result.red = (MagickRealType) p[r].red;
2283 result.green = (MagickRealType) p[r].green;
2284 result.blue = (MagickRealType) p[r].blue;
2285 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002286 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002287 if ( image->colorspace == CMYKColorspace)
2288 result.index = (MagickRealType) p_indexes[r];
2289
anthony602ab9b2010-01-05 08:06:50 +00002290 switch (method) {
2291 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002292 /* Set the user defined bias of the weighted average output */
2293 result.red =
2294 result.green =
2295 result.blue =
2296 result.opacity =
2297 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002298 break;
anthony4fd27e22010-02-07 08:17:18 +00002299 case DilateIntensityMorphology:
2300 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002301 /* use a boolean flag indicating when first match found */
2302 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002303 break;
anthony602ab9b2010-01-05 08:06:50 +00002304 default:
anthony602ab9b2010-01-05 08:06:50 +00002305 break;
2306 }
2307
2308 switch ( method ) {
2309 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002310 /* Weighted Average of pixels using reflected kernel
2311 **
2312 ** NOTE for correct working of this operation for asymetrical
2313 ** kernels, the kernel needs to be applied in its reflected form.
2314 ** That is its values needs to be reversed.
2315 **
2316 ** Correlation is actually the same as this but without reflecting
2317 ** the kernel, and thus 'lower-level' that Convolution. However
2318 ** as Convolution is the more common method used, and it does not
2319 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002320 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002321 **
2322 ** Correlation will have its kernel reflected before calling
2323 ** this function to do a Convolve.
2324 **
2325 ** For more details of Correlation vs Convolution see
2326 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2327 */
anthony5ef8e942010-05-11 06:51:12 +00002328 if (((channel & SyncChannels) != 0 ) &&
2329 (image->matte == MagickTrue))
2330 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2331 ** Weight the color channels with Alpha Channel so that
2332 ** transparent pixels are not part of the results.
2333 */
anthony602ab9b2010-01-05 08:06:50 +00002334 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002335 alpha, /* color channel weighting : kernel*alpha */
2336 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002337
2338 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002339 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002340 k_pixels = p;
2341 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002342 for (v=0; v < (long) kernel->height; v++) {
2343 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002344 if ( IsNan(*k) ) continue;
2345 alpha=(*k)*(QuantumScale*(QuantumRange-
2346 k_pixels[u].opacity));
2347 gamma += alpha;
2348 result.red += alpha*k_pixels[u].red;
2349 result.green += alpha*k_pixels[u].green;
2350 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002351 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002352 if ( image->colorspace == CMYKColorspace)
2353 result.index += alpha*k_indexes[u];
2354 }
2355 k_pixels += image->columns+kernel->width;
2356 k_indexes += image->columns+kernel->width;
2357 }
2358 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002359 result.red *= gamma;
2360 result.green *= gamma;
2361 result.blue *= gamma;
2362 result.opacity *= gamma;
2363 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002364 }
anthony5ef8e942010-05-11 06:51:12 +00002365 else
2366 {
2367 /* No 'Sync' flag, or no Alpha involved.
2368 ** Convolution is simple individual channel weigthed sum.
2369 */
2370 k = &kernel->values[ kernel->width*kernel->height-1 ];
2371 k_pixels = p;
2372 k_indexes = p_indexes;
2373 for (v=0; v < (long) kernel->height; v++) {
2374 for (u=0; u < (long) kernel->width; u++, k--) {
2375 if ( IsNan(*k) ) continue;
2376 result.red += (*k)*k_pixels[u].red;
2377 result.green += (*k)*k_pixels[u].green;
2378 result.blue += (*k)*k_pixels[u].blue;
2379 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2380 if ( image->colorspace == CMYKColorspace)
2381 result.index += (*k)*k_indexes[u];
2382 }
2383 k_pixels += image->columns+kernel->width;
2384 k_indexes += image->columns+kernel->width;
2385 }
2386 }
anthony602ab9b2010-01-05 08:06:50 +00002387 break;
2388
anthony4fd27e22010-02-07 08:17:18 +00002389 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002390 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002391 **
2392 ** NOTE that the kernel is not reflected for this operation!
2393 **
2394 ** NOTE: in normal Greyscale Morphology, the kernel value should
2395 ** be added to the real value, this is currently not done, due to
2396 ** the nature of the boolean kernels being used.
2397 */
anthony4fd27e22010-02-07 08:17:18 +00002398 k = kernel->values;
2399 k_pixels = p;
2400 k_indexes = p_indexes;
2401 for (v=0; v < (long) kernel->height; v++) {
2402 for (u=0; u < (long) kernel->width; u++, k++) {
2403 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002404 Minimize(min.red, (double) k_pixels[u].red);
2405 Minimize(min.green, (double) k_pixels[u].green);
2406 Minimize(min.blue, (double) k_pixels[u].blue);
2407 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002408 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002409 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002410 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002411 }
2412 k_pixels += image->columns+kernel->width;
2413 k_indexes += image->columns+kernel->width;
2414 }
2415 break;
2416
anthony5ef8e942010-05-11 06:51:12 +00002417
anthony83ba99b2010-01-24 08:48:15 +00002418 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002419 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002420 **
2421 ** NOTE for correct working of this operation for asymetrical
2422 ** kernels, the kernel needs to be applied in its reflected form.
2423 ** That is its values needs to be reversed.
2424 **
2425 ** NOTE: in normal Greyscale Morphology, the kernel value should
2426 ** be added to the real value, this is currently not done, due to
2427 ** the nature of the boolean kernels being used.
2428 **
2429 */
anthony29188a82010-01-22 10:12:34 +00002430 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002431 k_pixels = p;
2432 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002433 for (v=0; v < (long) kernel->height; v++) {
2434 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002435 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002436 Maximize(max.red, (double) k_pixels[u].red);
2437 Maximize(max.green, (double) k_pixels[u].green);
2438 Maximize(max.blue, (double) k_pixels[u].blue);
2439 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002440 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002441 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002442 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002443 }
2444 k_pixels += image->columns+kernel->width;
2445 k_indexes += image->columns+kernel->width;
2446 }
anthony602ab9b2010-01-05 08:06:50 +00002447 break;
2448
anthony5ef8e942010-05-11 06:51:12 +00002449 case HitAndMissMorphology:
2450 case ThinningMorphology:
2451 case ThickenMorphology:
2452 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2453 **
2454 ** NOTE that the kernel is not reflected for this operation,
2455 ** and consists of both foreground and background pixel
2456 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2457 ** with either Nan or 0.5 values for don't care.
2458 **
2459 ** Note that this can produce negative results, though really
2460 ** only a positive match has any real value.
2461 */
2462 k = kernel->values;
2463 k_pixels = p;
2464 k_indexes = p_indexes;
2465 for (v=0; v < (long) kernel->height; v++) {
2466 for (u=0; u < (long) kernel->width; u++, k++) {
2467 if ( IsNan(*k) ) continue;
2468 if ( (*k) > 0.7 )
2469 { /* minimim of foreground pixels */
2470 Minimize(min.red, (double) k_pixels[u].red);
2471 Minimize(min.green, (double) k_pixels[u].green);
2472 Minimize(min.blue, (double) k_pixels[u].blue);
2473 Minimize(min.opacity,
2474 QuantumRange-(double) k_pixels[u].opacity);
2475 if ( image->colorspace == CMYKColorspace)
2476 Minimize(min.index, (double) k_indexes[u]);
2477 }
2478 else if ( (*k) < 0.3 )
2479 { /* maximum of background pixels */
2480 Maximize(max.red, (double) k_pixels[u].red);
2481 Maximize(max.green, (double) k_pixels[u].green);
2482 Maximize(max.blue, (double) k_pixels[u].blue);
2483 Maximize(max.opacity,
2484 QuantumRange-(double) k_pixels[u].opacity);
2485 if ( image->colorspace == CMYKColorspace)
2486 Maximize(max.index, (double) k_indexes[u]);
2487 }
2488 }
2489 k_pixels += image->columns+kernel->width;
2490 k_indexes += image->columns+kernel->width;
2491 }
2492 /* Pattern Match only if min fg larger than min bg pixels */
2493 min.red -= max.red; Maximize( min.red, 0.0 );
2494 min.green -= max.green; Maximize( min.green, 0.0 );
2495 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2496 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2497 min.index -= max.index; Maximize( min.index, 0.0 );
2498 break;
2499
anthony4fd27e22010-02-07 08:17:18 +00002500 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002501 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2502 **
2503 ** WARNING: the intensity test fails for CMYK and does not
2504 ** take into account the moderating effect of teh alpha channel
2505 ** on the intensity.
2506 **
2507 ** NOTE that the kernel is not reflected for this operation!
2508 */
anthony602ab9b2010-01-05 08:06:50 +00002509 k = kernel->values;
2510 k_pixels = p;
2511 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002512 for (v=0; v < (long) kernel->height; v++) {
2513 for (u=0; u < (long) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002514 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002515 if ( result.red == 0.0 ||
2516 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2517 /* copy the whole pixel - no channel selection */
2518 *q = k_pixels[u];
2519 if ( result.red > 0.0 ) changed++;
2520 result.red = 1.0;
2521 }
anthony602ab9b2010-01-05 08:06:50 +00002522 }
2523 k_pixels += image->columns+kernel->width;
2524 k_indexes += image->columns+kernel->width;
2525 }
anthony602ab9b2010-01-05 08:06:50 +00002526 break;
2527
anthony83ba99b2010-01-24 08:48:15 +00002528 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002529 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2530 **
2531 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002532 ** take into account the moderating effect of the alpha channel
2533 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002534 **
2535 ** NOTE for correct working of this operation for asymetrical
2536 ** kernels, the kernel needs to be applied in its reflected form.
2537 ** That is its values needs to be reversed.
2538 */
anthony29188a82010-01-22 10:12:34 +00002539 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002540 k_pixels = p;
2541 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002542 for (v=0; v < (long) kernel->height; v++) {
2543 for (u=0; u < (long) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002544 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2545 if ( result.red == 0.0 ||
2546 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2547 /* copy the whole pixel - no channel selection */
2548 *q = k_pixels[u];
2549 if ( result.red > 0.0 ) changed++;
2550 result.red = 1.0;
2551 }
anthony602ab9b2010-01-05 08:06:50 +00002552 }
2553 k_pixels += image->columns+kernel->width;
2554 k_indexes += image->columns+kernel->width;
2555 }
anthony602ab9b2010-01-05 08:06:50 +00002556 break;
2557
anthony5ef8e942010-05-11 06:51:12 +00002558
anthony602ab9b2010-01-05 08:06:50 +00002559 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002560 /* Add kernel Value and select the minimum value found.
2561 ** The result is a iterative distance from edge of image shape.
2562 **
2563 ** All Distance Kernels are symetrical, but that may not always
2564 ** be the case. For example how about a distance from left edges?
2565 ** To work correctly with asymetrical kernels the reflected kernel
2566 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002567 **
2568 ** Actually this is really a GreyErode with a negative kernel!
2569 **
anthony930be612010-02-08 04:26:15 +00002570 */
anthony29188a82010-01-22 10:12:34 +00002571 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002572 k_pixels = p;
2573 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002574 for (v=0; v < (long) kernel->height; v++) {
2575 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002576 if ( IsNan(*k) ) continue;
2577 Minimize(result.red, (*k)+k_pixels[u].red);
2578 Minimize(result.green, (*k)+k_pixels[u].green);
2579 Minimize(result.blue, (*k)+k_pixels[u].blue);
2580 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2581 if ( image->colorspace == CMYKColorspace)
2582 Minimize(result.index, (*k)+k_indexes[u]);
2583 }
2584 k_pixels += image->columns+kernel->width;
2585 k_indexes += image->columns+kernel->width;
2586 }
anthony602ab9b2010-01-05 08:06:50 +00002587 break;
2588
2589 case UndefinedMorphology:
2590 default:
2591 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002592 }
anthony5ef8e942010-05-11 06:51:12 +00002593 /* Final mathematics of results (combine with original image?)
2594 **
2595 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2596 ** be done here but works better with iteration as a image difference
2597 ** in the controling function (below). Thicken and Thinning however
2598 ** should be done here so thay can be iterated correctly.
2599 */
2600 switch ( method ) {
2601 case HitAndMissMorphology:
2602 case ErodeMorphology:
2603 result = min; /* minimum of neighbourhood */
2604 break;
2605 case DilateMorphology:
2606 result = max; /* maximum of neighbourhood */
2607 break;
2608 case ThinningMorphology:
2609 /* subtract pattern match from original */
2610 result.red -= min.red;
2611 result.green -= min.green;
2612 result.blue -= min.blue;
2613 result.opacity -= min.opacity;
2614 result.index -= min.index;
2615 break;
2616 case ThickenMorphology:
2617 /* Union with original image (maximize) - or should this be + */
2618 Maximize( result.red, min.red );
2619 Maximize( result.green, min.green );
2620 Maximize( result.blue, min.blue );
2621 Maximize( result.opacity, min.opacity );
2622 Maximize( result.index, min.index );
2623 break;
2624 default:
2625 /* result directly calculated or assigned */
2626 break;
2627 }
2628 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002629 switch ( method ) {
2630 case UndefinedMorphology:
2631 case DilateIntensityMorphology:
2632 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002633 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002634 default:
anthony83ba99b2010-01-24 08:48:15 +00002635 if ((channel & RedChannel) != 0)
2636 q->red = ClampToQuantum(result.red);
2637 if ((channel & GreenChannel) != 0)
2638 q->green = ClampToQuantum(result.green);
2639 if ((channel & BlueChannel) != 0)
2640 q->blue = ClampToQuantum(result.blue);
2641 if ((channel & OpacityChannel) != 0
2642 && image->matte == MagickTrue )
2643 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2644 if ((channel & IndexChannel) != 0
2645 && image->colorspace == CMYKColorspace)
2646 q_indexes[x] = ClampToQuantum(result.index);
2647 break;
2648 }
anthony5ef8e942010-05-11 06:51:12 +00002649 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002650 if ( ( p[r].red != q->red )
2651 || ( p[r].green != q->green )
2652 || ( p[r].blue != q->blue )
2653 || ( p[r].opacity != q->opacity )
2654 || ( image->colorspace == CMYKColorspace &&
2655 p_indexes[r] != q_indexes[x] ) )
2656 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002657 p++;
2658 q++;
anthony83ba99b2010-01-24 08:48:15 +00002659 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002660 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2661 if (sync == MagickFalse)
2662 status=MagickFalse;
2663 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2664 {
2665 MagickBooleanType
2666 proceed;
2667
2668#if defined(MAGICKCORE_OPENMP_SUPPORT)
2669 #pragma omp critical (MagickCore_MorphologyImage)
2670#endif
2671 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2672 if (proceed == MagickFalse)
2673 status=MagickFalse;
2674 }
anthony83ba99b2010-01-24 08:48:15 +00002675 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002676 result_image->type=image->type;
2677 q_view=DestroyCacheView(q_view);
2678 p_view=DestroyCacheView(p_view);
cristy150989e2010-02-01 14:59:39 +00002679 return(status ? (unsigned long) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002680}
2681
anthony4fd27e22010-02-07 08:17:18 +00002682
anthony9eb4f742010-05-18 02:45:54 +00002683MagickExport Image *MorphologyApply(const Image *image, const ChannelType
2684 channel,const MorphologyMethod method, const long iterations,
anthony47f5d062010-05-23 07:47:50 +00002685 const KernelInfo *kernel, const CompositeOperator compose,
2686 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002687{
2688 Image
anthony47f5d062010-05-23 07:47:50 +00002689 *curr_image, /* Image we are working with or iterating */
2690 *work_image, /* secondary image for primative iteration */
2691 *save_image, /* saved image - for 'edge' method only */
2692 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002693
anthony4fd27e22010-02-07 08:17:18 +00002694 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002695 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2696 *norm_kernel, /* the current normal un-reflected kernel */
2697 *rflt_kernel, /* the current reflected kernel (if needed) */
2698 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002699
2700 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002701 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002702
2703 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002704 rslt_compose; /* multi-kernel compose method for results to use */
2705
2706 MagickBooleanType
2707 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002708
anthony1b2bc0a2010-05-12 05:25:22 +00002709 unsigned long
anthony47f5d062010-05-23 07:47:50 +00002710 method_loop, /* Loop 1: number of compound method iterations */
2711 method_limit, /* maximum number of compound method iterations */
2712 kernel_number, /* Loop 2: the kernel number being applied */
2713 stage_loop, /* Loop 3: primative loop for compound morphology */
2714 stage_limit, /* how many primatives in this compound */
2715 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2716 kernel_limit, /* number of times to iterate kernel */
2717 count, /* total count of primative steps applied */
2718 changed, /* number pixels changed by last primative operation */
2719 kernel_changed, /* total count of changed using iterated kernel */
2720 method_changed; /* total count of changed over method iteration */
2721
2722 char
2723 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002724
anthony602ab9b2010-01-05 08:06:50 +00002725 assert(image != (Image *) NULL);
2726 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002727 assert(kernel != (KernelInfo *) NULL);
2728 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002729 assert(exception != (ExceptionInfo *) NULL);
2730 assert(exception->signature == MagickSignature);
2731
anthonyc3e48252010-05-24 12:43:11 +00002732 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002733 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002734 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002735
anthony47f5d062010-05-23 07:47:50 +00002736 kernel_limit = (unsigned long) iterations;
2737 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2738 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002739
cristye96405a2010-05-19 02:24:31 +00002740 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2741 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002742
anthony9eb4f742010-05-18 02:45:54 +00002743 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002744 curr_image = (Image *) image;
2745 work_image = save_image = rslt_image = (Image *) NULL;
2746 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002747
anthony47f5d062010-05-23 07:47:50 +00002748 /* Initialize specific methods
2749 * + which loop should use the given iteratations
2750 * + how many primatives make up the compound morphology
2751 * + multi-kernel compose method to use (by default)
2752 */
2753 method_limit = 1; /* just do method once, unless otherwise set */
2754 stage_limit = 1; /* assume method is not a compount */
2755 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002756 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002757 case SmoothMorphology: /* 4 primative compound morphology */
2758 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002759 break;
anthony47f5d062010-05-23 07:47:50 +00002760 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002761 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002762 case TopHatMorphology:
2763 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002764 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002765 case BottomHatMorphology:
2766 case EdgeMorphology:
2767 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002768 break;
2769 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002770 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002771 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002772 break;
anthonyc3e48252010-05-24 12:43:11 +00002773 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002774 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002775 case DistanceMorphology:
2776 method_limit = kernel_limit; /* iterate method with each kernel */
2777 kernel_limit = 1; /* do not do kernel iteration */
2778 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002779 break;
2780 default:
anthony930be612010-02-08 04:26:15 +00002781 break;
anthony602ab9b2010-01-05 08:06:50 +00002782 }
2783
anthonyc3e48252010-05-24 12:43:11 +00002784 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002785 if ( compose != UndefinedCompositeOp )
2786 rslt_compose = compose; /* override default composition for method */
2787 if ( rslt_compose == UndefinedCompositeOp )
2788 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2789
anthonyc3e48252010-05-24 12:43:11 +00002790 /* Some methods require a reflected kernel to use with primatives.
2791 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002792 switch ( method ) {
2793 case CorrelateMorphology:
2794 case CloseMorphology:
2795 case CloseIntensityMorphology:
2796 case BottomHatMorphology:
2797 case SmoothMorphology:
2798 reflected_kernel = CloneKernelInfo(kernel);
2799 if (reflected_kernel == (KernelInfo *) NULL)
2800 goto error_cleanup;
2801 RotateKernelInfo(reflected_kernel,180);
2802 break;
2803 default:
2804 break;
anthony9eb4f742010-05-18 02:45:54 +00002805 }
anthony7a01dcf2010-05-11 12:25:52 +00002806
anthony47f5d062010-05-23 07:47:50 +00002807 /* Loop 1: iterate the compound method */
2808 method_loop = 0;
2809 method_changed = 1;
2810 while ( method_loop < method_limit && method_changed > 0 ) {
2811 method_loop++;
2812 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002813
anthony47f5d062010-05-23 07:47:50 +00002814 /* Loop 2: iterate over each kernel in a multi-kernel list */
2815 norm_kernel = (KernelInfo *) kernel;
2816 rflt_kernel = reflected_kernel;
2817 kernel_number = 0;
2818 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002819
anthony47f5d062010-05-23 07:47:50 +00002820 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2821 stage_loop = 0; /* the compound morphology stage number */
2822 while ( stage_loop < stage_limit ) {
2823 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002824
anthony47f5d062010-05-23 07:47:50 +00002825 /* Select primative morphology for this stage of compound method */
2826 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002827 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002828 switch( method ) {
2829 case ErodeMorphology: /* just erode */
2830 case EdgeInMorphology: /* erode and image difference */
2831 primative = ErodeMorphology;
2832 break;
2833 case DilateMorphology: /* just dilate */
2834 case EdgeOutMorphology: /* dilate and image difference */
2835 primative = DilateMorphology;
2836 break;
2837 case OpenMorphology: /* erode then dialate */
2838 case TopHatMorphology: /* open and image difference */
2839 primative = ErodeMorphology;
2840 if ( stage_loop == 2 )
2841 primative = DilateMorphology;
2842 break;
2843 case OpenIntensityMorphology:
2844 primative = ErodeIntensityMorphology;
2845 if ( stage_loop == 2 )
2846 primative = DilateIntensityMorphology;
2847 case CloseMorphology: /* dilate, then erode */
2848 case BottomHatMorphology: /* close and image difference */
2849 this_kernel = rflt_kernel; /* use the reflected kernel */
2850 primative = DilateMorphology;
2851 if ( stage_loop == 2 )
2852 primative = ErodeMorphology;
2853 break;
2854 case CloseIntensityMorphology:
2855 this_kernel = rflt_kernel; /* use the reflected kernel */
2856 primative = DilateIntensityMorphology;
2857 if ( stage_loop == 2 )
2858 primative = ErodeIntensityMorphology;
2859 break;
2860 case SmoothMorphology: /* open, close */
2861 switch ( stage_loop ) {
2862 case 1: /* start an open method, which starts with Erode */
2863 primative = ErodeMorphology;
2864 break;
2865 case 2: /* now Dilate the Erode */
2866 primative = DilateMorphology;
2867 break;
2868 case 3: /* Reflect kernel a close */
2869 this_kernel = rflt_kernel; /* use the reflected kernel */
2870 primative = DilateMorphology;
2871 break;
2872 case 4: /* Finish the Close */
2873 this_kernel = rflt_kernel; /* use the reflected kernel */
2874 primative = ErodeMorphology;
2875 break;
2876 }
2877 break;
2878 case EdgeMorphology: /* dilate and erode difference */
2879 primative = DilateMorphology;
2880 if ( stage_loop == 2 ) {
2881 save_image = curr_image; /* save the image difference */
2882 curr_image = (Image *) image;
2883 primative = ErodeMorphology;
2884 }
2885 break;
2886 case CorrelateMorphology:
2887 /* A Correlation is a Convolution with a reflected kernel.
2888 ** However a Convolution is a weighted sum using a reflected
2889 ** kernel. It may seem stange to convert a Correlation into a
2890 ** Convolution as the Correlation is the simplier method, but
2891 ** Convolution is much more commonly used, and it makes sense to
2892 ** implement it directly so as to avoid the need to duplicate the
2893 ** kernel when it is not required (which is typically the
2894 ** default).
2895 */
2896 this_kernel = rflt_kernel; /* use the reflected kernel */
2897 primative = ConvolveMorphology;
2898 break;
2899 default:
anthony47f5d062010-05-23 07:47:50 +00002900 break;
2901 }
anthony9eb4f742010-05-18 02:45:54 +00002902
anthony47f5d062010-05-23 07:47:50 +00002903 /* Extra information for debugging compound operations */
2904 if ( verbose == MagickTrue ) {
2905 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002906 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002907 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2908 method_loop, stage_loop );
2909 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002910 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002911 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2912 method_loop );
2913 else
2914 v_info[0] = '\0';
2915 }
2916
2917 /* Loop 4: Iterate the kernel with primative */
2918 kernel_loop = 0;
2919 kernel_changed = 0;
2920 changed = 1;
2921 while ( kernel_loop < kernel_limit && changed > 0 ) {
2922 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002923
2924 /* Create a destination image, if not yet defined */
2925 if ( work_image == (Image *) NULL )
2926 {
2927 work_image=CloneImage(image,0,0,MagickTrue,exception);
2928 if (work_image == (Image *) NULL)
2929 goto error_cleanup;
2930 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2931 {
2932 InheritException(exception,&work_image->exception);
2933 goto error_cleanup;
2934 }
2935 }
2936
anthony47f5d062010-05-23 07:47:50 +00002937 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002938 count++;
anthony47f5d062010-05-23 07:47:50 +00002939 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002940 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002941 kernel_changed += changed;
2942 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002943
anthony47f5d062010-05-23 07:47:50 +00002944 if ( verbose == MagickTrue ) {
2945 if ( kernel_loop > 1 )
2946 fprintf(stderr, "\n"); /* add end-of-line from previous */
2947 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2948 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2949 ( this_kernel == rflt_kernel ) ? "*" : "",
2950 method_loop+kernel_loop-1, kernel_number, count, changed);
2951 }
anthony9eb4f742010-05-18 02:45:54 +00002952 /* prepare next loop */
2953 { Image *tmp = work_image; /* swap images for iteration */
2954 work_image = curr_image;
2955 curr_image = tmp;
2956 }
2957 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002958 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002959
anthony47f5d062010-05-23 07:47:50 +00002960 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002961
anthony47f5d062010-05-23 07:47:50 +00002962 if ( verbose == MagickTrue && kernel_changed != changed )
2963 fprintf(stderr, " Total %lu", kernel_changed);
2964 if ( verbose == MagickTrue && stage_loop < stage_limit )
2965 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002966
2967#if 0
anthony47f5d062010-05-23 07:47:50 +00002968 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
2969 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
2970 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
2971 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
2972 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002973#endif
2974
anthony47f5d062010-05-23 07:47:50 +00002975 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002976
anthony47f5d062010-05-23 07:47:50 +00002977 /* Final Post-processing for some Compound Methods
2978 **
2979 ** The removal of any 'Sync' channel flag in the Image Compositon
2980 ** below ensures the methematical compose method is applied in a
2981 ** purely mathematical way, and only to the selected channels.
2982 ** Turn off SVG composition 'alpha blending'.
2983 */
2984 switch( method ) {
2985 case EdgeOutMorphology:
2986 case EdgeInMorphology:
2987 case TopHatMorphology:
2988 case BottomHatMorphology:
2989 if ( verbose == MagickTrue )
2990 fprintf(stderr, "\n%s: Difference with original image",
2991 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2992 (void) CompositeImageChannel(curr_image,
2993 (ChannelType) (channel & ~SyncChannels),
2994 DifferenceCompositeOp, image, 0, 0);
2995 break;
2996 case EdgeMorphology:
2997 if ( verbose == MagickTrue )
2998 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
2999 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3000 (void) CompositeImageChannel(curr_image,
3001 (ChannelType) (channel & ~SyncChannels),
3002 DifferenceCompositeOp, save_image, 0, 0);
3003 save_image = DestroyImage(save_image); /* finished with save image */
3004 break;
3005 default:
3006 break;
3007 }
3008
3009 /* multi-kernel handling: re-iterate, or compose results */
3010 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003011 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003012 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003013 { if ( verbose == MagickTrue ) {
3014 if ( this_kernel->next != (KernelInfo *) NULL )
3015 fprintf(stderr, " (re-iterate)");
3016 else
3017 fprintf(stderr, " (done)");
3018 }
3019 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003020 }
anthony47f5d062010-05-23 07:47:50 +00003021 else if ( rslt_image == (Image *) NULL)
3022 { if ( verbose == MagickTrue )
3023 fprintf(stderr, " (save for compose)");
3024 rslt_image = curr_image;
3025 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003026 }
anthony47f5d062010-05-23 07:47:50 +00003027 else
3028 { /* add the new 'current' result to the composition
3029 **
3030 ** The removal of any 'Sync' channel flag in the Image Compositon
3031 ** below ensures the methematical compose method is applied in a
3032 ** purely mathematical way, and only to the selected channels.
3033 ** Turn off SVG composition 'alpha blending'.
3034 */
3035 if ( verbose == MagickTrue )
3036 fprintf(stderr, " (compose \"%s\")",
3037 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3038 (void) CompositeImageChannel(rslt_image,
3039 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3040 curr_image, 0, 0);
3041 curr_image = (Image *) image; /* continue with original image */
3042 }
3043 if ( verbose == MagickTrue )
3044 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003045
anthony47f5d062010-05-23 07:47:50 +00003046 /* loop to the next kernel in a multi-kernel list */
3047 norm_kernel = norm_kernel->next;
3048 if ( rflt_kernel != (KernelInfo *) NULL )
3049 rflt_kernel = rflt_kernel->next;
3050 kernel_number++;
3051 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003052
anthony47f5d062010-05-23 07:47:50 +00003053 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003054
anthony9eb4f742010-05-18 02:45:54 +00003055 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003056
anthony47f5d062010-05-23 07:47:50 +00003057 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003058error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003059 if ( curr_image != (Image *) NULL &&
3060 curr_image != rslt_image &&
3061 curr_image != image )
3062 curr_image = DestroyImage(curr_image);
3063 if ( rslt_image != (Image *) NULL )
3064 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003065exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003066 if ( curr_image != (Image *) NULL &&
3067 curr_image != rslt_image &&
3068 curr_image != image )
3069 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003070 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003071 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003072 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003073 save_image = DestroyImage(save_image);
3074 if ( reflected_kernel != (KernelInfo *) NULL )
3075 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3076 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003077}
3078
3079/*
3080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3081% %
3082% %
3083% %
3084% M o r p h o l o g y I m a g e C h a n n e l %
3085% %
3086% %
3087% %
3088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089%
3090% MorphologyImageChannel() applies a user supplied kernel to the image
3091% according to the given mophology method.
3092%
3093% This function applies any and all user defined settings before calling
3094% the above internal function MorphologyApply().
3095%
3096% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003097% * Output Bias for Convolution and correlation ("-bias")
3098% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3099% This can also includes the addition of a scaled unity kernel.
3100% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003101%
3102% The format of the MorphologyImage method is:
3103%
3104% Image *MorphologyImage(const Image *image,MorphologyMethod method,
3105% const long iterations,KernelInfo *kernel,ExceptionInfo *exception)
3106%
3107% Image *MorphologyImageChannel(const Image *image, const ChannelType
3108% channel,MorphologyMethod method,const long iterations,
3109% KernelInfo *kernel,ExceptionInfo *exception)
3110%
3111% A description of each parameter follows:
3112%
3113% o image: the image.
3114%
3115% o method: the morphology method to be applied.
3116%
3117% o iterations: apply the operation this many times (or no change).
3118% A value of -1 means loop until no change found.
3119% How this is applied may depend on the morphology method.
3120% Typically this is a value of 1.
3121%
3122% o channel: the channel type.
3123%
3124% o kernel: An array of double representing the morphology kernel.
3125% Warning: kernel may be normalized for the Convolve method.
3126%
3127% o exception: return any errors or warnings in this structure.
3128%
3129*/
3130
3131MagickExport Image *MorphologyImageChannel(const Image *image,
3132 const ChannelType channel,const MorphologyMethod method,
3133 const long iterations,const KernelInfo *kernel,ExceptionInfo *exception)
3134{
3135 const char
3136 *artifact;
3137
3138 KernelInfo
3139 *curr_kernel;
3140
anthony47f5d062010-05-23 07:47:50 +00003141 CompositeOperator
3142 compose;
3143
anthony9eb4f742010-05-18 02:45:54 +00003144 Image
3145 *morphology_image;
3146
3147
anthony46a369d2010-05-19 02:41:48 +00003148 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3149 * This is done BEFORE the ShowKernelInfo() function is called so that
3150 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003151 */
3152 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003153 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003154 {
3155 artifact = GetImageArtifact(image,"convolve:scale");
3156 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003157 if ( curr_kernel == kernel )
3158 curr_kernel = CloneKernelInfo(kernel);
3159 if (curr_kernel == (KernelInfo *) NULL) {
3160 curr_kernel=DestroyKernelInfo(curr_kernel);
3161 return((Image *) NULL);
3162 }
anthony46a369d2010-05-19 02:41:48 +00003163 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003164 }
3165 }
3166
3167 /* display the (normalized) kernel via stderr */
3168 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003169 if ( artifact == (const char *) NULL)
3170 artifact = GetImageArtifact(image,"convolve:showkernel");
3171 if ( artifact == (const char *) NULL)
3172 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003173 if ( artifact != (const char *) NULL)
3174 ShowKernelInfo(curr_kernel);
3175
anthony47f5d062010-05-23 07:47:50 +00003176 /* override the default handling of multi-kernel morphology results
3177 * if 'Undefined' use the default method
3178 * if 'None' (default for 'Convolve') re-iterate previous result
3179 * otherwise merge resulting images using compose method given
3180 */
3181 compose = UndefinedCompositeOp; /* use default for method */
3182 artifact = GetImageArtifact(image,"morphology:compose");
3183 if ( artifact != (const char *) NULL)
3184 compose = (CompositeOperator) ParseMagickOption(
3185 MagickComposeOptions,MagickFalse,artifact);
3186
anthony9eb4f742010-05-18 02:45:54 +00003187 /* Apply the Morphology */
3188 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003189 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003190
3191 /* Cleanup and Exit */
3192 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003193 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003194 return(morphology_image);
3195}
3196
3197MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
3198 method, const long iterations,const KernelInfo *kernel, ExceptionInfo
3199 *exception)
3200{
3201 Image
3202 *morphology_image;
3203
3204 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3205 iterations,kernel,exception);
3206 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003207}
anthony83ba99b2010-01-24 08:48:15 +00003208
3209/*
3210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3211% %
3212% %
3213% %
anthony4fd27e22010-02-07 08:17:18 +00003214+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003215% %
3216% %
3217% %
3218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3219%
anthony46a369d2010-05-19 02:41:48 +00003220% RotateKernelInfo() rotates the kernel by the angle given.
3221%
3222% Currently it is restricted to 90 degree angles, of either 1D kernels
3223% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3224% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003225%
anthony4fd27e22010-02-07 08:17:18 +00003226% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003227%
anthony4fd27e22010-02-07 08:17:18 +00003228% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003229%
3230% A description of each parameter follows:
3231%
3232% o kernel: the Morphology/Convolution kernel
3233%
3234% o angle: angle to rotate in degrees
3235%
anthony46a369d2010-05-19 02:41:48 +00003236% This function is currently internal to this module only, but can be exported
3237% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003238*/
anthony4fd27e22010-02-07 08:17:18 +00003239static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003240{
anthony1b2bc0a2010-05-12 05:25:22 +00003241 /* angle the lower kernels first */
3242 if ( kernel->next != (KernelInfo *) NULL)
3243 RotateKernelInfo(kernel->next, angle);
3244
anthony83ba99b2010-01-24 08:48:15 +00003245 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3246 **
3247 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3248 */
3249
3250 /* Modulus the angle */
3251 angle = fmod(angle, 360.0);
3252 if ( angle < 0 )
3253 angle += 360.0;
3254
anthony3c10fc82010-05-13 02:40:51 +00003255 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003256 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003257
anthony3dd0f622010-05-13 12:57:32 +00003258 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003259 switch (kernel->type) {
3260 /* These built-in kernels are cylindrical kernels, rotating is useless */
3261 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003262 case DOGKernel:
3263 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003264 case PeaksKernel:
3265 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003266 case ChebyshevKernel:
3267 case ManhattenKernel:
3268 case EuclideanKernel:
3269 return;
3270
3271 /* These may be rotatable at non-90 angles in the future */
3272 /* but simply rotating them in multiples of 90 degrees is useless */
3273 case SquareKernel:
3274 case DiamondKernel:
3275 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003276 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003277 return;
3278
3279 /* These only allows a +/-90 degree rotation (by transpose) */
3280 /* A 180 degree rotation is useless */
3281 case BlurKernel:
3282 case RectangleKernel:
3283 if ( 135.0 < angle && angle <= 225.0 )
3284 return;
3285 if ( 225.0 < angle && angle <= 315.0 )
3286 angle -= 180;
3287 break;
3288
anthony3dd0f622010-05-13 12:57:32 +00003289 default:
anthony83ba99b2010-01-24 08:48:15 +00003290 break;
3291 }
anthony3c10fc82010-05-13 02:40:51 +00003292 /* Attempt rotations by 45 degrees */
3293 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3294 {
3295 if ( kernel->width == 3 && kernel->height == 3 )
3296 { /* Rotate a 3x3 square by 45 degree angle */
3297 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003298 kernel->values[0] = kernel->values[3];
3299 kernel->values[3] = kernel->values[6];
3300 kernel->values[6] = kernel->values[7];
3301 kernel->values[7] = kernel->values[8];
3302 kernel->values[8] = kernel->values[5];
3303 kernel->values[5] = kernel->values[2];
3304 kernel->values[2] = kernel->values[1];
3305 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003306 /* rotate non-centered origin */
3307 if ( kernel->x != 1 || kernel->y != 1 ) {
3308 long x,y;
3309 x = (long) kernel->x-1;
3310 y = (long) kernel->y-1;
3311 if ( x == y ) x = 0;
3312 else if ( x == 0 ) x = -y;
3313 else if ( x == -y ) y = 0;
3314 else if ( y == 0 ) y = x;
3315 kernel->x = (unsigned long) x+1;
3316 kernel->y = (unsigned long) y+1;
3317 }
anthony43c49252010-05-18 10:59:50 +00003318 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3319 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003320 }
3321 else
3322 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3323 }
3324 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3325 {
3326 if ( kernel->width == 1 || kernel->height == 1 )
3327 { /* Do a transpose of the image, which results in a 90
3328 ** degree rotation of a 1 dimentional kernel
3329 */
3330 long
3331 t;
3332 t = (long) kernel->width;
3333 kernel->width = kernel->height;
3334 kernel->height = (unsigned long) t;
3335 t = kernel->x;
3336 kernel->x = kernel->y;
3337 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003338 if ( kernel->width == 1 ) {
3339 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3340 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3341 } else {
3342 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3343 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3344 }
anthony3c10fc82010-05-13 02:40:51 +00003345 }
3346 else if ( kernel->width == kernel->height )
3347 { /* Rotate a square array of values by 90 degrees */
anthony1d45eb92010-05-25 11:13:23 +00003348 { register unsigned long
3349 i,j,x,y;
3350 register MagickRealType
3351 *k,t;
3352 k=kernel->values;
3353 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3354 for( j=0, y=kernel->height-1; j<y; j++, y--)
3355 { t = k[i+j*kernel->width];
3356 k[i+j*kernel->width] = k[j+x*kernel->width];
3357 k[j+x*kernel->width] = k[x+y*kernel->width];
3358 k[x+y*kernel->width] = k[y+i*kernel->width];
3359 k[y+i*kernel->width] = t;
3360 }
3361 }
3362 /* rotate the origin - relative to center of array */
3363 { register long x,y;
3364 x = (long) kernel->x*2-kernel->width+1;
3365 y = (long) kernel->y*2-kernel->height+1;
3366 kernel->x = (unsigned long) ( -y +kernel->width-1)/2;
3367 kernel->y = (unsigned long) ( +x +kernel->height-1)/2;
3368 }
anthony43c49252010-05-18 10:59:50 +00003369 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3370 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003371 }
3372 else
3373 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3374 }
anthony83ba99b2010-01-24 08:48:15 +00003375 if ( 135.0 < angle && angle <= 225.0 )
3376 {
anthony43c49252010-05-18 10:59:50 +00003377 /* For a 180 degree rotation - also know as a reflection
3378 * This is actually a very very common operation!
3379 * Basically all that is needed is a reversal of the kernel data!
3380 * And a reflection of the origon
3381 */
anthony83ba99b2010-01-24 08:48:15 +00003382 unsigned long
3383 i,j;
3384 register double
3385 *k,t;
3386
3387 k=kernel->values;
3388 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3389 t=k[i], k[i]=k[j], k[j]=t;
3390
anthony930be612010-02-08 04:26:15 +00003391 kernel->x = (long) kernel->width - kernel->x - 1;
3392 kernel->y = (long) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003393 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3394 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003395 }
anthony3c10fc82010-05-13 02:40:51 +00003396 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003397 * In the future some form of non-orthogonal angled rotates could be
3398 * performed here, posibily with a linear kernel restriction.
3399 */
3400
3401#if 0
anthony3c10fc82010-05-13 02:40:51 +00003402 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003403 */
3404 unsigned long
3405 y;
cristy150989e2010-02-01 14:59:39 +00003406 register long
anthony83ba99b2010-01-24 08:48:15 +00003407 x,r;
3408 register double
3409 *k,t;
3410
3411 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3412 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3413 t=k[x], k[x]=k[r], k[r]=t;
3414
cristyc99304f2010-02-01 15:26:27 +00003415 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003416 angle = fmod(angle+180.0, 360.0);
3417 }
3418#endif
3419 return;
3420}
3421
3422/*
3423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3424% %
3425% %
3426% %
anthony46a369d2010-05-19 02:41:48 +00003427% S c a l e G e o m e t r y K e r n e l I n f o %
3428% %
3429% %
3430% %
3431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3432%
3433% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3434% provided as a "-set option:convolve:scale {geometry}" user setting,
3435% and modifies the kernel according to the parsed arguments of that setting.
3436%
3437% The first argument (and any normalization flags) are passed to
3438% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3439% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3440% into the scaled/normalized kernel.
3441%
3442% The format of the ScaleKernelInfo method is:
3443%
3444% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3445% const MagickStatusType normalize_flags )
3446%
3447% A description of each parameter follows:
3448%
3449% o kernel: the Morphology/Convolution kernel to modify
3450%
3451% o geometry:
3452% The geometry string to parse, typically from the user provided
3453% "-set option:convolve:scale {geometry}" setting.
3454%
3455*/
3456MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3457 const char *geometry)
3458{
3459 GeometryFlags
3460 flags;
3461 GeometryInfo
3462 args;
3463
3464 SetGeometryInfo(&args);
3465 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3466
3467#if 0
3468 /* For Debugging Geometry Input */
3469 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3470 flags, args.rho, args.sigma, args.xi, args.psi );
3471#endif
3472
3473 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3474 args.rho *= 0.01, args.sigma *= 0.01;
3475
3476 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3477 args.rho = 1.0;
3478 if ( (flags & SigmaValue) == 0 )
3479 args.sigma = 0.0;
3480
3481 /* Scale/Normalize the input kernel */
3482 ScaleKernelInfo(kernel, args.rho, flags);
3483
3484 /* Add Unity Kernel, for blending with original */
3485 if ( (flags & SigmaValue) != 0 )
3486 UnityAddKernelInfo(kernel, args.sigma);
3487
3488 return;
3489}
3490/*
3491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3492% %
3493% %
3494% %
cristy6771f1e2010-03-05 19:43:39 +00003495% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003496% %
3497% %
3498% %
3499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3500%
anthony1b2bc0a2010-05-12 05:25:22 +00003501% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3502% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003503%
anthony999bb2c2010-02-18 12:38:01 +00003504% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003505% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003506%
anthony46a369d2010-05-19 02:41:48 +00003507% If either of the two 'normalize_flags' are given the kernel will first be
3508% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003509%
3510% Kernel normalization ('normalize_flags' given) is designed to ensure that
3511% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003512% morphology methods will fall into -1.0 to +1.0 range. Note that for
3513% non-HDRI versions of IM this may cause images to have any negative results
3514% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003515%
3516% More specifically. Kernels which only contain positive values (such as a
3517% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003518% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003519%
3520% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3521% the kernel will be scaled by the absolute of the sum of kernel values, so
3522% that it will generally fall within the +/- 1.0 range.
3523%
3524% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3525% will be scaled by just the sum of the postive values, so that its output
3526% range will again fall into the +/- 1.0 range.
3527%
3528% For special kernels designed for locating shapes using 'Correlate', (often
3529% only containing +1 and -1 values, representing foreground/brackground
3530% matching) a special normalization method is provided to scale the positive
3531% values seperatally to those of the negative values, so the kernel will be
3532% forced to become a zero-sum kernel better suited to such searches.
3533%
anthony1b2bc0a2010-05-12 05:25:22 +00003534% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003535% attributes within the kernel structure have been correctly set during the
3536% kernels creation.
3537%
3538% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003539% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3540% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003541%
anthony4fd27e22010-02-07 08:17:18 +00003542% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003543%
anthony999bb2c2010-02-18 12:38:01 +00003544% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3545% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003546%
3547% A description of each parameter follows:
3548%
3549% o kernel: the Morphology/Convolution kernel
3550%
anthony999bb2c2010-02-18 12:38:01 +00003551% o scaling_factor:
3552% multiply all values (after normalization) by this factor if not
3553% zero. If the kernel is normalized regardless of any flags.
3554%
3555% o normalize_flags:
3556% GeometryFlags defining normalization method to use.
3557% specifically: NormalizeValue, CorrelateNormalizeValue,
3558% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003559%
3560*/
cristy6771f1e2010-03-05 19:43:39 +00003561MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3562 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003563{
cristy150989e2010-02-01 14:59:39 +00003564 register long
anthonycc6c8362010-01-25 04:14:01 +00003565 i;
3566
anthony999bb2c2010-02-18 12:38:01 +00003567 register double
3568 pos_scale,
3569 neg_scale;
3570
anthony46a369d2010-05-19 02:41:48 +00003571 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003572 if ( kernel->next != (KernelInfo *) NULL)
3573 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3574
anthony46a369d2010-05-19 02:41:48 +00003575 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003576 pos_scale = 1.0;
3577 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003578 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003579 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003580 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003581 else
anthonyf4e00312010-05-20 12:06:35 +00003582 /* zero-summing kernel */
3583 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003584 }
anthony46a369d2010-05-19 02:41:48 +00003585 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003586 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3587 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3588 ? kernel->positive_range : 1.0;
3589 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3590 ? -kernel->negative_range : 1.0;
3591 }
3592 else
3593 neg_scale = pos_scale;
3594
3595 /* finialize scaling_factor for positive and negative components */
3596 pos_scale = scaling_factor/pos_scale;
3597 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003598
cristy150989e2010-02-01 14:59:39 +00003599 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003600 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003601 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003602
anthony999bb2c2010-02-18 12:38:01 +00003603 /* convolution output range */
3604 kernel->positive_range *= pos_scale;
3605 kernel->negative_range *= neg_scale;
3606 /* maximum and minimum values in kernel */
3607 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3608 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3609
anthony46a369d2010-05-19 02:41:48 +00003610 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003611 if ( scaling_factor < MagickEpsilon ) {
3612 double t;
3613 t = kernel->positive_range;
3614 kernel->positive_range = kernel->negative_range;
3615 kernel->negative_range = t;
3616 t = kernel->maximum;
3617 kernel->maximum = kernel->minimum;
3618 kernel->minimum = 1;
3619 }
anthonycc6c8362010-01-25 04:14:01 +00003620
3621 return;
3622}
3623
3624/*
3625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3626% %
3627% %
3628% %
anthony46a369d2010-05-19 02:41:48 +00003629% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003630% %
3631% %
3632% %
3633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3634%
anthony4fd27e22010-02-07 08:17:18 +00003635% ShowKernelInfo() outputs the details of the given kernel defination to
3636% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003637%
3638% The format of the ShowKernel method is:
3639%
anthony4fd27e22010-02-07 08:17:18 +00003640% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003641%
3642% A description of each parameter follows:
3643%
3644% o kernel: the Morphology/Convolution kernel
3645%
anthony83ba99b2010-01-24 08:48:15 +00003646*/
anthony4fd27e22010-02-07 08:17:18 +00003647MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003648{
anthony7a01dcf2010-05-11 12:25:52 +00003649 KernelInfo
3650 *k;
anthony83ba99b2010-01-24 08:48:15 +00003651
anthony43c49252010-05-18 10:59:50 +00003652 unsigned long
anthony7a01dcf2010-05-11 12:25:52 +00003653 c, i, u, v;
3654
3655 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3656
anthony46a369d2010-05-19 02:41:48 +00003657 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003658 if ( kernel->next != (KernelInfo *) NULL )
cristye96405a2010-05-19 02:24:31 +00003659 fprintf(stderr, " #%lu", c );
anthony43c49252010-05-18 10:59:50 +00003660 fprintf(stderr, " \"%s",
3661 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3662 if ( fabs(k->angle) > MagickEpsilon )
3663 fprintf(stderr, "@%lg", k->angle);
anthonya648a302010-05-27 02:14:36 +00003664 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",
anthony43c49252010-05-18 10:59:50 +00003665 k->width, k->height,
3666 k->x, k->y );
anthony7a01dcf2010-05-11 12:25:52 +00003667 fprintf(stderr,
3668 " with values from %.*lg to %.*lg\n",
3669 GetMagickPrecision(), k->minimum,
3670 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003671 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003672 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003673 GetMagickPrecision(), k->positive_range);
3674 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3675 fprintf(stderr, " (Zero-Summing)\n");
3676 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3677 fprintf(stderr, " (Normalized)\n");
3678 else
3679 fprintf(stderr, " (Sum %.*lg)\n",
3680 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003681 for (i=v=0; v < k->height; v++) {
anthony46a369d2010-05-19 02:41:48 +00003682 fprintf(stderr, "%2lu:", v );
anthony43c49252010-05-18 10:59:50 +00003683 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003684 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003685 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003686 else
anthonyf4e00312010-05-20 12:06:35 +00003687 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003688 GetMagickPrecision(), k->values[i]);
3689 fprintf(stderr,"\n");
3690 }
anthony83ba99b2010-01-24 08:48:15 +00003691 }
3692}
anthonycc6c8362010-01-25 04:14:01 +00003693
3694/*
3695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3696% %
3697% %
3698% %
anthony43c49252010-05-18 10:59:50 +00003699% U n i t y A d d K e r n a l I n f o %
3700% %
3701% %
3702% %
3703%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3704%
3705% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3706% to the given pre-scaled and normalized Kernel. This in effect adds that
3707% amount of the original image into the resulting convolution kernel. This
3708% value is usually provided by the user as a percentage value in the
3709% 'convolve:scale' setting.
3710%
3711% The resulting effect is to either convert a 'zero-summing' edge detection
3712% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3713% kernel.
3714%
3715% Alternativally by using a purely positive kernel, and using a negative
3716% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3717% as a "Gaussian") into a 'unsharp' kernel.
3718%
anthony46a369d2010-05-19 02:41:48 +00003719% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003720%
3721% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3722%
3723% A description of each parameter follows:
3724%
3725% o kernel: the Morphology/Convolution kernel
3726%
3727% o scale:
3728% scaling factor for the unity kernel to be added to
3729% the given kernel.
3730%
anthony43c49252010-05-18 10:59:50 +00003731*/
3732MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3733 const double scale)
3734{
anthony46a369d2010-05-19 02:41:48 +00003735 /* do the other kernels in a multi-kernel list first */
3736 if ( kernel->next != (KernelInfo *) NULL)
3737 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003738
anthony46a369d2010-05-19 02:41:48 +00003739 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003740 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003741 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003742
3743 return;
3744}
3745
3746/*
3747%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3748% %
3749% %
3750% %
3751% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003752% %
3753% %
3754% %
3755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3756%
3757% ZeroKernelNans() replaces any special 'nan' value that may be present in
3758% the kernel with a zero value. This is typically done when the kernel will
3759% be used in special hardware (GPU) convolution processors, to simply
3760% matters.
3761%
3762% The format of the ZeroKernelNans method is:
3763%
anthony46a369d2010-05-19 02:41:48 +00003764% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003765%
3766% A description of each parameter follows:
3767%
3768% o kernel: the Morphology/Convolution kernel
3769%
anthonycc6c8362010-01-25 04:14:01 +00003770*/
anthonyc4c86e02010-01-27 09:30:32 +00003771MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003772{
anthony43c49252010-05-18 10:59:50 +00003773 register unsigned long
anthonycc6c8362010-01-25 04:14:01 +00003774 i;
3775
anthony46a369d2010-05-19 02:41:48 +00003776 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003777 if ( kernel->next != (KernelInfo *) NULL)
3778 ZeroKernelNans(kernel->next);
3779
anthony43c49252010-05-18 10:59:50 +00003780 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003781 if ( IsNan(kernel->values[i]) )
3782 kernel->values[i] = 0.0;
3783
3784 return;
3785}