blob: bce939dd66a2d5bb25a8de5ca25081ad5d032349 [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%
anthonyc1061722010-05-14 06:23:49 +0000577% DOG:{radius},{sigma1},{sigma2}
578% "Difference of Gaussians" Kernel.
579% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
580% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
581% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000582%
anthony9eb4f742010-05-18 02:45:54 +0000583% LOG:{radius},{sigma}
584% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
585% The supposed ideal edge detection, zero-summing kernel.
586%
587% An alturnative to this kernel is to use a "DOG" with a sigma ratio of
588% approx 1.6, which can also be applied as a 2 pass "DOB" (see below).
589%
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%
603% DOB:{radius},{sigma1},{sigma2}[,{angle}]
604% "Difference of Blurs" Kernel.
605% As "Blur" but with the 1D gaussian produced by 'sigma2' subtracted
606% from thethe 1D gaussian produced by 'sigma1'.
607% The result is a zero-summing kernel.
608%
609% This can be used to generate a faster "DOG" convolution, in the same
610% way "Blur" can.
anthony602ab9b2010-01-05 08:06:50 +0000611%
anthony3c10fc82010-05-13 02:40:51 +0000612% Comet:{width},{sigma},{angle}
613% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000614% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000615% Adding two such blurs in opposite directions produces a Blur Kernel.
616% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000617%
anthony3c10fc82010-05-13 02:40:51 +0000618% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000619% radius of the kernel.
620%
621% # Still to be implemented...
622% #
anthony4fd27e22010-02-07 08:17:18 +0000623% # Filter2D
624% # Filter1D
625% # Set kernel values using a resize filter, and given scale (sigma)
626% # Cylindrical or Linear. Is this posible with an image?
627% #
anthony602ab9b2010-01-05 08:06:50 +0000628%
anthony3c10fc82010-05-13 02:40:51 +0000629% Named Constant Convolution Kernels
630%
anthonyc1061722010-05-14 06:23:49 +0000631% All these are unscaled, zero-summing kernels by default. As such for
632% non-HDRI version of ImageMagick some form of normalization, user scaling,
633% and biasing the results is recommended, to prevent the resulting image
634% being 'clipped'.
635%
636% The 3x3 kernels (most of these) can be circularly rotated in multiples of
637% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000638%
639% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000640% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000641% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
642% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000643% Type 2 : 3x3 with center:4 edge:1 corner:-2
644% Type 3 : 3x3 with center:4 edge:-2 corner:1
645% Type 5 : 5x5 laplacian
646% Type 7 : 7x7 laplacian
anthony43c49252010-05-18 10:59:50 +0000647% Type 15 : 5x5 LOG (sigma approx 1.4)
648% Type 19 : 9x9 LOG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000649%
650% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000651% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000652% -1, 0, 1
653% -2, 0,-2
654% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000655%
anthonyc1061722010-05-14 06:23:49 +0000656% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000657% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000658% 0, 0, 0
659% -1, 1, 0
660% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000661% Prewitt:{angle}
662% Prewitt Edge convolution kernel (3x3)
663% -1, 0, 1
664% -1, 0, 1
665% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000666% Compass:{angle}
667% Prewitt's "Compass" convolution kernel (3x3)
668% -1, 1, 1
669% -1,-2, 1
670% -1, 1, 1
671% Kirsch:{angle}
672% Kirsch's "Compass" convolution kernel (3x3)
673% -3,-3, 5
674% -3, 0, 5
675% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000676%
anthonye2a60ce2010-05-19 12:30:40 +0000677% FreiChen:{type},{angle}
678% Frei-Chen Edge Detector is a set of 9 unique convolution kernels that
anthonyc3cd15b2010-05-27 06:05:40 +0000679% are specially weighted.
680%
681% Type 0: | -1, 0, 1 |
682% | -sqrt(2), 0, sqrt(2) |
683% | -1, 0, 1 |
684%
685% This is basically the unnormalized discrete kernel that can be used
686% instead ot a Sobel kernel.
687%
688% The next 9 kernel types are specially pre-weighted. They should not
689% be normalized. After applying each to the original image, the results
690% is then added together. The square root of the resulting image is
691% the cosine of the edge, and the direction of the feature detection.
anthonye2a60ce2010-05-19 12:30:40 +0000692%
693% Type 1: | 1, sqrt(2), 1 |
694% | 0, 0, 0 | / 2*sqrt(2)
695% | -1, -sqrt(2), -1 |
696%
697% Type 2: | 1, 0, 1 |
698% | sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
699% | 1, 0, 1 |
700%
701% Type 3: | 0, -1, sqrt(2) |
702% | 1, 0, -1 | / 2*sqrt(2)
703% | -sqrt(2), 1, 0 |
704%
anthony6915d062010-05-19 12:45:51 +0000705% Type 4: | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000706% | -1, 0, 1 | / 2*sqrt(2)
707% | 0, 1, -sqrt(2) |
708%
709% Type 5: | 0, 1, 0 |
710% | -1, 0, -1 | / 2
711% | 0, 1, 0 |
712%
713% Type 6: | -1, 0, 1 |
714% | 0, 0, 0 | / 2
715% | 1, 0, -1 |
716%
anthonyf4e00312010-05-20 12:06:35 +0000717% Type 7: | 1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000718% | -2, 4, -2 | / 6
719% | 1, -2, 1 |
720%
anthonyf4e00312010-05-20 12:06:35 +0000721% Type 8: | -2, 1, -2 |
722% | 1, 4, 1 | / 6
723% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000724%
anthonyf4e00312010-05-20 12:06:35 +0000725% Type 9: | 1, 1, 1 |
726% | 1, 1, 1 | / 3
727% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000728%
729% The first 4 are for edge detection, the next 4 are for line detection
730% and the last is to add a average component to the results.
731%
anthonyc3cd15b2010-05-27 06:05:40 +0000732% Using a special type of '-1' will return all 9 pre-weighted kernels
733% as a multi-kernel list, so that you can use them directly (without
734% normalization) with the special "-set option:morphology:compose Plus"
735% setting to apply the full FreiChen Edge Detection Technique.
736%
anthony1dd091a2010-05-27 06:31:15 +0000737% If 'type' is large it will be taken to be an actual rotation angle for
738% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
739% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
740%
anthonye2a60ce2010-05-19 12:30:40 +0000741%
anthony602ab9b2010-01-05 08:06:50 +0000742% Boolean Kernels
743%
anthony3c10fc82010-05-13 02:40:51 +0000744% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000745% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000746% Kernel size will again be radius*2+1 square and defaults to radius 1,
747% generating a 3x3 kernel that is slightly larger than a square.
748%
anthony3c10fc82010-05-13 02:40:51 +0000749% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000750% Generate a square shaped kernel of size radius*2+1, and defaulting
751% to a 3x3 (radius 1).
752%
anthonyc1061722010-05-14 06:23:49 +0000753% Note that using a larger radius for the "Square" or the "Diamond" is
754% also equivelent to iterating the basic morphological method that many
755% times. However iterating with the smaller radius is actually faster
756% than using a larger kernel radius.
757%
758% Rectangle:{geometry}
759% Simply generate a rectangle of 1's with the size given. You can also
760% specify the location of the 'control point', otherwise the closest
761% pixel to the center of the rectangle is selected.
762%
763% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000764%
anthony3c10fc82010-05-13 02:40:51 +0000765% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000766% Generate a binary disk of the radius given, radius may be a float.
767% Kernel size will be ceil(radius)*2+1 square.
768% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000769% "Disk:1" => "diamond" or "cross:1"
770% "Disk:1.5" => "square"
771% "Disk:2" => "diamond:2"
772% "Disk:2.5" => a general disk shape of radius 2
773% "Disk:2.9" => "square:2"
774% "Disk:3.5" => default - octagonal/disk shape of radius 3
775% "Disk:4.2" => roughly octagonal shape of radius 4
776% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000777% After this all the kernel shape becomes more and more circular.
778%
779% Because a "disk" is more circular when using a larger radius, using a
780% larger radius is preferred over iterating the morphological operation.
781%
anthonyc1061722010-05-14 06:23:49 +0000782% Symbol Dilation Kernels
783%
784% These kernel is not a good general morphological kernel, but is used
785% more for highlighting and marking any single pixels in an image using,
786% a "Dilate" method as appropriate.
787%
788% For the same reasons iterating these kernels does not produce the
789% same result as using a larger radius for the symbol.
790%
anthony3c10fc82010-05-13 02:40:51 +0000791% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000792% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000793% Generate a kernel in the shape of a 'plus' or a 'cross' with
794% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000795%
796% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000797%
anthonyc1061722010-05-14 06:23:49 +0000798% Ring:{radius1},{radius2}[,{scale}]
799% A ring of the values given that falls between the two radii.
800% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
801% This is the 'edge' pixels of the default "Disk" kernel,
802% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000803%
anthony3dd0f622010-05-13 12:57:32 +0000804% Hit and Miss Kernels
805%
806% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000807% Find any peak larger than the pixels the fall between the two radii.
808% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000809% Edges
anthony1d45eb92010-05-25 11:13:23 +0000810% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000811% Corners
812% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000813% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000814% Find single pixel ridges or thin lines
815% Ridges2
816% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000817% Ridges3
818% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000819% LineEnds
820% Find end points of lines (for pruning a skeletion)
821% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000822% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000823% ConvexHull
824% Octagonal thicken kernel, to generate convex hulls of 45 degrees
825% Skeleton
826% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000827%
828% Distance Measuring Kernels
829%
anthonyc1061722010-05-14 06:23:49 +0000830% Different types of distance measuring methods, which are used with the
831% a 'Distance' morphology method for generating a gradient based on
832% distance from an edge of a binary shape, though there is a technique
833% for handling a anti-aliased shape.
834%
835% See the 'Distance' Morphological Method, for information of how it is
836% applied.
837%
anthony3dd0f622010-05-13 12:57:32 +0000838% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000839% Chebyshev Distance (also known as Tchebychev Distance) is a value of
840% one to any neighbour, orthogonal or diagonal. One why of thinking of
841% it is the number of squares a 'King' or 'Queen' in chess needs to
842% traverse reach any other position on a chess board. It results in a
843% 'square' like distance function, but one where diagonals are closer
844% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000845%
anthonyc1061722010-05-14 06:23:49 +0000846% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000847% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
848% Cab metric), is the distance needed when you can only travel in
849% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
850% in chess would travel. It results in a diamond like distances, where
851% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000852%
anthonyc1061722010-05-14 06:23:49 +0000853% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000854% Euclidean Distance is the 'direct' or 'as the crow flys distance.
855% However by default the kernel size only has a radius of 1, which
856% limits the distance to 'Knight' like moves, with only orthogonal and
857% diagonal measurements being correct. As such for the default kernel
858% you will get octagonal like distance function, which is reasonally
859% accurate.
860%
861% However if you use a larger radius such as "Euclidean:4" you will
862% get a much smoother distance gradient from the edge of the shape.
863% Of course a larger kernel is slower to use, and generally not needed.
864%
865% To allow the use of fractional distances that you get with diagonals
866% the actual distance is scaled by a fixed value which the user can
867% provide. This is not actually nessary for either ""Chebyshev" or
868% "Manhatten" distance kernels, but is done for all three distance
869% kernels. If no scale is provided it is set to a value of 100,
870% allowing for a maximum distance measurement of 655 pixels using a Q16
871% version of IM, from any edge. However for small images this can
872% result in quite a dark gradient.
873%
anthony602ab9b2010-01-05 08:06:50 +0000874*/
875
cristy2be15382010-01-21 02:38:03 +0000876MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000877 const GeometryInfo *args)
878{
cristy2be15382010-01-21 02:38:03 +0000879 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000880 *kernel;
881
cristybb503372010-05-27 20:51:26 +0000882 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000883 i;
884
cristybb503372010-05-27 20:51:26 +0000885 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000886 u,
887 v;
888
889 double
890 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
891
anthonyc1061722010-05-14 06:23:49 +0000892 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000893 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000894 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000895 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000896 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000897 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000898 break;
anthony1dd091a2010-05-27 06:31:15 +0000899 case UnityKernel: /* Named Descrete Convolution Kernels */
900 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000901 case SobelKernel:
902 case RobertsKernel:
903 case PrewittKernel:
904 case CompassKernel:
905 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000906 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000907 case CornersKernel: /* Hit and Miss kernels */
908 case LineEndsKernel:
909 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000910 case EdgesKernel:
911 case RidgesKernel:
912 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000913 case ConvexHullKernel:
914 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000915 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000916 /* A pre-generated kernel is not needed */
917 break;
anthony1dd091a2010-05-27 06:31:15 +0000918#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000919 case GaussianKernel:
920 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000921 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000922 case BlurKernel:
923 case DOBKernel:
924 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:
anthonyc1061722010-05-14 06:23:49 +0000955 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000956 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;
anthony9eb4f742010-05-18 02:45:54 +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
982 if ( type == GaussianKernel || type == DOGKernel )
983 { /* Calculate a Gaussian, OR positive half of a DOG */
984 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
anthonyc1061722010-05-14 06:23:49 +0000998 if ( type == DOGKernel )
999 { /* 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
1012 if ( type == LOGKernel )
1013 { /* 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:
anthonyc1061722010-05-14 06:23:49 +00001049 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001050 { double
anthonyc1061722010-05-14 06:23:49 +00001051 sigma = fabs(args->sigma),
1052 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001053 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001054
anthonyc1061722010-05-14 06:23:49 +00001055 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001056 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001057 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1058 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1059 else
1060 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001061 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001062 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001063 kernel->y = 0;
1064 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001065 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1066 kernel->height*sizeof(double));
1067 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001068 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001069
1070#if 1
1071#define KernelRank 3
1072 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1073 ** It generates a gaussian 3 times the width, and compresses it into
1074 ** the expected range. This produces a closer normalization of the
1075 ** resulting kernel, especially for very low sigma values.
1076 ** As such while wierd it is prefered.
1077 **
1078 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001079 **
1080 ** A properly normalized curve is generated (apart from edge clipping)
1081 ** even though we later normalize the result (for edge clipping)
1082 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001083 */
anthonyc1061722010-05-14 06:23:49 +00001084
1085 /* initialize */
cristybb503372010-05-27 20:51:26 +00001086 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001087 (void) ResetMagickMemory(kernel->values,0, (size_t)
1088 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001089 /* Calculate a Positive 1D Gaussian */
1090 if ( sigma > MagickEpsilon )
1091 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001092 A = 1.0/(2.0*sigma*sigma);
1093 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001094 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001095 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001096 }
1097 }
1098 else /* special case - generate a unity kernel */
1099 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001100
1101 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001102 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001103 {
anthonyc1061722010-05-14 06:23:49 +00001104 if ( sigma2 > MagickEpsilon )
1105 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001106 A = 1.0/(2.0*sigma*sigma);
1107 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001108 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001109 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001110 }
anthony9eb4f742010-05-18 02:45:54 +00001111 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001112 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1113 }
anthony602ab9b2010-01-05 08:06:50 +00001114#else
anthonyc1061722010-05-14 06:23:49 +00001115 /* Direct calculation without curve averaging */
1116
1117 /* Calculate a Positive Gaussian */
1118 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001119 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1120 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001121 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001122 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001123 }
1124 else /* special case - generate a unity kernel */
1125 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1126 kernel->width*kernel->height*sizeof(double));
1127 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1128 }
anthony9eb4f742010-05-18 02:45:54 +00001129
1130 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001131 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001132 {
anthonyc1061722010-05-14 06:23:49 +00001133 if ( sigma2 > MagickEpsilon )
1134 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001135 A = 1.0/(2.0*sigma*sigma);
1136 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001137 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001138 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001139 }
anthony9eb4f742010-05-18 02:45:54 +00001140 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001141 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1142 }
anthony602ab9b2010-01-05 08:06:50 +00001143#endif
anthonyc1061722010-05-14 06:23:49 +00001144 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001145 ** radius, producing a smaller (darker) kernel. Also for very small
1146 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1147 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001148 **
1149 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001150 */
anthonycc6c8362010-01-25 04:14:01 +00001151
anthony602ab9b2010-01-05 08:06:50 +00001152 /* Normalize the 1D Gaussian Kernel
1153 **
anthonyc1061722010-05-14 06:23:49 +00001154 ** NB: a CorrelateNormalize performs a normal Normalize if
1155 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001156 */
anthony46a369d2010-05-19 02:41:48 +00001157 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1158 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001159
anthonyc1061722010-05-14 06:23:49 +00001160 /* rotate the 1D kernel by given angle */
1161 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001162 break;
1163 }
1164 case CometKernel:
1165 { double
anthony9eb4f742010-05-18 02:45:54 +00001166 sigma = fabs(args->sigma),
1167 A;
anthony602ab9b2010-01-05 08:06:50 +00001168
anthony602ab9b2010-01-05 08:06:50 +00001169 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001170 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001171 else
cristybb503372010-05-27 20:51:26 +00001172 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001173 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001174 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001175 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001176 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1177 kernel->height*sizeof(double));
1178 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001179 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001180
anthonyc1061722010-05-14 06:23:49 +00001181 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001182 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001183 ** curve to use so may change in the future. The function must be
1184 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001185 **
1186 ** As we are normalizing and not subtracting gaussians,
1187 ** there is no need for a divisor in the gaussian formula
1188 **
anthony43c49252010-05-18 10:59:50 +00001189 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001190 */
anthony9eb4f742010-05-18 02:45:54 +00001191 if ( sigma > MagickEpsilon )
1192 {
anthony602ab9b2010-01-05 08:06:50 +00001193#if 1
1194#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001195 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001196 (void) ResetMagickMemory(kernel->values,0, (size_t)
1197 kernel->width*sizeof(double));
1198 sigma *= KernelRank; /* simplify the loop expression */
1199 A = 1.0/(2.0*sigma*sigma);
1200 /* B = 1.0/(MagickSQ2PI*sigma); */
1201 for ( u=0; u < v; u++) {
1202 kernel->values[u/KernelRank] +=
1203 exp(-((double)(u*u))*A);
1204 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1205 }
cristybb503372010-05-27 20:51:26 +00001206 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001207 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001208#else
anthony9eb4f742010-05-18 02:45:54 +00001209 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1210 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001211 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001212 kernel->positive_range +=
1213 kernel->values[i] =
1214 exp(-((double)(i*i))*A);
1215 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001216#endif
anthony9eb4f742010-05-18 02:45:54 +00001217 }
1218 else /* special case - generate a unity kernel */
1219 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1220 kernel->width*kernel->height*sizeof(double));
1221 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1222 kernel->positive_range = 1.0;
1223 }
anthony46a369d2010-05-19 02:41:48 +00001224
1225 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001226 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001227 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001228
anthony999bb2c2010-02-18 12:38:01 +00001229 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1230 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001231 break;
1232 }
anthonyc1061722010-05-14 06:23:49 +00001233
anthony3c10fc82010-05-13 02:40:51 +00001234 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001235 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001236 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001237 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001238 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001239 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001240 break;
anthony9eb4f742010-05-18 02:45:54 +00001241 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001242 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001243 break;
1244 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001245 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1246 break;
1247 case 3:
anthonyc1061722010-05-14 06:23:49 +00001248 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001249 break;
anthony9eb4f742010-05-18 02:45:54 +00001250 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001251 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001252 "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 +00001253 break;
anthony9eb4f742010-05-18 02:45:54 +00001254 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001255 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001256 "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 +00001257 break;
anthony43c49252010-05-18 10:59:50 +00001258 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001259 kernel=ParseKernelArray(
1260 "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");
1261 break;
anthony43c49252010-05-18 10:59:50 +00001262 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1263 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1264 kernel=ParseKernelArray(
1265 "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");
1266 break;
anthony3c10fc82010-05-13 02:40:51 +00001267 }
1268 if (kernel == (KernelInfo *) NULL)
1269 return(kernel);
1270 kernel->type = type;
1271 break;
1272 }
anthonyc1061722010-05-14 06:23:49 +00001273 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001274 {
anthonyc1061722010-05-14 06:23:49 +00001275 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1276 if (kernel == (KernelInfo *) NULL)
1277 return(kernel);
1278 kernel->type = type;
1279 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1280 break;
1281 }
1282 case RobertsKernel:
1283 {
1284 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1285 if (kernel == (KernelInfo *) NULL)
1286 return(kernel);
1287 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001288 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001289 break;
1290 }
1291 case PrewittKernel:
1292 {
1293 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1294 if (kernel == (KernelInfo *) NULL)
1295 return(kernel);
1296 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001297 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001298 break;
1299 }
1300 case CompassKernel:
1301 {
1302 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1303 if (kernel == (KernelInfo *) NULL)
1304 return(kernel);
1305 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001306 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001307 break;
1308 }
anthony9eb4f742010-05-18 02:45:54 +00001309 case KirschKernel:
1310 {
1311 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1312 if (kernel == (KernelInfo *) NULL)
1313 return(kernel);
1314 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001315 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001316 break;
1317 }
anthonye2a60ce2010-05-19 12:30:40 +00001318 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001319 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1320 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001321 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001322 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001323 case 0:
1324 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1325 if (kernel == (KernelInfo *) NULL)
1326 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001327 kernel->values[3] = -MagickSQ2;
1328 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001329 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001330 break;
anthonye2a60ce2010-05-19 12:30:40 +00001331 case 1:
1332 kernel=ParseKernelArray("3: 1,2,1 0,0,0 -1,2,-1");
1333 if (kernel == (KernelInfo *) NULL)
1334 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001335 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001336 kernel->values[1] = +MagickSQ2;
1337 kernel->values[7] = -MagickSQ2;
1338 CalcKernelMetaData(kernel); /* recalculate meta-data */
1339 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1340 break;
1341 case 2:
1342 kernel=ParseKernelArray("3: 1,0,1 2,0,2 1,0,1");
1343 if (kernel == (KernelInfo *) NULL)
1344 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001345 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001346 kernel->values[3] = +MagickSQ2;
1347 kernel->values[5] = +MagickSQ2;
1348 CalcKernelMetaData(kernel);
1349 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1350 break;
1351 case 3:
1352 kernel=ParseKernelArray("3: 0,-1,2 1,0,-1 -2,1,0");
1353 if (kernel == (KernelInfo *) NULL)
1354 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001355 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001356 kernel->values[2] = +MagickSQ2;
1357 kernel->values[6] = -MagickSQ2;
1358 CalcKernelMetaData(kernel);
1359 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1360 break;
1361 case 4:
anthony6915d062010-05-19 12:45:51 +00001362 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001363 if (kernel == (KernelInfo *) NULL)
1364 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001365 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001366 kernel->values[0] = +MagickSQ2;
1367 kernel->values[8] = -MagickSQ2;
1368 CalcKernelMetaData(kernel);
1369 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1370 break;
1371 case 5:
1372 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1373 if (kernel == (KernelInfo *) NULL)
1374 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001375 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001376 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1377 break;
1378 case 6:
1379 kernel=ParseKernelArray("3: -1,0,1 0,0,0 1,0,-1");
1380 if (kernel == (KernelInfo *) NULL)
1381 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001382 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001383 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1384 break;
1385 case 7:
anthonyf4e00312010-05-20 12:06:35 +00001386 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001387 if (kernel == (KernelInfo *) NULL)
1388 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001389 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001390 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1391 break;
1392 case 8:
1393 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1394 if (kernel == (KernelInfo *) NULL)
1395 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001396 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001397 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1398 break;
1399 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001400 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001401 if (kernel == (KernelInfo *) NULL)
1402 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001403 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001404 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1405 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001406 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001407 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1408 if (kernel == (KernelInfo *) NULL)
1409 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001410 break;
anthonye2a60ce2010-05-19 12:30:40 +00001411 }
anthonyc3cd15b2010-05-27 06:05:40 +00001412 if ( fabs(args->sigma) > MagickEpsilon )
1413 /* Rotate by correctly supplied 'angle' */
1414 RotateKernelInfo(kernel, args->sigma);
1415 else if ( args->rho > 30.0 || args->rho < -30.0 )
1416 /* Rotate by out of bounds 'type' */
1417 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001418 break;
1419 }
1420
anthonyc1061722010-05-14 06:23:49 +00001421 /* Boolean Kernels */
1422 case DiamondKernel:
1423 {
1424 if (args->rho < 1.0)
1425 kernel->width = kernel->height = 3; /* default radius = 1 */
1426 else
cristybb503372010-05-27 20:51:26 +00001427 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1428 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001429
1430 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1431 kernel->height*sizeof(double));
1432 if (kernel->values == (double *) NULL)
1433 return(DestroyKernelInfo(kernel));
1434
1435 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001436 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1437 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyecd0ab52010-05-30 14:59:20 +00001438 if ((ssize_t) (labs((long) u)+labs((long) v)) <= kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001439 kernel->positive_range += kernel->values[i] = args->sigma;
1440 else
1441 kernel->values[i] = nan;
1442 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1443 break;
1444 }
1445 case SquareKernel:
1446 case RectangleKernel:
1447 { double
1448 scale;
anthony602ab9b2010-01-05 08:06:50 +00001449 if ( type == SquareKernel )
1450 {
1451 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001452 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001453 else
cristybb503372010-05-27 20:51:26 +00001454 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1455 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001456 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001457 }
1458 else {
cristy2be15382010-01-21 02:38:03 +00001459 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001460 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001461 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001462 kernel->width = (size_t)args->rho;
1463 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001464 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1465 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001466 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001467 kernel->x = (ssize_t) args->xi;
1468 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001469 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001470 }
1471 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1472 kernel->height*sizeof(double));
1473 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001474 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001475
anthony3dd0f622010-05-13 12:57:32 +00001476 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001477 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001478 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001479 kernel->values[i] = scale;
1480 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1481 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001482 break;
anthony602ab9b2010-01-05 08:06:50 +00001483 }
anthony602ab9b2010-01-05 08:06:50 +00001484 case DiskKernel:
1485 {
anthonye4d89962010-05-29 10:53:11 +00001486 ssize_t
1487 limit = (ssize_t)(args->rho*args->rho);
1488
1489 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001490 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001491 else
anthonye4d89962010-05-29 10:53:11 +00001492 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001493 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001494
1495 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1496 kernel->height*sizeof(double));
1497 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001498 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001499
anthony3dd0f622010-05-13 12:57:32 +00001500 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001501 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1502 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001503 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001504 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001505 else
1506 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001507 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001508 break;
1509 }
1510 case PlusKernel:
1511 {
1512 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001513 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001514 else
cristybb503372010-05-27 20:51:26 +00001515 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1516 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001517
1518 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1519 kernel->height*sizeof(double));
1520 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001521 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001522
cristycee97112010-05-28 00:44:52 +00001523 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001524 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1525 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001526 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1527 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1528 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001529 break;
1530 }
anthony3dd0f622010-05-13 12:57:32 +00001531 case CrossKernel:
1532 {
1533 if (args->rho < 1.0)
1534 kernel->width = kernel->height = 5; /* default radius 2 */
1535 else
cristybb503372010-05-27 20:51:26 +00001536 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1537 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001538
1539 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1540 kernel->height*sizeof(double));
1541 if (kernel->values == (double *) NULL)
1542 return(DestroyKernelInfo(kernel));
1543
cristycee97112010-05-28 00:44:52 +00001544 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001545 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1546 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001547 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1548 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1549 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1550 break;
1551 }
1552 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001553 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001554 case PeaksKernel:
1555 {
cristybb503372010-05-27 20:51:26 +00001556 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001557 limit1,
anthonyc1061722010-05-14 06:23:49 +00001558 limit2,
1559 scale;
anthony3dd0f622010-05-13 12:57:32 +00001560
1561 if (args->rho < args->sigma)
1562 {
cristybb503372010-05-27 20:51:26 +00001563 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001564 limit1 = (ssize_t)(args->rho*args->rho);
1565 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001566 }
1567 else
1568 {
cristybb503372010-05-27 20:51:26 +00001569 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001570 limit1 = (ssize_t)(args->sigma*args->sigma);
1571 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001572 }
anthonyc1061722010-05-14 06:23:49 +00001573 if ( limit2 <= 0 )
1574 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1575
anthony3dd0f622010-05-13 12:57:32 +00001576 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001577 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001578 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1579 kernel->height*sizeof(double));
1580 if (kernel->values == (double *) NULL)
1581 return(DestroyKernelInfo(kernel));
1582
anthonyc1061722010-05-14 06:23:49 +00001583 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001584 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1585 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1586 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1587 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001588 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001589 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001590 else
1591 kernel->values[i] = nan;
1592 }
cristye96405a2010-05-19 02:24:31 +00001593 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001594 if ( type == PeaksKernel ) {
1595 /* set the central point in the middle */
1596 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1597 kernel->positive_range = 1.0;
1598 kernel->maximum = 1.0;
1599 }
anthony3dd0f622010-05-13 12:57:32 +00001600 break;
1601 }
anthony43c49252010-05-18 10:59:50 +00001602 case EdgesKernel:
1603 {
1604 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1605 if (kernel == (KernelInfo *) NULL)
1606 return(kernel);
1607 kernel->type = type;
1608 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1609 break;
1610 }
anthony3dd0f622010-05-13 12:57:32 +00001611 case CornersKernel:
1612 {
anthony4f1dcb72010-05-14 08:43:10 +00001613 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001614 if (kernel == (KernelInfo *) NULL)
1615 return(kernel);
1616 kernel->type = type;
1617 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1618 break;
1619 }
anthony47f5d062010-05-23 07:47:50 +00001620 case RidgesKernel:
1621 {
anthony24a19842010-05-27 12:18:34 +00001622 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001623 if (kernel == (KernelInfo *) NULL)
1624 return(kernel);
1625 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001626 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001627 break;
1628 }
anthony1d45eb92010-05-25 11:13:23 +00001629 case Ridges2Kernel:
1630 {
1631 KernelInfo
1632 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001633 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001634 if (kernel == (KernelInfo *) NULL)
1635 return(kernel);
1636 kernel->type = type;
1637 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001638#if 0
1639 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001640 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1641 if (new_kernel == (KernelInfo *) NULL)
1642 return(DestroyKernelInfo(kernel));
1643 new_kernel->type = type;
1644 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1645 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001646#endif
1647 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1648 /* Unfortunatally we can not yet rotate a non-square kernel */
1649 /* But then we can't flip a non-symetrical kernel either */
1650 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1651 if (new_kernel == (KernelInfo *) NULL)
1652 return(DestroyKernelInfo(kernel));
1653 new_kernel->type = type;
1654 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001655 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001656 if (new_kernel == (KernelInfo *) NULL)
1657 return(DestroyKernelInfo(kernel));
1658 new_kernel->type = type;
1659 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001660 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001661 if (new_kernel == (KernelInfo *) NULL)
1662 return(DestroyKernelInfo(kernel));
1663 new_kernel->type = type;
1664 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001665 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001666 if (new_kernel == (KernelInfo *) NULL)
1667 return(DestroyKernelInfo(kernel));
1668 new_kernel->type = type;
1669 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001670 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001671 if (new_kernel == (KernelInfo *) NULL)
1672 return(DestroyKernelInfo(kernel));
1673 new_kernel->type = type;
1674 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001675 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001676 if (new_kernel == (KernelInfo *) NULL)
1677 return(DestroyKernelInfo(kernel));
1678 new_kernel->type = type;
1679 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001680 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001681 if (new_kernel == (KernelInfo *) NULL)
1682 return(DestroyKernelInfo(kernel));
1683 new_kernel->type = type;
1684 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001685 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001686 if (new_kernel == (KernelInfo *) NULL)
1687 return(DestroyKernelInfo(kernel));
1688 new_kernel->type = type;
1689 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001690 break;
1691 }
anthony3dd0f622010-05-13 12:57:32 +00001692 case LineEndsKernel:
1693 {
anthony43c49252010-05-18 10:59:50 +00001694 KernelInfo
1695 *new_kernel;
1696 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001697 if (kernel == (KernelInfo *) NULL)
1698 return(kernel);
1699 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001700 ExpandKernelInfo(kernel, 90.0);
1701 /* append second set of 4 kernels */
1702 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1703 if (new_kernel == (KernelInfo *) NULL)
1704 return(DestroyKernelInfo(kernel));
1705 new_kernel->type = type;
1706 ExpandKernelInfo(new_kernel, 90.0);
1707 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001708 break;
1709 }
1710 case LineJunctionsKernel:
1711 {
1712 KernelInfo
1713 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001714 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001715 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001716 if (kernel == (KernelInfo *) NULL)
1717 return(kernel);
1718 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001719 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001720 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001721 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001722 if (new_kernel == (KernelInfo *) NULL)
1723 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001724 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001725 ExpandKernelInfo(new_kernel, 90.0);
1726 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001727 break;
1728 }
anthony3dd0f622010-05-13 12:57:32 +00001729 case ConvexHullKernel:
1730 {
anthony3928ec62010-05-27 14:03:29 +00001731 KernelInfo
1732 *new_kernel;
1733 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001734 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001735 if (kernel == (KernelInfo *) NULL)
1736 return(kernel);
1737 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001738 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001739 /* append the mirror versions too */
1740 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1741 if (new_kernel == (KernelInfo *) NULL)
1742 return(DestroyKernelInfo(kernel));
1743 new_kernel->type = type;
1744 ExpandKernelInfo(new_kernel, 45.0);
1745 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001746 break;
1747 }
anthony47f5d062010-05-23 07:47:50 +00001748 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001749 { /* what is the best form for skeletonization by thinning? */
anthonye4d89962010-05-29 10:53:11 +00001750#if 1
1751 /* Full Corner rotated to form edges too */
anthony43c49252010-05-18 10:59:50 +00001752 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001753 if (kernel == (KernelInfo *) NULL)
1754 return(kernel);
1755 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001756 ExpandKernelInfo(kernel, 45);
anthonye4d89962010-05-29 10:53:11 +00001757#endif
1758#if 0
1759 /* As last but thin the edges before looking for corners */
1760 KernelInfo
1761 *new_kernel;
1762 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1763 if (kernel == (KernelInfo *) NULL)
1764 return(kernel);
1765 kernel->type = type;
1766 ExpandKernelInfo(kernel, 90.0);
1767 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
1768 if (new_kernel == (KernelInfo *) NULL)
1769 return(DestroyKernelInfo(kernel));
1770 new_kernel->type = type;
1771 ExpandKernelInfo(new_kernel, 90.0);
1772 LastKernelInfo(kernel)->next = new_kernel;
1773#endif
1774#if 0
1775 kernel=AcquireKernelInfo("Edges;Corners");
1776#endif
1777#if 0
1778 kernel=AcquireKernelInfo("Corners;Edges");
anthony47f5d062010-05-23 07:47:50 +00001779#endif
anthony3dd0f622010-05-13 12:57:32 +00001780 break;
1781 }
anthonya648a302010-05-27 02:14:36 +00001782 case MatKernel: /* experimental - MAT from a Distance Gradient */
1783 {
1784 KernelInfo
1785 *new_kernel;
1786 /* Ridge Kernel but without the diagonal */
1787 kernel=ParseKernelArray("3x1: 0,1,0");
1788 if (kernel == (KernelInfo *) NULL)
1789 return(kernel);
1790 kernel->type = RidgesKernel;
1791 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1792 /* Plus the 2 pixel ridges kernel - no diagonal */
1793 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1794 if (new_kernel == (KernelInfo *) NULL)
1795 return(kernel);
1796 LastKernelInfo(kernel)->next = new_kernel;
1797 break;
1798 }
anthony602ab9b2010-01-05 08:06:50 +00001799 /* Distance Measuring Kernels */
1800 case ChebyshevKernel:
1801 {
anthony602ab9b2010-01-05 08:06:50 +00001802 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001803 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001804 else
cristybb503372010-05-27 20:51:26 +00001805 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1806 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001807
1808 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1809 kernel->height*sizeof(double));
1810 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001811 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001812
cristybb503372010-05-27 20:51:26 +00001813 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1814 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001815 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001816 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001817 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001818 break;
1819 }
1820 case ManhattenKernel:
1821 {
anthony602ab9b2010-01-05 08:06:50 +00001822 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001823 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001824 else
cristybb503372010-05-27 20:51:26 +00001825 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1826 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001827
1828 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1829 kernel->height*sizeof(double));
1830 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001831 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001832
cristybb503372010-05-27 20:51:26 +00001833 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1834 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001835 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001836 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001837 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001838 break;
1839 }
1840 case EuclideanKernel:
1841 {
anthony602ab9b2010-01-05 08:06:50 +00001842 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001843 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001844 else
cristybb503372010-05-27 20:51:26 +00001845 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1846 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001847
1848 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1849 kernel->height*sizeof(double));
1850 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001851 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001852
cristybb503372010-05-27 20:51:26 +00001853 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1854 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001855 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001856 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001857 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001858 break;
1859 }
anthony46a369d2010-05-19 02:41:48 +00001860 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001861 default:
anthonyc1061722010-05-14 06:23:49 +00001862 {
anthony46a369d2010-05-19 02:41:48 +00001863 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1864 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001865 if (kernel == (KernelInfo *) NULL)
1866 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001867 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001868 break;
1869 }
anthony602ab9b2010-01-05 08:06:50 +00001870 break;
1871 }
1872
1873 return(kernel);
1874}
anthonyc94cdb02010-01-06 08:15:29 +00001875
anthony602ab9b2010-01-05 08:06:50 +00001876/*
1877%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1878% %
1879% %
1880% %
cristy6771f1e2010-03-05 19:43:39 +00001881% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001882% %
1883% %
1884% %
1885%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1886%
anthony1b2bc0a2010-05-12 05:25:22 +00001887% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1888% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001889% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001890%
cristye6365592010-04-02 17:31:23 +00001891% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001892%
anthony930be612010-02-08 04:26:15 +00001893% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001894%
1895% A description of each parameter follows:
1896%
1897% o kernel: the Morphology/Convolution kernel to be cloned
1898%
1899*/
cristyef656912010-03-05 19:54:59 +00001900MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001901{
cristybb503372010-05-27 20:51:26 +00001902 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001903 i;
1904
cristy19eb6412010-04-23 14:42:29 +00001905 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001906 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001907
1908 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001909 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1910 if (new_kernel == (KernelInfo *) NULL)
1911 return(new_kernel);
1912 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001913
1914 /* replace the values with a copy of the values */
1915 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001916 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001917 if (new_kernel->values == (double *) NULL)
1918 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001919 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001920 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001921
1922 /* Also clone the next kernel in the kernel list */
1923 if ( kernel->next != (KernelInfo *) NULL ) {
1924 new_kernel->next = CloneKernelInfo(kernel->next);
1925 if ( new_kernel->next == (KernelInfo *) NULL )
1926 return(DestroyKernelInfo(new_kernel));
1927 }
1928
anthony7a01dcf2010-05-11 12:25:52 +00001929 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001930}
1931
1932/*
1933%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1934% %
1935% %
1936% %
anthony83ba99b2010-01-24 08:48:15 +00001937% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001938% %
1939% %
1940% %
1941%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1942%
anthony83ba99b2010-01-24 08:48:15 +00001943% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1944% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001945%
anthony83ba99b2010-01-24 08:48:15 +00001946% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001947%
anthony83ba99b2010-01-24 08:48:15 +00001948% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001949%
1950% A description of each parameter follows:
1951%
1952% o kernel: the Morphology/Convolution kernel to be destroyed
1953%
1954*/
1955
anthony83ba99b2010-01-24 08:48:15 +00001956MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001957{
cristy2be15382010-01-21 02:38:03 +00001958 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001959
anthony7a01dcf2010-05-11 12:25:52 +00001960 if ( kernel->next != (KernelInfo *) NULL )
1961 kernel->next = DestroyKernelInfo(kernel->next);
1962
1963 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1964 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001965 return(kernel);
1966}
anthonyc94cdb02010-01-06 08:15:29 +00001967
1968/*
1969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970% %
1971% %
1972% %
anthony3c10fc82010-05-13 02:40:51 +00001973% E x p a n d K e r n e l I n f o %
1974% %
1975% %
1976% %
1977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1978%
1979% ExpandKernelInfo() takes a single kernel, and expands it into a list
1980% of kernels each incrementally rotated the angle given.
1981%
1982% WARNING: 45 degree rotations only works for 3x3 kernels.
1983% While 90 degree roatations only works for linear and square kernels
1984%
1985% The format of the RotateKernelInfo method is:
1986%
1987% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1988%
1989% A description of each parameter follows:
1990%
1991% o kernel: the Morphology/Convolution kernel
1992%
1993% o angle: angle to rotate in degrees
1994%
1995% This function is only internel to this module, as it is not finalized,
1996% especially with regard to non-orthogonal angles, and rotation of larger
1997% 2D kernels.
1998*/
anthony47f5d062010-05-23 07:47:50 +00001999
2000/* Internal Routine - Return true if two kernels are the same */
2001static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
2002 const KernelInfo *kernel2)
2003{
cristybb503372010-05-27 20:51:26 +00002004 register size_t
anthony47f5d062010-05-23 07:47:50 +00002005 i;
anthony1d45eb92010-05-25 11:13:23 +00002006
2007 /* check size and origin location */
2008 if ( kernel1->width != kernel2->width
2009 || kernel1->height != kernel2->height
2010 || kernel1->x != kernel2->x
2011 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00002012 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002013
2014 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00002015 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00002016 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00002017 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
2018 return MagickFalse;
2019 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2020 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002021 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002022 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2023 return MagickFalse;
2024 }
anthony1d45eb92010-05-25 11:13:23 +00002025
anthony47f5d062010-05-23 07:47:50 +00002026 return MagickTrue;
2027}
2028
2029static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002030{
2031 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002032 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002033 *last;
cristya9a61ad2010-05-13 12:47:41 +00002034
anthony3c10fc82010-05-13 02:40:51 +00002035 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002036 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002037 clone = CloneKernelInfo(last);
2038 RotateKernelInfo(clone, angle);
2039 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002040 break;
cristy84d9b552010-05-24 18:23:54 +00002041 last->next = clone;
2042 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002043 }
cristy84d9b552010-05-24 18:23:54 +00002044 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002045 return;
anthony3c10fc82010-05-13 02:40:51 +00002046}
2047
2048/*
2049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2050% %
2051% %
2052% %
anthony46a369d2010-05-19 02:41:48 +00002053+ C a l c M e t a K e r n a l I n f o %
2054% %
2055% %
2056% %
2057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2058%
2059% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2060% using the kernel values. This should only ne used if it is not posible to
2061% calculate that meta-data in some easier way.
2062%
2063% It is important that the meta-data is correct before ScaleKernelInfo() is
2064% used to perform kernel normalization.
2065%
2066% The format of the CalcKernelMetaData method is:
2067%
2068% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2069%
2070% A description of each parameter follows:
2071%
2072% o kernel: the Morphology/Convolution kernel to modify
2073%
2074% WARNING: Minimum and Maximum values are assumed to include zero, even if
2075% zero is not part of the kernel (as in Gaussian Derived kernels). This
2076% however is not true for flat-shaped morphological kernels.
2077%
2078% WARNING: Only the specific kernel pointed to is modified, not a list of
2079% multiple kernels.
2080%
2081% This is an internal function and not expected to be useful outside this
2082% module. This could change however.
2083*/
2084static void CalcKernelMetaData(KernelInfo *kernel)
2085{
cristybb503372010-05-27 20:51:26 +00002086 register size_t
anthony46a369d2010-05-19 02:41:48 +00002087 i;
2088
2089 kernel->minimum = kernel->maximum = 0.0;
2090 kernel->negative_range = kernel->positive_range = 0.0;
2091 for (i=0; i < (kernel->width*kernel->height); i++)
2092 {
2093 if ( fabs(kernel->values[i]) < MagickEpsilon )
2094 kernel->values[i] = 0.0;
2095 ( kernel->values[i] < 0)
2096 ? ( kernel->negative_range += kernel->values[i] )
2097 : ( kernel->positive_range += kernel->values[i] );
2098 Minimize(kernel->minimum, kernel->values[i]);
2099 Maximize(kernel->maximum, kernel->values[i]);
2100 }
2101
2102 return;
2103}
2104
2105/*
2106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2107% %
2108% %
2109% %
anthony9eb4f742010-05-18 02:45:54 +00002110% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002111% %
2112% %
2113% %
2114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115%
anthony9eb4f742010-05-18 02:45:54 +00002116% MorphologyApply() applies a morphological method, multiple times using
2117% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002118%
anthony9eb4f742010-05-18 02:45:54 +00002119% It is basically equivelent to as MorphologyImageChannel() (see below) but
2120% without user controls, that that function extracts and applies to kernels
2121% and morphology methods.
2122%
2123% More specifically kernels are not normalized/scaled/blended by the
2124% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2125% (-bias setting or image->bias) is passed directly to this function,
2126% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002127%
anthony47f5d062010-05-23 07:47:50 +00002128% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002129%
anthony9eb4f742010-05-18 02:45:54 +00002130% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002131% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002132% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002133% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002134%
2135% A description of each parameter follows:
2136%
2137% o image: the image.
2138%
2139% o method: the morphology method to be applied.
2140%
2141% o iterations: apply the operation this many times (or no change).
2142% A value of -1 means loop until no change found.
2143% How this is applied may depend on the morphology method.
2144% Typically this is a value of 1.
2145%
2146% o channel: the channel type.
2147%
2148% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002149% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002150%
anthony47f5d062010-05-23 07:47:50 +00002151% o compose: How to handle or merge multi-kernel results.
2152% If 'Undefined' use default of the Morphology method.
2153% If 'No' force image to be re-iterated by each kernel.
2154% Otherwise merge the results using the mathematical compose
2155% method given.
2156%
2157% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002158%
anthony602ab9b2010-01-05 08:06:50 +00002159% o exception: return any errors or warnings in this structure.
2160%
anthony602ab9b2010-01-05 08:06:50 +00002161*/
2162
anthony930be612010-02-08 04:26:15 +00002163
anthony9eb4f742010-05-18 02:45:54 +00002164/* Apply a Morphology Primative to an image using the given kernel.
2165** Two pre-created images must be provided, no image is created.
2166** Returning the number of pixels that changed.
2167*/
cristybb503372010-05-27 20:51:26 +00002168static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002169 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002170 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002171{
cristy2be15382010-01-21 02:38:03 +00002172#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002173
cristy5f959472010-05-27 22:19:46 +00002174 CacheView
2175 *p_view,
2176 *q_view;
2177
cristybb503372010-05-27 20:51:26 +00002178 ssize_t
anthony29188a82010-01-22 10:12:34 +00002179 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002180 changed;
2181
2182 MagickBooleanType
2183 status;
2184
cristy5f959472010-05-27 22:19:46 +00002185 MagickOffsetType
2186 progress;
anthony602ab9b2010-01-05 08:06:50 +00002187
anthonye4d89962010-05-29 10:53:11 +00002188 assert(image != (Image *) NULL);
2189 assert(image->signature == MagickSignature);
2190 assert(result_image != (Image *) NULL);
2191 assert(result_image->signature == MagickSignature);
2192 assert(kernel != (KernelInfo *) NULL);
2193 assert(kernel->signature == MagickSignature);
2194 assert(exception != (ExceptionInfo *) NULL);
2195 assert(exception->signature == MagickSignature);
2196
anthony602ab9b2010-01-05 08:06:50 +00002197 status=MagickTrue;
2198 changed=0;
2199 progress=0;
2200
anthony602ab9b2010-01-05 08:06:50 +00002201 p_view=AcquireCacheView(image);
2202 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002203
anthonycc6c8362010-01-25 04:14:01 +00002204 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002205 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002206 */
cristyc99304f2010-02-01 15:26:27 +00002207 offx = kernel->x;
2208 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002209 switch(method) {
anthony930be612010-02-08 04:26:15 +00002210 case ConvolveMorphology:
2211 case DilateMorphology:
2212 case DilateIntensityMorphology:
2213 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002214 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002215 offx = (ssize_t) kernel->width-offx-1;
2216 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002217 break;
anthony5ef8e942010-05-11 06:51:12 +00002218 case ErodeMorphology:
2219 case ErodeIntensityMorphology:
2220 case HitAndMissMorphology:
2221 case ThinningMorphology:
2222 case ThickenMorphology:
2223 /* kernel is user as is, without reflection */
2224 break;
anthony930be612010-02-08 04:26:15 +00002225 default:
anthony9eb4f742010-05-18 02:45:54 +00002226 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002227 break;
anthony29188a82010-01-22 10:12:34 +00002228 }
2229
anthony602ab9b2010-01-05 08:06:50 +00002230#if defined(MAGICKCORE_OPENMP_SUPPORT)
2231 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2232#endif
cristybb503372010-05-27 20:51:26 +00002233 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002234 {
2235 MagickBooleanType
2236 sync;
2237
2238 register const PixelPacket
2239 *restrict p;
2240
2241 register const IndexPacket
2242 *restrict p_indexes;
2243
2244 register PixelPacket
2245 *restrict q;
2246
2247 register IndexPacket
2248 *restrict q_indexes;
2249
cristybb503372010-05-27 20:51:26 +00002250 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002251 x;
2252
cristybb503372010-05-27 20:51:26 +00002253 size_t
anthony602ab9b2010-01-05 08:06:50 +00002254 r;
2255
2256 if (status == MagickFalse)
2257 continue;
anthony29188a82010-01-22 10:12:34 +00002258 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2259 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002260 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2261 exception);
2262 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2263 {
2264 status=MagickFalse;
2265 continue;
2266 }
2267 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2268 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002269 r = (image->columns+kernel->width)*offy+offx; /* constant */
2270
cristybb503372010-05-27 20:51:26 +00002271 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002272 {
cristybb503372010-05-27 20:51:26 +00002273 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002274 v;
2275
cristybb503372010-05-27 20:51:26 +00002276 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002277 u;
2278
2279 register const double
2280 *restrict k;
2281
2282 register const PixelPacket
2283 *restrict k_pixels;
2284
2285 register const IndexPacket
2286 *restrict k_indexes;
2287
2288 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002289 result,
2290 min,
2291 max;
anthony602ab9b2010-01-05 08:06:50 +00002292
anthony29188a82010-01-22 10:12:34 +00002293 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002294 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002295 */
anthony602ab9b2010-01-05 08:06:50 +00002296 *q = p[r];
2297 if (image->colorspace == CMYKColorspace)
2298 q_indexes[x] = p_indexes[r];
2299
anthony5ef8e942010-05-11 06:51:12 +00002300 /* Defaults */
2301 min.red =
2302 min.green =
2303 min.blue =
2304 min.opacity =
2305 min.index = (MagickRealType) QuantumRange;
2306 max.red =
2307 max.green =
2308 max.blue =
2309 max.opacity =
2310 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002311 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002312 result.red = (MagickRealType) p[r].red;
2313 result.green = (MagickRealType) p[r].green;
2314 result.blue = (MagickRealType) p[r].blue;
2315 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002316 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002317 if ( image->colorspace == CMYKColorspace)
2318 result.index = (MagickRealType) p_indexes[r];
2319
anthony602ab9b2010-01-05 08:06:50 +00002320 switch (method) {
2321 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002322 /* Set the user defined bias of the weighted average output */
2323 result.red =
2324 result.green =
2325 result.blue =
2326 result.opacity =
2327 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002328 break;
anthony4fd27e22010-02-07 08:17:18 +00002329 case DilateIntensityMorphology:
2330 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002331 /* use a boolean flag indicating when first match found */
2332 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002333 break;
anthony602ab9b2010-01-05 08:06:50 +00002334 default:
anthony602ab9b2010-01-05 08:06:50 +00002335 break;
2336 }
2337
2338 switch ( method ) {
2339 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002340 /* Weighted Average of pixels using reflected kernel
2341 **
2342 ** NOTE for correct working of this operation for asymetrical
2343 ** kernels, the kernel needs to be applied in its reflected form.
2344 ** That is its values needs to be reversed.
2345 **
2346 ** Correlation is actually the same as this but without reflecting
2347 ** the kernel, and thus 'lower-level' that Convolution. However
2348 ** as Convolution is the more common method used, and it does not
2349 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002350 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002351 **
2352 ** Correlation will have its kernel reflected before calling
2353 ** this function to do a Convolve.
2354 **
2355 ** For more details of Correlation vs Convolution see
2356 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2357 */
anthony5ef8e942010-05-11 06:51:12 +00002358 if (((channel & SyncChannels) != 0 ) &&
2359 (image->matte == MagickTrue))
2360 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2361 ** Weight the color channels with Alpha Channel so that
2362 ** transparent pixels are not part of the results.
2363 */
anthony602ab9b2010-01-05 08:06:50 +00002364 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002365 alpha, /* color channel weighting : kernel*alpha */
2366 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002367
2368 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002369 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002370 k_pixels = p;
2371 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002372 for (v=0; v < (ssize_t) kernel->height; v++) {
2373 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002374 if ( IsNan(*k) ) continue;
2375 alpha=(*k)*(QuantumScale*(QuantumRange-
2376 k_pixels[u].opacity));
2377 gamma += alpha;
2378 result.red += alpha*k_pixels[u].red;
2379 result.green += alpha*k_pixels[u].green;
2380 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002381 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002382 if ( image->colorspace == CMYKColorspace)
2383 result.index += alpha*k_indexes[u];
2384 }
2385 k_pixels += image->columns+kernel->width;
2386 k_indexes += image->columns+kernel->width;
2387 }
2388 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002389 result.red *= gamma;
2390 result.green *= gamma;
2391 result.blue *= gamma;
2392 result.opacity *= gamma;
2393 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002394 }
anthony5ef8e942010-05-11 06:51:12 +00002395 else
2396 {
2397 /* No 'Sync' flag, or no Alpha involved.
2398 ** Convolution is simple individual channel weigthed sum.
2399 */
2400 k = &kernel->values[ kernel->width*kernel->height-1 ];
2401 k_pixels = p;
2402 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002403 for (v=0; v < (ssize_t) kernel->height; v++) {
2404 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002405 if ( IsNan(*k) ) continue;
2406 result.red += (*k)*k_pixels[u].red;
2407 result.green += (*k)*k_pixels[u].green;
2408 result.blue += (*k)*k_pixels[u].blue;
2409 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2410 if ( image->colorspace == CMYKColorspace)
2411 result.index += (*k)*k_indexes[u];
2412 }
2413 k_pixels += image->columns+kernel->width;
2414 k_indexes += image->columns+kernel->width;
2415 }
2416 }
anthony602ab9b2010-01-05 08:06:50 +00002417 break;
2418
anthony4fd27e22010-02-07 08:17:18 +00002419 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002420 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002421 **
2422 ** NOTE that the kernel is not reflected for this operation!
2423 **
2424 ** NOTE: in normal Greyscale Morphology, the kernel value should
2425 ** be added to the real value, this is currently not done, due to
2426 ** the nature of the boolean kernels being used.
2427 */
anthony4fd27e22010-02-07 08:17:18 +00002428 k = kernel->values;
2429 k_pixels = p;
2430 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002431 for (v=0; v < (ssize_t) kernel->height; v++) {
2432 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002433 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002434 Minimize(min.red, (double) k_pixels[u].red);
2435 Minimize(min.green, (double) k_pixels[u].green);
2436 Minimize(min.blue, (double) k_pixels[u].blue);
2437 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002438 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002439 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002440 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002441 }
2442 k_pixels += image->columns+kernel->width;
2443 k_indexes += image->columns+kernel->width;
2444 }
2445 break;
2446
anthony5ef8e942010-05-11 06:51:12 +00002447
anthony83ba99b2010-01-24 08:48:15 +00002448 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002449 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002450 **
2451 ** NOTE for correct working of this operation for asymetrical
2452 ** kernels, the kernel needs to be applied in its reflected form.
2453 ** That is its values needs to be reversed.
2454 **
2455 ** NOTE: in normal Greyscale Morphology, the kernel value should
2456 ** be added to the real value, this is currently not done, due to
2457 ** the nature of the boolean kernels being used.
2458 **
2459 */
anthony29188a82010-01-22 10:12:34 +00002460 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002461 k_pixels = p;
2462 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002463 for (v=0; v < (ssize_t) kernel->height; v++) {
2464 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002465 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002466 Maximize(max.red, (double) k_pixels[u].red);
2467 Maximize(max.green, (double) k_pixels[u].green);
2468 Maximize(max.blue, (double) k_pixels[u].blue);
2469 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002470 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002471 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002472 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002473 }
2474 k_pixels += image->columns+kernel->width;
2475 k_indexes += image->columns+kernel->width;
2476 }
anthony602ab9b2010-01-05 08:06:50 +00002477 break;
2478
anthony5ef8e942010-05-11 06:51:12 +00002479 case HitAndMissMorphology:
2480 case ThinningMorphology:
2481 case ThickenMorphology:
2482 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2483 **
2484 ** NOTE that the kernel is not reflected for this operation,
2485 ** and consists of both foreground and background pixel
2486 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2487 ** with either Nan or 0.5 values for don't care.
2488 **
2489 ** Note that this can produce negative results, though really
2490 ** only a positive match has any real value.
2491 */
2492 k = kernel->values;
2493 k_pixels = p;
2494 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002495 for (v=0; v < (ssize_t) kernel->height; v++) {
2496 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002497 if ( IsNan(*k) ) continue;
2498 if ( (*k) > 0.7 )
2499 { /* minimim of foreground pixels */
2500 Minimize(min.red, (double) k_pixels[u].red);
2501 Minimize(min.green, (double) k_pixels[u].green);
2502 Minimize(min.blue, (double) k_pixels[u].blue);
2503 Minimize(min.opacity,
2504 QuantumRange-(double) k_pixels[u].opacity);
2505 if ( image->colorspace == CMYKColorspace)
2506 Minimize(min.index, (double) k_indexes[u]);
2507 }
2508 else if ( (*k) < 0.3 )
2509 { /* maximum of background pixels */
2510 Maximize(max.red, (double) k_pixels[u].red);
2511 Maximize(max.green, (double) k_pixels[u].green);
2512 Maximize(max.blue, (double) k_pixels[u].blue);
2513 Maximize(max.opacity,
2514 QuantumRange-(double) k_pixels[u].opacity);
2515 if ( image->colorspace == CMYKColorspace)
2516 Maximize(max.index, (double) k_indexes[u]);
2517 }
2518 }
2519 k_pixels += image->columns+kernel->width;
2520 k_indexes += image->columns+kernel->width;
2521 }
2522 /* Pattern Match only if min fg larger than min bg pixels */
2523 min.red -= max.red; Maximize( min.red, 0.0 );
2524 min.green -= max.green; Maximize( min.green, 0.0 );
2525 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2526 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2527 min.index -= max.index; Maximize( min.index, 0.0 );
2528 break;
2529
anthony4fd27e22010-02-07 08:17:18 +00002530 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002531 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2532 **
2533 ** WARNING: the intensity test fails for CMYK and does not
2534 ** take into account the moderating effect of teh alpha channel
2535 ** on the intensity.
2536 **
2537 ** NOTE that the kernel is not reflected for this operation!
2538 */
anthony602ab9b2010-01-05 08:06:50 +00002539 k = kernel->values;
2540 k_pixels = p;
2541 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002542 for (v=0; v < (ssize_t) kernel->height; v++) {
2543 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002544 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002545 if ( result.red == 0.0 ||
2546 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2547 /* copy the whole pixel - no channel selection */
2548 *q = k_pixels[u];
2549 if ( result.red > 0.0 ) changed++;
2550 result.red = 1.0;
2551 }
anthony602ab9b2010-01-05 08:06:50 +00002552 }
2553 k_pixels += image->columns+kernel->width;
2554 k_indexes += image->columns+kernel->width;
2555 }
anthony602ab9b2010-01-05 08:06:50 +00002556 break;
2557
anthony83ba99b2010-01-24 08:48:15 +00002558 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002559 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2560 **
2561 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002562 ** take into account the moderating effect of the alpha channel
2563 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002564 **
2565 ** NOTE for correct working of this operation for asymetrical
2566 ** kernels, the kernel needs to be applied in its reflected form.
2567 ** That is its values needs to be reversed.
2568 */
anthony29188a82010-01-22 10:12:34 +00002569 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002570 k_pixels = p;
2571 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002572 for (v=0; v < (ssize_t) kernel->height; v++) {
2573 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002574 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2575 if ( result.red == 0.0 ||
2576 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2577 /* copy the whole pixel - no channel selection */
2578 *q = k_pixels[u];
2579 if ( result.red > 0.0 ) changed++;
2580 result.red = 1.0;
2581 }
anthony602ab9b2010-01-05 08:06:50 +00002582 }
2583 k_pixels += image->columns+kernel->width;
2584 k_indexes += image->columns+kernel->width;
2585 }
anthony602ab9b2010-01-05 08:06:50 +00002586 break;
2587
anthony5ef8e942010-05-11 06:51:12 +00002588
anthony602ab9b2010-01-05 08:06:50 +00002589 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002590 /* Add kernel Value and select the minimum value found.
2591 ** The result is a iterative distance from edge of image shape.
2592 **
2593 ** All Distance Kernels are symetrical, but that may not always
2594 ** be the case. For example how about a distance from left edges?
2595 ** To work correctly with asymetrical kernels the reflected kernel
2596 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002597 **
2598 ** Actually this is really a GreyErode with a negative kernel!
2599 **
anthony930be612010-02-08 04:26:15 +00002600 */
anthony29188a82010-01-22 10:12:34 +00002601 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002602 k_pixels = p;
2603 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002604 for (v=0; v < (ssize_t) kernel->height; v++) {
2605 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002606 if ( IsNan(*k) ) continue;
2607 Minimize(result.red, (*k)+k_pixels[u].red);
2608 Minimize(result.green, (*k)+k_pixels[u].green);
2609 Minimize(result.blue, (*k)+k_pixels[u].blue);
2610 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2611 if ( image->colorspace == CMYKColorspace)
2612 Minimize(result.index, (*k)+k_indexes[u]);
2613 }
2614 k_pixels += image->columns+kernel->width;
2615 k_indexes += image->columns+kernel->width;
2616 }
anthony602ab9b2010-01-05 08:06:50 +00002617 break;
2618
2619 case UndefinedMorphology:
2620 default:
2621 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002622 }
anthony5ef8e942010-05-11 06:51:12 +00002623 /* Final mathematics of results (combine with original image?)
2624 **
2625 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2626 ** be done here but works better with iteration as a image difference
2627 ** in the controling function (below). Thicken and Thinning however
2628 ** should be done here so thay can be iterated correctly.
2629 */
2630 switch ( method ) {
2631 case HitAndMissMorphology:
2632 case ErodeMorphology:
2633 result = min; /* minimum of neighbourhood */
2634 break;
2635 case DilateMorphology:
2636 result = max; /* maximum of neighbourhood */
2637 break;
2638 case ThinningMorphology:
2639 /* subtract pattern match from original */
2640 result.red -= min.red;
2641 result.green -= min.green;
2642 result.blue -= min.blue;
2643 result.opacity -= min.opacity;
2644 result.index -= min.index;
2645 break;
2646 case ThickenMorphology:
2647 /* Union with original image (maximize) - or should this be + */
2648 Maximize( result.red, min.red );
2649 Maximize( result.green, min.green );
2650 Maximize( result.blue, min.blue );
2651 Maximize( result.opacity, min.opacity );
2652 Maximize( result.index, min.index );
2653 break;
2654 default:
2655 /* result directly calculated or assigned */
2656 break;
2657 }
2658 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002659 switch ( method ) {
2660 case UndefinedMorphology:
2661 case DilateIntensityMorphology:
2662 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002663 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002664 default:
anthony83ba99b2010-01-24 08:48:15 +00002665 if ((channel & RedChannel) != 0)
2666 q->red = ClampToQuantum(result.red);
2667 if ((channel & GreenChannel) != 0)
2668 q->green = ClampToQuantum(result.green);
2669 if ((channel & BlueChannel) != 0)
2670 q->blue = ClampToQuantum(result.blue);
2671 if ((channel & OpacityChannel) != 0
2672 && image->matte == MagickTrue )
2673 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2674 if ((channel & IndexChannel) != 0
2675 && image->colorspace == CMYKColorspace)
2676 q_indexes[x] = ClampToQuantum(result.index);
2677 break;
2678 }
anthony5ef8e942010-05-11 06:51:12 +00002679 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002680 if ( ( p[r].red != q->red )
2681 || ( p[r].green != q->green )
2682 || ( p[r].blue != q->blue )
2683 || ( p[r].opacity != q->opacity )
2684 || ( image->colorspace == CMYKColorspace &&
2685 p_indexes[r] != q_indexes[x] ) )
2686 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002687 p++;
2688 q++;
anthony83ba99b2010-01-24 08:48:15 +00002689 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002690 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2691 if (sync == MagickFalse)
2692 status=MagickFalse;
2693 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2694 {
2695 MagickBooleanType
2696 proceed;
2697
2698#if defined(MAGICKCORE_OPENMP_SUPPORT)
2699 #pragma omp critical (MagickCore_MorphologyImage)
2700#endif
2701 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2702 if (proceed == MagickFalse)
2703 status=MagickFalse;
2704 }
anthony83ba99b2010-01-24 08:48:15 +00002705 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002706 result_image->type=image->type;
2707 q_view=DestroyCacheView(q_view);
2708 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002709 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002710}
2711
anthony4fd27e22010-02-07 08:17:18 +00002712
anthony9eb4f742010-05-18 02:45:54 +00002713MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002714 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002715 const KernelInfo *kernel, const CompositeOperator compose,
2716 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002717{
2718 Image
anthony47f5d062010-05-23 07:47:50 +00002719 *curr_image, /* Image we are working with or iterating */
2720 *work_image, /* secondary image for primative iteration */
2721 *save_image, /* saved image - for 'edge' method only */
2722 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002723
anthony4fd27e22010-02-07 08:17:18 +00002724 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002725 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2726 *norm_kernel, /* the current normal un-reflected kernel */
2727 *rflt_kernel, /* the current reflected kernel (if needed) */
2728 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002729
2730 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002731 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002732
2733 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002734 rslt_compose; /* multi-kernel compose method for results to use */
2735
2736 MagickBooleanType
2737 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002738
cristybb503372010-05-27 20:51:26 +00002739 size_t
anthony47f5d062010-05-23 07:47:50 +00002740 method_loop, /* Loop 1: number of compound method iterations */
2741 method_limit, /* maximum number of compound method iterations */
2742 kernel_number, /* Loop 2: the kernel number being applied */
2743 stage_loop, /* Loop 3: primative loop for compound morphology */
2744 stage_limit, /* how many primatives in this compound */
2745 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2746 kernel_limit, /* number of times to iterate kernel */
2747 count, /* total count of primative steps applied */
2748 changed, /* number pixels changed by last primative operation */
2749 kernel_changed, /* total count of changed using iterated kernel */
2750 method_changed; /* total count of changed over method iteration */
2751
2752 char
2753 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002754
anthony602ab9b2010-01-05 08:06:50 +00002755 assert(image != (Image *) NULL);
2756 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002757 assert(kernel != (KernelInfo *) NULL);
2758 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002759 assert(exception != (ExceptionInfo *) NULL);
2760 assert(exception->signature == MagickSignature);
2761
anthonyc3e48252010-05-24 12:43:11 +00002762 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002763 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002764 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002765
cristybb503372010-05-27 20:51:26 +00002766 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002767 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2768 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002769
cristye96405a2010-05-19 02:24:31 +00002770 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2771 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002772
anthony9eb4f742010-05-18 02:45:54 +00002773 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002774 curr_image = (Image *) image;
2775 work_image = save_image = rslt_image = (Image *) NULL;
2776 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002777
anthony47f5d062010-05-23 07:47:50 +00002778 /* Initialize specific methods
2779 * + which loop should use the given iteratations
2780 * + how many primatives make up the compound morphology
2781 * + multi-kernel compose method to use (by default)
2782 */
2783 method_limit = 1; /* just do method once, unless otherwise set */
2784 stage_limit = 1; /* assume method is not a compount */
2785 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002786 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002787 case SmoothMorphology: /* 4 primative compound morphology */
2788 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002789 break;
anthony47f5d062010-05-23 07:47:50 +00002790 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002791 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002792 case TopHatMorphology:
2793 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002794 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002795 case BottomHatMorphology:
2796 case EdgeMorphology:
2797 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002798 break;
2799 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002800 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002801 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002802 break;
anthonyc3e48252010-05-24 12:43:11 +00002803 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002804 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002805 method_limit = kernel_limit; /* iterate method with each kernel */
2806 kernel_limit = 1; /* do not do kernel iteration */
anthonye4d89962010-05-29 10:53:11 +00002807 case DistanceMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002808 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002809 break;
2810 default:
anthony930be612010-02-08 04:26:15 +00002811 break;
anthony602ab9b2010-01-05 08:06:50 +00002812 }
2813
anthonyc3e48252010-05-24 12:43:11 +00002814 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002815 if ( compose != UndefinedCompositeOp )
2816 rslt_compose = compose; /* override default composition for method */
2817 if ( rslt_compose == UndefinedCompositeOp )
2818 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2819
anthonyc3e48252010-05-24 12:43:11 +00002820 /* Some methods require a reflected kernel to use with primatives.
2821 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002822 switch ( method ) {
2823 case CorrelateMorphology:
2824 case CloseMorphology:
2825 case CloseIntensityMorphology:
2826 case BottomHatMorphology:
2827 case SmoothMorphology:
2828 reflected_kernel = CloneKernelInfo(kernel);
2829 if (reflected_kernel == (KernelInfo *) NULL)
2830 goto error_cleanup;
2831 RotateKernelInfo(reflected_kernel,180);
2832 break;
2833 default:
2834 break;
anthony9eb4f742010-05-18 02:45:54 +00002835 }
anthony7a01dcf2010-05-11 12:25:52 +00002836
anthony47f5d062010-05-23 07:47:50 +00002837 /* Loop 1: iterate the compound method */
2838 method_loop = 0;
2839 method_changed = 1;
2840 while ( method_loop < method_limit && method_changed > 0 ) {
2841 method_loop++;
2842 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002843
anthony47f5d062010-05-23 07:47:50 +00002844 /* Loop 2: iterate over each kernel in a multi-kernel list */
2845 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00002846 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00002847 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00002848
anthony47f5d062010-05-23 07:47:50 +00002849 kernel_number = 0;
2850 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002851
anthony47f5d062010-05-23 07:47:50 +00002852 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2853 stage_loop = 0; /* the compound morphology stage number */
2854 while ( stage_loop < stage_limit ) {
2855 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002856
anthony47f5d062010-05-23 07:47:50 +00002857 /* Select primative morphology for this stage of compound method */
2858 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002859 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002860 switch( method ) {
2861 case ErodeMorphology: /* just erode */
2862 case EdgeInMorphology: /* erode and image difference */
2863 primative = ErodeMorphology;
2864 break;
2865 case DilateMorphology: /* just dilate */
2866 case EdgeOutMorphology: /* dilate and image difference */
2867 primative = DilateMorphology;
2868 break;
2869 case OpenMorphology: /* erode then dialate */
2870 case TopHatMorphology: /* open and image difference */
2871 primative = ErodeMorphology;
2872 if ( stage_loop == 2 )
2873 primative = DilateMorphology;
2874 break;
2875 case OpenIntensityMorphology:
2876 primative = ErodeIntensityMorphology;
2877 if ( stage_loop == 2 )
2878 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00002879 break;
anthony47f5d062010-05-23 07:47:50 +00002880 case CloseMorphology: /* dilate, then erode */
2881 case BottomHatMorphology: /* close and image difference */
2882 this_kernel = rflt_kernel; /* use the reflected kernel */
2883 primative = DilateMorphology;
2884 if ( stage_loop == 2 )
2885 primative = ErodeMorphology;
2886 break;
2887 case CloseIntensityMorphology:
2888 this_kernel = rflt_kernel; /* use the reflected kernel */
2889 primative = DilateIntensityMorphology;
2890 if ( stage_loop == 2 )
2891 primative = ErodeIntensityMorphology;
2892 break;
2893 case SmoothMorphology: /* open, close */
2894 switch ( stage_loop ) {
2895 case 1: /* start an open method, which starts with Erode */
2896 primative = ErodeMorphology;
2897 break;
2898 case 2: /* now Dilate the Erode */
2899 primative = DilateMorphology;
2900 break;
2901 case 3: /* Reflect kernel a close */
2902 this_kernel = rflt_kernel; /* use the reflected kernel */
2903 primative = DilateMorphology;
2904 break;
2905 case 4: /* Finish the Close */
2906 this_kernel = rflt_kernel; /* use the reflected kernel */
2907 primative = ErodeMorphology;
2908 break;
2909 }
2910 break;
2911 case EdgeMorphology: /* dilate and erode difference */
2912 primative = DilateMorphology;
2913 if ( stage_loop == 2 ) {
2914 save_image = curr_image; /* save the image difference */
2915 curr_image = (Image *) image;
2916 primative = ErodeMorphology;
2917 }
2918 break;
2919 case CorrelateMorphology:
2920 /* A Correlation is a Convolution with a reflected kernel.
2921 ** However a Convolution is a weighted sum using a reflected
2922 ** kernel. It may seem stange to convert a Correlation into a
2923 ** Convolution as the Correlation is the simplier method, but
2924 ** Convolution is much more commonly used, and it makes sense to
2925 ** implement it directly so as to avoid the need to duplicate the
2926 ** kernel when it is not required (which is typically the
2927 ** default).
2928 */
2929 this_kernel = rflt_kernel; /* use the reflected kernel */
2930 primative = ConvolveMorphology;
2931 break;
2932 default:
anthony47f5d062010-05-23 07:47:50 +00002933 break;
2934 }
anthonye4d89962010-05-29 10:53:11 +00002935 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00002936
anthony47f5d062010-05-23 07:47:50 +00002937 /* Extra information for debugging compound operations */
2938 if ( verbose == MagickTrue ) {
2939 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002940 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002941 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2942 (unsigned long) method_loop,(unsigned long) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00002943 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002944 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002945 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2946 (unsigned long) method_loop);
anthony47f5d062010-05-23 07:47:50 +00002947 else
2948 v_info[0] = '\0';
2949 }
2950
2951 /* Loop 4: Iterate the kernel with primative */
2952 kernel_loop = 0;
2953 kernel_changed = 0;
2954 changed = 1;
2955 while ( kernel_loop < kernel_limit && changed > 0 ) {
2956 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002957
2958 /* Create a destination image, if not yet defined */
2959 if ( work_image == (Image *) NULL )
2960 {
2961 work_image=CloneImage(image,0,0,MagickTrue,exception);
2962 if (work_image == (Image *) NULL)
2963 goto error_cleanup;
2964 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2965 {
2966 InheritException(exception,&work_image->exception);
2967 goto error_cleanup;
2968 }
2969 }
2970
anthonye4d89962010-05-29 10:53:11 +00002971 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002972 count++;
anthony47f5d062010-05-23 07:47:50 +00002973 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002974 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002975 kernel_changed += changed;
2976 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002977
anthony47f5d062010-05-23 07:47:50 +00002978 if ( verbose == MagickTrue ) {
2979 if ( kernel_loop > 1 )
2980 fprintf(stderr, "\n"); /* add end-of-line from previous */
2981 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2982 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2983 ( this_kernel == rflt_kernel ) ? "*" : "",
cristyf2faecf2010-05-28 19:19:36 +00002984 (unsigned long) method_loop+kernel_loop-1,(unsigned long)
2985 kernel_number,(unsigned long) count,(unsigned long) changed);
anthony47f5d062010-05-23 07:47:50 +00002986 }
anthony9eb4f742010-05-18 02:45:54 +00002987 /* prepare next loop */
2988 { Image *tmp = work_image; /* swap images for iteration */
2989 work_image = curr_image;
2990 curr_image = tmp;
2991 }
2992 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002993 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002994
anthony47f5d062010-05-23 07:47:50 +00002995 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002996
anthony47f5d062010-05-23 07:47:50 +00002997 if ( verbose == MagickTrue && kernel_changed != changed )
cristyf2faecf2010-05-28 19:19:36 +00002998 fprintf(stderr, " Total %lu",(unsigned long) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00002999 if ( verbose == MagickTrue && stage_loop < stage_limit )
3000 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00003001
3002#if 0
anthonye4d89962010-05-29 10:53:11 +00003003 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
3004 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
3005 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
3006 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
3007 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003008#endif
3009
anthony47f5d062010-05-23 07:47:50 +00003010 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00003011
anthony47f5d062010-05-23 07:47:50 +00003012 /* Final Post-processing for some Compound Methods
3013 **
3014 ** The removal of any 'Sync' channel flag in the Image Compositon
3015 ** below ensures the methematical compose method is applied in a
3016 ** purely mathematical way, and only to the selected channels.
3017 ** Turn off SVG composition 'alpha blending'.
3018 */
3019 switch( method ) {
3020 case EdgeOutMorphology:
3021 case EdgeInMorphology:
3022 case TopHatMorphology:
3023 case BottomHatMorphology:
3024 if ( verbose == MagickTrue )
3025 fprintf(stderr, "\n%s: Difference with original image",
3026 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3027 (void) CompositeImageChannel(curr_image,
3028 (ChannelType) (channel & ~SyncChannels),
3029 DifferenceCompositeOp, image, 0, 0);
3030 break;
3031 case EdgeMorphology:
3032 if ( verbose == MagickTrue )
3033 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3034 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3035 (void) CompositeImageChannel(curr_image,
3036 (ChannelType) (channel & ~SyncChannels),
3037 DifferenceCompositeOp, save_image, 0, 0);
3038 save_image = DestroyImage(save_image); /* finished with save image */
3039 break;
3040 default:
3041 break;
3042 }
3043
3044 /* multi-kernel handling: re-iterate, or compose results */
3045 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003046 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003047 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003048 { if ( verbose == MagickTrue ) {
3049 if ( this_kernel->next != (KernelInfo *) NULL )
3050 fprintf(stderr, " (re-iterate)");
3051 else
3052 fprintf(stderr, " (done)");
3053 }
3054 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003055 }
anthony47f5d062010-05-23 07:47:50 +00003056 else if ( rslt_image == (Image *) NULL)
3057 { if ( verbose == MagickTrue )
3058 fprintf(stderr, " (save for compose)");
3059 rslt_image = curr_image;
3060 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003061 }
anthony47f5d062010-05-23 07:47:50 +00003062 else
3063 { /* add the new 'current' result to the composition
3064 **
3065 ** The removal of any 'Sync' channel flag in the Image Compositon
3066 ** below ensures the methematical compose method is applied in a
3067 ** purely mathematical way, and only to the selected channels.
3068 ** Turn off SVG composition 'alpha blending'.
3069 */
3070 if ( verbose == MagickTrue )
3071 fprintf(stderr, " (compose \"%s\")",
3072 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3073 (void) CompositeImageChannel(rslt_image,
3074 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3075 curr_image, 0, 0);
3076 curr_image = (Image *) image; /* continue with original image */
3077 }
3078 if ( verbose == MagickTrue )
3079 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003080
anthony47f5d062010-05-23 07:47:50 +00003081 /* loop to the next kernel in a multi-kernel list */
3082 norm_kernel = norm_kernel->next;
3083 if ( rflt_kernel != (KernelInfo *) NULL )
3084 rflt_kernel = rflt_kernel->next;
3085 kernel_number++;
3086 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003087
anthony47f5d062010-05-23 07:47:50 +00003088 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003089
anthony9eb4f742010-05-18 02:45:54 +00003090 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003091
anthony47f5d062010-05-23 07:47:50 +00003092 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003093error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003094 if ( curr_image != (Image *) NULL &&
3095 curr_image != rslt_image &&
3096 curr_image != image )
3097 curr_image = DestroyImage(curr_image);
3098 if ( rslt_image != (Image *) NULL )
3099 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003100exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003101 if ( curr_image != (Image *) NULL &&
3102 curr_image != rslt_image &&
3103 curr_image != image )
3104 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003105 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003106 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003107 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003108 save_image = DestroyImage(save_image);
3109 if ( reflected_kernel != (KernelInfo *) NULL )
3110 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3111 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003112}
3113
3114/*
3115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3116% %
3117% %
3118% %
3119% M o r p h o l o g y I m a g e C h a n n e l %
3120% %
3121% %
3122% %
3123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3124%
3125% MorphologyImageChannel() applies a user supplied kernel to the image
3126% according to the given mophology method.
3127%
3128% This function applies any and all user defined settings before calling
3129% the above internal function MorphologyApply().
3130%
3131% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003132% * Output Bias for Convolution and correlation ("-bias")
3133% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3134% This can also includes the addition of a scaled unity kernel.
3135% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003136%
3137% The format of the MorphologyImage method is:
3138%
3139% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003140% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003141%
3142% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003143% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003144% KernelInfo *kernel,ExceptionInfo *exception)
3145%
3146% A description of each parameter follows:
3147%
3148% o image: the image.
3149%
3150% o method: the morphology method to be applied.
3151%
3152% o iterations: apply the operation this many times (or no change).
3153% A value of -1 means loop until no change found.
3154% How this is applied may depend on the morphology method.
3155% Typically this is a value of 1.
3156%
3157% o channel: the channel type.
3158%
3159% o kernel: An array of double representing the morphology kernel.
3160% Warning: kernel may be normalized for the Convolve method.
3161%
3162% o exception: return any errors or warnings in this structure.
3163%
3164*/
3165
3166MagickExport Image *MorphologyImageChannel(const Image *image,
3167 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003168 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003169{
3170 const char
3171 *artifact;
3172
3173 KernelInfo
3174 *curr_kernel;
3175
anthony47f5d062010-05-23 07:47:50 +00003176 CompositeOperator
3177 compose;
3178
anthony9eb4f742010-05-18 02:45:54 +00003179 Image
3180 *morphology_image;
3181
3182
anthony46a369d2010-05-19 02:41:48 +00003183 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3184 * This is done BEFORE the ShowKernelInfo() function is called so that
3185 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003186 */
3187 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003188 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003189 {
3190 artifact = GetImageArtifact(image,"convolve:scale");
3191 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003192 if ( curr_kernel == kernel )
3193 curr_kernel = CloneKernelInfo(kernel);
3194 if (curr_kernel == (KernelInfo *) NULL) {
3195 curr_kernel=DestroyKernelInfo(curr_kernel);
3196 return((Image *) NULL);
3197 }
anthony46a369d2010-05-19 02:41:48 +00003198 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003199 }
3200 }
3201
3202 /* display the (normalized) kernel via stderr */
3203 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003204 if ( artifact == (const char *) NULL)
3205 artifact = GetImageArtifact(image,"convolve:showkernel");
3206 if ( artifact == (const char *) NULL)
3207 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003208 if ( artifact != (const char *) NULL)
3209 ShowKernelInfo(curr_kernel);
3210
anthony47f5d062010-05-23 07:47:50 +00003211 /* override the default handling of multi-kernel morphology results
3212 * if 'Undefined' use the default method
3213 * if 'None' (default for 'Convolve') re-iterate previous result
3214 * otherwise merge resulting images using compose method given
3215 */
3216 compose = UndefinedCompositeOp; /* use default for method */
3217 artifact = GetImageArtifact(image,"morphology:compose");
3218 if ( artifact != (const char *) NULL)
3219 compose = (CompositeOperator) ParseMagickOption(
3220 MagickComposeOptions,MagickFalse,artifact);
3221
anthony9eb4f742010-05-18 02:45:54 +00003222 /* Apply the Morphology */
3223 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003224 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003225
3226 /* Cleanup and Exit */
3227 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003228 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003229 return(morphology_image);
3230}
3231
3232MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003233 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003234 *exception)
3235{
3236 Image
3237 *morphology_image;
3238
3239 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3240 iterations,kernel,exception);
3241 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003242}
anthony83ba99b2010-01-24 08:48:15 +00003243
3244/*
3245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3246% %
3247% %
3248% %
anthony4fd27e22010-02-07 08:17:18 +00003249+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003250% %
3251% %
3252% %
3253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3254%
anthony46a369d2010-05-19 02:41:48 +00003255% RotateKernelInfo() rotates the kernel by the angle given.
3256%
3257% Currently it is restricted to 90 degree angles, of either 1D kernels
3258% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3259% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003260%
anthony4fd27e22010-02-07 08:17:18 +00003261% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003262%
anthony4fd27e22010-02-07 08:17:18 +00003263% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003264%
3265% A description of each parameter follows:
3266%
3267% o kernel: the Morphology/Convolution kernel
3268%
3269% o angle: angle to rotate in degrees
3270%
anthony46a369d2010-05-19 02:41:48 +00003271% This function is currently internal to this module only, but can be exported
3272% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003273*/
anthony4fd27e22010-02-07 08:17:18 +00003274static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003275{
anthony1b2bc0a2010-05-12 05:25:22 +00003276 /* angle the lower kernels first */
3277 if ( kernel->next != (KernelInfo *) NULL)
3278 RotateKernelInfo(kernel->next, angle);
3279
anthony83ba99b2010-01-24 08:48:15 +00003280 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3281 **
3282 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3283 */
3284
3285 /* Modulus the angle */
3286 angle = fmod(angle, 360.0);
3287 if ( angle < 0 )
3288 angle += 360.0;
3289
anthony3c10fc82010-05-13 02:40:51 +00003290 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003291 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003292
anthony3dd0f622010-05-13 12:57:32 +00003293 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003294 switch (kernel->type) {
3295 /* These built-in kernels are cylindrical kernels, rotating is useless */
3296 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003297 case DOGKernel:
3298 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003299 case PeaksKernel:
3300 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003301 case ChebyshevKernel:
3302 case ManhattenKernel:
3303 case EuclideanKernel:
3304 return;
3305
3306 /* These may be rotatable at non-90 angles in the future */
3307 /* but simply rotating them in multiples of 90 degrees is useless */
3308 case SquareKernel:
3309 case DiamondKernel:
3310 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003311 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003312 return;
3313
3314 /* These only allows a +/-90 degree rotation (by transpose) */
3315 /* A 180 degree rotation is useless */
3316 case BlurKernel:
3317 case RectangleKernel:
3318 if ( 135.0 < angle && angle <= 225.0 )
3319 return;
3320 if ( 225.0 < angle && angle <= 315.0 )
3321 angle -= 180;
3322 break;
3323
anthony3dd0f622010-05-13 12:57:32 +00003324 default:
anthony83ba99b2010-01-24 08:48:15 +00003325 break;
3326 }
anthony3c10fc82010-05-13 02:40:51 +00003327 /* Attempt rotations by 45 degrees */
3328 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3329 {
3330 if ( kernel->width == 3 && kernel->height == 3 )
3331 { /* Rotate a 3x3 square by 45 degree angle */
3332 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003333 kernel->values[0] = kernel->values[3];
3334 kernel->values[3] = kernel->values[6];
3335 kernel->values[6] = kernel->values[7];
3336 kernel->values[7] = kernel->values[8];
3337 kernel->values[8] = kernel->values[5];
3338 kernel->values[5] = kernel->values[2];
3339 kernel->values[2] = kernel->values[1];
3340 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003341 /* rotate non-centered origin */
3342 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003343 ssize_t x,y;
3344 x = (ssize_t) kernel->x-1;
3345 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003346 if ( x == y ) x = 0;
3347 else if ( x == 0 ) x = -y;
3348 else if ( x == -y ) y = 0;
3349 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003350 kernel->x = (ssize_t) x+1;
3351 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003352 }
anthony43c49252010-05-18 10:59:50 +00003353 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3354 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003355 }
3356 else
3357 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3358 }
3359 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3360 {
3361 if ( kernel->width == 1 || kernel->height == 1 )
3362 { /* Do a transpose of the image, which results in a 90
3363 ** degree rotation of a 1 dimentional kernel
3364 */
cristybb503372010-05-27 20:51:26 +00003365 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003366 t;
cristybb503372010-05-27 20:51:26 +00003367 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003368 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003369 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003370 t = kernel->x;
3371 kernel->x = kernel->y;
3372 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003373 if ( kernel->width == 1 ) {
3374 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3375 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3376 } else {
3377 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3378 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3379 }
anthony3c10fc82010-05-13 02:40:51 +00003380 }
3381 else if ( kernel->width == kernel->height )
3382 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003383 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003384 i,j,x,y;
3385 register MagickRealType
3386 *k,t;
3387 k=kernel->values;
3388 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3389 for( j=0, y=kernel->height-1; j<y; j++, y--)
3390 { t = k[i+j*kernel->width];
3391 k[i+j*kernel->width] = k[j+x*kernel->width];
3392 k[j+x*kernel->width] = k[x+y*kernel->width];
3393 k[x+y*kernel->width] = k[y+i*kernel->width];
3394 k[y+i*kernel->width] = t;
3395 }
3396 }
3397 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003398 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003399 x = (ssize_t) (kernel->x*2-kernel->width+1);
3400 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003401 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3402 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003403 }
anthony43c49252010-05-18 10:59:50 +00003404 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3405 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003406 }
3407 else
3408 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3409 }
anthony83ba99b2010-01-24 08:48:15 +00003410 if ( 135.0 < angle && angle <= 225.0 )
3411 {
anthony43c49252010-05-18 10:59:50 +00003412 /* For a 180 degree rotation - also know as a reflection
3413 * This is actually a very very common operation!
3414 * Basically all that is needed is a reversal of the kernel data!
3415 * And a reflection of the origon
3416 */
cristybb503372010-05-27 20:51:26 +00003417 size_t
anthony83ba99b2010-01-24 08:48:15 +00003418 i,j;
3419 register double
3420 *k,t;
3421
3422 k=kernel->values;
3423 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3424 t=k[i], k[i]=k[j], k[j]=t;
3425
cristybb503372010-05-27 20:51:26 +00003426 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3427 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003428 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3429 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003430 }
anthony3c10fc82010-05-13 02:40:51 +00003431 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003432 * In the future some form of non-orthogonal angled rotates could be
3433 * performed here, posibily with a linear kernel restriction.
3434 */
3435
3436#if 0
anthony3c10fc82010-05-13 02:40:51 +00003437 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003438 */
cristybb503372010-05-27 20:51:26 +00003439 size_t
anthony83ba99b2010-01-24 08:48:15 +00003440 y;
cristybb503372010-05-27 20:51:26 +00003441 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003442 x,r;
3443 register double
3444 *k,t;
3445
3446 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3447 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3448 t=k[x], k[x]=k[r], k[r]=t;
3449
cristyc99304f2010-02-01 15:26:27 +00003450 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003451 angle = fmod(angle+180.0, 360.0);
3452 }
3453#endif
3454 return;
3455}
3456
3457/*
3458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3459% %
3460% %
3461% %
anthony46a369d2010-05-19 02:41:48 +00003462% S c a l e G e o m e t r y K e r n e l I n f o %
3463% %
3464% %
3465% %
3466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3467%
3468% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3469% provided as a "-set option:convolve:scale {geometry}" user setting,
3470% and modifies the kernel according to the parsed arguments of that setting.
3471%
3472% The first argument (and any normalization flags) are passed to
3473% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3474% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3475% into the scaled/normalized kernel.
3476%
3477% The format of the ScaleKernelInfo method is:
3478%
3479% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3480% const MagickStatusType normalize_flags )
3481%
3482% A description of each parameter follows:
3483%
3484% o kernel: the Morphology/Convolution kernel to modify
3485%
3486% o geometry:
3487% The geometry string to parse, typically from the user provided
3488% "-set option:convolve:scale {geometry}" setting.
3489%
3490*/
3491MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3492 const char *geometry)
3493{
3494 GeometryFlags
3495 flags;
3496 GeometryInfo
3497 args;
3498
3499 SetGeometryInfo(&args);
3500 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3501
3502#if 0
3503 /* For Debugging Geometry Input */
3504 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3505 flags, args.rho, args.sigma, args.xi, args.psi );
3506#endif
3507
3508 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3509 args.rho *= 0.01, args.sigma *= 0.01;
3510
3511 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3512 args.rho = 1.0;
3513 if ( (flags & SigmaValue) == 0 )
3514 args.sigma = 0.0;
3515
3516 /* Scale/Normalize the input kernel */
3517 ScaleKernelInfo(kernel, args.rho, flags);
3518
3519 /* Add Unity Kernel, for blending with original */
3520 if ( (flags & SigmaValue) != 0 )
3521 UnityAddKernelInfo(kernel, args.sigma);
3522
3523 return;
3524}
3525/*
3526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3527% %
3528% %
3529% %
cristy6771f1e2010-03-05 19:43:39 +00003530% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003531% %
3532% %
3533% %
3534%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3535%
anthony1b2bc0a2010-05-12 05:25:22 +00003536% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3537% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003538%
anthony999bb2c2010-02-18 12:38:01 +00003539% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003540% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003541%
anthony46a369d2010-05-19 02:41:48 +00003542% If either of the two 'normalize_flags' are given the kernel will first be
3543% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003544%
3545% Kernel normalization ('normalize_flags' given) is designed to ensure that
3546% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003547% morphology methods will fall into -1.0 to +1.0 range. Note that for
3548% non-HDRI versions of IM this may cause images to have any negative results
3549% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003550%
3551% More specifically. Kernels which only contain positive values (such as a
3552% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003553% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003554%
3555% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3556% the kernel will be scaled by the absolute of the sum of kernel values, so
3557% that it will generally fall within the +/- 1.0 range.
3558%
3559% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3560% will be scaled by just the sum of the postive values, so that its output
3561% range will again fall into the +/- 1.0 range.
3562%
3563% For special kernels designed for locating shapes using 'Correlate', (often
3564% only containing +1 and -1 values, representing foreground/brackground
3565% matching) a special normalization method is provided to scale the positive
3566% values seperatally to those of the negative values, so the kernel will be
3567% forced to become a zero-sum kernel better suited to such searches.
3568%
anthony1b2bc0a2010-05-12 05:25:22 +00003569% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003570% attributes within the kernel structure have been correctly set during the
3571% kernels creation.
3572%
3573% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003574% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3575% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003576%
anthony4fd27e22010-02-07 08:17:18 +00003577% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003578%
anthony999bb2c2010-02-18 12:38:01 +00003579% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3580% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003581%
3582% A description of each parameter follows:
3583%
3584% o kernel: the Morphology/Convolution kernel
3585%
anthony999bb2c2010-02-18 12:38:01 +00003586% o scaling_factor:
3587% multiply all values (after normalization) by this factor if not
3588% zero. If the kernel is normalized regardless of any flags.
3589%
3590% o normalize_flags:
3591% GeometryFlags defining normalization method to use.
3592% specifically: NormalizeValue, CorrelateNormalizeValue,
3593% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003594%
3595*/
cristy6771f1e2010-03-05 19:43:39 +00003596MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3597 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003598{
cristybb503372010-05-27 20:51:26 +00003599 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003600 i;
3601
anthony999bb2c2010-02-18 12:38:01 +00003602 register double
3603 pos_scale,
3604 neg_scale;
3605
anthony46a369d2010-05-19 02:41:48 +00003606 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003607 if ( kernel->next != (KernelInfo *) NULL)
3608 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3609
anthony46a369d2010-05-19 02:41:48 +00003610 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003611 pos_scale = 1.0;
3612 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003613 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003614 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003615 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003616 else
anthonyf4e00312010-05-20 12:06:35 +00003617 /* zero-summing kernel */
3618 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003619 }
anthony46a369d2010-05-19 02:41:48 +00003620 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003621 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3622 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3623 ? kernel->positive_range : 1.0;
3624 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3625 ? -kernel->negative_range : 1.0;
3626 }
3627 else
3628 neg_scale = pos_scale;
3629
3630 /* finialize scaling_factor for positive and negative components */
3631 pos_scale = scaling_factor/pos_scale;
3632 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003633
cristybb503372010-05-27 20:51:26 +00003634 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003635 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003636 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003637
anthony999bb2c2010-02-18 12:38:01 +00003638 /* convolution output range */
3639 kernel->positive_range *= pos_scale;
3640 kernel->negative_range *= neg_scale;
3641 /* maximum and minimum values in kernel */
3642 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3643 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3644
anthony46a369d2010-05-19 02:41:48 +00003645 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003646 if ( scaling_factor < MagickEpsilon ) {
3647 double t;
3648 t = kernel->positive_range;
3649 kernel->positive_range = kernel->negative_range;
3650 kernel->negative_range = t;
3651 t = kernel->maximum;
3652 kernel->maximum = kernel->minimum;
3653 kernel->minimum = 1;
3654 }
anthonycc6c8362010-01-25 04:14:01 +00003655
3656 return;
3657}
3658
3659/*
3660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3661% %
3662% %
3663% %
anthony46a369d2010-05-19 02:41:48 +00003664% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003665% %
3666% %
3667% %
3668%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3669%
anthony4fd27e22010-02-07 08:17:18 +00003670% ShowKernelInfo() outputs the details of the given kernel defination to
3671% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003672%
3673% The format of the ShowKernel method is:
3674%
anthony4fd27e22010-02-07 08:17:18 +00003675% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003676%
3677% A description of each parameter follows:
3678%
3679% o kernel: the Morphology/Convolution kernel
3680%
anthony83ba99b2010-01-24 08:48:15 +00003681*/
anthony4fd27e22010-02-07 08:17:18 +00003682MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003683{
anthony7a01dcf2010-05-11 12:25:52 +00003684 KernelInfo
3685 *k;
anthony83ba99b2010-01-24 08:48:15 +00003686
cristybb503372010-05-27 20:51:26 +00003687 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003688 c, i, u, v;
3689
3690 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3691
anthony46a369d2010-05-19 02:41:48 +00003692 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003693 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003694 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003695 fprintf(stderr, " \"%s",
3696 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3697 if ( fabs(k->angle) > MagickEpsilon )
3698 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003699 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3700 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003701 fprintf(stderr,
3702 " with values from %.*lg to %.*lg\n",
3703 GetMagickPrecision(), k->minimum,
3704 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003705 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003706 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003707 GetMagickPrecision(), k->positive_range);
3708 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3709 fprintf(stderr, " (Zero-Summing)\n");
3710 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3711 fprintf(stderr, " (Normalized)\n");
3712 else
3713 fprintf(stderr, " (Sum %.*lg)\n",
3714 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003715 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003716 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003717 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003718 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003719 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003720 else
anthonyf4e00312010-05-20 12:06:35 +00003721 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003722 GetMagickPrecision(), k->values[i]);
3723 fprintf(stderr,"\n");
3724 }
anthony83ba99b2010-01-24 08:48:15 +00003725 }
3726}
anthonycc6c8362010-01-25 04:14:01 +00003727
3728/*
3729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3730% %
3731% %
3732% %
anthony43c49252010-05-18 10:59:50 +00003733% U n i t y A d d K e r n a l I n f o %
3734% %
3735% %
3736% %
3737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3738%
3739% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3740% to the given pre-scaled and normalized Kernel. This in effect adds that
3741% amount of the original image into the resulting convolution kernel. This
3742% value is usually provided by the user as a percentage value in the
3743% 'convolve:scale' setting.
3744%
3745% The resulting effect is to either convert a 'zero-summing' edge detection
3746% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3747% kernel.
3748%
3749% Alternativally by using a purely positive kernel, and using a negative
3750% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3751% as a "Gaussian") into a 'unsharp' kernel.
3752%
anthony46a369d2010-05-19 02:41:48 +00003753% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003754%
3755% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3756%
3757% A description of each parameter follows:
3758%
3759% o kernel: the Morphology/Convolution kernel
3760%
3761% o scale:
3762% scaling factor for the unity kernel to be added to
3763% the given kernel.
3764%
anthony43c49252010-05-18 10:59:50 +00003765*/
3766MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3767 const double scale)
3768{
anthony46a369d2010-05-19 02:41:48 +00003769 /* do the other kernels in a multi-kernel list first */
3770 if ( kernel->next != (KernelInfo *) NULL)
3771 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003772
anthony46a369d2010-05-19 02:41:48 +00003773 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003774 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003775 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003776
3777 return;
3778}
3779
3780/*
3781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3782% %
3783% %
3784% %
3785% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003786% %
3787% %
3788% %
3789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3790%
3791% ZeroKernelNans() replaces any special 'nan' value that may be present in
3792% the kernel with a zero value. This is typically done when the kernel will
3793% be used in special hardware (GPU) convolution processors, to simply
3794% matters.
3795%
3796% The format of the ZeroKernelNans method is:
3797%
anthony46a369d2010-05-19 02:41:48 +00003798% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003799%
3800% A description of each parameter follows:
3801%
3802% o kernel: the Morphology/Convolution kernel
3803%
anthonycc6c8362010-01-25 04:14:01 +00003804*/
anthonyc4c86e02010-01-27 09:30:32 +00003805MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003806{
cristybb503372010-05-27 20:51:26 +00003807 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003808 i;
3809
anthony46a369d2010-05-19 02:41:48 +00003810 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003811 if ( kernel->next != (KernelInfo *) NULL)
3812 ZeroKernelNans(kernel->next);
3813
anthony43c49252010-05-18 10:59:50 +00003814 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003815 if ( IsNan(kernel->values[i]) )
3816 kernel->values[i] = 0.0;
3817
3818 return;
3819}