blob: 552d5a0fc73b4722081d58dacd0fc7ce231fbd11 [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}
anthony1d5e6702010-05-31 10:19:12 +0000678% Frei-Chen Edge Detector is based on a kernel that is similar to
679% the Sobel Kernel, but is designed to be isotropic. That is it takes
680% into account the distance of the diagonal in the kernel.
anthonyc3cd15b2010-05-27 06:05:40 +0000681%
682% Type 0: | -1, 0, 1 |
683% | -sqrt(2), 0, sqrt(2) |
684% | -1, 0, 1 |
685%
anthony1d5e6702010-05-31 10:19:12 +0000686% However this kernel is als at the heart of the FreiChen Edge Detection
687% Process which uses a set of 9 specially weighted kernel. These 9
688% kernels not be normalized, but directly applied to the image. The
689% results is then added together, to produce the intensity of an edge in
690% a specific direction. The square root of the pixel value can then be
691% taken as the cosine of the edge, and at least 2 such runs at 90 degrees
692% from each other, both the direction and the strength of the edge can be
693% determined.
anthonyc3cd15b2010-05-27 06:05:40 +0000694%
anthony1d5e6702010-05-31 10:19:12 +0000695% Type 1: | -1, 0, 1 |
696% | -sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
697% | -1, 0, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000698%
anthony1d5e6702010-05-31 10:19:12 +0000699% Type 2: | -1, -sqrt(2), -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000700% | 0, 0, 0 | / 2*sqrt(2)
anthony1d5e6702010-05-31 10:19:12 +0000701% | 1, sqrt(2), 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000702%
anthony1d5e6702010-05-31 10:19:12 +0000703% Type 3: | -sqrt(2), 1, 0 |
704% | 1, 0, -1 | / 2*sqrt(2)
705% | 0, -1, sqrt(2) |
anthonye2a60ce2010-05-19 12:30:40 +0000706%
anthony1d5e6702010-05-31 10:19:12 +0000707% Type 4: | 0, 1, -sqrt(2) |
anthonye2a60ce2010-05-19 12:30:40 +0000708% | -1, 0, 1 | / 2*sqrt(2)
anthony1d5e6702010-05-31 10:19:12 +0000709% | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000710%
711% Type 5: | 0, 1, 0 |
712% | -1, 0, -1 | / 2
713% | 0, 1, 0 |
714%
anthony1d5e6702010-05-31 10:19:12 +0000715% Type 6: | 1, 0, -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000716% | 0, 0, 0 | / 2
anthony1d5e6702010-05-31 10:19:12 +0000717% | -1, 0, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000718%
anthony1d5e6702010-05-31 10:19:12 +0000719% Type 7: | -2, 1, -2 |
anthonyf4e00312010-05-20 12:06:35 +0000720% | 1, 4, 1 | / 6
721% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000722%
anthony1d5e6702010-05-31 10:19:12 +0000723% Type 8: | 1, -2, 1 |
724% | -2, 4, -2 | / 6
725% | 1, -2, 1 |
726%
anthonyf4e00312010-05-20 12:06:35 +0000727% Type 9: | 1, 1, 1 |
728% | 1, 1, 1 | / 3
729% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000730%
731% The first 4 are for edge detection, the next 4 are for line detection
732% and the last is to add a average component to the results.
733%
anthonyc3cd15b2010-05-27 06:05:40 +0000734% Using a special type of '-1' will return all 9 pre-weighted kernels
735% as a multi-kernel list, so that you can use them directly (without
736% normalization) with the special "-set option:morphology:compose Plus"
737% setting to apply the full FreiChen Edge Detection Technique.
738%
anthony1dd091a2010-05-27 06:31:15 +0000739% If 'type' is large it will be taken to be an actual rotation angle for
740% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
741% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
742%
anthonye2a60ce2010-05-19 12:30:40 +0000743%
anthony602ab9b2010-01-05 08:06:50 +0000744% Boolean Kernels
745%
anthony3c10fc82010-05-13 02:40:51 +0000746% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000747% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000748% Kernel size will again be radius*2+1 square and defaults to radius 1,
749% generating a 3x3 kernel that is slightly larger than a square.
750%
anthony3c10fc82010-05-13 02:40:51 +0000751% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000752% Generate a square shaped kernel of size radius*2+1, and defaulting
753% to a 3x3 (radius 1).
754%
anthonyc1061722010-05-14 06:23:49 +0000755% Note that using a larger radius for the "Square" or the "Diamond" is
756% also equivelent to iterating the basic morphological method that many
757% times. However iterating with the smaller radius is actually faster
758% than using a larger kernel radius.
759%
760% Rectangle:{geometry}
761% Simply generate a rectangle of 1's with the size given. You can also
762% specify the location of the 'control point', otherwise the closest
763% pixel to the center of the rectangle is selected.
764%
765% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000766%
anthony3c10fc82010-05-13 02:40:51 +0000767% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000768% Generate a binary disk of the radius given, radius may be a float.
769% Kernel size will be ceil(radius)*2+1 square.
770% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000771% "Disk:1" => "diamond" or "cross:1"
772% "Disk:1.5" => "square"
773% "Disk:2" => "diamond:2"
774% "Disk:2.5" => a general disk shape of radius 2
775% "Disk:2.9" => "square:2"
776% "Disk:3.5" => default - octagonal/disk shape of radius 3
777% "Disk:4.2" => roughly octagonal shape of radius 4
778% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000779% After this all the kernel shape becomes more and more circular.
780%
781% Because a "disk" is more circular when using a larger radius, using a
782% larger radius is preferred over iterating the morphological operation.
783%
anthonyc1061722010-05-14 06:23:49 +0000784% Symbol Dilation Kernels
785%
786% These kernel is not a good general morphological kernel, but is used
787% more for highlighting and marking any single pixels in an image using,
788% a "Dilate" method as appropriate.
789%
790% For the same reasons iterating these kernels does not produce the
791% same result as using a larger radius for the symbol.
792%
anthony3c10fc82010-05-13 02:40:51 +0000793% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000794% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000795% Generate a kernel in the shape of a 'plus' or a 'cross' with
796% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000797%
798% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000799%
anthonyc1061722010-05-14 06:23:49 +0000800% Ring:{radius1},{radius2}[,{scale}]
801% A ring of the values given that falls between the two radii.
802% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
803% This is the 'edge' pixels of the default "Disk" kernel,
804% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000805%
anthony3dd0f622010-05-13 12:57:32 +0000806% Hit and Miss Kernels
807%
808% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000809% Find any peak larger than the pixels the fall between the two radii.
810% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000811% Edges
anthony1d45eb92010-05-25 11:13:23 +0000812% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000813% Corners
814% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000815% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000816% Find single pixel ridges or thin lines
817% Ridges2
818% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000819% Ridges3
820% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000821% LineEnds
822% Find end points of lines (for pruning a skeletion)
823% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000824% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000825% ConvexHull
826% Octagonal thicken kernel, to generate convex hulls of 45 degrees
827% Skeleton
828% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000829%
830% Distance Measuring Kernels
831%
anthonyc1061722010-05-14 06:23:49 +0000832% Different types of distance measuring methods, which are used with the
833% a 'Distance' morphology method for generating a gradient based on
834% distance from an edge of a binary shape, though there is a technique
835% for handling a anti-aliased shape.
836%
837% See the 'Distance' Morphological Method, for information of how it is
838% applied.
839%
anthony3dd0f622010-05-13 12:57:32 +0000840% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000841% Chebyshev Distance (also known as Tchebychev Distance) is a value of
842% one to any neighbour, orthogonal or diagonal. One why of thinking of
843% it is the number of squares a 'King' or 'Queen' in chess needs to
844% traverse reach any other position on a chess board. It results in a
845% 'square' like distance function, but one where diagonals are closer
846% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000847%
anthonyc1061722010-05-14 06:23:49 +0000848% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000849% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
850% Cab metric), is the distance needed when you can only travel in
851% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
852% in chess would travel. It results in a diamond like distances, where
853% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000854%
anthonyc1061722010-05-14 06:23:49 +0000855% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000856% Euclidean Distance is the 'direct' or 'as the crow flys distance.
857% However by default the kernel size only has a radius of 1, which
858% limits the distance to 'Knight' like moves, with only orthogonal and
859% diagonal measurements being correct. As such for the default kernel
860% you will get octagonal like distance function, which is reasonally
861% accurate.
862%
863% However if you use a larger radius such as "Euclidean:4" you will
864% get a much smoother distance gradient from the edge of the shape.
865% Of course a larger kernel is slower to use, and generally not needed.
866%
867% To allow the use of fractional distances that you get with diagonals
868% the actual distance is scaled by a fixed value which the user can
869% provide. This is not actually nessary for either ""Chebyshev" or
870% "Manhatten" distance kernels, but is done for all three distance
871% kernels. If no scale is provided it is set to a value of 100,
872% allowing for a maximum distance measurement of 655 pixels using a Q16
873% version of IM, from any edge. However for small images this can
874% result in quite a dark gradient.
875%
anthony602ab9b2010-01-05 08:06:50 +0000876*/
877
cristy2be15382010-01-21 02:38:03 +0000878MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000879 const GeometryInfo *args)
880{
cristy2be15382010-01-21 02:38:03 +0000881 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000882 *kernel;
883
cristybb503372010-05-27 20:51:26 +0000884 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000885 i;
886
cristybb503372010-05-27 20:51:26 +0000887 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000888 u,
889 v;
890
891 double
892 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
893
anthonyc1061722010-05-14 06:23:49 +0000894 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000895 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000896 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000897 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000898 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000899 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000900 break;
anthony1dd091a2010-05-27 06:31:15 +0000901 case UnityKernel: /* Named Descrete Convolution Kernels */
902 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000903 case SobelKernel:
904 case RobertsKernel:
905 case PrewittKernel:
906 case CompassKernel:
907 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000908 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000909 case CornersKernel: /* Hit and Miss kernels */
910 case LineEndsKernel:
911 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000912 case EdgesKernel:
913 case RidgesKernel:
914 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000915 case ConvexHullKernel:
916 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000917 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000918 /* A pre-generated kernel is not needed */
919 break;
anthony1dd091a2010-05-27 06:31:15 +0000920#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000921 case GaussianKernel:
922 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000923 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000924 case BlurKernel:
925 case DOBKernel:
926 case CometKernel:
927 case DiamondKernel:
928 case SquareKernel:
929 case RectangleKernel:
930 case DiskKernel:
931 case PlusKernel:
932 case CrossKernel:
933 case RingKernel:
934 case PeaksKernel:
935 case ChebyshevKernel:
936 case ManhattenKernel:
937 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000938#else
anthony9eb4f742010-05-18 02:45:54 +0000939 default:
anthony1dd091a2010-05-27 06:31:15 +0000940#endif
anthony9eb4f742010-05-18 02:45:54 +0000941 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000942 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
943 if (kernel == (KernelInfo *) NULL)
944 return(kernel);
945 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000946 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000947 kernel->negative_range = kernel->positive_range = 0.0;
948 kernel->type = type;
949 kernel->next = (KernelInfo *) NULL;
950 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000951 break;
952 }
anthony602ab9b2010-01-05 08:06:50 +0000953
954 switch(type) {
955 /* Convolution Kernels */
956 case GaussianKernel:
anthonyc1061722010-05-14 06:23:49 +0000957 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000958 case LOGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000959 { double
anthonyc1061722010-05-14 06:23:49 +0000960 sigma = fabs(args->sigma),
961 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000962 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000963
anthonyc1061722010-05-14 06:23:49 +0000964 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +0000965 kernel->width = (size_t)args->rho*2+1;
anthony9eb4f742010-05-18 02:45:54 +0000966 else if ( (type != DOGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000967 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
968 else
969 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
970 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +0000971 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000972 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
973 kernel->height*sizeof(double));
974 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000975 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000976
anthony46a369d2010-05-19 02:41:48 +0000977 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000978 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000979 *
980 * How to do this is currently not known, but appears to be
981 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000982 */
983
984 if ( type == GaussianKernel || type == DOGKernel )
985 { /* Calculate a Gaussian, OR positive half of a DOG */
986 if ( sigma > MagickEpsilon )
987 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
988 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +0000989 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
990 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +0000991 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
992 }
993 else /* limiting case - a unity (normalized Dirac) kernel */
994 { (void) ResetMagickMemory(kernel->values,0, (size_t)
995 kernel->width*kernel->height*sizeof(double));
996 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
997 }
anthonyc1061722010-05-14 06:23:49 +0000998 }
anthony9eb4f742010-05-18 02:45:54 +0000999
anthonyc1061722010-05-14 06:23:49 +00001000 if ( type == DOGKernel )
1001 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
1002 if ( sigma2 > MagickEpsilon )
1003 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001004 A = 1.0/(2.0*sigma*sigma);
1005 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001006 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1007 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001008 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001009 }
anthony9eb4f742010-05-18 02:45:54 +00001010 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001011 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1012 }
anthony9eb4f742010-05-18 02:45:54 +00001013
1014 if ( type == LOGKernel )
1015 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1016 if ( sigma > MagickEpsilon )
1017 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1018 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001019 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1020 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001021 { R = ((double)(u*u+v*v))*A;
1022 kernel->values[i] = (1-R)*exp(-R)*B;
1023 }
1024 }
1025 else /* special case - generate a unity kernel */
1026 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1027 kernel->width*kernel->height*sizeof(double));
1028 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1029 }
1030 }
1031
1032 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001033 ** radius, producing a smaller (darker) kernel. Also for very small
1034 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1035 ** producing a very bright kernel.
1036 **
1037 ** Normalization will still be needed.
1038 */
anthony602ab9b2010-01-05 08:06:50 +00001039
anthony3dd0f622010-05-13 12:57:32 +00001040 /* Normalize the 2D Gaussian Kernel
1041 **
anthonyc1061722010-05-14 06:23:49 +00001042 ** NB: a CorrelateNormalize performs a normal Normalize if
1043 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001044 */
anthony46a369d2010-05-19 02:41:48 +00001045 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001046 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001047
1048 break;
1049 }
1050 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +00001051 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001052 { double
anthonyc1061722010-05-14 06:23:49 +00001053 sigma = fabs(args->sigma),
1054 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001055 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001056
anthonyc1061722010-05-14 06:23:49 +00001057 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001058 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001059 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1060 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1061 else
1062 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001063 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001064 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001065 kernel->y = 0;
1066 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001067 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1068 kernel->height*sizeof(double));
1069 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001070 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001071
1072#if 1
1073#define KernelRank 3
1074 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1075 ** It generates a gaussian 3 times the width, and compresses it into
1076 ** the expected range. This produces a closer normalization of the
1077 ** resulting kernel, especially for very low sigma values.
1078 ** As such while wierd it is prefered.
1079 **
1080 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001081 **
1082 ** A properly normalized curve is generated (apart from edge clipping)
1083 ** even though we later normalize the result (for edge clipping)
1084 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001085 */
anthonyc1061722010-05-14 06:23:49 +00001086
1087 /* initialize */
cristybb503372010-05-27 20:51:26 +00001088 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001089 (void) ResetMagickMemory(kernel->values,0, (size_t)
1090 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001091 /* Calculate a Positive 1D Gaussian */
1092 if ( sigma > MagickEpsilon )
1093 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001094 A = 1.0/(2.0*sigma*sigma);
1095 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001096 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001097 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001098 }
1099 }
1100 else /* special case - generate a unity kernel */
1101 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001102
1103 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001104 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001105 {
anthonyc1061722010-05-14 06:23:49 +00001106 if ( sigma2 > MagickEpsilon )
1107 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001108 A = 1.0/(2.0*sigma*sigma);
1109 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001110 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001111 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001112 }
anthony9eb4f742010-05-18 02:45:54 +00001113 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001114 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1115 }
anthony602ab9b2010-01-05 08:06:50 +00001116#else
anthonyc1061722010-05-14 06:23:49 +00001117 /* Direct calculation without curve averaging */
1118
1119 /* Calculate a Positive Gaussian */
1120 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001121 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1122 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001123 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001124 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001125 }
1126 else /* special case - generate a unity kernel */
1127 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1128 kernel->width*kernel->height*sizeof(double));
1129 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1130 }
anthony9eb4f742010-05-18 02:45:54 +00001131
1132 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001133 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001134 {
anthonyc1061722010-05-14 06:23:49 +00001135 if ( sigma2 > MagickEpsilon )
1136 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001137 A = 1.0/(2.0*sigma*sigma);
1138 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001139 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001140 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001141 }
anthony9eb4f742010-05-18 02:45:54 +00001142 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001143 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1144 }
anthony602ab9b2010-01-05 08:06:50 +00001145#endif
anthonyc1061722010-05-14 06:23:49 +00001146 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001147 ** radius, producing a smaller (darker) kernel. Also for very small
1148 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1149 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001150 **
1151 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001152 */
anthonycc6c8362010-01-25 04:14:01 +00001153
anthony602ab9b2010-01-05 08:06:50 +00001154 /* Normalize the 1D Gaussian Kernel
1155 **
anthonyc1061722010-05-14 06:23:49 +00001156 ** NB: a CorrelateNormalize performs a normal Normalize if
1157 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001158 */
anthony46a369d2010-05-19 02:41:48 +00001159 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1160 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001161
anthonyc1061722010-05-14 06:23:49 +00001162 /* rotate the 1D kernel by given angle */
1163 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001164 break;
1165 }
1166 case CometKernel:
1167 { double
anthony9eb4f742010-05-18 02:45:54 +00001168 sigma = fabs(args->sigma),
1169 A;
anthony602ab9b2010-01-05 08:06:50 +00001170
anthony602ab9b2010-01-05 08:06:50 +00001171 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001172 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001173 else
cristybb503372010-05-27 20:51:26 +00001174 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001175 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001176 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001177 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001178 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1179 kernel->height*sizeof(double));
1180 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001181 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001182
anthonyc1061722010-05-14 06:23:49 +00001183 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001184 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001185 ** curve to use so may change in the future. The function must be
1186 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001187 **
1188 ** As we are normalizing and not subtracting gaussians,
1189 ** there is no need for a divisor in the gaussian formula
1190 **
anthony43c49252010-05-18 10:59:50 +00001191 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001192 */
anthony9eb4f742010-05-18 02:45:54 +00001193 if ( sigma > MagickEpsilon )
1194 {
anthony602ab9b2010-01-05 08:06:50 +00001195#if 1
1196#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001197 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001198 (void) ResetMagickMemory(kernel->values,0, (size_t)
1199 kernel->width*sizeof(double));
1200 sigma *= KernelRank; /* simplify the loop expression */
1201 A = 1.0/(2.0*sigma*sigma);
1202 /* B = 1.0/(MagickSQ2PI*sigma); */
1203 for ( u=0; u < v; u++) {
1204 kernel->values[u/KernelRank] +=
1205 exp(-((double)(u*u))*A);
1206 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1207 }
cristybb503372010-05-27 20:51:26 +00001208 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001209 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001210#else
anthony9eb4f742010-05-18 02:45:54 +00001211 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1212 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001213 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001214 kernel->positive_range +=
1215 kernel->values[i] =
1216 exp(-((double)(i*i))*A);
1217 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001218#endif
anthony9eb4f742010-05-18 02:45:54 +00001219 }
1220 else /* special case - generate a unity kernel */
1221 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1222 kernel->width*kernel->height*sizeof(double));
1223 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1224 kernel->positive_range = 1.0;
1225 }
anthony46a369d2010-05-19 02:41:48 +00001226
1227 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001228 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001229 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001230
anthony999bb2c2010-02-18 12:38:01 +00001231 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1232 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001233 break;
1234 }
anthonyc1061722010-05-14 06:23:49 +00001235
anthony3c10fc82010-05-13 02:40:51 +00001236 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001237 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001238 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001239 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001240 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001241 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001242 break;
anthony9eb4f742010-05-18 02:45:54 +00001243 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001244 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001245 break;
1246 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001247 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1248 break;
1249 case 3:
anthonyc1061722010-05-14 06:23:49 +00001250 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001251 break;
anthony9eb4f742010-05-18 02:45:54 +00001252 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001253 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001254 "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 +00001255 break;
anthony9eb4f742010-05-18 02:45:54 +00001256 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001257 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001258 "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 +00001259 break;
anthony43c49252010-05-18 10:59:50 +00001260 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001261 kernel=ParseKernelArray(
1262 "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");
1263 break;
anthony43c49252010-05-18 10:59:50 +00001264 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1265 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1266 kernel=ParseKernelArray(
1267 "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");
1268 break;
anthony3c10fc82010-05-13 02:40:51 +00001269 }
1270 if (kernel == (KernelInfo *) NULL)
1271 return(kernel);
1272 kernel->type = type;
1273 break;
1274 }
anthonyc1061722010-05-14 06:23:49 +00001275 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001276 {
anthonyc1061722010-05-14 06:23:49 +00001277 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1278 if (kernel == (KernelInfo *) NULL)
1279 return(kernel);
1280 kernel->type = type;
1281 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1282 break;
1283 }
1284 case RobertsKernel:
1285 {
1286 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1287 if (kernel == (KernelInfo *) NULL)
1288 return(kernel);
1289 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001290 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001291 break;
1292 }
1293 case PrewittKernel:
1294 {
1295 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1296 if (kernel == (KernelInfo *) NULL)
1297 return(kernel);
1298 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001299 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001300 break;
1301 }
1302 case CompassKernel:
1303 {
1304 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1305 if (kernel == (KernelInfo *) NULL)
1306 return(kernel);
1307 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001308 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001309 break;
1310 }
anthony9eb4f742010-05-18 02:45:54 +00001311 case KirschKernel:
1312 {
1313 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1314 if (kernel == (KernelInfo *) NULL)
1315 return(kernel);
1316 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001317 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001318 break;
1319 }
anthonye2a60ce2010-05-19 12:30:40 +00001320 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001321 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1322 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001323 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001324 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001325 case 0:
1326 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1327 if (kernel == (KernelInfo *) NULL)
1328 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001329 kernel->values[3] = -MagickSQ2;
1330 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001331 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001332 break;
anthonye2a60ce2010-05-19 12:30:40 +00001333 case 1:
anthony1d5e6702010-05-31 10:19:12 +00001334 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001335 if (kernel == (KernelInfo *) NULL)
1336 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001337 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001338 kernel->values[3] = -MagickSQ2;
1339 kernel->values[5] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001340 CalcKernelMetaData(kernel); /* recalculate meta-data */
1341 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1342 break;
1343 case 2:
anthony1d5e6702010-05-31 10:19:12 +00001344 kernel=ParseKernelArray("3: -1,-2,-1 0,0,0 1,2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001345 if (kernel == (KernelInfo *) NULL)
1346 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001347 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001348 kernel->values[1] = +MagickSQ2;
1349 kernel->values[7] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001350 CalcKernelMetaData(kernel);
1351 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1352 break;
1353 case 3:
anthony1d5e6702010-05-31 10:19:12 +00001354 kernel=ParseKernelArray("3: -2,1,0 1,0,-1 0,-1,2");
anthonye2a60ce2010-05-19 12:30:40 +00001355 if (kernel == (KernelInfo *) NULL)
1356 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001357 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001358 kernel->values[0] = -MagickSQ2;
1359 kernel->values[8] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001360 CalcKernelMetaData(kernel);
1361 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1362 break;
1363 case 4:
anthony1d5e6702010-05-31 10:19:12 +00001364 kernel=ParseKernelArray("3: 0,1,-2 -1,0,1 2,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001365 if (kernel == (KernelInfo *) NULL)
1366 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001367 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001368 kernel->values[2] = -MagickSQ2;
1369 kernel->values[6] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001370 CalcKernelMetaData(kernel);
1371 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1372 break;
1373 case 5:
1374 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1375 if (kernel == (KernelInfo *) NULL)
1376 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001377 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001378 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1379 break;
1380 case 6:
anthony1d5e6702010-05-31 10:19:12 +00001381 kernel=ParseKernelArray("3: 1,0,-1 0,0,0 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001382 if (kernel == (KernelInfo *) NULL)
1383 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001384 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001385 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1386 break;
1387 case 7:
anthony1d5e6702010-05-31 10:19:12 +00001388 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001389 if (kernel == (KernelInfo *) NULL)
1390 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001391 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001392 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1393 break;
1394 case 8:
anthony1d5e6702010-05-31 10:19:12 +00001395 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001396 if (kernel == (KernelInfo *) NULL)
1397 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001398 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001399 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1400 break;
1401 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001402 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001403 if (kernel == (KernelInfo *) NULL)
1404 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001405 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001406 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1407 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001408 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001409 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1410 if (kernel == (KernelInfo *) NULL)
1411 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001412 break;
anthonye2a60ce2010-05-19 12:30:40 +00001413 }
anthonyc3cd15b2010-05-27 06:05:40 +00001414 if ( fabs(args->sigma) > MagickEpsilon )
1415 /* Rotate by correctly supplied 'angle' */
1416 RotateKernelInfo(kernel, args->sigma);
1417 else if ( args->rho > 30.0 || args->rho < -30.0 )
1418 /* Rotate by out of bounds 'type' */
1419 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001420 break;
1421 }
1422
anthonyc1061722010-05-14 06:23:49 +00001423 /* Boolean Kernels */
1424 case DiamondKernel:
1425 {
1426 if (args->rho < 1.0)
1427 kernel->width = kernel->height = 3; /* default radius = 1 */
1428 else
cristybb503372010-05-27 20:51:26 +00001429 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1430 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001431
1432 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1433 kernel->height*sizeof(double));
1434 if (kernel->values == (double *) NULL)
1435 return(DestroyKernelInfo(kernel));
1436
1437 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001438 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1439 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony1d5e6702010-05-31 10:19:12 +00001440 if ( (labs((long) u)+labs((long) v)) <= (long) kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001441 kernel->positive_range += kernel->values[i] = args->sigma;
1442 else
1443 kernel->values[i] = nan;
1444 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1445 break;
1446 }
1447 case SquareKernel:
1448 case RectangleKernel:
1449 { double
1450 scale;
anthony602ab9b2010-01-05 08:06:50 +00001451 if ( type == SquareKernel )
1452 {
1453 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001454 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001455 else
cristybb503372010-05-27 20:51:26 +00001456 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1457 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001458 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001459 }
1460 else {
cristy2be15382010-01-21 02:38:03 +00001461 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001462 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001463 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001464 kernel->width = (size_t)args->rho;
1465 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001466 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1467 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001468 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001469 kernel->x = (ssize_t) args->xi;
1470 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001471 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001472 }
1473 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1474 kernel->height*sizeof(double));
1475 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001476 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001477
anthony3dd0f622010-05-13 12:57:32 +00001478 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001479 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001480 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001481 kernel->values[i] = scale;
1482 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1483 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001484 break;
anthony602ab9b2010-01-05 08:06:50 +00001485 }
anthony602ab9b2010-01-05 08:06:50 +00001486 case DiskKernel:
1487 {
anthonye4d89962010-05-29 10:53:11 +00001488 ssize_t
1489 limit = (ssize_t)(args->rho*args->rho);
1490
1491 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001492 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001493 else
anthonye4d89962010-05-29 10:53:11 +00001494 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001495 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001496
1497 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1498 kernel->height*sizeof(double));
1499 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001500 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001501
anthony3dd0f622010-05-13 12:57:32 +00001502 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001503 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1504 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001505 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001506 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001507 else
1508 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001509 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001510 break;
1511 }
1512 case PlusKernel:
1513 {
1514 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001515 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001516 else
cristybb503372010-05-27 20:51:26 +00001517 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1518 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001519
1520 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1521 kernel->height*sizeof(double));
1522 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001523 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001524
cristycee97112010-05-28 00:44:52 +00001525 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001526 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1527 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001528 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1529 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1530 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001531 break;
1532 }
anthony3dd0f622010-05-13 12:57:32 +00001533 case CrossKernel:
1534 {
1535 if (args->rho < 1.0)
1536 kernel->width = kernel->height = 5; /* default radius 2 */
1537 else
cristybb503372010-05-27 20:51:26 +00001538 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1539 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001540
1541 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1542 kernel->height*sizeof(double));
1543 if (kernel->values == (double *) NULL)
1544 return(DestroyKernelInfo(kernel));
1545
cristycee97112010-05-28 00:44:52 +00001546 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001547 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1548 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001549 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1550 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1551 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1552 break;
1553 }
1554 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001555 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001556 case PeaksKernel:
1557 {
cristybb503372010-05-27 20:51:26 +00001558 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001559 limit1,
anthonyc1061722010-05-14 06:23:49 +00001560 limit2,
1561 scale;
anthony3dd0f622010-05-13 12:57:32 +00001562
1563 if (args->rho < args->sigma)
1564 {
cristybb503372010-05-27 20:51:26 +00001565 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001566 limit1 = (ssize_t)(args->rho*args->rho);
1567 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001568 }
1569 else
1570 {
cristybb503372010-05-27 20:51:26 +00001571 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001572 limit1 = (ssize_t)(args->sigma*args->sigma);
1573 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001574 }
anthonyc1061722010-05-14 06:23:49 +00001575 if ( limit2 <= 0 )
1576 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1577
anthony3dd0f622010-05-13 12:57:32 +00001578 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001579 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001580 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1581 kernel->height*sizeof(double));
1582 if (kernel->values == (double *) NULL)
1583 return(DestroyKernelInfo(kernel));
1584
anthonyc1061722010-05-14 06:23:49 +00001585 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001586 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1587 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1588 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1589 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001590 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001591 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001592 else
1593 kernel->values[i] = nan;
1594 }
cristye96405a2010-05-19 02:24:31 +00001595 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001596 if ( type == PeaksKernel ) {
1597 /* set the central point in the middle */
1598 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1599 kernel->positive_range = 1.0;
1600 kernel->maximum = 1.0;
1601 }
anthony3dd0f622010-05-13 12:57:32 +00001602 break;
1603 }
anthony43c49252010-05-18 10:59:50 +00001604 case EdgesKernel:
1605 {
1606 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1607 if (kernel == (KernelInfo *) NULL)
1608 return(kernel);
1609 kernel->type = type;
1610 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1611 break;
1612 }
anthony3dd0f622010-05-13 12:57:32 +00001613 case CornersKernel:
1614 {
anthony4f1dcb72010-05-14 08:43:10 +00001615 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001616 if (kernel == (KernelInfo *) NULL)
1617 return(kernel);
1618 kernel->type = type;
1619 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1620 break;
1621 }
anthony47f5d062010-05-23 07:47:50 +00001622 case RidgesKernel:
1623 {
anthony24a19842010-05-27 12:18:34 +00001624 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001625 if (kernel == (KernelInfo *) NULL)
1626 return(kernel);
1627 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001628 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001629 break;
1630 }
anthony1d45eb92010-05-25 11:13:23 +00001631 case Ridges2Kernel:
1632 {
1633 KernelInfo
1634 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001635 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001636 if (kernel == (KernelInfo *) NULL)
1637 return(kernel);
1638 kernel->type = type;
1639 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001640#if 0
1641 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001642 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1643 if (new_kernel == (KernelInfo *) NULL)
1644 return(DestroyKernelInfo(kernel));
1645 new_kernel->type = type;
1646 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1647 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001648#endif
1649 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1650 /* Unfortunatally we can not yet rotate a non-square kernel */
1651 /* But then we can't flip a non-symetrical kernel either */
1652 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1653 if (new_kernel == (KernelInfo *) NULL)
1654 return(DestroyKernelInfo(kernel));
1655 new_kernel->type = type;
1656 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001657 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001658 if (new_kernel == (KernelInfo *) NULL)
1659 return(DestroyKernelInfo(kernel));
1660 new_kernel->type = type;
1661 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001662 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001663 if (new_kernel == (KernelInfo *) NULL)
1664 return(DestroyKernelInfo(kernel));
1665 new_kernel->type = type;
1666 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001667 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001668 if (new_kernel == (KernelInfo *) NULL)
1669 return(DestroyKernelInfo(kernel));
1670 new_kernel->type = type;
1671 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001672 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001673 if (new_kernel == (KernelInfo *) NULL)
1674 return(DestroyKernelInfo(kernel));
1675 new_kernel->type = type;
1676 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001677 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001678 if (new_kernel == (KernelInfo *) NULL)
1679 return(DestroyKernelInfo(kernel));
1680 new_kernel->type = type;
1681 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001682 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001683 if (new_kernel == (KernelInfo *) NULL)
1684 return(DestroyKernelInfo(kernel));
1685 new_kernel->type = type;
1686 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001687 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001688 if (new_kernel == (KernelInfo *) NULL)
1689 return(DestroyKernelInfo(kernel));
1690 new_kernel->type = type;
1691 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001692 break;
1693 }
anthony3dd0f622010-05-13 12:57:32 +00001694 case LineEndsKernel:
1695 {
anthony43c49252010-05-18 10:59:50 +00001696 KernelInfo
1697 *new_kernel;
1698 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001699 if (kernel == (KernelInfo *) NULL)
1700 return(kernel);
1701 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001702 ExpandKernelInfo(kernel, 90.0);
1703 /* append second set of 4 kernels */
1704 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1705 if (new_kernel == (KernelInfo *) NULL)
1706 return(DestroyKernelInfo(kernel));
1707 new_kernel->type = type;
1708 ExpandKernelInfo(new_kernel, 90.0);
1709 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001710 break;
1711 }
1712 case LineJunctionsKernel:
1713 {
1714 KernelInfo
1715 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001716 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001717 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001718 if (kernel == (KernelInfo *) NULL)
1719 return(kernel);
1720 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001721 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001722 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001723 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001724 if (new_kernel == (KernelInfo *) NULL)
1725 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001726 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001727 ExpandKernelInfo(new_kernel, 90.0);
1728 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001729 break;
1730 }
anthony3dd0f622010-05-13 12:57:32 +00001731 case ConvexHullKernel:
1732 {
anthony3928ec62010-05-27 14:03:29 +00001733 KernelInfo
1734 *new_kernel;
1735 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001736 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001737 if (kernel == (KernelInfo *) NULL)
1738 return(kernel);
1739 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001740 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001741 /* append the mirror versions too */
1742 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1743 if (new_kernel == (KernelInfo *) NULL)
1744 return(DestroyKernelInfo(kernel));
1745 new_kernel->type = type;
1746 ExpandKernelInfo(new_kernel, 45.0);
1747 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001748 break;
1749 }
anthony47f5d062010-05-23 07:47:50 +00001750 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001751 { /* what is the best form for skeletonization by thinning? */
anthonye4d89962010-05-29 10:53:11 +00001752#if 1
1753 /* Full Corner rotated to form edges too */
anthony43c49252010-05-18 10:59:50 +00001754 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001755 if (kernel == (KernelInfo *) NULL)
1756 return(kernel);
1757 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001758 ExpandKernelInfo(kernel, 45);
anthonye4d89962010-05-29 10:53:11 +00001759#endif
1760#if 0
1761 /* As last but thin the edges before looking for corners */
1762 KernelInfo
1763 *new_kernel;
1764 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1765 if (kernel == (KernelInfo *) NULL)
1766 return(kernel);
1767 kernel->type = type;
1768 ExpandKernelInfo(kernel, 90.0);
1769 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
1770 if (new_kernel == (KernelInfo *) NULL)
1771 return(DestroyKernelInfo(kernel));
1772 new_kernel->type = type;
1773 ExpandKernelInfo(new_kernel, 90.0);
1774 LastKernelInfo(kernel)->next = new_kernel;
1775#endif
1776#if 0
1777 kernel=AcquireKernelInfo("Edges;Corners");
1778#endif
1779#if 0
1780 kernel=AcquireKernelInfo("Corners;Edges");
anthony47f5d062010-05-23 07:47:50 +00001781#endif
anthony3dd0f622010-05-13 12:57:32 +00001782 break;
1783 }
anthonya648a302010-05-27 02:14:36 +00001784 case MatKernel: /* experimental - MAT from a Distance Gradient */
1785 {
1786 KernelInfo
1787 *new_kernel;
1788 /* Ridge Kernel but without the diagonal */
1789 kernel=ParseKernelArray("3x1: 0,1,0");
1790 if (kernel == (KernelInfo *) NULL)
1791 return(kernel);
1792 kernel->type = RidgesKernel;
1793 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1794 /* Plus the 2 pixel ridges kernel - no diagonal */
1795 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1796 if (new_kernel == (KernelInfo *) NULL)
1797 return(kernel);
1798 LastKernelInfo(kernel)->next = new_kernel;
1799 break;
1800 }
anthony602ab9b2010-01-05 08:06:50 +00001801 /* Distance Measuring Kernels */
1802 case ChebyshevKernel:
1803 {
anthony602ab9b2010-01-05 08:06:50 +00001804 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001805 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001806 else
cristybb503372010-05-27 20:51:26 +00001807 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1808 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001809
1810 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1811 kernel->height*sizeof(double));
1812 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001813 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001814
cristybb503372010-05-27 20:51:26 +00001815 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1816 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001817 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001818 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001819 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001820 break;
1821 }
1822 case ManhattenKernel:
1823 {
anthony602ab9b2010-01-05 08:06:50 +00001824 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001825 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001826 else
cristybb503372010-05-27 20:51:26 +00001827 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1828 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001829
1830 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1831 kernel->height*sizeof(double));
1832 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001833 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001834
cristybb503372010-05-27 20:51:26 +00001835 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1836 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001837 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001838 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001839 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001840 break;
1841 }
1842 case EuclideanKernel:
1843 {
anthony602ab9b2010-01-05 08:06:50 +00001844 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001845 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001846 else
cristybb503372010-05-27 20:51:26 +00001847 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1848 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001849
1850 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1851 kernel->height*sizeof(double));
1852 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001853 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001854
cristybb503372010-05-27 20:51:26 +00001855 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1856 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001857 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001858 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001859 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001860 break;
1861 }
anthony46a369d2010-05-19 02:41:48 +00001862 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001863 default:
anthonyc1061722010-05-14 06:23:49 +00001864 {
anthony46a369d2010-05-19 02:41:48 +00001865 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1866 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001867 if (kernel == (KernelInfo *) NULL)
1868 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001869 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001870 break;
1871 }
anthony602ab9b2010-01-05 08:06:50 +00001872 break;
1873 }
1874
1875 return(kernel);
1876}
anthonyc94cdb02010-01-06 08:15:29 +00001877
anthony602ab9b2010-01-05 08:06:50 +00001878/*
1879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1880% %
1881% %
1882% %
cristy6771f1e2010-03-05 19:43:39 +00001883% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001884% %
1885% %
1886% %
1887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1888%
anthony1b2bc0a2010-05-12 05:25:22 +00001889% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1890% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001891% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001892%
cristye6365592010-04-02 17:31:23 +00001893% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001894%
anthony930be612010-02-08 04:26:15 +00001895% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001896%
1897% A description of each parameter follows:
1898%
1899% o kernel: the Morphology/Convolution kernel to be cloned
1900%
1901*/
cristyef656912010-03-05 19:54:59 +00001902MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001903{
cristybb503372010-05-27 20:51:26 +00001904 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001905 i;
1906
cristy19eb6412010-04-23 14:42:29 +00001907 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001908 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001909
1910 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001911 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1912 if (new_kernel == (KernelInfo *) NULL)
1913 return(new_kernel);
1914 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001915
1916 /* replace the values with a copy of the values */
1917 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001918 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001919 if (new_kernel->values == (double *) NULL)
1920 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001921 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001922 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001923
1924 /* Also clone the next kernel in the kernel list */
1925 if ( kernel->next != (KernelInfo *) NULL ) {
1926 new_kernel->next = CloneKernelInfo(kernel->next);
1927 if ( new_kernel->next == (KernelInfo *) NULL )
1928 return(DestroyKernelInfo(new_kernel));
1929 }
1930
anthony7a01dcf2010-05-11 12:25:52 +00001931 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001932}
1933
1934/*
1935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1936% %
1937% %
1938% %
anthony83ba99b2010-01-24 08:48:15 +00001939% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001940% %
1941% %
1942% %
1943%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1944%
anthony83ba99b2010-01-24 08:48:15 +00001945% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1946% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001947%
anthony83ba99b2010-01-24 08:48:15 +00001948% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001949%
anthony83ba99b2010-01-24 08:48:15 +00001950% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001951%
1952% A description of each parameter follows:
1953%
1954% o kernel: the Morphology/Convolution kernel to be destroyed
1955%
1956*/
1957
anthony83ba99b2010-01-24 08:48:15 +00001958MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001959{
cristy2be15382010-01-21 02:38:03 +00001960 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001961
anthony7a01dcf2010-05-11 12:25:52 +00001962 if ( kernel->next != (KernelInfo *) NULL )
1963 kernel->next = DestroyKernelInfo(kernel->next);
1964
1965 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1966 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001967 return(kernel);
1968}
anthonyc94cdb02010-01-06 08:15:29 +00001969
1970/*
1971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972% %
1973% %
1974% %
anthony3c10fc82010-05-13 02:40:51 +00001975% E x p a n d K e r n e l I n f o %
1976% %
1977% %
1978% %
1979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1980%
1981% ExpandKernelInfo() takes a single kernel, and expands it into a list
1982% of kernels each incrementally rotated the angle given.
1983%
1984% WARNING: 45 degree rotations only works for 3x3 kernels.
1985% While 90 degree roatations only works for linear and square kernels
1986%
1987% The format of the RotateKernelInfo method is:
1988%
1989% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1990%
1991% A description of each parameter follows:
1992%
1993% o kernel: the Morphology/Convolution kernel
1994%
1995% o angle: angle to rotate in degrees
1996%
1997% This function is only internel to this module, as it is not finalized,
1998% especially with regard to non-orthogonal angles, and rotation of larger
1999% 2D kernels.
2000*/
anthony47f5d062010-05-23 07:47:50 +00002001
2002/* Internal Routine - Return true if two kernels are the same */
2003static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
2004 const KernelInfo *kernel2)
2005{
cristybb503372010-05-27 20:51:26 +00002006 register size_t
anthony47f5d062010-05-23 07:47:50 +00002007 i;
anthony1d45eb92010-05-25 11:13:23 +00002008
2009 /* check size and origin location */
2010 if ( kernel1->width != kernel2->width
2011 || kernel1->height != kernel2->height
2012 || kernel1->x != kernel2->x
2013 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00002014 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002015
2016 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00002017 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00002018 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00002019 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
2020 return MagickFalse;
2021 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2022 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002023 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002024 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2025 return MagickFalse;
2026 }
anthony1d45eb92010-05-25 11:13:23 +00002027
anthony47f5d062010-05-23 07:47:50 +00002028 return MagickTrue;
2029}
2030
2031static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002032{
2033 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002034 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002035 *last;
cristya9a61ad2010-05-13 12:47:41 +00002036
anthony3c10fc82010-05-13 02:40:51 +00002037 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002038 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002039 clone = CloneKernelInfo(last);
2040 RotateKernelInfo(clone, angle);
2041 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002042 break;
cristy84d9b552010-05-24 18:23:54 +00002043 last->next = clone;
2044 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002045 }
cristy84d9b552010-05-24 18:23:54 +00002046 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002047 return;
anthony3c10fc82010-05-13 02:40:51 +00002048}
2049
2050/*
2051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2052% %
2053% %
2054% %
anthony46a369d2010-05-19 02:41:48 +00002055+ C a l c M e t a K e r n a l I n f o %
2056% %
2057% %
2058% %
2059%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2060%
2061% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2062% using the kernel values. This should only ne used if it is not posible to
2063% calculate that meta-data in some easier way.
2064%
2065% It is important that the meta-data is correct before ScaleKernelInfo() is
2066% used to perform kernel normalization.
2067%
2068% The format of the CalcKernelMetaData method is:
2069%
2070% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2071%
2072% A description of each parameter follows:
2073%
2074% o kernel: the Morphology/Convolution kernel to modify
2075%
2076% WARNING: Minimum and Maximum values are assumed to include zero, even if
2077% zero is not part of the kernel (as in Gaussian Derived kernels). This
2078% however is not true for flat-shaped morphological kernels.
2079%
2080% WARNING: Only the specific kernel pointed to is modified, not a list of
2081% multiple kernels.
2082%
2083% This is an internal function and not expected to be useful outside this
2084% module. This could change however.
2085*/
2086static void CalcKernelMetaData(KernelInfo *kernel)
2087{
cristybb503372010-05-27 20:51:26 +00002088 register size_t
anthony46a369d2010-05-19 02:41:48 +00002089 i;
2090
2091 kernel->minimum = kernel->maximum = 0.0;
2092 kernel->negative_range = kernel->positive_range = 0.0;
2093 for (i=0; i < (kernel->width*kernel->height); i++)
2094 {
2095 if ( fabs(kernel->values[i]) < MagickEpsilon )
2096 kernel->values[i] = 0.0;
2097 ( kernel->values[i] < 0)
2098 ? ( kernel->negative_range += kernel->values[i] )
2099 : ( kernel->positive_range += kernel->values[i] );
2100 Minimize(kernel->minimum, kernel->values[i]);
2101 Maximize(kernel->maximum, kernel->values[i]);
2102 }
2103
2104 return;
2105}
2106
2107/*
2108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2109% %
2110% %
2111% %
anthony9eb4f742010-05-18 02:45:54 +00002112% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002113% %
2114% %
2115% %
2116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2117%
anthony9eb4f742010-05-18 02:45:54 +00002118% MorphologyApply() applies a morphological method, multiple times using
2119% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002120%
anthony9eb4f742010-05-18 02:45:54 +00002121% It is basically equivelent to as MorphologyImageChannel() (see below) but
2122% without user controls, that that function extracts and applies to kernels
2123% and morphology methods.
2124%
2125% More specifically kernels are not normalized/scaled/blended by the
2126% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2127% (-bias setting or image->bias) is passed directly to this function,
2128% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002129%
anthony47f5d062010-05-23 07:47:50 +00002130% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002131%
anthony9eb4f742010-05-18 02:45:54 +00002132% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002133% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002134% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002135% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002136%
2137% A description of each parameter follows:
2138%
2139% o image: the image.
2140%
2141% o method: the morphology method to be applied.
2142%
2143% o iterations: apply the operation this many times (or no change).
2144% A value of -1 means loop until no change found.
2145% How this is applied may depend on the morphology method.
2146% Typically this is a value of 1.
2147%
2148% o channel: the channel type.
2149%
2150% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002151% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002152%
anthony47f5d062010-05-23 07:47:50 +00002153% o compose: How to handle or merge multi-kernel results.
2154% If 'Undefined' use default of the Morphology method.
2155% If 'No' force image to be re-iterated by each kernel.
2156% Otherwise merge the results using the mathematical compose
2157% method given.
2158%
2159% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002160%
anthony602ab9b2010-01-05 08:06:50 +00002161% o exception: return any errors or warnings in this structure.
2162%
anthony602ab9b2010-01-05 08:06:50 +00002163*/
2164
anthony930be612010-02-08 04:26:15 +00002165
anthony9eb4f742010-05-18 02:45:54 +00002166/* Apply a Morphology Primative to an image using the given kernel.
2167** Two pre-created images must be provided, no image is created.
2168** Returning the number of pixels that changed.
2169*/
cristybb503372010-05-27 20:51:26 +00002170static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002171 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002172 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002173{
cristy2be15382010-01-21 02:38:03 +00002174#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002175
cristy5f959472010-05-27 22:19:46 +00002176 CacheView
2177 *p_view,
2178 *q_view;
2179
cristybb503372010-05-27 20:51:26 +00002180 ssize_t
anthony29188a82010-01-22 10:12:34 +00002181 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002182 changed;
2183
2184 MagickBooleanType
2185 status;
2186
cristy5f959472010-05-27 22:19:46 +00002187 MagickOffsetType
2188 progress;
anthony602ab9b2010-01-05 08:06:50 +00002189
anthonye4d89962010-05-29 10:53:11 +00002190 assert(image != (Image *) NULL);
2191 assert(image->signature == MagickSignature);
2192 assert(result_image != (Image *) NULL);
2193 assert(result_image->signature == MagickSignature);
2194 assert(kernel != (KernelInfo *) NULL);
2195 assert(kernel->signature == MagickSignature);
2196 assert(exception != (ExceptionInfo *) NULL);
2197 assert(exception->signature == MagickSignature);
2198
anthony602ab9b2010-01-05 08:06:50 +00002199 status=MagickTrue;
2200 changed=0;
2201 progress=0;
2202
anthony602ab9b2010-01-05 08:06:50 +00002203 p_view=AcquireCacheView(image);
2204 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002205
anthonycc6c8362010-01-25 04:14:01 +00002206 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002207 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002208 */
cristyc99304f2010-02-01 15:26:27 +00002209 offx = kernel->x;
2210 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002211 switch(method) {
anthony930be612010-02-08 04:26:15 +00002212 case ConvolveMorphology:
2213 case DilateMorphology:
2214 case DilateIntensityMorphology:
2215 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002216 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002217 offx = (ssize_t) kernel->width-offx-1;
2218 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002219 break;
anthony5ef8e942010-05-11 06:51:12 +00002220 case ErodeMorphology:
2221 case ErodeIntensityMorphology:
2222 case HitAndMissMorphology:
2223 case ThinningMorphology:
2224 case ThickenMorphology:
2225 /* kernel is user as is, without reflection */
2226 break;
anthony930be612010-02-08 04:26:15 +00002227 default:
anthony9eb4f742010-05-18 02:45:54 +00002228 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002229 break;
anthony29188a82010-01-22 10:12:34 +00002230 }
2231
anthony602ab9b2010-01-05 08:06:50 +00002232#if defined(MAGICKCORE_OPENMP_SUPPORT)
2233 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2234#endif
cristybb503372010-05-27 20:51:26 +00002235 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002236 {
2237 MagickBooleanType
2238 sync;
2239
2240 register const PixelPacket
2241 *restrict p;
2242
2243 register const IndexPacket
2244 *restrict p_indexes;
2245
2246 register PixelPacket
2247 *restrict q;
2248
2249 register IndexPacket
2250 *restrict q_indexes;
2251
cristybb503372010-05-27 20:51:26 +00002252 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002253 x;
2254
cristybb503372010-05-27 20:51:26 +00002255 size_t
anthony602ab9b2010-01-05 08:06:50 +00002256 r;
2257
2258 if (status == MagickFalse)
2259 continue;
anthony29188a82010-01-22 10:12:34 +00002260 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2261 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002262 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2263 exception);
2264 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2265 {
2266 status=MagickFalse;
2267 continue;
2268 }
2269 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2270 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002271 r = (image->columns+kernel->width)*offy+offx; /* constant */
2272
cristybb503372010-05-27 20:51:26 +00002273 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002274 {
cristybb503372010-05-27 20:51:26 +00002275 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002276 v;
2277
cristybb503372010-05-27 20:51:26 +00002278 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002279 u;
2280
2281 register const double
2282 *restrict k;
2283
2284 register const PixelPacket
2285 *restrict k_pixels;
2286
2287 register const IndexPacket
2288 *restrict k_indexes;
2289
2290 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002291 result,
2292 min,
2293 max;
anthony602ab9b2010-01-05 08:06:50 +00002294
anthony29188a82010-01-22 10:12:34 +00002295 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002296 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002297 */
anthony602ab9b2010-01-05 08:06:50 +00002298 *q = p[r];
2299 if (image->colorspace == CMYKColorspace)
2300 q_indexes[x] = p_indexes[r];
2301
anthony5ef8e942010-05-11 06:51:12 +00002302 /* Defaults */
2303 min.red =
2304 min.green =
2305 min.blue =
2306 min.opacity =
2307 min.index = (MagickRealType) QuantumRange;
2308 max.red =
2309 max.green =
2310 max.blue =
2311 max.opacity =
2312 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002313 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002314 result.red = (MagickRealType) p[r].red;
2315 result.green = (MagickRealType) p[r].green;
2316 result.blue = (MagickRealType) p[r].blue;
2317 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002318 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002319 if ( image->colorspace == CMYKColorspace)
2320 result.index = (MagickRealType) p_indexes[r];
2321
anthony602ab9b2010-01-05 08:06:50 +00002322 switch (method) {
2323 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002324 /* Set the user defined bias of the weighted average output */
2325 result.red =
2326 result.green =
2327 result.blue =
2328 result.opacity =
2329 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002330 break;
anthony4fd27e22010-02-07 08:17:18 +00002331 case DilateIntensityMorphology:
2332 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002333 /* use a boolean flag indicating when first match found */
2334 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002335 break;
anthony602ab9b2010-01-05 08:06:50 +00002336 default:
anthony602ab9b2010-01-05 08:06:50 +00002337 break;
2338 }
2339
2340 switch ( method ) {
2341 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002342 /* Weighted Average of pixels using reflected kernel
2343 **
2344 ** NOTE for correct working of this operation for asymetrical
2345 ** kernels, the kernel needs to be applied in its reflected form.
2346 ** That is its values needs to be reversed.
2347 **
2348 ** Correlation is actually the same as this but without reflecting
2349 ** the kernel, and thus 'lower-level' that Convolution. However
2350 ** as Convolution is the more common method used, and it does not
2351 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002352 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002353 **
2354 ** Correlation will have its kernel reflected before calling
2355 ** this function to do a Convolve.
2356 **
2357 ** For more details of Correlation vs Convolution see
2358 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2359 */
anthony5ef8e942010-05-11 06:51:12 +00002360 if (((channel & SyncChannels) != 0 ) &&
2361 (image->matte == MagickTrue))
2362 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2363 ** Weight the color channels with Alpha Channel so that
2364 ** transparent pixels are not part of the results.
2365 */
anthony602ab9b2010-01-05 08:06:50 +00002366 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002367 alpha, /* color channel weighting : kernel*alpha */
2368 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002369
2370 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002371 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002372 k_pixels = p;
2373 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002374 for (v=0; v < (ssize_t) kernel->height; v++) {
2375 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002376 if ( IsNan(*k) ) continue;
2377 alpha=(*k)*(QuantumScale*(QuantumRange-
2378 k_pixels[u].opacity));
2379 gamma += alpha;
2380 result.red += alpha*k_pixels[u].red;
2381 result.green += alpha*k_pixels[u].green;
2382 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002383 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002384 if ( image->colorspace == CMYKColorspace)
2385 result.index += alpha*k_indexes[u];
2386 }
2387 k_pixels += image->columns+kernel->width;
2388 k_indexes += image->columns+kernel->width;
2389 }
2390 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002391 result.red *= gamma;
2392 result.green *= gamma;
2393 result.blue *= gamma;
2394 result.opacity *= gamma;
2395 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002396 }
anthony5ef8e942010-05-11 06:51:12 +00002397 else
2398 {
2399 /* No 'Sync' flag, or no Alpha involved.
2400 ** Convolution is simple individual channel weigthed sum.
2401 */
2402 k = &kernel->values[ kernel->width*kernel->height-1 ];
2403 k_pixels = p;
2404 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002405 for (v=0; v < (ssize_t) kernel->height; v++) {
2406 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002407 if ( IsNan(*k) ) continue;
2408 result.red += (*k)*k_pixels[u].red;
2409 result.green += (*k)*k_pixels[u].green;
2410 result.blue += (*k)*k_pixels[u].blue;
2411 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2412 if ( image->colorspace == CMYKColorspace)
2413 result.index += (*k)*k_indexes[u];
2414 }
2415 k_pixels += image->columns+kernel->width;
2416 k_indexes += image->columns+kernel->width;
2417 }
2418 }
anthony602ab9b2010-01-05 08:06:50 +00002419 break;
2420
anthony4fd27e22010-02-07 08:17:18 +00002421 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002422 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002423 **
2424 ** NOTE that the kernel is not reflected for this operation!
2425 **
2426 ** NOTE: in normal Greyscale Morphology, the kernel value should
2427 ** be added to the real value, this is currently not done, due to
2428 ** the nature of the boolean kernels being used.
2429 */
anthony4fd27e22010-02-07 08:17:18 +00002430 k = kernel->values;
2431 k_pixels = p;
2432 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002433 for (v=0; v < (ssize_t) kernel->height; v++) {
2434 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002435 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002436 Minimize(min.red, (double) k_pixels[u].red);
2437 Minimize(min.green, (double) k_pixels[u].green);
2438 Minimize(min.blue, (double) k_pixels[u].blue);
2439 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002440 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002441 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002442 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002443 }
2444 k_pixels += image->columns+kernel->width;
2445 k_indexes += image->columns+kernel->width;
2446 }
2447 break;
2448
anthony5ef8e942010-05-11 06:51:12 +00002449
anthony83ba99b2010-01-24 08:48:15 +00002450 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002451 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002452 **
2453 ** NOTE for correct working of this operation for asymetrical
2454 ** kernels, the kernel needs to be applied in its reflected form.
2455 ** That is its values needs to be reversed.
2456 **
2457 ** NOTE: in normal Greyscale Morphology, the kernel value should
2458 ** be added to the real value, this is currently not done, due to
2459 ** the nature of the boolean kernels being used.
2460 **
2461 */
anthony29188a82010-01-22 10:12:34 +00002462 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002463 k_pixels = p;
2464 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002465 for (v=0; v < (ssize_t) kernel->height; v++) {
2466 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002467 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002468 Maximize(max.red, (double) k_pixels[u].red);
2469 Maximize(max.green, (double) k_pixels[u].green);
2470 Maximize(max.blue, (double) k_pixels[u].blue);
2471 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002472 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002473 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002474 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002475 }
2476 k_pixels += image->columns+kernel->width;
2477 k_indexes += image->columns+kernel->width;
2478 }
anthony602ab9b2010-01-05 08:06:50 +00002479 break;
2480
anthony5ef8e942010-05-11 06:51:12 +00002481 case HitAndMissMorphology:
2482 case ThinningMorphology:
2483 case ThickenMorphology:
2484 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2485 **
2486 ** NOTE that the kernel is not reflected for this operation,
2487 ** and consists of both foreground and background pixel
2488 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2489 ** with either Nan or 0.5 values for don't care.
2490 **
2491 ** Note that this can produce negative results, though really
2492 ** only a positive match has any real value.
2493 */
2494 k = kernel->values;
2495 k_pixels = p;
2496 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002497 for (v=0; v < (ssize_t) kernel->height; v++) {
2498 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002499 if ( IsNan(*k) ) continue;
2500 if ( (*k) > 0.7 )
2501 { /* minimim of foreground pixels */
2502 Minimize(min.red, (double) k_pixels[u].red);
2503 Minimize(min.green, (double) k_pixels[u].green);
2504 Minimize(min.blue, (double) k_pixels[u].blue);
2505 Minimize(min.opacity,
2506 QuantumRange-(double) k_pixels[u].opacity);
2507 if ( image->colorspace == CMYKColorspace)
2508 Minimize(min.index, (double) k_indexes[u]);
2509 }
2510 else if ( (*k) < 0.3 )
2511 { /* maximum of background pixels */
2512 Maximize(max.red, (double) k_pixels[u].red);
2513 Maximize(max.green, (double) k_pixels[u].green);
2514 Maximize(max.blue, (double) k_pixels[u].blue);
2515 Maximize(max.opacity,
2516 QuantumRange-(double) k_pixels[u].opacity);
2517 if ( image->colorspace == CMYKColorspace)
2518 Maximize(max.index, (double) k_indexes[u]);
2519 }
2520 }
2521 k_pixels += image->columns+kernel->width;
2522 k_indexes += image->columns+kernel->width;
2523 }
2524 /* Pattern Match only if min fg larger than min bg pixels */
2525 min.red -= max.red; Maximize( min.red, 0.0 );
2526 min.green -= max.green; Maximize( min.green, 0.0 );
2527 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2528 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2529 min.index -= max.index; Maximize( min.index, 0.0 );
2530 break;
2531
anthony4fd27e22010-02-07 08:17:18 +00002532 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002533 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2534 **
2535 ** WARNING: the intensity test fails for CMYK and does not
2536 ** take into account the moderating effect of teh alpha channel
2537 ** on the intensity.
2538 **
2539 ** NOTE that the kernel is not reflected for this operation!
2540 */
anthony602ab9b2010-01-05 08:06:50 +00002541 k = kernel->values;
2542 k_pixels = p;
2543 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002544 for (v=0; v < (ssize_t) kernel->height; v++) {
2545 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002546 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002547 if ( result.red == 0.0 ||
2548 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2549 /* copy the whole pixel - no channel selection */
2550 *q = k_pixels[u];
2551 if ( result.red > 0.0 ) changed++;
2552 result.red = 1.0;
2553 }
anthony602ab9b2010-01-05 08:06:50 +00002554 }
2555 k_pixels += image->columns+kernel->width;
2556 k_indexes += image->columns+kernel->width;
2557 }
anthony602ab9b2010-01-05 08:06:50 +00002558 break;
2559
anthony83ba99b2010-01-24 08:48:15 +00002560 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002561 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2562 **
2563 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002564 ** take into account the moderating effect of the alpha channel
2565 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002566 **
2567 ** NOTE for correct working of this operation for asymetrical
2568 ** kernels, the kernel needs to be applied in its reflected form.
2569 ** That is its values needs to be reversed.
2570 */
anthony29188a82010-01-22 10:12:34 +00002571 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002572 k_pixels = p;
2573 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002574 for (v=0; v < (ssize_t) kernel->height; v++) {
2575 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002576 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2577 if ( result.red == 0.0 ||
2578 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2579 /* copy the whole pixel - no channel selection */
2580 *q = k_pixels[u];
2581 if ( result.red > 0.0 ) changed++;
2582 result.red = 1.0;
2583 }
anthony602ab9b2010-01-05 08:06:50 +00002584 }
2585 k_pixels += image->columns+kernel->width;
2586 k_indexes += image->columns+kernel->width;
2587 }
anthony602ab9b2010-01-05 08:06:50 +00002588 break;
2589
anthony5ef8e942010-05-11 06:51:12 +00002590
anthony602ab9b2010-01-05 08:06:50 +00002591 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002592 /* Add kernel Value and select the minimum value found.
2593 ** The result is a iterative distance from edge of image shape.
2594 **
2595 ** All Distance Kernels are symetrical, but that may not always
2596 ** be the case. For example how about a distance from left edges?
2597 ** To work correctly with asymetrical kernels the reflected kernel
2598 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002599 **
2600 ** Actually this is really a GreyErode with a negative kernel!
2601 **
anthony930be612010-02-08 04:26:15 +00002602 */
anthony29188a82010-01-22 10:12:34 +00002603 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002604 k_pixels = p;
2605 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002606 for (v=0; v < (ssize_t) kernel->height; v++) {
2607 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002608 if ( IsNan(*k) ) continue;
2609 Minimize(result.red, (*k)+k_pixels[u].red);
2610 Minimize(result.green, (*k)+k_pixels[u].green);
2611 Minimize(result.blue, (*k)+k_pixels[u].blue);
2612 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2613 if ( image->colorspace == CMYKColorspace)
2614 Minimize(result.index, (*k)+k_indexes[u]);
2615 }
2616 k_pixels += image->columns+kernel->width;
2617 k_indexes += image->columns+kernel->width;
2618 }
anthony602ab9b2010-01-05 08:06:50 +00002619 break;
2620
2621 case UndefinedMorphology:
2622 default:
2623 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002624 }
anthony5ef8e942010-05-11 06:51:12 +00002625 /* Final mathematics of results (combine with original image?)
2626 **
2627 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2628 ** be done here but works better with iteration as a image difference
2629 ** in the controling function (below). Thicken and Thinning however
2630 ** should be done here so thay can be iterated correctly.
2631 */
2632 switch ( method ) {
2633 case HitAndMissMorphology:
2634 case ErodeMorphology:
2635 result = min; /* minimum of neighbourhood */
2636 break;
2637 case DilateMorphology:
2638 result = max; /* maximum of neighbourhood */
2639 break;
2640 case ThinningMorphology:
2641 /* subtract pattern match from original */
2642 result.red -= min.red;
2643 result.green -= min.green;
2644 result.blue -= min.blue;
2645 result.opacity -= min.opacity;
2646 result.index -= min.index;
2647 break;
2648 case ThickenMorphology:
2649 /* Union with original image (maximize) - or should this be + */
2650 Maximize( result.red, min.red );
2651 Maximize( result.green, min.green );
2652 Maximize( result.blue, min.blue );
2653 Maximize( result.opacity, min.opacity );
2654 Maximize( result.index, min.index );
2655 break;
2656 default:
2657 /* result directly calculated or assigned */
2658 break;
2659 }
2660 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002661 switch ( method ) {
2662 case UndefinedMorphology:
2663 case DilateIntensityMorphology:
2664 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002665 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002666 default:
anthony83ba99b2010-01-24 08:48:15 +00002667 if ((channel & RedChannel) != 0)
2668 q->red = ClampToQuantum(result.red);
2669 if ((channel & GreenChannel) != 0)
2670 q->green = ClampToQuantum(result.green);
2671 if ((channel & BlueChannel) != 0)
2672 q->blue = ClampToQuantum(result.blue);
2673 if ((channel & OpacityChannel) != 0
2674 && image->matte == MagickTrue )
2675 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2676 if ((channel & IndexChannel) != 0
2677 && image->colorspace == CMYKColorspace)
2678 q_indexes[x] = ClampToQuantum(result.index);
2679 break;
2680 }
anthony5ef8e942010-05-11 06:51:12 +00002681 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002682 if ( ( p[r].red != q->red )
2683 || ( p[r].green != q->green )
2684 || ( p[r].blue != q->blue )
2685 || ( p[r].opacity != q->opacity )
2686 || ( image->colorspace == CMYKColorspace &&
2687 p_indexes[r] != q_indexes[x] ) )
2688 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002689 p++;
2690 q++;
anthony83ba99b2010-01-24 08:48:15 +00002691 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002692 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2693 if (sync == MagickFalse)
2694 status=MagickFalse;
2695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2696 {
2697 MagickBooleanType
2698 proceed;
2699
2700#if defined(MAGICKCORE_OPENMP_SUPPORT)
2701 #pragma omp critical (MagickCore_MorphologyImage)
2702#endif
2703 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2704 if (proceed == MagickFalse)
2705 status=MagickFalse;
2706 }
anthony83ba99b2010-01-24 08:48:15 +00002707 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002708 result_image->type=image->type;
2709 q_view=DestroyCacheView(q_view);
2710 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002711 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002712}
2713
anthony4fd27e22010-02-07 08:17:18 +00002714
anthony9eb4f742010-05-18 02:45:54 +00002715MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002716 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002717 const KernelInfo *kernel, const CompositeOperator compose,
2718 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002719{
2720 Image
anthony47f5d062010-05-23 07:47:50 +00002721 *curr_image, /* Image we are working with or iterating */
2722 *work_image, /* secondary image for primative iteration */
2723 *save_image, /* saved image - for 'edge' method only */
2724 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002725
anthony4fd27e22010-02-07 08:17:18 +00002726 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002727 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2728 *norm_kernel, /* the current normal un-reflected kernel */
2729 *rflt_kernel, /* the current reflected kernel (if needed) */
2730 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002731
2732 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002733 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002734
2735 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002736 rslt_compose; /* multi-kernel compose method for results to use */
2737
2738 MagickBooleanType
2739 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002740
cristybb503372010-05-27 20:51:26 +00002741 size_t
anthony47f5d062010-05-23 07:47:50 +00002742 method_loop, /* Loop 1: number of compound method iterations */
2743 method_limit, /* maximum number of compound method iterations */
2744 kernel_number, /* Loop 2: the kernel number being applied */
2745 stage_loop, /* Loop 3: primative loop for compound morphology */
2746 stage_limit, /* how many primatives in this compound */
2747 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2748 kernel_limit, /* number of times to iterate kernel */
2749 count, /* total count of primative steps applied */
2750 changed, /* number pixels changed by last primative operation */
2751 kernel_changed, /* total count of changed using iterated kernel */
2752 method_changed; /* total count of changed over method iteration */
2753
2754 char
2755 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002756
anthony602ab9b2010-01-05 08:06:50 +00002757 assert(image != (Image *) NULL);
2758 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002759 assert(kernel != (KernelInfo *) NULL);
2760 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002761 assert(exception != (ExceptionInfo *) NULL);
2762 assert(exception->signature == MagickSignature);
2763
anthonyc3e48252010-05-24 12:43:11 +00002764 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002765 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002766 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002767
cristybb503372010-05-27 20:51:26 +00002768 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002769 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2770 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002771
cristye96405a2010-05-19 02:24:31 +00002772 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2773 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002774
anthony9eb4f742010-05-18 02:45:54 +00002775 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002776 curr_image = (Image *) image;
2777 work_image = save_image = rslt_image = (Image *) NULL;
2778 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002779
anthony47f5d062010-05-23 07:47:50 +00002780 /* Initialize specific methods
2781 * + which loop should use the given iteratations
2782 * + how many primatives make up the compound morphology
2783 * + multi-kernel compose method to use (by default)
2784 */
2785 method_limit = 1; /* just do method once, unless otherwise set */
2786 stage_limit = 1; /* assume method is not a compount */
2787 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002788 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002789 case SmoothMorphology: /* 4 primative compound morphology */
2790 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002791 break;
anthony47f5d062010-05-23 07:47:50 +00002792 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002793 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002794 case TopHatMorphology:
2795 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002796 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002797 case BottomHatMorphology:
2798 case EdgeMorphology:
2799 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002800 break;
2801 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002802 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002803 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002804 break;
anthonyc3e48252010-05-24 12:43:11 +00002805 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002806 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002807 method_limit = kernel_limit; /* iterate method with each kernel */
2808 kernel_limit = 1; /* do not do kernel iteration */
anthonye4d89962010-05-29 10:53:11 +00002809 case DistanceMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002810 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002811 break;
2812 default:
anthony930be612010-02-08 04:26:15 +00002813 break;
anthony602ab9b2010-01-05 08:06:50 +00002814 }
2815
anthonyc3e48252010-05-24 12:43:11 +00002816 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002817 if ( compose != UndefinedCompositeOp )
2818 rslt_compose = compose; /* override default composition for method */
2819 if ( rslt_compose == UndefinedCompositeOp )
2820 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2821
anthonyc3e48252010-05-24 12:43:11 +00002822 /* Some methods require a reflected kernel to use with primatives.
2823 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002824 switch ( method ) {
2825 case CorrelateMorphology:
2826 case CloseMorphology:
2827 case CloseIntensityMorphology:
2828 case BottomHatMorphology:
2829 case SmoothMorphology:
2830 reflected_kernel = CloneKernelInfo(kernel);
2831 if (reflected_kernel == (KernelInfo *) NULL)
2832 goto error_cleanup;
2833 RotateKernelInfo(reflected_kernel,180);
2834 break;
2835 default:
2836 break;
anthony9eb4f742010-05-18 02:45:54 +00002837 }
anthony7a01dcf2010-05-11 12:25:52 +00002838
anthony47f5d062010-05-23 07:47:50 +00002839 /* Loop 1: iterate the compound method */
2840 method_loop = 0;
2841 method_changed = 1;
2842 while ( method_loop < method_limit && method_changed > 0 ) {
2843 method_loop++;
2844 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002845
anthony47f5d062010-05-23 07:47:50 +00002846 /* Loop 2: iterate over each kernel in a multi-kernel list */
2847 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00002848 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00002849 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00002850
anthony47f5d062010-05-23 07:47:50 +00002851 kernel_number = 0;
2852 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002853
anthony47f5d062010-05-23 07:47:50 +00002854 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2855 stage_loop = 0; /* the compound morphology stage number */
2856 while ( stage_loop < stage_limit ) {
2857 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002858
anthony47f5d062010-05-23 07:47:50 +00002859 /* Select primative morphology for this stage of compound method */
2860 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002861 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002862 switch( method ) {
2863 case ErodeMorphology: /* just erode */
2864 case EdgeInMorphology: /* erode and image difference */
2865 primative = ErodeMorphology;
2866 break;
2867 case DilateMorphology: /* just dilate */
2868 case EdgeOutMorphology: /* dilate and image difference */
2869 primative = DilateMorphology;
2870 break;
2871 case OpenMorphology: /* erode then dialate */
2872 case TopHatMorphology: /* open and image difference */
2873 primative = ErodeMorphology;
2874 if ( stage_loop == 2 )
2875 primative = DilateMorphology;
2876 break;
2877 case OpenIntensityMorphology:
2878 primative = ErodeIntensityMorphology;
2879 if ( stage_loop == 2 )
2880 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00002881 break;
anthony47f5d062010-05-23 07:47:50 +00002882 case CloseMorphology: /* dilate, then erode */
2883 case BottomHatMorphology: /* close and image difference */
2884 this_kernel = rflt_kernel; /* use the reflected kernel */
2885 primative = DilateMorphology;
2886 if ( stage_loop == 2 )
2887 primative = ErodeMorphology;
2888 break;
2889 case CloseIntensityMorphology:
2890 this_kernel = rflt_kernel; /* use the reflected kernel */
2891 primative = DilateIntensityMorphology;
2892 if ( stage_loop == 2 )
2893 primative = ErodeIntensityMorphology;
2894 break;
2895 case SmoothMorphology: /* open, close */
2896 switch ( stage_loop ) {
2897 case 1: /* start an open method, which starts with Erode */
2898 primative = ErodeMorphology;
2899 break;
2900 case 2: /* now Dilate the Erode */
2901 primative = DilateMorphology;
2902 break;
2903 case 3: /* Reflect kernel a close */
2904 this_kernel = rflt_kernel; /* use the reflected kernel */
2905 primative = DilateMorphology;
2906 break;
2907 case 4: /* Finish the Close */
2908 this_kernel = rflt_kernel; /* use the reflected kernel */
2909 primative = ErodeMorphology;
2910 break;
2911 }
2912 break;
2913 case EdgeMorphology: /* dilate and erode difference */
2914 primative = DilateMorphology;
2915 if ( stage_loop == 2 ) {
2916 save_image = curr_image; /* save the image difference */
2917 curr_image = (Image *) image;
2918 primative = ErodeMorphology;
2919 }
2920 break;
2921 case CorrelateMorphology:
2922 /* A Correlation is a Convolution with a reflected kernel.
2923 ** However a Convolution is a weighted sum using a reflected
2924 ** kernel. It may seem stange to convert a Correlation into a
2925 ** Convolution as the Correlation is the simplier method, but
2926 ** Convolution is much more commonly used, and it makes sense to
2927 ** implement it directly so as to avoid the need to duplicate the
2928 ** kernel when it is not required (which is typically the
2929 ** default).
2930 */
2931 this_kernel = rflt_kernel; /* use the reflected kernel */
2932 primative = ConvolveMorphology;
2933 break;
2934 default:
anthony47f5d062010-05-23 07:47:50 +00002935 break;
2936 }
anthonye4d89962010-05-29 10:53:11 +00002937 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00002938
anthony47f5d062010-05-23 07:47:50 +00002939 /* Extra information for debugging compound operations */
2940 if ( verbose == MagickTrue ) {
2941 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002942 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002943 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2944 (unsigned long) method_loop,(unsigned long) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00002945 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002946 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002947 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2948 (unsigned long) method_loop);
anthony47f5d062010-05-23 07:47:50 +00002949 else
2950 v_info[0] = '\0';
2951 }
2952
2953 /* Loop 4: Iterate the kernel with primative */
2954 kernel_loop = 0;
2955 kernel_changed = 0;
2956 changed = 1;
2957 while ( kernel_loop < kernel_limit && changed > 0 ) {
2958 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002959
2960 /* Create a destination image, if not yet defined */
2961 if ( work_image == (Image *) NULL )
2962 {
2963 work_image=CloneImage(image,0,0,MagickTrue,exception);
2964 if (work_image == (Image *) NULL)
2965 goto error_cleanup;
2966 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2967 {
2968 InheritException(exception,&work_image->exception);
2969 goto error_cleanup;
2970 }
2971 }
2972
anthonye4d89962010-05-29 10:53:11 +00002973 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002974 count++;
anthony47f5d062010-05-23 07:47:50 +00002975 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002976 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002977 kernel_changed += changed;
2978 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002979
anthony47f5d062010-05-23 07:47:50 +00002980 if ( verbose == MagickTrue ) {
2981 if ( kernel_loop > 1 )
2982 fprintf(stderr, "\n"); /* add end-of-line from previous */
2983 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2984 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2985 ( this_kernel == rflt_kernel ) ? "*" : "",
cristyf2faecf2010-05-28 19:19:36 +00002986 (unsigned long) method_loop+kernel_loop-1,(unsigned long)
2987 kernel_number,(unsigned long) count,(unsigned long) changed);
anthony47f5d062010-05-23 07:47:50 +00002988 }
anthony9eb4f742010-05-18 02:45:54 +00002989 /* prepare next loop */
2990 { Image *tmp = work_image; /* swap images for iteration */
2991 work_image = curr_image;
2992 curr_image = tmp;
2993 }
2994 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002995 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002996
anthony47f5d062010-05-23 07:47:50 +00002997 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002998
anthony47f5d062010-05-23 07:47:50 +00002999 if ( verbose == MagickTrue && kernel_changed != changed )
cristyf2faecf2010-05-28 19:19:36 +00003000 fprintf(stderr, " Total %lu",(unsigned long) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00003001 if ( verbose == MagickTrue && stage_loop < stage_limit )
3002 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00003003
3004#if 0
anthonye4d89962010-05-29 10:53:11 +00003005 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
3006 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
3007 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
3008 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
3009 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003010#endif
3011
anthony47f5d062010-05-23 07:47:50 +00003012 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00003013
anthony47f5d062010-05-23 07:47:50 +00003014 /* Final Post-processing for some Compound Methods
3015 **
3016 ** The removal of any 'Sync' channel flag in the Image Compositon
3017 ** below ensures the methematical compose method is applied in a
3018 ** purely mathematical way, and only to the selected channels.
3019 ** Turn off SVG composition 'alpha blending'.
3020 */
3021 switch( method ) {
3022 case EdgeOutMorphology:
3023 case EdgeInMorphology:
3024 case TopHatMorphology:
3025 case BottomHatMorphology:
3026 if ( verbose == MagickTrue )
3027 fprintf(stderr, "\n%s: Difference with original image",
3028 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3029 (void) CompositeImageChannel(curr_image,
3030 (ChannelType) (channel & ~SyncChannels),
3031 DifferenceCompositeOp, image, 0, 0);
3032 break;
3033 case EdgeMorphology:
3034 if ( verbose == MagickTrue )
3035 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3036 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3037 (void) CompositeImageChannel(curr_image,
3038 (ChannelType) (channel & ~SyncChannels),
3039 DifferenceCompositeOp, save_image, 0, 0);
3040 save_image = DestroyImage(save_image); /* finished with save image */
3041 break;
3042 default:
3043 break;
3044 }
3045
3046 /* multi-kernel handling: re-iterate, or compose results */
3047 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003048 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003049 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003050 { if ( verbose == MagickTrue ) {
3051 if ( this_kernel->next != (KernelInfo *) NULL )
3052 fprintf(stderr, " (re-iterate)");
3053 else
3054 fprintf(stderr, " (done)");
3055 }
3056 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003057 }
anthony47f5d062010-05-23 07:47:50 +00003058 else if ( rslt_image == (Image *) NULL)
3059 { if ( verbose == MagickTrue )
3060 fprintf(stderr, " (save for compose)");
3061 rslt_image = curr_image;
3062 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003063 }
anthony47f5d062010-05-23 07:47:50 +00003064 else
3065 { /* add the new 'current' result to the composition
3066 **
3067 ** The removal of any 'Sync' channel flag in the Image Compositon
3068 ** below ensures the methematical compose method is applied in a
3069 ** purely mathematical way, and only to the selected channels.
3070 ** Turn off SVG composition 'alpha blending'.
3071 */
3072 if ( verbose == MagickTrue )
3073 fprintf(stderr, " (compose \"%s\")",
3074 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3075 (void) CompositeImageChannel(rslt_image,
3076 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3077 curr_image, 0, 0);
3078 curr_image = (Image *) image; /* continue with original image */
3079 }
3080 if ( verbose == MagickTrue )
3081 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003082
anthony47f5d062010-05-23 07:47:50 +00003083 /* loop to the next kernel in a multi-kernel list */
3084 norm_kernel = norm_kernel->next;
3085 if ( rflt_kernel != (KernelInfo *) NULL )
3086 rflt_kernel = rflt_kernel->next;
3087 kernel_number++;
3088 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003089
anthony47f5d062010-05-23 07:47:50 +00003090 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003091
anthony9eb4f742010-05-18 02:45:54 +00003092 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003093
anthony47f5d062010-05-23 07:47:50 +00003094 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003095error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003096 if ( curr_image != (Image *) NULL &&
3097 curr_image != rslt_image &&
3098 curr_image != image )
3099 curr_image = DestroyImage(curr_image);
3100 if ( rslt_image != (Image *) NULL )
3101 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003102exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003103 if ( curr_image != (Image *) NULL &&
3104 curr_image != rslt_image &&
3105 curr_image != image )
3106 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003107 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003108 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003109 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003110 save_image = DestroyImage(save_image);
3111 if ( reflected_kernel != (KernelInfo *) NULL )
3112 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3113 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003114}
3115
3116/*
3117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3118% %
3119% %
3120% %
3121% M o r p h o l o g y I m a g e C h a n n e l %
3122% %
3123% %
3124% %
3125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3126%
3127% MorphologyImageChannel() applies a user supplied kernel to the image
3128% according to the given mophology method.
3129%
3130% This function applies any and all user defined settings before calling
3131% the above internal function MorphologyApply().
3132%
3133% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003134% * Output Bias for Convolution and correlation ("-bias")
3135% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3136% This can also includes the addition of a scaled unity kernel.
3137% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003138%
3139% The format of the MorphologyImage method is:
3140%
3141% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003142% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003143%
3144% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003145% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003146% KernelInfo *kernel,ExceptionInfo *exception)
3147%
3148% A description of each parameter follows:
3149%
3150% o image: the image.
3151%
3152% o method: the morphology method to be applied.
3153%
3154% o iterations: apply the operation this many times (or no change).
3155% A value of -1 means loop until no change found.
3156% How this is applied may depend on the morphology method.
3157% Typically this is a value of 1.
3158%
3159% o channel: the channel type.
3160%
3161% o kernel: An array of double representing the morphology kernel.
3162% Warning: kernel may be normalized for the Convolve method.
3163%
3164% o exception: return any errors or warnings in this structure.
3165%
3166*/
3167
3168MagickExport Image *MorphologyImageChannel(const Image *image,
3169 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003170 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003171{
3172 const char
3173 *artifact;
3174
3175 KernelInfo
3176 *curr_kernel;
3177
anthony47f5d062010-05-23 07:47:50 +00003178 CompositeOperator
3179 compose;
3180
anthony9eb4f742010-05-18 02:45:54 +00003181 Image
3182 *morphology_image;
3183
3184
anthony46a369d2010-05-19 02:41:48 +00003185 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3186 * This is done BEFORE the ShowKernelInfo() function is called so that
3187 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003188 */
3189 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003190 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003191 {
3192 artifact = GetImageArtifact(image,"convolve:scale");
3193 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003194 if ( curr_kernel == kernel )
3195 curr_kernel = CloneKernelInfo(kernel);
3196 if (curr_kernel == (KernelInfo *) NULL) {
3197 curr_kernel=DestroyKernelInfo(curr_kernel);
3198 return((Image *) NULL);
3199 }
anthony46a369d2010-05-19 02:41:48 +00003200 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003201 }
3202 }
3203
3204 /* display the (normalized) kernel via stderr */
3205 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003206 if ( artifact == (const char *) NULL)
3207 artifact = GetImageArtifact(image,"convolve:showkernel");
3208 if ( artifact == (const char *) NULL)
3209 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003210 if ( artifact != (const char *) NULL)
3211 ShowKernelInfo(curr_kernel);
3212
anthony47f5d062010-05-23 07:47:50 +00003213 /* override the default handling of multi-kernel morphology results
3214 * if 'Undefined' use the default method
3215 * if 'None' (default for 'Convolve') re-iterate previous result
3216 * otherwise merge resulting images using compose method given
3217 */
3218 compose = UndefinedCompositeOp; /* use default for method */
3219 artifact = GetImageArtifact(image,"morphology:compose");
3220 if ( artifact != (const char *) NULL)
3221 compose = (CompositeOperator) ParseMagickOption(
3222 MagickComposeOptions,MagickFalse,artifact);
3223
anthony9eb4f742010-05-18 02:45:54 +00003224 /* Apply the Morphology */
3225 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003226 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003227
3228 /* Cleanup and Exit */
3229 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003230 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003231 return(morphology_image);
3232}
3233
3234MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003235 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003236 *exception)
3237{
3238 Image
3239 *morphology_image;
3240
3241 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3242 iterations,kernel,exception);
3243 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003244}
anthony83ba99b2010-01-24 08:48:15 +00003245
3246/*
3247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3248% %
3249% %
3250% %
anthony4fd27e22010-02-07 08:17:18 +00003251+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003252% %
3253% %
3254% %
3255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3256%
anthony46a369d2010-05-19 02:41:48 +00003257% RotateKernelInfo() rotates the kernel by the angle given.
3258%
3259% Currently it is restricted to 90 degree angles, of either 1D kernels
3260% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3261% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003262%
anthony4fd27e22010-02-07 08:17:18 +00003263% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003264%
anthony4fd27e22010-02-07 08:17:18 +00003265% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003266%
3267% A description of each parameter follows:
3268%
3269% o kernel: the Morphology/Convolution kernel
3270%
3271% o angle: angle to rotate in degrees
3272%
anthony46a369d2010-05-19 02:41:48 +00003273% This function is currently internal to this module only, but can be exported
3274% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003275*/
anthony4fd27e22010-02-07 08:17:18 +00003276static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003277{
anthony1b2bc0a2010-05-12 05:25:22 +00003278 /* angle the lower kernels first */
3279 if ( kernel->next != (KernelInfo *) NULL)
3280 RotateKernelInfo(kernel->next, angle);
3281
anthony83ba99b2010-01-24 08:48:15 +00003282 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3283 **
3284 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3285 */
3286
3287 /* Modulus the angle */
3288 angle = fmod(angle, 360.0);
3289 if ( angle < 0 )
3290 angle += 360.0;
3291
anthony3c10fc82010-05-13 02:40:51 +00003292 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003293 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003294
anthony3dd0f622010-05-13 12:57:32 +00003295 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003296 switch (kernel->type) {
3297 /* These built-in kernels are cylindrical kernels, rotating is useless */
3298 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003299 case DOGKernel:
3300 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003301 case PeaksKernel:
3302 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003303 case ChebyshevKernel:
3304 case ManhattenKernel:
3305 case EuclideanKernel:
3306 return;
3307
3308 /* These may be rotatable at non-90 angles in the future */
3309 /* but simply rotating them in multiples of 90 degrees is useless */
3310 case SquareKernel:
3311 case DiamondKernel:
3312 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003313 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003314 return;
3315
3316 /* These only allows a +/-90 degree rotation (by transpose) */
3317 /* A 180 degree rotation is useless */
3318 case BlurKernel:
3319 case RectangleKernel:
3320 if ( 135.0 < angle && angle <= 225.0 )
3321 return;
3322 if ( 225.0 < angle && angle <= 315.0 )
3323 angle -= 180;
3324 break;
3325
anthony3dd0f622010-05-13 12:57:32 +00003326 default:
anthony83ba99b2010-01-24 08:48:15 +00003327 break;
3328 }
anthony3c10fc82010-05-13 02:40:51 +00003329 /* Attempt rotations by 45 degrees */
3330 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3331 {
3332 if ( kernel->width == 3 && kernel->height == 3 )
3333 { /* Rotate a 3x3 square by 45 degree angle */
3334 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003335 kernel->values[0] = kernel->values[3];
3336 kernel->values[3] = kernel->values[6];
3337 kernel->values[6] = kernel->values[7];
3338 kernel->values[7] = kernel->values[8];
3339 kernel->values[8] = kernel->values[5];
3340 kernel->values[5] = kernel->values[2];
3341 kernel->values[2] = kernel->values[1];
3342 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003343 /* rotate non-centered origin */
3344 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003345 ssize_t x,y;
3346 x = (ssize_t) kernel->x-1;
3347 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003348 if ( x == y ) x = 0;
3349 else if ( x == 0 ) x = -y;
3350 else if ( x == -y ) y = 0;
3351 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003352 kernel->x = (ssize_t) x+1;
3353 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003354 }
anthony43c49252010-05-18 10:59:50 +00003355 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3356 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003357 }
3358 else
3359 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3360 }
3361 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3362 {
3363 if ( kernel->width == 1 || kernel->height == 1 )
3364 { /* Do a transpose of the image, which results in a 90
3365 ** degree rotation of a 1 dimentional kernel
3366 */
cristybb503372010-05-27 20:51:26 +00003367 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003368 t;
cristybb503372010-05-27 20:51:26 +00003369 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003370 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003371 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003372 t = kernel->x;
3373 kernel->x = kernel->y;
3374 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003375 if ( kernel->width == 1 ) {
3376 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3377 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3378 } else {
3379 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3380 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3381 }
anthony3c10fc82010-05-13 02:40:51 +00003382 }
3383 else if ( kernel->width == kernel->height )
3384 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003385 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003386 i,j,x,y;
3387 register MagickRealType
3388 *k,t;
3389 k=kernel->values;
3390 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3391 for( j=0, y=kernel->height-1; j<y; j++, y--)
3392 { t = k[i+j*kernel->width];
3393 k[i+j*kernel->width] = k[j+x*kernel->width];
3394 k[j+x*kernel->width] = k[x+y*kernel->width];
3395 k[x+y*kernel->width] = k[y+i*kernel->width];
3396 k[y+i*kernel->width] = t;
3397 }
3398 }
3399 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003400 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003401 x = (ssize_t) (kernel->x*2-kernel->width+1);
3402 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003403 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3404 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003405 }
anthony43c49252010-05-18 10:59:50 +00003406 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3407 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003408 }
3409 else
3410 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3411 }
anthony83ba99b2010-01-24 08:48:15 +00003412 if ( 135.0 < angle && angle <= 225.0 )
3413 {
anthony43c49252010-05-18 10:59:50 +00003414 /* For a 180 degree rotation - also know as a reflection
3415 * This is actually a very very common operation!
3416 * Basically all that is needed is a reversal of the kernel data!
3417 * And a reflection of the origon
3418 */
cristybb503372010-05-27 20:51:26 +00003419 size_t
anthony83ba99b2010-01-24 08:48:15 +00003420 i,j;
3421 register double
3422 *k,t;
3423
3424 k=kernel->values;
3425 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3426 t=k[i], k[i]=k[j], k[j]=t;
3427
cristybb503372010-05-27 20:51:26 +00003428 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3429 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003430 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3431 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003432 }
anthony3c10fc82010-05-13 02:40:51 +00003433 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003434 * In the future some form of non-orthogonal angled rotates could be
3435 * performed here, posibily with a linear kernel restriction.
3436 */
3437
3438#if 0
anthony3c10fc82010-05-13 02:40:51 +00003439 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003440 */
cristybb503372010-05-27 20:51:26 +00003441 size_t
anthony83ba99b2010-01-24 08:48:15 +00003442 y;
cristybb503372010-05-27 20:51:26 +00003443 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003444 x,r;
3445 register double
3446 *k,t;
3447
3448 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3449 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3450 t=k[x], k[x]=k[r], k[r]=t;
3451
cristyc99304f2010-02-01 15:26:27 +00003452 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003453 angle = fmod(angle+180.0, 360.0);
3454 }
3455#endif
3456 return;
3457}
3458
3459/*
3460%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3461% %
3462% %
3463% %
anthony46a369d2010-05-19 02:41:48 +00003464% S c a l e G e o m e t r y K e r n e l I n f o %
3465% %
3466% %
3467% %
3468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3469%
3470% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3471% provided as a "-set option:convolve:scale {geometry}" user setting,
3472% and modifies the kernel according to the parsed arguments of that setting.
3473%
3474% The first argument (and any normalization flags) are passed to
3475% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3476% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3477% into the scaled/normalized kernel.
3478%
3479% The format of the ScaleKernelInfo method is:
3480%
3481% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3482% const MagickStatusType normalize_flags )
3483%
3484% A description of each parameter follows:
3485%
3486% o kernel: the Morphology/Convolution kernel to modify
3487%
3488% o geometry:
3489% The geometry string to parse, typically from the user provided
3490% "-set option:convolve:scale {geometry}" setting.
3491%
3492*/
3493MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3494 const char *geometry)
3495{
3496 GeometryFlags
3497 flags;
3498 GeometryInfo
3499 args;
3500
3501 SetGeometryInfo(&args);
3502 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3503
3504#if 0
3505 /* For Debugging Geometry Input */
3506 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3507 flags, args.rho, args.sigma, args.xi, args.psi );
3508#endif
3509
3510 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3511 args.rho *= 0.01, args.sigma *= 0.01;
3512
3513 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3514 args.rho = 1.0;
3515 if ( (flags & SigmaValue) == 0 )
3516 args.sigma = 0.0;
3517
3518 /* Scale/Normalize the input kernel */
3519 ScaleKernelInfo(kernel, args.rho, flags);
3520
3521 /* Add Unity Kernel, for blending with original */
3522 if ( (flags & SigmaValue) != 0 )
3523 UnityAddKernelInfo(kernel, args.sigma);
3524
3525 return;
3526}
3527/*
3528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529% %
3530% %
3531% %
cristy6771f1e2010-03-05 19:43:39 +00003532% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003533% %
3534% %
3535% %
3536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3537%
anthony1b2bc0a2010-05-12 05:25:22 +00003538% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3539% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003540%
anthony999bb2c2010-02-18 12:38:01 +00003541% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003542% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003543%
anthony46a369d2010-05-19 02:41:48 +00003544% If either of the two 'normalize_flags' are given the kernel will first be
3545% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003546%
3547% Kernel normalization ('normalize_flags' given) is designed to ensure that
3548% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003549% morphology methods will fall into -1.0 to +1.0 range. Note that for
3550% non-HDRI versions of IM this may cause images to have any negative results
3551% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003552%
3553% More specifically. Kernels which only contain positive values (such as a
3554% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003555% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003556%
3557% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3558% the kernel will be scaled by the absolute of the sum of kernel values, so
3559% that it will generally fall within the +/- 1.0 range.
3560%
3561% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3562% will be scaled by just the sum of the postive values, so that its output
3563% range will again fall into the +/- 1.0 range.
3564%
3565% For special kernels designed for locating shapes using 'Correlate', (often
3566% only containing +1 and -1 values, representing foreground/brackground
3567% matching) a special normalization method is provided to scale the positive
3568% values seperatally to those of the negative values, so the kernel will be
3569% forced to become a zero-sum kernel better suited to such searches.
3570%
anthony1b2bc0a2010-05-12 05:25:22 +00003571% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003572% attributes within the kernel structure have been correctly set during the
3573% kernels creation.
3574%
3575% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003576% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3577% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003578%
anthony4fd27e22010-02-07 08:17:18 +00003579% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003580%
anthony999bb2c2010-02-18 12:38:01 +00003581% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3582% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003583%
3584% A description of each parameter follows:
3585%
3586% o kernel: the Morphology/Convolution kernel
3587%
anthony999bb2c2010-02-18 12:38:01 +00003588% o scaling_factor:
3589% multiply all values (after normalization) by this factor if not
3590% zero. If the kernel is normalized regardless of any flags.
3591%
3592% o normalize_flags:
3593% GeometryFlags defining normalization method to use.
3594% specifically: NormalizeValue, CorrelateNormalizeValue,
3595% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003596%
3597*/
cristy6771f1e2010-03-05 19:43:39 +00003598MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3599 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003600{
cristybb503372010-05-27 20:51:26 +00003601 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003602 i;
3603
anthony999bb2c2010-02-18 12:38:01 +00003604 register double
3605 pos_scale,
3606 neg_scale;
3607
anthony46a369d2010-05-19 02:41:48 +00003608 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003609 if ( kernel->next != (KernelInfo *) NULL)
3610 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3611
anthony46a369d2010-05-19 02:41:48 +00003612 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003613 pos_scale = 1.0;
3614 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003615 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003616 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003617 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003618 else
anthonyf4e00312010-05-20 12:06:35 +00003619 /* zero-summing kernel */
3620 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003621 }
anthony46a369d2010-05-19 02:41:48 +00003622 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003623 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3624 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3625 ? kernel->positive_range : 1.0;
3626 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3627 ? -kernel->negative_range : 1.0;
3628 }
3629 else
3630 neg_scale = pos_scale;
3631
3632 /* finialize scaling_factor for positive and negative components */
3633 pos_scale = scaling_factor/pos_scale;
3634 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003635
cristybb503372010-05-27 20:51:26 +00003636 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003637 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003638 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003639
anthony999bb2c2010-02-18 12:38:01 +00003640 /* convolution output range */
3641 kernel->positive_range *= pos_scale;
3642 kernel->negative_range *= neg_scale;
3643 /* maximum and minimum values in kernel */
3644 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3645 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3646
anthony46a369d2010-05-19 02:41:48 +00003647 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003648 if ( scaling_factor < MagickEpsilon ) {
3649 double t;
3650 t = kernel->positive_range;
3651 kernel->positive_range = kernel->negative_range;
3652 kernel->negative_range = t;
3653 t = kernel->maximum;
3654 kernel->maximum = kernel->minimum;
3655 kernel->minimum = 1;
3656 }
anthonycc6c8362010-01-25 04:14:01 +00003657
3658 return;
3659}
3660
3661/*
3662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3663% %
3664% %
3665% %
anthony46a369d2010-05-19 02:41:48 +00003666% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003667% %
3668% %
3669% %
3670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3671%
anthony4fd27e22010-02-07 08:17:18 +00003672% ShowKernelInfo() outputs the details of the given kernel defination to
3673% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003674%
3675% The format of the ShowKernel method is:
3676%
anthony4fd27e22010-02-07 08:17:18 +00003677% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003678%
3679% A description of each parameter follows:
3680%
3681% o kernel: the Morphology/Convolution kernel
3682%
anthony83ba99b2010-01-24 08:48:15 +00003683*/
anthony4fd27e22010-02-07 08:17:18 +00003684MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003685{
anthony7a01dcf2010-05-11 12:25:52 +00003686 KernelInfo
3687 *k;
anthony83ba99b2010-01-24 08:48:15 +00003688
cristybb503372010-05-27 20:51:26 +00003689 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003690 c, i, u, v;
3691
3692 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3693
anthony46a369d2010-05-19 02:41:48 +00003694 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003695 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003696 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003697 fprintf(stderr, " \"%s",
3698 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3699 if ( fabs(k->angle) > MagickEpsilon )
3700 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003701 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3702 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003703 fprintf(stderr,
3704 " with values from %.*lg to %.*lg\n",
3705 GetMagickPrecision(), k->minimum,
3706 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003707 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003708 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003709 GetMagickPrecision(), k->positive_range);
3710 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3711 fprintf(stderr, " (Zero-Summing)\n");
3712 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3713 fprintf(stderr, " (Normalized)\n");
3714 else
3715 fprintf(stderr, " (Sum %.*lg)\n",
3716 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003717 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003718 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003719 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003720 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003721 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003722 else
anthonyf4e00312010-05-20 12:06:35 +00003723 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003724 GetMagickPrecision(), k->values[i]);
3725 fprintf(stderr,"\n");
3726 }
anthony83ba99b2010-01-24 08:48:15 +00003727 }
3728}
anthonycc6c8362010-01-25 04:14:01 +00003729
3730/*
3731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3732% %
3733% %
3734% %
anthony43c49252010-05-18 10:59:50 +00003735% U n i t y A d d K e r n a l I n f o %
3736% %
3737% %
3738% %
3739%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3740%
3741% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3742% to the given pre-scaled and normalized Kernel. This in effect adds that
3743% amount of the original image into the resulting convolution kernel. This
3744% value is usually provided by the user as a percentage value in the
3745% 'convolve:scale' setting.
3746%
3747% The resulting effect is to either convert a 'zero-summing' edge detection
3748% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3749% kernel.
3750%
3751% Alternativally by using a purely positive kernel, and using a negative
3752% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3753% as a "Gaussian") into a 'unsharp' kernel.
3754%
anthony46a369d2010-05-19 02:41:48 +00003755% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003756%
3757% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3758%
3759% A description of each parameter follows:
3760%
3761% o kernel: the Morphology/Convolution kernel
3762%
3763% o scale:
3764% scaling factor for the unity kernel to be added to
3765% the given kernel.
3766%
anthony43c49252010-05-18 10:59:50 +00003767*/
3768MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3769 const double scale)
3770{
anthony46a369d2010-05-19 02:41:48 +00003771 /* do the other kernels in a multi-kernel list first */
3772 if ( kernel->next != (KernelInfo *) NULL)
3773 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003774
anthony46a369d2010-05-19 02:41:48 +00003775 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003776 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003777 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003778
3779 return;
3780}
3781
3782/*
3783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3784% %
3785% %
3786% %
3787% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003788% %
3789% %
3790% %
3791%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3792%
3793% ZeroKernelNans() replaces any special 'nan' value that may be present in
3794% the kernel with a zero value. This is typically done when the kernel will
3795% be used in special hardware (GPU) convolution processors, to simply
3796% matters.
3797%
3798% The format of the ZeroKernelNans method is:
3799%
anthony46a369d2010-05-19 02:41:48 +00003800% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003801%
3802% A description of each parameter follows:
3803%
3804% o kernel: the Morphology/Convolution kernel
3805%
anthonycc6c8362010-01-25 04:14:01 +00003806*/
anthonyc4c86e02010-01-27 09:30:32 +00003807MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003808{
cristybb503372010-05-27 20:51:26 +00003809 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003810 i;
3811
anthony46a369d2010-05-19 02:41:48 +00003812 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003813 if ( kernel->next != (KernelInfo *) NULL)
3814 ZeroKernelNans(kernel->next);
3815
anthony43c49252010-05-18 10:59:50 +00003816 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003817 if ( IsNan(kernel->values[i]) )
3818 kernel->values[i] = 0.0;
3819
3820 return;
3821}