blob: fa0739e34840839838113b28caac4cc849a10bd9 [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 ) {
502 fprintf(stderr, "Failed to parse kernel number #%lu\n", kernel_number);
503 if ( kernel != (KernelInfo *) NULL )
504 kernel=DestroyKernelInfo(kernel);
505 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000506 }
anthonye108a3f2010-05-12 07:24:03 +0000507
508 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000509 if ( kernel == (KernelInfo *) NULL )
510 kernel = new_kernel;
511 else
anthony43c49252010-05-18 10:59:50 +0000512 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000513 }
514
515 /* look for the next kernel in list */
516 p = strchr(p, ';');
517 if ( p == (char *) NULL )
518 break;
519 p++;
520
521 }
anthony7a01dcf2010-05-11 12:25:52 +0000522 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000523}
524
anthony602ab9b2010-01-05 08:06:50 +0000525
526/*
527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528% %
529% %
530% %
531% A c q u i r e K e r n e l B u i l t I n %
532% %
533% %
534% %
535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536%
537% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
538% kernels used for special purposes such as gaussian blurring, skeleton
539% pruning, and edge distance determination.
540%
541% They take a KernelType, and a set of geometry style arguments, which were
542% typically decoded from a user supplied string, or from a more complex
543% Morphology Method that was requested.
544%
545% The format of the AcquireKernalBuiltIn method is:
546%
cristy2be15382010-01-21 02:38:03 +0000547% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000548% const GeometryInfo args)
549%
550% A description of each parameter follows:
551%
552% o type: the pre-defined type of kernel wanted
553%
554% o args: arguments defining or modifying the kernel
555%
556% Convolution Kernels
557%
anthony46a369d2010-05-19 02:41:48 +0000558% Unity
559% the No-Op kernel, also requivelent to Gaussian of sigma zero.
560% Basically a 3x3 kernel of a 1 surrounded by zeros.
561%
anthony3c10fc82010-05-13 02:40:51 +0000562% Gaussian:{radius},{sigma}
563% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000564% The sigma for the curve is required. The resulting kernel is
565% normalized,
566%
567% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000568%
569% NOTE: that the 'radius' is optional, but if provided can limit (clip)
570% the final size of the resulting kernel to a square 2*radius+1 in size.
571% The radius should be at least 2 times that of the sigma value, or
572% sever clipping and aliasing may result. If not given or set to 0 the
573% radius will be determined so as to produce the best minimal error
574% result, which is usally much larger than is normally needed.
575%
anthonyc1061722010-05-14 06:23:49 +0000576% DOG:{radius},{sigma1},{sigma2}
577% "Difference of Gaussians" Kernel.
578% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
579% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
580% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000581%
anthony9eb4f742010-05-18 02:45:54 +0000582% LOG:{radius},{sigma}
583% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
584% The supposed ideal edge detection, zero-summing kernel.
585%
586% An alturnative to this kernel is to use a "DOG" with a sigma ratio of
587% approx 1.6, which can also be applied as a 2 pass "DOB" (see below).
588%
anthonyc1061722010-05-14 06:23:49 +0000589% Blur:{radius},{sigma}[,{angle}]
590% Generates a 1 dimensional or linear gaussian blur, at the angle given
591% (current restricted to orthogonal angles). If a 'radius' is given the
592% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
593% by a 90 degree angle.
594%
595% If 'sigma' is zero, you get a single pixel on a field of zeros.
596%
597% Note that two convolutions with two "Blur" kernels perpendicular to
598% each other, is equivelent to a far larger "Gaussian" kernel with the
599% same sigma value, However it is much faster to apply. This is how the
600% "-blur" operator actually works.
601%
602% DOB:{radius},{sigma1},{sigma2}[,{angle}]
603% "Difference of Blurs" Kernel.
604% As "Blur" but with the 1D gaussian produced by 'sigma2' subtracted
605% from thethe 1D gaussian produced by 'sigma1'.
606% The result is a zero-summing kernel.
607%
608% This can be used to generate a faster "DOG" convolution, in the same
609% way "Blur" can.
anthony602ab9b2010-01-05 08:06:50 +0000610%
anthony3c10fc82010-05-13 02:40:51 +0000611% Comet:{width},{sigma},{angle}
612% Blur in one direction only, much like how a bright object leaves
anthony602ab9b2010-01-05 08:06:50 +0000613% a comet like trail. The Kernel is actually half a gaussian curve,
anthony3c10fc82010-05-13 02:40:51 +0000614% Adding two such blurs in opposite directions produces a Blur Kernel.
615% Angle can be rotated in multiples of 90 degrees.
anthony602ab9b2010-01-05 08:06:50 +0000616%
anthony3c10fc82010-05-13 02:40:51 +0000617% Note that the first argument is the width of the kernel and not the
anthony602ab9b2010-01-05 08:06:50 +0000618% radius of the kernel.
619%
620% # Still to be implemented...
621% #
anthony4fd27e22010-02-07 08:17:18 +0000622% # Filter2D
623% # Filter1D
624% # Set kernel values using a resize filter, and given scale (sigma)
625% # Cylindrical or Linear. Is this posible with an image?
626% #
anthony602ab9b2010-01-05 08:06:50 +0000627%
anthony3c10fc82010-05-13 02:40:51 +0000628% Named Constant Convolution Kernels
629%
anthonyc1061722010-05-14 06:23:49 +0000630% All these are unscaled, zero-summing kernels by default. As such for
631% non-HDRI version of ImageMagick some form of normalization, user scaling,
632% and biasing the results is recommended, to prevent the resulting image
633% being 'clipped'.
634%
635% The 3x3 kernels (most of these) can be circularly rotated in multiples of
636% 45 degrees to generate the 8 angled varients of each of the kernels.
anthony3c10fc82010-05-13 02:40:51 +0000637%
638% Laplacian:{type}
anthony43c49252010-05-18 10:59:50 +0000639% Discrete Lapacian Kernels, (without normalization)
anthonyc1061722010-05-14 06:23:49 +0000640% Type 0 : 3x3 with center:8 surounded by -1 (8 neighbourhood)
641% Type 1 : 3x3 with center:4 edge:-1 corner:0 (4 neighbourhood)
anthony9eb4f742010-05-18 02:45:54 +0000642% Type 2 : 3x3 with center:4 edge:1 corner:-2
643% Type 3 : 3x3 with center:4 edge:-2 corner:1
644% Type 5 : 5x5 laplacian
645% Type 7 : 7x7 laplacian
anthony43c49252010-05-18 10:59:50 +0000646% Type 15 : 5x5 LOG (sigma approx 1.4)
647% Type 19 : 9x9 LOG (sigma approx 1.4)
anthonyc1061722010-05-14 06:23:49 +0000648%
649% Sobel:{angle}
anthony46a369d2010-05-19 02:41:48 +0000650% Sobel 'Edge' convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000651% -1, 0, 1
652% -2, 0,-2
653% -1, 0, 1
anthonye2a60ce2010-05-19 12:30:40 +0000654%
anthonyc1061722010-05-14 06:23:49 +0000655% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000656% Roberts convolution kernel (3x3)
anthonyc1061722010-05-14 06:23:49 +0000657% 0, 0, 0
658% -1, 1, 0
659% 0, 0, 0
anthonyc1061722010-05-14 06:23:49 +0000660% Prewitt:{angle}
661% Prewitt Edge convolution kernel (3x3)
662% -1, 0, 1
663% -1, 0, 1
664% -1, 0, 1
anthony9eb4f742010-05-18 02:45:54 +0000665% Compass:{angle}
666% Prewitt's "Compass" convolution kernel (3x3)
667% -1, 1, 1
668% -1,-2, 1
669% -1, 1, 1
670% Kirsch:{angle}
671% Kirsch's "Compass" convolution kernel (3x3)
672% -3,-3, 5
673% -3, 0, 5
674% -3,-3, 5
anthony3c10fc82010-05-13 02:40:51 +0000675%
anthonye2a60ce2010-05-19 12:30:40 +0000676% FreiChen:{type},{angle}
677% Frei-Chen Edge Detector is a set of 9 unique convolution kernels that
anthonyc3cd15b2010-05-27 06:05:40 +0000678% are specially weighted.
679%
680% Type 0: | -1, 0, 1 |
681% | -sqrt(2), 0, sqrt(2) |
682% | -1, 0, 1 |
683%
684% This is basically the unnormalized discrete kernel that can be used
685% instead ot a Sobel kernel.
686%
687% The next 9 kernel types are specially pre-weighted. They should not
688% be normalized. After applying each to the original image, the results
689% is then added together. The square root of the resulting image is
690% the cosine of the edge, and the direction of the feature detection.
anthonye2a60ce2010-05-19 12:30:40 +0000691%
692% Type 1: | 1, sqrt(2), 1 |
693% | 0, 0, 0 | / 2*sqrt(2)
694% | -1, -sqrt(2), -1 |
695%
696% Type 2: | 1, 0, 1 |
697% | sqrt(2), 0, sqrt(2) | / 2*sqrt(2)
698% | 1, 0, 1 |
699%
700% Type 3: | 0, -1, sqrt(2) |
701% | 1, 0, -1 | / 2*sqrt(2)
702% | -sqrt(2), 1, 0 |
703%
anthony6915d062010-05-19 12:45:51 +0000704% Type 4: | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000705% | -1, 0, 1 | / 2*sqrt(2)
706% | 0, 1, -sqrt(2) |
707%
708% Type 5: | 0, 1, 0 |
709% | -1, 0, -1 | / 2
710% | 0, 1, 0 |
711%
712% Type 6: | -1, 0, 1 |
713% | 0, 0, 0 | / 2
714% | 1, 0, -1 |
715%
anthonyf4e00312010-05-20 12:06:35 +0000716% Type 7: | 1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000717% | -2, 4, -2 | / 6
718% | 1, -2, 1 |
719%
anthonyf4e00312010-05-20 12:06:35 +0000720% Type 8: | -2, 1, -2 |
721% | 1, 4, 1 | / 6
722% | -2, 1, -2 |
anthonye2a60ce2010-05-19 12:30:40 +0000723%
anthonyf4e00312010-05-20 12:06:35 +0000724% Type 9: | 1, 1, 1 |
725% | 1, 1, 1 | / 3
726% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000727%
728% The first 4 are for edge detection, the next 4 are for line detection
729% and the last is to add a average component to the results.
730%
anthonyc3cd15b2010-05-27 06:05:40 +0000731% Using a special type of '-1' will return all 9 pre-weighted kernels
732% as a multi-kernel list, so that you can use them directly (without
733% normalization) with the special "-set option:morphology:compose Plus"
734% setting to apply the full FreiChen Edge Detection Technique.
735%
anthony1dd091a2010-05-27 06:31:15 +0000736% If 'type' is large it will be taken to be an actual rotation angle for
737% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
738% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
739%
anthonye2a60ce2010-05-19 12:30:40 +0000740%
anthony602ab9b2010-01-05 08:06:50 +0000741% Boolean Kernels
742%
anthony3c10fc82010-05-13 02:40:51 +0000743% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000744% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000745% Kernel size will again be radius*2+1 square and defaults to radius 1,
746% generating a 3x3 kernel that is slightly larger than a square.
747%
anthony3c10fc82010-05-13 02:40:51 +0000748% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000749% Generate a square shaped kernel of size radius*2+1, and defaulting
750% to a 3x3 (radius 1).
751%
anthonyc1061722010-05-14 06:23:49 +0000752% Note that using a larger radius for the "Square" or the "Diamond" is
753% also equivelent to iterating the basic morphological method that many
754% times. However iterating with the smaller radius is actually faster
755% than using a larger kernel radius.
756%
757% Rectangle:{geometry}
758% Simply generate a rectangle of 1's with the size given. You can also
759% specify the location of the 'control point', otherwise the closest
760% pixel to the center of the rectangle is selected.
761%
762% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000763%
anthony3c10fc82010-05-13 02:40:51 +0000764% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000765% Generate a binary disk of the radius given, radius may be a float.
766% Kernel size will be ceil(radius)*2+1 square.
767% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000768% "Disk:1" => "diamond" or "cross:1"
769% "Disk:1.5" => "square"
770% "Disk:2" => "diamond:2"
771% "Disk:2.5" => a general disk shape of radius 2
772% "Disk:2.9" => "square:2"
773% "Disk:3.5" => default - octagonal/disk shape of radius 3
774% "Disk:4.2" => roughly octagonal shape of radius 4
775% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000776% After this all the kernel shape becomes more and more circular.
777%
778% Because a "disk" is more circular when using a larger radius, using a
779% larger radius is preferred over iterating the morphological operation.
780%
anthonyc1061722010-05-14 06:23:49 +0000781% Symbol Dilation Kernels
782%
783% These kernel is not a good general morphological kernel, but is used
784% more for highlighting and marking any single pixels in an image using,
785% a "Dilate" method as appropriate.
786%
787% For the same reasons iterating these kernels does not produce the
788% same result as using a larger radius for the symbol.
789%
anthony3c10fc82010-05-13 02:40:51 +0000790% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000791% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000792% Generate a kernel in the shape of a 'plus' or a 'cross' with
793% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000794%
795% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000796%
anthonyc1061722010-05-14 06:23:49 +0000797% Ring:{radius1},{radius2}[,{scale}]
798% A ring of the values given that falls between the two radii.
799% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
800% This is the 'edge' pixels of the default "Disk" kernel,
801% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000802%
anthony3dd0f622010-05-13 12:57:32 +0000803% Hit and Miss Kernels
804%
805% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000806% Find any peak larger than the pixels the fall between the two radii.
807% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000808% Edges
anthony1d45eb92010-05-25 11:13:23 +0000809% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000810% Corners
811% Find corners of a binary shape
anthony47f5d062010-05-23 07:47:50 +0000812% Ridges
anthony1d45eb92010-05-25 11:13:23 +0000813% Find single pixel ridges or thin lines
814% Ridges2
815% Find 2 pixel thick ridges or lines
anthonya648a302010-05-27 02:14:36 +0000816% Ridges3
817% Find 2 pixel thick diagonal ridges (experimental)
anthony3dd0f622010-05-13 12:57:32 +0000818% LineEnds
819% Find end points of lines (for pruning a skeletion)
820% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000821% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000822% ConvexHull
823% Octagonal thicken kernel, to generate convex hulls of 45 degrees
824% Skeleton
825% Thinning kernel, which leaves behind a skeletion of a shape
anthony602ab9b2010-01-05 08:06:50 +0000826%
827% Distance Measuring Kernels
828%
anthonyc1061722010-05-14 06:23:49 +0000829% Different types of distance measuring methods, which are used with the
830% a 'Distance' morphology method for generating a gradient based on
831% distance from an edge of a binary shape, though there is a technique
832% for handling a anti-aliased shape.
833%
834% See the 'Distance' Morphological Method, for information of how it is
835% applied.
836%
anthony3dd0f622010-05-13 12:57:32 +0000837% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000838% Chebyshev Distance (also known as Tchebychev Distance) is a value of
839% one to any neighbour, orthogonal or diagonal. One why of thinking of
840% it is the number of squares a 'King' or 'Queen' in chess needs to
841% traverse reach any other position on a chess board. It results in a
842% 'square' like distance function, but one where diagonals are closer
843% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000844%
anthonyc1061722010-05-14 06:23:49 +0000845% Manhatten:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000846% Manhatten Distance (also known as Rectilinear Distance, or the Taxi
847% Cab metric), is the distance needed when you can only travel in
848% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
849% in chess would travel. It results in a diamond like distances, where
850% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000851%
anthonyc1061722010-05-14 06:23:49 +0000852% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000853% Euclidean Distance is the 'direct' or 'as the crow flys distance.
854% However by default the kernel size only has a radius of 1, which
855% limits the distance to 'Knight' like moves, with only orthogonal and
856% diagonal measurements being correct. As such for the default kernel
857% you will get octagonal like distance function, which is reasonally
858% accurate.
859%
860% However if you use a larger radius such as "Euclidean:4" you will
861% get a much smoother distance gradient from the edge of the shape.
862% Of course a larger kernel is slower to use, and generally not needed.
863%
864% To allow the use of fractional distances that you get with diagonals
865% the actual distance is scaled by a fixed value which the user can
866% provide. This is not actually nessary for either ""Chebyshev" or
867% "Manhatten" distance kernels, but is done for all three distance
868% kernels. If no scale is provided it is set to a value of 100,
869% allowing for a maximum distance measurement of 655 pixels using a Q16
870% version of IM, from any edge. However for small images this can
871% result in quite a dark gradient.
872%
anthony602ab9b2010-01-05 08:06:50 +0000873*/
874
cristy2be15382010-01-21 02:38:03 +0000875MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000876 const GeometryInfo *args)
877{
cristy2be15382010-01-21 02:38:03 +0000878 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000879 *kernel;
880
cristybb503372010-05-27 20:51:26 +0000881 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000882 i;
883
cristybb503372010-05-27 20:51:26 +0000884 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000885 u,
886 v;
887
888 double
889 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
890
anthonyc1061722010-05-14 06:23:49 +0000891 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000892 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000893 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000894 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000895 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000896 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000897 break;
anthony1dd091a2010-05-27 06:31:15 +0000898 case UnityKernel: /* Named Descrete Convolution Kernels */
899 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000900 case SobelKernel:
901 case RobertsKernel:
902 case PrewittKernel:
903 case CompassKernel:
904 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000905 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000906 case CornersKernel: /* Hit and Miss kernels */
907 case LineEndsKernel:
908 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000909 case EdgesKernel:
910 case RidgesKernel:
911 case Ridges2Kernel:
anthony9eb4f742010-05-18 02:45:54 +0000912 case ConvexHullKernel:
913 case SkeletonKernel:
anthony1dd091a2010-05-27 06:31:15 +0000914 case MatKernel:
anthony9eb4f742010-05-18 02:45:54 +0000915 /* A pre-generated kernel is not needed */
916 break;
anthony1dd091a2010-05-27 06:31:15 +0000917#if 0 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000918 case GaussianKernel:
919 case DOGKernel:
anthony1dd091a2010-05-27 06:31:15 +0000920 case LOGKernel:
anthonyc1061722010-05-14 06:23:49 +0000921 case BlurKernel:
922 case DOBKernel:
923 case CometKernel:
924 case DiamondKernel:
925 case SquareKernel:
926 case RectangleKernel:
927 case DiskKernel:
928 case PlusKernel:
929 case CrossKernel:
930 case RingKernel:
931 case PeaksKernel:
932 case ChebyshevKernel:
933 case ManhattenKernel:
934 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000935#else
anthony9eb4f742010-05-18 02:45:54 +0000936 default:
anthony1dd091a2010-05-27 06:31:15 +0000937#endif
anthony9eb4f742010-05-18 02:45:54 +0000938 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000939 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
940 if (kernel == (KernelInfo *) NULL)
941 return(kernel);
942 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000943 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000944 kernel->negative_range = kernel->positive_range = 0.0;
945 kernel->type = type;
946 kernel->next = (KernelInfo *) NULL;
947 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000948 break;
949 }
anthony602ab9b2010-01-05 08:06:50 +0000950
951 switch(type) {
952 /* Convolution Kernels */
953 case GaussianKernel:
anthonyc1061722010-05-14 06:23:49 +0000954 case DOGKernel:
anthony9eb4f742010-05-18 02:45:54 +0000955 case LOGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000956 { double
anthonyc1061722010-05-14 06:23:49 +0000957 sigma = fabs(args->sigma),
958 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000959 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000960
anthonyc1061722010-05-14 06:23:49 +0000961 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +0000962 kernel->width = (size_t)args->rho*2+1;
anthony9eb4f742010-05-18 02:45:54 +0000963 else if ( (type != DOGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +0000964 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
965 else
966 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
967 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +0000968 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000969 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
970 kernel->height*sizeof(double));
971 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000972 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000973
anthony46a369d2010-05-19 02:41:48 +0000974 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +0000975 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +0000976 *
977 * How to do this is currently not known, but appears to be
978 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +0000979 */
980
981 if ( type == GaussianKernel || type == DOGKernel )
982 { /* Calculate a Gaussian, OR positive half of a DOG */
983 if ( sigma > MagickEpsilon )
984 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
985 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +0000986 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
987 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +0000988 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
989 }
990 else /* limiting case - a unity (normalized Dirac) kernel */
991 { (void) ResetMagickMemory(kernel->values,0, (size_t)
992 kernel->width*kernel->height*sizeof(double));
993 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
994 }
anthonyc1061722010-05-14 06:23:49 +0000995 }
anthony9eb4f742010-05-18 02:45:54 +0000996
anthonyc1061722010-05-14 06:23:49 +0000997 if ( type == DOGKernel )
998 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
999 if ( sigma2 > MagickEpsilon )
1000 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001001 A = 1.0/(2.0*sigma*sigma);
1002 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001003 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1004 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001005 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001006 }
anthony9eb4f742010-05-18 02:45:54 +00001007 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001008 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1009 }
anthony9eb4f742010-05-18 02:45:54 +00001010
1011 if ( type == LOGKernel )
1012 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1013 if ( sigma > MagickEpsilon )
1014 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1015 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001016 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1017 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001018 { R = ((double)(u*u+v*v))*A;
1019 kernel->values[i] = (1-R)*exp(-R)*B;
1020 }
1021 }
1022 else /* special case - generate a unity kernel */
1023 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1024 kernel->width*kernel->height*sizeof(double));
1025 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1026 }
1027 }
1028
1029 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001030 ** radius, producing a smaller (darker) kernel. Also for very small
1031 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1032 ** producing a very bright kernel.
1033 **
1034 ** Normalization will still be needed.
1035 */
anthony602ab9b2010-01-05 08:06:50 +00001036
anthony3dd0f622010-05-13 12:57:32 +00001037 /* Normalize the 2D Gaussian Kernel
1038 **
anthonyc1061722010-05-14 06:23:49 +00001039 ** NB: a CorrelateNormalize performs a normal Normalize if
1040 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001041 */
anthony46a369d2010-05-19 02:41:48 +00001042 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001043 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001044
1045 break;
1046 }
1047 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +00001048 case DOBKernel:
anthony602ab9b2010-01-05 08:06:50 +00001049 { double
anthonyc1061722010-05-14 06:23:49 +00001050 sigma = fabs(args->sigma),
1051 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001052 A, B;
anthony602ab9b2010-01-05 08:06:50 +00001053
anthonyc1061722010-05-14 06:23:49 +00001054 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001055 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001056 else if ( (type == BlurKernel) || (sigma >= sigma2) )
1057 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
1058 else
1059 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma2);
anthony602ab9b2010-01-05 08:06:50 +00001060 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001061 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001062 kernel->y = 0;
1063 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001064 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1065 kernel->height*sizeof(double));
1066 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001067 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001068
1069#if 1
1070#define KernelRank 3
1071 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1072 ** It generates a gaussian 3 times the width, and compresses it into
1073 ** the expected range. This produces a closer normalization of the
1074 ** resulting kernel, especially for very low sigma values.
1075 ** As such while wierd it is prefered.
1076 **
1077 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001078 **
1079 ** A properly normalized curve is generated (apart from edge clipping)
1080 ** even though we later normalize the result (for edge clipping)
1081 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001082 */
anthonyc1061722010-05-14 06:23:49 +00001083
1084 /* initialize */
cristybb503372010-05-27 20:51:26 +00001085 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001086 (void) ResetMagickMemory(kernel->values,0, (size_t)
1087 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001088 /* Calculate a Positive 1D Gaussian */
1089 if ( sigma > MagickEpsilon )
1090 { sigma *= KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001091 A = 1.0/(2.0*sigma*sigma);
1092 B = 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001093 for ( u=-v; u <= v; u++) {
anthony9eb4f742010-05-18 02:45:54 +00001094 kernel->values[(u+v)/KernelRank] += exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001095 }
1096 }
1097 else /* special case - generate a unity kernel */
1098 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony9eb4f742010-05-18 02:45:54 +00001099
1100 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001101 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001102 {
anthonyc1061722010-05-14 06:23:49 +00001103 if ( sigma2 > MagickEpsilon )
1104 { sigma = sigma2*KernelRank; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001105 A = 1.0/(2.0*sigma*sigma);
1106 B = 1.0/(MagickSQ2PI*sigma);
anthonyc1061722010-05-14 06:23:49 +00001107 for ( u=-v; u <= v; u++)
anthony9eb4f742010-05-18 02:45:54 +00001108 kernel->values[(u+v)/KernelRank] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001109 }
anthony9eb4f742010-05-18 02:45:54 +00001110 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001111 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1112 }
anthony602ab9b2010-01-05 08:06:50 +00001113#else
anthonyc1061722010-05-14 06:23:49 +00001114 /* Direct calculation without curve averaging */
1115
1116 /* Calculate a Positive Gaussian */
1117 if ( sigma > MagickEpsilon )
anthony9eb4f742010-05-18 02:45:54 +00001118 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1119 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001120 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001121 kernel->values[i] = exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001122 }
1123 else /* special case - generate a unity kernel */
1124 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1125 kernel->width*kernel->height*sizeof(double));
1126 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1127 }
anthony9eb4f742010-05-18 02:45:54 +00001128
1129 /* Subtract a Second 1D Gaussian for "Difference of Blur" */
anthonyc1061722010-05-14 06:23:49 +00001130 if ( type == DOBKernel )
anthony9eb4f742010-05-18 02:45:54 +00001131 {
anthonyc1061722010-05-14 06:23:49 +00001132 if ( sigma2 > MagickEpsilon )
1133 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001134 A = 1.0/(2.0*sigma*sigma);
1135 B = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001136 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001137 kernel->values[i] -= exp(-((double)(u*u))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001138 }
anthony9eb4f742010-05-18 02:45:54 +00001139 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001140 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1141 }
anthony602ab9b2010-01-05 08:06:50 +00001142#endif
anthonyc1061722010-05-14 06:23:49 +00001143 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001144 ** radius, producing a smaller (darker) kernel. Also for very small
1145 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1146 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001147 **
1148 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001149 */
anthonycc6c8362010-01-25 04:14:01 +00001150
anthony602ab9b2010-01-05 08:06:50 +00001151 /* Normalize the 1D Gaussian Kernel
1152 **
anthonyc1061722010-05-14 06:23:49 +00001153 ** NB: a CorrelateNormalize performs a normal Normalize if
1154 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001155 */
anthony46a369d2010-05-19 02:41:48 +00001156 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1157 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001158
anthonyc1061722010-05-14 06:23:49 +00001159 /* rotate the 1D kernel by given angle */
1160 RotateKernelInfo(kernel, (type == BlurKernel) ? args->xi : args->psi );
anthony602ab9b2010-01-05 08:06:50 +00001161 break;
1162 }
1163 case CometKernel:
1164 { double
anthony9eb4f742010-05-18 02:45:54 +00001165 sigma = fabs(args->sigma),
1166 A;
anthony602ab9b2010-01-05 08:06:50 +00001167
anthony602ab9b2010-01-05 08:06:50 +00001168 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001169 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001170 else
cristybb503372010-05-27 20:51:26 +00001171 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001172 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001173 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001174 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001175 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1176 kernel->height*sizeof(double));
1177 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001178 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001179
anthonyc1061722010-05-14 06:23:49 +00001180 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001181 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001182 ** curve to use so may change in the future. The function must be
1183 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001184 **
1185 ** As we are normalizing and not subtracting gaussians,
1186 ** there is no need for a divisor in the gaussian formula
1187 **
anthony43c49252010-05-18 10:59:50 +00001188 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001189 */
anthony9eb4f742010-05-18 02:45:54 +00001190 if ( sigma > MagickEpsilon )
1191 {
anthony602ab9b2010-01-05 08:06:50 +00001192#if 1
1193#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001194 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001195 (void) ResetMagickMemory(kernel->values,0, (size_t)
1196 kernel->width*sizeof(double));
1197 sigma *= KernelRank; /* simplify the loop expression */
1198 A = 1.0/(2.0*sigma*sigma);
1199 /* B = 1.0/(MagickSQ2PI*sigma); */
1200 for ( u=0; u < v; u++) {
1201 kernel->values[u/KernelRank] +=
1202 exp(-((double)(u*u))*A);
1203 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1204 }
cristybb503372010-05-27 20:51:26 +00001205 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001206 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001207#else
anthony9eb4f742010-05-18 02:45:54 +00001208 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1209 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001210 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001211 kernel->positive_range +=
1212 kernel->values[i] =
1213 exp(-((double)(i*i))*A);
1214 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001215#endif
anthony9eb4f742010-05-18 02:45:54 +00001216 }
1217 else /* special case - generate a unity kernel */
1218 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1219 kernel->width*kernel->height*sizeof(double));
1220 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1221 kernel->positive_range = 1.0;
1222 }
anthony46a369d2010-05-19 02:41:48 +00001223
1224 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001225 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001226 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001227
anthony999bb2c2010-02-18 12:38:01 +00001228 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1229 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001230 break;
1231 }
anthonyc1061722010-05-14 06:23:49 +00001232
anthony3c10fc82010-05-13 02:40:51 +00001233 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001234 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001235 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001236 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001237 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001238 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001239 break;
anthony9eb4f742010-05-18 02:45:54 +00001240 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001241 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001242 break;
1243 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001244 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1245 break;
1246 case 3:
anthonyc1061722010-05-14 06:23:49 +00001247 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001248 break;
anthony9eb4f742010-05-18 02:45:54 +00001249 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001250 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001251 "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 +00001252 break;
anthony9eb4f742010-05-18 02:45:54 +00001253 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001254 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001255 "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 +00001256 break;
anthony43c49252010-05-18 10:59:50 +00001257 case 15: /* a 5x5 LOG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001258 kernel=ParseKernelArray(
1259 "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");
1260 break;
anthony43c49252010-05-18 10:59:50 +00001261 case 19: /* a 9x9 LOG (sigma approx 1.4) */
1262 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1263 kernel=ParseKernelArray(
1264 "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");
1265 break;
anthony3c10fc82010-05-13 02:40:51 +00001266 }
1267 if (kernel == (KernelInfo *) NULL)
1268 return(kernel);
1269 kernel->type = type;
1270 break;
1271 }
anthonyc1061722010-05-14 06:23:49 +00001272 case SobelKernel:
anthony602ab9b2010-01-05 08:06:50 +00001273 {
anthonyc1061722010-05-14 06:23:49 +00001274 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1275 if (kernel == (KernelInfo *) NULL)
1276 return(kernel);
1277 kernel->type = type;
1278 RotateKernelInfo(kernel, args->rho); /* Rotate by angle */
1279 break;
1280 }
1281 case RobertsKernel:
1282 {
1283 kernel=ParseKernelArray("3: 0,0,0 -1,1,0 0,0,0");
1284 if (kernel == (KernelInfo *) NULL)
1285 return(kernel);
1286 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001287 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001288 break;
1289 }
1290 case PrewittKernel:
1291 {
1292 kernel=ParseKernelArray("3: -1,1,1 0,0,0 -1,1,1");
1293 if (kernel == (KernelInfo *) NULL)
1294 return(kernel);
1295 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001296 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001297 break;
1298 }
1299 case CompassKernel:
1300 {
1301 kernel=ParseKernelArray("3: -1,1,1 -1,-2,1 -1,1,1");
1302 if (kernel == (KernelInfo *) NULL)
1303 return(kernel);
1304 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001305 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001306 break;
1307 }
anthony9eb4f742010-05-18 02:45:54 +00001308 case KirschKernel:
1309 {
1310 kernel=ParseKernelArray("3: -3,-3,5 -3,0,5 -3,-3,5");
1311 if (kernel == (KernelInfo *) NULL)
1312 return(kernel);
1313 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001314 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001315 break;
1316 }
anthonye2a60ce2010-05-19 12:30:40 +00001317 case FreiChenKernel:
anthony6915d062010-05-19 12:45:51 +00001318 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf */
1319 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf */
anthony1dd091a2010-05-27 06:31:15 +00001320 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001321 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001322 case 0:
1323 kernel=ParseKernelArray("3: -1,0,1 -2,0,2 -1,0,1");
1324 if (kernel == (KernelInfo *) NULL)
1325 return(kernel);
anthony1dd091a2010-05-27 06:31:15 +00001326 kernel->values[3] = -MagickSQ2;
1327 kernel->values[5] = +MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001328 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001329 break;
anthonye2a60ce2010-05-19 12:30:40 +00001330 case 1:
1331 kernel=ParseKernelArray("3: 1,2,1 0,0,0 -1,2,-1");
1332 if (kernel == (KernelInfo *) NULL)
1333 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001334 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001335 kernel->values[1] = +MagickSQ2;
1336 kernel->values[7] = -MagickSQ2;
1337 CalcKernelMetaData(kernel); /* recalculate meta-data */
1338 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1339 break;
1340 case 2:
1341 kernel=ParseKernelArray("3: 1,0,1 2,0,2 1,0,1");
1342 if (kernel == (KernelInfo *) NULL)
1343 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001344 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001345 kernel->values[3] = +MagickSQ2;
1346 kernel->values[5] = +MagickSQ2;
1347 CalcKernelMetaData(kernel);
1348 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1349 break;
1350 case 3:
1351 kernel=ParseKernelArray("3: 0,-1,2 1,0,-1 -2,1,0");
1352 if (kernel == (KernelInfo *) NULL)
1353 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001354 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001355 kernel->values[2] = +MagickSQ2;
1356 kernel->values[6] = -MagickSQ2;
1357 CalcKernelMetaData(kernel);
1358 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1359 break;
1360 case 4:
anthony6915d062010-05-19 12:45:51 +00001361 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001362 if (kernel == (KernelInfo *) NULL)
1363 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001364 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001365 kernel->values[0] = +MagickSQ2;
1366 kernel->values[8] = -MagickSQ2;
1367 CalcKernelMetaData(kernel);
1368 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1369 break;
1370 case 5:
1371 kernel=ParseKernelArray("3: 0,1,0 -1,0,-1 0,1,0");
1372 if (kernel == (KernelInfo *) NULL)
1373 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001374 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001375 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1376 break;
1377 case 6:
1378 kernel=ParseKernelArray("3: -1,0,1 0,0,0 1,0,-1");
1379 if (kernel == (KernelInfo *) NULL)
1380 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001381 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001382 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1383 break;
1384 case 7:
anthonyf4e00312010-05-20 12:06:35 +00001385 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001386 if (kernel == (KernelInfo *) NULL)
1387 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001388 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001389 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1390 break;
1391 case 8:
1392 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1393 if (kernel == (KernelInfo *) NULL)
1394 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001395 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001396 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1397 break;
1398 case 9:
anthonyc3cd15b2010-05-27 06:05:40 +00001399 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001400 if (kernel == (KernelInfo *) NULL)
1401 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001402 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001403 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1404 break;
anthonyc3cd15b2010-05-27 06:05:40 +00001405 case -1:
anthony1dd091a2010-05-27 06:31:15 +00001406 kernel=AcquireKernelInfo("FreiChen:1;FreiChen:2;FreiChen:3;FreiChen:4;FreiChen:5;FreiChen:6;FreiChen:7;FreiChen:8;FreiChen:9");
1407 if (kernel == (KernelInfo *) NULL)
1408 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001409 break;
anthonye2a60ce2010-05-19 12:30:40 +00001410 }
anthonyc3cd15b2010-05-27 06:05:40 +00001411 if ( fabs(args->sigma) > MagickEpsilon )
1412 /* Rotate by correctly supplied 'angle' */
1413 RotateKernelInfo(kernel, args->sigma);
1414 else if ( args->rho > 30.0 || args->rho < -30.0 )
1415 /* Rotate by out of bounds 'type' */
1416 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001417 break;
1418 }
1419
anthonyc1061722010-05-14 06:23:49 +00001420 /* Boolean Kernels */
1421 case DiamondKernel:
1422 {
1423 if (args->rho < 1.0)
1424 kernel->width = kernel->height = 3; /* default radius = 1 */
1425 else
cristybb503372010-05-27 20:51:26 +00001426 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1427 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001428
1429 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1430 kernel->height*sizeof(double));
1431 if (kernel->values == (double *) NULL)
1432 return(DestroyKernelInfo(kernel));
1433
1434 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001435 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1436 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1437 if ((labs(u)+labs(v)) <= (ssize_t)kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001438 kernel->positive_range += kernel->values[i] = args->sigma;
1439 else
1440 kernel->values[i] = nan;
1441 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1442 break;
1443 }
1444 case SquareKernel:
1445 case RectangleKernel:
1446 { double
1447 scale;
anthony602ab9b2010-01-05 08:06:50 +00001448 if ( type == SquareKernel )
1449 {
1450 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001451 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001452 else
cristybb503372010-05-27 20:51:26 +00001453 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1454 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001455 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001456 }
1457 else {
cristy2be15382010-01-21 02:38:03 +00001458 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001459 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001460 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001461 kernel->width = (size_t)args->rho;
1462 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001463 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1464 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001465 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001466 kernel->x = (ssize_t) args->xi;
1467 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001468 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001469 }
1470 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1471 kernel->height*sizeof(double));
1472 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001473 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001474
anthony3dd0f622010-05-13 12:57:32 +00001475 /* set all kernel values to scale given */
cristybb503372010-05-27 20:51:26 +00001476 u=(ssize_t) kernel->width*kernel->height;
cristy150989e2010-02-01 14:59:39 +00001477 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001478 kernel->values[i] = scale;
1479 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1480 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001481 break;
anthony602ab9b2010-01-05 08:06:50 +00001482 }
anthony602ab9b2010-01-05 08:06:50 +00001483 case DiskKernel:
1484 {
cristybb503372010-05-27 20:51:26 +00001485 ssize_t limit = (ssize_t)(args->rho*args->rho);
anthony83ba99b2010-01-24 08:48:15 +00001486 if (args->rho < 0.1) /* default radius approx 3.5 */
1487 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001488 else
cristybb503372010-05-27 20:51:26 +00001489 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1490 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001491
1492 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1493 kernel->height*sizeof(double));
1494 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001495 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001496
anthony3dd0f622010-05-13 12:57:32 +00001497 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001498 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1499 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001500 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001501 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001502 else
1503 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001504 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001505 break;
1506 }
1507 case PlusKernel:
1508 {
1509 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001510 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001511 else
cristybb503372010-05-27 20:51:26 +00001512 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1513 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001514
1515 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1516 kernel->height*sizeof(double));
1517 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001518 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001519
cristycee97112010-05-28 00:44:52 +00001520 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001521 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1522 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001523 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1524 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1525 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001526 break;
1527 }
anthony3dd0f622010-05-13 12:57:32 +00001528 case CrossKernel:
1529 {
1530 if (args->rho < 1.0)
1531 kernel->width = kernel->height = 5; /* default radius 2 */
1532 else
cristybb503372010-05-27 20:51:26 +00001533 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1534 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001535
1536 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1537 kernel->height*sizeof(double));
1538 if (kernel->values == (double *) NULL)
1539 return(DestroyKernelInfo(kernel));
1540
cristycee97112010-05-28 00:44:52 +00001541 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001542 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1543 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001544 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1545 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1546 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1547 break;
1548 }
1549 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001550 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001551 case PeaksKernel:
1552 {
cristybb503372010-05-27 20:51:26 +00001553 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001554 limit1,
anthonyc1061722010-05-14 06:23:49 +00001555 limit2,
1556 scale;
anthony3dd0f622010-05-13 12:57:32 +00001557
1558 if (args->rho < args->sigma)
1559 {
cristybb503372010-05-27 20:51:26 +00001560 kernel->width = ((size_t)args->sigma)*2+1;
1561 limit1 = (ssize_t)args->rho*args->rho;
1562 limit2 = (ssize_t)args->sigma*args->sigma;
anthony3dd0f622010-05-13 12:57:32 +00001563 }
1564 else
1565 {
cristybb503372010-05-27 20:51:26 +00001566 kernel->width = ((size_t)args->rho)*2+1;
1567 limit1 = (ssize_t)args->sigma*args->sigma;
1568 limit2 = (ssize_t)args->rho*args->rho;
anthony3dd0f622010-05-13 12:57:32 +00001569 }
anthonyc1061722010-05-14 06:23:49 +00001570 if ( limit2 <= 0 )
1571 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1572
anthony3dd0f622010-05-13 12:57:32 +00001573 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001574 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001575 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1576 kernel->height*sizeof(double));
1577 if (kernel->values == (double *) NULL)
1578 return(DestroyKernelInfo(kernel));
1579
anthonyc1061722010-05-14 06:23:49 +00001580 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001581 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1582 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1583 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1584 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001585 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001586 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001587 else
1588 kernel->values[i] = nan;
1589 }
cristye96405a2010-05-19 02:24:31 +00001590 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001591 if ( type == PeaksKernel ) {
1592 /* set the central point in the middle */
1593 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1594 kernel->positive_range = 1.0;
1595 kernel->maximum = 1.0;
1596 }
anthony3dd0f622010-05-13 12:57:32 +00001597 break;
1598 }
anthony43c49252010-05-18 10:59:50 +00001599 case EdgesKernel:
1600 {
1601 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1602 if (kernel == (KernelInfo *) NULL)
1603 return(kernel);
1604 kernel->type = type;
1605 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1606 break;
1607 }
anthony3dd0f622010-05-13 12:57:32 +00001608 case CornersKernel:
1609 {
anthony4f1dcb72010-05-14 08:43:10 +00001610 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001611 if (kernel == (KernelInfo *) NULL)
1612 return(kernel);
1613 kernel->type = type;
1614 ExpandKernelInfo(kernel, 90.0); /* Create a list of 4 rotated kernels */
1615 break;
1616 }
anthony47f5d062010-05-23 07:47:50 +00001617 case RidgesKernel:
1618 {
anthony24a19842010-05-27 12:18:34 +00001619 kernel=ParseKernelArray("3x1:0,1,0");
anthony47f5d062010-05-23 07:47:50 +00001620 if (kernel == (KernelInfo *) NULL)
1621 return(kernel);
1622 kernel->type = type;
anthony24a19842010-05-27 12:18:34 +00001623 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
anthony47f5d062010-05-23 07:47:50 +00001624 break;
1625 }
anthony1d45eb92010-05-25 11:13:23 +00001626 case Ridges2Kernel:
1627 {
1628 KernelInfo
1629 *new_kernel;
anthony24a19842010-05-27 12:18:34 +00001630 kernel=ParseKernelArray("4x1:0,1,1,0");
anthony1d45eb92010-05-25 11:13:23 +00001631 if (kernel == (KernelInfo *) NULL)
1632 return(kernel);
1633 kernel->type = type;
1634 ExpandKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthonya648a302010-05-27 02:14:36 +00001635#if 0
1636 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
anthony1d45eb92010-05-25 11:13:23 +00001637 new_kernel=ParseKernelArray("4x4^:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1638 if (new_kernel == (KernelInfo *) NULL)
1639 return(DestroyKernelInfo(kernel));
1640 new_kernel->type = type;
1641 ExpandKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1642 LastKernelInfo(kernel)->next = new_kernel;
anthonya648a302010-05-27 02:14:36 +00001643#endif
1644 /* kernels to find a stepped 'thick' line - 4 rotates * mirror */
1645 /* Unfortunatally we can not yet rotate a non-square kernel */
1646 /* But then we can't flip a non-symetrical kernel either */
1647 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1648 if (new_kernel == (KernelInfo *) NULL)
1649 return(DestroyKernelInfo(kernel));
1650 new_kernel->type = type;
1651 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001652 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
anthonya648a302010-05-27 02:14:36 +00001653 if (new_kernel == (KernelInfo *) NULL)
1654 return(DestroyKernelInfo(kernel));
1655 new_kernel->type = type;
1656 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001657 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001658 if (new_kernel == (KernelInfo *) NULL)
1659 return(DestroyKernelInfo(kernel));
1660 new_kernel->type = type;
1661 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001662 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
anthonya648a302010-05-27 02:14:36 +00001663 if (new_kernel == (KernelInfo *) NULL)
1664 return(DestroyKernelInfo(kernel));
1665 new_kernel->type = type;
1666 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001667 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001668 if (new_kernel == (KernelInfo *) NULL)
1669 return(DestroyKernelInfo(kernel));
1670 new_kernel->type = type;
1671 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001672 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
anthonya648a302010-05-27 02:14:36 +00001673 if (new_kernel == (KernelInfo *) NULL)
1674 return(DestroyKernelInfo(kernel));
1675 new_kernel->type = type;
1676 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001677 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001678 if (new_kernel == (KernelInfo *) NULL)
1679 return(DestroyKernelInfo(kernel));
1680 new_kernel->type = type;
1681 LastKernelInfo(kernel)->next = new_kernel;
anthony24a19842010-05-27 12:18:34 +00001682 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
anthonya648a302010-05-27 02:14:36 +00001683 if (new_kernel == (KernelInfo *) NULL)
1684 return(DestroyKernelInfo(kernel));
1685 new_kernel->type = type;
1686 LastKernelInfo(kernel)->next = new_kernel;
anthony1d45eb92010-05-25 11:13:23 +00001687 break;
1688 }
anthony3dd0f622010-05-13 12:57:32 +00001689 case LineEndsKernel:
1690 {
anthony43c49252010-05-18 10:59:50 +00001691 KernelInfo
1692 *new_kernel;
1693 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001694 if (kernel == (KernelInfo *) NULL)
1695 return(kernel);
1696 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001697 ExpandKernelInfo(kernel, 90.0);
1698 /* append second set of 4 kernels */
1699 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1700 if (new_kernel == (KernelInfo *) NULL)
1701 return(DestroyKernelInfo(kernel));
1702 new_kernel->type = type;
1703 ExpandKernelInfo(new_kernel, 90.0);
1704 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001705 break;
1706 }
1707 case LineJunctionsKernel:
1708 {
1709 KernelInfo
1710 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001711 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001712 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001713 if (kernel == (KernelInfo *) NULL)
1714 return(kernel);
1715 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001716 ExpandKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001717 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001718 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001719 if (new_kernel == (KernelInfo *) NULL)
1720 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001721 new_kernel->type = type;
anthony3dd0f622010-05-13 12:57:32 +00001722 ExpandKernelInfo(new_kernel, 90.0);
1723 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001724 break;
1725 }
anthony3dd0f622010-05-13 12:57:32 +00001726 case ConvexHullKernel:
1727 {
anthony3928ec62010-05-27 14:03:29 +00001728 KernelInfo
1729 *new_kernel;
1730 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001731 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001732 if (kernel == (KernelInfo *) NULL)
1733 return(kernel);
1734 kernel->type = type;
anthony01f75e02010-05-27 13:19:10 +00001735 ExpandKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001736 /* append the mirror versions too */
1737 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1738 if (new_kernel == (KernelInfo *) NULL)
1739 return(DestroyKernelInfo(kernel));
1740 new_kernel->type = type;
1741 ExpandKernelInfo(new_kernel, 45.0);
1742 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001743 break;
1744 }
anthony47f5d062010-05-23 07:47:50 +00001745 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001746 { /* what is the best form for skeletonization by thinning? */
anthony47f5d062010-05-23 07:47:50 +00001747#if 0
1748# if 0
1749 kernel=AcquireKernelInfo("Corners;Edges");
1750# else
1751 kernel=AcquireKernelInfo("Edges;Corners");
1752# endif
1753#else
anthony43c49252010-05-18 10:59:50 +00001754 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,1");
anthony3dd0f622010-05-13 12:57:32 +00001755 if (kernel == (KernelInfo *) NULL)
1756 return(kernel);
1757 kernel->type = type;
anthony43c49252010-05-18 10:59:50 +00001758 ExpandKernelInfo(kernel, 45);
1759 break;
anthony47f5d062010-05-23 07:47:50 +00001760#endif
anthony3dd0f622010-05-13 12:57:32 +00001761 break;
1762 }
anthonya648a302010-05-27 02:14:36 +00001763 case MatKernel: /* experimental - MAT from a Distance Gradient */
1764 {
1765 KernelInfo
1766 *new_kernel;
1767 /* Ridge Kernel but without the diagonal */
1768 kernel=ParseKernelArray("3x1: 0,1,0");
1769 if (kernel == (KernelInfo *) NULL)
1770 return(kernel);
1771 kernel->type = RidgesKernel;
1772 ExpandKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1773 /* Plus the 2 pixel ridges kernel - no diagonal */
1774 new_kernel=AcquireKernelBuiltIn(Ridges2Kernel,args);
1775 if (new_kernel == (KernelInfo *) NULL)
1776 return(kernel);
1777 LastKernelInfo(kernel)->next = new_kernel;
1778 break;
1779 }
anthony602ab9b2010-01-05 08:06:50 +00001780 /* Distance Measuring Kernels */
1781 case ChebyshevKernel:
1782 {
anthony602ab9b2010-01-05 08:06:50 +00001783 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001784 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001785 else
cristybb503372010-05-27 20:51:26 +00001786 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1787 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001788
1789 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1790 kernel->height*sizeof(double));
1791 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001792 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001793
cristybb503372010-05-27 20:51:26 +00001794 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1795 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001796 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001797 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001798 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001799 break;
1800 }
1801 case ManhattenKernel:
1802 {
anthony602ab9b2010-01-05 08:06:50 +00001803 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001804 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001805 else
cristybb503372010-05-27 20:51:26 +00001806 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1807 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001808
1809 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1810 kernel->height*sizeof(double));
1811 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001812 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001813
cristybb503372010-05-27 20:51:26 +00001814 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1815 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001816 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001817 args->sigma*(labs(u)+labs(v)) );
cristyc99304f2010-02-01 15:26:27 +00001818 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001819 break;
1820 }
1821 case EuclideanKernel:
1822 {
anthony602ab9b2010-01-05 08:06:50 +00001823 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001824 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001825 else
cristybb503372010-05-27 20:51:26 +00001826 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1827 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001828
1829 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1830 kernel->height*sizeof(double));
1831 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001832 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001833
cristybb503372010-05-27 20:51:26 +00001834 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1835 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001836 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001837 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001838 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001839 break;
1840 }
anthony46a369d2010-05-19 02:41:48 +00001841 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001842 default:
anthonyc1061722010-05-14 06:23:49 +00001843 {
anthony46a369d2010-05-19 02:41:48 +00001844 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1845 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001846 if (kernel == (KernelInfo *) NULL)
1847 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001848 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001849 break;
1850 }
anthony602ab9b2010-01-05 08:06:50 +00001851 break;
1852 }
1853
1854 return(kernel);
1855}
anthonyc94cdb02010-01-06 08:15:29 +00001856
anthony602ab9b2010-01-05 08:06:50 +00001857/*
1858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859% %
1860% %
1861% %
cristy6771f1e2010-03-05 19:43:39 +00001862% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001863% %
1864% %
1865% %
1866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1867%
anthony1b2bc0a2010-05-12 05:25:22 +00001868% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1869% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001870% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001871%
cristye6365592010-04-02 17:31:23 +00001872% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001873%
anthony930be612010-02-08 04:26:15 +00001874% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001875%
1876% A description of each parameter follows:
1877%
1878% o kernel: the Morphology/Convolution kernel to be cloned
1879%
1880*/
cristyef656912010-03-05 19:54:59 +00001881MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001882{
cristybb503372010-05-27 20:51:26 +00001883 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001884 i;
1885
cristy19eb6412010-04-23 14:42:29 +00001886 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001887 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001888
1889 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001890 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1891 if (new_kernel == (KernelInfo *) NULL)
1892 return(new_kernel);
1893 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00001894
1895 /* replace the values with a copy of the values */
1896 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00001897 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00001898 if (new_kernel->values == (double *) NULL)
1899 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00001900 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00001901 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00001902
1903 /* Also clone the next kernel in the kernel list */
1904 if ( kernel->next != (KernelInfo *) NULL ) {
1905 new_kernel->next = CloneKernelInfo(kernel->next);
1906 if ( new_kernel->next == (KernelInfo *) NULL )
1907 return(DestroyKernelInfo(new_kernel));
1908 }
1909
anthony7a01dcf2010-05-11 12:25:52 +00001910 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00001911}
1912
1913/*
1914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1915% %
1916% %
1917% %
anthony83ba99b2010-01-24 08:48:15 +00001918% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00001919% %
1920% %
1921% %
1922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1923%
anthony83ba99b2010-01-24 08:48:15 +00001924% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1925% kernel.
anthony602ab9b2010-01-05 08:06:50 +00001926%
anthony83ba99b2010-01-24 08:48:15 +00001927% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00001928%
anthony83ba99b2010-01-24 08:48:15 +00001929% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001930%
1931% A description of each parameter follows:
1932%
1933% o kernel: the Morphology/Convolution kernel to be destroyed
1934%
1935*/
1936
anthony83ba99b2010-01-24 08:48:15 +00001937MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00001938{
cristy2be15382010-01-21 02:38:03 +00001939 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00001940
anthony7a01dcf2010-05-11 12:25:52 +00001941 if ( kernel->next != (KernelInfo *) NULL )
1942 kernel->next = DestroyKernelInfo(kernel->next);
1943
1944 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
1945 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00001946 return(kernel);
1947}
anthonyc94cdb02010-01-06 08:15:29 +00001948
1949/*
1950%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951% %
1952% %
1953% %
anthony3c10fc82010-05-13 02:40:51 +00001954% E x p a n d K e r n e l I n f o %
1955% %
1956% %
1957% %
1958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959%
1960% ExpandKernelInfo() takes a single kernel, and expands it into a list
1961% of kernels each incrementally rotated the angle given.
1962%
1963% WARNING: 45 degree rotations only works for 3x3 kernels.
1964% While 90 degree roatations only works for linear and square kernels
1965%
1966% The format of the RotateKernelInfo method is:
1967%
1968% void ExpandKernelInfo(KernelInfo *kernel, double angle)
1969%
1970% A description of each parameter follows:
1971%
1972% o kernel: the Morphology/Convolution kernel
1973%
1974% o angle: angle to rotate in degrees
1975%
1976% This function is only internel to this module, as it is not finalized,
1977% especially with regard to non-orthogonal angles, and rotation of larger
1978% 2D kernels.
1979*/
anthony47f5d062010-05-23 07:47:50 +00001980
1981/* Internal Routine - Return true if two kernels are the same */
1982static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
1983 const KernelInfo *kernel2)
1984{
cristybb503372010-05-27 20:51:26 +00001985 register size_t
anthony47f5d062010-05-23 07:47:50 +00001986 i;
anthony1d45eb92010-05-25 11:13:23 +00001987
1988 /* check size and origin location */
1989 if ( kernel1->width != kernel2->width
1990 || kernel1->height != kernel2->height
1991 || kernel1->x != kernel2->x
1992 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00001993 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00001994
1995 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00001996 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00001997 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00001998 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
1999 return MagickFalse;
2000 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2001 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002002 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002003 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2004 return MagickFalse;
2005 }
anthony1d45eb92010-05-25 11:13:23 +00002006
anthony47f5d062010-05-23 07:47:50 +00002007 return MagickTrue;
2008}
2009
2010static void ExpandKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002011{
2012 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002013 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002014 *last;
cristya9a61ad2010-05-13 12:47:41 +00002015
anthony3c10fc82010-05-13 02:40:51 +00002016 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002017 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002018 clone = CloneKernelInfo(last);
2019 RotateKernelInfo(clone, angle);
2020 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002021 break;
cristy84d9b552010-05-24 18:23:54 +00002022 last->next = clone;
2023 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002024 }
cristy84d9b552010-05-24 18:23:54 +00002025 clone = DestroyKernelInfo(clone); /* This was the same as the first - junk */
anthony47f5d062010-05-23 07:47:50 +00002026 return;
anthony3c10fc82010-05-13 02:40:51 +00002027}
2028
2029/*
2030%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2031% %
2032% %
2033% %
anthony46a369d2010-05-19 02:41:48 +00002034+ C a l c M e t a K e r n a l I n f o %
2035% %
2036% %
2037% %
2038%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039%
2040% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2041% using the kernel values. This should only ne used if it is not posible to
2042% calculate that meta-data in some easier way.
2043%
2044% It is important that the meta-data is correct before ScaleKernelInfo() is
2045% used to perform kernel normalization.
2046%
2047% The format of the CalcKernelMetaData method is:
2048%
2049% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2050%
2051% A description of each parameter follows:
2052%
2053% o kernel: the Morphology/Convolution kernel to modify
2054%
2055% WARNING: Minimum and Maximum values are assumed to include zero, even if
2056% zero is not part of the kernel (as in Gaussian Derived kernels). This
2057% however is not true for flat-shaped morphological kernels.
2058%
2059% WARNING: Only the specific kernel pointed to is modified, not a list of
2060% multiple kernels.
2061%
2062% This is an internal function and not expected to be useful outside this
2063% module. This could change however.
2064*/
2065static void CalcKernelMetaData(KernelInfo *kernel)
2066{
cristybb503372010-05-27 20:51:26 +00002067 register size_t
anthony46a369d2010-05-19 02:41:48 +00002068 i;
2069
2070 kernel->minimum = kernel->maximum = 0.0;
2071 kernel->negative_range = kernel->positive_range = 0.0;
2072 for (i=0; i < (kernel->width*kernel->height); i++)
2073 {
2074 if ( fabs(kernel->values[i]) < MagickEpsilon )
2075 kernel->values[i] = 0.0;
2076 ( kernel->values[i] < 0)
2077 ? ( kernel->negative_range += kernel->values[i] )
2078 : ( kernel->positive_range += kernel->values[i] );
2079 Minimize(kernel->minimum, kernel->values[i]);
2080 Maximize(kernel->maximum, kernel->values[i]);
2081 }
2082
2083 return;
2084}
2085
2086/*
2087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2088% %
2089% %
2090% %
anthony9eb4f742010-05-18 02:45:54 +00002091% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002092% %
2093% %
2094% %
2095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2096%
anthony9eb4f742010-05-18 02:45:54 +00002097% MorphologyApply() applies a morphological method, multiple times using
2098% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002099%
anthony9eb4f742010-05-18 02:45:54 +00002100% It is basically equivelent to as MorphologyImageChannel() (see below) but
2101% without user controls, that that function extracts and applies to kernels
2102% and morphology methods.
2103%
2104% More specifically kernels are not normalized/scaled/blended by the
2105% 'convolve:scale' Image Artifact (-set setting), and the convolve bias
2106% (-bias setting or image->bias) is passed directly to this function,
2107% and not extracted from an image.
anthony602ab9b2010-01-05 08:06:50 +00002108%
anthony47f5d062010-05-23 07:47:50 +00002109% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002110%
anthony9eb4f742010-05-18 02:45:54 +00002111% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002112% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002113% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002114% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002115%
2116% A description of each parameter follows:
2117%
2118% o image: the image.
2119%
2120% o method: the morphology method to be applied.
2121%
2122% o iterations: apply the operation this many times (or no change).
2123% A value of -1 means loop until no change found.
2124% How this is applied may depend on the morphology method.
2125% Typically this is a value of 1.
2126%
2127% o channel: the channel type.
2128%
2129% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002130% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002131%
anthony47f5d062010-05-23 07:47:50 +00002132% o compose: How to handle or merge multi-kernel results.
2133% If 'Undefined' use default of the Morphology method.
2134% If 'No' force image to be re-iterated by each kernel.
2135% Otherwise merge the results using the mathematical compose
2136% method given.
2137%
2138% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002139%
anthony602ab9b2010-01-05 08:06:50 +00002140% o exception: return any errors or warnings in this structure.
2141%
anthony602ab9b2010-01-05 08:06:50 +00002142*/
2143
anthony930be612010-02-08 04:26:15 +00002144
anthony9eb4f742010-05-18 02:45:54 +00002145/* Apply a Morphology Primative to an image using the given kernel.
2146** Two pre-created images must be provided, no image is created.
2147** Returning the number of pixels that changed.
2148*/
cristybb503372010-05-27 20:51:26 +00002149static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002150 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002151 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002152{
cristy2be15382010-01-21 02:38:03 +00002153#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002154
cristy5f959472010-05-27 22:19:46 +00002155 CacheView
2156 *p_view,
2157 *q_view;
2158
cristybb503372010-05-27 20:51:26 +00002159 ssize_t
anthony29188a82010-01-22 10:12:34 +00002160 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002161 changed;
2162
2163 MagickBooleanType
2164 status;
2165
cristy5f959472010-05-27 22:19:46 +00002166 MagickOffsetType
2167 progress;
anthony602ab9b2010-01-05 08:06:50 +00002168
anthony602ab9b2010-01-05 08:06:50 +00002169 status=MagickTrue;
2170 changed=0;
2171 progress=0;
2172
anthony602ab9b2010-01-05 08:06:50 +00002173 p_view=AcquireCacheView(image);
2174 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002175
anthonycc6c8362010-01-25 04:14:01 +00002176 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002177 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002178 */
cristyc99304f2010-02-01 15:26:27 +00002179 offx = kernel->x;
2180 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002181 switch(method) {
anthony930be612010-02-08 04:26:15 +00002182 case ConvolveMorphology:
2183 case DilateMorphology:
2184 case DilateIntensityMorphology:
2185 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002186 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002187 offx = (ssize_t) kernel->width-offx-1;
2188 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002189 break;
anthony5ef8e942010-05-11 06:51:12 +00002190 case ErodeMorphology:
2191 case ErodeIntensityMorphology:
2192 case HitAndMissMorphology:
2193 case ThinningMorphology:
2194 case ThickenMorphology:
2195 /* kernel is user as is, without reflection */
2196 break;
anthony930be612010-02-08 04:26:15 +00002197 default:
anthony9eb4f742010-05-18 02:45:54 +00002198 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002199 break;
anthony29188a82010-01-22 10:12:34 +00002200 }
2201
anthony602ab9b2010-01-05 08:06:50 +00002202#if defined(MAGICKCORE_OPENMP_SUPPORT)
2203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2204#endif
cristybb503372010-05-27 20:51:26 +00002205 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002206 {
2207 MagickBooleanType
2208 sync;
2209
2210 register const PixelPacket
2211 *restrict p;
2212
2213 register const IndexPacket
2214 *restrict p_indexes;
2215
2216 register PixelPacket
2217 *restrict q;
2218
2219 register IndexPacket
2220 *restrict q_indexes;
2221
cristybb503372010-05-27 20:51:26 +00002222 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002223 x;
2224
cristybb503372010-05-27 20:51:26 +00002225 size_t
anthony602ab9b2010-01-05 08:06:50 +00002226 r;
2227
2228 if (status == MagickFalse)
2229 continue;
anthony29188a82010-01-22 10:12:34 +00002230 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2231 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002232 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2233 exception);
2234 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2235 {
2236 status=MagickFalse;
2237 continue;
2238 }
2239 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2240 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002241 r = (image->columns+kernel->width)*offy+offx; /* constant */
2242
cristybb503372010-05-27 20:51:26 +00002243 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002244 {
cristybb503372010-05-27 20:51:26 +00002245 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002246 v;
2247
cristybb503372010-05-27 20:51:26 +00002248 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002249 u;
2250
2251 register const double
2252 *restrict k;
2253
2254 register const PixelPacket
2255 *restrict k_pixels;
2256
2257 register const IndexPacket
2258 *restrict k_indexes;
2259
2260 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002261 result,
2262 min,
2263 max;
anthony602ab9b2010-01-05 08:06:50 +00002264
anthony29188a82010-01-22 10:12:34 +00002265 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002266 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002267 */
anthony602ab9b2010-01-05 08:06:50 +00002268 *q = p[r];
2269 if (image->colorspace == CMYKColorspace)
2270 q_indexes[x] = p_indexes[r];
2271
anthony5ef8e942010-05-11 06:51:12 +00002272 /* Defaults */
2273 min.red =
2274 min.green =
2275 min.blue =
2276 min.opacity =
2277 min.index = (MagickRealType) QuantumRange;
2278 max.red =
2279 max.green =
2280 max.blue =
2281 max.opacity =
2282 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002283 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002284 result.red = (MagickRealType) p[r].red;
2285 result.green = (MagickRealType) p[r].green;
2286 result.blue = (MagickRealType) p[r].blue;
2287 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002288 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002289 if ( image->colorspace == CMYKColorspace)
2290 result.index = (MagickRealType) p_indexes[r];
2291
anthony602ab9b2010-01-05 08:06:50 +00002292 switch (method) {
2293 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002294 /* Set the user defined bias of the weighted average output */
2295 result.red =
2296 result.green =
2297 result.blue =
2298 result.opacity =
2299 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002300 break;
anthony4fd27e22010-02-07 08:17:18 +00002301 case DilateIntensityMorphology:
2302 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002303 /* use a boolean flag indicating when first match found */
2304 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002305 break;
anthony602ab9b2010-01-05 08:06:50 +00002306 default:
anthony602ab9b2010-01-05 08:06:50 +00002307 break;
2308 }
2309
2310 switch ( method ) {
2311 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002312 /* Weighted Average of pixels using reflected kernel
2313 **
2314 ** NOTE for correct working of this operation for asymetrical
2315 ** kernels, the kernel needs to be applied in its reflected form.
2316 ** That is its values needs to be reversed.
2317 **
2318 ** Correlation is actually the same as this but without reflecting
2319 ** the kernel, and thus 'lower-level' that Convolution. However
2320 ** as Convolution is the more common method used, and it does not
2321 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002322 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002323 **
2324 ** Correlation will have its kernel reflected before calling
2325 ** this function to do a Convolve.
2326 **
2327 ** For more details of Correlation vs Convolution see
2328 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2329 */
anthony5ef8e942010-05-11 06:51:12 +00002330 if (((channel & SyncChannels) != 0 ) &&
2331 (image->matte == MagickTrue))
2332 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2333 ** Weight the color channels with Alpha Channel so that
2334 ** transparent pixels are not part of the results.
2335 */
anthony602ab9b2010-01-05 08:06:50 +00002336 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002337 alpha, /* color channel weighting : kernel*alpha */
2338 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002339
2340 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002341 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002342 k_pixels = p;
2343 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002344 for (v=0; v < (ssize_t) kernel->height; v++) {
2345 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002346 if ( IsNan(*k) ) continue;
2347 alpha=(*k)*(QuantumScale*(QuantumRange-
2348 k_pixels[u].opacity));
2349 gamma += alpha;
2350 result.red += alpha*k_pixels[u].red;
2351 result.green += alpha*k_pixels[u].green;
2352 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002353 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002354 if ( image->colorspace == CMYKColorspace)
2355 result.index += alpha*k_indexes[u];
2356 }
2357 k_pixels += image->columns+kernel->width;
2358 k_indexes += image->columns+kernel->width;
2359 }
2360 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002361 result.red *= gamma;
2362 result.green *= gamma;
2363 result.blue *= gamma;
2364 result.opacity *= gamma;
2365 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002366 }
anthony5ef8e942010-05-11 06:51:12 +00002367 else
2368 {
2369 /* No 'Sync' flag, or no Alpha involved.
2370 ** Convolution is simple individual channel weigthed sum.
2371 */
2372 k = &kernel->values[ kernel->width*kernel->height-1 ];
2373 k_pixels = p;
2374 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002375 for (v=0; v < (ssize_t) kernel->height; v++) {
2376 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002377 if ( IsNan(*k) ) continue;
2378 result.red += (*k)*k_pixels[u].red;
2379 result.green += (*k)*k_pixels[u].green;
2380 result.blue += (*k)*k_pixels[u].blue;
2381 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2382 if ( image->colorspace == CMYKColorspace)
2383 result.index += (*k)*k_indexes[u];
2384 }
2385 k_pixels += image->columns+kernel->width;
2386 k_indexes += image->columns+kernel->width;
2387 }
2388 }
anthony602ab9b2010-01-05 08:06:50 +00002389 break;
2390
anthony4fd27e22010-02-07 08:17:18 +00002391 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002392 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002393 **
2394 ** NOTE that the kernel is not reflected for this operation!
2395 **
2396 ** NOTE: in normal Greyscale Morphology, the kernel value should
2397 ** be added to the real value, this is currently not done, due to
2398 ** the nature of the boolean kernels being used.
2399 */
anthony4fd27e22010-02-07 08:17:18 +00002400 k = kernel->values;
2401 k_pixels = p;
2402 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002403 for (v=0; v < (ssize_t) kernel->height; v++) {
2404 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002405 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002406 Minimize(min.red, (double) k_pixels[u].red);
2407 Minimize(min.green, (double) k_pixels[u].green);
2408 Minimize(min.blue, (double) k_pixels[u].blue);
2409 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002410 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002411 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002412 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002413 }
2414 k_pixels += image->columns+kernel->width;
2415 k_indexes += image->columns+kernel->width;
2416 }
2417 break;
2418
anthony5ef8e942010-05-11 06:51:12 +00002419
anthony83ba99b2010-01-24 08:48:15 +00002420 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002421 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002422 **
2423 ** NOTE for correct working of this operation for asymetrical
2424 ** kernels, the kernel needs to be applied in its reflected form.
2425 ** That is its values needs to be reversed.
2426 **
2427 ** NOTE: in normal Greyscale Morphology, the kernel value should
2428 ** be added to the real value, this is currently not done, due to
2429 ** the nature of the boolean kernels being used.
2430 **
2431 */
anthony29188a82010-01-22 10:12:34 +00002432 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002433 k_pixels = p;
2434 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002435 for (v=0; v < (ssize_t) kernel->height; v++) {
2436 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002437 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002438 Maximize(max.red, (double) k_pixels[u].red);
2439 Maximize(max.green, (double) k_pixels[u].green);
2440 Maximize(max.blue, (double) k_pixels[u].blue);
2441 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002442 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002443 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002444 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002445 }
2446 k_pixels += image->columns+kernel->width;
2447 k_indexes += image->columns+kernel->width;
2448 }
anthony602ab9b2010-01-05 08:06:50 +00002449 break;
2450
anthony5ef8e942010-05-11 06:51:12 +00002451 case HitAndMissMorphology:
2452 case ThinningMorphology:
2453 case ThickenMorphology:
2454 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2455 **
2456 ** NOTE that the kernel is not reflected for this operation,
2457 ** and consists of both foreground and background pixel
2458 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2459 ** with either Nan or 0.5 values for don't care.
2460 **
2461 ** Note that this can produce negative results, though really
2462 ** only a positive match has any real value.
2463 */
2464 k = kernel->values;
2465 k_pixels = p;
2466 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002467 for (v=0; v < (ssize_t) kernel->height; v++) {
2468 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002469 if ( IsNan(*k) ) continue;
2470 if ( (*k) > 0.7 )
2471 { /* minimim of foreground pixels */
2472 Minimize(min.red, (double) k_pixels[u].red);
2473 Minimize(min.green, (double) k_pixels[u].green);
2474 Minimize(min.blue, (double) k_pixels[u].blue);
2475 Minimize(min.opacity,
2476 QuantumRange-(double) k_pixels[u].opacity);
2477 if ( image->colorspace == CMYKColorspace)
2478 Minimize(min.index, (double) k_indexes[u]);
2479 }
2480 else if ( (*k) < 0.3 )
2481 { /* maximum of background pixels */
2482 Maximize(max.red, (double) k_pixels[u].red);
2483 Maximize(max.green, (double) k_pixels[u].green);
2484 Maximize(max.blue, (double) k_pixels[u].blue);
2485 Maximize(max.opacity,
2486 QuantumRange-(double) k_pixels[u].opacity);
2487 if ( image->colorspace == CMYKColorspace)
2488 Maximize(max.index, (double) k_indexes[u]);
2489 }
2490 }
2491 k_pixels += image->columns+kernel->width;
2492 k_indexes += image->columns+kernel->width;
2493 }
2494 /* Pattern Match only if min fg larger than min bg pixels */
2495 min.red -= max.red; Maximize( min.red, 0.0 );
2496 min.green -= max.green; Maximize( min.green, 0.0 );
2497 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2498 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2499 min.index -= max.index; Maximize( min.index, 0.0 );
2500 break;
2501
anthony4fd27e22010-02-07 08:17:18 +00002502 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002503 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2504 **
2505 ** WARNING: the intensity test fails for CMYK and does not
2506 ** take into account the moderating effect of teh alpha channel
2507 ** on the intensity.
2508 **
2509 ** NOTE that the kernel is not reflected for this operation!
2510 */
anthony602ab9b2010-01-05 08:06:50 +00002511 k = kernel->values;
2512 k_pixels = p;
2513 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002514 for (v=0; v < (ssize_t) kernel->height; v++) {
2515 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002516 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002517 if ( result.red == 0.0 ||
2518 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2519 /* copy the whole pixel - no channel selection */
2520 *q = k_pixels[u];
2521 if ( result.red > 0.0 ) changed++;
2522 result.red = 1.0;
2523 }
anthony602ab9b2010-01-05 08:06:50 +00002524 }
2525 k_pixels += image->columns+kernel->width;
2526 k_indexes += image->columns+kernel->width;
2527 }
anthony602ab9b2010-01-05 08:06:50 +00002528 break;
2529
anthony83ba99b2010-01-24 08:48:15 +00002530 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002531 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2532 **
2533 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002534 ** take into account the moderating effect of the alpha channel
2535 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002536 **
2537 ** NOTE for correct working of this operation for asymetrical
2538 ** kernels, the kernel needs to be applied in its reflected form.
2539 ** That is its values needs to be reversed.
2540 */
anthony29188a82010-01-22 10:12:34 +00002541 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002542 k_pixels = p;
2543 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002544 for (v=0; v < (ssize_t) kernel->height; v++) {
2545 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002546 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2547 if ( result.red == 0.0 ||
2548 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2549 /* copy the whole pixel - no channel selection */
2550 *q = k_pixels[u];
2551 if ( result.red > 0.0 ) changed++;
2552 result.red = 1.0;
2553 }
anthony602ab9b2010-01-05 08:06:50 +00002554 }
2555 k_pixels += image->columns+kernel->width;
2556 k_indexes += image->columns+kernel->width;
2557 }
anthony602ab9b2010-01-05 08:06:50 +00002558 break;
2559
anthony5ef8e942010-05-11 06:51:12 +00002560
anthony602ab9b2010-01-05 08:06:50 +00002561 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002562 /* Add kernel Value and select the minimum value found.
2563 ** The result is a iterative distance from edge of image shape.
2564 **
2565 ** All Distance Kernels are symetrical, but that may not always
2566 ** be the case. For example how about a distance from left edges?
2567 ** To work correctly with asymetrical kernels the reflected kernel
2568 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002569 **
2570 ** Actually this is really a GreyErode with a negative kernel!
2571 **
anthony930be612010-02-08 04:26:15 +00002572 */
anthony29188a82010-01-22 10:12:34 +00002573 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002574 k_pixels = p;
2575 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002576 for (v=0; v < (ssize_t) kernel->height; v++) {
2577 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002578 if ( IsNan(*k) ) continue;
2579 Minimize(result.red, (*k)+k_pixels[u].red);
2580 Minimize(result.green, (*k)+k_pixels[u].green);
2581 Minimize(result.blue, (*k)+k_pixels[u].blue);
2582 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2583 if ( image->colorspace == CMYKColorspace)
2584 Minimize(result.index, (*k)+k_indexes[u]);
2585 }
2586 k_pixels += image->columns+kernel->width;
2587 k_indexes += image->columns+kernel->width;
2588 }
anthony602ab9b2010-01-05 08:06:50 +00002589 break;
2590
2591 case UndefinedMorphology:
2592 default:
2593 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002594 }
anthony5ef8e942010-05-11 06:51:12 +00002595 /* Final mathematics of results (combine with original image?)
2596 **
2597 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2598 ** be done here but works better with iteration as a image difference
2599 ** in the controling function (below). Thicken and Thinning however
2600 ** should be done here so thay can be iterated correctly.
2601 */
2602 switch ( method ) {
2603 case HitAndMissMorphology:
2604 case ErodeMorphology:
2605 result = min; /* minimum of neighbourhood */
2606 break;
2607 case DilateMorphology:
2608 result = max; /* maximum of neighbourhood */
2609 break;
2610 case ThinningMorphology:
2611 /* subtract pattern match from original */
2612 result.red -= min.red;
2613 result.green -= min.green;
2614 result.blue -= min.blue;
2615 result.opacity -= min.opacity;
2616 result.index -= min.index;
2617 break;
2618 case ThickenMorphology:
2619 /* Union with original image (maximize) - or should this be + */
2620 Maximize( result.red, min.red );
2621 Maximize( result.green, min.green );
2622 Maximize( result.blue, min.blue );
2623 Maximize( result.opacity, min.opacity );
2624 Maximize( result.index, min.index );
2625 break;
2626 default:
2627 /* result directly calculated or assigned */
2628 break;
2629 }
2630 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002631 switch ( method ) {
2632 case UndefinedMorphology:
2633 case DilateIntensityMorphology:
2634 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002635 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002636 default:
anthony83ba99b2010-01-24 08:48:15 +00002637 if ((channel & RedChannel) != 0)
2638 q->red = ClampToQuantum(result.red);
2639 if ((channel & GreenChannel) != 0)
2640 q->green = ClampToQuantum(result.green);
2641 if ((channel & BlueChannel) != 0)
2642 q->blue = ClampToQuantum(result.blue);
2643 if ((channel & OpacityChannel) != 0
2644 && image->matte == MagickTrue )
2645 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2646 if ((channel & IndexChannel) != 0
2647 && image->colorspace == CMYKColorspace)
2648 q_indexes[x] = ClampToQuantum(result.index);
2649 break;
2650 }
anthony5ef8e942010-05-11 06:51:12 +00002651 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002652 if ( ( p[r].red != q->red )
2653 || ( p[r].green != q->green )
2654 || ( p[r].blue != q->blue )
2655 || ( p[r].opacity != q->opacity )
2656 || ( image->colorspace == CMYKColorspace &&
2657 p_indexes[r] != q_indexes[x] ) )
2658 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002659 p++;
2660 q++;
anthony83ba99b2010-01-24 08:48:15 +00002661 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002662 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2663 if (sync == MagickFalse)
2664 status=MagickFalse;
2665 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2666 {
2667 MagickBooleanType
2668 proceed;
2669
2670#if defined(MAGICKCORE_OPENMP_SUPPORT)
2671 #pragma omp critical (MagickCore_MorphologyImage)
2672#endif
2673 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2674 if (proceed == MagickFalse)
2675 status=MagickFalse;
2676 }
anthony83ba99b2010-01-24 08:48:15 +00002677 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002678 result_image->type=image->type;
2679 q_view=DestroyCacheView(q_view);
2680 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002681 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002682}
2683
anthony4fd27e22010-02-07 08:17:18 +00002684
anthony9eb4f742010-05-18 02:45:54 +00002685MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002686 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002687 const KernelInfo *kernel, const CompositeOperator compose,
2688 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002689{
2690 Image
anthony47f5d062010-05-23 07:47:50 +00002691 *curr_image, /* Image we are working with or iterating */
2692 *work_image, /* secondary image for primative iteration */
2693 *save_image, /* saved image - for 'edge' method only */
2694 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002695
anthony4fd27e22010-02-07 08:17:18 +00002696 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002697 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2698 *norm_kernel, /* the current normal un-reflected kernel */
2699 *rflt_kernel, /* the current reflected kernel (if needed) */
2700 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002701
2702 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002703 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002704
2705 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002706 rslt_compose; /* multi-kernel compose method for results to use */
2707
2708 MagickBooleanType
2709 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002710
cristybb503372010-05-27 20:51:26 +00002711 size_t
anthony47f5d062010-05-23 07:47:50 +00002712 method_loop, /* Loop 1: number of compound method iterations */
2713 method_limit, /* maximum number of compound method iterations */
2714 kernel_number, /* Loop 2: the kernel number being applied */
2715 stage_loop, /* Loop 3: primative loop for compound morphology */
2716 stage_limit, /* how many primatives in this compound */
2717 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2718 kernel_limit, /* number of times to iterate kernel */
2719 count, /* total count of primative steps applied */
2720 changed, /* number pixels changed by last primative operation */
2721 kernel_changed, /* total count of changed using iterated kernel */
2722 method_changed; /* total count of changed over method iteration */
2723
2724 char
2725 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002726
anthony602ab9b2010-01-05 08:06:50 +00002727 assert(image != (Image *) NULL);
2728 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002729 assert(kernel != (KernelInfo *) NULL);
2730 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002731 assert(exception != (ExceptionInfo *) NULL);
2732 assert(exception->signature == MagickSignature);
2733
anthonyc3e48252010-05-24 12:43:11 +00002734 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002735 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002736 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002737
cristybb503372010-05-27 20:51:26 +00002738 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002739 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2740 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002741
cristye96405a2010-05-19 02:24:31 +00002742 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2743 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002744
anthony9eb4f742010-05-18 02:45:54 +00002745 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002746 curr_image = (Image *) image;
2747 work_image = save_image = rslt_image = (Image *) NULL;
2748 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002749
anthony47f5d062010-05-23 07:47:50 +00002750 /* Initialize specific methods
2751 * + which loop should use the given iteratations
2752 * + how many primatives make up the compound morphology
2753 * + multi-kernel compose method to use (by default)
2754 */
2755 method_limit = 1; /* just do method once, unless otherwise set */
2756 stage_limit = 1; /* assume method is not a compount */
2757 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002758 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002759 case SmoothMorphology: /* 4 primative compound morphology */
2760 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002761 break;
anthony47f5d062010-05-23 07:47:50 +00002762 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002763 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002764 case TopHatMorphology:
2765 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002766 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002767 case BottomHatMorphology:
2768 case EdgeMorphology:
2769 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002770 break;
2771 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002772 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002773 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002774 break;
anthonyc3e48252010-05-24 12:43:11 +00002775 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002776 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002777 case DistanceMorphology:
2778 method_limit = kernel_limit; /* iterate method with each kernel */
2779 kernel_limit = 1; /* do not do kernel iteration */
2780 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002781 break;
2782 default:
anthony930be612010-02-08 04:26:15 +00002783 break;
anthony602ab9b2010-01-05 08:06:50 +00002784 }
2785
anthonyc3e48252010-05-24 12:43:11 +00002786 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002787 if ( compose != UndefinedCompositeOp )
2788 rslt_compose = compose; /* override default composition for method */
2789 if ( rslt_compose == UndefinedCompositeOp )
2790 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2791
anthonyc3e48252010-05-24 12:43:11 +00002792 /* Some methods require a reflected kernel to use with primatives.
2793 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002794 switch ( method ) {
2795 case CorrelateMorphology:
2796 case CloseMorphology:
2797 case CloseIntensityMorphology:
2798 case BottomHatMorphology:
2799 case SmoothMorphology:
2800 reflected_kernel = CloneKernelInfo(kernel);
2801 if (reflected_kernel == (KernelInfo *) NULL)
2802 goto error_cleanup;
2803 RotateKernelInfo(reflected_kernel,180);
2804 break;
2805 default:
2806 break;
anthony9eb4f742010-05-18 02:45:54 +00002807 }
anthony7a01dcf2010-05-11 12:25:52 +00002808
anthony47f5d062010-05-23 07:47:50 +00002809 /* Loop 1: iterate the compound method */
2810 method_loop = 0;
2811 method_changed = 1;
2812 while ( method_loop < method_limit && method_changed > 0 ) {
2813 method_loop++;
2814 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00002815
anthony47f5d062010-05-23 07:47:50 +00002816 /* Loop 2: iterate over each kernel in a multi-kernel list */
2817 norm_kernel = (KernelInfo *) kernel;
2818 rflt_kernel = reflected_kernel;
2819 kernel_number = 0;
2820 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00002821
anthony47f5d062010-05-23 07:47:50 +00002822 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
2823 stage_loop = 0; /* the compound morphology stage number */
2824 while ( stage_loop < stage_limit ) {
2825 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002826
anthony47f5d062010-05-23 07:47:50 +00002827 /* Select primative morphology for this stage of compound method */
2828 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00002829 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00002830 switch( method ) {
2831 case ErodeMorphology: /* just erode */
2832 case EdgeInMorphology: /* erode and image difference */
2833 primative = ErodeMorphology;
2834 break;
2835 case DilateMorphology: /* just dilate */
2836 case EdgeOutMorphology: /* dilate and image difference */
2837 primative = DilateMorphology;
2838 break;
2839 case OpenMorphology: /* erode then dialate */
2840 case TopHatMorphology: /* open and image difference */
2841 primative = ErodeMorphology;
2842 if ( stage_loop == 2 )
2843 primative = DilateMorphology;
2844 break;
2845 case OpenIntensityMorphology:
2846 primative = ErodeIntensityMorphology;
2847 if ( stage_loop == 2 )
2848 primative = DilateIntensityMorphology;
2849 case CloseMorphology: /* dilate, then erode */
2850 case BottomHatMorphology: /* close and image difference */
2851 this_kernel = rflt_kernel; /* use the reflected kernel */
2852 primative = DilateMorphology;
2853 if ( stage_loop == 2 )
2854 primative = ErodeMorphology;
2855 break;
2856 case CloseIntensityMorphology:
2857 this_kernel = rflt_kernel; /* use the reflected kernel */
2858 primative = DilateIntensityMorphology;
2859 if ( stage_loop == 2 )
2860 primative = ErodeIntensityMorphology;
2861 break;
2862 case SmoothMorphology: /* open, close */
2863 switch ( stage_loop ) {
2864 case 1: /* start an open method, which starts with Erode */
2865 primative = ErodeMorphology;
2866 break;
2867 case 2: /* now Dilate the Erode */
2868 primative = DilateMorphology;
2869 break;
2870 case 3: /* Reflect kernel a close */
2871 this_kernel = rflt_kernel; /* use the reflected kernel */
2872 primative = DilateMorphology;
2873 break;
2874 case 4: /* Finish the Close */
2875 this_kernel = rflt_kernel; /* use the reflected kernel */
2876 primative = ErodeMorphology;
2877 break;
2878 }
2879 break;
2880 case EdgeMorphology: /* dilate and erode difference */
2881 primative = DilateMorphology;
2882 if ( stage_loop == 2 ) {
2883 save_image = curr_image; /* save the image difference */
2884 curr_image = (Image *) image;
2885 primative = ErodeMorphology;
2886 }
2887 break;
2888 case CorrelateMorphology:
2889 /* A Correlation is a Convolution with a reflected kernel.
2890 ** However a Convolution is a weighted sum using a reflected
2891 ** kernel. It may seem stange to convert a Correlation into a
2892 ** Convolution as the Correlation is the simplier method, but
2893 ** Convolution is much more commonly used, and it makes sense to
2894 ** implement it directly so as to avoid the need to duplicate the
2895 ** kernel when it is not required (which is typically the
2896 ** default).
2897 */
2898 this_kernel = rflt_kernel; /* use the reflected kernel */
2899 primative = ConvolveMorphology;
2900 break;
2901 default:
anthony47f5d062010-05-23 07:47:50 +00002902 break;
2903 }
anthony9eb4f742010-05-18 02:45:54 +00002904
anthony47f5d062010-05-23 07:47:50 +00002905 /* Extra information for debugging compound operations */
2906 if ( verbose == MagickTrue ) {
2907 if ( stage_limit > 1 )
cristydc1c30b2010-05-23 14:23:12 +00002908 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu.%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002909 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2910 method_loop, stage_loop );
2911 else if ( primative != method )
cristydc1c30b2010-05-23 14:23:12 +00002912 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%lu -> ",
anthony47f5d062010-05-23 07:47:50 +00002913 MagickOptionToMnemonic(MagickMorphologyOptions, method),
2914 method_loop );
2915 else
2916 v_info[0] = '\0';
2917 }
2918
2919 /* Loop 4: Iterate the kernel with primative */
2920 kernel_loop = 0;
2921 kernel_changed = 0;
2922 changed = 1;
2923 while ( kernel_loop < kernel_limit && changed > 0 ) {
2924 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00002925
2926 /* Create a destination image, if not yet defined */
2927 if ( work_image == (Image *) NULL )
2928 {
2929 work_image=CloneImage(image,0,0,MagickTrue,exception);
2930 if (work_image == (Image *) NULL)
2931 goto error_cleanup;
2932 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
2933 {
2934 InheritException(exception,&work_image->exception);
2935 goto error_cleanup;
2936 }
2937 }
2938
anthony47f5d062010-05-23 07:47:50 +00002939 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00002940 count++;
anthony47f5d062010-05-23 07:47:50 +00002941 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00002942 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00002943 kernel_changed += changed;
2944 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00002945
anthony47f5d062010-05-23 07:47:50 +00002946 if ( verbose == MagickTrue ) {
2947 if ( kernel_loop > 1 )
2948 fprintf(stderr, "\n"); /* add end-of-line from previous */
2949 fprintf(stderr, "%s%s%s:%lu.%lu #%lu => Changed %lu", v_info,
2950 MagickOptionToMnemonic(MagickMorphologyOptions, primative),
2951 ( this_kernel == rflt_kernel ) ? "*" : "",
2952 method_loop+kernel_loop-1, kernel_number, count, changed);
2953 }
anthony9eb4f742010-05-18 02:45:54 +00002954 /* prepare next loop */
2955 { Image *tmp = work_image; /* swap images for iteration */
2956 work_image = curr_image;
2957 curr_image = tmp;
2958 }
2959 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00002960 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00002961
anthony47f5d062010-05-23 07:47:50 +00002962 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00002963
anthony47f5d062010-05-23 07:47:50 +00002964 if ( verbose == MagickTrue && kernel_changed != changed )
2965 fprintf(stderr, " Total %lu", kernel_changed);
2966 if ( verbose == MagickTrue && stage_loop < stage_limit )
2967 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00002968
2969#if 0
cristybb503372010-05-27 20:51:26 +00002970 fprintf(stderr, "--E-- image=0x%lx\n", (size_t)image);
2971 fprintf(stderr, " curr =0x%lx\n", (size_t)curr_image);
2972 fprintf(stderr, " work =0x%lx\n", (size_t)work_image);
2973 fprintf(stderr, " save =0x%lx\n", (size_t)save_image);
2974 fprintf(stderr, " union=0x%lx\n", (size_t)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00002975#endif
2976
anthony47f5d062010-05-23 07:47:50 +00002977 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00002978
anthony47f5d062010-05-23 07:47:50 +00002979 /* Final Post-processing for some Compound Methods
2980 **
2981 ** The removal of any 'Sync' channel flag in the Image Compositon
2982 ** below ensures the methematical compose method is applied in a
2983 ** purely mathematical way, and only to the selected channels.
2984 ** Turn off SVG composition 'alpha blending'.
2985 */
2986 switch( method ) {
2987 case EdgeOutMorphology:
2988 case EdgeInMorphology:
2989 case TopHatMorphology:
2990 case BottomHatMorphology:
2991 if ( verbose == MagickTrue )
2992 fprintf(stderr, "\n%s: Difference with original image",
2993 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
2994 (void) CompositeImageChannel(curr_image,
2995 (ChannelType) (channel & ~SyncChannels),
2996 DifferenceCompositeOp, image, 0, 0);
2997 break;
2998 case EdgeMorphology:
2999 if ( verbose == MagickTrue )
3000 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3001 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3002 (void) CompositeImageChannel(curr_image,
3003 (ChannelType) (channel & ~SyncChannels),
3004 DifferenceCompositeOp, save_image, 0, 0);
3005 save_image = DestroyImage(save_image); /* finished with save image */
3006 break;
3007 default:
3008 break;
3009 }
3010
3011 /* multi-kernel handling: re-iterate, or compose results */
3012 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003013 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003014 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003015 { if ( verbose == MagickTrue ) {
3016 if ( this_kernel->next != (KernelInfo *) NULL )
3017 fprintf(stderr, " (re-iterate)");
3018 else
3019 fprintf(stderr, " (done)");
3020 }
3021 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003022 }
anthony47f5d062010-05-23 07:47:50 +00003023 else if ( rslt_image == (Image *) NULL)
3024 { if ( verbose == MagickTrue )
3025 fprintf(stderr, " (save for compose)");
3026 rslt_image = curr_image;
3027 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003028 }
anthony47f5d062010-05-23 07:47:50 +00003029 else
3030 { /* add the new 'current' result to the composition
3031 **
3032 ** The removal of any 'Sync' channel flag in the Image Compositon
3033 ** below ensures the methematical compose method is applied in a
3034 ** purely mathematical way, and only to the selected channels.
3035 ** Turn off SVG composition 'alpha blending'.
3036 */
3037 if ( verbose == MagickTrue )
3038 fprintf(stderr, " (compose \"%s\")",
3039 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3040 (void) CompositeImageChannel(rslt_image,
3041 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3042 curr_image, 0, 0);
3043 curr_image = (Image *) image; /* continue with original image */
3044 }
3045 if ( verbose == MagickTrue )
3046 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003047
anthony47f5d062010-05-23 07:47:50 +00003048 /* loop to the next kernel in a multi-kernel list */
3049 norm_kernel = norm_kernel->next;
3050 if ( rflt_kernel != (KernelInfo *) NULL )
3051 rflt_kernel = rflt_kernel->next;
3052 kernel_number++;
3053 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003054
anthony47f5d062010-05-23 07:47:50 +00003055 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003056
anthony9eb4f742010-05-18 02:45:54 +00003057 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003058
anthony47f5d062010-05-23 07:47:50 +00003059 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003060error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003061 if ( curr_image != (Image *) NULL &&
3062 curr_image != rslt_image &&
3063 curr_image != image )
3064 curr_image = DestroyImage(curr_image);
3065 if ( rslt_image != (Image *) NULL )
3066 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003067exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003068 if ( curr_image != (Image *) NULL &&
3069 curr_image != rslt_image &&
3070 curr_image != image )
3071 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003072 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003073 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003074 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003075 save_image = DestroyImage(save_image);
3076 if ( reflected_kernel != (KernelInfo *) NULL )
3077 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3078 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003079}
3080
3081/*
3082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3083% %
3084% %
3085% %
3086% M o r p h o l o g y I m a g e C h a n n e l %
3087% %
3088% %
3089% %
3090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3091%
3092% MorphologyImageChannel() applies a user supplied kernel to the image
3093% according to the given mophology method.
3094%
3095% This function applies any and all user defined settings before calling
3096% the above internal function MorphologyApply().
3097%
3098% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003099% * Output Bias for Convolution and correlation ("-bias")
3100% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3101% This can also includes the addition of a scaled unity kernel.
3102% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003103%
3104% The format of the MorphologyImage method is:
3105%
3106% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003107% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003108%
3109% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003110% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003111% KernelInfo *kernel,ExceptionInfo *exception)
3112%
3113% A description of each parameter follows:
3114%
3115% o image: the image.
3116%
3117% o method: the morphology method to be applied.
3118%
3119% o iterations: apply the operation this many times (or no change).
3120% A value of -1 means loop until no change found.
3121% How this is applied may depend on the morphology method.
3122% Typically this is a value of 1.
3123%
3124% o channel: the channel type.
3125%
3126% o kernel: An array of double representing the morphology kernel.
3127% Warning: kernel may be normalized for the Convolve method.
3128%
3129% o exception: return any errors or warnings in this structure.
3130%
3131*/
3132
3133MagickExport Image *MorphologyImageChannel(const Image *image,
3134 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003135 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003136{
3137 const char
3138 *artifact;
3139
3140 KernelInfo
3141 *curr_kernel;
3142
anthony47f5d062010-05-23 07:47:50 +00003143 CompositeOperator
3144 compose;
3145
anthony9eb4f742010-05-18 02:45:54 +00003146 Image
3147 *morphology_image;
3148
3149
anthony46a369d2010-05-19 02:41:48 +00003150 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3151 * This is done BEFORE the ShowKernelInfo() function is called so that
3152 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003153 */
3154 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003155 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003156 {
3157 artifact = GetImageArtifact(image,"convolve:scale");
3158 if ( artifact != (char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003159 if ( curr_kernel == kernel )
3160 curr_kernel = CloneKernelInfo(kernel);
3161 if (curr_kernel == (KernelInfo *) NULL) {
3162 curr_kernel=DestroyKernelInfo(curr_kernel);
3163 return((Image *) NULL);
3164 }
anthony46a369d2010-05-19 02:41:48 +00003165 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003166 }
3167 }
3168
3169 /* display the (normalized) kernel via stderr */
3170 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003171 if ( artifact == (const char *) NULL)
3172 artifact = GetImageArtifact(image,"convolve:showkernel");
3173 if ( artifact == (const char *) NULL)
3174 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003175 if ( artifact != (const char *) NULL)
3176 ShowKernelInfo(curr_kernel);
3177
anthony47f5d062010-05-23 07:47:50 +00003178 /* override the default handling of multi-kernel morphology results
3179 * if 'Undefined' use the default method
3180 * if 'None' (default for 'Convolve') re-iterate previous result
3181 * otherwise merge resulting images using compose method given
3182 */
3183 compose = UndefinedCompositeOp; /* use default for method */
3184 artifact = GetImageArtifact(image,"morphology:compose");
3185 if ( artifact != (const char *) NULL)
3186 compose = (CompositeOperator) ParseMagickOption(
3187 MagickComposeOptions,MagickFalse,artifact);
3188
anthony9eb4f742010-05-18 02:45:54 +00003189 /* Apply the Morphology */
3190 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003191 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003192
3193 /* Cleanup and Exit */
3194 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003195 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003196 return(morphology_image);
3197}
3198
3199MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003200 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003201 *exception)
3202{
3203 Image
3204 *morphology_image;
3205
3206 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3207 iterations,kernel,exception);
3208 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003209}
anthony83ba99b2010-01-24 08:48:15 +00003210
3211/*
3212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3213% %
3214% %
3215% %
anthony4fd27e22010-02-07 08:17:18 +00003216+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003217% %
3218% %
3219% %
3220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3221%
anthony46a369d2010-05-19 02:41:48 +00003222% RotateKernelInfo() rotates the kernel by the angle given.
3223%
3224% Currently it is restricted to 90 degree angles, of either 1D kernels
3225% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3226% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003227%
anthony4fd27e22010-02-07 08:17:18 +00003228% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003229%
anthony4fd27e22010-02-07 08:17:18 +00003230% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003231%
3232% A description of each parameter follows:
3233%
3234% o kernel: the Morphology/Convolution kernel
3235%
3236% o angle: angle to rotate in degrees
3237%
anthony46a369d2010-05-19 02:41:48 +00003238% This function is currently internal to this module only, but can be exported
3239% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003240*/
anthony4fd27e22010-02-07 08:17:18 +00003241static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003242{
anthony1b2bc0a2010-05-12 05:25:22 +00003243 /* angle the lower kernels first */
3244 if ( kernel->next != (KernelInfo *) NULL)
3245 RotateKernelInfo(kernel->next, angle);
3246
anthony83ba99b2010-01-24 08:48:15 +00003247 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3248 **
3249 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3250 */
3251
3252 /* Modulus the angle */
3253 angle = fmod(angle, 360.0);
3254 if ( angle < 0 )
3255 angle += 360.0;
3256
anthony3c10fc82010-05-13 02:40:51 +00003257 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003258 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003259
anthony3dd0f622010-05-13 12:57:32 +00003260 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003261 switch (kernel->type) {
3262 /* These built-in kernels are cylindrical kernels, rotating is useless */
3263 case GaussianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003264 case DOGKernel:
3265 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003266 case PeaksKernel:
3267 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003268 case ChebyshevKernel:
3269 case ManhattenKernel:
3270 case EuclideanKernel:
3271 return;
3272
3273 /* These may be rotatable at non-90 angles in the future */
3274 /* but simply rotating them in multiples of 90 degrees is useless */
3275 case SquareKernel:
3276 case DiamondKernel:
3277 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003278 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003279 return;
3280
3281 /* These only allows a +/-90 degree rotation (by transpose) */
3282 /* A 180 degree rotation is useless */
3283 case BlurKernel:
3284 case RectangleKernel:
3285 if ( 135.0 < angle && angle <= 225.0 )
3286 return;
3287 if ( 225.0 < angle && angle <= 315.0 )
3288 angle -= 180;
3289 break;
3290
anthony3dd0f622010-05-13 12:57:32 +00003291 default:
anthony83ba99b2010-01-24 08:48:15 +00003292 break;
3293 }
anthony3c10fc82010-05-13 02:40:51 +00003294 /* Attempt rotations by 45 degrees */
3295 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3296 {
3297 if ( kernel->width == 3 && kernel->height == 3 )
3298 { /* Rotate a 3x3 square by 45 degree angle */
3299 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003300 kernel->values[0] = kernel->values[3];
3301 kernel->values[3] = kernel->values[6];
3302 kernel->values[6] = kernel->values[7];
3303 kernel->values[7] = kernel->values[8];
3304 kernel->values[8] = kernel->values[5];
3305 kernel->values[5] = kernel->values[2];
3306 kernel->values[2] = kernel->values[1];
3307 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003308 /* rotate non-centered origin */
3309 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003310 ssize_t x,y;
3311 x = (ssize_t) kernel->x-1;
3312 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003313 if ( x == y ) x = 0;
3314 else if ( x == 0 ) x = -y;
3315 else if ( x == -y ) y = 0;
3316 else if ( y == 0 ) y = x;
cristybb503372010-05-27 20:51:26 +00003317 kernel->x = (size_t) x+1;
3318 kernel->y = (size_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003319 }
anthony43c49252010-05-18 10:59:50 +00003320 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3321 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003322 }
3323 else
3324 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3325 }
3326 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3327 {
3328 if ( kernel->width == 1 || kernel->height == 1 )
3329 { /* Do a transpose of the image, which results in a 90
3330 ** degree rotation of a 1 dimentional kernel
3331 */
cristybb503372010-05-27 20:51:26 +00003332 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003333 t;
cristybb503372010-05-27 20:51:26 +00003334 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003335 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003336 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003337 t = kernel->x;
3338 kernel->x = kernel->y;
3339 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003340 if ( kernel->width == 1 ) {
3341 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3342 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3343 } else {
3344 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3345 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3346 }
anthony3c10fc82010-05-13 02:40:51 +00003347 }
3348 else if ( kernel->width == kernel->height )
3349 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003350 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003351 i,j,x,y;
3352 register MagickRealType
3353 *k,t;
3354 k=kernel->values;
3355 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3356 for( j=0, y=kernel->height-1; j<y; j++, y--)
3357 { t = k[i+j*kernel->width];
3358 k[i+j*kernel->width] = k[j+x*kernel->width];
3359 k[j+x*kernel->width] = k[x+y*kernel->width];
3360 k[x+y*kernel->width] = k[y+i*kernel->width];
3361 k[y+i*kernel->width] = t;
3362 }
3363 }
3364 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003365 { register ssize_t x,y;
3366 x = (ssize_t) kernel->x*2-kernel->width+1;
3367 y = (ssize_t) kernel->y*2-kernel->height+1;
3368 kernel->x = (size_t) ( -y +kernel->width-1)/2;
3369 kernel->y = (size_t) ( +x +kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003370 }
anthony43c49252010-05-18 10:59:50 +00003371 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3372 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003373 }
3374 else
3375 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3376 }
anthony83ba99b2010-01-24 08:48:15 +00003377 if ( 135.0 < angle && angle <= 225.0 )
3378 {
anthony43c49252010-05-18 10:59:50 +00003379 /* For a 180 degree rotation - also know as a reflection
3380 * This is actually a very very common operation!
3381 * Basically all that is needed is a reversal of the kernel data!
3382 * And a reflection of the origon
3383 */
cristybb503372010-05-27 20:51:26 +00003384 size_t
anthony83ba99b2010-01-24 08:48:15 +00003385 i,j;
3386 register double
3387 *k,t;
3388
3389 k=kernel->values;
3390 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3391 t=k[i], k[i]=k[j], k[j]=t;
3392
cristybb503372010-05-27 20:51:26 +00003393 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3394 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003395 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3396 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003397 }
anthony3c10fc82010-05-13 02:40:51 +00003398 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003399 * In the future some form of non-orthogonal angled rotates could be
3400 * performed here, posibily with a linear kernel restriction.
3401 */
3402
3403#if 0
anthony3c10fc82010-05-13 02:40:51 +00003404 { /* Do a Flop by reversing each row.
anthony83ba99b2010-01-24 08:48:15 +00003405 */
cristybb503372010-05-27 20:51:26 +00003406 size_t
anthony83ba99b2010-01-24 08:48:15 +00003407 y;
cristybb503372010-05-27 20:51:26 +00003408 register ssize_t
anthony83ba99b2010-01-24 08:48:15 +00003409 x,r;
3410 register double
3411 *k,t;
3412
3413 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
3414 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
3415 t=k[x], k[x]=k[r], k[r]=t;
3416
cristyc99304f2010-02-01 15:26:27 +00003417 kernel->x = kernel->width - kernel->x - 1;
anthony83ba99b2010-01-24 08:48:15 +00003418 angle = fmod(angle+180.0, 360.0);
3419 }
3420#endif
3421 return;
3422}
3423
3424/*
3425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3426% %
3427% %
3428% %
anthony46a369d2010-05-19 02:41:48 +00003429% S c a l e G e o m e t r y K e r n e l I n f o %
3430% %
3431% %
3432% %
3433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3434%
3435% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3436% provided as a "-set option:convolve:scale {geometry}" user setting,
3437% and modifies the kernel according to the parsed arguments of that setting.
3438%
3439% The first argument (and any normalization flags) are passed to
3440% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3441% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3442% into the scaled/normalized kernel.
3443%
3444% The format of the ScaleKernelInfo method is:
3445%
3446% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3447% const MagickStatusType normalize_flags )
3448%
3449% A description of each parameter follows:
3450%
3451% o kernel: the Morphology/Convolution kernel to modify
3452%
3453% o geometry:
3454% The geometry string to parse, typically from the user provided
3455% "-set option:convolve:scale {geometry}" setting.
3456%
3457*/
3458MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3459 const char *geometry)
3460{
3461 GeometryFlags
3462 flags;
3463 GeometryInfo
3464 args;
3465
3466 SetGeometryInfo(&args);
3467 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3468
3469#if 0
3470 /* For Debugging Geometry Input */
3471 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3472 flags, args.rho, args.sigma, args.xi, args.psi );
3473#endif
3474
3475 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3476 args.rho *= 0.01, args.sigma *= 0.01;
3477
3478 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3479 args.rho = 1.0;
3480 if ( (flags & SigmaValue) == 0 )
3481 args.sigma = 0.0;
3482
3483 /* Scale/Normalize the input kernel */
3484 ScaleKernelInfo(kernel, args.rho, flags);
3485
3486 /* Add Unity Kernel, for blending with original */
3487 if ( (flags & SigmaValue) != 0 )
3488 UnityAddKernelInfo(kernel, args.sigma);
3489
3490 return;
3491}
3492/*
3493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3494% %
3495% %
3496% %
cristy6771f1e2010-03-05 19:43:39 +00003497% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003498% %
3499% %
3500% %
3501%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3502%
anthony1b2bc0a2010-05-12 05:25:22 +00003503% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3504% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003505%
anthony999bb2c2010-02-18 12:38:01 +00003506% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003507% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003508%
anthony46a369d2010-05-19 02:41:48 +00003509% If either of the two 'normalize_flags' are given the kernel will first be
3510% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003511%
3512% Kernel normalization ('normalize_flags' given) is designed to ensure that
3513% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003514% morphology methods will fall into -1.0 to +1.0 range. Note that for
3515% non-HDRI versions of IM this may cause images to have any negative results
3516% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003517%
3518% More specifically. Kernels which only contain positive values (such as a
3519% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003520% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003521%
3522% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3523% the kernel will be scaled by the absolute of the sum of kernel values, so
3524% that it will generally fall within the +/- 1.0 range.
3525%
3526% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3527% will be scaled by just the sum of the postive values, so that its output
3528% range will again fall into the +/- 1.0 range.
3529%
3530% For special kernels designed for locating shapes using 'Correlate', (often
3531% only containing +1 and -1 values, representing foreground/brackground
3532% matching) a special normalization method is provided to scale the positive
3533% values seperatally to those of the negative values, so the kernel will be
3534% forced to become a zero-sum kernel better suited to such searches.
3535%
anthony1b2bc0a2010-05-12 05:25:22 +00003536% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003537% attributes within the kernel structure have been correctly set during the
3538% kernels creation.
3539%
3540% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003541% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3542% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003543%
anthony4fd27e22010-02-07 08:17:18 +00003544% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003545%
anthony999bb2c2010-02-18 12:38:01 +00003546% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3547% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003548%
3549% A description of each parameter follows:
3550%
3551% o kernel: the Morphology/Convolution kernel
3552%
anthony999bb2c2010-02-18 12:38:01 +00003553% o scaling_factor:
3554% multiply all values (after normalization) by this factor if not
3555% zero. If the kernel is normalized regardless of any flags.
3556%
3557% o normalize_flags:
3558% GeometryFlags defining normalization method to use.
3559% specifically: NormalizeValue, CorrelateNormalizeValue,
3560% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003561%
3562*/
cristy6771f1e2010-03-05 19:43:39 +00003563MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3564 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003565{
cristybb503372010-05-27 20:51:26 +00003566 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003567 i;
3568
anthony999bb2c2010-02-18 12:38:01 +00003569 register double
3570 pos_scale,
3571 neg_scale;
3572
anthony46a369d2010-05-19 02:41:48 +00003573 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003574 if ( kernel->next != (KernelInfo *) NULL)
3575 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3576
anthony46a369d2010-05-19 02:41:48 +00003577 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003578 pos_scale = 1.0;
3579 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003580 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003581 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003582 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003583 else
anthonyf4e00312010-05-20 12:06:35 +00003584 /* zero-summing kernel */
3585 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003586 }
anthony46a369d2010-05-19 02:41:48 +00003587 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003588 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3589 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3590 ? kernel->positive_range : 1.0;
3591 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3592 ? -kernel->negative_range : 1.0;
3593 }
3594 else
3595 neg_scale = pos_scale;
3596
3597 /* finialize scaling_factor for positive and negative components */
3598 pos_scale = scaling_factor/pos_scale;
3599 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003600
cristybb503372010-05-27 20:51:26 +00003601 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003602 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003603 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003604
anthony999bb2c2010-02-18 12:38:01 +00003605 /* convolution output range */
3606 kernel->positive_range *= pos_scale;
3607 kernel->negative_range *= neg_scale;
3608 /* maximum and minimum values in kernel */
3609 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3610 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3611
anthony46a369d2010-05-19 02:41:48 +00003612 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003613 if ( scaling_factor < MagickEpsilon ) {
3614 double t;
3615 t = kernel->positive_range;
3616 kernel->positive_range = kernel->negative_range;
3617 kernel->negative_range = t;
3618 t = kernel->maximum;
3619 kernel->maximum = kernel->minimum;
3620 kernel->minimum = 1;
3621 }
anthonycc6c8362010-01-25 04:14:01 +00003622
3623 return;
3624}
3625
3626/*
3627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3628% %
3629% %
3630% %
anthony46a369d2010-05-19 02:41:48 +00003631% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003632% %
3633% %
3634% %
3635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3636%
anthony4fd27e22010-02-07 08:17:18 +00003637% ShowKernelInfo() outputs the details of the given kernel defination to
3638% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003639%
3640% The format of the ShowKernel method is:
3641%
anthony4fd27e22010-02-07 08:17:18 +00003642% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003643%
3644% A description of each parameter follows:
3645%
3646% o kernel: the Morphology/Convolution kernel
3647%
anthony83ba99b2010-01-24 08:48:15 +00003648*/
anthony4fd27e22010-02-07 08:17:18 +00003649MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003650{
anthony7a01dcf2010-05-11 12:25:52 +00003651 KernelInfo
3652 *k;
anthony83ba99b2010-01-24 08:48:15 +00003653
cristybb503372010-05-27 20:51:26 +00003654 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003655 c, i, u, v;
3656
3657 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3658
anthony46a369d2010-05-19 02:41:48 +00003659 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003660 if ( kernel->next != (KernelInfo *) NULL )
cristye96405a2010-05-19 02:24:31 +00003661 fprintf(stderr, " #%lu", c );
anthony43c49252010-05-18 10:59:50 +00003662 fprintf(stderr, " \"%s",
3663 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3664 if ( fabs(k->angle) > MagickEpsilon )
3665 fprintf(stderr, "@%lg", k->angle);
anthonya648a302010-05-27 02:14:36 +00003666 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",
anthony43c49252010-05-18 10:59:50 +00003667 k->width, k->height,
3668 k->x, k->y );
anthony7a01dcf2010-05-11 12:25:52 +00003669 fprintf(stderr,
3670 " with values from %.*lg to %.*lg\n",
3671 GetMagickPrecision(), k->minimum,
3672 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003673 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003674 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003675 GetMagickPrecision(), k->positive_range);
3676 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3677 fprintf(stderr, " (Zero-Summing)\n");
3678 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3679 fprintf(stderr, " (Normalized)\n");
3680 else
3681 fprintf(stderr, " (Sum %.*lg)\n",
3682 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003683 for (i=v=0; v < k->height; v++) {
anthony46a369d2010-05-19 02:41:48 +00003684 fprintf(stderr, "%2lu:", v );
anthony43c49252010-05-18 10:59:50 +00003685 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003686 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003687 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003688 else
anthonyf4e00312010-05-20 12:06:35 +00003689 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003690 GetMagickPrecision(), k->values[i]);
3691 fprintf(stderr,"\n");
3692 }
anthony83ba99b2010-01-24 08:48:15 +00003693 }
3694}
anthonycc6c8362010-01-25 04:14:01 +00003695
3696/*
3697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3698% %
3699% %
3700% %
anthony43c49252010-05-18 10:59:50 +00003701% U n i t y A d d K e r n a l I n f o %
3702% %
3703% %
3704% %
3705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3706%
3707% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3708% to the given pre-scaled and normalized Kernel. This in effect adds that
3709% amount of the original image into the resulting convolution kernel. This
3710% value is usually provided by the user as a percentage value in the
3711% 'convolve:scale' setting.
3712%
3713% The resulting effect is to either convert a 'zero-summing' edge detection
3714% kernel (such as a "Laplacian", "DOG" or a "LOG") into a 'sharpening'
3715% kernel.
3716%
3717% Alternativally by using a purely positive kernel, and using a negative
3718% post-normalizing scaling factor, you can convert a 'blurring' kernel (such
3719% as a "Gaussian") into a 'unsharp' kernel.
3720%
anthony46a369d2010-05-19 02:41:48 +00003721% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003722%
3723% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3724%
3725% A description of each parameter follows:
3726%
3727% o kernel: the Morphology/Convolution kernel
3728%
3729% o scale:
3730% scaling factor for the unity kernel to be added to
3731% the given kernel.
3732%
anthony43c49252010-05-18 10:59:50 +00003733*/
3734MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3735 const double scale)
3736{
anthony46a369d2010-05-19 02:41:48 +00003737 /* do the other kernels in a multi-kernel list first */
3738 if ( kernel->next != (KernelInfo *) NULL)
3739 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003740
anthony46a369d2010-05-19 02:41:48 +00003741 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003742 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003743 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003744
3745 return;
3746}
3747
3748/*
3749%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3750% %
3751% %
3752% %
3753% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003754% %
3755% %
3756% %
3757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3758%
3759% ZeroKernelNans() replaces any special 'nan' value that may be present in
3760% the kernel with a zero value. This is typically done when the kernel will
3761% be used in special hardware (GPU) convolution processors, to simply
3762% matters.
3763%
3764% The format of the ZeroKernelNans method is:
3765%
anthony46a369d2010-05-19 02:41:48 +00003766% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003767%
3768% A description of each parameter follows:
3769%
3770% o kernel: the Morphology/Convolution kernel
3771%
anthonycc6c8362010-01-25 04:14:01 +00003772*/
anthonyc4c86e02010-01-27 09:30:32 +00003773MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003774{
cristybb503372010-05-27 20:51:26 +00003775 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003776 i;
3777
anthony46a369d2010-05-19 02:41:48 +00003778 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003779 if ( kernel->next != (KernelInfo *) NULL)
3780 ZeroKernelNans(kernel->next);
3781
anthony43c49252010-05-18 10:59:50 +00003782 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003783 if ( IsNan(kernel->values[i]) )
3784 kernel->values[i] = 0.0;
3785
3786 return;
3787}