blob: 5d54450f22de2454e4e685c06f1b2474943b92cb [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);
anthonyef33d9f2010-06-02 12:27:01 +00001297 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001298 kernel->values[3] = +MagickSQ2;
1299 kernel->values[5] = -MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001300 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001301 break;
anthonye2a60ce2010-05-19 12:30:40 +00001302 case 1:
anthony501c2f92010-06-02 10:55:14 +00001303 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonye2a60ce2010-05-19 12:30:40 +00001304 if (kernel == (KernelInfo *) NULL)
1305 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001306 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001307 kernel->values[3] = +MagickSQ2;
1308 kernel->values[5] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001309 CalcKernelMetaData(kernel); /* recalculate meta-data */
1310 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1311 break;
1312 case 2:
anthony501c2f92010-06-02 10:55:14 +00001313 kernel=ParseKernelArray("3: 1,2,1 0,0,0 1,2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001314 if (kernel == (KernelInfo *) NULL)
1315 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001316 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001317 kernel->values[1] = +MagickSQ2;
1318 kernel->values[7] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001319 CalcKernelMetaData(kernel);
1320 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1321 break;
1322 case 3:
anthony501c2f92010-06-02 10:55:14 +00001323 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001324 if (kernel == (KernelInfo *) NULL)
1325 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001326 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001327 kernel->values[0] = +MagickSQ2;
1328 kernel->values[8] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001329 CalcKernelMetaData(kernel);
1330 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1331 break;
1332 case 4:
anthony1d5e6702010-05-31 10:19:12 +00001333 kernel=ParseKernelArray("3: 0,1,-2 -1,0,1 2,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001334 if (kernel == (KernelInfo *) NULL)
1335 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001336 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001337 kernel->values[2] = -MagickSQ2;
1338 kernel->values[6] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001339 CalcKernelMetaData(kernel);
1340 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1341 break;
1342 case 5:
anthony501c2f92010-06-02 10:55:14 +00001343 kernel=ParseKernelArray("3: 0,-1,0 1,0,1 0,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001344 if (kernel == (KernelInfo *) NULL)
1345 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001346 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001347 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1348 break;
1349 case 6:
anthony1d5e6702010-05-31 10:19:12 +00001350 kernel=ParseKernelArray("3: 1,0,-1 0,0,0 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001351 if (kernel == (KernelInfo *) NULL)
1352 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001353 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001354 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1355 break;
1356 case 7:
anthony501c2f92010-06-02 10:55:14 +00001357 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 -1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001358 if (kernel == (KernelInfo *) NULL)
1359 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001360 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001361 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1362 break;
1363 case 8:
anthony501c2f92010-06-02 10:55:14 +00001364 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001365 if (kernel == (KernelInfo *) NULL)
1366 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001367 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001368 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1369 break;
1370 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001371 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001372 if (kernel == (KernelInfo *) NULL)
1373 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001374 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001375 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1376 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001377 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001378 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1379 if (kernel == (KernelInfo *) NULL)
1380 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001381 break;
anthonye2a60ce2010-05-19 12:30:40 +00001382 }
anthonyc3cd15b2010-05-27 06:05:40 +00001383 if ( fabs(args->sigma) > MagickEpsilon )
1384 /* Rotate by correctly supplied 'angle' */
1385 RotateKernelInfo(kernel, args->sigma);
1386 else if ( args->rho > 30.0 || args->rho < -30.0 )
1387 /* Rotate by out of bounds 'type' */
1388 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001389 break;
1390 }
1391
anthonyc1061722010-05-14 06:23:49 +00001392 /* Boolean Kernels */
1393 case DiamondKernel:
1394 {
1395 if (args->rho < 1.0)
1396 kernel->width = kernel->height = 3; /* default radius = 1 */
1397 else
cristybb503372010-05-27 20:51:26 +00001398 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1399 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001400
1401 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1402 kernel->height*sizeof(double));
1403 if (kernel->values == (double *) NULL)
1404 return(DestroyKernelInfo(kernel));
1405
1406 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001407 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1408 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony1d5e6702010-05-31 10:19:12 +00001409 if ( (labs((long) u)+labs((long) v)) <= (long) kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001410 kernel->positive_range += kernel->values[i] = args->sigma;
1411 else
1412 kernel->values[i] = nan;
1413 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1414 break;
1415 }
1416 case SquareKernel:
1417 case RectangleKernel:
1418 { double
1419 scale;
anthony602ab9b2010-01-05 08:06:50 +00001420 if ( type == SquareKernel )
1421 {
1422 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001423 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001424 else
cristybb503372010-05-27 20:51:26 +00001425 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1426 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001427 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001428 }
1429 else {
cristy2be15382010-01-21 02:38:03 +00001430 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001431 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001432 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001433 kernel->width = (size_t)args->rho;
1434 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001435 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1436 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001437 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001438 kernel->x = (ssize_t) args->xi;
1439 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001440 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001441 }
1442 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1443 kernel->height*sizeof(double));
1444 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001445 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001446
anthony3dd0f622010-05-13 12:57:32 +00001447 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001448 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001449 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001450 kernel->values[i] = scale;
1451 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1452 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001453 break;
anthony602ab9b2010-01-05 08:06:50 +00001454 }
anthony602ab9b2010-01-05 08:06:50 +00001455 case DiskKernel:
1456 {
anthonye4d89962010-05-29 10:53:11 +00001457 ssize_t
1458 limit = (ssize_t)(args->rho*args->rho);
1459
1460 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001461 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001462 else
anthonye4d89962010-05-29 10:53:11 +00001463 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001464 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001465
1466 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1467 kernel->height*sizeof(double));
1468 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001469 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001470
anthony3dd0f622010-05-13 12:57:32 +00001471 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001472 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1473 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001474 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001475 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001476 else
1477 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001478 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001479 break;
1480 }
1481 case PlusKernel:
1482 {
1483 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001484 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001485 else
cristybb503372010-05-27 20:51:26 +00001486 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1487 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001488
1489 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1490 kernel->height*sizeof(double));
1491 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001492 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001493
cristycee97112010-05-28 00:44:52 +00001494 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001495 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1496 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001497 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1498 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1499 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001500 break;
1501 }
anthony3dd0f622010-05-13 12:57:32 +00001502 case CrossKernel:
1503 {
1504 if (args->rho < 1.0)
1505 kernel->width = kernel->height = 5; /* default radius 2 */
1506 else
cristybb503372010-05-27 20:51:26 +00001507 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1508 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001509
1510 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1511 kernel->height*sizeof(double));
1512 if (kernel->values == (double *) NULL)
1513 return(DestroyKernelInfo(kernel));
1514
cristycee97112010-05-28 00:44:52 +00001515 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001516 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1517 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001518 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1519 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1520 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1521 break;
1522 }
1523 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001524 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001525 case PeaksKernel:
1526 {
cristybb503372010-05-27 20:51:26 +00001527 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001528 limit1,
anthonyc1061722010-05-14 06:23:49 +00001529 limit2,
1530 scale;
anthony3dd0f622010-05-13 12:57:32 +00001531
1532 if (args->rho < args->sigma)
1533 {
cristybb503372010-05-27 20:51:26 +00001534 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001535 limit1 = (ssize_t)(args->rho*args->rho);
1536 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001537 }
1538 else
1539 {
cristybb503372010-05-27 20:51:26 +00001540 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001541 limit1 = (ssize_t)(args->sigma*args->sigma);
1542 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001543 }
anthonyc1061722010-05-14 06:23:49 +00001544 if ( limit2 <= 0 )
1545 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1546
anthony3dd0f622010-05-13 12:57:32 +00001547 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001548 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001549 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1550 kernel->height*sizeof(double));
1551 if (kernel->values == (double *) NULL)
1552 return(DestroyKernelInfo(kernel));
1553
anthonyc1061722010-05-14 06:23:49 +00001554 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001555 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1556 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1557 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1558 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001559 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001560 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001561 else
1562 kernel->values[i] = nan;
1563 }
cristye96405a2010-05-19 02:24:31 +00001564 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001565 if ( type == PeaksKernel ) {
1566 /* set the central point in the middle */
1567 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1568 kernel->positive_range = 1.0;
1569 kernel->maximum = 1.0;
1570 }
anthony3dd0f622010-05-13 12:57:32 +00001571 break;
1572 }
anthony43c49252010-05-18 10:59:50 +00001573 case EdgesKernel:
1574 {
1575 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1576 if (kernel == (KernelInfo *) NULL)
1577 return(kernel);
1578 kernel->type = type;
1579 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1580 break;
1581 }
anthony3dd0f622010-05-13 12:57:32 +00001582 case CornersKernel:
1583 {
anthony4f1dcb72010-05-14 08:43:10 +00001584 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001585 if (kernel == (KernelInfo *) NULL)
1586 return(kernel);
1587 kernel->type = type;
1588 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1589 break;
1590 }
anthony47f5d062010-05-23 07:47:50 +00001591 case RidgesKernel:
1592 {
anthony24a19842010-05-27 12:18:34 +00001593 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001594 if (kernel == (KernelInfo *) NULL)
1595 return(kernel);
1596 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001597 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001598 break;
1599 }
anthony1d45eb92010-05-25 11:13:23 +00001600 case Ridges2Kernel:
1601 {
1602 KernelInfo
1603 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001604 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001605 if (kernel == (KernelInfo *) NULL)
1606 return(kernel);
1607 kernel->type = type;
1608 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001609#if 0
1610 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001611 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1612 if (new_kernel == (KernelInfo *) NULL)
1613 return(DestroyKernelInfo(kernel));
1614 new_kernel->type = type;
1615 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1616 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001617#endif
1618 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1619 /* Unfortunatally we can not yet rotate a non-square kernel */
1620 /* But then we can't flip a non-symetrical kernel either */
1621 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1622 if (new_kernel == (KernelInfo *) NULL)
1623 return(DestroyKernelInfo(kernel));
1624 new_kernel->type = type;
1625 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001626 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001627 if (new_kernel == (KernelInfo *) NULL)
1628 return(DestroyKernelInfo(kernel));
1629 new_kernel->type = type;
1630 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001631 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001632 if (new_kernel == (KernelInfo *) NULL)
1633 return(DestroyKernelInfo(kernel));
1634 new_kernel->type = type;
1635 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001636 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001637 if (new_kernel == (KernelInfo *) NULL)
1638 return(DestroyKernelInfo(kernel));
1639 new_kernel->type = type;
1640 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001641 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001642 if (new_kernel == (KernelInfo *) NULL)
1643 return(DestroyKernelInfo(kernel));
1644 new_kernel->type = type;
1645 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001646 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001647 if (new_kernel == (KernelInfo *) NULL)
1648 return(DestroyKernelInfo(kernel));
1649 new_kernel->type = type;
1650 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001651 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001652 if (new_kernel == (KernelInfo *) NULL)
1653 return(DestroyKernelInfo(kernel));
1654 new_kernel->type = type;
1655 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001656 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001657 if (new_kernel == (KernelInfo *) NULL)
1658 return(DestroyKernelInfo(kernel));
1659 new_kernel->type = type;
1660 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001661 break;
1662 }
anthony3dd0f622010-05-13 12:57:32 +00001663 case LineEndsKernel:
1664 {
anthony43c49252010-05-18 10:59:50 +00001665 KernelInfo
1666 *new_kernel;
1667 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001668 if (kernel == (KernelInfo *) NULL)
1669 return(kernel);
1670 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001671 ExpandKernelInfo(kernel, 90.0);
1672 /* append second set of 4 kernels */
1673 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1674 if (new_kernel == (KernelInfo *) NULL)
1675 return(DestroyKernelInfo(kernel));
1676 new_kernel->type = type;
1677 ExpandKernelInfo(new_kernel, 90.0);
1678 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001679 break;
1680 }
1681 case LineJunctionsKernel:
1682 {
1683 KernelInfo
1684 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001685 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001686 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001687 if (kernel == (KernelInfo *) NULL)
1688 return(kernel);
1689 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001690 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001691 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001692 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001693 if (new_kernel == (KernelInfo *) NULL)
1694 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001695 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001696 ExpandKernelInfo(new_kernel, 90.0);
1697 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001698 break;
1699 }
anthony3dd0f622010-05-13 12:57:32 +00001700 case ConvexHullKernel:
1701 {
anthony3928ec62010-05-27 14:03:29 +00001702 KernelInfo
1703 *new_kernel;
1704 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001705 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001706 if (kernel == (KernelInfo *) NULL)
1707 return(kernel);
1708 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001709 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001710 /* append the mirror versions too */
1711 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1712 if (new_kernel == (KernelInfo *) NULL)
1713 return(DestroyKernelInfo(kernel));
1714 new_kernel->type = type;
1715 ExpandKernelInfo(new_kernel, 45.0);
1716 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001717 break;
1718 }
anthony47f5d062010-05-23 07:47:50 +00001719 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001720 { /* what is the best form for skeletonization by thinning? */
anthonye4d89962010-05-29 10:53:11 +00001721#if 1
1722 /* Full Corner rotated to form edges too */
anthony43c49252010-05-18 10:59:50 +00001723 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001724 if (kernel == (KernelInfo *) NULL)
1725 return(kernel);
1726 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001727 ExpandKernelInfo(kernel, 45);
anthonye4d89962010-05-29 10:53:11 +00001728#endif
1729#if 0
1730 /* As last but thin the edges before looking for corners */
1731 KernelInfo
1732 *new_kernel;
1733 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1734 if (kernel == (KernelInfo *) NULL)
1735 return(kernel);
1736 kernel->type = type;
1737 ExpandKernelInfo(kernel, 90.0);
1738 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
1739 if (new_kernel == (KernelInfo *) NULL)
1740 return(DestroyKernelInfo(kernel));
1741 new_kernel->type = type;
1742 ExpandKernelInfo(new_kernel, 90.0);
1743 LastKernelInfo(kernel)->next = new_kernel;
1744#endif
1745#if 0
1746 kernel=AcquireKernelInfo("Edges;Corners");
1747#endif
1748#if 0
1749 kernel=AcquireKernelInfo("Corners;Edges");
anthony47f5d062010-05-23 07:47:50 +00001750#endif
anthony3dd0f622010-05-13 12:57:32 +00001751 break;
1752 }
anthonya648a302010-05-27 02:14:36 +00001753 case MatKernel: /* experimental - MAT from a Distance Gradient */
1754 {
1755 KernelInfo
1756 *new_kernel;
1757 /* Ridge Kernel but without the diagonal */
1758 kernel=ParseKernelArray("3x1: 0,1,0");
1759 if (kernel == (KernelInfo *) NULL)
1760 return(kernel);
1761 kernel->type = RidgesKernel;
1762 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1763 /* Plus the 2 pixel ridges kernel - no diagonal */
1764 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1765 if (new_kernel == (KernelInfo *) NULL)
1766 return(kernel);
1767 LastKernelInfo(kernel)->next = new_kernel;
1768 break;
1769 }
anthony602ab9b2010-01-05 08:06:50 +00001770 /* Distance Measuring Kernels */
1771 case ChebyshevKernel:
1772 {
anthony602ab9b2010-01-05 08:06:50 +00001773 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001774 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001775 else
cristybb503372010-05-27 20:51:26 +00001776 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1777 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001778
1779 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1780 kernel->height*sizeof(double));
1781 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001782 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001783
cristybb503372010-05-27 20:51:26 +00001784 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1785 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001786 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001787 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001788 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001789 break;
1790 }
1791 case ManhattenKernel:
1792 {
anthony602ab9b2010-01-05 08:06:50 +00001793 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001794 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001795 else
cristybb503372010-05-27 20:51:26 +00001796 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1797 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001798
1799 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1800 kernel->height*sizeof(double));
1801 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001802 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001803
cristybb503372010-05-27 20:51:26 +00001804 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1805 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001806 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001807 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001808 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001809 break;
1810 }
1811 case EuclideanKernel:
1812 {
anthony602ab9b2010-01-05 08:06:50 +00001813 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001814 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001815 else
cristybb503372010-05-27 20:51:26 +00001816 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1817 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001818
1819 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1820 kernel->height*sizeof(double));
1821 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001822 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001823
cristybb503372010-05-27 20:51:26 +00001824 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1825 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001826 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001827 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001828 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001829 break;
1830 }
anthony46a369d2010-05-19 02:41:48 +00001831 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001832 default:
anthonyc1061722010-05-14 06:23:49 +00001833 {
anthony46a369d2010-05-19 02:41:48 +00001834 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1835 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001836 if (kernel == (KernelInfo *) NULL)
1837 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001838 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001839 break;
1840 }
anthony602ab9b2010-01-05 08:06:50 +00001841 break;
1842 }
1843
1844 return(kernel);
1845}
anthonyc94cdb02010-01-06 08:15:29 +00001846
anthony602ab9b2010-01-05 08:06:50 +00001847/*
1848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1849% %
1850% %
1851% %
cristy6771f1e2010-03-05 19:43:39 +00001852% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001853% %
1854% %
1855% %
1856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1857%
anthony1b2bc0a2010-05-12 05:25:22 +00001858% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1859% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001860% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001861%
cristye6365592010-04-02 17:31:23 +00001862% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001863%
anthony930be612010-02-08 04:26:15 +00001864% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001865%
1866% A description of each parameter follows:
1867%
1868% o kernel: the Morphology/Convolution kernel to be cloned
1869%
1870*/
cristyef656912010-03-05 19:54:59 +00001871MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001872{
cristybb503372010-05-27 20:51:26 +00001873 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001874 i;
1875
cristy19eb6412010-04-23 14:42:29 +00001876 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001877 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001878
1879 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001880 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1881 if (new_kernel == (KernelInfo *) NULL)
1882 return(new_kernel);
1883 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001884
1885 /* replace the values with a copy of the values */
1886 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001887 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001888 if (new_kernel->values == (double *) NULL)
1889 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001890 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001891 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001892
1893 /* Also clone the next kernel in the kernel list */
1894 if ( kernel->next != (KernelInfo *) NULL ) {
1895 new_kernel->next = CloneKernelInfo(kernel->next);
1896 if ( new_kernel->next == (KernelInfo *) NULL )
1897 return(DestroyKernelInfo(new_kernel));
1898 }
1899
anthony7a01dcf2010-05-11 12:25:52 +00001900 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001901}
1902
1903/*
1904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1905% %
1906% %
1907% %
anthony83ba99b2010-01-24 08:48:15 +00001908% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001909% %
1910% %
1911% %
1912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1913%
anthony83ba99b2010-01-24 08:48:15 +00001914% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1915% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001916%
anthony83ba99b2010-01-24 08:48:15 +00001917% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001918%
anthony83ba99b2010-01-24 08:48:15 +00001919% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001920%
1921% A description of each parameter follows:
1922%
1923% o kernel: the Morphology/Convolution kernel to be destroyed
1924%
1925*/
1926
anthony83ba99b2010-01-24 08:48:15 +00001927MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001928{
cristy2be15382010-01-21 02:38:03 +00001929 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001930
anthony7a01dcf2010-05-11 12:25:52 +00001931 if ( kernel->next != (KernelInfo *) NULL )
1932 kernel->next = DestroyKernelInfo(kernel->next);
1933
1934 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1935 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001936 return(kernel);
1937}
anthonyc94cdb02010-01-06 08:15:29 +00001938
1939/*
1940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1941% %
1942% %
1943% %
anthony3c10fc82010-05-13 02:40:51 +00001944% E x p a n d K e r n e l I n f o %
1945% %
1946% %
1947% %
1948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1949%
1950% ExpandKernelInfo() takes a single kernel, and expands it into a list
1951% of kernels each incrementally rotated the angle given.
1952%
1953% WARNING: 45 degree rotations only works for 3x3 kernels.
1954% While 90 degree roatations only works for linear and square kernels
1955%
1956% The format of the RotateKernelInfo method is:
1957%
1958% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1959%
1960% A description of each parameter follows:
1961%
1962% o kernel: the Morphology/Convolution kernel
1963%
1964% o angle: angle to rotate in degrees
1965%
1966% This function is only internel to this module, as it is not finalized,
1967% especially with regard to non-orthogonal angles, and rotation of larger
1968% 2D kernels.
1969*/
anthony47f5d062010-05-23 07:47:50 +00001970
1971/* Internal Routine - Return true if two kernels are the same */
1972static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1973 const KernelInfo *kernel2)
1974{
cristybb503372010-05-27 20:51:26 +00001975 register size_t
anthony47f5d062010-05-23 07:47:50 +00001976 i;
anthony1d45eb92010-05-25 11:13:23 +00001977
1978 /* check size and origin location */
1979 if ( kernel1->width != kernel2->width
1980 || kernel1->height != kernel2->height
1981 || kernel1->x != kernel2->x
1982 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001983 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001984
1985 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001986 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001987 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001988 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1989 return MagickFalse;
1990 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
1991 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001992 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00001993 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
1994 return MagickFalse;
1995 }
anthony1d45eb92010-05-25 11:13:23 +00001996
anthony47f5d062010-05-23 07:47:50 +00001997 return MagickTrue;
1998}
1999
2000static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002001{
2002 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002003 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002004 *last;
cristya9a61ad2010-05-13 12:47:41 +00002005
anthony3c10fc82010-05-13 02:40:51 +00002006 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002007 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002008 clone = CloneKernelInfo(last);
2009 RotateKernelInfo(clone, angle);
2010 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002011 break;
cristy84d9b552010-05-24 18:23:54 +00002012 last->next = clone;
2013 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002014 }
cristy84d9b552010-05-24 18:23:54 +00002015 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002016 return;
anthony3c10fc82010-05-13 02:40:51 +00002017}
2018
2019/*
2020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021% %
2022% %
2023% %
anthony46a369d2010-05-19 02:41:48 +00002024+ C a l c M e t a K e r n a l I n f o %
2025% %
2026% %
2027% %
2028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029%
2030% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2031% using the kernel values. This should only ne used if it is not posible to
2032% calculate that meta-data in some easier way.
2033%
2034% It is important that the meta-data is correct before ScaleKernelInfo() is
2035% used to perform kernel normalization.
2036%
2037% The format of the CalcKernelMetaData method is:
2038%
2039% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2040%
2041% A description of each parameter follows:
2042%
2043% o kernel: the Morphology/Convolution kernel to modify
2044%
2045% WARNING: Minimum and Maximum values are assumed to include zero, even if
2046% zero is not part of the kernel (as in Gaussian Derived kernels). This
2047% however is not true for flat-shaped morphological kernels.
2048%
2049% WARNING: Only the specific kernel pointed to is modified, not a list of
2050% multiple kernels.
2051%
2052% This is an internal function and not expected to be useful outside this
2053% module. This could change however.
2054*/
2055static void CalcKernelMetaData(KernelInfo *kernel)
2056{
cristybb503372010-05-27 20:51:26 +00002057 register size_t
anthony46a369d2010-05-19 02:41:48 +00002058 i;
2059
2060 kernel->minimum = kernel->maximum = 0.0;
2061 kernel->negative_range = kernel->positive_range = 0.0;
2062 for (i=0; i < (kernel->width*kernel->height); i++)
2063 {
2064 if ( fabs(kernel->values[i]) < MagickEpsilon )
2065 kernel->values[i] = 0.0;
2066 ( kernel->values[i] < 0)
2067 ? ( kernel->negative_range += kernel->values[i] )
2068 : ( kernel->positive_range += kernel->values[i] );
2069 Minimize(kernel->minimum, kernel->values[i]);
2070 Maximize(kernel->maximum, kernel->values[i]);
2071 }
2072
2073 return;
2074}
2075
2076/*
2077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078% %
2079% %
2080% %
anthony9eb4f742010-05-18 02:45:54 +00002081% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002082% %
2083% %
2084% %
2085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2086%
anthony9eb4f742010-05-18 02:45:54 +00002087% MorphologyApply() applies a morphological method, multiple times using
2088% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002089%
anthony9eb4f742010-05-18 02:45:54 +00002090% It is basically equivelent to as MorphologyImageChannel() (see below) but
2091% without user controls, that that function extracts and applies to kernels
2092% and morphology methods.
2093%
2094% More specifically kernels are not normalized/scaled/blended by the
2095% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2096% (-bias setting or image->bias) is passed directly to this function,
2097% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002098%
anthony47f5d062010-05-23 07:47:50 +00002099% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002100%
anthony9eb4f742010-05-18 02:45:54 +00002101% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002102% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002103% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002104% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002105%
2106% A description of each parameter follows:
2107%
2108% o image: the image.
2109%
2110% o method: the morphology method to be applied.
2111%
2112% o iterations: apply the operation this many times (or no change).
2113% A value of -1 means loop until no change found.
2114% How this is applied may depend on the morphology method.
2115% Typically this is a value of 1.
2116%
2117% o channel: the channel type.
2118%
2119% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002120% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002121%
anthony47f5d062010-05-23 07:47:50 +00002122% o compose: How to handle or merge multi-kernel results.
2123% If 'Undefined' use default of the Morphology method.
2124% If 'No' force image to be re-iterated by each kernel.
2125% Otherwise merge the results using the mathematical compose
2126% method given.
2127%
2128% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002129%
anthony602ab9b2010-01-05 08:06:50 +00002130% o exception: return any errors or warnings in this structure.
2131%
anthony602ab9b2010-01-05 08:06:50 +00002132*/
2133
anthony930be612010-02-08 04:26:15 +00002134
anthony9eb4f742010-05-18 02:45:54 +00002135/* Apply a Morphology Primative to an image using the given kernel.
2136** Two pre-created images must be provided, no image is created.
2137** Returning the number of pixels that changed.
2138*/
cristybb503372010-05-27 20:51:26 +00002139static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002140 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002141 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002142{
cristy2be15382010-01-21 02:38:03 +00002143#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002144
cristy5f959472010-05-27 22:19:46 +00002145 CacheView
2146 *p_view,
2147 *q_view;
2148
cristybb503372010-05-27 20:51:26 +00002149 ssize_t
anthony29188a82010-01-22 10:12:34 +00002150 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002151 changed;
2152
2153 MagickBooleanType
2154 status;
2155
cristy5f959472010-05-27 22:19:46 +00002156 MagickOffsetType
2157 progress;
anthony602ab9b2010-01-05 08:06:50 +00002158
anthonye4d89962010-05-29 10:53:11 +00002159 assert(image != (Image *) NULL);
2160 assert(image->signature == MagickSignature);
2161 assert(result_image != (Image *) NULL);
2162 assert(result_image->signature == MagickSignature);
2163 assert(kernel != (KernelInfo *) NULL);
2164 assert(kernel->signature == MagickSignature);
2165 assert(exception != (ExceptionInfo *) NULL);
2166 assert(exception->signature == MagickSignature);
2167
anthony602ab9b2010-01-05 08:06:50 +00002168 status=MagickTrue;
2169 changed=0;
2170 progress=0;
2171
anthony602ab9b2010-01-05 08:06:50 +00002172 p_view=AcquireCacheView(image);
2173 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002174
anthonycc6c8362010-01-25 04:14:01 +00002175 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002176 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002177 */
cristyc99304f2010-02-01 15:26:27 +00002178 offx = kernel->x;
2179 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002180 switch(method) {
anthony930be612010-02-08 04:26:15 +00002181 case ConvolveMorphology:
2182 case DilateMorphology:
2183 case DilateIntensityMorphology:
2184 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002185 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002186 offx = (ssize_t) kernel->width-offx-1;
2187 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002188 break;
anthony5ef8e942010-05-11 06:51:12 +00002189 case ErodeMorphology:
2190 case ErodeIntensityMorphology:
2191 case HitAndMissMorphology:
2192 case ThinningMorphology:
2193 case ThickenMorphology:
2194 /* kernel is user as is, without reflection */
2195 break;
anthony930be612010-02-08 04:26:15 +00002196 default:
anthony9eb4f742010-05-18 02:45:54 +00002197 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002198 break;
anthony29188a82010-01-22 10:12:34 +00002199 }
2200
anthony602ab9b2010-01-05 08:06:50 +00002201#if defined(MAGICKCORE_OPENMP_SUPPORT)
2202 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2203#endif
cristybb503372010-05-27 20:51:26 +00002204 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002205 {
2206 MagickBooleanType
2207 sync;
2208
2209 register const PixelPacket
2210 *restrict p;
2211
2212 register const IndexPacket
2213 *restrict p_indexes;
2214
2215 register PixelPacket
2216 *restrict q;
2217
2218 register IndexPacket
2219 *restrict q_indexes;
2220
cristybb503372010-05-27 20:51:26 +00002221 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002222 x;
2223
cristybb503372010-05-27 20:51:26 +00002224 size_t
anthony602ab9b2010-01-05 08:06:50 +00002225 r;
2226
2227 if (status == MagickFalse)
2228 continue;
anthony29188a82010-01-22 10:12:34 +00002229 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2230 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002231 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2232 exception);
2233 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2234 {
2235 status=MagickFalse;
2236 continue;
2237 }
2238 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2239 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002240 r = (image->columns+kernel->width)*offy+offx; /* constant */
2241
cristybb503372010-05-27 20:51:26 +00002242 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002243 {
cristybb503372010-05-27 20:51:26 +00002244 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002245 v;
2246
cristybb503372010-05-27 20:51:26 +00002247 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002248 u;
2249
2250 register const double
2251 *restrict k;
2252
2253 register const PixelPacket
2254 *restrict k_pixels;
2255
2256 register const IndexPacket
2257 *restrict k_indexes;
2258
2259 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002260 result,
2261 min,
2262 max;
anthony602ab9b2010-01-05 08:06:50 +00002263
anthony29188a82010-01-22 10:12:34 +00002264 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002265 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002266 */
anthony602ab9b2010-01-05 08:06:50 +00002267 *q = p[r];
2268 if (image->colorspace == CMYKColorspace)
2269 q_indexes[x] = p_indexes[r];
2270
anthony5ef8e942010-05-11 06:51:12 +00002271 /* Defaults */
2272 min.red =
2273 min.green =
2274 min.blue =
2275 min.opacity =
2276 min.index = (MagickRealType) QuantumRange;
2277 max.red =
2278 max.green =
2279 max.blue =
2280 max.opacity =
2281 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002282 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002283 result.red = (MagickRealType) p[r].red;
2284 result.green = (MagickRealType) p[r].green;
2285 result.blue = (MagickRealType) p[r].blue;
2286 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002287 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002288 if ( image->colorspace == CMYKColorspace)
2289 result.index = (MagickRealType) p_indexes[r];
2290
anthony602ab9b2010-01-05 08:06:50 +00002291 switch (method) {
2292 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002293 /* Set the user defined bias of the weighted average output */
2294 result.red =
2295 result.green =
2296 result.blue =
2297 result.opacity =
2298 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002299 break;
anthony4fd27e22010-02-07 08:17:18 +00002300 case DilateIntensityMorphology:
2301 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002302 /* use a boolean flag indicating when first match found */
2303 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002304 break;
anthony602ab9b2010-01-05 08:06:50 +00002305 default:
anthony602ab9b2010-01-05 08:06:50 +00002306 break;
2307 }
2308
2309 switch ( method ) {
2310 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002311 /* Weighted Average of pixels using reflected kernel
2312 **
2313 ** NOTE for correct working of this operation for asymetrical
2314 ** kernels, the kernel needs to be applied in its reflected form.
2315 ** That is its values needs to be reversed.
2316 **
2317 ** Correlation is actually the same as this but without reflecting
2318 ** the kernel, and thus 'lower-level' that Convolution. However
2319 ** as Convolution is the more common method used, and it does not
2320 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002321 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002322 **
2323 ** Correlation will have its kernel reflected before calling
2324 ** this function to do a Convolve.
2325 **
2326 ** For more details of Correlation vs Convolution see
2327 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2328 */
anthony5ef8e942010-05-11 06:51:12 +00002329 if (((channel & SyncChannels) != 0 ) &&
2330 (image->matte == MagickTrue))
2331 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2332 ** Weight the color channels with Alpha Channel so that
2333 ** transparent pixels are not part of the results.
2334 */
anthony602ab9b2010-01-05 08:06:50 +00002335 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002336 alpha, /* color channel weighting : kernel*alpha */
2337 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002338
2339 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002340 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002341 k_pixels = p;
2342 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002343 for (v=0; v < (ssize_t) kernel->height; v++) {
2344 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002345 if ( IsNan(*k) ) continue;
2346 alpha=(*k)*(QuantumScale*(QuantumRange-
2347 k_pixels[u].opacity));
2348 gamma += alpha;
2349 result.red += alpha*k_pixels[u].red;
2350 result.green += alpha*k_pixels[u].green;
2351 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002352 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002353 if ( image->colorspace == CMYKColorspace)
2354 result.index += alpha*k_indexes[u];
2355 }
2356 k_pixels += image->columns+kernel->width;
2357 k_indexes += image->columns+kernel->width;
2358 }
2359 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002360 result.red *= gamma;
2361 result.green *= gamma;
2362 result.blue *= gamma;
2363 result.opacity *= gamma;
2364 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002365 }
anthony5ef8e942010-05-11 06:51:12 +00002366 else
2367 {
2368 /* No 'Sync' flag, or no Alpha involved.
2369 ** Convolution is simple individual channel weigthed sum.
2370 */
2371 k = &kernel->values[ kernel->width*kernel->height-1 ];
2372 k_pixels = p;
2373 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002374 for (v=0; v < (ssize_t) kernel->height; v++) {
2375 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002376 if ( IsNan(*k) ) continue;
2377 result.red += (*k)*k_pixels[u].red;
2378 result.green += (*k)*k_pixels[u].green;
2379 result.blue += (*k)*k_pixels[u].blue;
2380 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2381 if ( image->colorspace == CMYKColorspace)
2382 result.index += (*k)*k_indexes[u];
2383 }
2384 k_pixels += image->columns+kernel->width;
2385 k_indexes += image->columns+kernel->width;
2386 }
2387 }
anthony602ab9b2010-01-05 08:06:50 +00002388 break;
2389
anthony4fd27e22010-02-07 08:17:18 +00002390 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002391 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002392 **
2393 ** NOTE that the kernel is not reflected for this operation!
2394 **
2395 ** NOTE: in normal Greyscale Morphology, the kernel value should
2396 ** be added to the real value, this is currently not done, due to
2397 ** the nature of the boolean kernels being used.
2398 */
anthony4fd27e22010-02-07 08:17:18 +00002399 k = kernel->values;
2400 k_pixels = p;
2401 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002402 for (v=0; v < (ssize_t) kernel->height; v++) {
2403 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002404 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002405 Minimize(min.red, (double) k_pixels[u].red);
2406 Minimize(min.green, (double) k_pixels[u].green);
2407 Minimize(min.blue, (double) k_pixels[u].blue);
2408 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002409 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002410 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002411 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002412 }
2413 k_pixels += image->columns+kernel->width;
2414 k_indexes += image->columns+kernel->width;
2415 }
2416 break;
2417
anthony5ef8e942010-05-11 06:51:12 +00002418
anthony83ba99b2010-01-24 08:48:15 +00002419 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002420 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002421 **
2422 ** NOTE for correct working of this operation for asymetrical
2423 ** kernels, the kernel needs to be applied in its reflected form.
2424 ** That is its values needs to be reversed.
2425 **
2426 ** NOTE: in normal Greyscale Morphology, the kernel value should
2427 ** be added to the real value, this is currently not done, due to
2428 ** the nature of the boolean kernels being used.
2429 **
2430 */
anthony29188a82010-01-22 10:12:34 +00002431 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002432 k_pixels = p;
2433 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002434 for (v=0; v < (ssize_t) kernel->height; v++) {
2435 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002436 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002437 Maximize(max.red, (double) k_pixels[u].red);
2438 Maximize(max.green, (double) k_pixels[u].green);
2439 Maximize(max.blue, (double) k_pixels[u].blue);
2440 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002441 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002442 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002443 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002444 }
2445 k_pixels += image->columns+kernel->width;
2446 k_indexes += image->columns+kernel->width;
2447 }
anthony602ab9b2010-01-05 08:06:50 +00002448 break;
2449
anthony5ef8e942010-05-11 06:51:12 +00002450 case HitAndMissMorphology:
2451 case ThinningMorphology:
2452 case ThickenMorphology:
2453 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2454 **
2455 ** NOTE that the kernel is not reflected for this operation,
2456 ** and consists of both foreground and background pixel
2457 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2458 ** with either Nan or 0.5 values for don't care.
2459 **
2460 ** Note that this can produce negative results, though really
2461 ** only a positive match has any real value.
2462 */
2463 k = kernel->values;
2464 k_pixels = p;
2465 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002466 for (v=0; v < (ssize_t) kernel->height; v++) {
2467 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002468 if ( IsNan(*k) ) continue;
2469 if ( (*k) > 0.7 )
2470 { /* minimim of foreground pixels */
2471 Minimize(min.red, (double) k_pixels[u].red);
2472 Minimize(min.green, (double) k_pixels[u].green);
2473 Minimize(min.blue, (double) k_pixels[u].blue);
2474 Minimize(min.opacity,
2475 QuantumRange-(double) k_pixels[u].opacity);
2476 if ( image->colorspace == CMYKColorspace)
2477 Minimize(min.index, (double) k_indexes[u]);
2478 }
2479 else if ( (*k) < 0.3 )
2480 { /* maximum of background pixels */
2481 Maximize(max.red, (double) k_pixels[u].red);
2482 Maximize(max.green, (double) k_pixels[u].green);
2483 Maximize(max.blue, (double) k_pixels[u].blue);
2484 Maximize(max.opacity,
2485 QuantumRange-(double) k_pixels[u].opacity);
2486 if ( image->colorspace == CMYKColorspace)
2487 Maximize(max.index, (double) k_indexes[u]);
2488 }
2489 }
2490 k_pixels += image->columns+kernel->width;
2491 k_indexes += image->columns+kernel->width;
2492 }
2493 /* Pattern Match only if min fg larger than min bg pixels */
2494 min.red -= max.red; Maximize( min.red, 0.0 );
2495 min.green -= max.green; Maximize( min.green, 0.0 );
2496 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2497 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2498 min.index -= max.index; Maximize( min.index, 0.0 );
2499 break;
2500
anthony4fd27e22010-02-07 08:17:18 +00002501 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002502 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2503 **
2504 ** WARNING: the intensity test fails for CMYK and does not
2505 ** take into account the moderating effect of teh alpha channel
2506 ** on the intensity.
2507 **
2508 ** NOTE that the kernel is not reflected for this operation!
2509 */
anthony602ab9b2010-01-05 08:06:50 +00002510 k = kernel->values;
2511 k_pixels = p;
2512 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002513 for (v=0; v < (ssize_t) kernel->height; v++) {
2514 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002515 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002516 if ( result.red == 0.0 ||
2517 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2518 /* copy the whole pixel - no channel selection */
2519 *q = k_pixels[u];
2520 if ( result.red > 0.0 ) changed++;
2521 result.red = 1.0;
2522 }
anthony602ab9b2010-01-05 08:06:50 +00002523 }
2524 k_pixels += image->columns+kernel->width;
2525 k_indexes += image->columns+kernel->width;
2526 }
anthony602ab9b2010-01-05 08:06:50 +00002527 break;
2528
anthony83ba99b2010-01-24 08:48:15 +00002529 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002530 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2531 **
2532 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002533 ** take into account the moderating effect of the alpha channel
2534 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002535 **
2536 ** NOTE for correct working of this operation for asymetrical
2537 ** kernels, the kernel needs to be applied in its reflected form.
2538 ** That is its values needs to be reversed.
2539 */
anthony29188a82010-01-22 10:12:34 +00002540 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002541 k_pixels = p;
2542 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002543 for (v=0; v < (ssize_t) kernel->height; v++) {
2544 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002545 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2546 if ( result.red == 0.0 ||
2547 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2548 /* copy the whole pixel - no channel selection */
2549 *q = k_pixels[u];
2550 if ( result.red > 0.0 ) changed++;
2551 result.red = 1.0;
2552 }
anthony602ab9b2010-01-05 08:06:50 +00002553 }
2554 k_pixels += image->columns+kernel->width;
2555 k_indexes += image->columns+kernel->width;
2556 }
anthony602ab9b2010-01-05 08:06:50 +00002557 break;
2558
anthony5ef8e942010-05-11 06:51:12 +00002559
anthony602ab9b2010-01-05 08:06:50 +00002560 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002561 /* Add kernel Value and select the minimum value found.
2562 ** The result is a iterative distance from edge of image shape.
2563 **
2564 ** All Distance Kernels are symetrical, but that may not always
2565 ** be the case. For example how about a distance from left edges?
2566 ** To work correctly with asymetrical kernels the reflected kernel
2567 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002568 **
2569 ** Actually this is really a GreyErode with a negative kernel!
2570 **
anthony930be612010-02-08 04:26:15 +00002571 */
anthony29188a82010-01-22 10:12:34 +00002572 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002573 k_pixels = p;
2574 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002575 for (v=0; v < (ssize_t) kernel->height; v++) {
2576 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002577 if ( IsNan(*k) ) continue;
2578 Minimize(result.red, (*k)+k_pixels[u].red);
2579 Minimize(result.green, (*k)+k_pixels[u].green);
2580 Minimize(result.blue, (*k)+k_pixels[u].blue);
2581 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2582 if ( image->colorspace == CMYKColorspace)
2583 Minimize(result.index, (*k)+k_indexes[u]);
2584 }
2585 k_pixels += image->columns+kernel->width;
2586 k_indexes += image->columns+kernel->width;
2587 }
anthony602ab9b2010-01-05 08:06:50 +00002588 break;
2589
2590 case UndefinedMorphology:
2591 default:
2592 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002593 }
anthony5ef8e942010-05-11 06:51:12 +00002594 /* Final mathematics of results (combine with original image?)
2595 **
2596 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2597 ** be done here but works better with iteration as a image difference
2598 ** in the controling function (below). Thicken and Thinning however
2599 ** should be done here so thay can be iterated correctly.
2600 */
2601 switch ( method ) {
2602 case HitAndMissMorphology:
2603 case ErodeMorphology:
2604 result = min; /* minimum of neighbourhood */
2605 break;
2606 case DilateMorphology:
2607 result = max; /* maximum of neighbourhood */
2608 break;
2609 case ThinningMorphology:
2610 /* subtract pattern match from original */
2611 result.red -= min.red;
2612 result.green -= min.green;
2613 result.blue -= min.blue;
2614 result.opacity -= min.opacity;
2615 result.index -= min.index;
2616 break;
2617 case ThickenMorphology:
2618 /* Union with original image (maximize) - or should this be + */
2619 Maximize( result.red, min.red );
2620 Maximize( result.green, min.green );
2621 Maximize( result.blue, min.blue );
2622 Maximize( result.opacity, min.opacity );
2623 Maximize( result.index, min.index );
2624 break;
2625 default:
2626 /* result directly calculated or assigned */
2627 break;
2628 }
2629 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002630 switch ( method ) {
2631 case UndefinedMorphology:
2632 case DilateIntensityMorphology:
2633 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002634 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002635 default:
anthony83ba99b2010-01-24 08:48:15 +00002636 if ((channel & RedChannel) != 0)
2637 q->red = ClampToQuantum(result.red);
2638 if ((channel & GreenChannel) != 0)
2639 q->green = ClampToQuantum(result.green);
2640 if ((channel & BlueChannel) != 0)
2641 q->blue = ClampToQuantum(result.blue);
2642 if ((channel & OpacityChannel) != 0
2643 && image->matte == MagickTrue )
2644 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2645 if ((channel & IndexChannel) != 0
2646 && image->colorspace == CMYKColorspace)
2647 q_indexes[x] = ClampToQuantum(result.index);
2648 break;
2649 }
anthony5ef8e942010-05-11 06:51:12 +00002650 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002651 if ( ( p[r].red != q->red )
2652 || ( p[r].green != q->green )
2653 || ( p[r].blue != q->blue )
2654 || ( p[r].opacity != q->opacity )
2655 || ( image->colorspace == CMYKColorspace &&
2656 p_indexes[r] != q_indexes[x] ) )
2657 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002658 p++;
2659 q++;
anthony83ba99b2010-01-24 08:48:15 +00002660 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002661 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2662 if (sync == MagickFalse)
2663 status=MagickFalse;
2664 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2665 {
2666 MagickBooleanType
2667 proceed;
2668
2669#if defined(MAGICKCORE_OPENMP_SUPPORT)
2670 #pragma omp critical (MagickCore_MorphologyImage)
2671#endif
2672 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2673 if (proceed == MagickFalse)
2674 status=MagickFalse;
2675 }
anthony83ba99b2010-01-24 08:48:15 +00002676 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002677 result_image->type=image->type;
2678 q_view=DestroyCacheView(q_view);
2679 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002680 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002681}
2682
anthony4fd27e22010-02-07 08:17:18 +00002683
anthony9eb4f742010-05-18 02:45:54 +00002684MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002685 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002686 const KernelInfo *kernel, const CompositeOperator compose,
2687 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002688{
2689 Image
anthony47f5d062010-05-23 07:47:50 +00002690 *curr_image, /* Image we are working with or iterating */
2691 *work_image, /* secondary image for primative iteration */
2692 *save_image, /* saved image - for 'edge' method only */
2693 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002694
anthony4fd27e22010-02-07 08:17:18 +00002695 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002696 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2697 *norm_kernel, /* the current normal un-reflected kernel */
2698 *rflt_kernel, /* the current reflected kernel (if needed) */
2699 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002700
2701 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002702 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002703
2704 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002705 rslt_compose; /* multi-kernel compose method for results to use */
2706
2707 MagickBooleanType
2708 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002709
cristybb503372010-05-27 20:51:26 +00002710 size_t
anthony47f5d062010-05-23 07:47:50 +00002711 method_loop, /* Loop 1: number of compound method iterations */
2712 method_limit, /* maximum number of compound method iterations */
2713 kernel_number, /* Loop 2: the kernel number being applied */
2714 stage_loop, /* Loop 3: primative loop for compound morphology */
2715 stage_limit, /* how many primatives in this compound */
2716 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2717 kernel_limit, /* number of times to iterate kernel */
2718 count, /* total count of primative steps applied */
2719 changed, /* number pixels changed by last primative operation */
2720 kernel_changed, /* total count of changed using iterated kernel */
2721 method_changed; /* total count of changed over method iteration */
2722
2723 char
2724 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002725
anthony602ab9b2010-01-05 08:06:50 +00002726 assert(image != (Image *) NULL);
2727 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002728 assert(kernel != (KernelInfo *) NULL);
2729 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002730 assert(exception != (ExceptionInfo *) NULL);
2731 assert(exception->signature == MagickSignature);
2732
anthonyc3e48252010-05-24 12:43:11 +00002733 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002734 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002735 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002736
cristybb503372010-05-27 20:51:26 +00002737 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002738 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2739 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002740
cristye96405a2010-05-19 02:24:31 +00002741 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2742 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002743
anthony9eb4f742010-05-18 02:45:54 +00002744 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002745 curr_image = (Image *) image;
2746 work_image = save_image = rslt_image = (Image *) NULL;
2747 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002748
anthony47f5d062010-05-23 07:47:50 +00002749 /* Initialize specific methods
2750 * + which loop should use the given iteratations
2751 * + how many primatives make up the compound morphology
2752 * + multi-kernel compose method to use (by default)
2753 */
2754 method_limit = 1; /* just do method once, unless otherwise set */
2755 stage_limit = 1; /* assume method is not a compount */
2756 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002757 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002758 case SmoothMorphology: /* 4 primative compound morphology */
2759 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002760 break;
anthony47f5d062010-05-23 07:47:50 +00002761 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002762 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002763 case TopHatMorphology:
2764 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002765 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002766 case BottomHatMorphology:
2767 case EdgeMorphology:
2768 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002769 break;
2770 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002771 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002772 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002773 break;
anthonyc3e48252010-05-24 12:43:11 +00002774 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002775 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002776 method_limit = kernel_limit; /* iterate method with each kernel */
2777 kernel_limit = 1; /* do not do kernel iteration */
anthonye4d89962010-05-29 10:53:11 +00002778 case DistanceMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002779 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002780 break;
2781 default:
anthony930be612010-02-08 04:26:15 +00002782 break;
anthony602ab9b2010-01-05 08:06:50 +00002783 }
2784
anthonyc3e48252010-05-24 12:43:11 +00002785 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002786 if ( compose != UndefinedCompositeOp )
2787 rslt_compose = compose; /* override default composition for method */
2788 if ( rslt_compose == UndefinedCompositeOp )
2789 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2790
anthonyc3e48252010-05-24 12:43:11 +00002791 /* Some methods require a reflected kernel to use with primatives.
2792 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002793 switch ( method ) {
2794 case CorrelateMorphology:
2795 case CloseMorphology:
2796 case CloseIntensityMorphology:
2797 case BottomHatMorphology:
2798 case SmoothMorphology:
2799 reflected_kernel = CloneKernelInfo(kernel);
2800 if (reflected_kernel == (KernelInfo *) NULL)
2801 goto error_cleanup;
2802 RotateKernelInfo(reflected_kernel,180);
2803 break;
2804 default:
2805 break;
anthony9eb4f742010-05-18 02:45:54 +00002806 }
anthony7a01dcf2010-05-11 12:25:52 +00002807
anthony47f5d062010-05-23 07:47:50 +00002808 /* Loop 1: iterate the compound method */
2809 method_loop = 0;
2810 method_changed = 1;
2811 while ( method_loop < method_limit && method_changed > 0 ) {
2812 method_loop++;
2813 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002814
anthony47f5d062010-05-23 07:47:50 +00002815 /* Loop 2: iterate over each kernel in a multi-kernel list */
2816 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00002817 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00002818 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00002819
anthony47f5d062010-05-23 07:47:50 +00002820 kernel_number = 0;
2821 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002822
anthony47f5d062010-05-23 07:47:50 +00002823 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2824 stage_loop = 0; /* the compound morphology stage number */
2825 while ( stage_loop < stage_limit ) {
2826 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002827
anthony47f5d062010-05-23 07:47:50 +00002828 /* Select primative morphology for this stage of compound method */
2829 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002830 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002831 switch( method ) {
2832 case ErodeMorphology: /* just erode */
2833 case EdgeInMorphology: /* erode and image difference */
2834 primative = ErodeMorphology;
2835 break;
2836 case DilateMorphology: /* just dilate */
2837 case EdgeOutMorphology: /* dilate and image difference */
2838 primative = DilateMorphology;
2839 break;
2840 case OpenMorphology: /* erode then dialate */
2841 case TopHatMorphology: /* open and image difference */
2842 primative = ErodeMorphology;
2843 if ( stage_loop == 2 )
2844 primative = DilateMorphology;
2845 break;
2846 case OpenIntensityMorphology:
2847 primative = ErodeIntensityMorphology;
2848 if ( stage_loop == 2 )
2849 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00002850 break;
anthony47f5d062010-05-23 07:47:50 +00002851 case CloseMorphology: /* dilate, then erode */
2852 case BottomHatMorphology: /* close and image difference */
2853 this_kernel = rflt_kernel; /* use the reflected kernel */
2854 primative = DilateMorphology;
2855 if ( stage_loop == 2 )
2856 primative = ErodeMorphology;
2857 break;
2858 case CloseIntensityMorphology:
2859 this_kernel = rflt_kernel; /* use the reflected kernel */
2860 primative = DilateIntensityMorphology;
2861 if ( stage_loop == 2 )
2862 primative = ErodeIntensityMorphology;
2863 break;
2864 case SmoothMorphology: /* open, close */
2865 switch ( stage_loop ) {
2866 case 1: /* start an open method, which starts with Erode */
2867 primative = ErodeMorphology;
2868 break;
2869 case 2: /* now Dilate the Erode */
2870 primative = DilateMorphology;
2871 break;
2872 case 3: /* Reflect kernel a close */
2873 this_kernel = rflt_kernel; /* use the reflected kernel */
2874 primative = DilateMorphology;
2875 break;
2876 case 4: /* Finish the Close */
2877 this_kernel = rflt_kernel; /* use the reflected kernel */
2878 primative = ErodeMorphology;
2879 break;
2880 }
2881 break;
2882 case EdgeMorphology: /* dilate and erode difference */
2883 primative = DilateMorphology;
2884 if ( stage_loop == 2 ) {
2885 save_image = curr_image; /* save the image difference */
2886 curr_image = (Image *) image;
2887 primative = ErodeMorphology;
2888 }
2889 break;
2890 case CorrelateMorphology:
2891 /* A Correlation is a Convolution with a reflected kernel.
2892 ** However a Convolution is a weighted sum using a reflected
2893 ** kernel. It may seem stange to convert a Correlation into a
2894 ** Convolution as the Correlation is the simplier method, but
2895 ** Convolution is much more commonly used, and it makes sense to
2896 ** implement it directly so as to avoid the need to duplicate the
2897 ** kernel when it is not required (which is typically the
2898 ** default).
2899 */
2900 this_kernel = rflt_kernel; /* use the reflected kernel */
2901 primative = ConvolveMorphology;
2902 break;
2903 default:
anthony47f5d062010-05-23 07:47:50 +00002904 break;
2905 }
anthonye4d89962010-05-29 10:53:11 +00002906 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00002907
anthony47f5d062010-05-23 07:47:50 +00002908 /* Extra information for debugging compound operations */
2909 if ( verbose == MagickTrue ) {
2910 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002911 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002912 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2913 (unsigned long) method_loop,(unsigned long) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00002914 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002915 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002916 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2917 (unsigned long) method_loop);
anthony47f5d062010-05-23 07:47:50 +00002918 else
2919 v_info[0] = '\0';
2920 }
2921
2922 /* Loop 4: Iterate the kernel with primative */
2923 kernel_loop = 0;
2924 kernel_changed = 0;
2925 changed = 1;
2926 while ( kernel_loop < kernel_limit && changed > 0 ) {
2927 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002928
2929 /* Create a destination image, if not yet defined */
2930 if ( work_image == (Image *) NULL )
2931 {
2932 work_image=CloneImage(image,0,0,MagickTrue,exception);
2933 if (work_image == (Image *) NULL)
2934 goto error_cleanup;
2935 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2936 {
2937 InheritException(exception,&work_image->exception);
2938 goto error_cleanup;
2939 }
2940 }
2941
anthony501c2f92010-06-02 10:55:14 +00002942 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002943 count++;
anthony47f5d062010-05-23 07:47:50 +00002944 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002945 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002946 kernel_changed += changed;
2947 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002948
anthony47f5d062010-05-23 07:47:50 +00002949 if ( verbose == MagickTrue ) {
2950 if ( kernel_loop > 1 )
2951 fprintf(stderr, "\n"); /* add end-of-line from previous */
2952 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2953 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2954 ( this_kernel == rflt_kernel ) ? "*" : "",
cristyf2faecf2010-05-28 19:19:36 +00002955 (unsigned long) method_loop+kernel_loop-1,(unsigned long)
2956 kernel_number,(unsigned long) count,(unsigned long) changed);
anthony47f5d062010-05-23 07:47:50 +00002957 }
anthony9eb4f742010-05-18 02:45:54 +00002958 /* prepare next loop */
2959 { Image *tmp = work_image; /* swap images for iteration */
2960 work_image = curr_image;
2961 curr_image = tmp;
2962 }
2963 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002964 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002965
anthony47f5d062010-05-23 07:47:50 +00002966 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002967
anthony47f5d062010-05-23 07:47:50 +00002968 if ( verbose == MagickTrue && kernel_changed != changed )
cristyf2faecf2010-05-28 19:19:36 +00002969 fprintf(stderr, " Total %lu",(unsigned long) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00002970 if ( verbose == MagickTrue && stage_loop < stage_limit )
2971 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002972
2973#if 0
anthonye4d89962010-05-29 10:53:11 +00002974 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
2975 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
2976 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
2977 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
2978 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002979#endif
2980
anthony47f5d062010-05-23 07:47:50 +00002981 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002982
anthony47f5d062010-05-23 07:47:50 +00002983 /* Final Post-processing for some Compound Methods
2984 **
2985 ** The removal of any 'Sync' channel flag in the Image Compositon
2986 ** below ensures the methematical compose method is applied in a
2987 ** purely mathematical way, and only to the selected channels.
2988 ** Turn off SVG composition 'alpha blending'.
2989 */
2990 switch( method ) {
2991 case EdgeOutMorphology:
2992 case EdgeInMorphology:
2993 case TopHatMorphology:
2994 case BottomHatMorphology:
2995 if ( verbose == MagickTrue )
2996 fprintf(stderr, "\n%s: Difference with original image",
2997 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2998 (void) CompositeImageChannel(curr_image,
2999 (ChannelType) (channel & ~SyncChannels),
3000 DifferenceCompositeOp, image, 0, 0);
3001 break;
3002 case EdgeMorphology:
3003 if ( verbose == MagickTrue )
3004 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3005 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3006 (void) CompositeImageChannel(curr_image,
3007 (ChannelType) (channel & ~SyncChannels),
3008 DifferenceCompositeOp, save_image, 0, 0);
3009 save_image = DestroyImage(save_image); /* finished with save image */
3010 break;
3011 default:
3012 break;
3013 }
3014
3015 /* multi-kernel handling: re-iterate, or compose results */
3016 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003017 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003018 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003019 { if ( verbose == MagickTrue ) {
3020 if ( this_kernel->next != (KernelInfo *) NULL )
3021 fprintf(stderr, " (re-iterate)");
3022 else
3023 fprintf(stderr, " (done)");
3024 }
3025 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003026 }
anthony47f5d062010-05-23 07:47:50 +00003027 else if ( rslt_image == (Image *) NULL)
3028 { if ( verbose == MagickTrue )
3029 fprintf(stderr, " (save for compose)");
3030 rslt_image = curr_image;
3031 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003032 }
anthony47f5d062010-05-23 07:47:50 +00003033 else
3034 { /* add the new 'current' result to the composition
3035 **
3036 ** The removal of any 'Sync' channel flag in the Image Compositon
3037 ** below ensures the methematical compose method is applied in a
3038 ** purely mathematical way, and only to the selected channels.
3039 ** Turn off SVG composition 'alpha blending'.
3040 */
3041 if ( verbose == MagickTrue )
3042 fprintf(stderr, " (compose \"%s\")",
3043 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3044 (void) CompositeImageChannel(rslt_image,
3045 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3046 curr_image, 0, 0);
3047 curr_image = (Image *) image; /* continue with original image */
3048 }
3049 if ( verbose == MagickTrue )
3050 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003051
anthony47f5d062010-05-23 07:47:50 +00003052 /* loop to the next kernel in a multi-kernel list */
3053 norm_kernel = norm_kernel->next;
3054 if ( rflt_kernel != (KernelInfo *) NULL )
3055 rflt_kernel = rflt_kernel->next;
3056 kernel_number++;
3057 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003058
anthony47f5d062010-05-23 07:47:50 +00003059 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003060
anthony9eb4f742010-05-18 02:45:54 +00003061 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003062
anthony47f5d062010-05-23 07:47:50 +00003063 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003064error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003065 if ( curr_image != (Image *) NULL &&
3066 curr_image != rslt_image &&
3067 curr_image != image )
3068 curr_image = DestroyImage(curr_image);
3069 if ( rslt_image != (Image *) NULL )
3070 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003071exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003072 if ( curr_image != (Image *) NULL &&
3073 curr_image != rslt_image &&
3074 curr_image != image )
3075 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003076 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003077 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003078 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003079 save_image = DestroyImage(save_image);
3080 if ( reflected_kernel != (KernelInfo *) NULL )
3081 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3082 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003083}
3084
3085/*
3086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3087% %
3088% %
3089% %
3090% M o r p h o l o g y I m a g e C h a n n e l %
3091% %
3092% %
3093% %
3094%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3095%
3096% MorphologyImageChannel() applies a user supplied kernel to the image
3097% according to the given mophology method.
3098%
3099% This function applies any and all user defined settings before calling
3100% the above internal function MorphologyApply().
3101%
3102% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003103% * Output Bias for Convolution and correlation ("-bias")
3104% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3105% This can also includes the addition of a scaled unity kernel.
3106% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003107%
3108% The format of the MorphologyImage method is:
3109%
3110% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003111% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003112%
3113% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003114% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003115% KernelInfo *kernel,ExceptionInfo *exception)
3116%
3117% A description of each parameter follows:
3118%
3119% o image: the image.
3120%
3121% o method: the morphology method to be applied.
3122%
3123% o iterations: apply the operation this many times (or no change).
3124% A value of -1 means loop until no change found.
3125% How this is applied may depend on the morphology method.
3126% Typically this is a value of 1.
3127%
3128% o channel: the channel type.
3129%
3130% o kernel: An array of double representing the morphology kernel.
3131% Warning: kernel may be normalized for the Convolve method.
3132%
3133% o exception: return any errors or warnings in this structure.
3134%
3135*/
3136
3137MagickExport Image *MorphologyImageChannel(const Image *image,
3138 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003139 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003140{
3141 const char
3142 *artifact;
3143
3144 KernelInfo
3145 *curr_kernel;
3146
anthony47f5d062010-05-23 07:47:50 +00003147 CompositeOperator
3148 compose;
3149
anthony9eb4f742010-05-18 02:45:54 +00003150 Image
3151 *morphology_image;
3152
3153
anthony46a369d2010-05-19 02:41:48 +00003154 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3155 * This is done BEFORE the ShowKernelInfo() function is called so that
3156 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003157 */
3158 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003159 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003160 {
3161 artifact = GetImageArtifact(image,"convolve:scale");
3162 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003163 if ( curr_kernel == kernel )
3164 curr_kernel = CloneKernelInfo(kernel);
3165 if (curr_kernel == (KernelInfo *) NULL) {
3166 curr_kernel=DestroyKernelInfo(curr_kernel);
3167 return((Image *) NULL);
3168 }
anthony46a369d2010-05-19 02:41:48 +00003169 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003170 }
3171 }
3172
3173 /* display the (normalized) kernel via stderr */
3174 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003175 if ( artifact == (const char *) NULL)
3176 artifact = GetImageArtifact(image,"convolve:showkernel");
3177 if ( artifact == (const char *) NULL)
3178 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003179 if ( artifact != (const char *) NULL)
3180 ShowKernelInfo(curr_kernel);
3181
anthony47f5d062010-05-23 07:47:50 +00003182 /* override the default handling of multi-kernel morphology results
3183 * if 'Undefined' use the default method
3184 * if 'None' (default for 'Convolve') re-iterate previous result
3185 * otherwise merge resulting images using compose method given
3186 */
3187 compose = UndefinedCompositeOp; /* use default for method */
3188 artifact = GetImageArtifact(image,"morphology:compose");
3189 if ( artifact != (const char *) NULL)
3190 compose = (CompositeOperator) ParseMagickOption(
3191 MagickComposeOptions,MagickFalse,artifact);
3192
anthony9eb4f742010-05-18 02:45:54 +00003193 /* Apply the Morphology */
3194 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003195 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003196
3197 /* Cleanup and Exit */
3198 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003199 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003200 return(morphology_image);
3201}
3202
3203MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003204 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003205 *exception)
3206{
3207 Image
3208 *morphology_image;
3209
3210 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3211 iterations,kernel,exception);
3212 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003213}
anthony83ba99b2010-01-24 08:48:15 +00003214
3215/*
3216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3217% %
3218% %
3219% %
anthony4fd27e22010-02-07 08:17:18 +00003220+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003221% %
3222% %
3223% %
3224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3225%
anthony46a369d2010-05-19 02:41:48 +00003226% RotateKernelInfo() rotates the kernel by the angle given.
3227%
3228% Currently it is restricted to 90 degree angles, of either 1D kernels
3229% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3230% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003231%
anthony4fd27e22010-02-07 08:17:18 +00003232% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003233%
anthony4fd27e22010-02-07 08:17:18 +00003234% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003235%
3236% A description of each parameter follows:
3237%
3238% o kernel: the Morphology/Convolution kernel
3239%
3240% o angle: angle to rotate in degrees
3241%
anthony46a369d2010-05-19 02:41:48 +00003242% This function is currently internal to this module only, but can be exported
3243% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003244*/
anthony4fd27e22010-02-07 08:17:18 +00003245static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003246{
anthony1b2bc0a2010-05-12 05:25:22 +00003247 /* angle the lower kernels first */
3248 if ( kernel->next != (KernelInfo *) NULL)
3249 RotateKernelInfo(kernel->next, angle);
3250
anthony83ba99b2010-01-24 08:48:15 +00003251 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3252 **
3253 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3254 */
3255
3256 /* Modulus the angle */
3257 angle = fmod(angle, 360.0);
3258 if ( angle < 0 )
3259 angle += 360.0;
3260
anthony3c10fc82010-05-13 02:40:51 +00003261 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003262 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003263
anthony3dd0f622010-05-13 12:57:32 +00003264 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003265 switch (kernel->type) {
3266 /* These built-in kernels are cylindrical kernels, rotating is useless */
3267 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +00003268 case DoGKernel:
3269 case LoGKernel:
anthony83ba99b2010-01-24 08:48:15 +00003270 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003271 case PeaksKernel:
3272 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003273 case ChebyshevKernel:
3274 case ManhattenKernel:
3275 case EuclideanKernel:
3276 return;
3277
3278 /* These may be rotatable at non-90 angles in the future */
3279 /* but simply rotating them in multiples of 90 degrees is useless */
3280 case SquareKernel:
3281 case DiamondKernel:
3282 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003283 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003284 return;
3285
3286 /* These only allows a +/-90 degree rotation (by transpose) */
3287 /* A 180 degree rotation is useless */
3288 case BlurKernel:
3289 case RectangleKernel:
3290 if ( 135.0 < angle && angle <= 225.0 )
3291 return;
3292 if ( 225.0 < angle && angle <= 315.0 )
3293 angle -= 180;
3294 break;
3295
anthony3dd0f622010-05-13 12:57:32 +00003296 default:
anthony83ba99b2010-01-24 08:48:15 +00003297 break;
3298 }
anthony3c10fc82010-05-13 02:40:51 +00003299 /* Attempt rotations by 45 degrees */
3300 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3301 {
3302 if ( kernel->width == 3 && kernel->height == 3 )
3303 { /* Rotate a 3x3 square by 45 degree angle */
3304 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003305 kernel->values[0] = kernel->values[3];
3306 kernel->values[3] = kernel->values[6];
3307 kernel->values[6] = kernel->values[7];
3308 kernel->values[7] = kernel->values[8];
3309 kernel->values[8] = kernel->values[5];
3310 kernel->values[5] = kernel->values[2];
3311 kernel->values[2] = kernel->values[1];
3312 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003313 /* rotate non-centered origin */
3314 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003315 ssize_t x,y;
3316 x = (ssize_t) kernel->x-1;
3317 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003318 if ( x == y ) x = 0;
3319 else if ( x == 0 ) x = -y;
3320 else if ( x == -y ) y = 0;
3321 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003322 kernel->x = (ssize_t) x+1;
3323 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003324 }
anthony43c49252010-05-18 10:59:50 +00003325 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3326 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003327 }
3328 else
3329 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3330 }
3331 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3332 {
3333 if ( kernel->width == 1 || kernel->height == 1 )
3334 { /* Do a transpose of the image, which results in a 90
3335 ** degree rotation of a 1 dimentional kernel
3336 */
cristybb503372010-05-27 20:51:26 +00003337 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003338 t;
cristybb503372010-05-27 20:51:26 +00003339 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003340 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003341 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003342 t = kernel->x;
3343 kernel->x = kernel->y;
3344 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003345 if ( kernel->width == 1 ) {
3346 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3347 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3348 } else {
3349 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3350 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3351 }
anthony3c10fc82010-05-13 02:40:51 +00003352 }
3353 else if ( kernel->width == kernel->height )
3354 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003355 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003356 i,j,x,y;
3357 register MagickRealType
3358 *k,t;
3359 k=kernel->values;
3360 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3361 for( j=0, y=kernel->height-1; j<y; j++, y--)
3362 { t = k[i+j*kernel->width];
3363 k[i+j*kernel->width] = k[j+x*kernel->width];
3364 k[j+x*kernel->width] = k[x+y*kernel->width];
3365 k[x+y*kernel->width] = k[y+i*kernel->width];
3366 k[y+i*kernel->width] = t;
3367 }
3368 }
3369 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003370 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003371 x = (ssize_t) (kernel->x*2-kernel->width+1);
3372 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003373 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3374 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003375 }
anthony43c49252010-05-18 10:59:50 +00003376 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3377 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003378 }
3379 else
3380 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3381 }
anthony83ba99b2010-01-24 08:48:15 +00003382 if ( 135.0 < angle && angle <= 225.0 )
3383 {
anthony43c49252010-05-18 10:59:50 +00003384 /* For a 180 degree rotation - also know as a reflection
3385 * This is actually a very very common operation!
3386 * Basically all that is needed is a reversal of the kernel data!
3387 * And a reflection of the origon
3388 */
cristybb503372010-05-27 20:51:26 +00003389 size_t
anthony83ba99b2010-01-24 08:48:15 +00003390 i,j;
3391 register double
3392 *k,t;
3393
3394 k=kernel->values;
3395 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3396 t=k[i], k[i]=k[j], k[j]=t;
3397
cristybb503372010-05-27 20:51:26 +00003398 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3399 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003400 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3401 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003402 }
anthony3c10fc82010-05-13 02:40:51 +00003403 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003404 * In the future some form of non-orthogonal angled rotates could be
3405 * performed here, posibily with a linear kernel restriction.
3406 */
3407
3408#if 0
anthony3c10fc82010-05-13 02:40:51 +00003409 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003410 */
cristybb503372010-05-27 20:51:26 +00003411 size_t
anthony83ba99b2010-01-24 08:48:15 +00003412 y;
cristybb503372010-05-27 20:51:26 +00003413 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003414 x,r;
3415 register double
3416 *k,t;
3417
3418 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3419 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3420 t=k[x], k[x]=k[r], k[r]=t;
3421
cristyc99304f2010-02-01 15:26:27 +00003422 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003423 angle = fmod(angle+180.0, 360.0);
3424 }
3425#endif
3426 return;
3427}
3428
3429/*
3430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3431% %
3432% %
3433% %
anthony46a369d2010-05-19 02:41:48 +00003434% S c a l e G e o m e t r y K e r n e l I n f o %
3435% %
3436% %
3437% %
3438%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3439%
3440% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3441% provided as a "-set option:convolve:scale {geometry}" user setting,
3442% and modifies the kernel according to the parsed arguments of that setting.
3443%
3444% The first argument (and any normalization flags) are passed to
3445% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3446% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3447% into the scaled/normalized kernel.
3448%
3449% The format of the ScaleKernelInfo method is:
3450%
3451% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3452% const MagickStatusType normalize_flags )
3453%
3454% A description of each parameter follows:
3455%
3456% o kernel: the Morphology/Convolution kernel to modify
3457%
3458% o geometry:
3459% The geometry string to parse, typically from the user provided
3460% "-set option:convolve:scale {geometry}" setting.
3461%
3462*/
3463MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3464 const char *geometry)
3465{
3466 GeometryFlags
3467 flags;
3468 GeometryInfo
3469 args;
3470
3471 SetGeometryInfo(&args);
3472 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3473
3474#if 0
3475 /* For Debugging Geometry Input */
3476 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3477 flags, args.rho, args.sigma, args.xi, args.psi );
3478#endif
3479
3480 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3481 args.rho *= 0.01, args.sigma *= 0.01;
3482
3483 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3484 args.rho = 1.0;
3485 if ( (flags & SigmaValue) == 0 )
3486 args.sigma = 0.0;
3487
3488 /* Scale/Normalize the input kernel */
3489 ScaleKernelInfo(kernel, args.rho, flags);
3490
3491 /* Add Unity Kernel, for blending with original */
3492 if ( (flags & SigmaValue) != 0 )
3493 UnityAddKernelInfo(kernel, args.sigma);
3494
3495 return;
3496}
3497/*
3498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3499% %
3500% %
3501% %
cristy6771f1e2010-03-05 19:43:39 +00003502% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003503% %
3504% %
3505% %
3506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3507%
anthony1b2bc0a2010-05-12 05:25:22 +00003508% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3509% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003510%
anthony999bb2c2010-02-18 12:38:01 +00003511% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003512% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003513%
anthony46a369d2010-05-19 02:41:48 +00003514% If either of the two 'normalize_flags' are given the kernel will first be
3515% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003516%
3517% Kernel normalization ('normalize_flags' given) is designed to ensure that
3518% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003519% morphology methods will fall into -1.0 to +1.0 range. Note that for
3520% non-HDRI versions of IM this may cause images to have any negative results
3521% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003522%
3523% More specifically. Kernels which only contain positive values (such as a
3524% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003525% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003526%
3527% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3528% the kernel will be scaled by the absolute of the sum of kernel values, so
3529% that it will generally fall within the +/- 1.0 range.
3530%
3531% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3532% will be scaled by just the sum of the postive values, so that its output
3533% range will again fall into the +/- 1.0 range.
3534%
3535% For special kernels designed for locating shapes using 'Correlate', (often
3536% only containing +1 and -1 values, representing foreground/brackground
3537% matching) a special normalization method is provided to scale the positive
3538% values seperatally to those of the negative values, so the kernel will be
3539% forced to become a zero-sum kernel better suited to such searches.
3540%
anthony1b2bc0a2010-05-12 05:25:22 +00003541% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003542% attributes within the kernel structure have been correctly set during the
3543% kernels creation.
3544%
3545% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003546% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3547% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003548%
anthony4fd27e22010-02-07 08:17:18 +00003549% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003550%
anthony999bb2c2010-02-18 12:38:01 +00003551% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3552% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003553%
3554% A description of each parameter follows:
3555%
3556% o kernel: the Morphology/Convolution kernel
3557%
anthony999bb2c2010-02-18 12:38:01 +00003558% o scaling_factor:
3559% multiply all values (after normalization) by this factor if not
3560% zero. If the kernel is normalized regardless of any flags.
3561%
3562% o normalize_flags:
3563% GeometryFlags defining normalization method to use.
3564% specifically: NormalizeValue, CorrelateNormalizeValue,
3565% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003566%
3567*/
cristy6771f1e2010-03-05 19:43:39 +00003568MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3569 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003570{
cristybb503372010-05-27 20:51:26 +00003571 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003572 i;
3573
anthony999bb2c2010-02-18 12:38:01 +00003574 register double
3575 pos_scale,
3576 neg_scale;
3577
anthony46a369d2010-05-19 02:41:48 +00003578 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003579 if ( kernel->next != (KernelInfo *) NULL)
3580 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3581
anthony46a369d2010-05-19 02:41:48 +00003582 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003583 pos_scale = 1.0;
3584 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003585 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003586 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003587 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003588 else
anthonyf4e00312010-05-20 12:06:35 +00003589 /* zero-summing kernel */
3590 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003591 }
anthony46a369d2010-05-19 02:41:48 +00003592 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003593 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3594 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3595 ? kernel->positive_range : 1.0;
3596 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3597 ? -kernel->negative_range : 1.0;
3598 }
3599 else
3600 neg_scale = pos_scale;
3601
3602 /* finialize scaling_factor for positive and negative components */
3603 pos_scale = scaling_factor/pos_scale;
3604 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003605
cristybb503372010-05-27 20:51:26 +00003606 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003607 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003608 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003609
anthony999bb2c2010-02-18 12:38:01 +00003610 /* convolution output range */
3611 kernel->positive_range *= pos_scale;
3612 kernel->negative_range *= neg_scale;
3613 /* maximum and minimum values in kernel */
3614 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3615 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3616
anthony46a369d2010-05-19 02:41:48 +00003617 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003618 if ( scaling_factor < MagickEpsilon ) {
3619 double t;
3620 t = kernel->positive_range;
3621 kernel->positive_range = kernel->negative_range;
3622 kernel->negative_range = t;
3623 t = kernel->maximum;
3624 kernel->maximum = kernel->minimum;
3625 kernel->minimum = 1;
3626 }
anthonycc6c8362010-01-25 04:14:01 +00003627
3628 return;
3629}
3630
3631/*
3632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3633% %
3634% %
3635% %
anthony46a369d2010-05-19 02:41:48 +00003636% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003637% %
3638% %
3639% %
3640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3641%
anthony4fd27e22010-02-07 08:17:18 +00003642% ShowKernelInfo() outputs the details of the given kernel defination to
3643% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003644%
3645% The format of the ShowKernel method is:
3646%
anthony4fd27e22010-02-07 08:17:18 +00003647% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003648%
3649% A description of each parameter follows:
3650%
3651% o kernel: the Morphology/Convolution kernel
3652%
anthony83ba99b2010-01-24 08:48:15 +00003653*/
anthony4fd27e22010-02-07 08:17:18 +00003654MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003655{
anthony7a01dcf2010-05-11 12:25:52 +00003656 KernelInfo
3657 *k;
anthony83ba99b2010-01-24 08:48:15 +00003658
cristybb503372010-05-27 20:51:26 +00003659 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003660 c, i, u, v;
3661
3662 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3663
anthony46a369d2010-05-19 02:41:48 +00003664 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003665 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003666 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003667 fprintf(stderr, " \"%s",
3668 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3669 if ( fabs(k->angle) > MagickEpsilon )
3670 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003671 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3672 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003673 fprintf(stderr,
3674 " with values from %.*lg to %.*lg\n",
3675 GetMagickPrecision(), k->minimum,
3676 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003677 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003678 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003679 GetMagickPrecision(), k->positive_range);
3680 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3681 fprintf(stderr, " (Zero-Summing)\n");
3682 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3683 fprintf(stderr, " (Normalized)\n");
3684 else
3685 fprintf(stderr, " (Sum %.*lg)\n",
3686 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003687 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003688 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003689 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003690 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003691 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003692 else
anthonyf4e00312010-05-20 12:06:35 +00003693 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003694 GetMagickPrecision(), k->values[i]);
3695 fprintf(stderr,"\n");
3696 }
anthony83ba99b2010-01-24 08:48:15 +00003697 }
3698}
anthonycc6c8362010-01-25 04:14:01 +00003699
3700/*
3701%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3702% %
3703% %
3704% %
anthony43c49252010-05-18 10:59:50 +00003705% U n i t y A d d K e r n a l I n f o %
3706% %
3707% %
3708% %
3709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3710%
3711% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3712% to the given pre-scaled and normalized Kernel. This in effect adds that
3713% amount of the original image into the resulting convolution kernel. This
3714% value is usually provided by the user as a percentage value in the
3715% 'convolve:scale' setting.
3716%
anthony501c2f92010-06-02 10:55:14 +00003717% The resulting effect is to convert the defined kernels into blended
3718% soft-blurs, unsharp kernels or into sharpening kernels.
anthony43c49252010-05-18 10:59:50 +00003719%
anthony46a369d2010-05-19 02:41:48 +00003720% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003721%
3722% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3723%
3724% A description of each parameter follows:
3725%
3726% o kernel: the Morphology/Convolution kernel
3727%
3728% o scale:
3729% scaling factor for the unity kernel to be added to
3730% the given kernel.
3731%
anthony43c49252010-05-18 10:59:50 +00003732*/
3733MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3734 const double scale)
3735{
anthony46a369d2010-05-19 02:41:48 +00003736 /* do the other kernels in a multi-kernel list first */
3737 if ( kernel->next != (KernelInfo *) NULL)
3738 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003739
anthony46a369d2010-05-19 02:41:48 +00003740 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003741 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003742 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003743
3744 return;
3745}
3746
3747/*
3748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3749% %
3750% %
3751% %
3752% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003753% %
3754% %
3755% %
3756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3757%
3758% ZeroKernelNans() replaces any special 'nan' value that may be present in
3759% the kernel with a zero value. This is typically done when the kernel will
3760% be used in special hardware (GPU) convolution processors, to simply
3761% matters.
3762%
3763% The format of the ZeroKernelNans method is:
3764%
anthony46a369d2010-05-19 02:41:48 +00003765% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003766%
3767% A description of each parameter follows:
3768%
3769% o kernel: the Morphology/Convolution kernel
3770%
anthonycc6c8362010-01-25 04:14:01 +00003771*/
anthonyc4c86e02010-01-27 09:30:32 +00003772MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003773{
cristybb503372010-05-27 20:51:26 +00003774 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003775 i;
3776
anthony46a369d2010-05-19 02:41:48 +00003777 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003778 if ( kernel->next != (KernelInfo *) NULL)
3779 ZeroKernelNans(kernel->next);
3780
anthony43c49252010-05-18 10:59:50 +00003781 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003782 if ( IsNan(kernel->values[i]) )
3783 kernel->values[i] = 0.0;
3784
3785 return;
3786}