blob: 99737d771d49485bc35cd5b99a57a3279212fe4e [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
cristybb503372010-05-27 20:51:26 +0000150% center as origin, this is no ssize_ter the case, and any rectangular kernel
anthony83ba99b2010-01-24 08:48:15 +0000151% 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
cristybb503372010-05-27 20:51:26 +0000225 register ssize_t
anthonyc84dce52010-05-07 05:42:23 +0000226 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 */
cristybb503372010-05-27 20:51:26 +0000272 kernel->width = (size_t)args.rho;
273 kernel->height = (size_t)args.sigma;
anthony602ab9b2010-01-05 08:06:50 +0000274
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));
cristybb503372010-05-27 20:51:26 +0000278 kernel->x = ((flags & XValue)!=0) ? (ssize_t)args.xi
279 : (ssize_t) (kernel->width-1)/2;
280 kernel->y = ((flags & YValue)!=0) ? (ssize_t)args.psi
281 : (ssize_t) (kernel->height-1)/2;
282 if ( kernel->x >= (ssize_t) kernel->width ||
283 kernel->y >= (ssize_t) 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 */
cristybb503372010-05-27 20:51:26 +0000301 kernel->width = kernel->height= (size_t) sqrt((double) i+1.0);
302 kernel->x = kernel->y = (ssize_t) (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
cristybb503372010-05-27 20:51:26 +0000318 for (i=0; (i < (ssize_t) (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 */
cristybb503372010-05-27 20:51:26 +0000344 if ( i < (ssize_t) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000345 Minimize(kernel->minimum, kernel->values[i]);
346 Maximize(kernel->maximum, kernel->values[i]);
cristybb503372010-05-27 20:51:26 +0000347 for ( ; i < (ssize_t) (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 */
cristybb503372010-05-27 20:51:26 +0000352 if ( i < (ssize_t) (kernel->width*kernel->height) )
anthonyc84dce52010-05-07 05:42:23 +0000353 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
cristybb503372010-05-27 20:51:26 +0000376 ssize_t
anthony5ef8e942010-05-11 06:51:12 +0000377 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 */
cristybb503372010-05-27 20:51:26 +0000425 args.xi = (double)(((ssize_t)args.rho-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000426 if ( (flags & YValue) == 0 )
cristybb503372010-05-27 20:51:26 +0000427 args.psi = (double)(((ssize_t)args.sigma-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000428 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
cristybb503372010-05-27 20:51:26 +0000482 size_t
anthonye108a3f2010-05-12 07:24:03 +0000483 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 ) {
cristyf2faecf2010-05-28 19:19:36 +0000502 fprintf(stderr, "Failed to parse kernel number #%lu\n",(unsigned long)
503 kernel_number);
anthonye108a3f2010-05-12 07:24:03 +0000504 if ( kernel != (KernelInfo *) NULL )
505 kernel=DestroyKernelInfo(kernel);
506 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000507 }
anthonye108a3f2010-05-12 07:24:03 +0000508
509 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000510 if ( kernel == (KernelInfo *) NULL )
511 kernel = new_kernel;
512 else
anthony43c49252010-05-18 10:59:50 +0000513 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000514 }
515
516 /* look for the next kernel in list */
517 p = strchr(p, ';');
518 if ( p == (char *) NULL )
519 break;
520 p++;
521
522 }
anthony7a01dcf2010-05-11 12:25:52 +0000523 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000524}
525
anthony602ab9b2010-01-05 08:06:50 +0000526
527/*
528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529% %
530% %
531% %
532% A c q u i r e K e r n e l B u i l t I n %
533% %
534% %
535% %
536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537%
538% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
539% kernels used for special purposes such as gaussian blurring, skeleton
540% pruning, and edge distance determination.
541%
542% They take a KernelType, and a set of geometry style arguments, which were
543% typically decoded from a user supplied string, or from a more complex
544% Morphology Method that was requested.
545%
546% The format of the AcquireKernalBuiltIn method is:
547%
cristy2be15382010-01-21 02:38:03 +0000548% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000549% const GeometryInfo args)
550%
551% A description of each parameter follows:
552%
553% o type: the pre-defined type of kernel wanted
554%
555% o args: arguments defining or modifying the kernel
556%
557% Convolution Kernels
558%
anthony46a369d2010-05-19 02:41:48 +0000559% Unity
560% the No-Op kernel, also requivelent to Gaussian of sigma zero.
561% Basically a 3x3 kernel of a 1 surrounded by zeros.
562%
anthony3c10fc82010-05-13 02:40:51 +0000563% Gaussian:{radius},{sigma}
564% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000565% The sigma for the curve is required. The resulting kernel is
566% normalized,
567%
568% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000569%
570% NOTE: that the 'radius' is optional, but if provided can limit (clip)
571% the final size of the resulting kernel to a square 2*radius+1 in size.
572% The radius should be at least 2 times that of the sigma value, or
573% sever clipping and aliasing may result. If not given or set to 0 the
574% radius will be determined so as to produce the best minimal error
575% result, which is usally much larger than is normally needed.
576%
anthony501c2f92010-06-02 10:55:14 +0000577% LoG:{radius},{sigma}
578% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
579% The supposed ideal edge detection, zero-summing kernel.
580%
581% An alturnative to this kernel is to use a "DoG" with a sigma ratio of
582% approx 1.6 (according to wikipedia).
583%
584% DoG:{radius},{sigma1},{sigma2}
anthonyc1061722010-05-14 06:23:49 +0000585% "Difference of Gaussians" Kernel.
586% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
587% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
588% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000589%
anthonyc1061722010-05-14 06:23:49 +0000590% Blur:{radius},{sigma}[,{angle}]
591% Generates a 1 dimensional or linear gaussian blur, at the angle given
592% (current restricted to orthogonal angles). If a 'radius' is given the
593% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
594% by a 90 degree angle.
595%
596% If 'sigma' is zero, you get a single pixel on a field of zeros.
597%
598% Note that two convolutions with two "Blur" kernels perpendicular to
599% each other, is equivelent to a far larger "Gaussian" kernel with the
600% same sigma value, However it is much faster to apply. This is how the
601% "-blur" operator actually works.
602%
anthony3c10fc82010-05-13 02:40:51 +0000603% Comet:{width},{sigma},{angle}
604% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000605% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000606% Adding two such blurs in opposite directions produces a Blur Kernel.
607% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000608%
anthony3c10fc82010-05-13 02:40:51 +0000609% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000610% radius of the kernel.
611%
612% # Still to be implemented...
613% #
anthony4fd27e22010-02-07 08:17:18 +0000614% # Filter2D
615% # Filter1D
616% # Set kernel values using a resize filter, and given scale (sigma)
617% # Cylindrical or Linear. Is this posible with an image?
618% #
anthony602ab9b2010-01-05 08:06:50 +0000619%
anthony3c10fc82010-05-13 02:40:51 +0000620% Named Constant Convolution Kernels
621%
anthonyc1061722010-05-14 06:23:49 +0000622% All these are unscaled, zero-summing kernels by default. As such for
623% non-HDRI version of ImageMagick some form of normalization, user scaling,
624% and biasing the results is recommended, to prevent the resulting image
625% being 'clipped'.
626%
627% The 3x3 kernels (most of these) can be circularly rotated in multiples of
628% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000629%
630% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000631% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000632% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
633% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000634% Type 2 : 3x3 with center:4 edge:1 corner:-2
635% Type 3 : 3x3 with center:4 edge:-2 corner:1
636% Type 5 : 5x5 laplacian
637% Type 7 : 7x7 laplacian
anthony501c2f92010-06-02 10:55:14 +0000638% Type 15 : 5x5 LoG (sigma approx 1.4)
639% Type 19 : 9x9 LoG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000640%
641% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000642% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000643% -1, 0, 1
644% -2, 0,-2
645% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000646%
anthonyc1061722010-05-14 06:23:49 +0000647% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000648% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000649% 0, 0, 0
650% -1, 1, 0
651% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000652% Prewitt:{angle}
653% Prewitt Edge convolution kernel (3x3)
654% -1, 0, 1
655% -1, 0, 1
656% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000657% Compass:{angle}
658% Prewitt's "Compass" convolution kernel (3x3)
659% -1, 1, 1
660% -1,-2, 1
661% -1, 1, 1
662% Kirsch:{angle}
663% Kirsch's "Compass" convolution kernel (3x3)
664% -3,-3, 5
665% -3, 0, 5
666% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000667%
anthonye2a60ce2010-05-19 12:30:40 +0000668% FreiChen:{type},{angle}
anthony1d5e6702010-05-31 10:19:12 +0000669% Frei-Chen Edge Detector is based on a kernel that is similar to
670% the Sobel Kernel, but is designed to be isotropic. That is it takes
671% into account the distance of the diagonal in the kernel.
anthonyc3cd15b2010-05-27 06:05:40 +0000672%
anthony501c2f92010-06-02 10:55:14 +0000673% Type 0: | 1, 0, -1 |
674% | sqrt(2), 0, -sqrt(2) |
675% | 1, 0, -1 |
anthonyc3cd15b2010-05-27 06:05:40 +0000676%
anthony1d5e6702010-05-31 10:19:12 +0000677% However this kernel is als at the heart of the FreiChen Edge Detection
678% Process which uses a set of 9 specially weighted kernel. These 9
679% kernels not be normalized, but directly applied to the image. The
680% results is then added together, to produce the intensity of an edge in
681% a specific direction. The square root of the pixel value can then be
682% taken as the cosine of the edge, and at least 2 such runs at 90 degrees
683% from each other, both the direction and the strength of the edge can be
684% determined.
anthonyc3cd15b2010-05-27 06:05:40 +0000685%
anthony501c2f92010-06-02 10:55:14 +0000686% Type 1: | 1, 0, -1 |
687% | sqrt(2), 0, -sqrt(2) | / 2*sqrt(2)
688% | 1, 0, -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000689%
anthony501c2f92010-06-02 10:55:14 +0000690% Type 2: | 1, sqrt(2), 1 |
691% | 0, 0, 0 | / 2*sqrt(2)
692% | 1, sqrt(2), 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000693%
anthony501c2f92010-06-02 10:55:14 +0000694% Type 3: | sqrt(2), -1, 0 |
695% | -1, 0, 1 | / 2*sqrt(2)
696% | 0, 1, -sqrt(2) |
anthonye2a60ce2010-05-19 12:30:40 +0000697%
anthony1d5e6702010-05-31 10:19:12 +0000698% Type 4: | 0, 1, -sqrt(2) |
anthonye2a60ce2010-05-19 12:30:40 +0000699% | -1, 0, 1 | / 2*sqrt(2)
anthony1d5e6702010-05-31 10:19:12 +0000700% | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000701%
anthony501c2f92010-06-02 10:55:14 +0000702% Type 5: | 0, -1, 0 |
703% | 1, 0, 1 | / 2
704% | 0, -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000705%
anthony1d5e6702010-05-31 10:19:12 +0000706% Type 6: | 1, 0, -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000707% | 0, 0, 0 | / 2
anthony1d5e6702010-05-31 10:19:12 +0000708% | -1, 0, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000709%
anthony501c2f92010-06-02 10:55:14 +0000710% Type 7: | 1, -2, 1 |
711% | -2, 4, -2 | / 6
712% | -1, -2, 1 |
713%
714% Type 8: | -2, 1, -2 |
anthonyf4e00312010-05-20 12:06:35 +0000715% | 1, 4, 1 | / 6
716% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000717%
anthonyf4e00312010-05-20 12:06:35 +0000718% Type 9: | 1, 1, 1 |
719% | 1, 1, 1 | / 3
720% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000721%
722% The first 4 are for edge detection, the next 4 are for line detection
723% and the last is to add a average component to the results.
724%
anthonyc3cd15b2010-05-27 06:05:40 +0000725% Using a special type of '-1' will return all 9 pre-weighted kernels
726% as a multi-kernel list, so that you can use them directly (without
727% normalization) with the special "-set option:morphology:compose Plus"
728% setting to apply the full FreiChen Edge Detection Technique.
729%
anthony1dd091a2010-05-27 06:31:15 +0000730% If 'type' is large it will be taken to be an actual rotation angle for
731% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
732% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
733%
anthony501c2f92010-06-02 10:55:14 +0000734% WARNING: The above was layed out as per
735% http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf
736% But rotated 90 degrees so direction is from left rather than the top.
737% I have yet to find any secondary confirmation of the above. The only
738% other source found was actual source code at
739% http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf
740% Neigher paper defineds the kernels in a way that looks locical or
741% correct when taken as a whole.
anthonye2a60ce2010-05-19 12:30:40 +0000742%
anthony602ab9b2010-01-05 08:06:50 +0000743% Boolean Kernels
744%
anthony3c10fc82010-05-13 02:40:51 +0000745% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000746% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000747% Kernel size will again be radius*2+1 square and defaults to radius 1,
748% generating a 3x3 kernel that is slightly larger than a square.
749%
anthony3c10fc82010-05-13 02:40:51 +0000750% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000751% Generate a square shaped kernel of size radius*2+1, and defaulting
752% to a 3x3 (radius 1).
753%
anthonyc1061722010-05-14 06:23:49 +0000754% Note that using a larger radius for the "Square" or the "Diamond" is
755% also equivelent to iterating the basic morphological method that many
756% times. However iterating with the smaller radius is actually faster
757% than using a larger kernel radius.
758%
759% Rectangle:{geometry}
760% Simply generate a rectangle of 1's with the size given. You can also
761% specify the location of the 'control point', otherwise the closest
762% pixel to the center of the rectangle is selected.
763%
764% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000765%
anthony3c10fc82010-05-13 02:40:51 +0000766% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000767% Generate a binary disk of the radius given, radius may be a float.
768% Kernel size will be ceil(radius)*2+1 square.
769% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000770% "Disk:1" => "diamond" or "cross:1"
771% "Disk:1.5" => "square"
772% "Disk:2" => "diamond:2"
773% "Disk:2.5" => a general disk shape of radius 2
774% "Disk:2.9" => "square:2"
775% "Disk:3.5" => default - octagonal/disk shape of radius 3
776% "Disk:4.2" => roughly octagonal shape of radius 4
777% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000778% After this all the kernel shape becomes more and more circular.
779%
780% Because a "disk" is more circular when using a larger radius, using a
781% larger radius is preferred over iterating the morphological operation.
782%
anthonyc1061722010-05-14 06:23:49 +0000783% Symbol Dilation Kernels
784%
785% These kernel is not a good general morphological kernel, but is used
786% more for highlighting and marking any single pixels in an image using,
787% a "Dilate" method as appropriate.
788%
789% For the same reasons iterating these kernels does not produce the
790% same result as using a larger radius for the symbol.
791%
anthony3c10fc82010-05-13 02:40:51 +0000792% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000793% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000794% Generate a kernel in the shape of a 'plus' or a 'cross' with
795% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000796%
797% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000798%
anthonyc1061722010-05-14 06:23:49 +0000799% Ring:{radius1},{radius2}[,{scale}]
800% A ring of the values given that falls between the two radii.
801% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
802% This is the 'edge' pixels of the default "Disk" kernel,
803% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000804%
anthony3dd0f622010-05-13 12:57:32 +0000805% Hit and Miss Kernels
806%
807% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000808% Find any peak larger than the pixels the fall between the two radii.
809% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000810% Edges
anthony1d45eb92010-05-25 11:13:23 +0000811% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000812% Corners
813% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000814% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000815% Find single pixel ridges or thin lines
816% Ridges2
817% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000818% Ridges3
819% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000820% LineEnds
821% Find end points of lines (for pruning a skeletion)
822% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000823% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000824% ConvexHull
825% Octagonal thicken kernel, to generate convex hulls of 45 degrees
826% Skeleton
827% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000828%
829% Distance Measuring Kernels
830%
anthonyc1061722010-05-14 06:23:49 +0000831% Different types of distance measuring methods, which are used with the
832% a 'Distance' morphology method for generating a gradient based on
833% distance from an edge of a binary shape, though there is a technique
834% for handling a anti-aliased shape.
835%
836% See the 'Distance' Morphological Method, for information of how it is
837% applied.
838%
anthony3dd0f622010-05-13 12:57:32 +0000839% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000840% Chebyshev Distance (also known as Tchebychev Distance) is a value of
841% one to any neighbour, orthogonal or diagonal. One why of thinking of
842% it is the number of squares a 'King' or 'Queen' in chess needs to
843% traverse reach any other position on a chess board. It results in a
844% 'square' like distance function, but one where diagonals are closer
845% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000846%
anthonyc1061722010-05-14 06:23:49 +0000847% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000848% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
849% Cab metric), is the distance needed when you can only travel in
850% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
851% in chess would travel. It results in a diamond like distances, where
852% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000853%
anthonyc1061722010-05-14 06:23:49 +0000854% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000855% Euclidean Distance is the 'direct' or 'as the crow flys distance.
856% However by default the kernel size only has a radius of 1, which
857% limits the distance to 'Knight' like moves, with only orthogonal and
858% diagonal measurements being correct. As such for the default kernel
859% you will get octagonal like distance function, which is reasonally
860% accurate.
861%
862% However if you use a larger radius such as "Euclidean:4" you will
863% get a much smoother distance gradient from the edge of the shape.
864% Of course a larger kernel is slower to use, and generally not needed.
865%
866% To allow the use of fractional distances that you get with diagonals
867% the actual distance is scaled by a fixed value which the user can
868% provide. This is not actually nessary for either ""Chebyshev" or
869% "Manhatten" distance kernels, but is done for all three distance
870% kernels. If no scale is provided it is set to a value of 100,
871% allowing for a maximum distance measurement of 655 pixels using a Q16
872% version of IM, from any edge. However for small images this can
873% result in quite a dark gradient.
874%
anthony602ab9b2010-01-05 08:06:50 +0000875*/
876
cristy2be15382010-01-21 02:38:03 +0000877MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000878 const GeometryInfo *args)
879{
cristy2be15382010-01-21 02:38:03 +0000880 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000881 *kernel;
882
cristybb503372010-05-27 20:51:26 +0000883 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000884 i;
885
cristybb503372010-05-27 20:51:26 +0000886 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000887 u,
888 v;
889
890 double
891 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
892
anthonyc1061722010-05-14 06:23:49 +0000893 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000894 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000895 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000896 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000897 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000898 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000899 break;
anthony1dd091a2010-05-27 06:31:15 +0000900 case UnityKernel: /* Named Descrete Convolution Kernels */
901 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000902 case SobelKernel:
903 case RobertsKernel:
904 case PrewittKernel:
905 case CompassKernel:
906 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000907 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000908 case CornersKernel: /* Hit and Miss kernels */
909 case LineEndsKernel:
910 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000911 case EdgesKernel:
912 case RidgesKernel:
913 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000914 case ConvexHullKernel:
915 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000916 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000917 /* A pre-generated kernel is not needed */
918 break;
anthony1dd091a2010-05-27 06:31:15 +0000919#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000920 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +0000921 case DoGKernel:
922 case LoGKernel:
anthonyc1061722010-05-14 06:23:49 +0000923 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +0000924 case CometKernel:
925 case DiamondKernel:
926 case SquareKernel:
927 case RectangleKernel:
928 case DiskKernel:
929 case PlusKernel:
930 case CrossKernel:
931 case RingKernel:
932 case PeaksKernel:
933 case ChebyshevKernel:
934 case ManhattenKernel:
935 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000936#else
anthony9eb4f742010-05-18 02:45:54 +0000937 default:
anthony1dd091a2010-05-27 06:31:15 +0000938#endif
anthony9eb4f742010-05-18 02:45:54 +0000939 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000940 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
941 if (kernel == (KernelInfo *) NULL)
942 return(kernel);
943 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000944 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000945 kernel->negative_range = kernel->positive_range = 0.0;
946 kernel->type = type;
947 kernel->next = (KernelInfo *) NULL;
948 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000949 break;
950 }
anthony602ab9b2010-01-05 08:06:50 +0000951
952 switch(type) {
953 /* Convolution Kernels */
954 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +0000955 case DoGKernel:
956 case LoGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000957 { double
anthonyc1061722010-05-14 06:23:49 +0000958 sigma = fabs(args->sigma),
959 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000960 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000961
anthonyc1061722010-05-14 06:23:49 +0000962 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +0000963 kernel->width = (size_t)args->rho*2+1;
anthony501c2f92010-06-02 10:55:14 +0000964 else if ( (type != DoGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000965 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
966 else
967 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
968 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +0000969 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000970 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
971 kernel->height*sizeof(double));
972 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000973 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000974
anthony46a369d2010-05-19 02:41:48 +0000975 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000976 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000977 *
978 * How to do this is currently not known, but appears to be
979 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000980 */
981
anthony501c2f92010-06-02 10:55:14 +0000982 if ( type == GaussianKernel || type == DoGKernel )
983 { /* Calculate a Gaussian, OR positive half of a DoG */
anthony9eb4f742010-05-18 02:45:54 +0000984 if ( sigma > MagickEpsilon )
985 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
986 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +0000987 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
988 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +0000989 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
990 }
991 else /* limiting case - a unity (normalized Dirac) kernel */
992 { (void) ResetMagickMemory(kernel->values,0, (size_t)
993 kernel->width*kernel->height*sizeof(double));
994 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
995 }
anthonyc1061722010-05-14 06:23:49 +0000996 }
anthony9eb4f742010-05-18 02:45:54 +0000997
anthony501c2f92010-06-02 10:55:14 +0000998 if ( type == DoGKernel )
anthonyc1061722010-05-14 06:23:49 +0000999 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
1000 if ( sigma2 > MagickEpsilon )
1001 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001002 A = 1.0/(2.0*sigma*sigma);
1003 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001004 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1005 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001006 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001007 }
anthony9eb4f742010-05-18 02:45:54 +00001008 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001009 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1010 }
anthony9eb4f742010-05-18 02:45:54 +00001011
anthony501c2f92010-06-02 10:55:14 +00001012 if ( type == LoGKernel )
anthony9eb4f742010-05-18 02:45:54 +00001013 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1014 if ( sigma > MagickEpsilon )
1015 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1016 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001017 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1018 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001019 { R = ((double)(u*u+v*v))*A;
1020 kernel->values[i] = (1-R)*exp(-R)*B;
1021 }
1022 }
1023 else /* special case - generate a unity kernel */
1024 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1025 kernel->width*kernel->height*sizeof(double));
1026 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1027 }
1028 }
1029
1030 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001031 ** radius, producing a smaller (darker) kernel. Also for very small
1032 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1033 ** producing a very bright kernel.
1034 **
1035 ** Normalization will still be needed.
1036 */
anthony602ab9b2010-01-05 08:06:50 +00001037
anthony3dd0f622010-05-13 12:57:32 +00001038 /* Normalize the 2D Gaussian Kernel
1039 **
anthonyc1061722010-05-14 06:23:49 +00001040 ** NB: a CorrelateNormalize performs a normal Normalize if
1041 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001042 */
anthony46a369d2010-05-19 02:41:48 +00001043 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001044 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001045
1046 break;
1047 }
1048 case BlurKernel:
1049 { double
anthonyc1061722010-05-14 06:23:49 +00001050 sigma = fabs(args->sigma),
anthony501c2f92010-06-02 10:55:14 +00001051 alpha, beta;
anthony602ab9b2010-01-05 08:06:50 +00001052
anthonyc1061722010-05-14 06:23:49 +00001053 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001054 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001055 else
anthony501c2f92010-06-02 10:55:14 +00001056 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
anthony602ab9b2010-01-05 08:06:50 +00001057 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001058 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001059 kernel->y = 0;
1060 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001061 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1062 kernel->height*sizeof(double));
1063 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001064 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001065
1066#if 1
1067#define KernelRank 3
1068 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1069 ** It generates a gaussian 3 times the width, and compresses it into
1070 ** the expected range. This produces a closer normalization of the
1071 ** resulting kernel, especially for very low sigma values.
1072 ** As such while wierd it is prefered.
1073 **
1074 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001075 **
1076 ** A properly normalized curve is generated (apart from edge clipping)
1077 ** even though we later normalize the result (for edge clipping)
1078 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001079 */
anthonyc1061722010-05-14 06:23:49 +00001080
1081 /* initialize */
cristybb503372010-05-27 20:51:26 +00001082 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001083 (void) ResetMagickMemory(kernel->values,0, (size_t)
1084 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001085 /* Calculate a Positive 1D Gaussian */
1086 if ( sigma > MagickEpsilon )
1087 { sigma *= KernelRank; /* simplify loop expressions */
anthony501c2f92010-06-02 10:55:14 +00001088 alpha = 1.0/(2.0*sigma*sigma);
1089 beta= 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001090 for ( u=-v; u <= v; u++) {
anthony501c2f92010-06-02 10:55:14 +00001091 kernel->values[(u+v)/KernelRank] +=
1092 exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001093 }
1094 }
1095 else /* special case - generate a unity kernel */
1096 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001097#else
anthonyc1061722010-05-14 06:23:49 +00001098 /* Direct calculation without curve averaging */
1099
1100 /* Calculate a Positive Gaussian */
1101 if ( sigma > MagickEpsilon )
anthony501c2f92010-06-02 10:55:14 +00001102 { alpha = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1103 beta = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001104 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony501c2f92010-06-02 10:55:14 +00001105 kernel->values[i] = exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001106 }
1107 else /* special case - generate a unity kernel */
1108 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1109 kernel->width*kernel->height*sizeof(double));
1110 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1111 }
anthony602ab9b2010-01-05 08:06:50 +00001112#endif
anthonyc1061722010-05-14 06:23:49 +00001113 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001114 ** radius, producing a smaller (darker) kernel. Also for very small
1115 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1116 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001117 **
1118 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001119 */
anthonycc6c8362010-01-25 04:14:01 +00001120
anthony602ab9b2010-01-05 08:06:50 +00001121 /* Normalize the 1D Gaussian Kernel
1122 **
anthonyc1061722010-05-14 06:23:49 +00001123 ** NB: a CorrelateNormalize performs a normal Normalize if
1124 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001125 */
anthony46a369d2010-05-19 02:41:48 +00001126 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1127 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001128
anthonyc1061722010-05-14 06:23:49 +00001129 /* rotate the 1D kernel by given angle */
anthony501c2f92010-06-02 10:55:14 +00001130 RotateKernelInfo(kernel, args->xi );
anthony602ab9b2010-01-05 08:06:50 +00001131 break;
1132 }
1133 case CometKernel:
1134 { double
anthony9eb4f742010-05-18 02:45:54 +00001135 sigma = fabs(args->sigma),
1136 A;
anthony602ab9b2010-01-05 08:06:50 +00001137
anthony602ab9b2010-01-05 08:06:50 +00001138 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001139 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001140 else
cristybb503372010-05-27 20:51:26 +00001141 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001142 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001143 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001144 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001145 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1146 kernel->height*sizeof(double));
1147 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001148 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001149
anthonyc1061722010-05-14 06:23:49 +00001150 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001151 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001152 ** curve to use so may change in the future. The function must be
1153 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001154 **
1155 ** As we are normalizing and not subtracting gaussians,
1156 ** there is no need for a divisor in the gaussian formula
1157 **
anthony43c49252010-05-18 10:59:50 +00001158 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001159 */
anthony9eb4f742010-05-18 02:45:54 +00001160 if ( sigma > MagickEpsilon )
1161 {
anthony602ab9b2010-01-05 08:06:50 +00001162#if 1
1163#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001164 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001165 (void) ResetMagickMemory(kernel->values,0, (size_t)
1166 kernel->width*sizeof(double));
1167 sigma *= KernelRank; /* simplify the loop expression */
1168 A = 1.0/(2.0*sigma*sigma);
1169 /* B = 1.0/(MagickSQ2PI*sigma); */
1170 for ( u=0; u < v; u++) {
1171 kernel->values[u/KernelRank] +=
1172 exp(-((double)(u*u))*A);
1173 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1174 }
cristybb503372010-05-27 20:51:26 +00001175 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001176 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001177#else
anthony9eb4f742010-05-18 02:45:54 +00001178 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1179 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001180 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001181 kernel->positive_range +=
1182 kernel->values[i] =
1183 exp(-((double)(i*i))*A);
1184 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001185#endif
anthony9eb4f742010-05-18 02:45:54 +00001186 }
1187 else /* special case - generate a unity kernel */
1188 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1189 kernel->width*kernel->height*sizeof(double));
1190 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1191 kernel->positive_range = 1.0;
1192 }
anthony46a369d2010-05-19 02:41:48 +00001193
1194 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001195 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001196 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001197
anthony999bb2c2010-02-18 12:38:01 +00001198 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1199 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001200 break;
1201 }
anthonyc1061722010-05-14 06:23:49 +00001202
anthony3c10fc82010-05-13 02:40:51 +00001203 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001204 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001205 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001206 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001207 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001208 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001209 break;
anthony9eb4f742010-05-18 02:45:54 +00001210 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001211 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001212 break;
1213 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001214 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1215 break;
1216 case 3:
anthonyc1061722010-05-14 06:23:49 +00001217 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001218 break;
anthony9eb4f742010-05-18 02:45:54 +00001219 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001220 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001221 "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 +00001222 break;
anthony9eb4f742010-05-18 02:45:54 +00001223 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001224 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001225 "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 +00001226 break;
anthony501c2f92010-06-02 10:55:14 +00001227 case 15: /* a 5x5 LoG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001228 kernel=ParseKernelArray(
1229 "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");
1230 break;
anthony501c2f92010-06-02 10:55:14 +00001231 case 19: /* a 9x9 LoG (sigma approx 1.4) */
anthony43c49252010-05-18 10:59:50 +00001232 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1233 kernel=ParseKernelArray(
1234 "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");
1235 break;
anthony3c10fc82010-05-13 02:40:51 +00001236 }
1237 if (kernel == (KernelInfo *) NULL)
1238 return(kernel);
1239 kernel->type = type;
1240 break;
1241 }
anthonyc1061722010-05-14 06:23:49 +00001242 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001243 {
anthony501c2f92010-06-02 10:55:14 +00001244 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonyc1061722010-05-14 06:23:49 +00001245 if (kernel == (KernelInfo *) NULL)
1246 return(kernel);
1247 kernel->type = type;
1248 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1249 break;
1250 }
1251 case RobertsKernel:
1252 {
anthony501c2f92010-06-02 10:55:14 +00001253 kernel=ParseKernelArray("3: 0,0,0 1,-1,0 0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001254 if (kernel == (KernelInfo *) NULL)
1255 return(kernel);
1256 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001257 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001258 break;
1259 }
1260 case PrewittKernel:
1261 {
anthony501c2f92010-06-02 10:55:14 +00001262 kernel=ParseKernelArray("3: 1,0,-1 1,0,-1 1,0,-1");
anthonyc1061722010-05-14 06:23:49 +00001263 if (kernel == (KernelInfo *) NULL)
1264 return(kernel);
1265 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001266 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001267 break;
1268 }
1269 case CompassKernel:
1270 {
anthony501c2f92010-06-02 10:55:14 +00001271 kernel=ParseKernelArray("3: 1,1,-1 1,-2,-1 1,1,-1");
anthonyc1061722010-05-14 06:23:49 +00001272 if (kernel == (KernelInfo *) NULL)
1273 return(kernel);
1274 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001275 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001276 break;
1277 }
anthony9eb4f742010-05-18 02:45:54 +00001278 case KirschKernel:
1279 {
anthony501c2f92010-06-02 10:55:14 +00001280 kernel=ParseKernelArray("3: 5,-3,-3 5,0,-3 5,-3,-3");
anthony9eb4f742010-05-18 02:45:54 +00001281 if (kernel == (KernelInfo *) NULL)
1282 return(kernel);
1283 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001284 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001285 break;
1286 }
anthonye2a60ce2010-05-19 12:30:40 +00001287 case FreiChenKernel:
anthony501c2f92010-06-02 10:55:14 +00001288 /* Direction is set to be left to right positive */
1289 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf -- RIGHT? */
1290 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf -- WRONG? */
anthony1dd091a2010-05-27 06:31:15 +00001291 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001292 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001293 case 0:
anthony501c2f92010-06-02 10:55:14 +00001294 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonyc3cd15b2010-05-27 06:05:40 +00001295 if (kernel == (KernelInfo *) NULL)
1296 return(kernel);
anthony501c2f92010-06-02 10:55:14 +00001297 kernel->values[3] = +MagickSQ2;
1298 kernel->values[5] = -MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001299 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001300 break;
anthonye2a60ce2010-05-19 12:30:40 +00001301 case 1:
anthony501c2f92010-06-02 10:55:14 +00001302 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonye2a60ce2010-05-19 12:30:40 +00001303 if (kernel == (KernelInfo *) NULL)
1304 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001305 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001306 kernel->values[3] = +MagickSQ2;
1307 kernel->values[5] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001308 CalcKernelMetaData(kernel); /* recalculate meta-data */
1309 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1310 break;
1311 case 2:
anthony501c2f92010-06-02 10:55:14 +00001312 kernel=ParseKernelArray("3: 1,2,1 0,0,0 1,2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001313 if (kernel == (KernelInfo *) NULL)
1314 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001315 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001316 kernel->values[1] = +MagickSQ2;
1317 kernel->values[7] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001318 CalcKernelMetaData(kernel);
1319 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1320 break;
1321 case 3:
anthony501c2f92010-06-02 10:55:14 +00001322 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001323 if (kernel == (KernelInfo *) NULL)
1324 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001325 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001326 kernel->values[0] = +MagickSQ2;
1327 kernel->values[8] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001328 CalcKernelMetaData(kernel);
1329 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1330 break;
1331 case 4:
anthony1d5e6702010-05-31 10:19:12 +00001332 kernel=ParseKernelArray("3: 0,1,-2 -1,0,1 2,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001333 if (kernel == (KernelInfo *) NULL)
1334 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001335 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001336 kernel->values[2] = -MagickSQ2;
1337 kernel->values[6] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001338 CalcKernelMetaData(kernel);
1339 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1340 break;
1341 case 5:
anthony501c2f92010-06-02 10:55:14 +00001342 kernel=ParseKernelArray("3: 0,-1,0 1,0,1 0,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001343 if (kernel == (KernelInfo *) NULL)
1344 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001345 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001346 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1347 break;
1348 case 6:
anthony1d5e6702010-05-31 10:19:12 +00001349 kernel=ParseKernelArray("3: 1,0,-1 0,0,0 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001350 if (kernel == (KernelInfo *) NULL)
1351 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001352 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001353 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1354 break;
1355 case 7:
anthony501c2f92010-06-02 10:55:14 +00001356 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 -1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001357 if (kernel == (KernelInfo *) NULL)
1358 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001359 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001360 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1361 break;
1362 case 8:
anthony501c2f92010-06-02 10:55:14 +00001363 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001364 if (kernel == (KernelInfo *) NULL)
1365 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001366 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001367 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1368 break;
1369 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001370 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001371 if (kernel == (KernelInfo *) NULL)
1372 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001373 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001374 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1375 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001376 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001377 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1378 if (kernel == (KernelInfo *) NULL)
1379 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001380 break;
anthonye2a60ce2010-05-19 12:30:40 +00001381 }
anthonyc3cd15b2010-05-27 06:05:40 +00001382 if ( fabs(args->sigma) > MagickEpsilon )
1383 /* Rotate by correctly supplied 'angle' */
1384 RotateKernelInfo(kernel, args->sigma);
1385 else if ( args->rho > 30.0 || args->rho < -30.0 )
1386 /* Rotate by out of bounds 'type' */
1387 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001388 break;
1389 }
1390
anthonyc1061722010-05-14 06:23:49 +00001391 /* Boolean Kernels */
1392 case DiamondKernel:
1393 {
1394 if (args->rho < 1.0)
1395 kernel->width = kernel->height = 3; /* default radius = 1 */
1396 else
cristybb503372010-05-27 20:51:26 +00001397 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1398 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001399
1400 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1401 kernel->height*sizeof(double));
1402 if (kernel->values == (double *) NULL)
1403 return(DestroyKernelInfo(kernel));
1404
1405 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001406 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1407 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony1d5e6702010-05-31 10:19:12 +00001408 if ( (labs((long) u)+labs((long) v)) <= (long) kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001409 kernel->positive_range += kernel->values[i] = args->sigma;
1410 else
1411 kernel->values[i] = nan;
1412 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1413 break;
1414 }
1415 case SquareKernel:
1416 case RectangleKernel:
1417 { double
1418 scale;
anthony602ab9b2010-01-05 08:06:50 +00001419 if ( type == SquareKernel )
1420 {
1421 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001422 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001423 else
cristybb503372010-05-27 20:51:26 +00001424 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1425 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001426 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001427 }
1428 else {
cristy2be15382010-01-21 02:38:03 +00001429 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001430 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001431 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001432 kernel->width = (size_t)args->rho;
1433 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001434 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1435 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001436 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001437 kernel->x = (ssize_t) args->xi;
1438 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001439 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001440 }
1441 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1442 kernel->height*sizeof(double));
1443 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001444 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001445
anthony3dd0f622010-05-13 12:57:32 +00001446 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001447 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001448 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001449 kernel->values[i] = scale;
1450 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1451 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001452 break;
anthony602ab9b2010-01-05 08:06:50 +00001453 }
anthony602ab9b2010-01-05 08:06:50 +00001454 case DiskKernel:
1455 {
anthonye4d89962010-05-29 10:53:11 +00001456 ssize_t
1457 limit = (ssize_t)(args->rho*args->rho);
1458
1459 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001460 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001461 else
anthonye4d89962010-05-29 10:53:11 +00001462 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001463 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001464
1465 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1466 kernel->height*sizeof(double));
1467 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001468 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001469
anthony3dd0f622010-05-13 12:57:32 +00001470 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001471 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1472 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001473 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001474 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001475 else
1476 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001477 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001478 break;
1479 }
1480 case PlusKernel:
1481 {
1482 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001483 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001484 else
cristybb503372010-05-27 20:51:26 +00001485 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1486 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001487
1488 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1489 kernel->height*sizeof(double));
1490 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001491 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001492
cristycee97112010-05-28 00:44:52 +00001493 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001494 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1495 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001496 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1497 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1498 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001499 break;
1500 }
anthony3dd0f622010-05-13 12:57:32 +00001501 case CrossKernel:
1502 {
1503 if (args->rho < 1.0)
1504 kernel->width = kernel->height = 5; /* default radius 2 */
1505 else
cristybb503372010-05-27 20:51:26 +00001506 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1507 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001508
1509 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1510 kernel->height*sizeof(double));
1511 if (kernel->values == (double *) NULL)
1512 return(DestroyKernelInfo(kernel));
1513
cristycee97112010-05-28 00:44:52 +00001514 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001515 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1516 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001517 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1518 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1519 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1520 break;
1521 }
1522 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001523 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001524 case PeaksKernel:
1525 {
cristybb503372010-05-27 20:51:26 +00001526 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001527 limit1,
anthonyc1061722010-05-14 06:23:49 +00001528 limit2,
1529 scale;
anthony3dd0f622010-05-13 12:57:32 +00001530
1531 if (args->rho < args->sigma)
1532 {
cristybb503372010-05-27 20:51:26 +00001533 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001534 limit1 = (ssize_t)(args->rho*args->rho);
1535 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001536 }
1537 else
1538 {
cristybb503372010-05-27 20:51:26 +00001539 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001540 limit1 = (ssize_t)(args->sigma*args->sigma);
1541 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001542 }
anthonyc1061722010-05-14 06:23:49 +00001543 if ( limit2 <= 0 )
1544 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1545
anthony3dd0f622010-05-13 12:57:32 +00001546 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001547 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001548 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1549 kernel->height*sizeof(double));
1550 if (kernel->values == (double *) NULL)
1551 return(DestroyKernelInfo(kernel));
1552
anthonyc1061722010-05-14 06:23:49 +00001553 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001554 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1555 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1556 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1557 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001558 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001559 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001560 else
1561 kernel->values[i] = nan;
1562 }
cristye96405a2010-05-19 02:24:31 +00001563 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001564 if ( type == PeaksKernel ) {
1565 /* set the central point in the middle */
1566 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1567 kernel->positive_range = 1.0;
1568 kernel->maximum = 1.0;
1569 }
anthony3dd0f622010-05-13 12:57:32 +00001570 break;
1571 }
anthony43c49252010-05-18 10:59:50 +00001572 case EdgesKernel:
1573 {
1574 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1575 if (kernel == (KernelInfo *) NULL)
1576 return(kernel);
1577 kernel->type = type;
1578 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1579 break;
1580 }
anthony3dd0f622010-05-13 12:57:32 +00001581 case CornersKernel:
1582 {
anthony4f1dcb72010-05-14 08:43:10 +00001583 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001584 if (kernel == (KernelInfo *) NULL)
1585 return(kernel);
1586 kernel->type = type;
1587 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1588 break;
1589 }
anthony47f5d062010-05-23 07:47:50 +00001590 case RidgesKernel:
1591 {
anthony24a19842010-05-27 12:18:34 +00001592 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001593 if (kernel == (KernelInfo *) NULL)
1594 return(kernel);
1595 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001596 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001597 break;
1598 }
anthony1d45eb92010-05-25 11:13:23 +00001599 case Ridges2Kernel:
1600 {
1601 KernelInfo
1602 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001603 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001604 if (kernel == (KernelInfo *) NULL)
1605 return(kernel);
1606 kernel->type = type;
1607 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001608#if 0
1609 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001610 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1611 if (new_kernel == (KernelInfo *) NULL)
1612 return(DestroyKernelInfo(kernel));
1613 new_kernel->type = type;
1614 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1615 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001616#endif
1617 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1618 /* Unfortunatally we can not yet rotate a non-square kernel */
1619 /* But then we can't flip a non-symetrical kernel either */
1620 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1621 if (new_kernel == (KernelInfo *) NULL)
1622 return(DestroyKernelInfo(kernel));
1623 new_kernel->type = type;
1624 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001625 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001626 if (new_kernel == (KernelInfo *) NULL)
1627 return(DestroyKernelInfo(kernel));
1628 new_kernel->type = type;
1629 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001630 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001631 if (new_kernel == (KernelInfo *) NULL)
1632 return(DestroyKernelInfo(kernel));
1633 new_kernel->type = type;
1634 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001635 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001636 if (new_kernel == (KernelInfo *) NULL)
1637 return(DestroyKernelInfo(kernel));
1638 new_kernel->type = type;
1639 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001640 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001641 if (new_kernel == (KernelInfo *) NULL)
1642 return(DestroyKernelInfo(kernel));
1643 new_kernel->type = type;
1644 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001645 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001646 if (new_kernel == (KernelInfo *) NULL)
1647 return(DestroyKernelInfo(kernel));
1648 new_kernel->type = type;
1649 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001650 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001651 if (new_kernel == (KernelInfo *) NULL)
1652 return(DestroyKernelInfo(kernel));
1653 new_kernel->type = type;
1654 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001655 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001656 if (new_kernel == (KernelInfo *) NULL)
1657 return(DestroyKernelInfo(kernel));
1658 new_kernel->type = type;
1659 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001660 break;
1661 }
anthony3dd0f622010-05-13 12:57:32 +00001662 case LineEndsKernel:
1663 {
anthony43c49252010-05-18 10:59:50 +00001664 KernelInfo
1665 *new_kernel;
1666 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001667 if (kernel == (KernelInfo *) NULL)
1668 return(kernel);
1669 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001670 ExpandKernelInfo(kernel, 90.0);
1671 /* append second set of 4 kernels */
1672 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1673 if (new_kernel == (KernelInfo *) NULL)
1674 return(DestroyKernelInfo(kernel));
1675 new_kernel->type = type;
1676 ExpandKernelInfo(new_kernel, 90.0);
1677 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001678 break;
1679 }
1680 case LineJunctionsKernel:
1681 {
1682 KernelInfo
1683 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001684 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001685 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001686 if (kernel == (KernelInfo *) NULL)
1687 return(kernel);
1688 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001689 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001690 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001691 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001692 if (new_kernel == (KernelInfo *) NULL)
1693 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001694 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001695 ExpandKernelInfo(new_kernel, 90.0);
1696 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001697 break;
1698 }
anthony3dd0f622010-05-13 12:57:32 +00001699 case ConvexHullKernel:
1700 {
anthony3928ec62010-05-27 14:03:29 +00001701 KernelInfo
1702 *new_kernel;
1703 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001704 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001705 if (kernel == (KernelInfo *) NULL)
1706 return(kernel);
1707 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001708 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001709 /* append the mirror versions too */
1710 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1711 if (new_kernel == (KernelInfo *) NULL)
1712 return(DestroyKernelInfo(kernel));
1713 new_kernel->type = type;
1714 ExpandKernelInfo(new_kernel, 45.0);
1715 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001716 break;
1717 }
anthony47f5d062010-05-23 07:47:50 +00001718 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001719 { /* what is the best form for skeletonization by thinning? */
anthonye4d89962010-05-29 10:53:11 +00001720#if 1
1721 /* Full Corner rotated to form edges too */
anthony43c49252010-05-18 10:59:50 +00001722 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001723 if (kernel == (KernelInfo *) NULL)
1724 return(kernel);
1725 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001726 ExpandKernelInfo(kernel, 45);
anthonye4d89962010-05-29 10:53:11 +00001727#endif
1728#if 0
1729 /* As last but thin the edges before looking for corners */
1730 KernelInfo
1731 *new_kernel;
1732 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1733 if (kernel == (KernelInfo *) NULL)
1734 return(kernel);
1735 kernel->type = type;
1736 ExpandKernelInfo(kernel, 90.0);
1737 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
1738 if (new_kernel == (KernelInfo *) NULL)
1739 return(DestroyKernelInfo(kernel));
1740 new_kernel->type = type;
1741 ExpandKernelInfo(new_kernel, 90.0);
1742 LastKernelInfo(kernel)->next = new_kernel;
1743#endif
1744#if 0
1745 kernel=AcquireKernelInfo("Edges;Corners");
1746#endif
1747#if 0
1748 kernel=AcquireKernelInfo("Corners;Edges");
anthony47f5d062010-05-23 07:47:50 +00001749#endif
anthony3dd0f622010-05-13 12:57:32 +00001750 break;
1751 }
anthonya648a302010-05-27 02:14:36 +00001752 case MatKernel: /* experimental - MAT from a Distance Gradient */
1753 {
1754 KernelInfo
1755 *new_kernel;
1756 /* Ridge Kernel but without the diagonal */
1757 kernel=ParseKernelArray("3x1: 0,1,0");
1758 if (kernel == (KernelInfo *) NULL)
1759 return(kernel);
1760 kernel->type = RidgesKernel;
1761 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1762 /* Plus the 2 pixel ridges kernel - no diagonal */
1763 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1764 if (new_kernel == (KernelInfo *) NULL)
1765 return(kernel);
1766 LastKernelInfo(kernel)->next = new_kernel;
1767 break;
1768 }
anthony602ab9b2010-01-05 08:06:50 +00001769 /* Distance Measuring Kernels */
1770 case ChebyshevKernel:
1771 {
anthony602ab9b2010-01-05 08:06:50 +00001772 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001773 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001774 else
cristybb503372010-05-27 20:51:26 +00001775 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1776 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001777
1778 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1779 kernel->height*sizeof(double));
1780 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001781 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001782
cristybb503372010-05-27 20:51:26 +00001783 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1784 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001785 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001786 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001787 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001788 break;
1789 }
1790 case ManhattenKernel:
1791 {
anthony602ab9b2010-01-05 08:06:50 +00001792 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001793 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001794 else
cristybb503372010-05-27 20:51:26 +00001795 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1796 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001797
1798 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1799 kernel->height*sizeof(double));
1800 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001801 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001802
cristybb503372010-05-27 20:51:26 +00001803 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1804 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001805 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001806 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001807 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001808 break;
1809 }
1810 case EuclideanKernel:
1811 {
anthony602ab9b2010-01-05 08:06:50 +00001812 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001813 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001814 else
cristybb503372010-05-27 20:51:26 +00001815 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1816 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001817
1818 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1819 kernel->height*sizeof(double));
1820 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001821 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001822
cristybb503372010-05-27 20:51:26 +00001823 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1824 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001825 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001826 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001827 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001828 break;
1829 }
anthony46a369d2010-05-19 02:41:48 +00001830 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001831 default:
anthonyc1061722010-05-14 06:23:49 +00001832 {
anthony46a369d2010-05-19 02:41:48 +00001833 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1834 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001835 if (kernel == (KernelInfo *) NULL)
1836 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001837 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001838 break;
1839 }
anthony602ab9b2010-01-05 08:06:50 +00001840 break;
1841 }
1842
1843 return(kernel);
1844}
anthonyc94cdb02010-01-06 08:15:29 +00001845
anthony602ab9b2010-01-05 08:06:50 +00001846/*
1847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1848% %
1849% %
1850% %
cristy6771f1e2010-03-05 19:43:39 +00001851% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001852% %
1853% %
1854% %
1855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1856%
anthony1b2bc0a2010-05-12 05:25:22 +00001857% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1858% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001859% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001860%
cristye6365592010-04-02 17:31:23 +00001861% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001862%
anthony930be612010-02-08 04:26:15 +00001863% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001864%
1865% A description of each parameter follows:
1866%
1867% o kernel: the Morphology/Convolution kernel to be cloned
1868%
1869*/
cristyef656912010-03-05 19:54:59 +00001870MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001871{
cristybb503372010-05-27 20:51:26 +00001872 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001873 i;
1874
cristy19eb6412010-04-23 14:42:29 +00001875 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001876 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001877
1878 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001879 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1880 if (new_kernel == (KernelInfo *) NULL)
1881 return(new_kernel);
1882 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001883
1884 /* replace the values with a copy of the values */
1885 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001886 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001887 if (new_kernel->values == (double *) NULL)
1888 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001889 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001890 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001891
1892 /* Also clone the next kernel in the kernel list */
1893 if ( kernel->next != (KernelInfo *) NULL ) {
1894 new_kernel->next = CloneKernelInfo(kernel->next);
1895 if ( new_kernel->next == (KernelInfo *) NULL )
1896 return(DestroyKernelInfo(new_kernel));
1897 }
1898
anthony7a01dcf2010-05-11 12:25:52 +00001899 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001900}
1901
1902/*
1903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1904% %
1905% %
1906% %
anthony83ba99b2010-01-24 08:48:15 +00001907% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001908% %
1909% %
1910% %
1911%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1912%
anthony83ba99b2010-01-24 08:48:15 +00001913% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1914% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001915%
anthony83ba99b2010-01-24 08:48:15 +00001916% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001917%
anthony83ba99b2010-01-24 08:48:15 +00001918% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001919%
1920% A description of each parameter follows:
1921%
1922% o kernel: the Morphology/Convolution kernel to be destroyed
1923%
1924*/
1925
anthony83ba99b2010-01-24 08:48:15 +00001926MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001927{
cristy2be15382010-01-21 02:38:03 +00001928 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001929
anthony7a01dcf2010-05-11 12:25:52 +00001930 if ( kernel->next != (KernelInfo *) NULL )
1931 kernel->next = DestroyKernelInfo(kernel->next);
1932
1933 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1934 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001935 return(kernel);
1936}
anthonyc94cdb02010-01-06 08:15:29 +00001937
1938/*
1939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1940% %
1941% %
1942% %
anthony3c10fc82010-05-13 02:40:51 +00001943% E x p a n d K e r n e l I n f o %
1944% %
1945% %
1946% %
1947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948%
1949% ExpandKernelInfo() takes a single kernel, and expands it into a list
1950% of kernels each incrementally rotated the angle given.
1951%
1952% WARNING: 45 degree rotations only works for 3x3 kernels.
1953% While 90 degree roatations only works for linear and square kernels
1954%
1955% The format of the RotateKernelInfo method is:
1956%
1957% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1958%
1959% A description of each parameter follows:
1960%
1961% o kernel: the Morphology/Convolution kernel
1962%
1963% o angle: angle to rotate in degrees
1964%
1965% This function is only internel to this module, as it is not finalized,
1966% especially with regard to non-orthogonal angles, and rotation of larger
1967% 2D kernels.
1968*/
anthony47f5d062010-05-23 07:47:50 +00001969
1970/* Internal Routine - Return true if two kernels are the same */
1971static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1972 const KernelInfo *kernel2)
1973{
cristybb503372010-05-27 20:51:26 +00001974 register size_t
anthony47f5d062010-05-23 07:47:50 +00001975 i;
anthony1d45eb92010-05-25 11:13:23 +00001976
1977 /* check size and origin location */
1978 if ( kernel1->width != kernel2->width
1979 || kernel1->height != kernel2->height
1980 || kernel1->x != kernel2->x
1981 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001982 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001983
1984 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001985 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001986 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001987 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1988 return MagickFalse;
1989 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
1990 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001991 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00001992 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
1993 return MagickFalse;
1994 }
anthony1d45eb92010-05-25 11:13:23 +00001995
anthony47f5d062010-05-23 07:47:50 +00001996 return MagickTrue;
1997}
1998
1999static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002000{
2001 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002002 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002003 *last;
cristya9a61ad2010-05-13 12:47:41 +00002004
anthony3c10fc82010-05-13 02:40:51 +00002005 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002006 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002007 clone = CloneKernelInfo(last);
2008 RotateKernelInfo(clone, angle);
2009 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002010 break;
cristy84d9b552010-05-24 18:23:54 +00002011 last->next = clone;
2012 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002013 }
cristy84d9b552010-05-24 18:23:54 +00002014 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002015 return;
anthony3c10fc82010-05-13 02:40:51 +00002016}
2017
2018/*
2019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020% %
2021% %
2022% %
anthony46a369d2010-05-19 02:41:48 +00002023+ C a l c M e t a K e r n a l I n f o %
2024% %
2025% %
2026% %
2027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028%
2029% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2030% using the kernel values. This should only ne used if it is not posible to
2031% calculate that meta-data in some easier way.
2032%
2033% It is important that the meta-data is correct before ScaleKernelInfo() is
2034% used to perform kernel normalization.
2035%
2036% The format of the CalcKernelMetaData method is:
2037%
2038% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2039%
2040% A description of each parameter follows:
2041%
2042% o kernel: the Morphology/Convolution kernel to modify
2043%
2044% WARNING: Minimum and Maximum values are assumed to include zero, even if
2045% zero is not part of the kernel (as in Gaussian Derived kernels). This
2046% however is not true for flat-shaped morphological kernels.
2047%
2048% WARNING: Only the specific kernel pointed to is modified, not a list of
2049% multiple kernels.
2050%
2051% This is an internal function and not expected to be useful outside this
2052% module. This could change however.
2053*/
2054static void CalcKernelMetaData(KernelInfo *kernel)
2055{
cristybb503372010-05-27 20:51:26 +00002056 register size_t
anthony46a369d2010-05-19 02:41:48 +00002057 i;
2058
2059 kernel->minimum = kernel->maximum = 0.0;
2060 kernel->negative_range = kernel->positive_range = 0.0;
2061 for (i=0; i < (kernel->width*kernel->height); i++)
2062 {
2063 if ( fabs(kernel->values[i]) < MagickEpsilon )
2064 kernel->values[i] = 0.0;
2065 ( kernel->values[i] < 0)
2066 ? ( kernel->negative_range += kernel->values[i] )
2067 : ( kernel->positive_range += kernel->values[i] );
2068 Minimize(kernel->minimum, kernel->values[i]);
2069 Maximize(kernel->maximum, kernel->values[i]);
2070 }
2071
2072 return;
2073}
2074
2075/*
2076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2077% %
2078% %
2079% %
anthony9eb4f742010-05-18 02:45:54 +00002080% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002081% %
2082% %
2083% %
2084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2085%
anthony9eb4f742010-05-18 02:45:54 +00002086% MorphologyApply() applies a morphological method, multiple times using
2087% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002088%
anthony9eb4f742010-05-18 02:45:54 +00002089% It is basically equivelent to as MorphologyImageChannel() (see below) but
2090% without user controls, that that function extracts and applies to kernels
2091% and morphology methods.
2092%
2093% More specifically kernels are not normalized/scaled/blended by the
2094% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2095% (-bias setting or image->bias) is passed directly to this function,
2096% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002097%
anthony47f5d062010-05-23 07:47:50 +00002098% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002099%
anthony9eb4f742010-05-18 02:45:54 +00002100% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002101% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002102% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002103% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002104%
2105% A description of each parameter follows:
2106%
2107% o image: the image.
2108%
2109% o method: the morphology method to be applied.
2110%
2111% o iterations: apply the operation this many times (or no change).
2112% A value of -1 means loop until no change found.
2113% How this is applied may depend on the morphology method.
2114% Typically this is a value of 1.
2115%
2116% o channel: the channel type.
2117%
2118% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002119% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002120%
anthony47f5d062010-05-23 07:47:50 +00002121% o compose: How to handle or merge multi-kernel results.
2122% If 'Undefined' use default of the Morphology method.
2123% If 'No' force image to be re-iterated by each kernel.
2124% Otherwise merge the results using the mathematical compose
2125% method given.
2126%
2127% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002128%
anthony602ab9b2010-01-05 08:06:50 +00002129% o exception: return any errors or warnings in this structure.
2130%
anthony602ab9b2010-01-05 08:06:50 +00002131*/
2132
anthony930be612010-02-08 04:26:15 +00002133
anthony9eb4f742010-05-18 02:45:54 +00002134/* Apply a Morphology Primative to an image using the given kernel.
2135** Two pre-created images must be provided, no image is created.
2136** Returning the number of pixels that changed.
2137*/
cristybb503372010-05-27 20:51:26 +00002138static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002139 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002140 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002141{
cristy2be15382010-01-21 02:38:03 +00002142#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002143
cristy5f959472010-05-27 22:19:46 +00002144 CacheView
2145 *p_view,
2146 *q_view;
2147
cristybb503372010-05-27 20:51:26 +00002148 ssize_t
anthony29188a82010-01-22 10:12:34 +00002149 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002150 changed;
2151
2152 MagickBooleanType
2153 status;
2154
cristy5f959472010-05-27 22:19:46 +00002155 MagickOffsetType
2156 progress;
anthony602ab9b2010-01-05 08:06:50 +00002157
anthonye4d89962010-05-29 10:53:11 +00002158 assert(image != (Image *) NULL);
2159 assert(image->signature == MagickSignature);
2160 assert(result_image != (Image *) NULL);
2161 assert(result_image->signature == MagickSignature);
2162 assert(kernel != (KernelInfo *) NULL);
2163 assert(kernel->signature == MagickSignature);
2164 assert(exception != (ExceptionInfo *) NULL);
2165 assert(exception->signature == MagickSignature);
2166
anthony602ab9b2010-01-05 08:06:50 +00002167 status=MagickTrue;
2168 changed=0;
2169 progress=0;
2170
anthony602ab9b2010-01-05 08:06:50 +00002171 p_view=AcquireCacheView(image);
2172 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002173
anthonycc6c8362010-01-25 04:14:01 +00002174 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002175 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002176 */
cristyc99304f2010-02-01 15:26:27 +00002177 offx = kernel->x;
2178 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002179 switch(method) {
anthony930be612010-02-08 04:26:15 +00002180 case ConvolveMorphology:
2181 case DilateMorphology:
2182 case DilateIntensityMorphology:
2183 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002184 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002185 offx = (ssize_t) kernel->width-offx-1;
2186 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002187 break;
anthony5ef8e942010-05-11 06:51:12 +00002188 case ErodeMorphology:
2189 case ErodeIntensityMorphology:
2190 case HitAndMissMorphology:
2191 case ThinningMorphology:
2192 case ThickenMorphology:
2193 /* kernel is user as is, without reflection */
2194 break;
anthony930be612010-02-08 04:26:15 +00002195 default:
anthony9eb4f742010-05-18 02:45:54 +00002196 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002197 break;
anthony29188a82010-01-22 10:12:34 +00002198 }
2199
anthony602ab9b2010-01-05 08:06:50 +00002200#if defined(MAGICKCORE_OPENMP_SUPPORT)
2201 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2202#endif
cristybb503372010-05-27 20:51:26 +00002203 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002204 {
2205 MagickBooleanType
2206 sync;
2207
2208 register const PixelPacket
2209 *restrict p;
2210
2211 register const IndexPacket
2212 *restrict p_indexes;
2213
2214 register PixelPacket
2215 *restrict q;
2216
2217 register IndexPacket
2218 *restrict q_indexes;
2219
cristybb503372010-05-27 20:51:26 +00002220 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002221 x;
2222
cristybb503372010-05-27 20:51:26 +00002223 size_t
anthony602ab9b2010-01-05 08:06:50 +00002224 r;
2225
2226 if (status == MagickFalse)
2227 continue;
anthony29188a82010-01-22 10:12:34 +00002228 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2229 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002230 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2231 exception);
2232 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2233 {
2234 status=MagickFalse;
2235 continue;
2236 }
2237 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2238 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002239 r = (image->columns+kernel->width)*offy+offx; /* constant */
2240
cristybb503372010-05-27 20:51:26 +00002241 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002242 {
cristybb503372010-05-27 20:51:26 +00002243 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002244 v;
2245
cristybb503372010-05-27 20:51:26 +00002246 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002247 u;
2248
2249 register const double
2250 *restrict k;
2251
2252 register const PixelPacket
2253 *restrict k_pixels;
2254
2255 register const IndexPacket
2256 *restrict k_indexes;
2257
2258 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002259 result,
2260 min,
2261 max;
anthony602ab9b2010-01-05 08:06:50 +00002262
anthony29188a82010-01-22 10:12:34 +00002263 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002264 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002265 */
anthony602ab9b2010-01-05 08:06:50 +00002266 *q = p[r];
2267 if (image->colorspace == CMYKColorspace)
2268 q_indexes[x] = p_indexes[r];
2269
anthony5ef8e942010-05-11 06:51:12 +00002270 /* Defaults */
2271 min.red =
2272 min.green =
2273 min.blue =
2274 min.opacity =
2275 min.index = (MagickRealType) QuantumRange;
2276 max.red =
2277 max.green =
2278 max.blue =
2279 max.opacity =
2280 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002281 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002282 result.red = (MagickRealType) p[r].red;
2283 result.green = (MagickRealType) p[r].green;
2284 result.blue = (MagickRealType) p[r].blue;
2285 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002286 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002287 if ( image->colorspace == CMYKColorspace)
2288 result.index = (MagickRealType) p_indexes[r];
2289
anthony602ab9b2010-01-05 08:06:50 +00002290 switch (method) {
2291 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002292 /* Set the user defined bias of the weighted average output */
2293 result.red =
2294 result.green =
2295 result.blue =
2296 result.opacity =
2297 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002298 break;
anthony4fd27e22010-02-07 08:17:18 +00002299 case DilateIntensityMorphology:
2300 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002301 /* use a boolean flag indicating when first match found */
2302 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002303 break;
anthony602ab9b2010-01-05 08:06:50 +00002304 default:
anthony602ab9b2010-01-05 08:06:50 +00002305 break;
2306 }
2307
2308 switch ( method ) {
2309 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002310 /* Weighted Average of pixels using reflected kernel
2311 **
2312 ** NOTE for correct working of this operation for asymetrical
2313 ** kernels, the kernel needs to be applied in its reflected form.
2314 ** That is its values needs to be reversed.
2315 **
2316 ** Correlation is actually the same as this but without reflecting
2317 ** the kernel, and thus 'lower-level' that Convolution. However
2318 ** as Convolution is the more common method used, and it does not
2319 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002320 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002321 **
2322 ** Correlation will have its kernel reflected before calling
2323 ** this function to do a Convolve.
2324 **
2325 ** For more details of Correlation vs Convolution see
2326 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2327 */
anthony5ef8e942010-05-11 06:51:12 +00002328 if (((channel & SyncChannels) != 0 ) &&
2329 (image->matte == MagickTrue))
2330 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2331 ** Weight the color channels with Alpha Channel so that
2332 ** transparent pixels are not part of the results.
2333 */
anthony602ab9b2010-01-05 08:06:50 +00002334 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002335 alpha, /* color channel weighting : kernel*alpha */
2336 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002337
2338 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002339 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002340 k_pixels = p;
2341 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002342 for (v=0; v < (ssize_t) kernel->height; v++) {
2343 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002344 if ( IsNan(*k) ) continue;
2345 alpha=(*k)*(QuantumScale*(QuantumRange-
2346 k_pixels[u].opacity));
2347 gamma += alpha;
2348 result.red += alpha*k_pixels[u].red;
2349 result.green += alpha*k_pixels[u].green;
2350 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002351 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002352 if ( image->colorspace == CMYKColorspace)
2353 result.index += alpha*k_indexes[u];
2354 }
2355 k_pixels += image->columns+kernel->width;
2356 k_indexes += image->columns+kernel->width;
2357 }
2358 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002359 result.red *= gamma;
2360 result.green *= gamma;
2361 result.blue *= gamma;
2362 result.opacity *= gamma;
2363 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002364 }
anthony5ef8e942010-05-11 06:51:12 +00002365 else
2366 {
2367 /* No 'Sync' flag, or no Alpha involved.
2368 ** Convolution is simple individual channel weigthed sum.
2369 */
2370 k = &kernel->values[ kernel->width*kernel->height-1 ];
2371 k_pixels = p;
2372 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002373 for (v=0; v < (ssize_t) kernel->height; v++) {
2374 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002375 if ( IsNan(*k) ) continue;
2376 result.red += (*k)*k_pixels[u].red;
2377 result.green += (*k)*k_pixels[u].green;
2378 result.blue += (*k)*k_pixels[u].blue;
2379 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2380 if ( image->colorspace == CMYKColorspace)
2381 result.index += (*k)*k_indexes[u];
2382 }
2383 k_pixels += image->columns+kernel->width;
2384 k_indexes += image->columns+kernel->width;
2385 }
2386 }
anthony602ab9b2010-01-05 08:06:50 +00002387 break;
2388
anthony4fd27e22010-02-07 08:17:18 +00002389 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002390 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002391 **
2392 ** NOTE that the kernel is not reflected for this operation!
2393 **
2394 ** NOTE: in normal Greyscale Morphology, the kernel value should
2395 ** be added to the real value, this is currently not done, due to
2396 ** the nature of the boolean kernels being used.
2397 */
anthony4fd27e22010-02-07 08:17:18 +00002398 k = kernel->values;
2399 k_pixels = p;
2400 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002401 for (v=0; v < (ssize_t) kernel->height; v++) {
2402 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002403 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002404 Minimize(min.red, (double) k_pixels[u].red);
2405 Minimize(min.green, (double) k_pixels[u].green);
2406 Minimize(min.blue, (double) k_pixels[u].blue);
2407 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002408 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002409 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002410 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002411 }
2412 k_pixels += image->columns+kernel->width;
2413 k_indexes += image->columns+kernel->width;
2414 }
2415 break;
2416
anthony5ef8e942010-05-11 06:51:12 +00002417
anthony83ba99b2010-01-24 08:48:15 +00002418 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002419 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002420 **
2421 ** NOTE for correct working of this operation for asymetrical
2422 ** kernels, the kernel needs to be applied in its reflected form.
2423 ** That is its values needs to be reversed.
2424 **
2425 ** NOTE: in normal Greyscale Morphology, the kernel value should
2426 ** be added to the real value, this is currently not done, due to
2427 ** the nature of the boolean kernels being used.
2428 **
2429 */
anthony29188a82010-01-22 10:12:34 +00002430 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002431 k_pixels = p;
2432 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002433 for (v=0; v < (ssize_t) kernel->height; v++) {
2434 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002435 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002436 Maximize(max.red, (double) k_pixels[u].red);
2437 Maximize(max.green, (double) k_pixels[u].green);
2438 Maximize(max.blue, (double) k_pixels[u].blue);
2439 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002440 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002441 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002442 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002443 }
2444 k_pixels += image->columns+kernel->width;
2445 k_indexes += image->columns+kernel->width;
2446 }
anthony602ab9b2010-01-05 08:06:50 +00002447 break;
2448
anthony5ef8e942010-05-11 06:51:12 +00002449 case HitAndMissMorphology:
2450 case ThinningMorphology:
2451 case ThickenMorphology:
2452 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2453 **
2454 ** NOTE that the kernel is not reflected for this operation,
2455 ** and consists of both foreground and background pixel
2456 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2457 ** with either Nan or 0.5 values for don't care.
2458 **
2459 ** Note that this can produce negative results, though really
2460 ** only a positive match has any real value.
2461 */
2462 k = kernel->values;
2463 k_pixels = p;
2464 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002465 for (v=0; v < (ssize_t) kernel->height; v++) {
2466 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002467 if ( IsNan(*k) ) continue;
2468 if ( (*k) > 0.7 )
2469 { /* minimim of foreground pixels */
2470 Minimize(min.red, (double) k_pixels[u].red);
2471 Minimize(min.green, (double) k_pixels[u].green);
2472 Minimize(min.blue, (double) k_pixels[u].blue);
2473 Minimize(min.opacity,
2474 QuantumRange-(double) k_pixels[u].opacity);
2475 if ( image->colorspace == CMYKColorspace)
2476 Minimize(min.index, (double) k_indexes[u]);
2477 }
2478 else if ( (*k) < 0.3 )
2479 { /* maximum of background pixels */
2480 Maximize(max.red, (double) k_pixels[u].red);
2481 Maximize(max.green, (double) k_pixels[u].green);
2482 Maximize(max.blue, (double) k_pixels[u].blue);
2483 Maximize(max.opacity,
2484 QuantumRange-(double) k_pixels[u].opacity);
2485 if ( image->colorspace == CMYKColorspace)
2486 Maximize(max.index, (double) k_indexes[u]);
2487 }
2488 }
2489 k_pixels += image->columns+kernel->width;
2490 k_indexes += image->columns+kernel->width;
2491 }
2492 /* Pattern Match only if min fg larger than min bg pixels */
2493 min.red -= max.red; Maximize( min.red, 0.0 );
2494 min.green -= max.green; Maximize( min.green, 0.0 );
2495 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2496 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2497 min.index -= max.index; Maximize( min.index, 0.0 );
2498 break;
2499
anthony4fd27e22010-02-07 08:17:18 +00002500 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002501 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2502 **
2503 ** WARNING: the intensity test fails for CMYK and does not
2504 ** take into account the moderating effect of teh alpha channel
2505 ** on the intensity.
2506 **
2507 ** NOTE that the kernel is not reflected for this operation!
2508 */
anthony602ab9b2010-01-05 08:06:50 +00002509 k = kernel->values;
2510 k_pixels = p;
2511 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002512 for (v=0; v < (ssize_t) kernel->height; v++) {
2513 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002514 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002515 if ( result.red == 0.0 ||
2516 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2517 /* copy the whole pixel - no channel selection */
2518 *q = k_pixels[u];
2519 if ( result.red > 0.0 ) changed++;
2520 result.red = 1.0;
2521 }
anthony602ab9b2010-01-05 08:06:50 +00002522 }
2523 k_pixels += image->columns+kernel->width;
2524 k_indexes += image->columns+kernel->width;
2525 }
anthony602ab9b2010-01-05 08:06:50 +00002526 break;
2527
anthony83ba99b2010-01-24 08:48:15 +00002528 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002529 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2530 **
2531 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002532 ** take into account the moderating effect of the alpha channel
2533 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002534 **
2535 ** NOTE for correct working of this operation for asymetrical
2536 ** kernels, the kernel needs to be applied in its reflected form.
2537 ** That is its values needs to be reversed.
2538 */
anthony29188a82010-01-22 10:12:34 +00002539 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002540 k_pixels = p;
2541 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002542 for (v=0; v < (ssize_t) kernel->height; v++) {
2543 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002544 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2545 if ( result.red == 0.0 ||
2546 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2547 /* copy the whole pixel - no channel selection */
2548 *q = k_pixels[u];
2549 if ( result.red > 0.0 ) changed++;
2550 result.red = 1.0;
2551 }
anthony602ab9b2010-01-05 08:06:50 +00002552 }
2553 k_pixels += image->columns+kernel->width;
2554 k_indexes += image->columns+kernel->width;
2555 }
anthony602ab9b2010-01-05 08:06:50 +00002556 break;
2557
anthony5ef8e942010-05-11 06:51:12 +00002558
anthony602ab9b2010-01-05 08:06:50 +00002559 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002560 /* Add kernel Value and select the minimum value found.
2561 ** The result is a iterative distance from edge of image shape.
2562 **
2563 ** All Distance Kernels are symetrical, but that may not always
2564 ** be the case. For example how about a distance from left edges?
2565 ** To work correctly with asymetrical kernels the reflected kernel
2566 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002567 **
2568 ** Actually this is really a GreyErode with a negative kernel!
2569 **
anthony930be612010-02-08 04:26:15 +00002570 */
anthony29188a82010-01-22 10:12:34 +00002571 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002572 k_pixels = p;
2573 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002574 for (v=0; v < (ssize_t) kernel->height; v++) {
2575 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002576 if ( IsNan(*k) ) continue;
2577 Minimize(result.red, (*k)+k_pixels[u].red);
2578 Minimize(result.green, (*k)+k_pixels[u].green);
2579 Minimize(result.blue, (*k)+k_pixels[u].blue);
2580 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2581 if ( image->colorspace == CMYKColorspace)
2582 Minimize(result.index, (*k)+k_indexes[u]);
2583 }
2584 k_pixels += image->columns+kernel->width;
2585 k_indexes += image->columns+kernel->width;
2586 }
anthony602ab9b2010-01-05 08:06:50 +00002587 break;
2588
2589 case UndefinedMorphology:
2590 default:
2591 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002592 }
anthony5ef8e942010-05-11 06:51:12 +00002593 /* Final mathematics of results (combine with original image?)
2594 **
2595 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2596 ** be done here but works better with iteration as a image difference
2597 ** in the controling function (below). Thicken and Thinning however
2598 ** should be done here so thay can be iterated correctly.
2599 */
2600 switch ( method ) {
2601 case HitAndMissMorphology:
2602 case ErodeMorphology:
2603 result = min; /* minimum of neighbourhood */
2604 break;
2605 case DilateMorphology:
2606 result = max; /* maximum of neighbourhood */
2607 break;
2608 case ThinningMorphology:
2609 /* subtract pattern match from original */
2610 result.red -= min.red;
2611 result.green -= min.green;
2612 result.blue -= min.blue;
2613 result.opacity -= min.opacity;
2614 result.index -= min.index;
2615 break;
2616 case ThickenMorphology:
2617 /* Union with original image (maximize) - or should this be + */
2618 Maximize( result.red, min.red );
2619 Maximize( result.green, min.green );
2620 Maximize( result.blue, min.blue );
2621 Maximize( result.opacity, min.opacity );
2622 Maximize( result.index, min.index );
2623 break;
2624 default:
2625 /* result directly calculated or assigned */
2626 break;
2627 }
2628 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002629 switch ( method ) {
2630 case UndefinedMorphology:
2631 case DilateIntensityMorphology:
2632 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002633 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002634 default:
anthony83ba99b2010-01-24 08:48:15 +00002635 if ((channel & RedChannel) != 0)
2636 q->red = ClampToQuantum(result.red);
2637 if ((channel & GreenChannel) != 0)
2638 q->green = ClampToQuantum(result.green);
2639 if ((channel & BlueChannel) != 0)
2640 q->blue = ClampToQuantum(result.blue);
2641 if ((channel & OpacityChannel) != 0
2642 && image->matte == MagickTrue )
2643 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2644 if ((channel & IndexChannel) != 0
2645 && image->colorspace == CMYKColorspace)
2646 q_indexes[x] = ClampToQuantum(result.index);
2647 break;
2648 }
anthony5ef8e942010-05-11 06:51:12 +00002649 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002650 if ( ( p[r].red != q->red )
2651 || ( p[r].green != q->green )
2652 || ( p[r].blue != q->blue )
2653 || ( p[r].opacity != q->opacity )
2654 || ( image->colorspace == CMYKColorspace &&
2655 p_indexes[r] != q_indexes[x] ) )
2656 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002657 p++;
2658 q++;
anthony83ba99b2010-01-24 08:48:15 +00002659 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002660 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2661 if (sync == MagickFalse)
2662 status=MagickFalse;
2663 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2664 {
2665 MagickBooleanType
2666 proceed;
2667
2668#if defined(MAGICKCORE_OPENMP_SUPPORT)
2669 #pragma omp critical (MagickCore_MorphologyImage)
2670#endif
2671 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2672 if (proceed == MagickFalse)
2673 status=MagickFalse;
2674 }
anthony83ba99b2010-01-24 08:48:15 +00002675 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002676 result_image->type=image->type;
2677 q_view=DestroyCacheView(q_view);
2678 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002679 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002680}
2681
anthony4fd27e22010-02-07 08:17:18 +00002682
anthony9eb4f742010-05-18 02:45:54 +00002683MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002684 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002685 const KernelInfo *kernel, const CompositeOperator compose,
2686 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002687{
2688 Image
anthony47f5d062010-05-23 07:47:50 +00002689 *curr_image, /* Image we are working with or iterating */
2690 *work_image, /* secondary image for primative iteration */
2691 *save_image, /* saved image - for 'edge' method only */
2692 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002693
anthony4fd27e22010-02-07 08:17:18 +00002694 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002695 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2696 *norm_kernel, /* the current normal un-reflected kernel */
2697 *rflt_kernel, /* the current reflected kernel (if needed) */
2698 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002699
2700 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002701 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002702
2703 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002704 rslt_compose; /* multi-kernel compose method for results to use */
2705
2706 MagickBooleanType
2707 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002708
cristybb503372010-05-27 20:51:26 +00002709 size_t
anthony47f5d062010-05-23 07:47:50 +00002710 method_loop, /* Loop 1: number of compound method iterations */
2711 method_limit, /* maximum number of compound method iterations */
2712 kernel_number, /* Loop 2: the kernel number being applied */
2713 stage_loop, /* Loop 3: primative loop for compound morphology */
2714 stage_limit, /* how many primatives in this compound */
2715 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2716 kernel_limit, /* number of times to iterate kernel */
2717 count, /* total count of primative steps applied */
2718 changed, /* number pixels changed by last primative operation */
2719 kernel_changed, /* total count of changed using iterated kernel */
2720 method_changed; /* total count of changed over method iteration */
2721
2722 char
2723 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002724
anthony602ab9b2010-01-05 08:06:50 +00002725 assert(image != (Image *) NULL);
2726 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002727 assert(kernel != (KernelInfo *) NULL);
2728 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002729 assert(exception != (ExceptionInfo *) NULL);
2730 assert(exception->signature == MagickSignature);
2731
anthonyc3e48252010-05-24 12:43:11 +00002732 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002733 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002734 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002735
cristybb503372010-05-27 20:51:26 +00002736 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002737 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2738 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002739
cristye96405a2010-05-19 02:24:31 +00002740 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2741 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002742
anthony9eb4f742010-05-18 02:45:54 +00002743 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002744 curr_image = (Image *) image;
2745 work_image = save_image = rslt_image = (Image *) NULL;
2746 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002747
anthony47f5d062010-05-23 07:47:50 +00002748 /* Initialize specific methods
2749 * + which loop should use the given iteratations
2750 * + how many primatives make up the compound morphology
2751 * + multi-kernel compose method to use (by default)
2752 */
2753 method_limit = 1; /* just do method once, unless otherwise set */
2754 stage_limit = 1; /* assume method is not a compount */
2755 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002756 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002757 case SmoothMorphology: /* 4 primative compound morphology */
2758 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002759 break;
anthony47f5d062010-05-23 07:47:50 +00002760 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002761 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002762 case TopHatMorphology:
2763 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002764 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002765 case BottomHatMorphology:
2766 case EdgeMorphology:
2767 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002768 break;
2769 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002770 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002771 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002772 break;
anthonyc3e48252010-05-24 12:43:11 +00002773 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002774 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002775 method_limit = kernel_limit; /* iterate method with each kernel */
2776 kernel_limit = 1; /* do not do kernel iteration */
anthonye4d89962010-05-29 10:53:11 +00002777 case DistanceMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002778 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002779 break;
2780 default:
anthony930be612010-02-08 04:26:15 +00002781 break;
anthony602ab9b2010-01-05 08:06:50 +00002782 }
2783
anthonyc3e48252010-05-24 12:43:11 +00002784 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002785 if ( compose != UndefinedCompositeOp )
2786 rslt_compose = compose; /* override default composition for method */
2787 if ( rslt_compose == UndefinedCompositeOp )
2788 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2789
anthonyc3e48252010-05-24 12:43:11 +00002790 /* Some methods require a reflected kernel to use with primatives.
2791 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002792 switch ( method ) {
2793 case CorrelateMorphology:
2794 case CloseMorphology:
2795 case CloseIntensityMorphology:
2796 case BottomHatMorphology:
2797 case SmoothMorphology:
2798 reflected_kernel = CloneKernelInfo(kernel);
2799 if (reflected_kernel == (KernelInfo *) NULL)
2800 goto error_cleanup;
2801 RotateKernelInfo(reflected_kernel,180);
2802 break;
2803 default:
2804 break;
anthony9eb4f742010-05-18 02:45:54 +00002805 }
anthony7a01dcf2010-05-11 12:25:52 +00002806
anthony47f5d062010-05-23 07:47:50 +00002807 /* Loop 1: iterate the compound method */
2808 method_loop = 0;
2809 method_changed = 1;
2810 while ( method_loop < method_limit && method_changed > 0 ) {
2811 method_loop++;
2812 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002813
anthony47f5d062010-05-23 07:47:50 +00002814 /* Loop 2: iterate over each kernel in a multi-kernel list */
2815 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00002816 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00002817 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00002818
anthony47f5d062010-05-23 07:47:50 +00002819 kernel_number = 0;
2820 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002821
anthony47f5d062010-05-23 07:47:50 +00002822 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2823 stage_loop = 0; /* the compound morphology stage number */
2824 while ( stage_loop < stage_limit ) {
2825 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002826
anthony47f5d062010-05-23 07:47:50 +00002827 /* Select primative morphology for this stage of compound method */
2828 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002829 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002830 switch( method ) {
2831 case ErodeMorphology: /* just erode */
2832 case EdgeInMorphology: /* erode and image difference */
2833 primative = ErodeMorphology;
2834 break;
2835 case DilateMorphology: /* just dilate */
2836 case EdgeOutMorphology: /* dilate and image difference */
2837 primative = DilateMorphology;
2838 break;
2839 case OpenMorphology: /* erode then dialate */
2840 case TopHatMorphology: /* open and image difference */
2841 primative = ErodeMorphology;
2842 if ( stage_loop == 2 )
2843 primative = DilateMorphology;
2844 break;
2845 case OpenIntensityMorphology:
2846 primative = ErodeIntensityMorphology;
2847 if ( stage_loop == 2 )
2848 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00002849 break;
anthony47f5d062010-05-23 07:47:50 +00002850 case CloseMorphology: /* dilate, then erode */
2851 case BottomHatMorphology: /* close and image difference */
2852 this_kernel = rflt_kernel; /* use the reflected kernel */
2853 primative = DilateMorphology;
2854 if ( stage_loop == 2 )
2855 primative = ErodeMorphology;
2856 break;
2857 case CloseIntensityMorphology:
2858 this_kernel = rflt_kernel; /* use the reflected kernel */
2859 primative = DilateIntensityMorphology;
2860 if ( stage_loop == 2 )
2861 primative = ErodeIntensityMorphology;
2862 break;
2863 case SmoothMorphology: /* open, close */
2864 switch ( stage_loop ) {
2865 case 1: /* start an open method, which starts with Erode */
2866 primative = ErodeMorphology;
2867 break;
2868 case 2: /* now Dilate the Erode */
2869 primative = DilateMorphology;
2870 break;
2871 case 3: /* Reflect kernel a close */
2872 this_kernel = rflt_kernel; /* use the reflected kernel */
2873 primative = DilateMorphology;
2874 break;
2875 case 4: /* Finish the Close */
2876 this_kernel = rflt_kernel; /* use the reflected kernel */
2877 primative = ErodeMorphology;
2878 break;
2879 }
2880 break;
2881 case EdgeMorphology: /* dilate and erode difference */
2882 primative = DilateMorphology;
2883 if ( stage_loop == 2 ) {
2884 save_image = curr_image; /* save the image difference */
2885 curr_image = (Image *) image;
2886 primative = ErodeMorphology;
2887 }
2888 break;
2889 case CorrelateMorphology:
2890 /* A Correlation is a Convolution with a reflected kernel.
2891 ** However a Convolution is a weighted sum using a reflected
2892 ** kernel. It may seem stange to convert a Correlation into a
2893 ** Convolution as the Correlation is the simplier method, but
2894 ** Convolution is much more commonly used, and it makes sense to
2895 ** implement it directly so as to avoid the need to duplicate the
2896 ** kernel when it is not required (which is typically the
2897 ** default).
2898 */
2899 this_kernel = rflt_kernel; /* use the reflected kernel */
2900 primative = ConvolveMorphology;
2901 break;
2902 default:
anthony47f5d062010-05-23 07:47:50 +00002903 break;
2904 }
anthonye4d89962010-05-29 10:53:11 +00002905 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00002906
anthony47f5d062010-05-23 07:47:50 +00002907 /* Extra information for debugging compound operations */
2908 if ( verbose == MagickTrue ) {
2909 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002910 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002911 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2912 (unsigned long) method_loop,(unsigned long) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00002913 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002914 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002915 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2916 (unsigned long) method_loop);
anthony47f5d062010-05-23 07:47:50 +00002917 else
2918 v_info[0] = '\0';
2919 }
2920
2921 /* Loop 4: Iterate the kernel with primative */
2922 kernel_loop = 0;
2923 kernel_changed = 0;
2924 changed = 1;
2925 while ( kernel_loop < kernel_limit && changed > 0 ) {
2926 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002927
2928 /* Create a destination image, if not yet defined */
2929 if ( work_image == (Image *) NULL )
2930 {
2931 work_image=CloneImage(image,0,0,MagickTrue,exception);
2932 if (work_image == (Image *) NULL)
2933 goto error_cleanup;
2934 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2935 {
2936 InheritException(exception,&work_image->exception);
2937 goto error_cleanup;
2938 }
2939 }
2940
anthony501c2f92010-06-02 10:55:14 +00002941 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002942 count++;
anthony47f5d062010-05-23 07:47:50 +00002943 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002944 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002945 kernel_changed += changed;
2946 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002947
anthony47f5d062010-05-23 07:47:50 +00002948 if ( verbose == MagickTrue ) {
2949 if ( kernel_loop > 1 )
2950 fprintf(stderr, "\n"); /* add end-of-line from previous */
2951 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2952 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2953 ( this_kernel == rflt_kernel ) ? "*" : "",
cristyf2faecf2010-05-28 19:19:36 +00002954 (unsigned long) method_loop+kernel_loop-1,(unsigned long)
2955 kernel_number,(unsigned long) count,(unsigned long) changed);
anthony47f5d062010-05-23 07:47:50 +00002956 }
anthony9eb4f742010-05-18 02:45:54 +00002957 /* prepare next loop */
2958 { Image *tmp = work_image; /* swap images for iteration */
2959 work_image = curr_image;
2960 curr_image = tmp;
2961 }
2962 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002963 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002964
anthony47f5d062010-05-23 07:47:50 +00002965 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002966
anthony47f5d062010-05-23 07:47:50 +00002967 if ( verbose == MagickTrue && kernel_changed != changed )
cristyf2faecf2010-05-28 19:19:36 +00002968 fprintf(stderr, " Total %lu",(unsigned long) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00002969 if ( verbose == MagickTrue && stage_loop < stage_limit )
2970 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002971
2972#if 0
anthonye4d89962010-05-29 10:53:11 +00002973 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
2974 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
2975 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
2976 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
2977 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002978#endif
2979
anthony47f5d062010-05-23 07:47:50 +00002980 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002981
anthony47f5d062010-05-23 07:47:50 +00002982 /* Final Post-processing for some Compound Methods
2983 **
2984 ** The removal of any 'Sync' channel flag in the Image Compositon
2985 ** below ensures the methematical compose method is applied in a
2986 ** purely mathematical way, and only to the selected channels.
2987 ** Turn off SVG composition 'alpha blending'.
2988 */
2989 switch( method ) {
2990 case EdgeOutMorphology:
2991 case EdgeInMorphology:
2992 case TopHatMorphology:
2993 case BottomHatMorphology:
2994 if ( verbose == MagickTrue )
2995 fprintf(stderr, "\n%s: Difference with original image",
2996 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2997 (void) CompositeImageChannel(curr_image,
2998 (ChannelType) (channel & ~SyncChannels),
2999 DifferenceCompositeOp, image, 0, 0);
3000 break;
3001 case EdgeMorphology:
3002 if ( verbose == MagickTrue )
3003 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3004 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3005 (void) CompositeImageChannel(curr_image,
3006 (ChannelType) (channel & ~SyncChannels),
3007 DifferenceCompositeOp, save_image, 0, 0);
3008 save_image = DestroyImage(save_image); /* finished with save image */
3009 break;
3010 default:
3011 break;
3012 }
3013
3014 /* multi-kernel handling: re-iterate, or compose results */
3015 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003016 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003017 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003018 { if ( verbose == MagickTrue ) {
3019 if ( this_kernel->next != (KernelInfo *) NULL )
3020 fprintf(stderr, " (re-iterate)");
3021 else
3022 fprintf(stderr, " (done)");
3023 }
3024 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003025 }
anthony47f5d062010-05-23 07:47:50 +00003026 else if ( rslt_image == (Image *) NULL)
3027 { if ( verbose == MagickTrue )
3028 fprintf(stderr, " (save for compose)");
3029 rslt_image = curr_image;
3030 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003031 }
anthony47f5d062010-05-23 07:47:50 +00003032 else
3033 { /* add the new 'current' result to the composition
3034 **
3035 ** The removal of any 'Sync' channel flag in the Image Compositon
3036 ** below ensures the methematical compose method is applied in a
3037 ** purely mathematical way, and only to the selected channels.
3038 ** Turn off SVG composition 'alpha blending'.
3039 */
3040 if ( verbose == MagickTrue )
3041 fprintf(stderr, " (compose \"%s\")",
3042 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3043 (void) CompositeImageChannel(rslt_image,
3044 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3045 curr_image, 0, 0);
3046 curr_image = (Image *) image; /* continue with original image */
3047 }
3048 if ( verbose == MagickTrue )
3049 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003050
anthony47f5d062010-05-23 07:47:50 +00003051 /* loop to the next kernel in a multi-kernel list */
3052 norm_kernel = norm_kernel->next;
3053 if ( rflt_kernel != (KernelInfo *) NULL )
3054 rflt_kernel = rflt_kernel->next;
3055 kernel_number++;
3056 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003057
anthony47f5d062010-05-23 07:47:50 +00003058 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003059
anthony9eb4f742010-05-18 02:45:54 +00003060 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003061
anthony47f5d062010-05-23 07:47:50 +00003062 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003063error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003064 if ( curr_image != (Image *) NULL &&
3065 curr_image != rslt_image &&
3066 curr_image != image )
3067 curr_image = DestroyImage(curr_image);
3068 if ( rslt_image != (Image *) NULL )
3069 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003070exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003071 if ( curr_image != (Image *) NULL &&
3072 curr_image != rslt_image &&
3073 curr_image != image )
3074 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003075 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003076 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003077 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003078 save_image = DestroyImage(save_image);
3079 if ( reflected_kernel != (KernelInfo *) NULL )
3080 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3081 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003082}
3083
3084/*
3085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3086% %
3087% %
3088% %
3089% M o r p h o l o g y I m a g e C h a n n e l %
3090% %
3091% %
3092% %
3093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3094%
3095% MorphologyImageChannel() applies a user supplied kernel to the image
3096% according to the given mophology method.
3097%
3098% This function applies any and all user defined settings before calling
3099% the above internal function MorphologyApply().
3100%
3101% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003102% * Output Bias for Convolution and correlation ("-bias")
3103% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3104% This can also includes the addition of a scaled unity kernel.
3105% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003106%
3107% The format of the MorphologyImage method is:
3108%
3109% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003110% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003111%
3112% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003113% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003114% KernelInfo *kernel,ExceptionInfo *exception)
3115%
3116% A description of each parameter follows:
3117%
3118% o image: the image.
3119%
3120% o method: the morphology method to be applied.
3121%
3122% o iterations: apply the operation this many times (or no change).
3123% A value of -1 means loop until no change found.
3124% How this is applied may depend on the morphology method.
3125% Typically this is a value of 1.
3126%
3127% o channel: the channel type.
3128%
3129% o kernel: An array of double representing the morphology kernel.
3130% Warning: kernel may be normalized for the Convolve method.
3131%
3132% o exception: return any errors or warnings in this structure.
3133%
3134*/
3135
3136MagickExport Image *MorphologyImageChannel(const Image *image,
3137 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003138 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003139{
3140 const char
3141 *artifact;
3142
3143 KernelInfo
3144 *curr_kernel;
3145
anthony47f5d062010-05-23 07:47:50 +00003146 CompositeOperator
3147 compose;
3148
anthony9eb4f742010-05-18 02:45:54 +00003149 Image
3150 *morphology_image;
3151
3152
anthony46a369d2010-05-19 02:41:48 +00003153 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3154 * This is done BEFORE the ShowKernelInfo() function is called so that
3155 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003156 */
3157 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003158 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003159 {
3160 artifact = GetImageArtifact(image,"convolve:scale");
3161 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003162 if ( curr_kernel == kernel )
3163 curr_kernel = CloneKernelInfo(kernel);
3164 if (curr_kernel == (KernelInfo *) NULL) {
3165 curr_kernel=DestroyKernelInfo(curr_kernel);
3166 return((Image *) NULL);
3167 }
anthony46a369d2010-05-19 02:41:48 +00003168 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003169 }
3170 }
3171
3172 /* display the (normalized) kernel via stderr */
3173 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003174 if ( artifact == (const char *) NULL)
3175 artifact = GetImageArtifact(image,"convolve:showkernel");
3176 if ( artifact == (const char *) NULL)
3177 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003178 if ( artifact != (const char *) NULL)
3179 ShowKernelInfo(curr_kernel);
3180
anthony47f5d062010-05-23 07:47:50 +00003181 /* override the default handling of multi-kernel morphology results
3182 * if 'Undefined' use the default method
3183 * if 'None' (default for 'Convolve') re-iterate previous result
3184 * otherwise merge resulting images using compose method given
3185 */
3186 compose = UndefinedCompositeOp; /* use default for method */
3187 artifact = GetImageArtifact(image,"morphology:compose");
3188 if ( artifact != (const char *) NULL)
3189 compose = (CompositeOperator) ParseMagickOption(
3190 MagickComposeOptions,MagickFalse,artifact);
3191
anthony9eb4f742010-05-18 02:45:54 +00003192 /* Apply the Morphology */
3193 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003194 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003195
3196 /* Cleanup and Exit */
3197 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003198 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003199 return(morphology_image);
3200}
3201
3202MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003203 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003204 *exception)
3205{
3206 Image
3207 *morphology_image;
3208
3209 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3210 iterations,kernel,exception);
3211 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003212}
anthony83ba99b2010-01-24 08:48:15 +00003213
3214/*
3215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3216% %
3217% %
3218% %
anthony4fd27e22010-02-07 08:17:18 +00003219+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003220% %
3221% %
3222% %
3223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3224%
anthony46a369d2010-05-19 02:41:48 +00003225% RotateKernelInfo() rotates the kernel by the angle given.
3226%
3227% Currently it is restricted to 90 degree angles, of either 1D kernels
3228% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3229% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003230%
anthony4fd27e22010-02-07 08:17:18 +00003231% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003232%
anthony4fd27e22010-02-07 08:17:18 +00003233% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003234%
3235% A description of each parameter follows:
3236%
3237% o kernel: the Morphology/Convolution kernel
3238%
3239% o angle: angle to rotate in degrees
3240%
anthony46a369d2010-05-19 02:41:48 +00003241% This function is currently internal to this module only, but can be exported
3242% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003243*/
anthony4fd27e22010-02-07 08:17:18 +00003244static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003245{
anthony1b2bc0a2010-05-12 05:25:22 +00003246 /* angle the lower kernels first */
3247 if ( kernel->next != (KernelInfo *) NULL)
3248 RotateKernelInfo(kernel->next, angle);
3249
anthony83ba99b2010-01-24 08:48:15 +00003250 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3251 **
3252 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3253 */
3254
3255 /* Modulus the angle */
3256 angle = fmod(angle, 360.0);
3257 if ( angle < 0 )
3258 angle += 360.0;
3259
anthony3c10fc82010-05-13 02:40:51 +00003260 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003261 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003262
anthony3dd0f622010-05-13 12:57:32 +00003263 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003264 switch (kernel->type) {
3265 /* These built-in kernels are cylindrical kernels, rotating is useless */
3266 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +00003267 case DoGKernel:
3268 case LoGKernel:
anthony83ba99b2010-01-24 08:48:15 +00003269 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003270 case PeaksKernel:
3271 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003272 case ChebyshevKernel:
3273 case ManhattenKernel:
3274 case EuclideanKernel:
3275 return;
3276
3277 /* These may be rotatable at non-90 angles in the future */
3278 /* but simply rotating them in multiples of 90 degrees is useless */
3279 case SquareKernel:
3280 case DiamondKernel:
3281 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003282 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003283 return;
3284
3285 /* These only allows a +/-90 degree rotation (by transpose) */
3286 /* A 180 degree rotation is useless */
3287 case BlurKernel:
3288 case RectangleKernel:
3289 if ( 135.0 < angle && angle <= 225.0 )
3290 return;
3291 if ( 225.0 < angle && angle <= 315.0 )
3292 angle -= 180;
3293 break;
3294
anthony3dd0f622010-05-13 12:57:32 +00003295 default:
anthony83ba99b2010-01-24 08:48:15 +00003296 break;
3297 }
anthony3c10fc82010-05-13 02:40:51 +00003298 /* Attempt rotations by 45 degrees */
3299 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3300 {
3301 if ( kernel->width == 3 && kernel->height == 3 )
3302 { /* Rotate a 3x3 square by 45 degree angle */
3303 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003304 kernel->values[0] = kernel->values[3];
3305 kernel->values[3] = kernel->values[6];
3306 kernel->values[6] = kernel->values[7];
3307 kernel->values[7] = kernel->values[8];
3308 kernel->values[8] = kernel->values[5];
3309 kernel->values[5] = kernel->values[2];
3310 kernel->values[2] = kernel->values[1];
3311 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003312 /* rotate non-centered origin */
3313 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003314 ssize_t x,y;
3315 x = (ssize_t) kernel->x-1;
3316 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003317 if ( x == y ) x = 0;
3318 else if ( x == 0 ) x = -y;
3319 else if ( x == -y ) y = 0;
3320 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003321 kernel->x = (ssize_t) x+1;
3322 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003323 }
anthony43c49252010-05-18 10:59:50 +00003324 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3325 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003326 }
3327 else
3328 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3329 }
3330 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3331 {
3332 if ( kernel->width == 1 || kernel->height == 1 )
3333 { /* Do a transpose of the image, which results in a 90
3334 ** degree rotation of a 1 dimentional kernel
3335 */
cristybb503372010-05-27 20:51:26 +00003336 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003337 t;
cristybb503372010-05-27 20:51:26 +00003338 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003339 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003340 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003341 t = kernel->x;
3342 kernel->x = kernel->y;
3343 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003344 if ( kernel->width == 1 ) {
3345 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3346 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3347 } else {
3348 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3349 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3350 }
anthony3c10fc82010-05-13 02:40:51 +00003351 }
3352 else if ( kernel->width == kernel->height )
3353 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003354 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003355 i,j,x,y;
3356 register MagickRealType
3357 *k,t;
3358 k=kernel->values;
3359 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3360 for( j=0, y=kernel->height-1; j<y; j++, y--)
3361 { t = k[i+j*kernel->width];
3362 k[i+j*kernel->width] = k[j+x*kernel->width];
3363 k[j+x*kernel->width] = k[x+y*kernel->width];
3364 k[x+y*kernel->width] = k[y+i*kernel->width];
3365 k[y+i*kernel->width] = t;
3366 }
3367 }
3368 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003369 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003370 x = (ssize_t) (kernel->x*2-kernel->width+1);
3371 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003372 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3373 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003374 }
anthony43c49252010-05-18 10:59:50 +00003375 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3376 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003377 }
3378 else
3379 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3380 }
anthony83ba99b2010-01-24 08:48:15 +00003381 if ( 135.0 < angle && angle <= 225.0 )
3382 {
anthony43c49252010-05-18 10:59:50 +00003383 /* For a 180 degree rotation - also know as a reflection
3384 * This is actually a very very common operation!
3385 * Basically all that is needed is a reversal of the kernel data!
3386 * And a reflection of the origon
3387 */
cristybb503372010-05-27 20:51:26 +00003388 size_t
anthony83ba99b2010-01-24 08:48:15 +00003389 i,j;
3390 register double
3391 *k,t;
3392
3393 k=kernel->values;
3394 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3395 t=k[i], k[i]=k[j], k[j]=t;
3396
cristybb503372010-05-27 20:51:26 +00003397 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3398 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003399 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3400 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003401 }
anthony3c10fc82010-05-13 02:40:51 +00003402 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003403 * In the future some form of non-orthogonal angled rotates could be
3404 * performed here, posibily with a linear kernel restriction.
3405 */
3406
3407#if 0
anthony3c10fc82010-05-13 02:40:51 +00003408 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003409 */
cristybb503372010-05-27 20:51:26 +00003410 size_t
anthony83ba99b2010-01-24 08:48:15 +00003411 y;
cristybb503372010-05-27 20:51:26 +00003412 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003413 x,r;
3414 register double
3415 *k,t;
3416
3417 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3418 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3419 t=k[x], k[x]=k[r], k[r]=t;
3420
cristyc99304f2010-02-01 15:26:27 +00003421 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003422 angle = fmod(angle+180.0, 360.0);
3423 }
3424#endif
3425 return;
3426}
3427
3428/*
3429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3430% %
3431% %
3432% %
anthony46a369d2010-05-19 02:41:48 +00003433% S c a l e G e o m e t r y K e r n e l I n f o %
3434% %
3435% %
3436% %
3437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3438%
3439% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3440% provided as a "-set option:convolve:scale {geometry}" user setting,
3441% and modifies the kernel according to the parsed arguments of that setting.
3442%
3443% The first argument (and any normalization flags) are passed to
3444% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3445% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3446% into the scaled/normalized kernel.
3447%
3448% The format of the ScaleKernelInfo method is:
3449%
3450% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3451% const MagickStatusType normalize_flags )
3452%
3453% A description of each parameter follows:
3454%
3455% o kernel: the Morphology/Convolution kernel to modify
3456%
3457% o geometry:
3458% The geometry string to parse, typically from the user provided
3459% "-set option:convolve:scale {geometry}" setting.
3460%
3461*/
3462MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3463 const char *geometry)
3464{
3465 GeometryFlags
3466 flags;
3467 GeometryInfo
3468 args;
3469
3470 SetGeometryInfo(&args);
3471 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3472
3473#if 0
3474 /* For Debugging Geometry Input */
3475 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3476 flags, args.rho, args.sigma, args.xi, args.psi );
3477#endif
3478
3479 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3480 args.rho *= 0.01, args.sigma *= 0.01;
3481
3482 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3483 args.rho = 1.0;
3484 if ( (flags & SigmaValue) == 0 )
3485 args.sigma = 0.0;
3486
3487 /* Scale/Normalize the input kernel */
3488 ScaleKernelInfo(kernel, args.rho, flags);
3489
3490 /* Add Unity Kernel, for blending with original */
3491 if ( (flags & SigmaValue) != 0 )
3492 UnityAddKernelInfo(kernel, args.sigma);
3493
3494 return;
3495}
3496/*
3497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3498% %
3499% %
3500% %
cristy6771f1e2010-03-05 19:43:39 +00003501% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003502% %
3503% %
3504% %
3505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3506%
anthony1b2bc0a2010-05-12 05:25:22 +00003507% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3508% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003509%
anthony999bb2c2010-02-18 12:38:01 +00003510% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003511% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003512%
anthony46a369d2010-05-19 02:41:48 +00003513% If either of the two 'normalize_flags' are given the kernel will first be
3514% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003515%
3516% Kernel normalization ('normalize_flags' given) is designed to ensure that
3517% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003518% morphology methods will fall into -1.0 to +1.0 range. Note that for
3519% non-HDRI versions of IM this may cause images to have any negative results
3520% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003521%
3522% More specifically. Kernels which only contain positive values (such as a
3523% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003524% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003525%
3526% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3527% the kernel will be scaled by the absolute of the sum of kernel values, so
3528% that it will generally fall within the +/- 1.0 range.
3529%
3530% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3531% will be scaled by just the sum of the postive values, so that its output
3532% range will again fall into the +/- 1.0 range.
3533%
3534% For special kernels designed for locating shapes using 'Correlate', (often
3535% only containing +1 and -1 values, representing foreground/brackground
3536% matching) a special normalization method is provided to scale the positive
3537% values seperatally to those of the negative values, so the kernel will be
3538% forced to become a zero-sum kernel better suited to such searches.
3539%
anthony1b2bc0a2010-05-12 05:25:22 +00003540% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003541% attributes within the kernel structure have been correctly set during the
3542% kernels creation.
3543%
3544% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003545% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3546% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003547%
anthony4fd27e22010-02-07 08:17:18 +00003548% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003549%
anthony999bb2c2010-02-18 12:38:01 +00003550% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3551% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003552%
3553% A description of each parameter follows:
3554%
3555% o kernel: the Morphology/Convolution kernel
3556%
anthony999bb2c2010-02-18 12:38:01 +00003557% o scaling_factor:
3558% multiply all values (after normalization) by this factor if not
3559% zero. If the kernel is normalized regardless of any flags.
3560%
3561% o normalize_flags:
3562% GeometryFlags defining normalization method to use.
3563% specifically: NormalizeValue, CorrelateNormalizeValue,
3564% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003565%
3566*/
cristy6771f1e2010-03-05 19:43:39 +00003567MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3568 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003569{
cristybb503372010-05-27 20:51:26 +00003570 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003571 i;
3572
anthony999bb2c2010-02-18 12:38:01 +00003573 register double
3574 pos_scale,
3575 neg_scale;
3576
anthony46a369d2010-05-19 02:41:48 +00003577 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003578 if ( kernel->next != (KernelInfo *) NULL)
3579 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3580
anthony46a369d2010-05-19 02:41:48 +00003581 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003582 pos_scale = 1.0;
3583 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003584 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003585 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003586 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003587 else
anthonyf4e00312010-05-20 12:06:35 +00003588 /* zero-summing kernel */
3589 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003590 }
anthony46a369d2010-05-19 02:41:48 +00003591 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003592 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3593 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3594 ? kernel->positive_range : 1.0;
3595 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3596 ? -kernel->negative_range : 1.0;
3597 }
3598 else
3599 neg_scale = pos_scale;
3600
3601 /* finialize scaling_factor for positive and negative components */
3602 pos_scale = scaling_factor/pos_scale;
3603 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003604
cristybb503372010-05-27 20:51:26 +00003605 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003606 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003607 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003608
anthony999bb2c2010-02-18 12:38:01 +00003609 /* convolution output range */
3610 kernel->positive_range *= pos_scale;
3611 kernel->negative_range *= neg_scale;
3612 /* maximum and minimum values in kernel */
3613 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3614 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3615
anthony46a369d2010-05-19 02:41:48 +00003616 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003617 if ( scaling_factor < MagickEpsilon ) {
3618 double t;
3619 t = kernel->positive_range;
3620 kernel->positive_range = kernel->negative_range;
3621 kernel->negative_range = t;
3622 t = kernel->maximum;
3623 kernel->maximum = kernel->minimum;
3624 kernel->minimum = 1;
3625 }
anthonycc6c8362010-01-25 04:14:01 +00003626
3627 return;
3628}
3629
3630/*
3631%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3632% %
3633% %
3634% %
anthony46a369d2010-05-19 02:41:48 +00003635% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003636% %
3637% %
3638% %
3639%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3640%
anthony4fd27e22010-02-07 08:17:18 +00003641% ShowKernelInfo() outputs the details of the given kernel defination to
3642% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003643%
3644% The format of the ShowKernel method is:
3645%
anthony4fd27e22010-02-07 08:17:18 +00003646% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003647%
3648% A description of each parameter follows:
3649%
3650% o kernel: the Morphology/Convolution kernel
3651%
anthony83ba99b2010-01-24 08:48:15 +00003652*/
anthony4fd27e22010-02-07 08:17:18 +00003653MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003654{
anthony7a01dcf2010-05-11 12:25:52 +00003655 KernelInfo
3656 *k;
anthony83ba99b2010-01-24 08:48:15 +00003657
cristybb503372010-05-27 20:51:26 +00003658 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003659 c, i, u, v;
3660
3661 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3662
anthony46a369d2010-05-19 02:41:48 +00003663 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003664 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003665 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003666 fprintf(stderr, " \"%s",
3667 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3668 if ( fabs(k->angle) > MagickEpsilon )
3669 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003670 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3671 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003672 fprintf(stderr,
3673 " with values from %.*lg to %.*lg\n",
3674 GetMagickPrecision(), k->minimum,
3675 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003676 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003677 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003678 GetMagickPrecision(), k->positive_range);
3679 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3680 fprintf(stderr, " (Zero-Summing)\n");
3681 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3682 fprintf(stderr, " (Normalized)\n");
3683 else
3684 fprintf(stderr, " (Sum %.*lg)\n",
3685 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003686 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003687 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003688 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003689 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003690 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003691 else
anthonyf4e00312010-05-20 12:06:35 +00003692 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003693 GetMagickPrecision(), k->values[i]);
3694 fprintf(stderr,"\n");
3695 }
anthony83ba99b2010-01-24 08:48:15 +00003696 }
3697}
anthonycc6c8362010-01-25 04:14:01 +00003698
3699/*
3700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3701% %
3702% %
3703% %
anthony43c49252010-05-18 10:59:50 +00003704% U n i t y A d d K e r n a l I n f o %
3705% %
3706% %
3707% %
3708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3709%
3710% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3711% to the given pre-scaled and normalized Kernel. This in effect adds that
3712% amount of the original image into the resulting convolution kernel. This
3713% value is usually provided by the user as a percentage value in the
3714% 'convolve:scale' setting.
3715%
anthony501c2f92010-06-02 10:55:14 +00003716% The resulting effect is to convert the defined kernels into blended
3717% soft-blurs, unsharp kernels or into sharpening kernels.
anthony43c49252010-05-18 10:59:50 +00003718%
anthony46a369d2010-05-19 02:41:48 +00003719% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003720%
3721% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3722%
3723% A description of each parameter follows:
3724%
3725% o kernel: the Morphology/Convolution kernel
3726%
3727% o scale:
3728% scaling factor for the unity kernel to be added to
3729% the given kernel.
3730%
anthony43c49252010-05-18 10:59:50 +00003731*/
3732MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3733 const double scale)
3734{
anthony46a369d2010-05-19 02:41:48 +00003735 /* do the other kernels in a multi-kernel list first */
3736 if ( kernel->next != (KernelInfo *) NULL)
3737 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003738
anthony46a369d2010-05-19 02:41:48 +00003739 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003740 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003741 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003742
3743 return;
3744}
3745
3746/*
3747%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3748% %
3749% %
3750% %
3751% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003752% %
3753% %
3754% %
3755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3756%
3757% ZeroKernelNans() replaces any special 'nan' value that may be present in
3758% the kernel with a zero value. This is typically done when the kernel will
3759% be used in special hardware (GPU) convolution processors, to simply
3760% matters.
3761%
3762% The format of the ZeroKernelNans method is:
3763%
anthony46a369d2010-05-19 02:41:48 +00003764% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003765%
3766% A description of each parameter follows:
3767%
3768% o kernel: the Morphology/Convolution kernel
3769%
anthonycc6c8362010-01-25 04:14:01 +00003770*/
anthonyc4c86e02010-01-27 09:30:32 +00003771MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003772{
cristybb503372010-05-27 20:51:26 +00003773 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003774 i;
3775
anthony46a369d2010-05-19 02:41:48 +00003776 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003777 if ( kernel->next != (KernelInfo *) NULL)
3778 ZeroKernelNans(kernel->next);
3779
anthony43c49252010-05-18 10:59:50 +00003780 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003781 if ( IsNan(kernel->values[i]) )
3782 kernel->values[i] = 0.0;
3783
3784 return;
3785}