blob: 96cc52c81a1a5e3c597bf9720af9bba3dabe4f43 [file] [log] [blame]
cristy701db312009-11-20 03:14:08 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M OOO RRRR PPPP H H OOO L OOO GGGG Y Y %
7% MM MM O O R R P P H H O O L O O G Y Y %
8% M M M O O RRRR PPPP HHHHH O O L O O G GGG Y %
9% M M O O R R P H H O O L O O G G Y %
10% M M OOO R R P H H OOO LLLLL OOO GGG Y %
11% %
12% %
13% MagickCore Morphology Methods %
14% %
15% Software Design %
16% Anthony Thyssen %
anthonyc94cdb02010-01-06 08:15:29 +000017% January 2010 %
cristy701db312009-11-20 03:14:08 +000018% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy701db312009-11-20 03:14:08 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
anthony1b2bc0a2010-05-12 05:25:22 +000036% Morpology is the the application of various kernels, of any size and even
anthony602ab9b2010-01-05 08:06:50 +000037% shape, to a image in various ways (typically binary, but not always).
cristy701db312009-11-20 03:14:08 +000038%
anthony602ab9b2010-01-05 08:06:50 +000039% Convolution (weighted sum or average) is just one specific type of
40% morphology. Just one that is very common for image bluring and sharpening
41% effects. Not only 2D Gaussian blurring, but also 2-pass 1D Blurring.
42%
43% This module provides not only a general morphology function, and the ability
44% to apply more advanced or iterative morphologies, but also functions for the
45% generation of many different types of kernel arrays from user supplied
46% arguments. Prehaps even the generation of a kernel from a small image.
cristy701db312009-11-20 03:14:08 +000047*/
48
49/*
50 Include declarations.
51*/
52#include "magick/studio.h"
anthony602ab9b2010-01-05 08:06:50 +000053#include "magick/artifact.h"
cristy701db312009-11-20 03:14:08 +000054#include "magick/cache-view.h"
55#include "magick/color-private.h"
56#include "magick/enhance.h"
57#include "magick/exception.h"
58#include "magick/exception-private.h"
anthony602ab9b2010-01-05 08:06:50 +000059#include "magick/gem.h"
cristy701db312009-11-20 03:14:08 +000060#include "magick/hashmap.h"
61#include "magick/image.h"
cristybba804b2010-01-05 15:39:59 +000062#include "magick/image-private.h"
cristy701db312009-11-20 03:14:08 +000063#include "magick/list.h"
anthony29188a82010-01-22 10:12:34 +000064#include "magick/magick.h"
cristy701db312009-11-20 03:14:08 +000065#include "magick/memory_.h"
66#include "magick/monitor-private.h"
67#include "magick/morphology.h"
anthony46a369d2010-05-19 02:41:48 +000068#include "magick/morphology-private.h"
anthony602ab9b2010-01-05 08:06:50 +000069#include "magick/option.h"
cristy701db312009-11-20 03:14:08 +000070#include "magick/pixel-private.h"
71#include "magick/prepress.h"
72#include "magick/quantize.h"
73#include "magick/registry.h"
74#include "magick/semaphore.h"
75#include "magick/splay-tree.h"
76#include "magick/statistic.h"
77#include "magick/string_.h"
anthony602ab9b2010-01-05 08:06:50 +000078#include "magick/string-private.h"
79#include "magick/token.h"
cristya29d45f2010-03-05 21:14:54 +000080
anthonyc3cd15b2010-05-27 06:05:40 +000081
anthony602ab9b2010-01-05 08:06:50 +000082/*
anthonyc3cd15b2010-05-27 06:05:40 +000083** The following test is for special floating point numbers of value NaN (not
84** a number), that may be used within a Kernel Definition. NaN's are defined
85** as part of the IEEE standard for floating point number representation.
86**
87** These are used as a Kernel value to mean that this kernel position is not
88** part of the kernel neighbourhood for convolution or morphology processing,
89** and thus should be ignored. This allows the use of 'shaped' kernels.
90**
91** The special properity that two NaN's are never equal, even if they are from
92** the same variable allow you to test if a value is special NaN value.
93**
94** This macro IsNaN() is thus is only true if the value given is NaN.
cristya29d45f2010-03-05 21:14:54 +000095*/
anthony602ab9b2010-01-05 08:06:50 +000096#define IsNan(a) ((a)!=(a))
97
anthony29188a82010-01-22 10:12:34 +000098/*
cristya29d45f2010-03-05 21:14:54 +000099 Other global definitions used by module.
100*/
anthony29188a82010-01-22 10:12:34 +0000101static inline double MagickMin(const double x,const double y)
102{
103 return( x < y ? x : y);
104}
105static inline double MagickMax(const double x,const double y)
106{
107 return( x > y ? x : y);
108}
109#define Minimize(assign,value) assign=MagickMin(assign,value)
110#define Maximize(assign,value) assign=MagickMax(assign,value)
111
anthonyc4c86e02010-01-27 09:30:32 +0000112/* Currently these are only internal to this module */
113static void
anthony46a369d2010-05-19 02:41:48 +0000114 CalcKernelMetaData(KernelInfo *),
cristyeb8db6d2010-05-24 18:34:11 +0000115 ExpandKernelInfo(KernelInfo *, const double),
cristyef656912010-03-05 19:54:59 +0000116 RotateKernelInfo(KernelInfo *, double);
anthony602ab9b2010-01-05 08:06:50 +0000117
anthony3dd0f622010-05-13 12:57:32 +0000118
119/* Quick function to find last kernel in a kernel list */
120static inline KernelInfo *LastKernelInfo(KernelInfo *kernel)
121{
122 while (kernel->next != (KernelInfo *) NULL)
123 kernel = kernel->next;
124 return(kernel);
125}
126
127
anthony602ab9b2010-01-05 08:06:50 +0000128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% %
131% %
132% %
anthony83ba99b2010-01-24 08:48:15 +0000133% A c q u i r e K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000134% %
135% %
136% %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
cristy2be15382010-01-21 02:38:03 +0000139% AcquireKernelInfo() takes the given string (generally supplied by the
anthony602ab9b2010-01-05 08:06:50 +0000140% user) and converts it into a Morphology/Convolution Kernel. This allows
141% users to specify a kernel from a number of pre-defined kernels, or to fully
142% specify their own kernel for a specific Convolution or Morphology
143% Operation.
144%
145% The kernel so generated can be any rectangular array of floating point
146% values (doubles) with the 'control point' or 'pixel being affected'
147% anywhere within that array of values.
148%
anthony83ba99b2010-01-24 08:48:15 +0000149% Previously IM was restricted to a square of odd size using the exact
cristybb503372010-05-27 20:51:26 +0000150% center as origin, this is no ssize_ter the case, and any rectangular kernel
anthony83ba99b2010-01-24 08:48:15 +0000151% with any value being declared the origin. This in turn allows the use of
152% highly asymmetrical kernels.
anthony602ab9b2010-01-05 08:06:50 +0000153%
154% The floating point values in the kernel can also include a special value
anthony83ba99b2010-01-24 08:48:15 +0000155% known as 'nan' or 'not a number' to indicate that this value is not part
156% of the kernel array. This allows you to shaped the kernel within its
157% rectangular area. That is 'nan' values provide a 'mask' for the kernel
158% shape. However at least one non-nan value must be provided for correct
159% working of a kernel.
anthony602ab9b2010-01-05 08:06:50 +0000160%
anthony7a01dcf2010-05-11 12:25:52 +0000161% The returned kernel should be freed using the DestroyKernelInfo() when you
162% are finished with it. Do not free this memory yourself.
anthony602ab9b2010-01-05 08:06:50 +0000163%
164% Input kernel defintion strings can consist of any of three types.
165%
anthony29188a82010-01-22 10:12:34 +0000166% "name:args"
167% Select from one of the built in kernels, using the name and
168% geometry arguments supplied. See AcquireKernelBuiltIn()
anthony602ab9b2010-01-05 08:06:50 +0000169%
anthony43c49252010-05-18 10:59:50 +0000170% "WxH[+X+Y][^@]:num, num, num ..."
anthony1b2bc0a2010-05-12 05:25:22 +0000171% a kernel of size W by H, with W*H floating point numbers following.
anthony602ab9b2010-01-05 08:06:50 +0000172% the 'center' can be optionally be defined at +X+Y (such that +0+0
anthony29188a82010-01-22 10:12:34 +0000173% is top left corner). If not defined the pixel in the center, for
174% odd sizes, or to the immediate top or left of center for even sizes
175% is automatically selected.
anthony602ab9b2010-01-05 08:06:50 +0000176%
anthony43c49252010-05-18 10:59:50 +0000177% If a '^' is included the kernel expanded with 90-degree rotations,
178% While a '@' will allow you to expand a 3x3 kernel using 45-degree
179% circular rotates.
180%
anthony29188a82010-01-22 10:12:34 +0000181% "num, num, num, num, ..."
182% list of floating point numbers defining an 'old style' odd sized
183% square kernel. At least 9 values should be provided for a 3x3
184% square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
185% Values can be space or comma separated. This is not recommended.
anthony602ab9b2010-01-05 08:06:50 +0000186%
anthony7a01dcf2010-05-11 12:25:52 +0000187% You can define a 'list of kernels' which can be used by some morphology
188% operators A list is defined as a semi-colon seperated list kernels.
189%
anthonydbc89892010-05-12 07:05:27 +0000190% " kernel ; kernel ; kernel ; "
anthony7a01dcf2010-05-11 12:25:52 +0000191%
anthony1dd091a2010-05-27 06:31:15 +0000192% Any extra ';' characters, at start, end or between kernel defintions are
anthony43c49252010-05-18 10:59:50 +0000193% simply ignored.
194%
195% Note that 'name' kernels will start with an alphabetic character while the
196% new kernel specification has a ':' character in its specification string.
197% If neither is the case, it is assumed an old style of a simple list of
198% numbers generating a odd-sized square kernel has been given.
anthony7a01dcf2010-05-11 12:25:52 +0000199%
anthony602ab9b2010-01-05 08:06:50 +0000200% The format of the AcquireKernal method is:
201%
cristy2be15382010-01-21 02:38:03 +0000202% KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000203%
204% A description of each parameter follows:
205%
206% o kernel_string: the Morphology/Convolution kernel wanted.
207%
208*/
209
anthonyc84dce52010-05-07 05:42:23 +0000210/* This was separated so that it could be used as a separate
anthony5ef8e942010-05-11 06:51:12 +0000211** array input handling function, such as for -color-matrix
anthonyc84dce52010-05-07 05:42:23 +0000212*/
anthony5ef8e942010-05-11 06:51:12 +0000213static KernelInfo *ParseKernelArray(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000214{
cristy2be15382010-01-21 02:38:03 +0000215 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000216 *kernel;
217
218 char
219 token[MaxTextExtent];
220
anthony602ab9b2010-01-05 08:06:50 +0000221 const char
anthony5ef8e942010-05-11 06:51:12 +0000222 *p,
223 *end;
anthony602ab9b2010-01-05 08:06:50 +0000224
cristybb503372010-05-27 20:51:26 +0000225 register ssize_t
anthonyc84dce52010-05-07 05:42:23 +0000226 i;
anthony602ab9b2010-01-05 08:06:50 +0000227
anthony29188a82010-01-22 10:12:34 +0000228 double
229 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
230
anthony43c49252010-05-18 10:59:50 +0000231 MagickStatusType
232 flags;
233
234 GeometryInfo
235 args;
236
cristy2be15382010-01-21 02:38:03 +0000237 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
238 if (kernel == (KernelInfo *)NULL)
anthony602ab9b2010-01-05 08:06:50 +0000239 return(kernel);
240 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000241 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthony7a01dcf2010-05-11 12:25:52 +0000242 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000243 kernel->type = UserDefinedKernel;
anthony7a01dcf2010-05-11 12:25:52 +0000244 kernel->next = (KernelInfo *) NULL;
cristyd43a46b2010-01-21 02:13:41 +0000245 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000246
anthony5ef8e942010-05-11 06:51:12 +0000247 /* find end of this specific kernel definition string */
248 end = strchr(kernel_string, ';');
249 if ( end == (char *) NULL )
250 end = strchr(kernel_string, '\0');
251
anthony43c49252010-05-18 10:59:50 +0000252 /* clear flags - for Expanding kernal lists thorugh rotations */
253 flags = NoValue;
254
anthony602ab9b2010-01-05 08:06:50 +0000255 /* Has a ':' in argument - New user kernel specification */
256 p = strchr(kernel_string, ':');
anthony5ef8e942010-05-11 06:51:12 +0000257 if ( p != (char *) NULL && p < end)
anthony602ab9b2010-01-05 08:06:50 +0000258 {
anthony602ab9b2010-01-05 08:06:50 +0000259 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
cristy150989e2010-02-01 14:59:39 +0000260 memcpy(token, kernel_string, (size_t) (p-kernel_string));
anthony602ab9b2010-01-05 08:06:50 +0000261 token[p-kernel_string] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000262 SetGeometryInfo(&args);
anthony602ab9b2010-01-05 08:06:50 +0000263 flags = ParseGeometry(token, &args);
anthony602ab9b2010-01-05 08:06:50 +0000264
anthony29188a82010-01-22 10:12:34 +0000265 /* Size handling and checks of geometry settings */
anthony602ab9b2010-01-05 08:06:50 +0000266 if ( (flags & WidthValue) == 0 ) /* if no width then */
267 args.rho = args.sigma; /* then width = height */
268 if ( args.rho < 1.0 ) /* if width too small */
269 args.rho = 1.0; /* then width = 1 */
270 if ( args.sigma < 1.0 ) /* if height too small */
271 args.sigma = args.rho; /* then height = width */
cristybb503372010-05-27 20:51:26 +0000272 kernel->width = (size_t)args.rho;
273 kernel->height = (size_t)args.sigma;
anthony602ab9b2010-01-05 08:06:50 +0000274
275 /* Offset Handling and Checks */
276 if ( args.xi < 0.0 || args.psi < 0.0 )
anthony83ba99b2010-01-24 08:48:15 +0000277 return(DestroyKernelInfo(kernel));
cristybb503372010-05-27 20:51:26 +0000278 kernel->x = ((flags & XValue)!=0) ? (ssize_t)args.xi
279 : (ssize_t) (kernel->width-1)/2;
280 kernel->y = ((flags & YValue)!=0) ? (ssize_t)args.psi
281 : (ssize_t) (kernel->height-1)/2;
282 if ( kernel->x >= (ssize_t) kernel->width ||
283 kernel->y >= (ssize_t) kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000284 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000285
286 p++; /* advance beyond the ':' */
287 }
288 else
anthonyc84dce52010-05-07 05:42:23 +0000289 { /* ELSE - Old old specification, forming odd-square kernel */
anthony602ab9b2010-01-05 08:06:50 +0000290 /* count up number of values given */
291 p=(const char *) kernel_string;
cristya699b172010-01-06 16:48:49 +0000292 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
anthony29188a82010-01-22 10:12:34 +0000293 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony5ef8e942010-05-11 06:51:12 +0000294 for (i=0; p < end; i++)
anthony602ab9b2010-01-05 08:06:50 +0000295 {
296 GetMagickToken(p,&p,token);
297 if (*token == ',')
298 GetMagickToken(p,&p,token);
299 }
300 /* set the size of the kernel - old sized square */
cristybb503372010-05-27 20:51:26 +0000301 kernel->width = kernel->height= (size_t) sqrt((double) i+1.0);
302 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000303 p=(const char *) kernel_string;
anthony29188a82010-01-22 10:12:34 +0000304 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
305 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000306 }
307
308 /* Read in the kernel values from rest of input string argument */
309 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
310 kernel->height*sizeof(double));
311 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000312 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000313
cristyc99304f2010-02-01 15:26:27 +0000314 kernel->minimum = +MagickHuge;
315 kernel->maximum = -MagickHuge;
316 kernel->negative_range = kernel->positive_range = 0.0;
anthonyc84dce52010-05-07 05:42:23 +0000317
cristybb503372010-05-27 20:51:26 +0000318 for (i=0; (i < (ssize_t) (kernel->width*kernel->height)) && (p < end); i++)
anthony602ab9b2010-01-05 08:06:50 +0000319 {
320 GetMagickToken(p,&p,token);
321 if (*token == ',')
322 GetMagickToken(p,&p,token);
anthony29188a82010-01-22 10:12:34 +0000323 if ( LocaleCompare("nan",token) == 0
anthonyc84dce52010-05-07 05:42:23 +0000324 || LocaleCompare("-",token) == 0 ) {
anthony29188a82010-01-22 10:12:34 +0000325 kernel->values[i] = nan; /* do not include this value in kernel */
326 }
327 else {
328 kernel->values[i] = StringToDouble(token);
329 ( kernel->values[i] < 0)
cristyc99304f2010-02-01 15:26:27 +0000330 ? ( kernel->negative_range += kernel->values[i] )
331 : ( kernel->positive_range += kernel->values[i] );
332 Minimize(kernel->minimum, kernel->values[i]);
333 Maximize(kernel->maximum, kernel->values[i]);
anthony29188a82010-01-22 10:12:34 +0000334 }
anthony602ab9b2010-01-05 08:06:50 +0000335 }
anthony29188a82010-01-22 10:12:34 +0000336
anthony5ef8e942010-05-11 06:51:12 +0000337 /* sanity check -- no more values in kernel definition */
338 GetMagickToken(p,&p,token);
339 if ( *token != '\0' && *token != ';' && *token != '\'' )
340 return(DestroyKernelInfo(kernel));
341
anthonyc84dce52010-05-07 05:42:23 +0000342#if 0
343 /* this was the old method of handling a incomplete kernel */
cristybb503372010-05-27 20:51:26 +0000344 if ( i < (ssize_t) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000345 Minimize(kernel->minimum, kernel->values[i]);
346 Maximize(kernel->maximum, kernel->values[i]);
cristybb503372010-05-27 20:51:26 +0000347 for ( ; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony29188a82010-01-22 10:12:34 +0000348 kernel->values[i]=0.0;
349 }
anthonyc84dce52010-05-07 05:42:23 +0000350#else
351 /* Number of values for kernel was not enough - Report Error */
cristybb503372010-05-27 20:51:26 +0000352 if ( i < (ssize_t) (kernel->width*kernel->height) )
anthonyc84dce52010-05-07 05:42:23 +0000353 return(DestroyKernelInfo(kernel));
354#endif
355
356 /* check that we recieved at least one real (non-nan) value! */
357 if ( kernel->minimum == MagickHuge )
358 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000359
anthony43c49252010-05-18 10:59:50 +0000360 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel size */
361 ExpandKernelInfo(kernel, 45.0);
362 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel size */
363 ExpandKernelInfo(kernel, 90.0);
364
anthony602ab9b2010-01-05 08:06:50 +0000365 return(kernel);
366}
anthonyc84dce52010-05-07 05:42:23 +0000367
anthony43c49252010-05-18 10:59:50 +0000368static KernelInfo *ParseKernelName(const char *kernel_string)
anthonyc84dce52010-05-07 05:42:23 +0000369{
anthonyf0176c32010-05-23 23:08:57 +0000370 KernelInfo
371 *kernel;
372
anthonyc84dce52010-05-07 05:42:23 +0000373 char
374 token[MaxTextExtent];
375
cristybb503372010-05-27 20:51:26 +0000376 ssize_t
anthony5ef8e942010-05-11 06:51:12 +0000377 type;
378
anthonyc84dce52010-05-07 05:42:23 +0000379 const char
anthony7a01dcf2010-05-11 12:25:52 +0000380 *p,
381 *end;
anthonyc84dce52010-05-07 05:42:23 +0000382
383 MagickStatusType
384 flags;
385
386 GeometryInfo
387 args;
388
anthonyc84dce52010-05-07 05:42:23 +0000389 /* Parse special 'named' kernel */
anthony5ef8e942010-05-11 06:51:12 +0000390 GetMagickToken(kernel_string,&p,token);
anthonyc84dce52010-05-07 05:42:23 +0000391 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
392 if ( type < 0 || type == UserDefinedKernel )
anthony5ef8e942010-05-11 06:51:12 +0000393 return((KernelInfo *)NULL); /* not a valid named kernel */
anthonyc84dce52010-05-07 05:42:23 +0000394
395 while (((isspace((int) ((unsigned char) *p)) != 0) ||
anthony5ef8e942010-05-11 06:51:12 +0000396 (*p == ',') || (*p == ':' )) && (*p != '\0') && (*p != ';'))
anthonyc84dce52010-05-07 05:42:23 +0000397 p++;
anthony7a01dcf2010-05-11 12:25:52 +0000398
399 end = strchr(p, ';'); /* end of this kernel defintion */
400 if ( end == (char *) NULL )
401 end = strchr(p, '\0');
402
403 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
404 memcpy(token, p, (size_t) (end-p));
405 token[end-p] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000406 SetGeometryInfo(&args);
anthony7a01dcf2010-05-11 12:25:52 +0000407 flags = ParseGeometry(token, &args);
anthonyc84dce52010-05-07 05:42:23 +0000408
anthony3c10fc82010-05-13 02:40:51 +0000409#if 0
410 /* For Debugging Geometry Input */
anthony46a369d2010-05-19 02:41:48 +0000411 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
anthony3c10fc82010-05-13 02:40:51 +0000412 flags, args.rho, args.sigma, args.xi, args.psi );
413#endif
414
anthonyc84dce52010-05-07 05:42:23 +0000415 /* special handling of missing values in input string */
416 switch( type ) {
anthony5ef8e942010-05-11 06:51:12 +0000417 case RectangleKernel:
418 if ( (flags & WidthValue) == 0 ) /* if no width then */
419 args.rho = args.sigma; /* then width = height */
420 if ( args.rho < 1.0 ) /* if width too small */
421 args.rho = 3; /* then width = 3 */
422 if ( args.sigma < 1.0 ) /* if height too small */
423 args.sigma = args.rho; /* then height = width */
424 if ( (flags & XValue) == 0 ) /* center offset if not defined */
cristybb503372010-05-27 20:51:26 +0000425 args.xi = (double)(((ssize_t)args.rho-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000426 if ( (flags & YValue) == 0 )
cristybb503372010-05-27 20:51:26 +0000427 args.psi = (double)(((ssize_t)args.sigma-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000428 break;
429 case SquareKernel:
430 case DiamondKernel:
431 case DiskKernel:
432 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +0000433 case CrossKernel:
anthony5ef8e942010-05-11 06:51:12 +0000434 /* If no scale given (a 0 scale is valid! - set it to 1.0 */
435 if ( (flags & HeightValue) == 0 )
436 args.sigma = 1.0;
437 break;
anthonyc1061722010-05-14 06:23:49 +0000438 case RingKernel:
439 if ( (flags & XValue) == 0 )
440 args.xi = 1.0;
441 break;
anthony5ef8e942010-05-11 06:51:12 +0000442 case ChebyshevKernel:
443 case ManhattenKernel:
444 case EuclideanKernel:
anthony43c49252010-05-18 10:59:50 +0000445 if ( (flags & HeightValue) == 0 ) /* no distance scale */
446 args.sigma = 100.0; /* default distance scaling */
447 else if ( (flags & AspectValue ) != 0 ) /* '!' flag */
448 args.sigma = QuantumRange/(args.sigma+1); /* maximum pixel distance */
449 else if ( (flags & PercentValue ) != 0 ) /* '%' flag */
450 args.sigma *= QuantumRange/100.0; /* percentage of color range */
anthony5ef8e942010-05-11 06:51:12 +0000451 break;
452 default:
453 break;
anthonyc84dce52010-05-07 05:42:23 +0000454 }
455
anthonyf0176c32010-05-23 23:08:57 +0000456 kernel = AcquireKernelBuiltIn((KernelInfoType)type, &args);
457
458 /* global expand to rotated kernel list - only for single kernels */
459 if ( kernel->next == (KernelInfo *) NULL ) {
460 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel args */
461 ExpandKernelInfo(kernel, 45.0);
462 else if ( (flags & MinimumValue) != 0 ) /* '^' symbol in kernel args */
463 ExpandKernelInfo(kernel, 90.0);
464 }
465
466 return(kernel);
anthonyc84dce52010-05-07 05:42:23 +0000467}
468
anthony5ef8e942010-05-11 06:51:12 +0000469MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
470{
anthony7a01dcf2010-05-11 12:25:52 +0000471
472 KernelInfo
anthonydbc89892010-05-12 07:05:27 +0000473 *kernel,
anthony43c49252010-05-18 10:59:50 +0000474 *new_kernel;
anthony7a01dcf2010-05-11 12:25:52 +0000475
anthony5ef8e942010-05-11 06:51:12 +0000476 char
477 token[MaxTextExtent];
478
anthony7a01dcf2010-05-11 12:25:52 +0000479 const char
anthonydbc89892010-05-12 07:05:27 +0000480 *p;
anthony7a01dcf2010-05-11 12:25:52 +0000481
cristybb503372010-05-27 20:51:26 +0000482 size_t
anthonye108a3f2010-05-12 07:24:03 +0000483 kernel_number;
484
anthonydbc89892010-05-12 07:05:27 +0000485 p = kernel_string;
anthony43c49252010-05-18 10:59:50 +0000486 kernel = NULL;
anthonye108a3f2010-05-12 07:24:03 +0000487 kernel_number = 0;
anthony5ef8e942010-05-11 06:51:12 +0000488
anthonydbc89892010-05-12 07:05:27 +0000489 while ( GetMagickToken(p,NULL,token), *token != '\0' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000490
anthony43c49252010-05-18 10:59:50 +0000491 /* ignore extra or multiple ';' kernel seperators */
anthonydbc89892010-05-12 07:05:27 +0000492 if ( *token != ';' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000493
anthonydbc89892010-05-12 07:05:27 +0000494 /* tokens starting with alpha is a Named kernel */
anthony43c49252010-05-18 10:59:50 +0000495 if (isalpha((int) *token) != 0)
496 new_kernel = ParseKernelName(p);
anthonydbc89892010-05-12 07:05:27 +0000497 else /* otherwise a user defined kernel array */
anthony43c49252010-05-18 10:59:50 +0000498 new_kernel = ParseKernelArray(p);
anthony7a01dcf2010-05-11 12:25:52 +0000499
anthonye108a3f2010-05-12 07:24:03 +0000500 /* Error handling -- this is not proper error handling! */
501 if ( new_kernel == (KernelInfo *) NULL ) {
cristyf2faecf2010-05-28 19:19:36 +0000502 fprintf(stderr, "Failed to parse kernel number #%lu\n",(unsigned long)
503 kernel_number);
anthonye108a3f2010-05-12 07:24:03 +0000504 if ( kernel != (KernelInfo *) NULL )
505 kernel=DestroyKernelInfo(kernel);
506 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000507 }
anthonye108a3f2010-05-12 07:24:03 +0000508
509 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000510 if ( kernel == (KernelInfo *) NULL )
511 kernel = new_kernel;
512 else
anthony43c49252010-05-18 10:59:50 +0000513 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000514 }
515
516 /* look for the next kernel in list */
517 p = strchr(p, ';');
518 if ( p == (char *) NULL )
519 break;
520 p++;
521
522 }
anthony7a01dcf2010-05-11 12:25:52 +0000523 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000524}
525
anthony602ab9b2010-01-05 08:06:50 +0000526
527/*
528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529% %
530% %
531% %
532% A c q u i r e K e r n e l B u i l t I n %
533% %
534% %
535% %
536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537%
538% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
539% kernels used for special purposes such as gaussian blurring, skeleton
540% pruning, and edge distance determination.
541%
542% They take a KernelType, and a set of geometry style arguments, which were
543% typically decoded from a user supplied string, or from a more complex
544% Morphology Method that was requested.
545%
546% The format of the AcquireKernalBuiltIn method is:
547%
cristy2be15382010-01-21 02:38:03 +0000548% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000549% const GeometryInfo args)
550%
551% A description of each parameter follows:
552%
553% o type: the pre-defined type of kernel wanted
554%
555% o args: arguments defining or modifying the kernel
556%
557% Convolution Kernels
558%
anthony46a369d2010-05-19 02:41:48 +0000559% Unity
560% the No-Op kernel, also requivelent to Gaussian of sigma zero.
561% Basically a 3x3 kernel of a 1 surrounded by zeros.
562%
anthony3c10fc82010-05-13 02:40:51 +0000563% Gaussian:{radius},{sigma}
564% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000565% The sigma for the curve is required. The resulting kernel is
566% normalized,
567%
568% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000569%
570% NOTE: that the 'radius' is optional, but if provided can limit (clip)
571% the final size of the resulting kernel to a square 2*radius+1 in size.
572% The radius should be at least 2 times that of the sigma value, or
573% sever clipping and aliasing may result. If not given or set to 0 the
574% radius will be determined so as to produce the best minimal error
575% result, which is usally much larger than is normally needed.
576%
anthonyc1061722010-05-14 06:23:49 +0000577% DOG:{radius},{sigma1},{sigma2}
578% "Difference of Gaussians" Kernel.
579% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
580% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
581% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000582%
anthony9eb4f742010-05-18 02:45:54 +0000583% LOG:{radius},{sigma}
584% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
585% The supposed ideal edge detection, zero-summing kernel.
586%
587% An alturnative to this kernel is to use a "DOG" with a sigma ratio of
588% approx 1.6, which can also be applied as a 2 pass "DOB" (see below).
589%
anthonyc1061722010-05-14 06:23:49 +0000590% Blur:{radius},{sigma}[,{angle}]
591% Generates a 1 dimensional or linear gaussian blur, at the angle given
592% (current restricted to orthogonal angles). If a 'radius' is given the
593% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
594% by a 90 degree angle.
595%
596% If 'sigma' is zero, you get a single pixel on a field of zeros.
597%
598% Note that two convolutions with two "Blur" kernels perpendicular to
599% each other, is equivelent to a far larger "Gaussian" kernel with the
600% same sigma value, However it is much faster to apply. This is how the
601% "-blur" operator actually works.
602%
603% DOB:{radius},{sigma1},{sigma2}[,{angle}]
604% "Difference of Blurs" Kernel.
605% As "Blur" but with the 1D gaussian produced by 'sigma2' subtracted
606% from thethe 1D gaussian produced by 'sigma1'.
607% The result is a zero-summing kernel.
608%
609% This can be used to generate a faster "DOG" convolution, in the same
610% way "Blur" can.
anthony602ab9b2010-01-05 08:06:50 +0000611%
anthony3c10fc82010-05-13 02:40:51 +0000612% Comet:{width},{sigma},{angle}
613% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000614% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000615% Adding two such blurs in opposite directions produces a Blur Kernel.
616% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000617%
anthony3c10fc82010-05-13 02:40:51 +0000618% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000619% radius of the kernel.
620%
621% # Still to be implemented...
622% #
anthony4fd27e22010-02-07 08:17:18 +0000623% # Filter2D
624% # Filter1D
625% # Set kernel values using a resize filter, and given scale (sigma)
626% # Cylindrical or Linear. Is this posible with an image?
627% #
anthony602ab9b2010-01-05 08:06:50 +0000628%
anthony3c10fc82010-05-13 02:40:51 +0000629% Named Constant Convolution Kernels
630%
anthonyc1061722010-05-14 06:23:49 +0000631% All these are unscaled, zero-summing kernels by default. As such for
632% non-HDRI version of ImageMagick some form of normalization, user scaling,
633% and biasing the results is recommended, to prevent the resulting image
634% being 'clipped'.
635%
636% The 3x3 kernels (most of these) can be circularly rotated in multiples of
637% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000638%
639% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000640% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000641% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
642% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000643% Type 2 : 3x3 with center:4 edge:1 corner:-2
644% Type 3 : 3x3 with center:4 edge:-2 corner:1
645% Type 5 : 5x5 laplacian
646% Type 7 : 7x7 laplacian
anthony43c49252010-05-18 10:59:50 +0000647% Type 15 : 5x5 LOG (sigma approx 1.4)
648% Type 19 : 9x9 LOG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000649%
650% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000651% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000652% -1, 0, 1
653% -2, 0,-2
654% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000655%
anthonyc1061722010-05-14 06:23:49 +0000656% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000657% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000658% 0, 0, 0
659% -1, 1, 0
660% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000661% Prewitt:{angle}
662% Prewitt Edge convolution kernel (3x3)
663% -1, 0, 1
664% -1, 0, 1
665% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000666% Compass:{angle}
667% Prewitt's "Compass" convolution kernel (3x3)
668% -1, 1, 1
669% -1,-2, 1
670% -1, 1, 1
671% Kirsch:{angle}
672% Kirsch's "Compass" convolution kernel (3x3)
673% -3,-3, 5
674% -3, 0, 5
675% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000676%
anthonye2a60ce2010-05-19 12:30:40 +0000677% FreiChen:{type},{angle}
678% Frei-Chen Edge Detector is a set of 9 unique convolution kernels that
anthonyc3cd15b2010-05-27 06:05:40 +0000679% are specially weighted.
680%
681% Type 0: | -1, 0, 1 |
682% | -sqrt(2), 0, sqrt(2) |
683% | -1, 0, 1 |
684%
685% This is basically the unnormalized discrete kernel that can be used
686% instead ot a Sobel kernel.
687%
688% The next 9 kernel types are specially pre-weighted. They should not
689% be normalized. After applying each to the original image, the results
690% is then added together. The square root of the resulting image is
691% the cosine of the edge, and the direction of the feature detection.
anthonye2a60ce2010-05-19 12:30:40 +0000692%
693% Type 1: | 1, sqrt(2), 1 |
694% | 0, 0, 0 | / 2*sqrt(2)
695% | -1, -sqrt(2), -1 |
696%
697% Type 2: | 1, 0, 1 |
698% | sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
699% | 1, 0, 1 |
700%
701% Type 3: | 0, -1, sqrt(2) |
702% | 1, 0, -1 | / 2*sqrt(2)
703% | -sqrt(2), 1, 0 |
704%
anthony6915d062010-05-19 12:45:51 +0000705% Type 4: | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000706% | -1, 0, 1 | / 2*sqrt(2)
707% | 0, 1, -sqrt(2) |
708%
709% Type 5: | 0, 1, 0 |
710% | -1, 0, -1 | / 2
711% | 0, 1, 0 |
712%
713% Type 6: | -1, 0, 1 |
714% | 0, 0, 0 | / 2
715% | 1, 0, -1 |
716%
anthonyf4e00312010-05-20 12:06:35 +0000717% Type 7: | 1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000718% | -2, 4, -2 | / 6
719% | 1, -2, 1 |
720%
anthonyf4e00312010-05-20 12:06:35 +0000721% Type 8: | -2, 1, -2 |
722% | 1, 4, 1 | / 6
723% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000724%
anthonyf4e00312010-05-20 12:06:35 +0000725% Type 9: | 1, 1, 1 |
726% | 1, 1, 1 | / 3
727% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000728%
729% The first 4 are for edge detection, the next 4 are for line detection
730% and the last is to add a average component to the results.
731%
anthonyc3cd15b2010-05-27 06:05:40 +0000732% Using a special type of '-1' will return all 9 pre-weighted kernels
733% as a multi-kernel list, so that you can use them directly (without
734% normalization) with the special "-set option:morphology:compose Plus"
735% setting to apply the full FreiChen Edge Detection Technique.
736%
anthony1dd091a2010-05-27 06:31:15 +0000737% If 'type' is large it will be taken to be an actual rotation angle for
738% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
739% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
740%
anthonye2a60ce2010-05-19 12:30:40 +0000741%
anthony602ab9b2010-01-05 08:06:50 +0000742% Boolean Kernels
743%
anthony3c10fc82010-05-13 02:40:51 +0000744% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000745% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000746% Kernel size will again be radius*2+1 square and defaults to radius 1,
747% generating a 3x3 kernel that is slightly larger than a square.
748%
anthony3c10fc82010-05-13 02:40:51 +0000749% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000750% Generate a square shaped kernel of size radius*2+1, and defaulting
751% to a 3x3 (radius 1).
752%
anthonyc1061722010-05-14 06:23:49 +0000753% Note that using a larger radius for the "Square" or the "Diamond" is
754% also equivelent to iterating the basic morphological method that many
755% times. However iterating with the smaller radius is actually faster
756% than using a larger kernel radius.
757%
758% Rectangle:{geometry}
759% Simply generate a rectangle of 1's with the size given. You can also
760% specify the location of the 'control point', otherwise the closest
761% pixel to the center of the rectangle is selected.
762%
763% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000764%
anthony3c10fc82010-05-13 02:40:51 +0000765% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000766% Generate a binary disk of the radius given, radius may be a float.
767% Kernel size will be ceil(radius)*2+1 square.
768% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000769% "Disk:1" => "diamond" or "cross:1"
770% "Disk:1.5" => "square"
771% "Disk:2" => "diamond:2"
772% "Disk:2.5" => a general disk shape of radius 2
773% "Disk:2.9" => "square:2"
774% "Disk:3.5" => default - octagonal/disk shape of radius 3
775% "Disk:4.2" => roughly octagonal shape of radius 4
776% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000777% After this all the kernel shape becomes more and more circular.
778%
779% Because a "disk" is more circular when using a larger radius, using a
780% larger radius is preferred over iterating the morphological operation.
781%
anthonyc1061722010-05-14 06:23:49 +0000782% Symbol Dilation Kernels
783%
784% These kernel is not a good general morphological kernel, but is used
785% more for highlighting and marking any single pixels in an image using,
786% a "Dilate" method as appropriate.
787%
788% For the same reasons iterating these kernels does not produce the
789% same result as using a larger radius for the symbol.
790%
anthony3c10fc82010-05-13 02:40:51 +0000791% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000792% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000793% Generate a kernel in the shape of a 'plus' or a 'cross' with
794% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000795%
796% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000797%
anthonyc1061722010-05-14 06:23:49 +0000798% Ring:{radius1},{radius2}[,{scale}]
799% A ring of the values given that falls between the two radii.
800% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
801% This is the 'edge' pixels of the default "Disk" kernel,
802% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000803%
anthony3dd0f622010-05-13 12:57:32 +0000804% Hit and Miss Kernels
805%
806% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000807% Find any peak larger than the pixels the fall between the two radii.
808% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000809% Edges
anthony1d45eb92010-05-25 11:13:23 +0000810% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000811% Corners
812% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000813% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000814% Find single pixel ridges or thin lines
815% Ridges2
816% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000817% Ridges3
818% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000819% LineEnds
820% Find end points of lines (for pruning a skeletion)
821% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000822% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000823% ConvexHull
824% Octagonal thicken kernel, to generate convex hulls of 45 degrees
825% Skeleton
826% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000827%
828% Distance Measuring Kernels
829%
anthonyc1061722010-05-14 06:23:49 +0000830% Different types of distance measuring methods, which are used with the
831% a 'Distance' morphology method for generating a gradient based on
832% distance from an edge of a binary shape, though there is a technique
833% for handling a anti-aliased shape.
834%
835% See the 'Distance' Morphological Method, for information of how it is
836% applied.
837%
anthony3dd0f622010-05-13 12:57:32 +0000838% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000839% Chebyshev Distance (also known as Tchebychev Distance) is a value of
840% one to any neighbour, orthogonal or diagonal. One why of thinking of
841% it is the number of squares a 'King' or 'Queen' in chess needs to
842% traverse reach any other position on a chess board. It results in a
843% 'square' like distance function, but one where diagonals are closer
844% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000845%
anthonyc1061722010-05-14 06:23:49 +0000846% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000847% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
848% Cab metric), is the distance needed when you can only travel in
849% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
850% in chess would travel. It results in a diamond like distances, where
851% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000852%
anthonyc1061722010-05-14 06:23:49 +0000853% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000854% Euclidean Distance is the 'direct' or 'as the crow flys distance.
855% However by default the kernel size only has a radius of 1, which
856% limits the distance to 'Knight' like moves, with only orthogonal and
857% diagonal measurements being correct. As such for the default kernel
858% you will get octagonal like distance function, which is reasonally
859% accurate.
860%
861% However if you use a larger radius such as "Euclidean:4" you will
862% get a much smoother distance gradient from the edge of the shape.
863% Of course a larger kernel is slower to use, and generally not needed.
864%
865% To allow the use of fractional distances that you get with diagonals
866% the actual distance is scaled by a fixed value which the user can
867% provide. This is not actually nessary for either ""Chebyshev" or
868% "Manhatten" distance kernels, but is done for all three distance
869% kernels. If no scale is provided it is set to a value of 100,
870% allowing for a maximum distance measurement of 655 pixels using a Q16
871% version of IM, from any edge. However for small images this can
872% result in quite a dark gradient.
873%
anthony602ab9b2010-01-05 08:06:50 +0000874*/
875
cristy2be15382010-01-21 02:38:03 +0000876MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000877 const GeometryInfo *args)
878{
cristy2be15382010-01-21 02:38:03 +0000879 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000880 *kernel;
881
cristybb503372010-05-27 20:51:26 +0000882 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000883 i;
884
cristybb503372010-05-27 20:51:26 +0000885 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000886 u,
887 v;
888
889 double
890 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
891
anthonyc1061722010-05-14 06:23:49 +0000892 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000893 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000894 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000895 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000896 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000897 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000898 break;
anthony1dd091a2010-05-27 06:31:15 +0000899 case UnityKernel: /* Named Descrete Convolution Kernels */
900 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000901 case SobelKernel:
902 case RobertsKernel:
903 case PrewittKernel:
904 case CompassKernel:
905 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000906 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000907 case CornersKernel: /* Hit and Miss kernels */
908 case LineEndsKernel:
909 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000910 case EdgesKernel:
911 case RidgesKernel:
912 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000913 case ConvexHullKernel:
914 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000915 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000916 /* A pre-generated kernel is not needed */
917 break;
anthony1dd091a2010-05-27 06:31:15 +0000918#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000919 case GaussianKernel:
920 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000921 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000922 case BlurKernel:
923 case DOBKernel:
924 case CometKernel:
925 case DiamondKernel:
926 case SquareKernel:
927 case RectangleKernel:
928 case DiskKernel:
929 case PlusKernel:
930 case CrossKernel:
931 case RingKernel:
932 case PeaksKernel:
933 case ChebyshevKernel:
934 case ManhattenKernel:
935 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000936#else
anthony9eb4f742010-05-18 02:45:54 +0000937 default:
anthony1dd091a2010-05-27 06:31:15 +0000938#endif
anthony9eb4f742010-05-18 02:45:54 +0000939 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000940 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
941 if (kernel == (KernelInfo *) NULL)
942 return(kernel);
943 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000944 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000945 kernel->negative_range = kernel->positive_range = 0.0;
946 kernel->type = type;
947 kernel->next = (KernelInfo *) NULL;
948 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000949 break;
950 }
anthony602ab9b2010-01-05 08:06:50 +0000951
952 switch(type) {
953 /* Convolution Kernels */
954 case GaussianKernel:
anthonyc1061722010-05-14 06:23:49 +0000955 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000956 case LOGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000957 { double
anthonyc1061722010-05-14 06:23:49 +0000958 sigma = fabs(args->sigma),
959 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000960 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000961
anthonyc1061722010-05-14 06:23:49 +0000962 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +0000963 kernel->width = (size_t)args->rho*2+1;
anthony9eb4f742010-05-18 02:45:54 +0000964 else if ( (type != DOGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000965 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
966 else
967 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
968 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +0000969 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000970 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
971 kernel->height*sizeof(double));
972 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000973 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000974
anthony46a369d2010-05-19 02:41:48 +0000975 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000976 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000977 *
978 * How to do this is currently not known, but appears to be
979 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000980 */
981
982 if ( type == GaussianKernel || type == DOGKernel )
983 { /* Calculate a Gaussian, OR positive half of a DOG */
984 if ( sigma > MagickEpsilon )
985 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
986 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +0000987 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
988 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +0000989 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
990 }
991 else /* limiting case - a unity (normalized Dirac) kernel */
992 { (void) ResetMagickMemory(kernel->values,0, (size_t)
993 kernel->width*kernel->height*sizeof(double));
994 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
995 }
anthonyc1061722010-05-14 06:23:49 +0000996 }
anthony9eb4f742010-05-18 02:45:54 +0000997
anthonyc1061722010-05-14 06:23:49 +0000998 if ( type == DOGKernel )
999 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
1000 if ( sigma2 > MagickEpsilon )
1001 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001002 A = 1.0/(2.0*sigma*sigma);
1003 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001004 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1005 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001006 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001007 }
anthony9eb4f742010-05-18 02:45:54 +00001008 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001009 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1010 }
anthony9eb4f742010-05-18 02:45:54 +00001011
1012 if ( type == LOGKernel )
1013 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1014 if ( sigma > MagickEpsilon )
1015 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1016 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001017 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1018 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001019 { R = ((double)(u*u+v*v))*A;
1020 kernel->values[i] = (1-R)*exp(-R)*B;
1021 }
1022 }
1023 else /* special case - generate a unity kernel */
1024 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1025 kernel->width*kernel->height*sizeof(double));
1026 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1027 }
1028 }
1029
1030 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001031 ** radius, producing a smaller (darker) kernel. Also for very small
1032 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1033 ** producing a very bright kernel.
1034 **
1035 ** Normalization will still be needed.
1036 */
anthony602ab9b2010-01-05 08:06:50 +00001037
anthony3dd0f622010-05-13 12:57:32 +00001038 /* Normalize the 2D Gaussian Kernel
1039 **
anthonyc1061722010-05-14 06:23:49 +00001040 ** NB: a CorrelateNormalize performs a normal Normalize if
1041 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001042 */
anthony46a369d2010-05-19 02:41:48 +00001043 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001044 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001045
1046 break;
1047 }
1048 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +00001049 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001050 { double
anthonyc1061722010-05-14 06:23:49 +00001051 sigma = fabs(args->sigma),
1052 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001053 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001054
anthonyc1061722010-05-14 06:23:49 +00001055 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001056 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001057 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1058 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1059 else
1060 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001061 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001062 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001063 kernel->y = 0;
1064 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001065 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1066 kernel->height*sizeof(double));
1067 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001068 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001069
1070#if 1
1071#define KernelRank 3
1072 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1073 ** It generates a gaussian 3 times the width, and compresses it into
1074 ** the expected range. This produces a closer normalization of the
1075 ** resulting kernel, especially for very low sigma values.
1076 ** As such while wierd it is prefered.
1077 **
1078 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001079 **
1080 ** A properly normalized curve is generated (apart from edge clipping)
1081 ** even though we later normalize the result (for edge clipping)
1082 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001083 */
anthonyc1061722010-05-14 06:23:49 +00001084
1085 /* initialize */
cristybb503372010-05-27 20:51:26 +00001086 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001087 (void) ResetMagickMemory(kernel->values,0, (size_t)
1088 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001089 /* Calculate a Positive 1D Gaussian */
1090 if ( sigma > MagickEpsilon )
1091 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001092 A = 1.0/(2.0*sigma*sigma);
1093 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001094 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001095 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001096 }
1097 }
1098 else /* special case - generate a unity kernel */
1099 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001100
1101 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001102 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001103 {
anthonyc1061722010-05-14 06:23:49 +00001104 if ( sigma2 > MagickEpsilon )
1105 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001106 A = 1.0/(2.0*sigma*sigma);
1107 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001108 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001109 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001110 }
anthony9eb4f742010-05-18 02:45:54 +00001111 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001112 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1113 }
anthony602ab9b2010-01-05 08:06:50 +00001114#else
anthonyc1061722010-05-14 06:23:49 +00001115 /* Direct calculation without curve averaging */
1116
1117 /* Calculate a Positive Gaussian */
1118 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001119 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1120 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001121 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001122 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001123 }
1124 else /* special case - generate a unity kernel */
1125 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1126 kernel->width*kernel->height*sizeof(double));
1127 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1128 }
anthony9eb4f742010-05-18 02:45:54 +00001129
1130 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001131 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001132 {
anthonyc1061722010-05-14 06:23:49 +00001133 if ( sigma2 > MagickEpsilon )
1134 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001135 A = 1.0/(2.0*sigma*sigma);
1136 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001137 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001138 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001139 }
anthony9eb4f742010-05-18 02:45:54 +00001140 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001141 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1142 }
anthony602ab9b2010-01-05 08:06:50 +00001143#endif
anthonyc1061722010-05-14 06:23:49 +00001144 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001145 ** radius, producing a smaller (darker) kernel. Also for very small
1146 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1147 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001148 **
1149 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001150 */
anthonycc6c8362010-01-25 04:14:01 +00001151
anthony602ab9b2010-01-05 08:06:50 +00001152 /* Normalize the 1D Gaussian Kernel
1153 **
anthonyc1061722010-05-14 06:23:49 +00001154 ** NB: a CorrelateNormalize performs a normal Normalize if
1155 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001156 */
anthony46a369d2010-05-19 02:41:48 +00001157 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1158 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001159
anthonyc1061722010-05-14 06:23:49 +00001160 /* rotate the 1D kernel by given angle */
1161 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001162 break;
1163 }
1164 case CometKernel:
1165 { double
anthony9eb4f742010-05-18 02:45:54 +00001166 sigma = fabs(args->sigma),
1167 A;
anthony602ab9b2010-01-05 08:06:50 +00001168
anthony602ab9b2010-01-05 08:06:50 +00001169 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001170 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001171 else
cristybb503372010-05-27 20:51:26 +00001172 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001173 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001174 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001175 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001176 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1177 kernel->height*sizeof(double));
1178 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001179 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001180
anthonyc1061722010-05-14 06:23:49 +00001181 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001182 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001183 ** curve to use so may change in the future. The function must be
1184 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001185 **
1186 ** As we are normalizing and not subtracting gaussians,
1187 ** there is no need for a divisor in the gaussian formula
1188 **
anthony43c49252010-05-18 10:59:50 +00001189 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001190 */
anthony9eb4f742010-05-18 02:45:54 +00001191 if ( sigma > MagickEpsilon )
1192 {
anthony602ab9b2010-01-05 08:06:50 +00001193#if 1
1194#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001195 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001196 (void) ResetMagickMemory(kernel->values,0, (size_t)
1197 kernel->width*sizeof(double));
1198 sigma *= KernelRank; /* simplify the loop expression */
1199 A = 1.0/(2.0*sigma*sigma);
1200 /* B = 1.0/(MagickSQ2PI*sigma); */
1201 for ( u=0; u < v; u++) {
1202 kernel->values[u/KernelRank] +=
1203 exp(-((double)(u*u))*A);
1204 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1205 }
cristybb503372010-05-27 20:51:26 +00001206 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001207 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001208#else
anthony9eb4f742010-05-18 02:45:54 +00001209 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1210 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001211 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001212 kernel->positive_range +=
1213 kernel->values[i] =
1214 exp(-((double)(i*i))*A);
1215 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001216#endif
anthony9eb4f742010-05-18 02:45:54 +00001217 }
1218 else /* special case - generate a unity kernel */
1219 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1220 kernel->width*kernel->height*sizeof(double));
1221 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1222 kernel->positive_range = 1.0;
1223 }
anthony46a369d2010-05-19 02:41:48 +00001224
1225 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001226 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001227 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001228
anthony999bb2c2010-02-18 12:38:01 +00001229 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1230 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001231 break;
1232 }
anthonyc1061722010-05-14 06:23:49 +00001233
anthony3c10fc82010-05-13 02:40:51 +00001234 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001235 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001236 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001237 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001238 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001239 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001240 break;
anthony9eb4f742010-05-18 02:45:54 +00001241 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001242 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001243 break;
1244 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001245 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1246 break;
1247 case 3:
anthonyc1061722010-05-14 06:23:49 +00001248 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001249 break;
anthony9eb4f742010-05-18 02:45:54 +00001250 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001251 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001252 "5: -4,-1,0,-1,-4 -1,2,3,2,-1 0,3,4,3,0 -1,2,3,2,-1 -4,-1,0,-1,-4");
anthony3c10fc82010-05-13 02:40:51 +00001253 break;
anthony9eb4f742010-05-18 02:45:54 +00001254 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001255 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001256 "7:-10,-5,-2,-1,-2,-5,-10 -5,0,3,4,3,0,-5 -2,3,6,7,6,3,-2 -1,4,7,8,7,4,-1 -2,3,6,7,6,3,-2 -5,0,3,4,3,0,-5 -10,-5,-2,-1,-2,-5,-10" );
anthony3c10fc82010-05-13 02:40:51 +00001257 break;
anthony43c49252010-05-18 10:59:50 +00001258 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001259 kernel=ParseKernelArray(
1260 "5: 0,0,-1,0,0 0,-1,-2,-1,0 -1,-2,16,-2,-1 0,-1,-2,-1,0 0,0,-1,0,0");
1261 break;
anthony43c49252010-05-18 10:59:50 +00001262 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1263 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1264 kernel=ParseKernelArray(
1265 "9: 0,-1,-1,-2,-2,-2,-1,-1,0 -1,-2,-4,-5,-5,-5,-4,-2,-1 -1,-4,-5,-3,-0,-3,-5,-4,-1 -2,-5,-3,@12,@24,@12,-3,-5,-2 -2,-5,-0,@24,@40,@24,-0,-5,-2 -2,-5,-3,@12,@24,@12,-3,-5,-2 -1,-4,-5,-3,-0,-3,-5,-4,-1 -1,-2,-4,-5,-5,-5,-4,-2,-1 0,-1,-1,-2,-2,-2,-1,-1,0");
1266 break;
anthony3c10fc82010-05-13 02:40:51 +00001267 }
1268 if (kernel == (KernelInfo *) NULL)
1269 return(kernel);
1270 kernel->type = type;
1271 break;
1272 }
anthonyc1061722010-05-14 06:23:49 +00001273 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001274 {
anthonyc1061722010-05-14 06:23:49 +00001275 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1276 if (kernel == (KernelInfo *) NULL)
1277 return(kernel);
1278 kernel->type = type;
1279 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1280 break;
1281 }
1282 case RobertsKernel:
1283 {
1284 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1285 if (kernel == (KernelInfo *) NULL)
1286 return(kernel);
1287 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001288 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001289 break;
1290 }
1291 case PrewittKernel:
1292 {
1293 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1294 if (kernel == (KernelInfo *) NULL)
1295 return(kernel);
1296 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001297 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001298 break;
1299 }
1300 case CompassKernel:
1301 {
1302 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1303 if (kernel == (KernelInfo *) NULL)
1304 return(kernel);
1305 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001306 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001307 break;
1308 }
anthony9eb4f742010-05-18 02:45:54 +00001309 case KirschKernel:
1310 {
1311 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1312 if (kernel == (KernelInfo *) NULL)
1313 return(kernel);
1314 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001315 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001316 break;
1317 }
anthonye2a60ce2010-05-19 12:30:40 +00001318 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001319 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1320 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001321 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001322 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001323 case 0:
1324 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1325 if (kernel == (KernelInfo *) NULL)
1326 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001327 kernel->values[3] = -MagickSQ2;
1328 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001329 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001330 break;
anthonye2a60ce2010-05-19 12:30:40 +00001331 case 1:
1332 kernel=ParseKernelArray("3: 1,2,1 0,0,0 -1,2,-1");
1333 if (kernel == (KernelInfo *) NULL)
1334 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001335 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001336 kernel->values[1] = +MagickSQ2;
1337 kernel->values[7] = -MagickSQ2;
1338 CalcKernelMetaData(kernel); /* recalculate meta-data */
1339 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1340 break;
1341 case 2:
1342 kernel=ParseKernelArray("3: 1,0,1 2,0,2 1,0,1");
1343 if (kernel == (KernelInfo *) NULL)
1344 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001345 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001346 kernel->values[3] = +MagickSQ2;
1347 kernel->values[5] = +MagickSQ2;
1348 CalcKernelMetaData(kernel);
1349 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1350 break;
1351 case 3:
1352 kernel=ParseKernelArray("3: 0,-1,2 1,0,-1 -2,1,0");
1353 if (kernel == (KernelInfo *) NULL)
1354 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001355 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001356 kernel->values[2] = +MagickSQ2;
1357 kernel->values[6] = -MagickSQ2;
1358 CalcKernelMetaData(kernel);
1359 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1360 break;
1361 case 4:
anthony6915d062010-05-19 12:45:51 +00001362 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001363 if (kernel == (KernelInfo *) NULL)
1364 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001365 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001366 kernel->values[0] = +MagickSQ2;
1367 kernel->values[8] = -MagickSQ2;
1368 CalcKernelMetaData(kernel);
1369 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1370 break;
1371 case 5:
1372 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1373 if (kernel == (KernelInfo *) NULL)
1374 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001375 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001376 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1377 break;
1378 case 6:
1379 kernel=ParseKernelArray("3: -1,0,1 0,0,0 1,0,-1");
1380 if (kernel == (KernelInfo *) NULL)
1381 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001382 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001383 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1384 break;
1385 case 7:
anthonyf4e00312010-05-20 12:06:35 +00001386 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001387 if (kernel == (KernelInfo *) NULL)
1388 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001389 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001390 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1391 break;
1392 case 8:
1393 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1394 if (kernel == (KernelInfo *) NULL)
1395 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001396 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001397 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1398 break;
1399 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001400 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001401 if (kernel == (KernelInfo *) NULL)
1402 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001403 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001404 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1405 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001406 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001407 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1408 if (kernel == (KernelInfo *) NULL)
1409 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001410 break;
anthonye2a60ce2010-05-19 12:30:40 +00001411 }
anthonyc3cd15b2010-05-27 06:05:40 +00001412 if ( fabs(args->sigma) > MagickEpsilon )
1413 /* Rotate by correctly supplied 'angle' */
1414 RotateKernelInfo(kernel, args->sigma);
1415 else if ( args->rho > 30.0 || args->rho < -30.0 )
1416 /* Rotate by out of bounds 'type' */
1417 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001418 break;
1419 }
1420
anthonyc1061722010-05-14 06:23:49 +00001421 /* Boolean Kernels */
1422 case DiamondKernel:
1423 {
1424 if (args->rho < 1.0)
1425 kernel->width = kernel->height = 3; /* default radius = 1 */
1426 else
cristybb503372010-05-27 20:51:26 +00001427 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1428 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001429
1430 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1431 kernel->height*sizeof(double));
1432 if (kernel->values == (double *) NULL)
1433 return(DestroyKernelInfo(kernel));
1434
1435 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001436 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1437 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1438 if ((labs(u)+labs(v)) <= (ssize_t)kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001439 kernel->positive_range += kernel->values[i] = args->sigma;
1440 else
1441 kernel->values[i] = nan;
1442 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1443 break;
1444 }
1445 case SquareKernel:
1446 case RectangleKernel:
1447 { double
1448 scale;
anthony602ab9b2010-01-05 08:06:50 +00001449 if ( type == SquareKernel )
1450 {
1451 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001452 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001453 else
cristybb503372010-05-27 20:51:26 +00001454 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1455 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001456 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001457 }
1458 else {
cristy2be15382010-01-21 02:38:03 +00001459 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001460 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001461 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001462 kernel->width = (size_t)args->rho;
1463 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001464 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1465 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001466 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001467 kernel->x = (ssize_t) args->xi;
1468 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001469 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001470 }
1471 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1472 kernel->height*sizeof(double));
1473 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001474 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001475
anthony3dd0f622010-05-13 12:57:32 +00001476 /* set all kernel values to scale given */
cristybb503372010-05-27 20:51:26 +00001477 u=(ssize_t) kernel->width*kernel->height;
cristy150989e2010-02-01 14:59:39 +00001478 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001479 kernel->values[i] = scale;
1480 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1481 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001482 break;
anthony602ab9b2010-01-05 08:06:50 +00001483 }
anthony602ab9b2010-01-05 08:06:50 +00001484 case DiskKernel:
1485 {
cristybb503372010-05-27 20:51:26 +00001486 ssize_t limit = (ssize_t)(args->rho*args->rho);
anthony83ba99b2010-01-24 08:48:15 +00001487 if (args->rho < 0.1) /* default radius approx 3.5 */
1488 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001489 else
cristybb503372010-05-27 20:51:26 +00001490 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1491 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001492
1493 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1494 kernel->height*sizeof(double));
1495 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001496 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001497
anthony3dd0f622010-05-13 12:57:32 +00001498 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001499 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1500 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001501 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001502 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001503 else
1504 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001505 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001506 break;
1507 }
1508 case PlusKernel:
1509 {
1510 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001511 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001512 else
cristybb503372010-05-27 20:51:26 +00001513 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1514 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001515
1516 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1517 kernel->height*sizeof(double));
1518 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001519 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001520
cristycee97112010-05-28 00:44:52 +00001521 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001522 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1523 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001524 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1525 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1526 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001527 break;
1528 }
anthony3dd0f622010-05-13 12:57:32 +00001529 case CrossKernel:
1530 {
1531 if (args->rho < 1.0)
1532 kernel->width = kernel->height = 5; /* default radius 2 */
1533 else
cristybb503372010-05-27 20:51:26 +00001534 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1535 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001536
1537 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1538 kernel->height*sizeof(double));
1539 if (kernel->values == (double *) NULL)
1540 return(DestroyKernelInfo(kernel));
1541
cristycee97112010-05-28 00:44:52 +00001542 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001543 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1544 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001545 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1546 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1547 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1548 break;
1549 }
1550 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001551 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001552 case PeaksKernel:
1553 {
cristybb503372010-05-27 20:51:26 +00001554 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001555 limit1,
anthonyc1061722010-05-14 06:23:49 +00001556 limit2,
1557 scale;
anthony3dd0f622010-05-13 12:57:32 +00001558
1559 if (args->rho < args->sigma)
1560 {
cristybb503372010-05-27 20:51:26 +00001561 kernel->width = ((size_t)args->sigma)*2+1;
1562 limit1 = (ssize_t)args->rho*args->rho;
1563 limit2 = (ssize_t)args->sigma*args->sigma;
anthony3dd0f622010-05-13 12:57:32 +00001564 }
1565 else
1566 {
cristybb503372010-05-27 20:51:26 +00001567 kernel->width = ((size_t)args->rho)*2+1;
1568 limit1 = (ssize_t)args->sigma*args->sigma;
1569 limit2 = (ssize_t)args->rho*args->rho;
anthony3dd0f622010-05-13 12:57:32 +00001570 }
anthonyc1061722010-05-14 06:23:49 +00001571 if ( limit2 <= 0 )
1572 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1573
anthony3dd0f622010-05-13 12:57:32 +00001574 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001575 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001576 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1577 kernel->height*sizeof(double));
1578 if (kernel->values == (double *) NULL)
1579 return(DestroyKernelInfo(kernel));
1580
anthonyc1061722010-05-14 06:23:49 +00001581 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001582 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1583 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1584 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1585 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001586 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001587 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001588 else
1589 kernel->values[i] = nan;
1590 }
cristye96405a2010-05-19 02:24:31 +00001591 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001592 if ( type == PeaksKernel ) {
1593 /* set the central point in the middle */
1594 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1595 kernel->positive_range = 1.0;
1596 kernel->maximum = 1.0;
1597 }
anthony3dd0f622010-05-13 12:57:32 +00001598 break;
1599 }
anthony43c49252010-05-18 10:59:50 +00001600 case EdgesKernel:
1601 {
1602 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1603 if (kernel == (KernelInfo *) NULL)
1604 return(kernel);
1605 kernel->type = type;
1606 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1607 break;
1608 }
anthony3dd0f622010-05-13 12:57:32 +00001609 case CornersKernel:
1610 {
anthony4f1dcb72010-05-14 08:43:10 +00001611 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001612 if (kernel == (KernelInfo *) NULL)
1613 return(kernel);
1614 kernel->type = type;
1615 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1616 break;
1617 }
anthony47f5d062010-05-23 07:47:50 +00001618 case RidgesKernel:
1619 {
anthony24a19842010-05-27 12:18:34 +00001620 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001621 if (kernel == (KernelInfo *) NULL)
1622 return(kernel);
1623 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001624 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001625 break;
1626 }
anthony1d45eb92010-05-25 11:13:23 +00001627 case Ridges2Kernel:
1628 {
1629 KernelInfo
1630 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001631 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001632 if (kernel == (KernelInfo *) NULL)
1633 return(kernel);
1634 kernel->type = type;
1635 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001636#if 0
1637 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001638 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1639 if (new_kernel == (KernelInfo *) NULL)
1640 return(DestroyKernelInfo(kernel));
1641 new_kernel->type = type;
1642 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1643 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001644#endif
1645 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1646 /* Unfortunatally we can not yet rotate a non-square kernel */
1647 /* But then we can't flip a non-symetrical kernel either */
1648 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1649 if (new_kernel == (KernelInfo *) NULL)
1650 return(DestroyKernelInfo(kernel));
1651 new_kernel->type = type;
1652 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001653 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001654 if (new_kernel == (KernelInfo *) NULL)
1655 return(DestroyKernelInfo(kernel));
1656 new_kernel->type = type;
1657 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001658 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001659 if (new_kernel == (KernelInfo *) NULL)
1660 return(DestroyKernelInfo(kernel));
1661 new_kernel->type = type;
1662 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001663 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001664 if (new_kernel == (KernelInfo *) NULL)
1665 return(DestroyKernelInfo(kernel));
1666 new_kernel->type = type;
1667 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001668 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001669 if (new_kernel == (KernelInfo *) NULL)
1670 return(DestroyKernelInfo(kernel));
1671 new_kernel->type = type;
1672 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001673 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001674 if (new_kernel == (KernelInfo *) NULL)
1675 return(DestroyKernelInfo(kernel));
1676 new_kernel->type = type;
1677 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001678 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001679 if (new_kernel == (KernelInfo *) NULL)
1680 return(DestroyKernelInfo(kernel));
1681 new_kernel->type = type;
1682 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001683 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001684 if (new_kernel == (KernelInfo *) NULL)
1685 return(DestroyKernelInfo(kernel));
1686 new_kernel->type = type;
1687 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001688 break;
1689 }
anthony3dd0f622010-05-13 12:57:32 +00001690 case LineEndsKernel:
1691 {
anthony43c49252010-05-18 10:59:50 +00001692 KernelInfo
1693 *new_kernel;
1694 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001695 if (kernel == (KernelInfo *) NULL)
1696 return(kernel);
1697 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001698 ExpandKernelInfo(kernel, 90.0);
1699 /* append second set of 4 kernels */
1700 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1701 if (new_kernel == (KernelInfo *) NULL)
1702 return(DestroyKernelInfo(kernel));
1703 new_kernel->type = type;
1704 ExpandKernelInfo(new_kernel, 90.0);
1705 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001706 break;
1707 }
1708 case LineJunctionsKernel:
1709 {
1710 KernelInfo
1711 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001712 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001713 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001714 if (kernel == (KernelInfo *) NULL)
1715 return(kernel);
1716 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001717 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001718 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001719 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001720 if (new_kernel == (KernelInfo *) NULL)
1721 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001722 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001723 ExpandKernelInfo(new_kernel, 90.0);
1724 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001725 break;
1726 }
anthony3dd0f622010-05-13 12:57:32 +00001727 case ConvexHullKernel:
1728 {
anthony3928ec62010-05-27 14:03:29 +00001729 KernelInfo
1730 *new_kernel;
1731 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001732 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001733 if (kernel == (KernelInfo *) NULL)
1734 return(kernel);
1735 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001736 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001737 /* append the mirror versions too */
1738 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1739 if (new_kernel == (KernelInfo *) NULL)
1740 return(DestroyKernelInfo(kernel));
1741 new_kernel->type = type;
1742 ExpandKernelInfo(new_kernel, 45.0);
1743 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001744 break;
1745 }
anthony47f5d062010-05-23 07:47:50 +00001746 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001747 { /* what is the best form for skeletonization by thinning? */
anthony47f5d062010-05-23 07:47:50 +00001748#if 0
1749# if 0
1750 kernel=AcquireKernelInfo("Corners;Edges");
1751# else
1752 kernel=AcquireKernelInfo("Edges;Corners");
1753# endif
1754#else
anthony43c49252010-05-18 10:59:50 +00001755 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001756 if (kernel == (KernelInfo *) NULL)
1757 return(kernel);
1758 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001759 ExpandKernelInfo(kernel, 45);
1760 break;
anthony47f5d062010-05-23 07:47:50 +00001761#endif
anthony3dd0f622010-05-13 12:57:32 +00001762 break;
1763 }
anthonya648a302010-05-27 02:14:36 +00001764 case MatKernel: /* experimental - MAT from a Distance Gradient */
1765 {
1766 KernelInfo
1767 *new_kernel;
1768 /* Ridge Kernel but without the diagonal */
1769 kernel=ParseKernelArray("3x1: 0,1,0");
1770 if (kernel == (KernelInfo *) NULL)
1771 return(kernel);
1772 kernel->type = RidgesKernel;
1773 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1774 /* Plus the 2 pixel ridges kernel - no diagonal */
1775 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1776 if (new_kernel == (KernelInfo *) NULL)
1777 return(kernel);
1778 LastKernelInfo(kernel)->next = new_kernel;
1779 break;
1780 }
anthony602ab9b2010-01-05 08:06:50 +00001781 /* Distance Measuring Kernels */
1782 case ChebyshevKernel:
1783 {
anthony602ab9b2010-01-05 08:06:50 +00001784 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001785 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001786 else
cristybb503372010-05-27 20:51:26 +00001787 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1788 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001789
1790 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1791 kernel->height*sizeof(double));
1792 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001793 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001794
cristybb503372010-05-27 20:51:26 +00001795 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1796 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001797 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001798 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001799 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001800 break;
1801 }
1802 case ManhattenKernel:
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] =
anthonyc84dce52010-05-07 05:42:23 +00001818 args->sigma*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001819 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001820 break;
1821 }
1822 case EuclideanKernel:
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] =
anthonyc84dce52010-05-07 05:42:23 +00001838 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001839 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001840 break;
1841 }
anthony46a369d2010-05-19 02:41:48 +00001842 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001843 default:
anthonyc1061722010-05-14 06:23:49 +00001844 {
anthony46a369d2010-05-19 02:41:48 +00001845 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1846 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001847 if (kernel == (KernelInfo *) NULL)
1848 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001849 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001850 break;
1851 }
anthony602ab9b2010-01-05 08:06:50 +00001852 break;
1853 }
1854
1855 return(kernel);
1856}
anthonyc94cdb02010-01-06 08:15:29 +00001857
anthony602ab9b2010-01-05 08:06:50 +00001858/*
1859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1860% %
1861% %
1862% %
cristy6771f1e2010-03-05 19:43:39 +00001863% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001864% %
1865% %
1866% %
1867%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1868%
anthony1b2bc0a2010-05-12 05:25:22 +00001869% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1870% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001871% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001872%
cristye6365592010-04-02 17:31:23 +00001873% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001874%
anthony930be612010-02-08 04:26:15 +00001875% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001876%
1877% A description of each parameter follows:
1878%
1879% o kernel: the Morphology/Convolution kernel to be cloned
1880%
1881*/
cristyef656912010-03-05 19:54:59 +00001882MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001883{
cristybb503372010-05-27 20:51:26 +00001884 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001885 i;
1886
cristy19eb6412010-04-23 14:42:29 +00001887 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001888 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001889
1890 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001891 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1892 if (new_kernel == (KernelInfo *) NULL)
1893 return(new_kernel);
1894 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001895
1896 /* replace the values with a copy of the values */
1897 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001898 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001899 if (new_kernel->values == (double *) NULL)
1900 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001901 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001902 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001903
1904 /* Also clone the next kernel in the kernel list */
1905 if ( kernel->next != (KernelInfo *) NULL ) {
1906 new_kernel->next = CloneKernelInfo(kernel->next);
1907 if ( new_kernel->next == (KernelInfo *) NULL )
1908 return(DestroyKernelInfo(new_kernel));
1909 }
1910
anthony7a01dcf2010-05-11 12:25:52 +00001911 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001912}
1913
1914/*
1915%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1916% %
1917% %
1918% %
anthony83ba99b2010-01-24 08:48:15 +00001919% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001920% %
1921% %
1922% %
1923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924%
anthony83ba99b2010-01-24 08:48:15 +00001925% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1926% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001927%
anthony83ba99b2010-01-24 08:48:15 +00001928% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001929%
anthony83ba99b2010-01-24 08:48:15 +00001930% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001931%
1932% A description of each parameter follows:
1933%
1934% o kernel: the Morphology/Convolution kernel to be destroyed
1935%
1936*/
1937
anthony83ba99b2010-01-24 08:48:15 +00001938MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001939{
cristy2be15382010-01-21 02:38:03 +00001940 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001941
anthony7a01dcf2010-05-11 12:25:52 +00001942 if ( kernel->next != (KernelInfo *) NULL )
1943 kernel->next = DestroyKernelInfo(kernel->next);
1944
1945 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1946 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001947 return(kernel);
1948}
anthonyc94cdb02010-01-06 08:15:29 +00001949
1950/*
1951%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1952% %
1953% %
1954% %
anthony3c10fc82010-05-13 02:40:51 +00001955% E x p a n d K e r n e l I n f o %
1956% %
1957% %
1958% %
1959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960%
1961% ExpandKernelInfo() takes a single kernel, and expands it into a list
1962% of kernels each incrementally rotated the angle given.
1963%
1964% WARNING: 45 degree rotations only works for 3x3 kernels.
1965% While 90 degree roatations only works for linear and square kernels
1966%
1967% The format of the RotateKernelInfo method is:
1968%
1969% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1970%
1971% A description of each parameter follows:
1972%
1973% o kernel: the Morphology/Convolution kernel
1974%
1975% o angle: angle to rotate in degrees
1976%
1977% This function is only internel to this module, as it is not finalized,
1978% especially with regard to non-orthogonal angles, and rotation of larger
1979% 2D kernels.
1980*/
anthony47f5d062010-05-23 07:47:50 +00001981
1982/* Internal Routine - Return true if two kernels are the same */
1983static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1984 const KernelInfo *kernel2)
1985{
cristybb503372010-05-27 20:51:26 +00001986 register size_t
anthony47f5d062010-05-23 07:47:50 +00001987 i;
anthony1d45eb92010-05-25 11:13:23 +00001988
1989 /* check size and origin location */
1990 if ( kernel1->width != kernel2->width
1991 || kernel1->height != kernel2->height
1992 || kernel1->x != kernel2->x
1993 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001994 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001995
1996 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001997 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001998 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001999 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
2000 return MagickFalse;
2001 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2002 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002003 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002004 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2005 return MagickFalse;
2006 }
anthony1d45eb92010-05-25 11:13:23 +00002007
anthony47f5d062010-05-23 07:47:50 +00002008 return MagickTrue;
2009}
2010
2011static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002012{
2013 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002014 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002015 *last;
cristya9a61ad2010-05-13 12:47:41 +00002016
anthony3c10fc82010-05-13 02:40:51 +00002017 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002018 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002019 clone = CloneKernelInfo(last);
2020 RotateKernelInfo(clone, angle);
2021 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002022 break;
cristy84d9b552010-05-24 18:23:54 +00002023 last->next = clone;
2024 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002025 }
cristy84d9b552010-05-24 18:23:54 +00002026 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002027 return;
anthony3c10fc82010-05-13 02:40:51 +00002028}
2029
2030/*
2031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032% %
2033% %
2034% %
anthony46a369d2010-05-19 02:41:48 +00002035+ C a l c M e t a K e r n a l I n f o %
2036% %
2037% %
2038% %
2039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2040%
2041% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2042% using the kernel values. This should only ne used if it is not posible to
2043% calculate that meta-data in some easier way.
2044%
2045% It is important that the meta-data is correct before ScaleKernelInfo() is
2046% used to perform kernel normalization.
2047%
2048% The format of the CalcKernelMetaData method is:
2049%
2050% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2051%
2052% A description of each parameter follows:
2053%
2054% o kernel: the Morphology/Convolution kernel to modify
2055%
2056% WARNING: Minimum and Maximum values are assumed to include zero, even if
2057% zero is not part of the kernel (as in Gaussian Derived kernels). This
2058% however is not true for flat-shaped morphological kernels.
2059%
2060% WARNING: Only the specific kernel pointed to is modified, not a list of
2061% multiple kernels.
2062%
2063% This is an internal function and not expected to be useful outside this
2064% module. This could change however.
2065*/
2066static void CalcKernelMetaData(KernelInfo *kernel)
2067{
cristybb503372010-05-27 20:51:26 +00002068 register size_t
anthony46a369d2010-05-19 02:41:48 +00002069 i;
2070
2071 kernel->minimum = kernel->maximum = 0.0;
2072 kernel->negative_range = kernel->positive_range = 0.0;
2073 for (i=0; i < (kernel->width*kernel->height); i++)
2074 {
2075 if ( fabs(kernel->values[i]) < MagickEpsilon )
2076 kernel->values[i] = 0.0;
2077 ( kernel->values[i] < 0)
2078 ? ( kernel->negative_range += kernel->values[i] )
2079 : ( kernel->positive_range += kernel->values[i] );
2080 Minimize(kernel->minimum, kernel->values[i]);
2081 Maximize(kernel->maximum, kernel->values[i]);
2082 }
2083
2084 return;
2085}
2086
2087/*
2088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2089% %
2090% %
2091% %
anthony9eb4f742010-05-18 02:45:54 +00002092% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002093% %
2094% %
2095% %
2096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097%
anthony9eb4f742010-05-18 02:45:54 +00002098% MorphologyApply() applies a morphological method, multiple times using
2099% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002100%
anthony9eb4f742010-05-18 02:45:54 +00002101% It is basically equivelent to as MorphologyImageChannel() (see below) but
2102% without user controls, that that function extracts and applies to kernels
2103% and morphology methods.
2104%
2105% More specifically kernels are not normalized/scaled/blended by the
2106% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2107% (-bias setting or image->bias) is passed directly to this function,
2108% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002109%
anthony47f5d062010-05-23 07:47:50 +00002110% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002111%
anthony9eb4f742010-05-18 02:45:54 +00002112% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002113% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002114% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002115% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002116%
2117% A description of each parameter follows:
2118%
2119% o image: the image.
2120%
2121% o method: the morphology method to be applied.
2122%
2123% o iterations: apply the operation this many times (or no change).
2124% A value of -1 means loop until no change found.
2125% How this is applied may depend on the morphology method.
2126% Typically this is a value of 1.
2127%
2128% o channel: the channel type.
2129%
2130% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002131% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002132%
anthony47f5d062010-05-23 07:47:50 +00002133% o compose: How to handle or merge multi-kernel results.
2134% If 'Undefined' use default of the Morphology method.
2135% If 'No' force image to be re-iterated by each kernel.
2136% Otherwise merge the results using the mathematical compose
2137% method given.
2138%
2139% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002140%
anthony602ab9b2010-01-05 08:06:50 +00002141% o exception: return any errors or warnings in this structure.
2142%
anthony602ab9b2010-01-05 08:06:50 +00002143*/
2144
anthony930be612010-02-08 04:26:15 +00002145
anthony9eb4f742010-05-18 02:45:54 +00002146/* Apply a Morphology Primative to an image using the given kernel.
2147** Two pre-created images must be provided, no image is created.
2148** Returning the number of pixels that changed.
2149*/
cristybb503372010-05-27 20:51:26 +00002150static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002151 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002152 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002153{
cristy2be15382010-01-21 02:38:03 +00002154#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002155
cristy5f959472010-05-27 22:19:46 +00002156 CacheView
2157 *p_view,
2158 *q_view;
2159
cristybb503372010-05-27 20:51:26 +00002160 ssize_t
anthony29188a82010-01-22 10:12:34 +00002161 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002162 changed;
2163
2164 MagickBooleanType
2165 status;
2166
cristy5f959472010-05-27 22:19:46 +00002167 MagickOffsetType
2168 progress;
anthony602ab9b2010-01-05 08:06:50 +00002169
anthony602ab9b2010-01-05 08:06:50 +00002170 status=MagickTrue;
2171 changed=0;
2172 progress=0;
2173
anthony602ab9b2010-01-05 08:06:50 +00002174 p_view=AcquireCacheView(image);
2175 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002176
anthonycc6c8362010-01-25 04:14:01 +00002177 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002178 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002179 */
cristyc99304f2010-02-01 15:26:27 +00002180 offx = kernel->x;
2181 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002182 switch(method) {
anthony930be612010-02-08 04:26:15 +00002183 case ConvolveMorphology:
2184 case DilateMorphology:
2185 case DilateIntensityMorphology:
2186 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002187 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002188 offx = (ssize_t) kernel->width-offx-1;
2189 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002190 break;
anthony5ef8e942010-05-11 06:51:12 +00002191 case ErodeMorphology:
2192 case ErodeIntensityMorphology:
2193 case HitAndMissMorphology:
2194 case ThinningMorphology:
2195 case ThickenMorphology:
2196 /* kernel is user as is, without reflection */
2197 break;
anthony930be612010-02-08 04:26:15 +00002198 default:
anthony9eb4f742010-05-18 02:45:54 +00002199 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002200 break;
anthony29188a82010-01-22 10:12:34 +00002201 }
2202
anthony602ab9b2010-01-05 08:06:50 +00002203#if defined(MAGICKCORE_OPENMP_SUPPORT)
2204 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2205#endif
cristybb503372010-05-27 20:51:26 +00002206 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002207 {
2208 MagickBooleanType
2209 sync;
2210
2211 register const PixelPacket
2212 *restrict p;
2213
2214 register const IndexPacket
2215 *restrict p_indexes;
2216
2217 register PixelPacket
2218 *restrict q;
2219
2220 register IndexPacket
2221 *restrict q_indexes;
2222
cristybb503372010-05-27 20:51:26 +00002223 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002224 x;
2225
cristybb503372010-05-27 20:51:26 +00002226 size_t
anthony602ab9b2010-01-05 08:06:50 +00002227 r;
2228
2229 if (status == MagickFalse)
2230 continue;
anthony29188a82010-01-22 10:12:34 +00002231 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2232 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002233 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2234 exception);
2235 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2236 {
2237 status=MagickFalse;
2238 continue;
2239 }
2240 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2241 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002242 r = (image->columns+kernel->width)*offy+offx; /* constant */
2243
cristybb503372010-05-27 20:51:26 +00002244 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002245 {
cristybb503372010-05-27 20:51:26 +00002246 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002247 v;
2248
cristybb503372010-05-27 20:51:26 +00002249 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002250 u;
2251
2252 register const double
2253 *restrict k;
2254
2255 register const PixelPacket
2256 *restrict k_pixels;
2257
2258 register const IndexPacket
2259 *restrict k_indexes;
2260
2261 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002262 result,
2263 min,
2264 max;
anthony602ab9b2010-01-05 08:06:50 +00002265
anthony29188a82010-01-22 10:12:34 +00002266 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002267 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002268 */
anthony602ab9b2010-01-05 08:06:50 +00002269 *q = p[r];
2270 if (image->colorspace == CMYKColorspace)
2271 q_indexes[x] = p_indexes[r];
2272
anthony5ef8e942010-05-11 06:51:12 +00002273 /* Defaults */
2274 min.red =
2275 min.green =
2276 min.blue =
2277 min.opacity =
2278 min.index = (MagickRealType) QuantumRange;
2279 max.red =
2280 max.green =
2281 max.blue =
2282 max.opacity =
2283 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002284 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002285 result.red = (MagickRealType) p[r].red;
2286 result.green = (MagickRealType) p[r].green;
2287 result.blue = (MagickRealType) p[r].blue;
2288 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002289 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002290 if ( image->colorspace == CMYKColorspace)
2291 result.index = (MagickRealType) p_indexes[r];
2292
anthony602ab9b2010-01-05 08:06:50 +00002293 switch (method) {
2294 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002295 /* Set the user defined bias of the weighted average output */
2296 result.red =
2297 result.green =
2298 result.blue =
2299 result.opacity =
2300 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002301 break;
anthony4fd27e22010-02-07 08:17:18 +00002302 case DilateIntensityMorphology:
2303 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002304 /* use a boolean flag indicating when first match found */
2305 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002306 break;
anthony602ab9b2010-01-05 08:06:50 +00002307 default:
anthony602ab9b2010-01-05 08:06:50 +00002308 break;
2309 }
2310
2311 switch ( method ) {
2312 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002313 /* Weighted Average of pixels using reflected kernel
2314 **
2315 ** NOTE for correct working of this operation for asymetrical
2316 ** kernels, the kernel needs to be applied in its reflected form.
2317 ** That is its values needs to be reversed.
2318 **
2319 ** Correlation is actually the same as this but without reflecting
2320 ** the kernel, and thus 'lower-level' that Convolution. However
2321 ** as Convolution is the more common method used, and it does not
2322 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002323 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002324 **
2325 ** Correlation will have its kernel reflected before calling
2326 ** this function to do a Convolve.
2327 **
2328 ** For more details of Correlation vs Convolution see
2329 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2330 */
anthony5ef8e942010-05-11 06:51:12 +00002331 if (((channel & SyncChannels) != 0 ) &&
2332 (image->matte == MagickTrue))
2333 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2334 ** Weight the color channels with Alpha Channel so that
2335 ** transparent pixels are not part of the results.
2336 */
anthony602ab9b2010-01-05 08:06:50 +00002337 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002338 alpha, /* color channel weighting : kernel*alpha */
2339 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002340
2341 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002342 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002343 k_pixels = p;
2344 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002345 for (v=0; v < (ssize_t) kernel->height; v++) {
2346 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002347 if ( IsNan(*k) ) continue;
2348 alpha=(*k)*(QuantumScale*(QuantumRange-
2349 k_pixels[u].opacity));
2350 gamma += alpha;
2351 result.red += alpha*k_pixels[u].red;
2352 result.green += alpha*k_pixels[u].green;
2353 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002354 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002355 if ( image->colorspace == CMYKColorspace)
2356 result.index += alpha*k_indexes[u];
2357 }
2358 k_pixels += image->columns+kernel->width;
2359 k_indexes += image->columns+kernel->width;
2360 }
2361 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002362 result.red *= gamma;
2363 result.green *= gamma;
2364 result.blue *= gamma;
2365 result.opacity *= gamma;
2366 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002367 }
anthony5ef8e942010-05-11 06:51:12 +00002368 else
2369 {
2370 /* No 'Sync' flag, or no Alpha involved.
2371 ** Convolution is simple individual channel weigthed sum.
2372 */
2373 k = &kernel->values[ kernel->width*kernel->height-1 ];
2374 k_pixels = p;
2375 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002376 for (v=0; v < (ssize_t) kernel->height; v++) {
2377 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002378 if ( IsNan(*k) ) continue;
2379 result.red += (*k)*k_pixels[u].red;
2380 result.green += (*k)*k_pixels[u].green;
2381 result.blue += (*k)*k_pixels[u].blue;
2382 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2383 if ( image->colorspace == CMYKColorspace)
2384 result.index += (*k)*k_indexes[u];
2385 }
2386 k_pixels += image->columns+kernel->width;
2387 k_indexes += image->columns+kernel->width;
2388 }
2389 }
anthony602ab9b2010-01-05 08:06:50 +00002390 break;
2391
anthony4fd27e22010-02-07 08:17:18 +00002392 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002393 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002394 **
2395 ** NOTE that the kernel is not reflected for this operation!
2396 **
2397 ** NOTE: in normal Greyscale Morphology, the kernel value should
2398 ** be added to the real value, this is currently not done, due to
2399 ** the nature of the boolean kernels being used.
2400 */
anthony4fd27e22010-02-07 08:17:18 +00002401 k = kernel->values;
2402 k_pixels = p;
2403 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002404 for (v=0; v < (ssize_t) kernel->height; v++) {
2405 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002406 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002407 Minimize(min.red, (double) k_pixels[u].red);
2408 Minimize(min.green, (double) k_pixels[u].green);
2409 Minimize(min.blue, (double) k_pixels[u].blue);
2410 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002411 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002412 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002413 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002414 }
2415 k_pixels += image->columns+kernel->width;
2416 k_indexes += image->columns+kernel->width;
2417 }
2418 break;
2419
anthony5ef8e942010-05-11 06:51:12 +00002420
anthony83ba99b2010-01-24 08:48:15 +00002421 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002422 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002423 **
2424 ** NOTE for correct working of this operation for asymetrical
2425 ** kernels, the kernel needs to be applied in its reflected form.
2426 ** That is its values needs to be reversed.
2427 **
2428 ** NOTE: in normal Greyscale Morphology, the kernel value should
2429 ** be added to the real value, this is currently not done, due to
2430 ** the nature of the boolean kernels being used.
2431 **
2432 */
anthony29188a82010-01-22 10:12:34 +00002433 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002434 k_pixels = p;
2435 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002436 for (v=0; v < (ssize_t) kernel->height; v++) {
2437 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002438 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002439 Maximize(max.red, (double) k_pixels[u].red);
2440 Maximize(max.green, (double) k_pixels[u].green);
2441 Maximize(max.blue, (double) k_pixels[u].blue);
2442 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002443 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002444 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002445 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002446 }
2447 k_pixels += image->columns+kernel->width;
2448 k_indexes += image->columns+kernel->width;
2449 }
anthony602ab9b2010-01-05 08:06:50 +00002450 break;
2451
anthony5ef8e942010-05-11 06:51:12 +00002452 case HitAndMissMorphology:
2453 case ThinningMorphology:
2454 case ThickenMorphology:
2455 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2456 **
2457 ** NOTE that the kernel is not reflected for this operation,
2458 ** and consists of both foreground and background pixel
2459 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2460 ** with either Nan or 0.5 values for don't care.
2461 **
2462 ** Note that this can produce negative results, though really
2463 ** only a positive match has any real value.
2464 */
2465 k = kernel->values;
2466 k_pixels = p;
2467 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002468 for (v=0; v < (ssize_t) kernel->height; v++) {
2469 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002470 if ( IsNan(*k) ) continue;
2471 if ( (*k) > 0.7 )
2472 { /* minimim of foreground pixels */
2473 Minimize(min.red, (double) k_pixels[u].red);
2474 Minimize(min.green, (double) k_pixels[u].green);
2475 Minimize(min.blue, (double) k_pixels[u].blue);
2476 Minimize(min.opacity,
2477 QuantumRange-(double) k_pixels[u].opacity);
2478 if ( image->colorspace == CMYKColorspace)
2479 Minimize(min.index, (double) k_indexes[u]);
2480 }
2481 else if ( (*k) < 0.3 )
2482 { /* maximum of background pixels */
2483 Maximize(max.red, (double) k_pixels[u].red);
2484 Maximize(max.green, (double) k_pixels[u].green);
2485 Maximize(max.blue, (double) k_pixels[u].blue);
2486 Maximize(max.opacity,
2487 QuantumRange-(double) k_pixels[u].opacity);
2488 if ( image->colorspace == CMYKColorspace)
2489 Maximize(max.index, (double) k_indexes[u]);
2490 }
2491 }
2492 k_pixels += image->columns+kernel->width;
2493 k_indexes += image->columns+kernel->width;
2494 }
2495 /* Pattern Match only if min fg larger than min bg pixels */
2496 min.red -= max.red; Maximize( min.red, 0.0 );
2497 min.green -= max.green; Maximize( min.green, 0.0 );
2498 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2499 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2500 min.index -= max.index; Maximize( min.index, 0.0 );
2501 break;
2502
anthony4fd27e22010-02-07 08:17:18 +00002503 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002504 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2505 **
2506 ** WARNING: the intensity test fails for CMYK and does not
2507 ** take into account the moderating effect of teh alpha channel
2508 ** on the intensity.
2509 **
2510 ** NOTE that the kernel is not reflected for this operation!
2511 */
anthony602ab9b2010-01-05 08:06:50 +00002512 k = kernel->values;
2513 k_pixels = p;
2514 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002515 for (v=0; v < (ssize_t) kernel->height; v++) {
2516 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002517 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002518 if ( result.red == 0.0 ||
2519 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2520 /* copy the whole pixel - no channel selection */
2521 *q = k_pixels[u];
2522 if ( result.red > 0.0 ) changed++;
2523 result.red = 1.0;
2524 }
anthony602ab9b2010-01-05 08:06:50 +00002525 }
2526 k_pixels += image->columns+kernel->width;
2527 k_indexes += image->columns+kernel->width;
2528 }
anthony602ab9b2010-01-05 08:06:50 +00002529 break;
2530
anthony83ba99b2010-01-24 08:48:15 +00002531 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002532 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2533 **
2534 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002535 ** take into account the moderating effect of the alpha channel
2536 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002537 **
2538 ** NOTE for correct working of this operation for asymetrical
2539 ** kernels, the kernel needs to be applied in its reflected form.
2540 ** That is its values needs to be reversed.
2541 */
anthony29188a82010-01-22 10:12:34 +00002542 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002543 k_pixels = p;
2544 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002545 for (v=0; v < (ssize_t) kernel->height; v++) {
2546 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002547 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2548 if ( result.red == 0.0 ||
2549 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2550 /* copy the whole pixel - no channel selection */
2551 *q = k_pixels[u];
2552 if ( result.red > 0.0 ) changed++;
2553 result.red = 1.0;
2554 }
anthony602ab9b2010-01-05 08:06:50 +00002555 }
2556 k_pixels += image->columns+kernel->width;
2557 k_indexes += image->columns+kernel->width;
2558 }
anthony602ab9b2010-01-05 08:06:50 +00002559 break;
2560
anthony5ef8e942010-05-11 06:51:12 +00002561
anthony602ab9b2010-01-05 08:06:50 +00002562 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002563 /* Add kernel Value and select the minimum value found.
2564 ** The result is a iterative distance from edge of image shape.
2565 **
2566 ** All Distance Kernels are symetrical, but that may not always
2567 ** be the case. For example how about a distance from left edges?
2568 ** To work correctly with asymetrical kernels the reflected kernel
2569 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002570 **
2571 ** Actually this is really a GreyErode with a negative kernel!
2572 **
anthony930be612010-02-08 04:26:15 +00002573 */
anthony29188a82010-01-22 10:12:34 +00002574 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002575 k_pixels = p;
2576 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002577 for (v=0; v < (ssize_t) kernel->height; v++) {
2578 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002579 if ( IsNan(*k) ) continue;
2580 Minimize(result.red, (*k)+k_pixels[u].red);
2581 Minimize(result.green, (*k)+k_pixels[u].green);
2582 Minimize(result.blue, (*k)+k_pixels[u].blue);
2583 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2584 if ( image->colorspace == CMYKColorspace)
2585 Minimize(result.index, (*k)+k_indexes[u]);
2586 }
2587 k_pixels += image->columns+kernel->width;
2588 k_indexes += image->columns+kernel->width;
2589 }
anthony602ab9b2010-01-05 08:06:50 +00002590 break;
2591
2592 case UndefinedMorphology:
2593 default:
2594 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002595 }
anthony5ef8e942010-05-11 06:51:12 +00002596 /* Final mathematics of results (combine with original image?)
2597 **
2598 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2599 ** be done here but works better with iteration as a image difference
2600 ** in the controling function (below). Thicken and Thinning however
2601 ** should be done here so thay can be iterated correctly.
2602 */
2603 switch ( method ) {
2604 case HitAndMissMorphology:
2605 case ErodeMorphology:
2606 result = min; /* minimum of neighbourhood */
2607 break;
2608 case DilateMorphology:
2609 result = max; /* maximum of neighbourhood */
2610 break;
2611 case ThinningMorphology:
2612 /* subtract pattern match from original */
2613 result.red -= min.red;
2614 result.green -= min.green;
2615 result.blue -= min.blue;
2616 result.opacity -= min.opacity;
2617 result.index -= min.index;
2618 break;
2619 case ThickenMorphology:
2620 /* Union with original image (maximize) - or should this be + */
2621 Maximize( result.red, min.red );
2622 Maximize( result.green, min.green );
2623 Maximize( result.blue, min.blue );
2624 Maximize( result.opacity, min.opacity );
2625 Maximize( result.index, min.index );
2626 break;
2627 default:
2628 /* result directly calculated or assigned */
2629 break;
2630 }
2631 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002632 switch ( method ) {
2633 case UndefinedMorphology:
2634 case DilateIntensityMorphology:
2635 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002636 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002637 default:
anthony83ba99b2010-01-24 08:48:15 +00002638 if ((channel & RedChannel) != 0)
2639 q->red = ClampToQuantum(result.red);
2640 if ((channel & GreenChannel) != 0)
2641 q->green = ClampToQuantum(result.green);
2642 if ((channel & BlueChannel) != 0)
2643 q->blue = ClampToQuantum(result.blue);
2644 if ((channel & OpacityChannel) != 0
2645 && image->matte == MagickTrue )
2646 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2647 if ((channel & IndexChannel) != 0
2648 && image->colorspace == CMYKColorspace)
2649 q_indexes[x] = ClampToQuantum(result.index);
2650 break;
2651 }
anthony5ef8e942010-05-11 06:51:12 +00002652 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002653 if ( ( p[r].red != q->red )
2654 || ( p[r].green != q->green )
2655 || ( p[r].blue != q->blue )
2656 || ( p[r].opacity != q->opacity )
2657 || ( image->colorspace == CMYKColorspace &&
2658 p_indexes[r] != q_indexes[x] ) )
2659 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002660 p++;
2661 q++;
anthony83ba99b2010-01-24 08:48:15 +00002662 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002663 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2664 if (sync == MagickFalse)
2665 status=MagickFalse;
2666 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2667 {
2668 MagickBooleanType
2669 proceed;
2670
2671#if defined(MAGICKCORE_OPENMP_SUPPORT)
2672 #pragma omp critical (MagickCore_MorphologyImage)
2673#endif
2674 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2675 if (proceed == MagickFalse)
2676 status=MagickFalse;
2677 }
anthony83ba99b2010-01-24 08:48:15 +00002678 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002679 result_image->type=image->type;
2680 q_view=DestroyCacheView(q_view);
2681 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002682 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002683}
2684
anthony4fd27e22010-02-07 08:17:18 +00002685
anthony9eb4f742010-05-18 02:45:54 +00002686MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002687 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002688 const KernelInfo *kernel, const CompositeOperator compose,
2689 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002690{
2691 Image
anthony47f5d062010-05-23 07:47:50 +00002692 *curr_image, /* Image we are working with or iterating */
2693 *work_image, /* secondary image for primative iteration */
2694 *save_image, /* saved image - for 'edge' method only */
2695 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002696
anthony4fd27e22010-02-07 08:17:18 +00002697 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002698 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2699 *norm_kernel, /* the current normal un-reflected kernel */
2700 *rflt_kernel, /* the current reflected kernel (if needed) */
2701 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002702
2703 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002704 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002705
2706 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002707 rslt_compose; /* multi-kernel compose method for results to use */
2708
2709 MagickBooleanType
2710 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002711
cristybb503372010-05-27 20:51:26 +00002712 size_t
anthony47f5d062010-05-23 07:47:50 +00002713 method_loop, /* Loop 1: number of compound method iterations */
2714 method_limit, /* maximum number of compound method iterations */
2715 kernel_number, /* Loop 2: the kernel number being applied */
2716 stage_loop, /* Loop 3: primative loop for compound morphology */
2717 stage_limit, /* how many primatives in this compound */
2718 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2719 kernel_limit, /* number of times to iterate kernel */
2720 count, /* total count of primative steps applied */
2721 changed, /* number pixels changed by last primative operation */
2722 kernel_changed, /* total count of changed using iterated kernel */
2723 method_changed; /* total count of changed over method iteration */
2724
2725 char
2726 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002727
anthony602ab9b2010-01-05 08:06:50 +00002728 assert(image != (Image *) NULL);
2729 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002730 assert(kernel != (KernelInfo *) NULL);
2731 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002732 assert(exception != (ExceptionInfo *) NULL);
2733 assert(exception->signature == MagickSignature);
2734
anthonyc3e48252010-05-24 12:43:11 +00002735 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002736 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002737 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002738
cristybb503372010-05-27 20:51:26 +00002739 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002740 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2741 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002742
cristye96405a2010-05-19 02:24:31 +00002743 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2744 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002745
anthony9eb4f742010-05-18 02:45:54 +00002746 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002747 curr_image = (Image *) image;
2748 work_image = save_image = rslt_image = (Image *) NULL;
2749 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002750
anthony47f5d062010-05-23 07:47:50 +00002751 /* Initialize specific methods
2752 * + which loop should use the given iteratations
2753 * + how many primatives make up the compound morphology
2754 * + multi-kernel compose method to use (by default)
2755 */
2756 method_limit = 1; /* just do method once, unless otherwise set */
2757 stage_limit = 1; /* assume method is not a compount */
2758 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002759 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002760 case SmoothMorphology: /* 4 primative compound morphology */
2761 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002762 break;
anthony47f5d062010-05-23 07:47:50 +00002763 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002764 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002765 case TopHatMorphology:
2766 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002767 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002768 case BottomHatMorphology:
2769 case EdgeMorphology:
2770 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002771 break;
2772 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002773 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002774 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002775 break;
anthonyc3e48252010-05-24 12:43:11 +00002776 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002777 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002778 case DistanceMorphology:
2779 method_limit = kernel_limit; /* iterate method with each kernel */
2780 kernel_limit = 1; /* do not do kernel iteration */
2781 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002782 break;
2783 default:
anthony930be612010-02-08 04:26:15 +00002784 break;
anthony602ab9b2010-01-05 08:06:50 +00002785 }
2786
anthonyc3e48252010-05-24 12:43:11 +00002787 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002788 if ( compose != UndefinedCompositeOp )
2789 rslt_compose = compose; /* override default composition for method */
2790 if ( rslt_compose == UndefinedCompositeOp )
2791 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2792
anthonyc3e48252010-05-24 12:43:11 +00002793 /* Some methods require a reflected kernel to use with primatives.
2794 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002795 switch ( method ) {
2796 case CorrelateMorphology:
2797 case CloseMorphology:
2798 case CloseIntensityMorphology:
2799 case BottomHatMorphology:
2800 case SmoothMorphology:
2801 reflected_kernel = CloneKernelInfo(kernel);
2802 if (reflected_kernel == (KernelInfo *) NULL)
2803 goto error_cleanup;
2804 RotateKernelInfo(reflected_kernel,180);
2805 break;
2806 default:
2807 break;
anthony9eb4f742010-05-18 02:45:54 +00002808 }
anthony7a01dcf2010-05-11 12:25:52 +00002809
anthony47f5d062010-05-23 07:47:50 +00002810 /* Loop 1: iterate the compound method */
2811 method_loop = 0;
2812 method_changed = 1;
2813 while ( method_loop < method_limit && method_changed > 0 ) {
2814 method_loop++;
2815 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002816
anthony47f5d062010-05-23 07:47:50 +00002817 /* Loop 2: iterate over each kernel in a multi-kernel list */
2818 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00002819 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00002820 rflt_kernel = reflected_kernel;
2821 kernel_number = 0;
2822 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002823
anthony47f5d062010-05-23 07:47:50 +00002824 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2825 stage_loop = 0; /* the compound morphology stage number */
2826 while ( stage_loop < stage_limit ) {
2827 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002828
anthony47f5d062010-05-23 07:47:50 +00002829 /* Select primative morphology for this stage of compound method */
2830 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002831 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002832 switch( method ) {
2833 case ErodeMorphology: /* just erode */
2834 case EdgeInMorphology: /* erode and image difference */
2835 primative = ErodeMorphology;
2836 break;
2837 case DilateMorphology: /* just dilate */
2838 case EdgeOutMorphology: /* dilate and image difference */
2839 primative = DilateMorphology;
2840 break;
2841 case OpenMorphology: /* erode then dialate */
2842 case TopHatMorphology: /* open and image difference */
2843 primative = ErodeMorphology;
2844 if ( stage_loop == 2 )
2845 primative = DilateMorphology;
2846 break;
2847 case OpenIntensityMorphology:
2848 primative = ErodeIntensityMorphology;
2849 if ( stage_loop == 2 )
2850 primative = DilateIntensityMorphology;
2851 case CloseMorphology: /* dilate, then erode */
2852 case BottomHatMorphology: /* close and image difference */
2853 this_kernel = rflt_kernel; /* use the reflected kernel */
2854 primative = DilateMorphology;
2855 if ( stage_loop == 2 )
2856 primative = ErodeMorphology;
2857 break;
2858 case CloseIntensityMorphology:
2859 this_kernel = rflt_kernel; /* use the reflected kernel */
2860 primative = DilateIntensityMorphology;
2861 if ( stage_loop == 2 )
2862 primative = ErodeIntensityMorphology;
2863 break;
2864 case SmoothMorphology: /* open, close */
2865 switch ( stage_loop ) {
2866 case 1: /* start an open method, which starts with Erode */
2867 primative = ErodeMorphology;
2868 break;
2869 case 2: /* now Dilate the Erode */
2870 primative = DilateMorphology;
2871 break;
2872 case 3: /* Reflect kernel a close */
2873 this_kernel = rflt_kernel; /* use the reflected kernel */
2874 primative = DilateMorphology;
2875 break;
2876 case 4: /* Finish the Close */
2877 this_kernel = rflt_kernel; /* use the reflected kernel */
2878 primative = ErodeMorphology;
2879 break;
2880 }
2881 break;
2882 case EdgeMorphology: /* dilate and erode difference */
2883 primative = DilateMorphology;
2884 if ( stage_loop == 2 ) {
2885 save_image = curr_image; /* save the image difference */
2886 curr_image = (Image *) image;
2887 primative = ErodeMorphology;
2888 }
2889 break;
2890 case CorrelateMorphology:
2891 /* A Correlation is a Convolution with a reflected kernel.
2892 ** However a Convolution is a weighted sum using a reflected
2893 ** kernel. It may seem stange to convert a Correlation into a
2894 ** Convolution as the Correlation is the simplier method, but
2895 ** Convolution is much more commonly used, and it makes sense to
2896 ** implement it directly so as to avoid the need to duplicate the
2897 ** kernel when it is not required (which is typically the
2898 ** default).
2899 */
2900 this_kernel = rflt_kernel; /* use the reflected kernel */
2901 primative = ConvolveMorphology;
2902 break;
2903 default:
anthony47f5d062010-05-23 07:47:50 +00002904 break;
2905 }
anthony9eb4f742010-05-18 02:45:54 +00002906
anthony47f5d062010-05-23 07:47:50 +00002907 /* Extra information for debugging compound operations */
2908 if ( verbose == MagickTrue ) {
2909 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002910 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002911 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2912 (unsigned long) method_loop,(unsigned long) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00002913 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002914 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
cristyf2faecf2010-05-28 19:19:36 +00002915 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2916 (unsigned long) method_loop);
anthony47f5d062010-05-23 07:47:50 +00002917 else
2918 v_info[0] = '\0';
2919 }
2920
2921 /* Loop 4: Iterate the kernel with primative */
2922 kernel_loop = 0;
2923 kernel_changed = 0;
2924 changed = 1;
2925 while ( kernel_loop < kernel_limit && changed > 0 ) {
2926 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002927
2928 /* Create a destination image, if not yet defined */
2929 if ( work_image == (Image *) NULL )
2930 {
2931 work_image=CloneImage(image,0,0,MagickTrue,exception);
2932 if (work_image == (Image *) NULL)
2933 goto error_cleanup;
2934 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2935 {
2936 InheritException(exception,&work_image->exception);
2937 goto error_cleanup;
2938 }
2939 }
2940
anthony47f5d062010-05-23 07:47:50 +00002941 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002942 count++;
anthony47f5d062010-05-23 07:47:50 +00002943 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002944 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002945 kernel_changed += changed;
2946 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002947
anthony47f5d062010-05-23 07:47:50 +00002948 if ( verbose == MagickTrue ) {
2949 if ( kernel_loop > 1 )
2950 fprintf(stderr, "\n"); /* add end-of-line from previous */
2951 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2952 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2953 ( this_kernel == rflt_kernel ) ? "*" : "",
cristyf2faecf2010-05-28 19:19:36 +00002954 (unsigned long) method_loop+kernel_loop-1,(unsigned long)
2955 kernel_number,(unsigned long) count,(unsigned long) changed);
anthony47f5d062010-05-23 07:47:50 +00002956 }
anthony9eb4f742010-05-18 02:45:54 +00002957 /* prepare next loop */
2958 { Image *tmp = work_image; /* swap images for iteration */
2959 work_image = curr_image;
2960 curr_image = tmp;
2961 }
2962 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002963 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002964
anthony47f5d062010-05-23 07:47:50 +00002965 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002966
anthony47f5d062010-05-23 07:47:50 +00002967 if ( verbose == MagickTrue && kernel_changed != changed )
cristyf2faecf2010-05-28 19:19:36 +00002968 fprintf(stderr, " Total %lu",(unsigned long) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00002969 if ( verbose == MagickTrue && stage_loop < stage_limit )
2970 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002971
2972#if 0
cristybb503372010-05-27 20:51:26 +00002973 fprintf(stderr, "--E-- image=0x%lx\n", (size_t)image);
2974 fprintf(stderr, " curr =0x%lx\n", (size_t)curr_image);
2975 fprintf(stderr, " work =0x%lx\n", (size_t)work_image);
2976 fprintf(stderr, " save =0x%lx\n", (size_t)save_image);
2977 fprintf(stderr, " union=0x%lx\n", (size_t)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002978#endif
2979
anthony47f5d062010-05-23 07:47:50 +00002980 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002981
anthony47f5d062010-05-23 07:47:50 +00002982 /* Final Post-processing for some Compound Methods
2983 **
2984 ** The removal of any 'Sync' channel flag in the Image Compositon
2985 ** below ensures the methematical compose method is applied in a
2986 ** purely mathematical way, and only to the selected channels.
2987 ** Turn off SVG composition 'alpha blending'.
2988 */
2989 switch( method ) {
2990 case EdgeOutMorphology:
2991 case EdgeInMorphology:
2992 case TopHatMorphology:
2993 case BottomHatMorphology:
2994 if ( verbose == MagickTrue )
2995 fprintf(stderr, "\n%s: Difference with original image",
2996 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2997 (void) CompositeImageChannel(curr_image,
2998 (ChannelType) (channel & ~SyncChannels),
2999 DifferenceCompositeOp, image, 0, 0);
3000 break;
3001 case EdgeMorphology:
3002 if ( verbose == MagickTrue )
3003 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3004 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3005 (void) CompositeImageChannel(curr_image,
3006 (ChannelType) (channel & ~SyncChannels),
3007 DifferenceCompositeOp, save_image, 0, 0);
3008 save_image = DestroyImage(save_image); /* finished with save image */
3009 break;
3010 default:
3011 break;
3012 }
3013
3014 /* multi-kernel handling: re-iterate, or compose results */
3015 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003016 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003017 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003018 { if ( verbose == MagickTrue ) {
3019 if ( this_kernel->next != (KernelInfo *) NULL )
3020 fprintf(stderr, " (re-iterate)");
3021 else
3022 fprintf(stderr, " (done)");
3023 }
3024 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003025 }
anthony47f5d062010-05-23 07:47:50 +00003026 else if ( rslt_image == (Image *) NULL)
3027 { if ( verbose == MagickTrue )
3028 fprintf(stderr, " (save for compose)");
3029 rslt_image = curr_image;
3030 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003031 }
anthony47f5d062010-05-23 07:47:50 +00003032 else
3033 { /* add the new 'current' result to the composition
3034 **
3035 ** The removal of any 'Sync' channel flag in the Image Compositon
3036 ** below ensures the methematical compose method is applied in a
3037 ** purely mathematical way, and only to the selected channels.
3038 ** Turn off SVG composition 'alpha blending'.
3039 */
3040 if ( verbose == MagickTrue )
3041 fprintf(stderr, " (compose \"%s\")",
3042 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3043 (void) CompositeImageChannel(rslt_image,
3044 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3045 curr_image, 0, 0);
3046 curr_image = (Image *) image; /* continue with original image */
3047 }
3048 if ( verbose == MagickTrue )
3049 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003050
anthony47f5d062010-05-23 07:47:50 +00003051 /* loop to the next kernel in a multi-kernel list */
3052 norm_kernel = norm_kernel->next;
3053 if ( rflt_kernel != (KernelInfo *) NULL )
3054 rflt_kernel = rflt_kernel->next;
3055 kernel_number++;
3056 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003057
anthony47f5d062010-05-23 07:47:50 +00003058 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003059
anthony9eb4f742010-05-18 02:45:54 +00003060 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003061
anthony47f5d062010-05-23 07:47:50 +00003062 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003063error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003064 if ( curr_image != (Image *) NULL &&
3065 curr_image != rslt_image &&
3066 curr_image != image )
3067 curr_image = DestroyImage(curr_image);
3068 if ( rslt_image != (Image *) NULL )
3069 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003070exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003071 if ( curr_image != (Image *) NULL &&
3072 curr_image != rslt_image &&
3073 curr_image != image )
3074 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003075 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003076 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003077 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003078 save_image = DestroyImage(save_image);
3079 if ( reflected_kernel != (KernelInfo *) NULL )
3080 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3081 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003082}
3083
3084/*
3085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3086% %
3087% %
3088% %
3089% M o r p h o l o g y I m a g e C h a n n e l %
3090% %
3091% %
3092% %
3093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3094%
3095% MorphologyImageChannel() applies a user supplied kernel to the image
3096% according to the given mophology method.
3097%
3098% This function applies any and all user defined settings before calling
3099% the above internal function MorphologyApply().
3100%
3101% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003102% * Output Bias for Convolution and correlation ("-bias")
3103% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3104% This can also includes the addition of a scaled unity kernel.
3105% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003106%
3107% The format of the MorphologyImage method is:
3108%
3109% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003110% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003111%
3112% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003113% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003114% KernelInfo *kernel,ExceptionInfo *exception)
3115%
3116% A description of each parameter follows:
3117%
3118% o image: the image.
3119%
3120% o method: the morphology method to be applied.
3121%
3122% o iterations: apply the operation this many times (or no change).
3123% A value of -1 means loop until no change found.
3124% How this is applied may depend on the morphology method.
3125% Typically this is a value of 1.
3126%
3127% o channel: the channel type.
3128%
3129% o kernel: An array of double representing the morphology kernel.
3130% Warning: kernel may be normalized for the Convolve method.
3131%
3132% o exception: return any errors or warnings in this structure.
3133%
3134*/
3135
3136MagickExport Image *MorphologyImageChannel(const Image *image,
3137 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003138 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003139{
3140 const char
3141 *artifact;
3142
3143 KernelInfo
3144 *curr_kernel;
3145
anthony47f5d062010-05-23 07:47:50 +00003146 CompositeOperator
3147 compose;
3148
anthony9eb4f742010-05-18 02:45:54 +00003149 Image
3150 *morphology_image;
3151
3152
anthony46a369d2010-05-19 02:41:48 +00003153 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3154 * This is done BEFORE the ShowKernelInfo() function is called so that
3155 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003156 */
3157 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003158 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003159 {
3160 artifact = GetImageArtifact(image,"convolve:scale");
3161 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003162 if ( curr_kernel == kernel )
3163 curr_kernel = CloneKernelInfo(kernel);
3164 if (curr_kernel == (KernelInfo *) NULL) {
3165 curr_kernel=DestroyKernelInfo(curr_kernel);
3166 return((Image *) NULL);
3167 }
anthony46a369d2010-05-19 02:41:48 +00003168 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003169 }
3170 }
3171
3172 /* display the (normalized) kernel via stderr */
3173 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003174 if ( artifact == (const char *) NULL)
3175 artifact = GetImageArtifact(image,"convolve:showkernel");
3176 if ( artifact == (const char *) NULL)
3177 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003178 if ( artifact != (const char *) NULL)
3179 ShowKernelInfo(curr_kernel);
3180
anthony47f5d062010-05-23 07:47:50 +00003181 /* override the default handling of multi-kernel morphology results
3182 * if 'Undefined' use the default method
3183 * if 'None' (default for 'Convolve') re-iterate previous result
3184 * otherwise merge resulting images using compose method given
3185 */
3186 compose = UndefinedCompositeOp; /* use default for method */
3187 artifact = GetImageArtifact(image,"morphology:compose");
3188 if ( artifact != (const char *) NULL)
3189 compose = (CompositeOperator) ParseMagickOption(
3190 MagickComposeOptions,MagickFalse,artifact);
3191
anthony9eb4f742010-05-18 02:45:54 +00003192 /* Apply the Morphology */
3193 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003194 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003195
3196 /* Cleanup and Exit */
3197 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003198 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003199 return(morphology_image);
3200}
3201
3202MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003203 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003204 *exception)
3205{
3206 Image
3207 *morphology_image;
3208
3209 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3210 iterations,kernel,exception);
3211 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003212}
anthony83ba99b2010-01-24 08:48:15 +00003213
3214/*
3215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3216% %
3217% %
3218% %
anthony4fd27e22010-02-07 08:17:18 +00003219+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003220% %
3221% %
3222% %
3223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3224%
anthony46a369d2010-05-19 02:41:48 +00003225% RotateKernelInfo() rotates the kernel by the angle given.
3226%
3227% Currently it is restricted to 90 degree angles, of either 1D kernels
3228% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3229% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003230%
anthony4fd27e22010-02-07 08:17:18 +00003231% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003232%
anthony4fd27e22010-02-07 08:17:18 +00003233% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003234%
3235% A description of each parameter follows:
3236%
3237% o kernel: the Morphology/Convolution kernel
3238%
3239% o angle: angle to rotate in degrees
3240%
anthony46a369d2010-05-19 02:41:48 +00003241% This function is currently internal to this module only, but can be exported
3242% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003243*/
anthony4fd27e22010-02-07 08:17:18 +00003244static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003245{
anthony1b2bc0a2010-05-12 05:25:22 +00003246 /* angle the lower kernels first */
3247 if ( kernel->next != (KernelInfo *) NULL)
3248 RotateKernelInfo(kernel->next, angle);
3249
anthony83ba99b2010-01-24 08:48:15 +00003250 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3251 **
3252 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3253 */
3254
3255 /* Modulus the angle */
3256 angle = fmod(angle, 360.0);
3257 if ( angle < 0 )
3258 angle += 360.0;
3259
anthony3c10fc82010-05-13 02:40:51 +00003260 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003261 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003262
anthony3dd0f622010-05-13 12:57:32 +00003263 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003264 switch (kernel->type) {
3265 /* These built-in kernels are cylindrical kernels, rotating is useless */
3266 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003267 case DOGKernel:
3268 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003269 case PeaksKernel:
3270 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003271 case ChebyshevKernel:
3272 case ManhattenKernel:
3273 case EuclideanKernel:
3274 return;
3275
3276 /* These may be rotatable at non-90 angles in the future */
3277 /* but simply rotating them in multiples of 90 degrees is useless */
3278 case SquareKernel:
3279 case DiamondKernel:
3280 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003281 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003282 return;
3283
3284 /* These only allows a +/-90 degree rotation (by transpose) */
3285 /* A 180 degree rotation is useless */
3286 case BlurKernel:
3287 case RectangleKernel:
3288 if ( 135.0 < angle && angle <= 225.0 )
3289 return;
3290 if ( 225.0 < angle && angle <= 315.0 )
3291 angle -= 180;
3292 break;
3293
anthony3dd0f622010-05-13 12:57:32 +00003294 default:
anthony83ba99b2010-01-24 08:48:15 +00003295 break;
3296 }
anthony3c10fc82010-05-13 02:40:51 +00003297 /* Attempt rotations by 45 degrees */
3298 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3299 {
3300 if ( kernel->width == 3 && kernel->height == 3 )
3301 { /* Rotate a 3x3 square by 45 degree angle */
3302 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003303 kernel->values[0] = kernel->values[3];
3304 kernel->values[3] = kernel->values[6];
3305 kernel->values[6] = kernel->values[7];
3306 kernel->values[7] = kernel->values[8];
3307 kernel->values[8] = kernel->values[5];
3308 kernel->values[5] = kernel->values[2];
3309 kernel->values[2] = kernel->values[1];
3310 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003311 /* rotate non-centered origin */
3312 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003313 ssize_t x,y;
3314 x = (ssize_t) kernel->x-1;
3315 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003316 if ( x == y ) x = 0;
3317 else if ( x == 0 ) x = -y;
3318 else if ( x == -y ) y = 0;
3319 else if ( y == 0 ) y = x;
cristybb503372010-05-27 20:51:26 +00003320 kernel->x = (size_t) x+1;
3321 kernel->y = (size_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003322 }
anthony43c49252010-05-18 10:59:50 +00003323 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3324 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003325 }
3326 else
3327 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3328 }
3329 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3330 {
3331 if ( kernel->width == 1 || kernel->height == 1 )
3332 { /* Do a transpose of the image, which results in a 90
3333 ** degree rotation of a 1 dimentional kernel
3334 */
cristybb503372010-05-27 20:51:26 +00003335 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003336 t;
cristybb503372010-05-27 20:51:26 +00003337 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003338 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003339 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003340 t = kernel->x;
3341 kernel->x = kernel->y;
3342 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003343 if ( kernel->width == 1 ) {
3344 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3345 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3346 } else {
3347 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3348 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3349 }
anthony3c10fc82010-05-13 02:40:51 +00003350 }
3351 else if ( kernel->width == kernel->height )
3352 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003353 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003354 i,j,x,y;
3355 register MagickRealType
3356 *k,t;
3357 k=kernel->values;
3358 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3359 for( j=0, y=kernel->height-1; j<y; j++, y--)
3360 { t = k[i+j*kernel->width];
3361 k[i+j*kernel->width] = k[j+x*kernel->width];
3362 k[j+x*kernel->width] = k[x+y*kernel->width];
3363 k[x+y*kernel->width] = k[y+i*kernel->width];
3364 k[y+i*kernel->width] = t;
3365 }
3366 }
3367 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003368 { register ssize_t x,y;
3369 x = (ssize_t) kernel->x*2-kernel->width+1;
3370 y = (ssize_t) kernel->y*2-kernel->height+1;
3371 kernel->x = (size_t) ( -y +kernel->width-1)/2;
3372 kernel->y = (size_t) ( +x +kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003373 }
anthony43c49252010-05-18 10:59:50 +00003374 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3375 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003376 }
3377 else
3378 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3379 }
anthony83ba99b2010-01-24 08:48:15 +00003380 if ( 135.0 < angle && angle <= 225.0 )
3381 {
anthony43c49252010-05-18 10:59:50 +00003382 /* For a 180 degree rotation - also know as a reflection
3383 * This is actually a very very common operation!
3384 * Basically all that is needed is a reversal of the kernel data!
3385 * And a reflection of the origon
3386 */
cristybb503372010-05-27 20:51:26 +00003387 size_t
anthony83ba99b2010-01-24 08:48:15 +00003388 i,j;
3389 register double
3390 *k,t;
3391
3392 k=kernel->values;
3393 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3394 t=k[i], k[i]=k[j], k[j]=t;
3395
cristybb503372010-05-27 20:51:26 +00003396 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3397 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003398 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3399 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003400 }
anthony3c10fc82010-05-13 02:40:51 +00003401 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003402 * In the future some form of non-orthogonal angled rotates could be
3403 * performed here, posibily with a linear kernel restriction.
3404 */
3405
3406#if 0
anthony3c10fc82010-05-13 02:40:51 +00003407 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003408 */
cristybb503372010-05-27 20:51:26 +00003409 size_t
anthony83ba99b2010-01-24 08:48:15 +00003410 y;
cristybb503372010-05-27 20:51:26 +00003411 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003412 x,r;
3413 register double
3414 *k,t;
3415
3416 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3417 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3418 t=k[x], k[x]=k[r], k[r]=t;
3419
cristyc99304f2010-02-01 15:26:27 +00003420 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003421 angle = fmod(angle+180.0, 360.0);
3422 }
3423#endif
3424 return;
3425}
3426
3427/*
3428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3429% %
3430% %
3431% %
anthony46a369d2010-05-19 02:41:48 +00003432% S c a l e G e o m e t r y K e r n e l I n f o %
3433% %
3434% %
3435% %
3436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3437%
3438% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3439% provided as a "-set option:convolve:scale {geometry}" user setting,
3440% and modifies the kernel according to the parsed arguments of that setting.
3441%
3442% The first argument (and any normalization flags) are passed to
3443% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3444% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3445% into the scaled/normalized kernel.
3446%
3447% The format of the ScaleKernelInfo method is:
3448%
3449% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3450% const MagickStatusType normalize_flags )
3451%
3452% A description of each parameter follows:
3453%
3454% o kernel: the Morphology/Convolution kernel to modify
3455%
3456% o geometry:
3457% The geometry string to parse, typically from the user provided
3458% "-set option:convolve:scale {geometry}" setting.
3459%
3460*/
3461MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3462 const char *geometry)
3463{
3464 GeometryFlags
3465 flags;
3466 GeometryInfo
3467 args;
3468
3469 SetGeometryInfo(&args);
3470 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3471
3472#if 0
3473 /* For Debugging Geometry Input */
3474 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3475 flags, args.rho, args.sigma, args.xi, args.psi );
3476#endif
3477
3478 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3479 args.rho *= 0.01, args.sigma *= 0.01;
3480
3481 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3482 args.rho = 1.0;
3483 if ( (flags & SigmaValue) == 0 )
3484 args.sigma = 0.0;
3485
3486 /* Scale/Normalize the input kernel */
3487 ScaleKernelInfo(kernel, args.rho, flags);
3488
3489 /* Add Unity Kernel, for blending with original */
3490 if ( (flags & SigmaValue) != 0 )
3491 UnityAddKernelInfo(kernel, args.sigma);
3492
3493 return;
3494}
3495/*
3496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3497% %
3498% %
3499% %
cristy6771f1e2010-03-05 19:43:39 +00003500% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003501% %
3502% %
3503% %
3504%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3505%
anthony1b2bc0a2010-05-12 05:25:22 +00003506% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3507% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003508%
anthony999bb2c2010-02-18 12:38:01 +00003509% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003510% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003511%
anthony46a369d2010-05-19 02:41:48 +00003512% If either of the two 'normalize_flags' are given the kernel will first be
3513% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003514%
3515% Kernel normalization ('normalize_flags' given) is designed to ensure that
3516% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003517% morphology methods will fall into -1.0 to +1.0 range. Note that for
3518% non-HDRI versions of IM this may cause images to have any negative results
3519% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003520%
3521% More specifically. Kernels which only contain positive values (such as a
3522% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003523% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003524%
3525% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3526% the kernel will be scaled by the absolute of the sum of kernel values, so
3527% that it will generally fall within the +/- 1.0 range.
3528%
3529% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3530% will be scaled by just the sum of the postive values, so that its output
3531% range will again fall into the +/- 1.0 range.
3532%
3533% For special kernels designed for locating shapes using 'Correlate', (often
3534% only containing +1 and -1 values, representing foreground/brackground
3535% matching) a special normalization method is provided to scale the positive
3536% values seperatally to those of the negative values, so the kernel will be
3537% forced to become a zero-sum kernel better suited to such searches.
3538%
anthony1b2bc0a2010-05-12 05:25:22 +00003539% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003540% attributes within the kernel structure have been correctly set during the
3541% kernels creation.
3542%
3543% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003544% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3545% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003546%
anthony4fd27e22010-02-07 08:17:18 +00003547% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003548%
anthony999bb2c2010-02-18 12:38:01 +00003549% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3550% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003551%
3552% A description of each parameter follows:
3553%
3554% o kernel: the Morphology/Convolution kernel
3555%
anthony999bb2c2010-02-18 12:38:01 +00003556% o scaling_factor:
3557% multiply all values (after normalization) by this factor if not
3558% zero. If the kernel is normalized regardless of any flags.
3559%
3560% o normalize_flags:
3561% GeometryFlags defining normalization method to use.
3562% specifically: NormalizeValue, CorrelateNormalizeValue,
3563% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003564%
3565*/
cristy6771f1e2010-03-05 19:43:39 +00003566MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3567 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003568{
cristybb503372010-05-27 20:51:26 +00003569 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003570 i;
3571
anthony999bb2c2010-02-18 12:38:01 +00003572 register double
3573 pos_scale,
3574 neg_scale;
3575
anthony46a369d2010-05-19 02:41:48 +00003576 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003577 if ( kernel->next != (KernelInfo *) NULL)
3578 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3579
anthony46a369d2010-05-19 02:41:48 +00003580 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003581 pos_scale = 1.0;
3582 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003583 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003584 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003585 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003586 else
anthonyf4e00312010-05-20 12:06:35 +00003587 /* zero-summing kernel */
3588 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003589 }
anthony46a369d2010-05-19 02:41:48 +00003590 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003591 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3592 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3593 ? kernel->positive_range : 1.0;
3594 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3595 ? -kernel->negative_range : 1.0;
3596 }
3597 else
3598 neg_scale = pos_scale;
3599
3600 /* finialize scaling_factor for positive and negative components */
3601 pos_scale = scaling_factor/pos_scale;
3602 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003603
cristybb503372010-05-27 20:51:26 +00003604 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003605 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003606 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003607
anthony999bb2c2010-02-18 12:38:01 +00003608 /* convolution output range */
3609 kernel->positive_range *= pos_scale;
3610 kernel->negative_range *= neg_scale;
3611 /* maximum and minimum values in kernel */
3612 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3613 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3614
anthony46a369d2010-05-19 02:41:48 +00003615 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003616 if ( scaling_factor < MagickEpsilon ) {
3617 double t;
3618 t = kernel->positive_range;
3619 kernel->positive_range = kernel->negative_range;
3620 kernel->negative_range = t;
3621 t = kernel->maximum;
3622 kernel->maximum = kernel->minimum;
3623 kernel->minimum = 1;
3624 }
anthonycc6c8362010-01-25 04:14:01 +00003625
3626 return;
3627}
3628
3629/*
3630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3631% %
3632% %
3633% %
anthony46a369d2010-05-19 02:41:48 +00003634% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003635% %
3636% %
3637% %
3638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3639%
anthony4fd27e22010-02-07 08:17:18 +00003640% ShowKernelInfo() outputs the details of the given kernel defination to
3641% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003642%
3643% The format of the ShowKernel method is:
3644%
anthony4fd27e22010-02-07 08:17:18 +00003645% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003646%
3647% A description of each parameter follows:
3648%
3649% o kernel: the Morphology/Convolution kernel
3650%
anthony83ba99b2010-01-24 08:48:15 +00003651*/
anthony4fd27e22010-02-07 08:17:18 +00003652MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003653{
anthony7a01dcf2010-05-11 12:25:52 +00003654 KernelInfo
3655 *k;
anthony83ba99b2010-01-24 08:48:15 +00003656
cristybb503372010-05-27 20:51:26 +00003657 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003658 c, i, u, v;
3659
3660 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3661
anthony46a369d2010-05-19 02:41:48 +00003662 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003663 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003664 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003665 fprintf(stderr, " \"%s",
3666 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3667 if ( fabs(k->angle) > MagickEpsilon )
3668 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003669 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3670 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003671 fprintf(stderr,
3672 " with values from %.*lg to %.*lg\n",
3673 GetMagickPrecision(), k->minimum,
3674 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003675 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003676 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003677 GetMagickPrecision(), k->positive_range);
3678 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3679 fprintf(stderr, " (Zero-Summing)\n");
3680 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3681 fprintf(stderr, " (Normalized)\n");
3682 else
3683 fprintf(stderr, " (Sum %.*lg)\n",
3684 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003685 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003686 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003687 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003688 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003689 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003690 else
anthonyf4e00312010-05-20 12:06:35 +00003691 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003692 GetMagickPrecision(), k->values[i]);
3693 fprintf(stderr,"\n");
3694 }
anthony83ba99b2010-01-24 08:48:15 +00003695 }
3696}
anthonycc6c8362010-01-25 04:14:01 +00003697
3698/*
3699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3700% %
3701% %
3702% %
anthony43c49252010-05-18 10:59:50 +00003703% U n i t y A d d K e r n a l I n f o %
3704% %
3705% %
3706% %
3707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3708%
3709% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3710% to the given pre-scaled and normalized Kernel. This in effect adds that
3711% amount of the original image into the resulting convolution kernel. This
3712% value is usually provided by the user as a percentage value in the
3713% 'convolve:scale' setting.
3714%
3715% The resulting effect is to either convert a 'zero-summing' edge detection
3716% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3717% kernel.
3718%
3719% Alternativally by using a purely positive kernel, and using a negative
3720% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3721% as a "Gaussian") into a 'unsharp' kernel.
3722%
anthony46a369d2010-05-19 02:41:48 +00003723% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003724%
3725% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3726%
3727% A description of each parameter follows:
3728%
3729% o kernel: the Morphology/Convolution kernel
3730%
3731% o scale:
3732% scaling factor for the unity kernel to be added to
3733% the given kernel.
3734%
anthony43c49252010-05-18 10:59:50 +00003735*/
3736MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3737 const double scale)
3738{
anthony46a369d2010-05-19 02:41:48 +00003739 /* do the other kernels in a multi-kernel list first */
3740 if ( kernel->next != (KernelInfo *) NULL)
3741 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003742
anthony46a369d2010-05-19 02:41:48 +00003743 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003744 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003745 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003746
3747 return;
3748}
3749
3750/*
3751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3752% %
3753% %
3754% %
3755% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003756% %
3757% %
3758% %
3759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3760%
3761% ZeroKernelNans() replaces any special 'nan' value that may be present in
3762% the kernel with a zero value. This is typically done when the kernel will
3763% be used in special hardware (GPU) convolution processors, to simply
3764% matters.
3765%
3766% The format of the ZeroKernelNans method is:
3767%
anthony46a369d2010-05-19 02:41:48 +00003768% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003769%
3770% A description of each parameter follows:
3771%
3772% o kernel: the Morphology/Convolution kernel
3773%
anthonycc6c8362010-01-25 04:14:01 +00003774*/
anthonyc4c86e02010-01-27 09:30:32 +00003775MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003776{
cristybb503372010-05-27 20:51:26 +00003777 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003778 i;
3779
anthony46a369d2010-05-19 02:41:48 +00003780 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003781 if ( kernel->next != (KernelInfo *) NULL)
3782 ZeroKernelNans(kernel->next);
3783
anthony43c49252010-05-18 10:59:50 +00003784 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003785 if ( IsNan(kernel->values[i]) )
3786 kernel->values[i] = 0.0;
3787
3788 return;
3789}