blob: 1a71ef91bc08c5b6b6d4bff5896f04f00b6fc888 [file] [log] [blame]
cristy701db312009-11-20 03:14:08 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M OOO RRRR PPPP H H OOO L OOO GGGG Y Y %
7% MM MM O O R R P P H H O O L O O G Y Y %
8% M M M O O RRRR PPPP HHHHH O O L O O G GGG Y %
9% M M O O R R P H H O O L O O G G Y %
10% M M OOO R R P H H OOO LLLLL OOO GGG Y %
11% %
12% %
13% MagickCore Morphology Methods %
14% %
15% Software Design %
16% Anthony Thyssen %
anthonyc94cdb02010-01-06 08:15:29 +000017% January 2010 %
cristy701db312009-11-20 03:14:08 +000018% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy701db312009-11-20 03:14:08 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
anthony1b2bc0a2010-05-12 05:25:22 +000036% Morpology is the the application of various kernels, of any size and even
anthony602ab9b2010-01-05 08:06:50 +000037% shape, to a image in various ways (typically binary, but not always).
cristy701db312009-11-20 03:14:08 +000038%
anthony602ab9b2010-01-05 08:06:50 +000039% Convolution (weighted sum or average) is just one specific type of
40% morphology. Just one that is very common for image bluring and sharpening
41% effects. Not only 2D Gaussian blurring, but also 2-pass 1D Blurring.
42%
43% This module provides not only a general morphology function, and the ability
44% to apply more advanced or iterative morphologies, but also functions for the
45% generation of many different types of kernel arrays from user supplied
46% arguments. Prehaps even the generation of a kernel from a small image.
cristy701db312009-11-20 03:14:08 +000047*/
48
49/*
50 Include declarations.
51*/
52#include "magick/studio.h"
anthony602ab9b2010-01-05 08:06:50 +000053#include "magick/artifact.h"
cristy701db312009-11-20 03:14:08 +000054#include "magick/cache-view.h"
55#include "magick/color-private.h"
56#include "magick/enhance.h"
57#include "magick/exception.h"
58#include "magick/exception-private.h"
anthony602ab9b2010-01-05 08:06:50 +000059#include "magick/gem.h"
cristy701db312009-11-20 03:14:08 +000060#include "magick/hashmap.h"
61#include "magick/image.h"
cristybba804b2010-01-05 15:39:59 +000062#include "magick/image-private.h"
cristy701db312009-11-20 03:14:08 +000063#include "magick/list.h"
anthony29188a82010-01-22 10:12:34 +000064#include "magick/magick.h"
cristy701db312009-11-20 03:14:08 +000065#include "magick/memory_.h"
66#include "magick/monitor-private.h"
67#include "magick/morphology.h"
anthony46a369d2010-05-19 02:41:48 +000068#include "magick/morphology-private.h"
anthony602ab9b2010-01-05 08:06:50 +000069#include "magick/option.h"
cristy701db312009-11-20 03:14:08 +000070#include "magick/pixel-private.h"
71#include "magick/prepress.h"
72#include "magick/quantize.h"
73#include "magick/registry.h"
74#include "magick/semaphore.h"
75#include "magick/splay-tree.h"
76#include "magick/statistic.h"
77#include "magick/string_.h"
anthony602ab9b2010-01-05 08:06:50 +000078#include "magick/string-private.h"
79#include "magick/token.h"
cristya29d45f2010-03-05 21:14:54 +000080
anthonyc3cd15b2010-05-27 06:05:40 +000081
anthony602ab9b2010-01-05 08:06:50 +000082/*
anthonyc3cd15b2010-05-27 06:05:40 +000083** The following test is for special floating point numbers of value NaN (not
84** a number), that may be used within a Kernel Definition. NaN's are defined
85** as part of the IEEE standard for floating point number representation.
86**
87** These are used as a Kernel value to mean that this kernel position is not
88** part of the kernel neighbourhood for convolution or morphology processing,
89** and thus should be ignored. This allows the use of 'shaped' kernels.
90**
91** The special properity that two NaN's are never equal, even if they are from
92** the same variable allow you to test if a value is special NaN value.
93**
94** This macro IsNaN() is thus is only true if the value given is NaN.
cristya29d45f2010-03-05 21:14:54 +000095*/
anthony602ab9b2010-01-05 08:06:50 +000096#define IsNan(a) ((a)!=(a))
97
anthony29188a82010-01-22 10:12:34 +000098/*
cristya29d45f2010-03-05 21:14:54 +000099 Other global definitions used by module.
100*/
anthony29188a82010-01-22 10:12:34 +0000101static inline double MagickMin(const double x,const double y)
102{
103 return( x < y ? x : y);
104}
105static inline double MagickMax(const double x,const double y)
106{
107 return( x > y ? x : y);
108}
109#define Minimize(assign,value) assign=MagickMin(assign,value)
110#define Maximize(assign,value) assign=MagickMax(assign,value)
111
anthonyc4c86e02010-01-27 09:30:32 +0000112/* Currently these are only internal to this module */
113static void
anthony46a369d2010-05-19 02:41:48 +0000114 CalcKernelMetaData(KernelInfo *),
cristyeb8db6d2010-05-24 18:34:11 +0000115 ExpandKernelInfo(KernelInfo *, const double),
cristyef656912010-03-05 19:54:59 +0000116 RotateKernelInfo(KernelInfo *, double);
anthony602ab9b2010-01-05 08:06:50 +0000117
anthony3dd0f622010-05-13 12:57:32 +0000118
119/* Quick function to find last kernel in a kernel list */
120static inline KernelInfo *LastKernelInfo(KernelInfo *kernel)
121{
122 while (kernel->next != (KernelInfo *) NULL)
123 kernel = kernel->next;
124 return(kernel);
125}
126
127
anthony602ab9b2010-01-05 08:06:50 +0000128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% %
131% %
132% %
anthony83ba99b2010-01-24 08:48:15 +0000133% A c q u i r e K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000134% %
135% %
136% %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
cristy2be15382010-01-21 02:38:03 +0000139% AcquireKernelInfo() takes the given string (generally supplied by the
anthony602ab9b2010-01-05 08:06:50 +0000140% user) and converts it into a Morphology/Convolution Kernel. This allows
141% users to specify a kernel from a number of pre-defined kernels, or to fully
142% specify their own kernel for a specific Convolution or Morphology
143% Operation.
144%
145% The kernel so generated can be any rectangular array of floating point
146% values (doubles) with the 'control point' or 'pixel being affected'
147% anywhere within that array of values.
148%
anthony83ba99b2010-01-24 08:48:15 +0000149% Previously IM was restricted to a square of odd size using the exact
150% center as origin, this is no longer the case, and any rectangular kernel
151% with any value being declared the origin. This in turn allows the use of
152% highly asymmetrical kernels.
anthony602ab9b2010-01-05 08:06:50 +0000153%
154% The floating point values in the kernel can also include a special value
anthony83ba99b2010-01-24 08:48:15 +0000155% known as 'nan' or 'not a number' to indicate that this value is not part
156% of the kernel array. This allows you to shaped the kernel within its
157% rectangular area. That is 'nan' values provide a 'mask' for the kernel
158% shape. However at least one non-nan value must be provided for correct
159% working of a kernel.
anthony602ab9b2010-01-05 08:06:50 +0000160%
anthony7a01dcf2010-05-11 12:25:52 +0000161% The returned kernel should be freed using the DestroyKernelInfo() when you
162% are finished with it. Do not free this memory yourself.
anthony602ab9b2010-01-05 08:06:50 +0000163%
164% Input kernel defintion strings can consist of any of three types.
165%
anthony29188a82010-01-22 10:12:34 +0000166% "name:args"
167% Select from one of the built in kernels, using the name and
168% geometry arguments supplied. See AcquireKernelBuiltIn()
anthony602ab9b2010-01-05 08:06:50 +0000169%
anthony43c49252010-05-18 10:59:50 +0000170% "WxH[+X+Y][^@]:num, num, num ..."
anthony1b2bc0a2010-05-12 05:25:22 +0000171% a kernel of size W by H, with W*H floating point numbers following.
anthony602ab9b2010-01-05 08:06:50 +0000172% the 'center' can be optionally be defined at +X+Y (such that +0+0
anthony29188a82010-01-22 10:12:34 +0000173% is top left corner). If not defined the pixel in the center, for
174% odd sizes, or to the immediate top or left of center for even sizes
175% is automatically selected.
anthony602ab9b2010-01-05 08:06:50 +0000176%
anthony43c49252010-05-18 10:59:50 +0000177% If a '^' is included the kernel expanded with 90-degree rotations,
178% While a '@' will allow you to expand a 3x3 kernel using 45-degree
179% circular rotates.
180%
anthony29188a82010-01-22 10:12:34 +0000181% "num, num, num, num, ..."
182% list of floating point numbers defining an 'old style' odd sized
183% square kernel. At least 9 values should be provided for a 3x3
184% square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
185% Values can be space or comma separated. This is not recommended.
anthony602ab9b2010-01-05 08:06:50 +0000186%
anthony7a01dcf2010-05-11 12:25:52 +0000187% You can define a 'list of kernels' which can be used by some morphology
188% operators A list is defined as a semi-colon seperated list kernels.
189%
anthonydbc89892010-05-12 07:05:27 +0000190% " kernel ; kernel ; kernel ; "
anthony7a01dcf2010-05-11 12:25:52 +0000191%
anthony1dd091a2010-05-27 06:31:15 +0000192% Any extra ';' characters, at start, end or between kernel defintions are
anthony43c49252010-05-18 10:59:50 +0000193% simply ignored.
194%
195% Note that 'name' kernels will start with an alphabetic character while the
196% new kernel specification has a ':' character in its specification string.
197% If neither is the case, it is assumed an old style of a simple list of
198% numbers generating a odd-sized square kernel has been given.
anthony7a01dcf2010-05-11 12:25:52 +0000199%
anthony602ab9b2010-01-05 08:06:50 +0000200% The format of the AcquireKernal method is:
201%
cristy2be15382010-01-21 02:38:03 +0000202% KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000203%
204% A description of each parameter follows:
205%
206% o kernel_string: the Morphology/Convolution kernel wanted.
207%
208*/
209
anthonyc84dce52010-05-07 05:42:23 +0000210/* This was separated so that it could be used as a separate
anthony5ef8e942010-05-11 06:51:12 +0000211** array input handling function, such as for -color-matrix
anthonyc84dce52010-05-07 05:42:23 +0000212*/
anthony5ef8e942010-05-11 06:51:12 +0000213static KernelInfo *ParseKernelArray(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000214{
cristy2be15382010-01-21 02:38:03 +0000215 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000216 *kernel;
217
218 char
219 token[MaxTextExtent];
220
anthony602ab9b2010-01-05 08:06:50 +0000221 const char
anthony5ef8e942010-05-11 06:51:12 +0000222 *p,
223 *end;
anthony602ab9b2010-01-05 08:06:50 +0000224
anthonyc84dce52010-05-07 05:42:23 +0000225 register long
226 i;
anthony602ab9b2010-01-05 08:06:50 +0000227
anthony29188a82010-01-22 10:12:34 +0000228 double
229 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
230
anthony43c49252010-05-18 10:59:50 +0000231 MagickStatusType
232 flags;
233
234 GeometryInfo
235 args;
236
cristy2be15382010-01-21 02:38:03 +0000237 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
238 if (kernel == (KernelInfo *)NULL)
anthony602ab9b2010-01-05 08:06:50 +0000239 return(kernel);
240 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000241 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthony7a01dcf2010-05-11 12:25:52 +0000242 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000243 kernel->type = UserDefinedKernel;
anthony7a01dcf2010-05-11 12:25:52 +0000244 kernel->next = (KernelInfo *) NULL;
cristyd43a46b2010-01-21 02:13:41 +0000245 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000246
anthony5ef8e942010-05-11 06:51:12 +0000247 /* find end of this specific kernel definition string */
248 end = strchr(kernel_string, ';');
249 if ( end == (char *) NULL )
250 end = strchr(kernel_string, '\0');
251
anthony43c49252010-05-18 10:59:50 +0000252 /* clear flags - for Expanding kernal lists thorugh rotations */
253 flags = NoValue;
254
anthony602ab9b2010-01-05 08:06:50 +0000255 /* Has a ':' in argument - New user kernel specification */
256 p = strchr(kernel_string, ':');
anthony5ef8e942010-05-11 06:51:12 +0000257 if ( p != (char *) NULL && p < end)
anthony602ab9b2010-01-05 08:06:50 +0000258 {
anthony602ab9b2010-01-05 08:06:50 +0000259 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
cristy150989e2010-02-01 14:59:39 +0000260 memcpy(token, kernel_string, (size_t) (p-kernel_string));
anthony602ab9b2010-01-05 08:06:50 +0000261 token[p-kernel_string] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000262 SetGeometryInfo(&args);
anthony602ab9b2010-01-05 08:06:50 +0000263 flags = ParseGeometry(token, &args);
anthony602ab9b2010-01-05 08:06:50 +0000264
anthony29188a82010-01-22 10:12:34 +0000265 /* Size handling and checks of geometry settings */
anthony602ab9b2010-01-05 08:06:50 +0000266 if ( (flags & WidthValue) == 0 ) /* if no width then */
267 args.rho = args.sigma; /* then width = height */
268 if ( args.rho < 1.0 ) /* if width too small */
269 args.rho = 1.0; /* then width = 1 */
270 if ( args.sigma < 1.0 ) /* if height too small */
271 args.sigma = args.rho; /* then height = width */
272 kernel->width = (unsigned long)args.rho;
273 kernel->height = (unsigned long)args.sigma;
274
275 /* Offset Handling and Checks */
276 if ( args.xi < 0.0 || args.psi < 0.0 )
anthony83ba99b2010-01-24 08:48:15 +0000277 return(DestroyKernelInfo(kernel));
cristyc99304f2010-02-01 15:26:27 +0000278 kernel->x = ((flags & XValue)!=0) ? (long)args.xi
cristy150989e2010-02-01 14:59:39 +0000279 : (long) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000280 kernel->y = ((flags & YValue)!=0) ? (long)args.psi
cristy150989e2010-02-01 14:59:39 +0000281 : (long) (kernel->height-1)/2;
cristyc99304f2010-02-01 15:26:27 +0000282 if ( kernel->x >= (long) kernel->width ||
283 kernel->y >= (long) kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000284 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000285
286 p++; /* advance beyond the ':' */
287 }
288 else
anthonyc84dce52010-05-07 05:42:23 +0000289 { /* ELSE - Old old specification, forming odd-square kernel */
anthony602ab9b2010-01-05 08:06:50 +0000290 /* count up number of values given */
291 p=(const char *) kernel_string;
cristya699b172010-01-06 16:48:49 +0000292 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
anthony29188a82010-01-22 10:12:34 +0000293 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony5ef8e942010-05-11 06:51:12 +0000294 for (i=0; p < end; i++)
anthony602ab9b2010-01-05 08:06:50 +0000295 {
296 GetMagickToken(p,&p,token);
297 if (*token == ',')
298 GetMagickToken(p,&p,token);
299 }
300 /* set the size of the kernel - old sized square */
301 kernel->width = kernel->height= (unsigned long) sqrt((double) i+1.0);
cristyc99304f2010-02-01 15:26:27 +0000302 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000303 p=(const char *) kernel_string;
anthony29188a82010-01-22 10:12:34 +0000304 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
305 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000306 }
307
308 /* Read in the kernel values from rest of input string argument */
309 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
310 kernel->height*sizeof(double));
311 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000312 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000313
cristyc99304f2010-02-01 15:26:27 +0000314 kernel->minimum = +MagickHuge;
315 kernel->maximum = -MagickHuge;
316 kernel->negative_range = kernel->positive_range = 0.0;
anthonyc84dce52010-05-07 05:42:23 +0000317
anthony5ef8e942010-05-11 06:51:12 +0000318 for (i=0; (i < (long) (kernel->width*kernel->height)) && (p < end); i++)
anthony602ab9b2010-01-05 08:06:50 +0000319 {
320 GetMagickToken(p,&p,token);
321 if (*token == ',')
322 GetMagickToken(p,&p,token);
anthony29188a82010-01-22 10:12:34 +0000323 if ( LocaleCompare("nan",token) == 0
anthonyc84dce52010-05-07 05:42:23 +0000324 || LocaleCompare("-",token) == 0 ) {
anthony29188a82010-01-22 10:12:34 +0000325 kernel->values[i] = nan; /* do not include this value in kernel */
326 }
327 else {
328 kernel->values[i] = StringToDouble(token);
329 ( kernel->values[i] < 0)
cristyc99304f2010-02-01 15:26:27 +0000330 ? ( kernel->negative_range += kernel->values[i] )
331 : ( kernel->positive_range += kernel->values[i] );
332 Minimize(kernel->minimum, kernel->values[i]);
333 Maximize(kernel->maximum, kernel->values[i]);
anthony29188a82010-01-22 10:12:34 +0000334 }
anthony602ab9b2010-01-05 08:06:50 +0000335 }
anthony29188a82010-01-22 10:12:34 +0000336
anthony5ef8e942010-05-11 06:51:12 +0000337 /* sanity check -- no more values in kernel definition */
338 GetMagickToken(p,&p,token);
339 if ( *token != '\0' && *token != ';' && *token != '\'' )
340 return(DestroyKernelInfo(kernel));
341
anthonyc84dce52010-05-07 05:42:23 +0000342#if 0
343 /* this was the old method of handling a incomplete kernel */
cristy150989e2010-02-01 14:59:39 +0000344 if ( i < (long) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000345 Minimize(kernel->minimum, kernel->values[i]);
346 Maximize(kernel->maximum, kernel->values[i]);
cristy150989e2010-02-01 14:59:39 +0000347 for ( ; i < (long) (kernel->width*kernel->height); i++)
anthony29188a82010-01-22 10:12:34 +0000348 kernel->values[i]=0.0;
349 }
anthonyc84dce52010-05-07 05:42:23 +0000350#else
351 /* Number of values for kernel was not enough - Report Error */
352 if ( i < (long) (kernel->width*kernel->height) )
353 return(DestroyKernelInfo(kernel));
354#endif
355
356 /* check that we recieved at least one real (non-nan) value! */
357 if ( kernel->minimum == MagickHuge )
358 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000359
anthony43c49252010-05-18 10:59:50 +0000360 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel size */
361 ExpandKernelInfo(kernel, 45.0);
362 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel size */
363 ExpandKernelInfo(kernel, 90.0);
364
anthony602ab9b2010-01-05 08:06:50 +0000365 return(kernel);
366}
anthonyc84dce52010-05-07 05:42:23 +0000367
anthony43c49252010-05-18 10:59:50 +0000368static KernelInfo *ParseKernelName(const char *kernel_string)
anthonyc84dce52010-05-07 05:42:23 +0000369{
anthonyf0176c32010-05-23 23:08:57 +0000370 KernelInfo
371 *kernel;
372
anthonyc84dce52010-05-07 05:42:23 +0000373 char
374 token[MaxTextExtent];
375
anthony5ef8e942010-05-11 06:51:12 +0000376 long
377 type;
378
anthonyc84dce52010-05-07 05:42:23 +0000379 const char
anthony7a01dcf2010-05-11 12:25:52 +0000380 *p,
381 *end;
anthonyc84dce52010-05-07 05:42:23 +0000382
383 MagickStatusType
384 flags;
385
386 GeometryInfo
387 args;
388
anthonyc84dce52010-05-07 05:42:23 +0000389 /* Parse special 'named' kernel */
anthony5ef8e942010-05-11 06:51:12 +0000390 GetMagickToken(kernel_string,&p,token);
anthonyc84dce52010-05-07 05:42:23 +0000391 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
392 if ( type < 0 || type == UserDefinedKernel )
anthony5ef8e942010-05-11 06:51:12 +0000393 return((KernelInfo *)NULL); /* not a valid named kernel */
anthonyc84dce52010-05-07 05:42:23 +0000394
395 while (((isspace((int) ((unsigned char) *p)) != 0) ||
anthony5ef8e942010-05-11 06:51:12 +0000396 (*p == ',') || (*p == ':' )) && (*p != '\0') && (*p != ';'))
anthonyc84dce52010-05-07 05:42:23 +0000397 p++;
anthony7a01dcf2010-05-11 12:25:52 +0000398
399 end = strchr(p, ';'); /* end of this kernel defintion */
400 if ( end == (char *) NULL )
401 end = strchr(p, '\0');
402
403 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
404 memcpy(token, p, (size_t) (end-p));
405 token[end-p] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000406 SetGeometryInfo(&args);
anthony7a01dcf2010-05-11 12:25:52 +0000407 flags = ParseGeometry(token, &args);
anthonyc84dce52010-05-07 05:42:23 +0000408
anthony3c10fc82010-05-13 02:40:51 +0000409#if 0
410 /* For Debugging Geometry Input */
anthony46a369d2010-05-19 02:41:48 +0000411 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
anthony3c10fc82010-05-13 02:40:51 +0000412 flags, args.rho, args.sigma, args.xi, args.psi );
413#endif
414
anthonyc84dce52010-05-07 05:42:23 +0000415 /* special handling of missing values in input string */
416 switch( type ) {
anthony5ef8e942010-05-11 06:51:12 +0000417 case RectangleKernel:
418 if ( (flags & WidthValue) == 0 ) /* if no width then */
419 args.rho = args.sigma; /* then width = height */
420 if ( args.rho < 1.0 ) /* if width too small */
421 args.rho = 3; /* then width = 3 */
422 if ( args.sigma < 1.0 ) /* if height too small */
423 args.sigma = args.rho; /* then height = width */
424 if ( (flags & XValue) == 0 ) /* center offset if not defined */
425 args.xi = (double)(((long)args.rho-1)/2);
426 if ( (flags & YValue) == 0 )
427 args.psi = (double)(((long)args.sigma-1)/2);
428 break;
429 case SquareKernel:
430 case DiamondKernel:
431 case DiskKernel:
432 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +0000433 case CrossKernel:
anthony5ef8e942010-05-11 06:51:12 +0000434 /* If no scale given (a 0 scale is valid! - set it to 1.0 */
435 if ( (flags & HeightValue) == 0 )
436 args.sigma = 1.0;
437 break;
anthonyc1061722010-05-14 06:23:49 +0000438 case RingKernel:
439 if ( (flags & XValue) == 0 )
440 args.xi = 1.0;
441 break;
anthony5ef8e942010-05-11 06:51:12 +0000442 case ChebyshevKernel:
443 case ManhattenKernel:
444 case EuclideanKernel:
anthony43c49252010-05-18 10:59:50 +0000445 if ( (flags & HeightValue) == 0 ) /* no distance scale */
446 args.sigma = 100.0; /* default distance scaling */
447 else if ( (flags & AspectValue ) != 0 ) /* '!' flag */
448 args.sigma = QuantumRange/(args.sigma+1); /* maximum pixel distance */
449 else if ( (flags & PercentValue ) != 0 ) /* '%' flag */
450 args.sigma *= QuantumRange/100.0; /* percentage of color range */
anthony5ef8e942010-05-11 06:51:12 +0000451 break;
452 default:
453 break;
anthonyc84dce52010-05-07 05:42:23 +0000454 }
455
anthonyf0176c32010-05-23 23:08:57 +0000456 kernel = AcquireKernelBuiltIn((KernelInfoType)type, &args);
457
458 /* global expand to rotated kernel list - only for single kernels */
459 if ( kernel->next == (KernelInfo *) NULL ) {
460 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel args */
461 ExpandKernelInfo(kernel, 45.0);
462 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel args */
463 ExpandKernelInfo(kernel, 90.0);
464 }
465
466 return(kernel);
anthonyc84dce52010-05-07 05:42:23 +0000467}
468
anthony5ef8e942010-05-11 06:51:12 +0000469MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
470{
anthony7a01dcf2010-05-11 12:25:52 +0000471
472 KernelInfo
anthonydbc89892010-05-12 07:05:27 +0000473 *kernel,
anthony43c49252010-05-18 10:59:50 +0000474 *new_kernel;
anthony7a01dcf2010-05-11 12:25:52 +0000475
anthony5ef8e942010-05-11 06:51:12 +0000476 char
477 token[MaxTextExtent];
478
anthony7a01dcf2010-05-11 12:25:52 +0000479 const char
anthonydbc89892010-05-12 07:05:27 +0000480 *p;
anthony7a01dcf2010-05-11 12:25:52 +0000481
anthonye108a3f2010-05-12 07:24:03 +0000482 unsigned long
483 kernel_number;
484
anthonydbc89892010-05-12 07:05:27 +0000485 p = kernel_string;
anthony43c49252010-05-18 10:59:50 +0000486 kernel = NULL;
anthonye108a3f2010-05-12 07:24:03 +0000487 kernel_number = 0;
anthony5ef8e942010-05-11 06:51:12 +0000488
anthonydbc89892010-05-12 07:05:27 +0000489 while ( GetMagickToken(p,NULL,token), *token != '\0' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000490
anthony43c49252010-05-18 10:59:50 +0000491 /* ignore extra or multiple ';' kernel seperators */
anthonydbc89892010-05-12 07:05:27 +0000492 if ( *token != ';' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000493
anthonydbc89892010-05-12 07:05:27 +0000494 /* tokens starting with alpha is a Named kernel */
anthony43c49252010-05-18 10:59:50 +0000495 if (isalpha((int) *token) != 0)
496 new_kernel = ParseKernelName(p);
anthonydbc89892010-05-12 07:05:27 +0000497 else /* otherwise a user defined kernel array */
anthony43c49252010-05-18 10:59:50 +0000498 new_kernel = ParseKernelArray(p);
anthony7a01dcf2010-05-11 12:25:52 +0000499
anthonye108a3f2010-05-12 07:24:03 +0000500 /* Error handling -- this is not proper error handling! */
501 if ( new_kernel == (KernelInfo *) NULL ) {
502 fprintf(stderr, "Failed to parse kernel number #%lu\n", kernel_number);
503 if ( kernel != (KernelInfo *) NULL )
504 kernel=DestroyKernelInfo(kernel);
505 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000506 }
anthonye108a3f2010-05-12 07:24:03 +0000507
508 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000509 if ( kernel == (KernelInfo *) NULL )
510 kernel = new_kernel;
511 else
anthony43c49252010-05-18 10:59:50 +0000512 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000513 }
514
515 /* look for the next kernel in list */
516 p = strchr(p, ';');
517 if ( p == (char *) NULL )
518 break;
519 p++;
520
521 }
anthony7a01dcf2010-05-11 12:25:52 +0000522 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000523}
524
anthony602ab9b2010-01-05 08:06:50 +0000525
526/*
527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528% %
529% %
530% %
531% A c q u i r e K e r n e l B u i l t I n %
532% %
533% %
534% %
535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536%
537% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
538% kernels used for special purposes such as gaussian blurring, skeleton
539% pruning, and edge distance determination.
540%
541% They take a KernelType, and a set of geometry style arguments, which were
542% typically decoded from a user supplied string, or from a more complex
543% Morphology Method that was requested.
544%
545% The format of the AcquireKernalBuiltIn method is:
546%
cristy2be15382010-01-21 02:38:03 +0000547% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000548% const GeometryInfo args)
549%
550% A description of each parameter follows:
551%
552% o type: the pre-defined type of kernel wanted
553%
554% o args: arguments defining or modifying the kernel
555%
556% Convolution Kernels
557%
anthony46a369d2010-05-19 02:41:48 +0000558% Unity
559% the No-Op kernel, also requivelent to Gaussian of sigma zero.
560% Basically a 3x3 kernel of a 1 surrounded by zeros.
561%
anthony3c10fc82010-05-13 02:40:51 +0000562% Gaussian:{radius},{sigma}
563% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000564% The sigma for the curve is required. The resulting kernel is
565% normalized,
566%
567% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000568%
569% NOTE: that the 'radius' is optional, but if provided can limit (clip)
570% the final size of the resulting kernel to a square 2*radius+1 in size.
571% The radius should be at least 2 times that of the sigma value, or
572% sever clipping and aliasing may result. If not given or set to 0 the
573% radius will be determined so as to produce the best minimal error
574% result, which is usally much larger than is normally needed.
575%
anthonyc1061722010-05-14 06:23:49 +0000576% DOG:{radius},{sigma1},{sigma2}
577% "Difference of Gaussians" Kernel.
578% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
579% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
580% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000581%
anthony9eb4f742010-05-18 02:45:54 +0000582% LOG:{radius},{sigma}
583% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
584% The supposed ideal edge detection, zero-summing kernel.
585%
586% An alturnative to this kernel is to use a "DOG" with a sigma ratio of
587% approx 1.6, which can also be applied as a 2 pass "DOB" (see below).
588%
anthonyc1061722010-05-14 06:23:49 +0000589% Blur:{radius},{sigma}[,{angle}]
590% Generates a 1 dimensional or linear gaussian blur, at the angle given
591% (current restricted to orthogonal angles). If a 'radius' is given the
592% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
593% by a 90 degree angle.
594%
595% If 'sigma' is zero, you get a single pixel on a field of zeros.
596%
597% Note that two convolutions with two "Blur" kernels perpendicular to
598% each other, is equivelent to a far larger "Gaussian" kernel with the
599% same sigma value, However it is much faster to apply. This is how the
600% "-blur" operator actually works.
601%
602% DOB:{radius},{sigma1},{sigma2}[,{angle}]
603% "Difference of Blurs" Kernel.
604% As "Blur" but with the 1D gaussian produced by 'sigma2' subtracted
605% from thethe 1D gaussian produced by 'sigma1'.
606% The result is a zero-summing kernel.
607%
608% This can be used to generate a faster "DOG" convolution, in the same
609% way "Blur" can.
anthony602ab9b2010-01-05 08:06:50 +0000610%
anthony3c10fc82010-05-13 02:40:51 +0000611% Comet:{width},{sigma},{angle}
612% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000613% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000614% Adding two such blurs in opposite directions produces a Blur Kernel.
615% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000616%
anthony3c10fc82010-05-13 02:40:51 +0000617% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000618% radius of the kernel.
619%
620% # Still to be implemented...
621% #
anthony4fd27e22010-02-07 08:17:18 +0000622% # Filter2D
623% # Filter1D
624% # Set kernel values using a resize filter, and given scale (sigma)
625% # Cylindrical or Linear. Is this posible with an image?
626% #
anthony602ab9b2010-01-05 08:06:50 +0000627%
anthony3c10fc82010-05-13 02:40:51 +0000628% Named Constant Convolution Kernels
629%
anthonyc1061722010-05-14 06:23:49 +0000630% All these are unscaled, zero-summing kernels by default. As such for
631% non-HDRI version of ImageMagick some form of normalization, user scaling,
632% and biasing the results is recommended, to prevent the resulting image
633% being 'clipped'.
634%
635% The 3x3 kernels (most of these) can be circularly rotated in multiples of
636% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000637%
638% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000639% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000640% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
641% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000642% Type 2 : 3x3 with center:4 edge:1 corner:-2
643% Type 3 : 3x3 with center:4 edge:-2 corner:1
644% Type 5 : 5x5 laplacian
645% Type 7 : 7x7 laplacian
anthony43c49252010-05-18 10:59:50 +0000646% Type 15 : 5x5 LOG (sigma approx 1.4)
647% Type 19 : 9x9 LOG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000648%
649% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000650% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000651% -1, 0, 1
652% -2, 0,-2
653% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000654%
anthonyc1061722010-05-14 06:23:49 +0000655% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000656% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000657% 0, 0, 0
658% -1, 1, 0
659% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000660% Prewitt:{angle}
661% Prewitt Edge convolution kernel (3x3)
662% -1, 0, 1
663% -1, 0, 1
664% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000665% Compass:{angle}
666% Prewitt's "Compass" convolution kernel (3x3)
667% -1, 1, 1
668% -1,-2, 1
669% -1, 1, 1
670% Kirsch:{angle}
671% Kirsch's "Compass" convolution kernel (3x3)
672% -3,-3, 5
673% -3, 0, 5
674% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000675%
anthonye2a60ce2010-05-19 12:30:40 +0000676% FreiChen:{type},{angle}
677% Frei-Chen Edge Detector is a set of 9 unique convolution kernels that
anthonyc3cd15b2010-05-27 06:05:40 +0000678% are specially weighted.
679%
680% Type 0: | -1, 0, 1 |
681% | -sqrt(2), 0, sqrt(2) |
682% | -1, 0, 1 |
683%
684% This is basically the unnormalized discrete kernel that can be used
685% instead ot a Sobel kernel.
686%
687% The next 9 kernel types are specially pre-weighted. They should not
688% be normalized. After applying each to the original image, the results
689% is then added together. The square root of the resulting image is
690% the cosine of the edge, and the direction of the feature detection.
anthonye2a60ce2010-05-19 12:30:40 +0000691%
692% Type 1: | 1, sqrt(2), 1 |
693% | 0, 0, 0 | / 2*sqrt(2)
694% | -1, -sqrt(2), -1 |
695%
696% Type 2: | 1, 0, 1 |
697% | sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
698% | 1, 0, 1 |
699%
700% Type 3: | 0, -1, sqrt(2) |
701% | 1, 0, -1 | / 2*sqrt(2)
702% | -sqrt(2), 1, 0 |
703%
anthony6915d062010-05-19 12:45:51 +0000704% Type 4: | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000705% | -1, 0, 1 | / 2*sqrt(2)
706% | 0, 1, -sqrt(2) |
707%
708% Type 5: | 0, 1, 0 |
709% | -1, 0, -1 | / 2
710% | 0, 1, 0 |
711%
712% Type 6: | -1, 0, 1 |
713% | 0, 0, 0 | / 2
714% | 1, 0, -1 |
715%
anthonyf4e00312010-05-20 12:06:35 +0000716% Type 7: | 1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000717% | -2, 4, -2 | / 6
718% | 1, -2, 1 |
719%
anthonyf4e00312010-05-20 12:06:35 +0000720% Type 8: | -2, 1, -2 |
721% | 1, 4, 1 | / 6
722% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000723%
anthonyf4e00312010-05-20 12:06:35 +0000724% Type 9: | 1, 1, 1 |
725% | 1, 1, 1 | / 3
726% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000727%
728% The first 4 are for edge detection, the next 4 are for line detection
729% and the last is to add a average component to the results.
730%
anthonyc3cd15b2010-05-27 06:05:40 +0000731% Using a special type of '-1' will return all 9 pre-weighted kernels
732% as a multi-kernel list, so that you can use them directly (without
733% normalization) with the special "-set option:morphology:compose Plus"
734% setting to apply the full FreiChen Edge Detection Technique.
735%
anthony1dd091a2010-05-27 06:31:15 +0000736% If 'type' is large it will be taken to be an actual rotation angle for
737% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
738% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
739%
anthonye2a60ce2010-05-19 12:30:40 +0000740%
anthony602ab9b2010-01-05 08:06:50 +0000741% Boolean Kernels
742%
anthony3c10fc82010-05-13 02:40:51 +0000743% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000744% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000745% Kernel size will again be radius*2+1 square and defaults to radius 1,
746% generating a 3x3 kernel that is slightly larger than a square.
747%
anthony3c10fc82010-05-13 02:40:51 +0000748% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000749% Generate a square shaped kernel of size radius*2+1, and defaulting
750% to a 3x3 (radius 1).
751%
anthonyc1061722010-05-14 06:23:49 +0000752% Note that using a larger radius for the "Square" or the "Diamond" is
753% also equivelent to iterating the basic morphological method that many
754% times. However iterating with the smaller radius is actually faster
755% than using a larger kernel radius.
756%
757% Rectangle:{geometry}
758% Simply generate a rectangle of 1's with the size given. You can also
759% specify the location of the 'control point', otherwise the closest
760% pixel to the center of the rectangle is selected.
761%
762% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000763%
anthony3c10fc82010-05-13 02:40:51 +0000764% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000765% Generate a binary disk of the radius given, radius may be a float.
766% Kernel size will be ceil(radius)*2+1 square.
767% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000768% "Disk:1" => "diamond" or "cross:1"
769% "Disk:1.5" => "square"
770% "Disk:2" => "diamond:2"
771% "Disk:2.5" => a general disk shape of radius 2
772% "Disk:2.9" => "square:2"
773% "Disk:3.5" => default - octagonal/disk shape of radius 3
774% "Disk:4.2" => roughly octagonal shape of radius 4
775% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000776% After this all the kernel shape becomes more and more circular.
777%
778% Because a "disk" is more circular when using a larger radius, using a
779% larger radius is preferred over iterating the morphological operation.
780%
anthonyc1061722010-05-14 06:23:49 +0000781% Symbol Dilation Kernels
782%
783% These kernel is not a good general morphological kernel, but is used
784% more for highlighting and marking any single pixels in an image using,
785% a "Dilate" method as appropriate.
786%
787% For the same reasons iterating these kernels does not produce the
788% same result as using a larger radius for the symbol.
789%
anthony3c10fc82010-05-13 02:40:51 +0000790% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000791% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000792% Generate a kernel in the shape of a 'plus' or a 'cross' with
793% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000794%
795% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000796%
anthonyc1061722010-05-14 06:23:49 +0000797% Ring:{radius1},{radius2}[,{scale}]
798% A ring of the values given that falls between the two radii.
799% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
800% This is the 'edge' pixels of the default "Disk" kernel,
801% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000802%
anthony3dd0f622010-05-13 12:57:32 +0000803% Hit and Miss Kernels
804%
805% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000806% Find any peak larger than the pixels the fall between the two radii.
807% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000808% Edges
anthony1d45eb92010-05-25 11:13:23 +0000809% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000810% Corners
811% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000812% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000813% Find single pixel ridges or thin lines
814% Ridges2
815% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000816% Ridges3
817% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000818% LineEnds
819% Find end points of lines (for pruning a skeletion)
820% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000821% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000822% ConvexHull
823% Octagonal thicken kernel, to generate convex hulls of 45 degrees
824% Skeleton
825% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000826%
827% Distance Measuring Kernels
828%
anthonyc1061722010-05-14 06:23:49 +0000829% Different types of distance measuring methods, which are used with the
830% a 'Distance' morphology method for generating a gradient based on
831% distance from an edge of a binary shape, though there is a technique
832% for handling a anti-aliased shape.
833%
834% See the 'Distance' Morphological Method, for information of how it is
835% applied.
836%
anthony3dd0f622010-05-13 12:57:32 +0000837% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000838% Chebyshev Distance (also known as Tchebychev Distance) is a value of
839% one to any neighbour, orthogonal or diagonal. One why of thinking of
840% it is the number of squares a 'King' or 'Queen' in chess needs to
841% traverse reach any other position on a chess board. It results in a
842% 'square' like distance function, but one where diagonals are closer
843% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000844%
anthonyc1061722010-05-14 06:23:49 +0000845% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000846% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
847% Cab metric), is the distance needed when you can only travel in
848% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
849% in chess would travel. It results in a diamond like distances, where
850% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000851%
anthonyc1061722010-05-14 06:23:49 +0000852% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000853% Euclidean Distance is the 'direct' or 'as the crow flys distance.
854% However by default the kernel size only has a radius of 1, which
855% limits the distance to 'Knight' like moves, with only orthogonal and
856% diagonal measurements being correct. As such for the default kernel
857% you will get octagonal like distance function, which is reasonally
858% accurate.
859%
860% However if you use a larger radius such as "Euclidean:4" you will
861% get a much smoother distance gradient from the edge of the shape.
862% Of course a larger kernel is slower to use, and generally not needed.
863%
864% To allow the use of fractional distances that you get with diagonals
865% the actual distance is scaled by a fixed value which the user can
866% provide. This is not actually nessary for either ""Chebyshev" or
867% "Manhatten" distance kernels, but is done for all three distance
868% kernels. If no scale is provided it is set to a value of 100,
869% allowing for a maximum distance measurement of 655 pixels using a Q16
870% version of IM, from any edge. However for small images this can
871% result in quite a dark gradient.
872%
anthony602ab9b2010-01-05 08:06:50 +0000873*/
874
cristy2be15382010-01-21 02:38:03 +0000875MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000876 const GeometryInfo *args)
877{
cristy2be15382010-01-21 02:38:03 +0000878 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000879 *kernel;
880
cristy150989e2010-02-01 14:59:39 +0000881 register long
anthony602ab9b2010-01-05 08:06:50 +0000882 i;
883
884 register long
885 u,
886 v;
887
888 double
889 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
890
anthonyc1061722010-05-14 06:23:49 +0000891 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000892 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000893 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000894 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000895 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000896 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000897 break;
anthony1dd091a2010-05-27 06:31:15 +0000898 case UnityKernel: /* Named Descrete Convolution Kernels */
899 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000900 case SobelKernel:
901 case RobertsKernel:
902 case PrewittKernel:
903 case CompassKernel:
904 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000905 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000906 case CornersKernel: /* Hit and Miss kernels */
907 case LineEndsKernel:
908 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000909 case EdgesKernel:
910 case RidgesKernel:
911 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000912 case ConvexHullKernel:
913 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000914 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000915 /* A pre-generated kernel is not needed */
916 break;
anthony1dd091a2010-05-27 06:31:15 +0000917#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000918 case GaussianKernel:
919 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000920 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000921 case BlurKernel:
922 case DOBKernel:
923 case CometKernel:
924 case DiamondKernel:
925 case SquareKernel:
926 case RectangleKernel:
927 case DiskKernel:
928 case PlusKernel:
929 case CrossKernel:
930 case RingKernel:
931 case PeaksKernel:
932 case ChebyshevKernel:
933 case ManhattenKernel:
934 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000935#else
anthony9eb4f742010-05-18 02:45:54 +0000936 default:
anthony1dd091a2010-05-27 06:31:15 +0000937#endif
anthony9eb4f742010-05-18 02:45:54 +0000938 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000939 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
940 if (kernel == (KernelInfo *) NULL)
941 return(kernel);
942 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000943 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000944 kernel->negative_range = kernel->positive_range = 0.0;
945 kernel->type = type;
946 kernel->next = (KernelInfo *) NULL;
947 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000948 break;
949 }
anthony602ab9b2010-01-05 08:06:50 +0000950
951 switch(type) {
952 /* Convolution Kernels */
953 case GaussianKernel:
anthonyc1061722010-05-14 06:23:49 +0000954 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000955 case LOGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000956 { double
anthonyc1061722010-05-14 06:23:49 +0000957 sigma = fabs(args->sigma),
958 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000959 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000960
anthonyc1061722010-05-14 06:23:49 +0000961 if ( args->rho >= 1.0 )
962 kernel->width = (unsigned long)args->rho*2+1;
anthony9eb4f742010-05-18 02:45:54 +0000963 else if ( (type != DOGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000964 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
965 else
966 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
967 kernel->height = kernel->width;
cristyc99304f2010-02-01 15:26:27 +0000968 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000969 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
970 kernel->height*sizeof(double));
971 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000972 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000973
anthony46a369d2010-05-19 02:41:48 +0000974 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000975 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000976 *
977 * How to do this is currently not known, but appears to be
978 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000979 */
980
981 if ( type == GaussianKernel || type == DOGKernel )
982 { /* Calculate a Gaussian, OR positive half of a DOG */
983 if ( sigma > MagickEpsilon )
984 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
985 B = 1.0/(Magick2PI*sigma*sigma);
986 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
987 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
988 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
989 }
990 else /* limiting case - a unity (normalized Dirac) kernel */
991 { (void) ResetMagickMemory(kernel->values,0, (size_t)
992 kernel->width*kernel->height*sizeof(double));
993 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
994 }
anthonyc1061722010-05-14 06:23:49 +0000995 }
anthony9eb4f742010-05-18 02:45:54 +0000996
anthonyc1061722010-05-14 06:23:49 +0000997 if ( type == DOGKernel )
998 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
999 if ( sigma2 > MagickEpsilon )
1000 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001001 A = 1.0/(2.0*sigma*sigma);
1002 B = 1.0/(Magick2PI*sigma*sigma);
anthonyc1061722010-05-14 06:23:49 +00001003 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1004 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001005 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001006 }
anthony9eb4f742010-05-18 02:45:54 +00001007 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001008 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1009 }
anthony9eb4f742010-05-18 02:45:54 +00001010
1011 if ( type == LOGKernel )
1012 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1013 if ( sigma > MagickEpsilon )
1014 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1015 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
1016 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1017 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1018 { R = ((double)(u*u+v*v))*A;
1019 kernel->values[i] = (1-R)*exp(-R)*B;
1020 }
1021 }
1022 else /* special case - generate a unity kernel */
1023 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1024 kernel->width*kernel->height*sizeof(double));
1025 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1026 }
1027 }
1028
1029 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001030 ** radius, producing a smaller (darker) kernel. Also for very small
1031 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1032 ** producing a very bright kernel.
1033 **
1034 ** Normalization will still be needed.
1035 */
anthony602ab9b2010-01-05 08:06:50 +00001036
anthony3dd0f622010-05-13 12:57:32 +00001037 /* Normalize the 2D Gaussian Kernel
1038 **
anthonyc1061722010-05-14 06:23:49 +00001039 ** NB: a CorrelateNormalize performs a normal Normalize if
1040 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001041 */
anthony46a369d2010-05-19 02:41:48 +00001042 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001043 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001044
1045 break;
1046 }
1047 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +00001048 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001049 { double
anthonyc1061722010-05-14 06:23:49 +00001050 sigma = fabs(args->sigma),
1051 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001052 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001053
anthonyc1061722010-05-14 06:23:49 +00001054 if ( args->rho >= 1.0 )
1055 kernel->width = (unsigned long)args->rho*2+1;
1056 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1057 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1058 else
1059 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001060 kernel->height = 1;
anthonyc1061722010-05-14 06:23:49 +00001061 kernel->x = (long) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001062 kernel->y = 0;
1063 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001064 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1065 kernel->height*sizeof(double));
1066 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001067 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001068
1069#if 1
1070#define KernelRank 3
1071 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1072 ** It generates a gaussian 3 times the width, and compresses it into
1073 ** the expected range. This produces a closer normalization of the
1074 ** resulting kernel, especially for very low sigma values.
1075 ** As such while wierd it is prefered.
1076 **
1077 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001078 **
1079 ** A properly normalized curve is generated (apart from edge clipping)
1080 ** even though we later normalize the result (for edge clipping)
1081 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001082 */
anthonyc1061722010-05-14 06:23:49 +00001083
1084 /* initialize */
cristy150989e2010-02-01 14:59:39 +00001085 v = (long) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001086 (void) ResetMagickMemory(kernel->values,0, (size_t)
1087 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001088 /* Calculate a Positive 1D Gaussian */
1089 if ( sigma > MagickEpsilon )
1090 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001091 A = 1.0/(2.0*sigma*sigma);
1092 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001093 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001094 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001095 }
1096 }
1097 else /* special case - generate a unity kernel */
1098 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001099
1100 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001101 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001102 {
anthonyc1061722010-05-14 06:23:49 +00001103 if ( sigma2 > MagickEpsilon )
1104 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001105 A = 1.0/(2.0*sigma*sigma);
1106 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001107 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001108 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001109 }
anthony9eb4f742010-05-18 02:45:54 +00001110 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001111 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1112 }
anthony602ab9b2010-01-05 08:06:50 +00001113#else
anthonyc1061722010-05-14 06:23:49 +00001114 /* Direct calculation without curve averaging */
1115
1116 /* Calculate a Positive Gaussian */
1117 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001118 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1119 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001120 for ( i=0, u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001121 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001122 }
1123 else /* special case - generate a unity kernel */
1124 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1125 kernel->width*kernel->height*sizeof(double));
1126 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1127 }
anthony9eb4f742010-05-18 02:45:54 +00001128
1129 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001130 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001131 {
anthonyc1061722010-05-14 06:23:49 +00001132 if ( sigma2 > MagickEpsilon )
1133 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001134 A = 1.0/(2.0*sigma*sigma);
1135 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001136 for ( i=0, u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001137 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001138 }
anthony9eb4f742010-05-18 02:45:54 +00001139 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001140 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1141 }
anthony602ab9b2010-01-05 08:06:50 +00001142#endif
anthonyc1061722010-05-14 06:23:49 +00001143 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001144 ** radius, producing a smaller (darker) kernel. Also for very small
1145 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1146 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001147 **
1148 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001149 */
anthonycc6c8362010-01-25 04:14:01 +00001150
anthony602ab9b2010-01-05 08:06:50 +00001151 /* Normalize the 1D Gaussian Kernel
1152 **
anthonyc1061722010-05-14 06:23:49 +00001153 ** NB: a CorrelateNormalize performs a normal Normalize if
1154 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001155 */
anthony46a369d2010-05-19 02:41:48 +00001156 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1157 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001158
anthonyc1061722010-05-14 06:23:49 +00001159 /* rotate the 1D kernel by given angle */
1160 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001161 break;
1162 }
1163 case CometKernel:
1164 { double
anthony9eb4f742010-05-18 02:45:54 +00001165 sigma = fabs(args->sigma),
1166 A;
anthony602ab9b2010-01-05 08:06:50 +00001167
anthony602ab9b2010-01-05 08:06:50 +00001168 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001169 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001170 else
1171 kernel->width = (unsigned long)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001172 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001173 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001174 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001175 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1176 kernel->height*sizeof(double));
1177 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001178 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001179
anthonyc1061722010-05-14 06:23:49 +00001180 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001181 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001182 ** curve to use so may change in the future. The function must be
1183 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001184 **
1185 ** As we are normalizing and not subtracting gaussians,
1186 ** there is no need for a divisor in the gaussian formula
1187 **
anthony43c49252010-05-18 10:59:50 +00001188 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001189 */
anthony9eb4f742010-05-18 02:45:54 +00001190 if ( sigma > MagickEpsilon )
1191 {
anthony602ab9b2010-01-05 08:06:50 +00001192#if 1
1193#define KernelRank 3
anthony9eb4f742010-05-18 02:45:54 +00001194 v = (long) kernel->width*KernelRank; /* start/end points */
1195 (void) ResetMagickMemory(kernel->values,0, (size_t)
1196 kernel->width*sizeof(double));
1197 sigma *= KernelRank; /* simplify the loop expression */
1198 A = 1.0/(2.0*sigma*sigma);
1199 /* B = 1.0/(MagickSQ2PI*sigma); */
1200 for ( u=0; u < v; u++) {
1201 kernel->values[u/KernelRank] +=
1202 exp(-((double)(u*u))*A);
1203 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1204 }
1205 for (i=0; i < (long) kernel->width; i++)
1206 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001207#else
anthony9eb4f742010-05-18 02:45:54 +00001208 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1209 /* B = 1.0/(MagickSQ2PI*sigma); */
1210 for ( i=0; i < (long) kernel->width; i++)
1211 kernel->positive_range +=
1212 kernel->values[i] =
1213 exp(-((double)(i*i))*A);
1214 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001215#endif
anthony9eb4f742010-05-18 02:45:54 +00001216 }
1217 else /* special case - generate a unity kernel */
1218 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1219 kernel->width*kernel->height*sizeof(double));
1220 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1221 kernel->positive_range = 1.0;
1222 }
anthony46a369d2010-05-19 02:41:48 +00001223
1224 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001225 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001226 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001227
anthony999bb2c2010-02-18 12:38:01 +00001228 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1229 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001230 break;
1231 }
anthonyc1061722010-05-14 06:23:49 +00001232
anthony3c10fc82010-05-13 02:40:51 +00001233 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001234 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001235 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001236 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001237 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001238 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001239 break;
anthony9eb4f742010-05-18 02:45:54 +00001240 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001241 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001242 break;
1243 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001244 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1245 break;
1246 case 3:
anthonyc1061722010-05-14 06:23:49 +00001247 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001248 break;
anthony9eb4f742010-05-18 02:45:54 +00001249 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001250 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001251 "5: -4,-1,0,-1,-4 -1,2,3,2,-1 0,3,4,3,0 -1,2,3,2,-1 -4,-1,0,-1,-4");
anthony3c10fc82010-05-13 02:40:51 +00001252 break;
anthony9eb4f742010-05-18 02:45:54 +00001253 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001254 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001255 "7:-10,-5,-2,-1,-2,-5,-10 -5,0,3,4,3,0,-5 -2,3,6,7,6,3,-2 -1,4,7,8,7,4,-1 -2,3,6,7,6,3,-2 -5,0,3,4,3,0,-5 -10,-5,-2,-1,-2,-5,-10" );
anthony3c10fc82010-05-13 02:40:51 +00001256 break;
anthony43c49252010-05-18 10:59:50 +00001257 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001258 kernel=ParseKernelArray(
1259 "5: 0,0,-1,0,0 0,-1,-2,-1,0 -1,-2,16,-2,-1 0,-1,-2,-1,0 0,0,-1,0,0");
1260 break;
anthony43c49252010-05-18 10:59:50 +00001261 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1262 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1263 kernel=ParseKernelArray(
1264 "9: 0,-1,-1,-2,-2,-2,-1,-1,0 -1,-2,-4,-5,-5,-5,-4,-2,-1 -1,-4,-5,-3,-0,-3,-5,-4,-1 -2,-5,-3,@12,@24,@12,-3,-5,-2 -2,-5,-0,@24,@40,@24,-0,-5,-2 -2,-5,-3,@12,@24,@12,-3,-5,-2 -1,-4,-5,-3,-0,-3,-5,-4,-1 -1,-2,-4,-5,-5,-5,-4,-2,-1 0,-1,-1,-2,-2,-2,-1,-1,0");
1265 break;
anthony3c10fc82010-05-13 02:40:51 +00001266 }
1267 if (kernel == (KernelInfo *) NULL)
1268 return(kernel);
1269 kernel->type = type;
1270 break;
1271 }
anthonyc1061722010-05-14 06:23:49 +00001272 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001273 {
anthonyc1061722010-05-14 06:23:49 +00001274 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1275 if (kernel == (KernelInfo *) NULL)
1276 return(kernel);
1277 kernel->type = type;
1278 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1279 break;
1280 }
1281 case RobertsKernel:
1282 {
1283 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1284 if (kernel == (KernelInfo *) NULL)
1285 return(kernel);
1286 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001287 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001288 break;
1289 }
1290 case PrewittKernel:
1291 {
1292 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1293 if (kernel == (KernelInfo *) NULL)
1294 return(kernel);
1295 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001296 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001297 break;
1298 }
1299 case CompassKernel:
1300 {
1301 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1302 if (kernel == (KernelInfo *) NULL)
1303 return(kernel);
1304 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001305 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001306 break;
1307 }
anthony9eb4f742010-05-18 02:45:54 +00001308 case KirschKernel:
1309 {
1310 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1311 if (kernel == (KernelInfo *) NULL)
1312 return(kernel);
1313 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001314 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001315 break;
1316 }
anthonye2a60ce2010-05-19 12:30:40 +00001317 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001318 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1319 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001320 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001321 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001322 case 0:
1323 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1324 if (kernel == (KernelInfo *) NULL)
1325 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001326 kernel->values[3] = -MagickSQ2;
1327 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001328 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001329 break;
anthonye2a60ce2010-05-19 12:30:40 +00001330 case 1:
1331 kernel=ParseKernelArray("3: 1,2,1 0,0,0 -1,2,-1");
1332 if (kernel == (KernelInfo *) NULL)
1333 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001334 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001335 kernel->values[1] = +MagickSQ2;
1336 kernel->values[7] = -MagickSQ2;
1337 CalcKernelMetaData(kernel); /* recalculate meta-data */
1338 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1339 break;
1340 case 2:
1341 kernel=ParseKernelArray("3: 1,0,1 2,0,2 1,0,1");
1342 if (kernel == (KernelInfo *) NULL)
1343 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001344 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001345 kernel->values[3] = +MagickSQ2;
1346 kernel->values[5] = +MagickSQ2;
1347 CalcKernelMetaData(kernel);
1348 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1349 break;
1350 case 3:
1351 kernel=ParseKernelArray("3: 0,-1,2 1,0,-1 -2,1,0");
1352 if (kernel == (KernelInfo *) NULL)
1353 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001354 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001355 kernel->values[2] = +MagickSQ2;
1356 kernel->values[6] = -MagickSQ2;
1357 CalcKernelMetaData(kernel);
1358 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1359 break;
1360 case 4:
anthony6915d062010-05-19 12:45:51 +00001361 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001362 if (kernel == (KernelInfo *) NULL)
1363 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001364 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001365 kernel->values[0] = +MagickSQ2;
1366 kernel->values[8] = -MagickSQ2;
1367 CalcKernelMetaData(kernel);
1368 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1369 break;
1370 case 5:
1371 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1372 if (kernel == (KernelInfo *) NULL)
1373 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001374 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001375 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1376 break;
1377 case 6:
1378 kernel=ParseKernelArray("3: -1,0,1 0,0,0 1,0,-1");
1379 if (kernel == (KernelInfo *) NULL)
1380 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001381 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001382 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1383 break;
1384 case 7:
anthonyf4e00312010-05-20 12:06:35 +00001385 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001386 if (kernel == (KernelInfo *) NULL)
1387 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001388 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001389 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1390 break;
1391 case 8:
1392 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1393 if (kernel == (KernelInfo *) NULL)
1394 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001395 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001396 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1397 break;
1398 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001399 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001400 if (kernel == (KernelInfo *) NULL)
1401 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001402 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001403 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1404 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001405 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001406 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1407 if (kernel == (KernelInfo *) NULL)
1408 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001409 break;
anthonye2a60ce2010-05-19 12:30:40 +00001410 }
anthonyc3cd15b2010-05-27 06:05:40 +00001411 if ( fabs(args->sigma) > MagickEpsilon )
1412 /* Rotate by correctly supplied 'angle' */
1413 RotateKernelInfo(kernel, args->sigma);
1414 else if ( args->rho > 30.0 || args->rho < -30.0 )
1415 /* Rotate by out of bounds 'type' */
1416 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001417 break;
1418 }
1419
anthonyc1061722010-05-14 06:23:49 +00001420 /* Boolean Kernels */
1421 case DiamondKernel:
1422 {
1423 if (args->rho < 1.0)
1424 kernel->width = kernel->height = 3; /* default radius = 1 */
1425 else
1426 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
1427 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1428
1429 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1430 kernel->height*sizeof(double));
1431 if (kernel->values == (double *) NULL)
1432 return(DestroyKernelInfo(kernel));
1433
1434 /* set all kernel values within diamond area to scale given */
1435 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1436 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1437 if ((labs(u)+labs(v)) <= (long)kernel->x)
1438 kernel->positive_range += kernel->values[i] = args->sigma;
1439 else
1440 kernel->values[i] = nan;
1441 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1442 break;
1443 }
1444 case SquareKernel:
1445 case RectangleKernel:
1446 { double
1447 scale;
anthony602ab9b2010-01-05 08:06:50 +00001448 if ( type == SquareKernel )
1449 {
1450 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001451 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001452 else
cristy150989e2010-02-01 14:59:39 +00001453 kernel->width = kernel->height = (unsigned long) (2*args->rho+1);
cristyc99304f2010-02-01 15:26:27 +00001454 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001455 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001456 }
1457 else {
cristy2be15382010-01-21 02:38:03 +00001458 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001459 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001460 return(DestroyKernelInfo(kernel)); /* invalid args given */
anthony602ab9b2010-01-05 08:06:50 +00001461 kernel->width = (unsigned long)args->rho;
1462 kernel->height = (unsigned long)args->sigma;
1463 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1464 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001465 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristyc99304f2010-02-01 15:26:27 +00001466 kernel->x = (long) args->xi;
1467 kernel->y = (long) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001468 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001469 }
1470 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1471 kernel->height*sizeof(double));
1472 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001473 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001474
anthony3dd0f622010-05-13 12:57:32 +00001475 /* set all kernel values to scale given */
cristy150989e2010-02-01 14:59:39 +00001476 u=(long) kernel->width*kernel->height;
1477 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001478 kernel->values[i] = scale;
1479 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1480 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001481 break;
anthony602ab9b2010-01-05 08:06:50 +00001482 }
anthony602ab9b2010-01-05 08:06:50 +00001483 case DiskKernel:
1484 {
anthonyc1061722010-05-14 06:23:49 +00001485 long limit = (long)(args->rho*args->rho);
anthony83ba99b2010-01-24 08:48:15 +00001486 if (args->rho < 0.1) /* default radius approx 3.5 */
1487 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001488 else
1489 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001490 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001491
1492 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1493 kernel->height*sizeof(double));
1494 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001495 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001496
anthony3dd0f622010-05-13 12:57:32 +00001497 /* set all kernel values within disk area to scale given */
1498 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
cristyc99304f2010-02-01 15:26:27 +00001499 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001500 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001501 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001502 else
1503 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001504 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001505 break;
1506 }
1507 case PlusKernel:
1508 {
1509 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001510 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001511 else
1512 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001513 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001514
1515 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1516 kernel->height*sizeof(double));
1517 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001518 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001519
anthony3dd0f622010-05-13 12:57:32 +00001520 /* set all kernel values along axises to given scale */
cristyc99304f2010-02-01 15:26:27 +00001521 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1522 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001523 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1524 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1525 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001526 break;
1527 }
anthony3dd0f622010-05-13 12:57:32 +00001528 case CrossKernel:
1529 {
1530 if (args->rho < 1.0)
1531 kernel->width = kernel->height = 5; /* default radius 2 */
1532 else
1533 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
1534 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1535
1536 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1537 kernel->height*sizeof(double));
1538 if (kernel->values == (double *) NULL)
1539 return(DestroyKernelInfo(kernel));
1540
1541 /* set all kernel values along axises to given scale */
1542 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1543 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1544 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1545 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1546 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1547 break;
1548 }
1549 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001550 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001551 case PeaksKernel:
1552 {
1553 long
1554 limit1,
anthonyc1061722010-05-14 06:23:49 +00001555 limit2,
1556 scale;
anthony3dd0f622010-05-13 12:57:32 +00001557
1558 if (args->rho < args->sigma)
1559 {
1560 kernel->width = ((unsigned long)args->sigma)*2+1;
1561 limit1 = (long)args->rho*args->rho;
1562 limit2 = (long)args->sigma*args->sigma;
1563 }
1564 else
1565 {
1566 kernel->width = ((unsigned long)args->rho)*2+1;
1567 limit1 = (long)args->sigma*args->sigma;
1568 limit2 = (long)args->rho*args->rho;
1569 }
anthonyc1061722010-05-14 06:23:49 +00001570 if ( limit2 <= 0 )
1571 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1572
anthony3dd0f622010-05-13 12:57:32 +00001573 kernel->height = kernel->width;
1574 kernel->x = kernel->y = (long) (kernel->width-1)/2;
1575 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1576 kernel->height*sizeof(double));
1577 if (kernel->values == (double *) NULL)
1578 return(DestroyKernelInfo(kernel));
1579
anthonyc1061722010-05-14 06:23:49 +00001580 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristye96405a2010-05-19 02:24:31 +00001581 scale = (long) (( type == PeaksKernel) ? 0.0 : args->xi);
anthony3dd0f622010-05-13 12:57:32 +00001582 for ( i=0, v= -kernel->y; v <= (long)kernel->y; v++)
1583 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1584 { long radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001585 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001586 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001587 else
1588 kernel->values[i] = nan;
1589 }
cristye96405a2010-05-19 02:24:31 +00001590 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001591 if ( type == PeaksKernel ) {
1592 /* set the central point in the middle */
1593 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1594 kernel->positive_range = 1.0;
1595 kernel->maximum = 1.0;
1596 }
anthony3dd0f622010-05-13 12:57:32 +00001597 break;
1598 }
anthony43c49252010-05-18 10:59:50 +00001599 case EdgesKernel:
1600 {
1601 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1602 if (kernel == (KernelInfo *) NULL)
1603 return(kernel);
1604 kernel->type = type;
1605 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1606 break;
1607 }
anthony3dd0f622010-05-13 12:57:32 +00001608 case CornersKernel:
1609 {
anthony4f1dcb72010-05-14 08:43:10 +00001610 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001611 if (kernel == (KernelInfo *) NULL)
1612 return(kernel);
1613 kernel->type = type;
1614 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1615 break;
1616 }
anthony47f5d062010-05-23 07:47:50 +00001617 case RidgesKernel:
1618 {
anthony24a19842010-05-27 12:18:34 +00001619 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001620 if (kernel == (KernelInfo *) NULL)
1621 return(kernel);
1622 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001623 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001624 break;
1625 }
anthony1d45eb92010-05-25 11:13:23 +00001626 case Ridges2Kernel:
1627 {
1628 KernelInfo
1629 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001630 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001631 if (kernel == (KernelInfo *) NULL)
1632 return(kernel);
1633 kernel->type = type;
1634 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001635#if 0
1636 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001637 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1638 if (new_kernel == (KernelInfo *) NULL)
1639 return(DestroyKernelInfo(kernel));
1640 new_kernel->type = type;
1641 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1642 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001643#endif
1644 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1645 /* Unfortunatally we can not yet rotate a non-square kernel */
1646 /* But then we can't flip a non-symetrical kernel either */
1647 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1648 if (new_kernel == (KernelInfo *) NULL)
1649 return(DestroyKernelInfo(kernel));
1650 new_kernel->type = type;
1651 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001652 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001653 if (new_kernel == (KernelInfo *) NULL)
1654 return(DestroyKernelInfo(kernel));
1655 new_kernel->type = type;
1656 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001657 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001658 if (new_kernel == (KernelInfo *) NULL)
1659 return(DestroyKernelInfo(kernel));
1660 new_kernel->type = type;
1661 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001662 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001663 if (new_kernel == (KernelInfo *) NULL)
1664 return(DestroyKernelInfo(kernel));
1665 new_kernel->type = type;
1666 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001667 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001668 if (new_kernel == (KernelInfo *) NULL)
1669 return(DestroyKernelInfo(kernel));
1670 new_kernel->type = type;
1671 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001672 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001673 if (new_kernel == (KernelInfo *) NULL)
1674 return(DestroyKernelInfo(kernel));
1675 new_kernel->type = type;
1676 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001677 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001678 if (new_kernel == (KernelInfo *) NULL)
1679 return(DestroyKernelInfo(kernel));
1680 new_kernel->type = type;
1681 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001682 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001683 if (new_kernel == (KernelInfo *) NULL)
1684 return(DestroyKernelInfo(kernel));
1685 new_kernel->type = type;
1686 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001687 break;
1688 }
anthony3dd0f622010-05-13 12:57:32 +00001689 case LineEndsKernel:
1690 {
anthony43c49252010-05-18 10:59:50 +00001691 KernelInfo
1692 *new_kernel;
1693 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001694 if (kernel == (KernelInfo *) NULL)
1695 return(kernel);
1696 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001697 ExpandKernelInfo(kernel, 90.0);
1698 /* append second set of 4 kernels */
1699 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1700 if (new_kernel == (KernelInfo *) NULL)
1701 return(DestroyKernelInfo(kernel));
1702 new_kernel->type = type;
1703 ExpandKernelInfo(new_kernel, 90.0);
1704 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001705 break;
1706 }
1707 case LineJunctionsKernel:
1708 {
1709 KernelInfo
1710 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001711 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001712 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001713 if (kernel == (KernelInfo *) NULL)
1714 return(kernel);
1715 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001716 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001717 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001718 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001719 if (new_kernel == (KernelInfo *) NULL)
1720 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001721 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001722 ExpandKernelInfo(new_kernel, 90.0);
1723 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001724 break;
1725 }
anthony3dd0f622010-05-13 12:57:32 +00001726 case ConvexHullKernel:
1727 {
anthony4f1dcb72010-05-14 08:43:10 +00001728 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001729 if (kernel == (KernelInfo *) NULL)
1730 return(kernel);
1731 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001732 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001733 /* append the mirror versions too */
1734 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1735 if (new_kernel == (KernelInfo *) NULL)
1736 return(DestroyKernelInfo(kernel));
1737 new_kernel->type = type;
1738 ExpandKernelInfo(new_kernel, 45.0);
1739 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001740 break;
1741 }
anthony47f5d062010-05-23 07:47:50 +00001742 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001743 { /* what is the best form for skeletonization by thinning? */
anthony47f5d062010-05-23 07:47:50 +00001744#if 0
1745# if 0
1746 kernel=AcquireKernelInfo("Corners;Edges");
1747# else
1748 kernel=AcquireKernelInfo("Edges;Corners");
1749# endif
1750#else
anthony43c49252010-05-18 10:59:50 +00001751 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001752 if (kernel == (KernelInfo *) NULL)
1753 return(kernel);
1754 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001755 ExpandKernelInfo(kernel, 45);
1756 break;
anthony47f5d062010-05-23 07:47:50 +00001757#endif
anthony3dd0f622010-05-13 12:57:32 +00001758 break;
1759 }
anthonya648a302010-05-27 02:14:36 +00001760 case MatKernel: /* experimental - MAT from a Distance Gradient */
1761 {
1762 KernelInfo
1763 *new_kernel;
1764 /* Ridge Kernel but without the diagonal */
1765 kernel=ParseKernelArray("3x1: 0,1,0");
1766 if (kernel == (KernelInfo *) NULL)
1767 return(kernel);
1768 kernel->type = RidgesKernel;
1769 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1770 /* Plus the 2 pixel ridges kernel - no diagonal */
1771 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1772 if (new_kernel == (KernelInfo *) NULL)
1773 return(kernel);
1774 LastKernelInfo(kernel)->next = new_kernel;
1775 break;
1776 }
anthony602ab9b2010-01-05 08:06:50 +00001777 /* Distance Measuring Kernels */
1778 case ChebyshevKernel:
1779 {
anthony602ab9b2010-01-05 08:06:50 +00001780 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001781 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001782 else
1783 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001784 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001785
1786 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1787 kernel->height*sizeof(double));
1788 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001789 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001790
cristyc99304f2010-02-01 15:26:27 +00001791 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1792 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1793 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001794 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001795 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001796 break;
1797 }
1798 case ManhattenKernel:
1799 {
anthony602ab9b2010-01-05 08:06:50 +00001800 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001801 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001802 else
1803 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001804 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001805
1806 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1807 kernel->height*sizeof(double));
1808 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001809 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001810
cristyc99304f2010-02-01 15:26:27 +00001811 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1812 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1813 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001814 args->sigma*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001815 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001816 break;
1817 }
1818 case EuclideanKernel:
1819 {
anthony602ab9b2010-01-05 08:06:50 +00001820 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001821 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001822 else
1823 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
cristyc99304f2010-02-01 15:26:27 +00001824 kernel->x = kernel->y = (long) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001825
1826 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1827 kernel->height*sizeof(double));
1828 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001829 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001830
cristyc99304f2010-02-01 15:26:27 +00001831 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
1832 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
1833 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001834 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001835 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001836 break;
1837 }
anthony46a369d2010-05-19 02:41:48 +00001838 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001839 default:
anthonyc1061722010-05-14 06:23:49 +00001840 {
anthony46a369d2010-05-19 02:41:48 +00001841 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1842 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001843 if (kernel == (KernelInfo *) NULL)
1844 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001845 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001846 break;
1847 }
anthony602ab9b2010-01-05 08:06:50 +00001848 break;
1849 }
1850
1851 return(kernel);
1852}
anthonyc94cdb02010-01-06 08:15:29 +00001853
anthony602ab9b2010-01-05 08:06:50 +00001854/*
1855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1856% %
1857% %
1858% %
cristy6771f1e2010-03-05 19:43:39 +00001859% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001860% %
1861% %
1862% %
1863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864%
anthony1b2bc0a2010-05-12 05:25:22 +00001865% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1866% can be modified without effecting the original. The cloned kernel should
1867% be destroyed using DestoryKernelInfo() when no longer needed.
anthony7a01dcf2010-05-11 12:25:52 +00001868%
cristye6365592010-04-02 17:31:23 +00001869% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001870%
anthony930be612010-02-08 04:26:15 +00001871% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001872%
1873% A description of each parameter follows:
1874%
1875% o kernel: the Morphology/Convolution kernel to be cloned
1876%
1877*/
cristyef656912010-03-05 19:54:59 +00001878MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001879{
1880 register long
1881 i;
1882
cristy19eb6412010-04-23 14:42:29 +00001883 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001884 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001885
1886 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001887 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1888 if (new_kernel == (KernelInfo *) NULL)
1889 return(new_kernel);
1890 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001891
1892 /* replace the values with a copy of the values */
1893 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001894 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001895 if (new_kernel->values == (double *) NULL)
1896 return(DestroyKernelInfo(new_kernel));
anthony4fd27e22010-02-07 08:17:18 +00001897 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001898 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001899
1900 /* Also clone the next kernel in the kernel list */
1901 if ( kernel->next != (KernelInfo *) NULL ) {
1902 new_kernel->next = CloneKernelInfo(kernel->next);
1903 if ( new_kernel->next == (KernelInfo *) NULL )
1904 return(DestroyKernelInfo(new_kernel));
1905 }
1906
anthony7a01dcf2010-05-11 12:25:52 +00001907 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001908}
1909
1910/*
1911%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1912% %
1913% %
1914% %
anthony83ba99b2010-01-24 08:48:15 +00001915% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001916% %
1917% %
1918% %
1919%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1920%
anthony83ba99b2010-01-24 08:48:15 +00001921% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1922% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001923%
anthony83ba99b2010-01-24 08:48:15 +00001924% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001925%
anthony83ba99b2010-01-24 08:48:15 +00001926% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001927%
1928% A description of each parameter follows:
1929%
1930% o kernel: the Morphology/Convolution kernel to be destroyed
1931%
1932*/
1933
anthony83ba99b2010-01-24 08:48:15 +00001934MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001935{
cristy2be15382010-01-21 02:38:03 +00001936 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001937
anthony7a01dcf2010-05-11 12:25:52 +00001938 if ( kernel->next != (KernelInfo *) NULL )
1939 kernel->next = DestroyKernelInfo(kernel->next);
1940
1941 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1942 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001943 return(kernel);
1944}
anthonyc94cdb02010-01-06 08:15:29 +00001945
1946/*
1947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948% %
1949% %
1950% %
anthony3c10fc82010-05-13 02:40:51 +00001951% E x p a n d K e r n e l I n f o %
1952% %
1953% %
1954% %
1955%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1956%
1957% ExpandKernelInfo() takes a single kernel, and expands it into a list
1958% of kernels each incrementally rotated the angle given.
1959%
1960% WARNING: 45 degree rotations only works for 3x3 kernels.
1961% While 90 degree roatations only works for linear and square kernels
1962%
1963% The format of the RotateKernelInfo method is:
1964%
1965% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1966%
1967% A description of each parameter follows:
1968%
1969% o kernel: the Morphology/Convolution kernel
1970%
1971% o angle: angle to rotate in degrees
1972%
1973% This function is only internel to this module, as it is not finalized,
1974% especially with regard to non-orthogonal angles, and rotation of larger
1975% 2D kernels.
1976*/
anthony47f5d062010-05-23 07:47:50 +00001977
1978/* Internal Routine - Return true if two kernels are the same */
1979static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1980 const KernelInfo *kernel2)
1981{
1982 register unsigned long
1983 i;
anthony1d45eb92010-05-25 11:13:23 +00001984
1985 /* check size and origin location */
1986 if ( kernel1->width != kernel2->width
1987 || kernel1->height != kernel2->height
1988 || kernel1->x != kernel2->x
1989 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001990 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001991
1992 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001993 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001994 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001995 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1996 return MagickFalse;
1997 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
1998 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001999 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002000 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2001 return MagickFalse;
2002 }
anthony1d45eb92010-05-25 11:13:23 +00002003
anthony47f5d062010-05-23 07:47:50 +00002004 return MagickTrue;
2005}
2006
2007static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002008{
2009 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002010 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002011 *last;
cristya9a61ad2010-05-13 12:47:41 +00002012
anthony3c10fc82010-05-13 02:40:51 +00002013 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002014 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002015 clone = CloneKernelInfo(last);
2016 RotateKernelInfo(clone, angle);
2017 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002018 break;
cristy84d9b552010-05-24 18:23:54 +00002019 last->next = clone;
2020 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002021 }
cristy84d9b552010-05-24 18:23:54 +00002022 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002023 return;
anthony3c10fc82010-05-13 02:40:51 +00002024}
2025
2026/*
2027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028% %
2029% %
2030% %
anthony46a369d2010-05-19 02:41:48 +00002031+ C a l c M e t a K e r n a l I n f o %
2032% %
2033% %
2034% %
2035%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2036%
2037% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2038% using the kernel values. This should only ne used if it is not posible to
2039% calculate that meta-data in some easier way.
2040%
2041% It is important that the meta-data is correct before ScaleKernelInfo() is
2042% used to perform kernel normalization.
2043%
2044% The format of the CalcKernelMetaData method is:
2045%
2046% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2047%
2048% A description of each parameter follows:
2049%
2050% o kernel: the Morphology/Convolution kernel to modify
2051%
2052% WARNING: Minimum and Maximum values are assumed to include zero, even if
2053% zero is not part of the kernel (as in Gaussian Derived kernels). This
2054% however is not true for flat-shaped morphological kernels.
2055%
2056% WARNING: Only the specific kernel pointed to is modified, not a list of
2057% multiple kernels.
2058%
2059% This is an internal function and not expected to be useful outside this
2060% module. This could change however.
2061*/
2062static void CalcKernelMetaData(KernelInfo *kernel)
2063{
2064 register unsigned long
2065 i;
2066
2067 kernel->minimum = kernel->maximum = 0.0;
2068 kernel->negative_range = kernel->positive_range = 0.0;
2069 for (i=0; i < (kernel->width*kernel->height); i++)
2070 {
2071 if ( fabs(kernel->values[i]) < MagickEpsilon )
2072 kernel->values[i] = 0.0;
2073 ( kernel->values[i] < 0)
2074 ? ( kernel->negative_range += kernel->values[i] )
2075 : ( kernel->positive_range += kernel->values[i] );
2076 Minimize(kernel->minimum, kernel->values[i]);
2077 Maximize(kernel->maximum, kernel->values[i]);
2078 }
2079
2080 return;
2081}
2082
2083/*
2084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2085% %
2086% %
2087% %
anthony9eb4f742010-05-18 02:45:54 +00002088% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002089% %
2090% %
2091% %
2092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2093%
anthony9eb4f742010-05-18 02:45:54 +00002094% MorphologyApply() applies a morphological method, multiple times using
2095% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002096%
anthony9eb4f742010-05-18 02:45:54 +00002097% It is basically equivelent to as MorphologyImageChannel() (see below) but
2098% without user controls, that that function extracts and applies to kernels
2099% and morphology methods.
2100%
2101% More specifically kernels are not normalized/scaled/blended by the
2102% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2103% (-bias setting or image->bias) is passed directly to this function,
2104% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002105%
anthony47f5d062010-05-23 07:47:50 +00002106% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002107%
anthony9eb4f742010-05-18 02:45:54 +00002108% Image *MorphologyApply(const Image *image,MorphologyMethod method,
anthony47f5d062010-05-23 07:47:50 +00002109% const long iterations,const KernelInfo *kernel,
2110% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002111% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002112%
2113% A description of each parameter follows:
2114%
2115% o image: the image.
2116%
2117% o method: the morphology method to be applied.
2118%
2119% o iterations: apply the operation this many times (or no change).
2120% A value of -1 means loop until no change found.
2121% How this is applied may depend on the morphology method.
2122% Typically this is a value of 1.
2123%
2124% o channel: the channel type.
2125%
2126% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002127% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002128%
anthony47f5d062010-05-23 07:47:50 +00002129% o compose: How to handle or merge multi-kernel results.
2130% If 'Undefined' use default of the Morphology method.
2131% If 'No' force image to be re-iterated by each kernel.
2132% Otherwise merge the results using the mathematical compose
2133% method given.
2134%
2135% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002136%
anthony602ab9b2010-01-05 08:06:50 +00002137% o exception: return any errors or warnings in this structure.
2138%
anthony602ab9b2010-01-05 08:06:50 +00002139*/
2140
anthony930be612010-02-08 04:26:15 +00002141
anthony9eb4f742010-05-18 02:45:54 +00002142/* Apply a Morphology Primative to an image using the given kernel.
2143** Two pre-created images must be provided, no image is created.
2144** Returning the number of pixels that changed.
2145*/
anthony46a369d2010-05-19 02:41:48 +00002146static unsigned long MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002147 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002148 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002149{
cristy2be15382010-01-21 02:38:03 +00002150#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002151
2152 long
cristy150989e2010-02-01 14:59:39 +00002153 progress,
anthony29188a82010-01-22 10:12:34 +00002154 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002155 changed;
2156
2157 MagickBooleanType
2158 status;
2159
anthony602ab9b2010-01-05 08:06:50 +00002160 CacheView
2161 *p_view,
2162 *q_view;
2163
anthony602ab9b2010-01-05 08:06:50 +00002164 status=MagickTrue;
2165 changed=0;
2166 progress=0;
2167
anthony602ab9b2010-01-05 08:06:50 +00002168 p_view=AcquireCacheView(image);
2169 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002170
anthonycc6c8362010-01-25 04:14:01 +00002171 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002172 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002173 */
cristyc99304f2010-02-01 15:26:27 +00002174 offx = kernel->x;
2175 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002176 switch(method) {
anthony930be612010-02-08 04:26:15 +00002177 case ConvolveMorphology:
2178 case DilateMorphology:
2179 case DilateIntensityMorphology:
2180 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002181 /* kernel needs to used with reflection about origin */
cristy150989e2010-02-01 14:59:39 +00002182 offx = (long) kernel->width-offx-1;
2183 offy = (long) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002184 break;
anthony5ef8e942010-05-11 06:51:12 +00002185 case ErodeMorphology:
2186 case ErodeIntensityMorphology:
2187 case HitAndMissMorphology:
2188 case ThinningMorphology:
2189 case ThickenMorphology:
2190 /* kernel is user as is, without reflection */
2191 break;
anthony930be612010-02-08 04:26:15 +00002192 default:
anthony9eb4f742010-05-18 02:45:54 +00002193 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002194 break;
anthony29188a82010-01-22 10:12:34 +00002195 }
2196
anthony602ab9b2010-01-05 08:06:50 +00002197#if defined(MAGICKCORE_OPENMP_SUPPORT)
2198 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2199#endif
cristy150989e2010-02-01 14:59:39 +00002200 for (y=0; y < (long) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002201 {
2202 MagickBooleanType
2203 sync;
2204
2205 register const PixelPacket
2206 *restrict p;
2207
2208 register const IndexPacket
2209 *restrict p_indexes;
2210
2211 register PixelPacket
2212 *restrict q;
2213
2214 register IndexPacket
2215 *restrict q_indexes;
2216
cristy150989e2010-02-01 14:59:39 +00002217 register long
anthony602ab9b2010-01-05 08:06:50 +00002218 x;
2219
anthony29188a82010-01-22 10:12:34 +00002220 unsigned long
anthony602ab9b2010-01-05 08:06:50 +00002221 r;
2222
2223 if (status == MagickFalse)
2224 continue;
anthony29188a82010-01-22 10:12:34 +00002225 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2226 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002227 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2228 exception);
2229 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2230 {
2231 status=MagickFalse;
2232 continue;
2233 }
2234 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2235 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002236 r = (image->columns+kernel->width)*offy+offx; /* constant */
2237
cristy150989e2010-02-01 14:59:39 +00002238 for (x=0; x < (long) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002239 {
cristy150989e2010-02-01 14:59:39 +00002240 long
anthony602ab9b2010-01-05 08:06:50 +00002241 v;
2242
cristy150989e2010-02-01 14:59:39 +00002243 register long
anthony602ab9b2010-01-05 08:06:50 +00002244 u;
2245
2246 register const double
2247 *restrict k;
2248
2249 register const PixelPacket
2250 *restrict k_pixels;
2251
2252 register const IndexPacket
2253 *restrict k_indexes;
2254
2255 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002256 result,
2257 min,
2258 max;
anthony602ab9b2010-01-05 08:06:50 +00002259
anthony29188a82010-01-22 10:12:34 +00002260 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002261 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002262 */
anthony602ab9b2010-01-05 08:06:50 +00002263 *q = p[r];
2264 if (image->colorspace == CMYKColorspace)
2265 q_indexes[x] = p_indexes[r];
2266
anthony5ef8e942010-05-11 06:51:12 +00002267 /* Defaults */
2268 min.red =
2269 min.green =
2270 min.blue =
2271 min.opacity =
2272 min.index = (MagickRealType) QuantumRange;
2273 max.red =
2274 max.green =
2275 max.blue =
2276 max.opacity =
2277 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002278 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002279 result.red = (MagickRealType) p[r].red;
2280 result.green = (MagickRealType) p[r].green;
2281 result.blue = (MagickRealType) p[r].blue;
2282 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002283 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002284 if ( image->colorspace == CMYKColorspace)
2285 result.index = (MagickRealType) p_indexes[r];
2286
anthony602ab9b2010-01-05 08:06:50 +00002287 switch (method) {
2288 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002289 /* Set the user defined bias of the weighted average output */
2290 result.red =
2291 result.green =
2292 result.blue =
2293 result.opacity =
2294 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002295 break;
anthony4fd27e22010-02-07 08:17:18 +00002296 case DilateIntensityMorphology:
2297 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002298 /* use a boolean flag indicating when first match found */
2299 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002300 break;
anthony602ab9b2010-01-05 08:06:50 +00002301 default:
anthony602ab9b2010-01-05 08:06:50 +00002302 break;
2303 }
2304
2305 switch ( method ) {
2306 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002307 /* Weighted Average of pixels using reflected kernel
2308 **
2309 ** NOTE for correct working of this operation for asymetrical
2310 ** kernels, the kernel needs to be applied in its reflected form.
2311 ** That is its values needs to be reversed.
2312 **
2313 ** Correlation is actually the same as this but without reflecting
2314 ** the kernel, and thus 'lower-level' that Convolution. However
2315 ** as Convolution is the more common method used, and it does not
2316 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002317 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002318 **
2319 ** Correlation will have its kernel reflected before calling
2320 ** this function to do a Convolve.
2321 **
2322 ** For more details of Correlation vs Convolution see
2323 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2324 */
anthony5ef8e942010-05-11 06:51:12 +00002325 if (((channel & SyncChannels) != 0 ) &&
2326 (image->matte == MagickTrue))
2327 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2328 ** Weight the color channels with Alpha Channel so that
2329 ** transparent pixels are not part of the results.
2330 */
anthony602ab9b2010-01-05 08:06:50 +00002331 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002332 alpha, /* color channel weighting : kernel*alpha */
2333 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002334
2335 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002336 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002337 k_pixels = p;
2338 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002339 for (v=0; v < (long) kernel->height; v++) {
2340 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002341 if ( IsNan(*k) ) continue;
2342 alpha=(*k)*(QuantumScale*(QuantumRange-
2343 k_pixels[u].opacity));
2344 gamma += alpha;
2345 result.red += alpha*k_pixels[u].red;
2346 result.green += alpha*k_pixels[u].green;
2347 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002348 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002349 if ( image->colorspace == CMYKColorspace)
2350 result.index += alpha*k_indexes[u];
2351 }
2352 k_pixels += image->columns+kernel->width;
2353 k_indexes += image->columns+kernel->width;
2354 }
2355 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002356 result.red *= gamma;
2357 result.green *= gamma;
2358 result.blue *= gamma;
2359 result.opacity *= gamma;
2360 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002361 }
anthony5ef8e942010-05-11 06:51:12 +00002362 else
2363 {
2364 /* No 'Sync' flag, or no Alpha involved.
2365 ** Convolution is simple individual channel weigthed sum.
2366 */
2367 k = &kernel->values[ kernel->width*kernel->height-1 ];
2368 k_pixels = p;
2369 k_indexes = p_indexes;
2370 for (v=0; v < (long) kernel->height; v++) {
2371 for (u=0; u < (long) kernel->width; u++, k--) {
2372 if ( IsNan(*k) ) continue;
2373 result.red += (*k)*k_pixels[u].red;
2374 result.green += (*k)*k_pixels[u].green;
2375 result.blue += (*k)*k_pixels[u].blue;
2376 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2377 if ( image->colorspace == CMYKColorspace)
2378 result.index += (*k)*k_indexes[u];
2379 }
2380 k_pixels += image->columns+kernel->width;
2381 k_indexes += image->columns+kernel->width;
2382 }
2383 }
anthony602ab9b2010-01-05 08:06:50 +00002384 break;
2385
anthony4fd27e22010-02-07 08:17:18 +00002386 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002387 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002388 **
2389 ** NOTE that the kernel is not reflected for this operation!
2390 **
2391 ** NOTE: in normal Greyscale Morphology, the kernel value should
2392 ** be added to the real value, this is currently not done, due to
2393 ** the nature of the boolean kernels being used.
2394 */
anthony4fd27e22010-02-07 08:17:18 +00002395 k = kernel->values;
2396 k_pixels = p;
2397 k_indexes = p_indexes;
2398 for (v=0; v < (long) kernel->height; v++) {
2399 for (u=0; u < (long) kernel->width; u++, k++) {
2400 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002401 Minimize(min.red, (double) k_pixels[u].red);
2402 Minimize(min.green, (double) k_pixels[u].green);
2403 Minimize(min.blue, (double) k_pixels[u].blue);
2404 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002405 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002406 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002407 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002408 }
2409 k_pixels += image->columns+kernel->width;
2410 k_indexes += image->columns+kernel->width;
2411 }
2412 break;
2413
anthony5ef8e942010-05-11 06:51:12 +00002414
anthony83ba99b2010-01-24 08:48:15 +00002415 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002416 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002417 **
2418 ** NOTE for correct working of this operation for asymetrical
2419 ** kernels, the kernel needs to be applied in its reflected form.
2420 ** That is its values needs to be reversed.
2421 **
2422 ** NOTE: in normal Greyscale Morphology, the kernel value should
2423 ** be added to the real value, this is currently not done, due to
2424 ** the nature of the boolean kernels being used.
2425 **
2426 */
anthony29188a82010-01-22 10:12:34 +00002427 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002428 k_pixels = p;
2429 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002430 for (v=0; v < (long) kernel->height; v++) {
2431 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002432 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002433 Maximize(max.red, (double) k_pixels[u].red);
2434 Maximize(max.green, (double) k_pixels[u].green);
2435 Maximize(max.blue, (double) k_pixels[u].blue);
2436 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002437 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002438 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002439 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002440 }
2441 k_pixels += image->columns+kernel->width;
2442 k_indexes += image->columns+kernel->width;
2443 }
anthony602ab9b2010-01-05 08:06:50 +00002444 break;
2445
anthony5ef8e942010-05-11 06:51:12 +00002446 case HitAndMissMorphology:
2447 case ThinningMorphology:
2448 case ThickenMorphology:
2449 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2450 **
2451 ** NOTE that the kernel is not reflected for this operation,
2452 ** and consists of both foreground and background pixel
2453 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2454 ** with either Nan or 0.5 values for don't care.
2455 **
2456 ** Note that this can produce negative results, though really
2457 ** only a positive match has any real value.
2458 */
2459 k = kernel->values;
2460 k_pixels = p;
2461 k_indexes = p_indexes;
2462 for (v=0; v < (long) kernel->height; v++) {
2463 for (u=0; u < (long) kernel->width; u++, k++) {
2464 if ( IsNan(*k) ) continue;
2465 if ( (*k) > 0.7 )
2466 { /* minimim of foreground pixels */
2467 Minimize(min.red, (double) k_pixels[u].red);
2468 Minimize(min.green, (double) k_pixels[u].green);
2469 Minimize(min.blue, (double) k_pixels[u].blue);
2470 Minimize(min.opacity,
2471 QuantumRange-(double) k_pixels[u].opacity);
2472 if ( image->colorspace == CMYKColorspace)
2473 Minimize(min.index, (double) k_indexes[u]);
2474 }
2475 else if ( (*k) < 0.3 )
2476 { /* maximum of background pixels */
2477 Maximize(max.red, (double) k_pixels[u].red);
2478 Maximize(max.green, (double) k_pixels[u].green);
2479 Maximize(max.blue, (double) k_pixels[u].blue);
2480 Maximize(max.opacity,
2481 QuantumRange-(double) k_pixels[u].opacity);
2482 if ( image->colorspace == CMYKColorspace)
2483 Maximize(max.index, (double) k_indexes[u]);
2484 }
2485 }
2486 k_pixels += image->columns+kernel->width;
2487 k_indexes += image->columns+kernel->width;
2488 }
2489 /* Pattern Match only if min fg larger than min bg pixels */
2490 min.red -= max.red; Maximize( min.red, 0.0 );
2491 min.green -= max.green; Maximize( min.green, 0.0 );
2492 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2493 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2494 min.index -= max.index; Maximize( min.index, 0.0 );
2495 break;
2496
anthony4fd27e22010-02-07 08:17:18 +00002497 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002498 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2499 **
2500 ** WARNING: the intensity test fails for CMYK and does not
2501 ** take into account the moderating effect of teh alpha channel
2502 ** on the intensity.
2503 **
2504 ** NOTE that the kernel is not reflected for this operation!
2505 */
anthony602ab9b2010-01-05 08:06:50 +00002506 k = kernel->values;
2507 k_pixels = p;
2508 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002509 for (v=0; v < (long) kernel->height; v++) {
2510 for (u=0; u < (long) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002511 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002512 if ( result.red == 0.0 ||
2513 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2514 /* copy the whole pixel - no channel selection */
2515 *q = k_pixels[u];
2516 if ( result.red > 0.0 ) changed++;
2517 result.red = 1.0;
2518 }
anthony602ab9b2010-01-05 08:06:50 +00002519 }
2520 k_pixels += image->columns+kernel->width;
2521 k_indexes += image->columns+kernel->width;
2522 }
anthony602ab9b2010-01-05 08:06:50 +00002523 break;
2524
anthony83ba99b2010-01-24 08:48:15 +00002525 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002526 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2527 **
2528 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002529 ** take into account the moderating effect of the alpha channel
2530 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002531 **
2532 ** NOTE for correct working of this operation for asymetrical
2533 ** kernels, the kernel needs to be applied in its reflected form.
2534 ** That is its values needs to be reversed.
2535 */
anthony29188a82010-01-22 10:12:34 +00002536 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002537 k_pixels = p;
2538 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002539 for (v=0; v < (long) kernel->height; v++) {
2540 for (u=0; u < (long) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002541 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2542 if ( result.red == 0.0 ||
2543 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2544 /* copy the whole pixel - no channel selection */
2545 *q = k_pixels[u];
2546 if ( result.red > 0.0 ) changed++;
2547 result.red = 1.0;
2548 }
anthony602ab9b2010-01-05 08:06:50 +00002549 }
2550 k_pixels += image->columns+kernel->width;
2551 k_indexes += image->columns+kernel->width;
2552 }
anthony602ab9b2010-01-05 08:06:50 +00002553 break;
2554
anthony5ef8e942010-05-11 06:51:12 +00002555
anthony602ab9b2010-01-05 08:06:50 +00002556 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002557 /* Add kernel Value and select the minimum value found.
2558 ** The result is a iterative distance from edge of image shape.
2559 **
2560 ** All Distance Kernels are symetrical, but that may not always
2561 ** be the case. For example how about a distance from left edges?
2562 ** To work correctly with asymetrical kernels the reflected kernel
2563 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002564 **
2565 ** Actually this is really a GreyErode with a negative kernel!
2566 **
anthony930be612010-02-08 04:26:15 +00002567 */
anthony29188a82010-01-22 10:12:34 +00002568 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002569 k_pixels = p;
2570 k_indexes = p_indexes;
cristy150989e2010-02-01 14:59:39 +00002571 for (v=0; v < (long) kernel->height; v++) {
2572 for (u=0; u < (long) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002573 if ( IsNan(*k) ) continue;
2574 Minimize(result.red, (*k)+k_pixels[u].red);
2575 Minimize(result.green, (*k)+k_pixels[u].green);
2576 Minimize(result.blue, (*k)+k_pixels[u].blue);
2577 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2578 if ( image->colorspace == CMYKColorspace)
2579 Minimize(result.index, (*k)+k_indexes[u]);
2580 }
2581 k_pixels += image->columns+kernel->width;
2582 k_indexes += image->columns+kernel->width;
2583 }
anthony602ab9b2010-01-05 08:06:50 +00002584 break;
2585
2586 case UndefinedMorphology:
2587 default:
2588 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002589 }
anthony5ef8e942010-05-11 06:51:12 +00002590 /* Final mathematics of results (combine with original image?)
2591 **
2592 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2593 ** be done here but works better with iteration as a image difference
2594 ** in the controling function (below). Thicken and Thinning however
2595 ** should be done here so thay can be iterated correctly.
2596 */
2597 switch ( method ) {
2598 case HitAndMissMorphology:
2599 case ErodeMorphology:
2600 result = min; /* minimum of neighbourhood */
2601 break;
2602 case DilateMorphology:
2603 result = max; /* maximum of neighbourhood */
2604 break;
2605 case ThinningMorphology:
2606 /* subtract pattern match from original */
2607 result.red -= min.red;
2608 result.green -= min.green;
2609 result.blue -= min.blue;
2610 result.opacity -= min.opacity;
2611 result.index -= min.index;
2612 break;
2613 case ThickenMorphology:
2614 /* Union with original image (maximize) - or should this be + */
2615 Maximize( result.red, min.red );
2616 Maximize( result.green, min.green );
2617 Maximize( result.blue, min.blue );
2618 Maximize( result.opacity, min.opacity );
2619 Maximize( result.index, min.index );
2620 break;
2621 default:
2622 /* result directly calculated or assigned */
2623 break;
2624 }
2625 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002626 switch ( method ) {
2627 case UndefinedMorphology:
2628 case DilateIntensityMorphology:
2629 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002630 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002631 default:
anthony83ba99b2010-01-24 08:48:15 +00002632 if ((channel & RedChannel) != 0)
2633 q->red = ClampToQuantum(result.red);
2634 if ((channel & GreenChannel) != 0)
2635 q->green = ClampToQuantum(result.green);
2636 if ((channel & BlueChannel) != 0)
2637 q->blue = ClampToQuantum(result.blue);
2638 if ((channel & OpacityChannel) != 0
2639 && image->matte == MagickTrue )
2640 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2641 if ((channel & IndexChannel) != 0
2642 && image->colorspace == CMYKColorspace)
2643 q_indexes[x] = ClampToQuantum(result.index);
2644 break;
2645 }
anthony5ef8e942010-05-11 06:51:12 +00002646 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002647 if ( ( p[r].red != q->red )
2648 || ( p[r].green != q->green )
2649 || ( p[r].blue != q->blue )
2650 || ( p[r].opacity != q->opacity )
2651 || ( image->colorspace == CMYKColorspace &&
2652 p_indexes[r] != q_indexes[x] ) )
2653 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002654 p++;
2655 q++;
anthony83ba99b2010-01-24 08:48:15 +00002656 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002657 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2658 if (sync == MagickFalse)
2659 status=MagickFalse;
2660 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2661 {
2662 MagickBooleanType
2663 proceed;
2664
2665#if defined(MAGICKCORE_OPENMP_SUPPORT)
2666 #pragma omp critical (MagickCore_MorphologyImage)
2667#endif
2668 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2669 if (proceed == MagickFalse)
2670 status=MagickFalse;
2671 }
anthony83ba99b2010-01-24 08:48:15 +00002672 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002673 result_image->type=image->type;
2674 q_view=DestroyCacheView(q_view);
2675 p_view=DestroyCacheView(p_view);
cristy150989e2010-02-01 14:59:39 +00002676 return(status ? (unsigned long) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002677}
2678
anthony4fd27e22010-02-07 08:17:18 +00002679
anthony9eb4f742010-05-18 02:45:54 +00002680MagickExport Image *MorphologyApply(const Image *image, const ChannelType
2681 channel,const MorphologyMethod method, const long iterations,
anthony47f5d062010-05-23 07:47:50 +00002682 const KernelInfo *kernel, const CompositeOperator compose,
2683 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002684{
2685 Image
anthony47f5d062010-05-23 07:47:50 +00002686 *curr_image, /* Image we are working with or iterating */
2687 *work_image, /* secondary image for primative iteration */
2688 *save_image, /* saved image - for 'edge' method only */
2689 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002690
anthony4fd27e22010-02-07 08:17:18 +00002691 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002692 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2693 *norm_kernel, /* the current normal un-reflected kernel */
2694 *rflt_kernel, /* the current reflected kernel (if needed) */
2695 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002696
2697 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002698 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002699
2700 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002701 rslt_compose; /* multi-kernel compose method for results to use */
2702
2703 MagickBooleanType
2704 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002705
anthony1b2bc0a2010-05-12 05:25:22 +00002706 unsigned long
anthony47f5d062010-05-23 07:47:50 +00002707 method_loop, /* Loop 1: number of compound method iterations */
2708 method_limit, /* maximum number of compound method iterations */
2709 kernel_number, /* Loop 2: the kernel number being applied */
2710 stage_loop, /* Loop 3: primative loop for compound morphology */
2711 stage_limit, /* how many primatives in this compound */
2712 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2713 kernel_limit, /* number of times to iterate kernel */
2714 count, /* total count of primative steps applied */
2715 changed, /* number pixels changed by last primative operation */
2716 kernel_changed, /* total count of changed using iterated kernel */
2717 method_changed; /* total count of changed over method iteration */
2718
2719 char
2720 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002721
anthony602ab9b2010-01-05 08:06:50 +00002722 assert(image != (Image *) NULL);
2723 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002724 assert(kernel != (KernelInfo *) NULL);
2725 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002726 assert(exception != (ExceptionInfo *) NULL);
2727 assert(exception->signature == MagickSignature);
2728
anthonyc3e48252010-05-24 12:43:11 +00002729 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002730 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002731 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002732
anthony47f5d062010-05-23 07:47:50 +00002733 kernel_limit = (unsigned long) iterations;
2734 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2735 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002736
cristye96405a2010-05-19 02:24:31 +00002737 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2738 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002739
anthony9eb4f742010-05-18 02:45:54 +00002740 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002741 curr_image = (Image *) image;
2742 work_image = save_image = rslt_image = (Image *) NULL;
2743 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002744
anthony47f5d062010-05-23 07:47:50 +00002745 /* Initialize specific methods
2746 * + which loop should use the given iteratations
2747 * + how many primatives make up the compound morphology
2748 * + multi-kernel compose method to use (by default)
2749 */
2750 method_limit = 1; /* just do method once, unless otherwise set */
2751 stage_limit = 1; /* assume method is not a compount */
2752 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002753 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002754 case SmoothMorphology: /* 4 primative compound morphology */
2755 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002756 break;
anthony47f5d062010-05-23 07:47:50 +00002757 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002758 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002759 case TopHatMorphology:
2760 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002761 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002762 case BottomHatMorphology:
2763 case EdgeMorphology:
2764 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002765 break;
2766 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002767 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002768 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002769 break;
anthonyc3e48252010-05-24 12:43:11 +00002770 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002771 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002772 case DistanceMorphology:
2773 method_limit = kernel_limit; /* iterate method with each kernel */
2774 kernel_limit = 1; /* do not do kernel iteration */
2775 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002776 break;
2777 default:
anthony930be612010-02-08 04:26:15 +00002778 break;
anthony602ab9b2010-01-05 08:06:50 +00002779 }
2780
anthonyc3e48252010-05-24 12:43:11 +00002781 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002782 if ( compose != UndefinedCompositeOp )
2783 rslt_compose = compose; /* override default composition for method */
2784 if ( rslt_compose == UndefinedCompositeOp )
2785 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2786
anthonyc3e48252010-05-24 12:43:11 +00002787 /* Some methods require a reflected kernel to use with primatives.
2788 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002789 switch ( method ) {
2790 case CorrelateMorphology:
2791 case CloseMorphology:
2792 case CloseIntensityMorphology:
2793 case BottomHatMorphology:
2794 case SmoothMorphology:
2795 reflected_kernel = CloneKernelInfo(kernel);
2796 if (reflected_kernel == (KernelInfo *) NULL)
2797 goto error_cleanup;
2798 RotateKernelInfo(reflected_kernel,180);
2799 break;
2800 default:
2801 break;
anthony9eb4f742010-05-18 02:45:54 +00002802 }
anthony7a01dcf2010-05-11 12:25:52 +00002803
anthony47f5d062010-05-23 07:47:50 +00002804 /* Loop 1: iterate the compound method */
2805 method_loop = 0;
2806 method_changed = 1;
2807 while ( method_loop < method_limit && method_changed > 0 ) {
2808 method_loop++;
2809 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002810
anthony47f5d062010-05-23 07:47:50 +00002811 /* Loop 2: iterate over each kernel in a multi-kernel list */
2812 norm_kernel = (KernelInfo *) kernel;
2813 rflt_kernel = reflected_kernel;
2814 kernel_number = 0;
2815 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002816
anthony47f5d062010-05-23 07:47:50 +00002817 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2818 stage_loop = 0; /* the compound morphology stage number */
2819 while ( stage_loop < stage_limit ) {
2820 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002821
anthony47f5d062010-05-23 07:47:50 +00002822 /* Select primative morphology for this stage of compound method */
2823 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002824 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002825 switch( method ) {
2826 case ErodeMorphology: /* just erode */
2827 case EdgeInMorphology: /* erode and image difference */
2828 primative = ErodeMorphology;
2829 break;
2830 case DilateMorphology: /* just dilate */
2831 case EdgeOutMorphology: /* dilate and image difference */
2832 primative = DilateMorphology;
2833 break;
2834 case OpenMorphology: /* erode then dialate */
2835 case TopHatMorphology: /* open and image difference */
2836 primative = ErodeMorphology;
2837 if ( stage_loop == 2 )
2838 primative = DilateMorphology;
2839 break;
2840 case OpenIntensityMorphology:
2841 primative = ErodeIntensityMorphology;
2842 if ( stage_loop == 2 )
2843 primative = DilateIntensityMorphology;
2844 case CloseMorphology: /* dilate, then erode */
2845 case BottomHatMorphology: /* close and image difference */
2846 this_kernel = rflt_kernel; /* use the reflected kernel */
2847 primative = DilateMorphology;
2848 if ( stage_loop == 2 )
2849 primative = ErodeMorphology;
2850 break;
2851 case CloseIntensityMorphology:
2852 this_kernel = rflt_kernel; /* use the reflected kernel */
2853 primative = DilateIntensityMorphology;
2854 if ( stage_loop == 2 )
2855 primative = ErodeIntensityMorphology;
2856 break;
2857 case SmoothMorphology: /* open, close */
2858 switch ( stage_loop ) {
2859 case 1: /* start an open method, which starts with Erode */
2860 primative = ErodeMorphology;
2861 break;
2862 case 2: /* now Dilate the Erode */
2863 primative = DilateMorphology;
2864 break;
2865 case 3: /* Reflect kernel a close */
2866 this_kernel = rflt_kernel; /* use the reflected kernel */
2867 primative = DilateMorphology;
2868 break;
2869 case 4: /* Finish the Close */
2870 this_kernel = rflt_kernel; /* use the reflected kernel */
2871 primative = ErodeMorphology;
2872 break;
2873 }
2874 break;
2875 case EdgeMorphology: /* dilate and erode difference */
2876 primative = DilateMorphology;
2877 if ( stage_loop == 2 ) {
2878 save_image = curr_image; /* save the image difference */
2879 curr_image = (Image *) image;
2880 primative = ErodeMorphology;
2881 }
2882 break;
2883 case CorrelateMorphology:
2884 /* A Correlation is a Convolution with a reflected kernel.
2885 ** However a Convolution is a weighted sum using a reflected
2886 ** kernel. It may seem stange to convert a Correlation into a
2887 ** Convolution as the Correlation is the simplier method, but
2888 ** Convolution is much more commonly used, and it makes sense to
2889 ** implement it directly so as to avoid the need to duplicate the
2890 ** kernel when it is not required (which is typically the
2891 ** default).
2892 */
2893 this_kernel = rflt_kernel; /* use the reflected kernel */
2894 primative = ConvolveMorphology;
2895 break;
2896 default:
anthony47f5d062010-05-23 07:47:50 +00002897 break;
2898 }
anthony9eb4f742010-05-18 02:45:54 +00002899
anthony47f5d062010-05-23 07:47:50 +00002900 /* Extra information for debugging compound operations */
2901 if ( verbose == MagickTrue ) {
2902 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002903 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002904 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2905 method_loop, stage_loop );
2906 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002907 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002908 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2909 method_loop );
2910 else
2911 v_info[0] = '\0';
2912 }
2913
2914 /* Loop 4: Iterate the kernel with primative */
2915 kernel_loop = 0;
2916 kernel_changed = 0;
2917 changed = 1;
2918 while ( kernel_loop < kernel_limit && changed > 0 ) {
2919 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002920
2921 /* Create a destination image, if not yet defined */
2922 if ( work_image == (Image *) NULL )
2923 {
2924 work_image=CloneImage(image,0,0,MagickTrue,exception);
2925 if (work_image == (Image *) NULL)
2926 goto error_cleanup;
2927 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2928 {
2929 InheritException(exception,&work_image->exception);
2930 goto error_cleanup;
2931 }
2932 }
2933
anthony47f5d062010-05-23 07:47:50 +00002934 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002935 count++;
anthony47f5d062010-05-23 07:47:50 +00002936 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002937 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002938 kernel_changed += changed;
2939 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002940
anthony47f5d062010-05-23 07:47:50 +00002941 if ( verbose == MagickTrue ) {
2942 if ( kernel_loop > 1 )
2943 fprintf(stderr, "\n"); /* add end-of-line from previous */
2944 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2945 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2946 ( this_kernel == rflt_kernel ) ? "*" : "",
2947 method_loop+kernel_loop-1, kernel_number, count, changed);
2948 }
anthony9eb4f742010-05-18 02:45:54 +00002949 /* prepare next loop */
2950 { Image *tmp = work_image; /* swap images for iteration */
2951 work_image = curr_image;
2952 curr_image = tmp;
2953 }
2954 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002955 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002956
anthony47f5d062010-05-23 07:47:50 +00002957 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002958
anthony47f5d062010-05-23 07:47:50 +00002959 if ( verbose == MagickTrue && kernel_changed != changed )
2960 fprintf(stderr, " Total %lu", kernel_changed);
2961 if ( verbose == MagickTrue && stage_loop < stage_limit )
2962 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002963
2964#if 0
anthony47f5d062010-05-23 07:47:50 +00002965 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
2966 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
2967 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
2968 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
2969 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002970#endif
2971
anthony47f5d062010-05-23 07:47:50 +00002972 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002973
anthony47f5d062010-05-23 07:47:50 +00002974 /* Final Post-processing for some Compound Methods
2975 **
2976 ** The removal of any 'Sync' channel flag in the Image Compositon
2977 ** below ensures the methematical compose method is applied in a
2978 ** purely mathematical way, and only to the selected channels.
2979 ** Turn off SVG composition 'alpha blending'.
2980 */
2981 switch( method ) {
2982 case EdgeOutMorphology:
2983 case EdgeInMorphology:
2984 case TopHatMorphology:
2985 case BottomHatMorphology:
2986 if ( verbose == MagickTrue )
2987 fprintf(stderr, "\n%s: Difference with original image",
2988 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2989 (void) CompositeImageChannel(curr_image,
2990 (ChannelType) (channel & ~SyncChannels),
2991 DifferenceCompositeOp, image, 0, 0);
2992 break;
2993 case EdgeMorphology:
2994 if ( verbose == MagickTrue )
2995 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
2996 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2997 (void) CompositeImageChannel(curr_image,
2998 (ChannelType) (channel & ~SyncChannels),
2999 DifferenceCompositeOp, save_image, 0, 0);
3000 save_image = DestroyImage(save_image); /* finished with save image */
3001 break;
3002 default:
3003 break;
3004 }
3005
3006 /* multi-kernel handling: re-iterate, or compose results */
3007 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003008 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003009 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003010 { if ( verbose == MagickTrue ) {
3011 if ( this_kernel->next != (KernelInfo *) NULL )
3012 fprintf(stderr, " (re-iterate)");
3013 else
3014 fprintf(stderr, " (done)");
3015 }
3016 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003017 }
anthony47f5d062010-05-23 07:47:50 +00003018 else if ( rslt_image == (Image *) NULL)
3019 { if ( verbose == MagickTrue )
3020 fprintf(stderr, " (save for compose)");
3021 rslt_image = curr_image;
3022 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003023 }
anthony47f5d062010-05-23 07:47:50 +00003024 else
3025 { /* add the new 'current' result to the composition
3026 **
3027 ** The removal of any 'Sync' channel flag in the Image Compositon
3028 ** below ensures the methematical compose method is applied in a
3029 ** purely mathematical way, and only to the selected channels.
3030 ** Turn off SVG composition 'alpha blending'.
3031 */
3032 if ( verbose == MagickTrue )
3033 fprintf(stderr, " (compose \"%s\")",
3034 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3035 (void) CompositeImageChannel(rslt_image,
3036 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3037 curr_image, 0, 0);
3038 curr_image = (Image *) image; /* continue with original image */
3039 }
3040 if ( verbose == MagickTrue )
3041 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003042
anthony47f5d062010-05-23 07:47:50 +00003043 /* loop to the next kernel in a multi-kernel list */
3044 norm_kernel = norm_kernel->next;
3045 if ( rflt_kernel != (KernelInfo *) NULL )
3046 rflt_kernel = rflt_kernel->next;
3047 kernel_number++;
3048 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003049
anthony47f5d062010-05-23 07:47:50 +00003050 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003051
anthony9eb4f742010-05-18 02:45:54 +00003052 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003053
anthony47f5d062010-05-23 07:47:50 +00003054 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003055error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003056 if ( curr_image != (Image *) NULL &&
3057 curr_image != rslt_image &&
3058 curr_image != image )
3059 curr_image = DestroyImage(curr_image);
3060 if ( rslt_image != (Image *) NULL )
3061 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003062exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003063 if ( curr_image != (Image *) NULL &&
3064 curr_image != rslt_image &&
3065 curr_image != image )
3066 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003067 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003068 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003069 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003070 save_image = DestroyImage(save_image);
3071 if ( reflected_kernel != (KernelInfo *) NULL )
3072 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3073 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003074}
3075
3076/*
3077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3078% %
3079% %
3080% %
3081% M o r p h o l o g y I m a g e C h a n n e l %
3082% %
3083% %
3084% %
3085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3086%
3087% MorphologyImageChannel() applies a user supplied kernel to the image
3088% according to the given mophology method.
3089%
3090% This function applies any and all user defined settings before calling
3091% the above internal function MorphologyApply().
3092%
3093% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003094% * Output Bias for Convolution and correlation ("-bias")
3095% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3096% This can also includes the addition of a scaled unity kernel.
3097% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003098%
3099% The format of the MorphologyImage method is:
3100%
3101% Image *MorphologyImage(const Image *image,MorphologyMethod method,
3102% const long iterations,KernelInfo *kernel,ExceptionInfo *exception)
3103%
3104% Image *MorphologyImageChannel(const Image *image, const ChannelType
3105% channel,MorphologyMethod method,const long iterations,
3106% KernelInfo *kernel,ExceptionInfo *exception)
3107%
3108% A description of each parameter follows:
3109%
3110% o image: the image.
3111%
3112% o method: the morphology method to be applied.
3113%
3114% o iterations: apply the operation this many times (or no change).
3115% A value of -1 means loop until no change found.
3116% How this is applied may depend on the morphology method.
3117% Typically this is a value of 1.
3118%
3119% o channel: the channel type.
3120%
3121% o kernel: An array of double representing the morphology kernel.
3122% Warning: kernel may be normalized for the Convolve method.
3123%
3124% o exception: return any errors or warnings in this structure.
3125%
3126*/
3127
3128MagickExport Image *MorphologyImageChannel(const Image *image,
3129 const ChannelType channel,const MorphologyMethod method,
3130 const long iterations,const KernelInfo *kernel,ExceptionInfo *exception)
3131{
3132 const char
3133 *artifact;
3134
3135 KernelInfo
3136 *curr_kernel;
3137
anthony47f5d062010-05-23 07:47:50 +00003138 CompositeOperator
3139 compose;
3140
anthony9eb4f742010-05-18 02:45:54 +00003141 Image
3142 *morphology_image;
3143
3144
anthony46a369d2010-05-19 02:41:48 +00003145 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3146 * This is done BEFORE the ShowKernelInfo() function is called so that
3147 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003148 */
3149 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003150 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003151 {
3152 artifact = GetImageArtifact(image,"convolve:scale");
3153 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003154 if ( curr_kernel == kernel )
3155 curr_kernel = CloneKernelInfo(kernel);
3156 if (curr_kernel == (KernelInfo *) NULL) {
3157 curr_kernel=DestroyKernelInfo(curr_kernel);
3158 return((Image *) NULL);
3159 }
anthony46a369d2010-05-19 02:41:48 +00003160 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003161 }
3162 }
3163
3164 /* display the (normalized) kernel via stderr */
3165 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003166 if ( artifact == (const char *) NULL)
3167 artifact = GetImageArtifact(image,"convolve:showkernel");
3168 if ( artifact == (const char *) NULL)
3169 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003170 if ( artifact != (const char *) NULL)
3171 ShowKernelInfo(curr_kernel);
3172
anthony47f5d062010-05-23 07:47:50 +00003173 /* override the default handling of multi-kernel morphology results
3174 * if 'Undefined' use the default method
3175 * if 'None' (default for 'Convolve') re-iterate previous result
3176 * otherwise merge resulting images using compose method given
3177 */
3178 compose = UndefinedCompositeOp; /* use default for method */
3179 artifact = GetImageArtifact(image,"morphology:compose");
3180 if ( artifact != (const char *) NULL)
3181 compose = (CompositeOperator) ParseMagickOption(
3182 MagickComposeOptions,MagickFalse,artifact);
3183
anthony9eb4f742010-05-18 02:45:54 +00003184 /* Apply the Morphology */
3185 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003186 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003187
3188 /* Cleanup and Exit */
3189 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003190 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003191 return(morphology_image);
3192}
3193
3194MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
3195 method, const long iterations,const KernelInfo *kernel, ExceptionInfo
3196 *exception)
3197{
3198 Image
3199 *morphology_image;
3200
3201 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3202 iterations,kernel,exception);
3203 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003204}
anthony83ba99b2010-01-24 08:48:15 +00003205
3206/*
3207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3208% %
3209% %
3210% %
anthony4fd27e22010-02-07 08:17:18 +00003211+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003212% %
3213% %
3214% %
3215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3216%
anthony46a369d2010-05-19 02:41:48 +00003217% RotateKernelInfo() rotates the kernel by the angle given.
3218%
3219% Currently it is restricted to 90 degree angles, of either 1D kernels
3220% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3221% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003222%
anthony4fd27e22010-02-07 08:17:18 +00003223% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003224%
anthony4fd27e22010-02-07 08:17:18 +00003225% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003226%
3227% A description of each parameter follows:
3228%
3229% o kernel: the Morphology/Convolution kernel
3230%
3231% o angle: angle to rotate in degrees
3232%
anthony46a369d2010-05-19 02:41:48 +00003233% This function is currently internal to this module only, but can be exported
3234% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003235*/
anthony4fd27e22010-02-07 08:17:18 +00003236static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003237{
anthony1b2bc0a2010-05-12 05:25:22 +00003238 /* angle the lower kernels first */
3239 if ( kernel->next != (KernelInfo *) NULL)
3240 RotateKernelInfo(kernel->next, angle);
3241
anthony83ba99b2010-01-24 08:48:15 +00003242 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3243 **
3244 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3245 */
3246
3247 /* Modulus the angle */
3248 angle = fmod(angle, 360.0);
3249 if ( angle < 0 )
3250 angle += 360.0;
3251
anthony3c10fc82010-05-13 02:40:51 +00003252 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003253 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003254
anthony3dd0f622010-05-13 12:57:32 +00003255 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003256 switch (kernel->type) {
3257 /* These built-in kernels are cylindrical kernels, rotating is useless */
3258 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003259 case DOGKernel:
3260 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003261 case PeaksKernel:
3262 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003263 case ChebyshevKernel:
3264 case ManhattenKernel:
3265 case EuclideanKernel:
3266 return;
3267
3268 /* These may be rotatable at non-90 angles in the future */
3269 /* but simply rotating them in multiples of 90 degrees is useless */
3270 case SquareKernel:
3271 case DiamondKernel:
3272 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003273 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003274 return;
3275
3276 /* These only allows a +/-90 degree rotation (by transpose) */
3277 /* A 180 degree rotation is useless */
3278 case BlurKernel:
3279 case RectangleKernel:
3280 if ( 135.0 < angle && angle <= 225.0 )
3281 return;
3282 if ( 225.0 < angle && angle <= 315.0 )
3283 angle -= 180;
3284 break;
3285
anthony3dd0f622010-05-13 12:57:32 +00003286 default:
anthony83ba99b2010-01-24 08:48:15 +00003287 break;
3288 }
anthony3c10fc82010-05-13 02:40:51 +00003289 /* Attempt rotations by 45 degrees */
3290 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3291 {
3292 if ( kernel->width == 3 && kernel->height == 3 )
3293 { /* Rotate a 3x3 square by 45 degree angle */
3294 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003295 kernel->values[0] = kernel->values[3];
3296 kernel->values[3] = kernel->values[6];
3297 kernel->values[6] = kernel->values[7];
3298 kernel->values[7] = kernel->values[8];
3299 kernel->values[8] = kernel->values[5];
3300 kernel->values[5] = kernel->values[2];
3301 kernel->values[2] = kernel->values[1];
3302 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003303 /* rotate non-centered origin */
3304 if ( kernel->x != 1 || kernel->y != 1 ) {
3305 long x,y;
3306 x = (long) kernel->x-1;
3307 y = (long) kernel->y-1;
3308 if ( x == y ) x = 0;
3309 else if ( x == 0 ) x = -y;
3310 else if ( x == -y ) y = 0;
3311 else if ( y == 0 ) y = x;
3312 kernel->x = (unsigned long) x+1;
3313 kernel->y = (unsigned long) y+1;
3314 }
anthony43c49252010-05-18 10:59:50 +00003315 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3316 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003317 }
3318 else
3319 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3320 }
3321 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3322 {
3323 if ( kernel->width == 1 || kernel->height == 1 )
3324 { /* Do a transpose of the image, which results in a 90
3325 ** degree rotation of a 1 dimentional kernel
3326 */
3327 long
3328 t;
3329 t = (long) kernel->width;
3330 kernel->width = kernel->height;
3331 kernel->height = (unsigned long) t;
3332 t = kernel->x;
3333 kernel->x = kernel->y;
3334 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003335 if ( kernel->width == 1 ) {
3336 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3337 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3338 } else {
3339 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3340 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3341 }
anthony3c10fc82010-05-13 02:40:51 +00003342 }
3343 else if ( kernel->width == kernel->height )
3344 { /* Rotate a square array of values by 90 degrees */
anthony1d45eb92010-05-25 11:13:23 +00003345 { register unsigned long
3346 i,j,x,y;
3347 register MagickRealType
3348 *k,t;
3349 k=kernel->values;
3350 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3351 for( j=0, y=kernel->height-1; j<y; j++, y--)
3352 { t = k[i+j*kernel->width];
3353 k[i+j*kernel->width] = k[j+x*kernel->width];
3354 k[j+x*kernel->width] = k[x+y*kernel->width];
3355 k[x+y*kernel->width] = k[y+i*kernel->width];
3356 k[y+i*kernel->width] = t;
3357 }
3358 }
3359 /* rotate the origin - relative to center of array */
3360 { register long x,y;
3361 x = (long) kernel->x*2-kernel->width+1;
3362 y = (long) kernel->y*2-kernel->height+1;
3363 kernel->x = (unsigned long) ( -y +kernel->width-1)/2;
3364 kernel->y = (unsigned long) ( +x +kernel->height-1)/2;
3365 }
anthony43c49252010-05-18 10:59:50 +00003366 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3367 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003368 }
3369 else
3370 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3371 }
anthony83ba99b2010-01-24 08:48:15 +00003372 if ( 135.0 < angle && angle <= 225.0 )
3373 {
anthony43c49252010-05-18 10:59:50 +00003374 /* For a 180 degree rotation - also know as a reflection
3375 * This is actually a very very common operation!
3376 * Basically all that is needed is a reversal of the kernel data!
3377 * And a reflection of the origon
3378 */
anthony83ba99b2010-01-24 08:48:15 +00003379 unsigned long
3380 i,j;
3381 register double
3382 *k,t;
3383
3384 k=kernel->values;
3385 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3386 t=k[i], k[i]=k[j], k[j]=t;
3387
anthony930be612010-02-08 04:26:15 +00003388 kernel->x = (long) kernel->width - kernel->x - 1;
3389 kernel->y = (long) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003390 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3391 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003392 }
anthony3c10fc82010-05-13 02:40:51 +00003393 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003394 * In the future some form of non-orthogonal angled rotates could be
3395 * performed here, posibily with a linear kernel restriction.
3396 */
3397
3398#if 0
anthony3c10fc82010-05-13 02:40:51 +00003399 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003400 */
3401 unsigned long
3402 y;
cristy150989e2010-02-01 14:59:39 +00003403 register long
anthony83ba99b2010-01-24 08:48:15 +00003404 x,r;
3405 register double
3406 *k,t;
3407
3408 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3409 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3410 t=k[x], k[x]=k[r], k[r]=t;
3411
cristyc99304f2010-02-01 15:26:27 +00003412 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003413 angle = fmod(angle+180.0, 360.0);
3414 }
3415#endif
3416 return;
3417}
3418
3419/*
3420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3421% %
3422% %
3423% %
anthony46a369d2010-05-19 02:41:48 +00003424% S c a l e G e o m e t r y K e r n e l I n f o %
3425% %
3426% %
3427% %
3428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3429%
3430% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3431% provided as a "-set option:convolve:scale {geometry}" user setting,
3432% and modifies the kernel according to the parsed arguments of that setting.
3433%
3434% The first argument (and any normalization flags) are passed to
3435% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3436% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3437% into the scaled/normalized kernel.
3438%
3439% The format of the ScaleKernelInfo method is:
3440%
3441% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3442% const MagickStatusType normalize_flags )
3443%
3444% A description of each parameter follows:
3445%
3446% o kernel: the Morphology/Convolution kernel to modify
3447%
3448% o geometry:
3449% The geometry string to parse, typically from the user provided
3450% "-set option:convolve:scale {geometry}" setting.
3451%
3452*/
3453MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3454 const char *geometry)
3455{
3456 GeometryFlags
3457 flags;
3458 GeometryInfo
3459 args;
3460
3461 SetGeometryInfo(&args);
3462 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3463
3464#if 0
3465 /* For Debugging Geometry Input */
3466 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3467 flags, args.rho, args.sigma, args.xi, args.psi );
3468#endif
3469
3470 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3471 args.rho *= 0.01, args.sigma *= 0.01;
3472
3473 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3474 args.rho = 1.0;
3475 if ( (flags & SigmaValue) == 0 )
3476 args.sigma = 0.0;
3477
3478 /* Scale/Normalize the input kernel */
3479 ScaleKernelInfo(kernel, args.rho, flags);
3480
3481 /* Add Unity Kernel, for blending with original */
3482 if ( (flags & SigmaValue) != 0 )
3483 UnityAddKernelInfo(kernel, args.sigma);
3484
3485 return;
3486}
3487/*
3488%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3489% %
3490% %
3491% %
cristy6771f1e2010-03-05 19:43:39 +00003492% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003493% %
3494% %
3495% %
3496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3497%
anthony1b2bc0a2010-05-12 05:25:22 +00003498% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3499% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003500%
anthony999bb2c2010-02-18 12:38:01 +00003501% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003502% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003503%
anthony46a369d2010-05-19 02:41:48 +00003504% If either of the two 'normalize_flags' are given the kernel will first be
3505% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003506%
3507% Kernel normalization ('normalize_flags' given) is designed to ensure that
3508% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003509% morphology methods will fall into -1.0 to +1.0 range. Note that for
3510% non-HDRI versions of IM this may cause images to have any negative results
3511% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003512%
3513% More specifically. Kernels which only contain positive values (such as a
3514% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003515% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003516%
3517% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3518% the kernel will be scaled by the absolute of the sum of kernel values, so
3519% that it will generally fall within the +/- 1.0 range.
3520%
3521% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3522% will be scaled by just the sum of the postive values, so that its output
3523% range will again fall into the +/- 1.0 range.
3524%
3525% For special kernels designed for locating shapes using 'Correlate', (often
3526% only containing +1 and -1 values, representing foreground/brackground
3527% matching) a special normalization method is provided to scale the positive
3528% values seperatally to those of the negative values, so the kernel will be
3529% forced to become a zero-sum kernel better suited to such searches.
3530%
anthony1b2bc0a2010-05-12 05:25:22 +00003531% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003532% attributes within the kernel structure have been correctly set during the
3533% kernels creation.
3534%
3535% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003536% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3537% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003538%
anthony4fd27e22010-02-07 08:17:18 +00003539% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003540%
anthony999bb2c2010-02-18 12:38:01 +00003541% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3542% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003543%
3544% A description of each parameter follows:
3545%
3546% o kernel: the Morphology/Convolution kernel
3547%
anthony999bb2c2010-02-18 12:38:01 +00003548% o scaling_factor:
3549% multiply all values (after normalization) by this factor if not
3550% zero. If the kernel is normalized regardless of any flags.
3551%
3552% o normalize_flags:
3553% GeometryFlags defining normalization method to use.
3554% specifically: NormalizeValue, CorrelateNormalizeValue,
3555% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003556%
3557*/
cristy6771f1e2010-03-05 19:43:39 +00003558MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3559 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003560{
cristy150989e2010-02-01 14:59:39 +00003561 register long
anthonycc6c8362010-01-25 04:14:01 +00003562 i;
3563
anthony999bb2c2010-02-18 12:38:01 +00003564 register double
3565 pos_scale,
3566 neg_scale;
3567
anthony46a369d2010-05-19 02:41:48 +00003568 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003569 if ( kernel->next != (KernelInfo *) NULL)
3570 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3571
anthony46a369d2010-05-19 02:41:48 +00003572 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003573 pos_scale = 1.0;
3574 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003575 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003576 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003577 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003578 else
anthonyf4e00312010-05-20 12:06:35 +00003579 /* zero-summing kernel */
3580 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003581 }
anthony46a369d2010-05-19 02:41:48 +00003582 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003583 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3584 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3585 ? kernel->positive_range : 1.0;
3586 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3587 ? -kernel->negative_range : 1.0;
3588 }
3589 else
3590 neg_scale = pos_scale;
3591
3592 /* finialize scaling_factor for positive and negative components */
3593 pos_scale = scaling_factor/pos_scale;
3594 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003595
cristy150989e2010-02-01 14:59:39 +00003596 for (i=0; i < (long) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003597 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003598 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003599
anthony999bb2c2010-02-18 12:38:01 +00003600 /* convolution output range */
3601 kernel->positive_range *= pos_scale;
3602 kernel->negative_range *= neg_scale;
3603 /* maximum and minimum values in kernel */
3604 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3605 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3606
anthony46a369d2010-05-19 02:41:48 +00003607 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003608 if ( scaling_factor < MagickEpsilon ) {
3609 double t;
3610 t = kernel->positive_range;
3611 kernel->positive_range = kernel->negative_range;
3612 kernel->negative_range = t;
3613 t = kernel->maximum;
3614 kernel->maximum = kernel->minimum;
3615 kernel->minimum = 1;
3616 }
anthonycc6c8362010-01-25 04:14:01 +00003617
3618 return;
3619}
3620
3621/*
3622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3623% %
3624% %
3625% %
anthony46a369d2010-05-19 02:41:48 +00003626% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003627% %
3628% %
3629% %
3630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3631%
anthony4fd27e22010-02-07 08:17:18 +00003632% ShowKernelInfo() outputs the details of the given kernel defination to
3633% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003634%
3635% The format of the ShowKernel method is:
3636%
anthony4fd27e22010-02-07 08:17:18 +00003637% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003638%
3639% A description of each parameter follows:
3640%
3641% o kernel: the Morphology/Convolution kernel
3642%
anthony83ba99b2010-01-24 08:48:15 +00003643*/
anthony4fd27e22010-02-07 08:17:18 +00003644MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003645{
anthony7a01dcf2010-05-11 12:25:52 +00003646 KernelInfo
3647 *k;
anthony83ba99b2010-01-24 08:48:15 +00003648
anthony43c49252010-05-18 10:59:50 +00003649 unsigned long
anthony7a01dcf2010-05-11 12:25:52 +00003650 c, i, u, v;
3651
3652 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3653
anthony46a369d2010-05-19 02:41:48 +00003654 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003655 if ( kernel->next != (KernelInfo *) NULL )
cristye96405a2010-05-19 02:24:31 +00003656 fprintf(stderr, " #%lu", c );
anthony43c49252010-05-18 10:59:50 +00003657 fprintf(stderr, " \"%s",
3658 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3659 if ( fabs(k->angle) > MagickEpsilon )
3660 fprintf(stderr, "@%lg", k->angle);
anthonya648a302010-05-27 02:14:36 +00003661 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",
anthony43c49252010-05-18 10:59:50 +00003662 k->width, k->height,
3663 k->x, k->y );
anthony7a01dcf2010-05-11 12:25:52 +00003664 fprintf(stderr,
3665 " with values from %.*lg to %.*lg\n",
3666 GetMagickPrecision(), k->minimum,
3667 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003668 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003669 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003670 GetMagickPrecision(), k->positive_range);
3671 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3672 fprintf(stderr, " (Zero-Summing)\n");
3673 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3674 fprintf(stderr, " (Normalized)\n");
3675 else
3676 fprintf(stderr, " (Sum %.*lg)\n",
3677 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003678 for (i=v=0; v < k->height; v++) {
anthony46a369d2010-05-19 02:41:48 +00003679 fprintf(stderr, "%2lu:", v );
anthony43c49252010-05-18 10:59:50 +00003680 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003681 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003682 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003683 else
anthonyf4e00312010-05-20 12:06:35 +00003684 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003685 GetMagickPrecision(), k->values[i]);
3686 fprintf(stderr,"\n");
3687 }
anthony83ba99b2010-01-24 08:48:15 +00003688 }
3689}
anthonycc6c8362010-01-25 04:14:01 +00003690
3691/*
3692%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3693% %
3694% %
3695% %
anthony43c49252010-05-18 10:59:50 +00003696% U n i t y A d d K e r n a l I n f o %
3697% %
3698% %
3699% %
3700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3701%
3702% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3703% to the given pre-scaled and normalized Kernel. This in effect adds that
3704% amount of the original image into the resulting convolution kernel. This
3705% value is usually provided by the user as a percentage value in the
3706% 'convolve:scale' setting.
3707%
3708% The resulting effect is to either convert a 'zero-summing' edge detection
3709% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3710% kernel.
3711%
3712% Alternativally by using a purely positive kernel, and using a negative
3713% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3714% as a "Gaussian") into a 'unsharp' kernel.
3715%
anthony46a369d2010-05-19 02:41:48 +00003716% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003717%
3718% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3719%
3720% A description of each parameter follows:
3721%
3722% o kernel: the Morphology/Convolution kernel
3723%
3724% o scale:
3725% scaling factor for the unity kernel to be added to
3726% the given kernel.
3727%
anthony43c49252010-05-18 10:59:50 +00003728*/
3729MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3730 const double scale)
3731{
anthony46a369d2010-05-19 02:41:48 +00003732 /* do the other kernels in a multi-kernel list first */
3733 if ( kernel->next != (KernelInfo *) NULL)
3734 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003735
anthony46a369d2010-05-19 02:41:48 +00003736 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003737 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003738 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003739
3740 return;
3741}
3742
3743/*
3744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3745% %
3746% %
3747% %
3748% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003749% %
3750% %
3751% %
3752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3753%
3754% ZeroKernelNans() replaces any special 'nan' value that may be present in
3755% the kernel with a zero value. This is typically done when the kernel will
3756% be used in special hardware (GPU) convolution processors, to simply
3757% matters.
3758%
3759% The format of the ZeroKernelNans method is:
3760%
anthony46a369d2010-05-19 02:41:48 +00003761% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003762%
3763% A description of each parameter follows:
3764%
3765% o kernel: the Morphology/Convolution kernel
3766%
anthonycc6c8362010-01-25 04:14:01 +00003767*/
anthonyc4c86e02010-01-27 09:30:32 +00003768MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003769{
anthony43c49252010-05-18 10:59:50 +00003770 register unsigned long
anthonycc6c8362010-01-25 04:14:01 +00003771 i;
3772
anthony46a369d2010-05-19 02:41:48 +00003773 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003774 if ( kernel->next != (KernelInfo *) NULL)
3775 ZeroKernelNans(kernel->next);
3776
anthony43c49252010-05-18 10:59:50 +00003777 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003778 if ( IsNan(kernel->values[i]) )
3779 kernel->values[i] = 0.0;
3780
3781 return;
3782}