blob: 77b7cf7d298c4f01b83cf996276f7fef75558987 [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 *),
anthonybfb635a2010-06-04 00:18:04 +0000115 ExpandMirrorKernelInfo(KernelInfo *),
116 ExpandRotateKernelInfo(KernelInfo *, const double),
cristyef656912010-03-05 19:54:59 +0000117 RotateKernelInfo(KernelInfo *, double);
anthony602ab9b2010-01-05 08:06:50 +0000118
anthony3dd0f622010-05-13 12:57:32 +0000119
120/* Quick function to find last kernel in a kernel list */
121static inline KernelInfo *LastKernelInfo(KernelInfo *kernel)
122{
123 while (kernel->next != (KernelInfo *) NULL)
124 kernel = kernel->next;
125 return(kernel);
126}
127
128
anthony602ab9b2010-01-05 08:06:50 +0000129/*
130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131% %
132% %
133% %
anthony83ba99b2010-01-24 08:48:15 +0000134% A c q u i r e K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +0000135% %
136% %
137% %
138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139%
cristy2be15382010-01-21 02:38:03 +0000140% AcquireKernelInfo() takes the given string (generally supplied by the
anthony602ab9b2010-01-05 08:06:50 +0000141% user) and converts it into a Morphology/Convolution Kernel. This allows
142% users to specify a kernel from a number of pre-defined kernels, or to fully
143% specify their own kernel for a specific Convolution or Morphology
144% Operation.
145%
146% The kernel so generated can be any rectangular array of floating point
147% values (doubles) with the 'control point' or 'pixel being affected'
148% anywhere within that array of values.
149%
anthony83ba99b2010-01-24 08:48:15 +0000150% Previously IM was restricted to a square of odd size using the exact
cristybb503372010-05-27 20:51:26 +0000151% center as origin, this is no ssize_ter the case, and any rectangular kernel
anthony83ba99b2010-01-24 08:48:15 +0000152% with any value being declared the origin. This in turn allows the use of
153% highly asymmetrical kernels.
anthony602ab9b2010-01-05 08:06:50 +0000154%
155% The floating point values in the kernel can also include a special value
anthony83ba99b2010-01-24 08:48:15 +0000156% known as 'nan' or 'not a number' to indicate that this value is not part
157% of the kernel array. This allows you to shaped the kernel within its
158% rectangular area. That is 'nan' values provide a 'mask' for the kernel
159% shape. However at least one non-nan value must be provided for correct
160% working of a kernel.
anthony602ab9b2010-01-05 08:06:50 +0000161%
anthony7a01dcf2010-05-11 12:25:52 +0000162% The returned kernel should be freed using the DestroyKernelInfo() when you
163% are finished with it. Do not free this memory yourself.
anthony602ab9b2010-01-05 08:06:50 +0000164%
165% Input kernel defintion strings can consist of any of three types.
166%
anthonybfb635a2010-06-04 00:18:04 +0000167% "name:args[[@><]"
anthony29188a82010-01-22 10:12:34 +0000168% Select from one of the built in kernels, using the name and
169% geometry arguments supplied. See AcquireKernelBuiltIn()
anthony602ab9b2010-01-05 08:06:50 +0000170%
anthonybfb635a2010-06-04 00:18:04 +0000171% "WxH[+X+Y][@><]:num, num, num ..."
anthony1b2bc0a2010-05-12 05:25:22 +0000172% a kernel of size W by H, with W*H floating point numbers following.
anthony602ab9b2010-01-05 08:06:50 +0000173% the 'center' can be optionally be defined at +X+Y (such that +0+0
anthony29188a82010-01-22 10:12:34 +0000174% is top left corner). If not defined the pixel in the center, for
175% odd sizes, or to the immediate top or left of center for even sizes
176% is automatically selected.
anthony602ab9b2010-01-05 08:06:50 +0000177%
anthony29188a82010-01-22 10:12:34 +0000178% "num, num, num, num, ..."
179% list of floating point numbers defining an 'old style' odd sized
180% square kernel. At least 9 values should be provided for a 3x3
181% square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
182% Values can be space or comma separated. This is not recommended.
anthony602ab9b2010-01-05 08:06:50 +0000183%
anthony7a01dcf2010-05-11 12:25:52 +0000184% You can define a 'list of kernels' which can be used by some morphology
185% operators A list is defined as a semi-colon seperated list kernels.
186%
anthonydbc89892010-05-12 07:05:27 +0000187% " kernel ; kernel ; kernel ; "
anthony7a01dcf2010-05-11 12:25:52 +0000188%
anthony1dd091a2010-05-27 06:31:15 +0000189% Any extra ';' characters, at start, end or between kernel defintions are
anthony43c49252010-05-18 10:59:50 +0000190% simply ignored.
191%
anthonybfb635a2010-06-04 00:18:04 +0000192% The special flags will expand a single kernel, into a list of rotated
193% kernels. A '@' flag will expand a 3x3 kernel into a list of 45-degree
194% cyclic rotations, while a '>' will generate a list of 90-degree rotations.
195% The '<' also exands using 90-degree rotates, but giving a 180-degree
196% reflected kernel before the +/- 90-degree rotations, which can be important
197% for Thinning operations.
198%
anthony43c49252010-05-18 10:59:50 +0000199% Note that 'name' kernels will start with an alphabetic character while the
200% new kernel specification has a ':' character in its specification string.
201% If neither is the case, it is assumed an old style of a simple list of
202% numbers generating a odd-sized square kernel has been given.
anthony7a01dcf2010-05-11 12:25:52 +0000203%
anthony602ab9b2010-01-05 08:06:50 +0000204% The format of the AcquireKernal method is:
205%
cristy2be15382010-01-21 02:38:03 +0000206% KernelInfo *AcquireKernelInfo(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000207%
208% A description of each parameter follows:
209%
210% o kernel_string: the Morphology/Convolution kernel wanted.
211%
212*/
213
anthonyc84dce52010-05-07 05:42:23 +0000214/* This was separated so that it could be used as a separate
anthony5ef8e942010-05-11 06:51:12 +0000215** array input handling function, such as for -color-matrix
anthonyc84dce52010-05-07 05:42:23 +0000216*/
anthony5ef8e942010-05-11 06:51:12 +0000217static KernelInfo *ParseKernelArray(const char *kernel_string)
anthony602ab9b2010-01-05 08:06:50 +0000218{
cristy2be15382010-01-21 02:38:03 +0000219 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000220 *kernel;
221
222 char
223 token[MaxTextExtent];
224
anthony602ab9b2010-01-05 08:06:50 +0000225 const char
anthony5ef8e942010-05-11 06:51:12 +0000226 *p,
227 *end;
anthony602ab9b2010-01-05 08:06:50 +0000228
cristybb503372010-05-27 20:51:26 +0000229 register ssize_t
anthonyc84dce52010-05-07 05:42:23 +0000230 i;
anthony602ab9b2010-01-05 08:06:50 +0000231
anthony29188a82010-01-22 10:12:34 +0000232 double
233 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
234
anthony43c49252010-05-18 10:59:50 +0000235 MagickStatusType
236 flags;
237
238 GeometryInfo
239 args;
240
cristy2be15382010-01-21 02:38:03 +0000241 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
242 if (kernel == (KernelInfo *)NULL)
anthony602ab9b2010-01-05 08:06:50 +0000243 return(kernel);
244 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000245 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthony7a01dcf2010-05-11 12:25:52 +0000246 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +0000247 kernel->type = UserDefinedKernel;
anthony7a01dcf2010-05-11 12:25:52 +0000248 kernel->next = (KernelInfo *) NULL;
cristyd43a46b2010-01-21 02:13:41 +0000249 kernel->signature = MagickSignature;
anthony602ab9b2010-01-05 08:06:50 +0000250
anthony5ef8e942010-05-11 06:51:12 +0000251 /* find end of this specific kernel definition string */
252 end = strchr(kernel_string, ';');
253 if ( end == (char *) NULL )
254 end = strchr(kernel_string, '\0');
255
anthony43c49252010-05-18 10:59:50 +0000256 /* clear flags - for Expanding kernal lists thorugh rotations */
257 flags = NoValue;
258
anthony602ab9b2010-01-05 08:06:50 +0000259 /* Has a ':' in argument - New user kernel specification */
260 p = strchr(kernel_string, ':');
anthony5ef8e942010-05-11 06:51:12 +0000261 if ( p != (char *) NULL && p < end)
anthony602ab9b2010-01-05 08:06:50 +0000262 {
anthony602ab9b2010-01-05 08:06:50 +0000263 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
cristy150989e2010-02-01 14:59:39 +0000264 memcpy(token, kernel_string, (size_t) (p-kernel_string));
anthony602ab9b2010-01-05 08:06:50 +0000265 token[p-kernel_string] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000266 SetGeometryInfo(&args);
anthony602ab9b2010-01-05 08:06:50 +0000267 flags = ParseGeometry(token, &args);
anthony602ab9b2010-01-05 08:06:50 +0000268
anthony29188a82010-01-22 10:12:34 +0000269 /* Size handling and checks of geometry settings */
anthony602ab9b2010-01-05 08:06:50 +0000270 if ( (flags & WidthValue) == 0 ) /* if no width then */
271 args.rho = args.sigma; /* then width = height */
272 if ( args.rho < 1.0 ) /* if width too small */
273 args.rho = 1.0; /* then width = 1 */
274 if ( args.sigma < 1.0 ) /* if height too small */
275 args.sigma = args.rho; /* then height = width */
cristybb503372010-05-27 20:51:26 +0000276 kernel->width = (size_t)args.rho;
277 kernel->height = (size_t)args.sigma;
anthony602ab9b2010-01-05 08:06:50 +0000278
279 /* Offset Handling and Checks */
280 if ( args.xi < 0.0 || args.psi < 0.0 )
anthony83ba99b2010-01-24 08:48:15 +0000281 return(DestroyKernelInfo(kernel));
cristybb503372010-05-27 20:51:26 +0000282 kernel->x = ((flags & XValue)!=0) ? (ssize_t)args.xi
283 : (ssize_t) (kernel->width-1)/2;
284 kernel->y = ((flags & YValue)!=0) ? (ssize_t)args.psi
285 : (ssize_t) (kernel->height-1)/2;
286 if ( kernel->x >= (ssize_t) kernel->width ||
287 kernel->y >= (ssize_t) kernel->height )
anthony83ba99b2010-01-24 08:48:15 +0000288 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000289
290 p++; /* advance beyond the ':' */
291 }
292 else
anthonyc84dce52010-05-07 05:42:23 +0000293 { /* ELSE - Old old specification, forming odd-square kernel */
anthony602ab9b2010-01-05 08:06:50 +0000294 /* count up number of values given */
295 p=(const char *) kernel_string;
cristya699b172010-01-06 16:48:49 +0000296 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
anthony29188a82010-01-22 10:12:34 +0000297 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony5ef8e942010-05-11 06:51:12 +0000298 for (i=0; p < end; i++)
anthony602ab9b2010-01-05 08:06:50 +0000299 {
300 GetMagickToken(p,&p,token);
301 if (*token == ',')
302 GetMagickToken(p,&p,token);
303 }
304 /* set the size of the kernel - old sized square */
cristybb503372010-05-27 20:51:26 +0000305 kernel->width = kernel->height= (size_t) sqrt((double) i+1.0);
306 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +0000307 p=(const char *) kernel_string;
anthony29188a82010-01-22 10:12:34 +0000308 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
309 p++; /* ignore "'" chars for convolve filter usage - Cristy */
anthony602ab9b2010-01-05 08:06:50 +0000310 }
311
312 /* Read in the kernel values from rest of input string argument */
313 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
314 kernel->height*sizeof(double));
315 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +0000316 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000317
cristyc99304f2010-02-01 15:26:27 +0000318 kernel->minimum = +MagickHuge;
319 kernel->maximum = -MagickHuge;
320 kernel->negative_range = kernel->positive_range = 0.0;
anthonyc84dce52010-05-07 05:42:23 +0000321
cristybb503372010-05-27 20:51:26 +0000322 for (i=0; (i < (ssize_t) (kernel->width*kernel->height)) && (p < end); i++)
anthony602ab9b2010-01-05 08:06:50 +0000323 {
324 GetMagickToken(p,&p,token);
325 if (*token == ',')
326 GetMagickToken(p,&p,token);
anthony29188a82010-01-22 10:12:34 +0000327 if ( LocaleCompare("nan",token) == 0
anthonyc84dce52010-05-07 05:42:23 +0000328 || LocaleCompare("-",token) == 0 ) {
anthony29188a82010-01-22 10:12:34 +0000329 kernel->values[i] = nan; /* do not include this value in kernel */
330 }
331 else {
332 kernel->values[i] = StringToDouble(token);
333 ( kernel->values[i] < 0)
cristyc99304f2010-02-01 15:26:27 +0000334 ? ( kernel->negative_range += kernel->values[i] )
335 : ( kernel->positive_range += kernel->values[i] );
336 Minimize(kernel->minimum, kernel->values[i]);
337 Maximize(kernel->maximum, kernel->values[i]);
anthony29188a82010-01-22 10:12:34 +0000338 }
anthony602ab9b2010-01-05 08:06:50 +0000339 }
anthony29188a82010-01-22 10:12:34 +0000340
anthony5ef8e942010-05-11 06:51:12 +0000341 /* sanity check -- no more values in kernel definition */
342 GetMagickToken(p,&p,token);
343 if ( *token != '\0' && *token != ';' && *token != '\'' )
344 return(DestroyKernelInfo(kernel));
345
anthonyc84dce52010-05-07 05:42:23 +0000346#if 0
347 /* this was the old method of handling a incomplete kernel */
cristybb503372010-05-27 20:51:26 +0000348 if ( i < (ssize_t) (kernel->width*kernel->height) ) {
cristyc99304f2010-02-01 15:26:27 +0000349 Minimize(kernel->minimum, kernel->values[i]);
350 Maximize(kernel->maximum, kernel->values[i]);
cristybb503372010-05-27 20:51:26 +0000351 for ( ; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony29188a82010-01-22 10:12:34 +0000352 kernel->values[i]=0.0;
353 }
anthonyc84dce52010-05-07 05:42:23 +0000354#else
355 /* Number of values for kernel was not enough - Report Error */
cristybb503372010-05-27 20:51:26 +0000356 if ( i < (ssize_t) (kernel->width*kernel->height) )
anthonyc84dce52010-05-07 05:42:23 +0000357 return(DestroyKernelInfo(kernel));
358#endif
359
360 /* check that we recieved at least one real (non-nan) value! */
361 if ( kernel->minimum == MagickHuge )
362 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +0000363
anthony43c49252010-05-18 10:59:50 +0000364 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel size */
anthonybfb635a2010-06-04 00:18:04 +0000365 ExpandRotateKernelInfo(kernel, 45.0); /* cyclic rotate 3x3 kernels */
366 else if ( (flags & GreaterValue) != 0 ) /* '>' symbol in kernel args */
367 ExpandRotateKernelInfo(kernel, 90.0); /* 90 degree rotate of kernel */
368 else if ( (flags & LessValue) != 0 ) /* '<' symbol in kernel args */
369 ExpandMirrorKernelInfo(kernel); /* 90 degree mirror rotate */
anthony43c49252010-05-18 10:59:50 +0000370
anthony602ab9b2010-01-05 08:06:50 +0000371 return(kernel);
372}
anthonyc84dce52010-05-07 05:42:23 +0000373
anthony43c49252010-05-18 10:59:50 +0000374static KernelInfo *ParseKernelName(const char *kernel_string)
anthonyc84dce52010-05-07 05:42:23 +0000375{
anthonyf0176c32010-05-23 23:08:57 +0000376 KernelInfo
377 *kernel;
378
anthonyc84dce52010-05-07 05:42:23 +0000379 char
380 token[MaxTextExtent];
381
cristybb503372010-05-27 20:51:26 +0000382 ssize_t
anthony5ef8e942010-05-11 06:51:12 +0000383 type;
384
anthonyc84dce52010-05-07 05:42:23 +0000385 const char
anthony7a01dcf2010-05-11 12:25:52 +0000386 *p,
387 *end;
anthonyc84dce52010-05-07 05:42:23 +0000388
389 MagickStatusType
390 flags;
391
392 GeometryInfo
393 args;
394
anthonyc84dce52010-05-07 05:42:23 +0000395 /* Parse special 'named' kernel */
anthony5ef8e942010-05-11 06:51:12 +0000396 GetMagickToken(kernel_string,&p,token);
anthonyc84dce52010-05-07 05:42:23 +0000397 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
398 if ( type < 0 || type == UserDefinedKernel )
anthony5ef8e942010-05-11 06:51:12 +0000399 return((KernelInfo *)NULL); /* not a valid named kernel */
anthonyc84dce52010-05-07 05:42:23 +0000400
401 while (((isspace((int) ((unsigned char) *p)) != 0) ||
anthony5ef8e942010-05-11 06:51:12 +0000402 (*p == ',') || (*p == ':' )) && (*p != '\0') && (*p != ';'))
anthonyc84dce52010-05-07 05:42:23 +0000403 p++;
anthony7a01dcf2010-05-11 12:25:52 +0000404
405 end = strchr(p, ';'); /* end of this kernel defintion */
406 if ( end == (char *) NULL )
407 end = strchr(p, '\0');
408
409 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
410 memcpy(token, p, (size_t) (end-p));
411 token[end-p] = '\0';
anthonyc84dce52010-05-07 05:42:23 +0000412 SetGeometryInfo(&args);
anthony7a01dcf2010-05-11 12:25:52 +0000413 flags = ParseGeometry(token, &args);
anthonyc84dce52010-05-07 05:42:23 +0000414
anthony3c10fc82010-05-13 02:40:51 +0000415#if 0
416 /* For Debugging Geometry Input */
anthony46a369d2010-05-19 02:41:48 +0000417 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
anthony3c10fc82010-05-13 02:40:51 +0000418 flags, args.rho, args.sigma, args.xi, args.psi );
419#endif
420
anthonyc84dce52010-05-07 05:42:23 +0000421 /* special handling of missing values in input string */
422 switch( type ) {
anthony5ef8e942010-05-11 06:51:12 +0000423 case RectangleKernel:
424 if ( (flags & WidthValue) == 0 ) /* if no width then */
425 args.rho = args.sigma; /* then width = height */
426 if ( args.rho < 1.0 ) /* if width too small */
427 args.rho = 3; /* then width = 3 */
428 if ( args.sigma < 1.0 ) /* if height too small */
429 args.sigma = args.rho; /* then height = width */
430 if ( (flags & XValue) == 0 ) /* center offset if not defined */
cristybb503372010-05-27 20:51:26 +0000431 args.xi = (double)(((ssize_t)args.rho-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000432 if ( (flags & YValue) == 0 )
cristybb503372010-05-27 20:51:26 +0000433 args.psi = (double)(((ssize_t)args.sigma-1)/2);
anthony5ef8e942010-05-11 06:51:12 +0000434 break;
435 case SquareKernel:
436 case DiamondKernel:
437 case DiskKernel:
438 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +0000439 case CrossKernel:
anthony5ef8e942010-05-11 06:51:12 +0000440 /* If no scale given (a 0 scale is valid! - set it to 1.0 */
441 if ( (flags & HeightValue) == 0 )
442 args.sigma = 1.0;
443 break;
anthonyc1061722010-05-14 06:23:49 +0000444 case RingKernel:
445 if ( (flags & XValue) == 0 )
446 args.xi = 1.0;
447 break;
anthony5ef8e942010-05-11 06:51:12 +0000448 case ChebyshevKernel:
anthonybee715c2010-06-04 01:25:57 +0000449 case ManhattanKernel:
anthony5ef8e942010-05-11 06:51:12 +0000450 case EuclideanKernel:
anthony43c49252010-05-18 10:59:50 +0000451 if ( (flags & HeightValue) == 0 ) /* no distance scale */
452 args.sigma = 100.0; /* default distance scaling */
453 else if ( (flags & AspectValue ) != 0 ) /* '!' flag */
454 args.sigma = QuantumRange/(args.sigma+1); /* maximum pixel distance */
455 else if ( (flags & PercentValue ) != 0 ) /* '%' flag */
456 args.sigma *= QuantumRange/100.0; /* percentage of color range */
anthony5ef8e942010-05-11 06:51:12 +0000457 break;
458 default:
459 break;
anthonyc84dce52010-05-07 05:42:23 +0000460 }
461
anthonyf0176c32010-05-23 23:08:57 +0000462 kernel = AcquireKernelBuiltIn((KernelInfoType)type, &args);
463
464 /* global expand to rotated kernel list - only for single kernels */
465 if ( kernel->next == (KernelInfo *) NULL ) {
466 if ( (flags & AreaValue) != 0 ) /* '@' symbol in kernel args */
anthonybfb635a2010-06-04 00:18:04 +0000467 ExpandRotateKernelInfo(kernel, 45.0);
468 else if ( (flags & GreaterValue) != 0 ) /* '>' symbol in kernel args */
469 ExpandRotateKernelInfo(kernel, 90.0);
470 else if ( (flags & LessValue) != 0 ) /* '<' symbol in kernel args */
471 ExpandMirrorKernelInfo(kernel);
anthonyf0176c32010-05-23 23:08:57 +0000472 }
473
474 return(kernel);
anthonyc84dce52010-05-07 05:42:23 +0000475}
476
anthony5ef8e942010-05-11 06:51:12 +0000477MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
478{
anthony7a01dcf2010-05-11 12:25:52 +0000479
480 KernelInfo
anthonydbc89892010-05-12 07:05:27 +0000481 *kernel,
anthony43c49252010-05-18 10:59:50 +0000482 *new_kernel;
anthony7a01dcf2010-05-11 12:25:52 +0000483
anthony5ef8e942010-05-11 06:51:12 +0000484 char
485 token[MaxTextExtent];
486
anthony7a01dcf2010-05-11 12:25:52 +0000487 const char
anthonydbc89892010-05-12 07:05:27 +0000488 *p;
anthony7a01dcf2010-05-11 12:25:52 +0000489
cristybb503372010-05-27 20:51:26 +0000490 size_t
anthonye108a3f2010-05-12 07:24:03 +0000491 kernel_number;
492
anthonydbc89892010-05-12 07:05:27 +0000493 p = kernel_string;
anthony43c49252010-05-18 10:59:50 +0000494 kernel = NULL;
anthonye108a3f2010-05-12 07:24:03 +0000495 kernel_number = 0;
anthony5ef8e942010-05-11 06:51:12 +0000496
anthonydbc89892010-05-12 07:05:27 +0000497 while ( GetMagickToken(p,NULL,token), *token != '\0' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000498
anthony43c49252010-05-18 10:59:50 +0000499 /* ignore extra or multiple ';' kernel seperators */
anthonydbc89892010-05-12 07:05:27 +0000500 if ( *token != ';' ) {
anthony7a01dcf2010-05-11 12:25:52 +0000501
anthonydbc89892010-05-12 07:05:27 +0000502 /* tokens starting with alpha is a Named kernel */
anthony43c49252010-05-18 10:59:50 +0000503 if (isalpha((int) *token) != 0)
504 new_kernel = ParseKernelName(p);
anthonydbc89892010-05-12 07:05:27 +0000505 else /* otherwise a user defined kernel array */
anthony43c49252010-05-18 10:59:50 +0000506 new_kernel = ParseKernelArray(p);
anthony7a01dcf2010-05-11 12:25:52 +0000507
anthonye108a3f2010-05-12 07:24:03 +0000508 /* Error handling -- this is not proper error handling! */
509 if ( new_kernel == (KernelInfo *) NULL ) {
cristye8c25f92010-06-03 00:53:06 +0000510 fprintf(stderr, "Failed to parse kernel number #%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +0000511 kernel_number);
anthonye108a3f2010-05-12 07:24:03 +0000512 if ( kernel != (KernelInfo *) NULL )
513 kernel=DestroyKernelInfo(kernel);
514 return((KernelInfo *) NULL);
anthonydbc89892010-05-12 07:05:27 +0000515 }
anthonye108a3f2010-05-12 07:24:03 +0000516
517 /* initialise or append the kernel list */
anthony3dd0f622010-05-13 12:57:32 +0000518 if ( kernel == (KernelInfo *) NULL )
519 kernel = new_kernel;
520 else
anthony43c49252010-05-18 10:59:50 +0000521 LastKernelInfo(kernel)->next = new_kernel;
anthonydbc89892010-05-12 07:05:27 +0000522 }
523
524 /* look for the next kernel in list */
525 p = strchr(p, ';');
526 if ( p == (char *) NULL )
527 break;
528 p++;
529
530 }
anthony7a01dcf2010-05-11 12:25:52 +0000531 return(kernel);
anthony5ef8e942010-05-11 06:51:12 +0000532}
533
anthony602ab9b2010-01-05 08:06:50 +0000534
535/*
536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537% %
538% %
539% %
540% A c q u i r e K e r n e l B u i l t I n %
541% %
542% %
543% %
544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545%
546% AcquireKernelBuiltIn() returned one of the 'named' built-in types of
547% kernels used for special purposes such as gaussian blurring, skeleton
548% pruning, and edge distance determination.
549%
550% They take a KernelType, and a set of geometry style arguments, which were
551% typically decoded from a user supplied string, or from a more complex
552% Morphology Method that was requested.
553%
554% The format of the AcquireKernalBuiltIn method is:
555%
cristy2be15382010-01-21 02:38:03 +0000556% KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000557% const GeometryInfo args)
558%
559% A description of each parameter follows:
560%
561% o type: the pre-defined type of kernel wanted
562%
563% o args: arguments defining or modifying the kernel
564%
565% Convolution Kernels
566%
anthony46a369d2010-05-19 02:41:48 +0000567% Unity
568% the No-Op kernel, also requivelent to Gaussian of sigma zero.
569% Basically a 3x3 kernel of a 1 surrounded by zeros.
570%
anthony3c10fc82010-05-13 02:40:51 +0000571% Gaussian:{radius},{sigma}
572% Generate a two-dimentional gaussian kernel, as used by -gaussian.
anthonyc1061722010-05-14 06:23:49 +0000573% The sigma for the curve is required. The resulting kernel is
574% normalized,
575%
576% If 'sigma' is zero, you get a single pixel on a field of zeros.
anthony602ab9b2010-01-05 08:06:50 +0000577%
578% NOTE: that the 'radius' is optional, but if provided can limit (clip)
579% the final size of the resulting kernel to a square 2*radius+1 in size.
580% The radius should be at least 2 times that of the sigma value, or
581% sever clipping and aliasing may result. If not given or set to 0 the
582% radius will be determined so as to produce the best minimal error
583% result, which is usally much larger than is normally needed.
584%
anthony501c2f92010-06-02 10:55:14 +0000585% LoG:{radius},{sigma}
586% "Laplacian of a Gaussian" or "Mexician Hat" Kernel.
587% The supposed ideal edge detection, zero-summing kernel.
588%
589% An alturnative to this kernel is to use a "DoG" with a sigma ratio of
590% approx 1.6 (according to wikipedia).
591%
592% DoG:{radius},{sigma1},{sigma2}
anthonyc1061722010-05-14 06:23:49 +0000593% "Difference of Gaussians" Kernel.
594% As "Gaussian" but with a gaussian produced by 'sigma2' subtracted
595% from the gaussian produced by 'sigma1'. Typically sigma2 > sigma1.
596% The result is a zero-summing kernel.
anthony602ab9b2010-01-05 08:06:50 +0000597%
anthonyc1061722010-05-14 06:23:49 +0000598% Blur:{radius},{sigma}[,{angle}]
599% Generates a 1 dimensional or linear gaussian blur, at the angle given
600% (current restricted to orthogonal angles). If a 'radius' is given the
601% kernel is clipped to a width of 2*radius+1. Kernel can be rotated
602% by a 90 degree angle.
603%
604% If 'sigma' is zero, you get a single pixel on a field of zeros.
605%
606% Note that two convolutions with two "Blur" kernels perpendicular to
607% each other, is equivelent to a far larger "Gaussian" kernel with the
608% same sigma value, However it is much faster to apply. This is how the
609% "-blur" operator actually works.
610%
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
anthony501c2f92010-06-02 10:55:14 +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)
anthonyc40ac1e2010-06-06 11:49:31 +0000651% | -1, 0, 1 |
652% | -2, 0,-2 |
653% | -1, 0, 1 |
654%
655% Sobel:{type},{angle}
656% Type 0: default un-nomalized version shown above.
657%
658% Type 1: As default but pre-normalized
659% | 1, 0, -1 |
660% | 2, 0, -2 | / 4
661% | 1, 0, -1 |
662%
663% Type 2: Diagonal version with same normalization as 1
664% | 1, 0, -1 |
665% | 2, 0, -2 | / 4
666% | 1, 0, -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000667%
anthonyc1061722010-05-14 06:23:49 +0000668% Roberts:{angle}
anthony46a369d2010-05-19 02:41:48 +0000669% Roberts convolution kernel (3x3)
anthonyc40ac1e2010-06-06 11:49:31 +0000670% | 0, 0, 0 |
671% | -1, 1, 0 |
672% | 0, 0, 0 |
673%
anthonyc1061722010-05-14 06:23:49 +0000674% Prewitt:{angle}
675% Prewitt Edge convolution kernel (3x3)
anthonyc40ac1e2010-06-06 11:49:31 +0000676% | -1, 0, 1 |
677% | -1, 0, 1 |
678% | -1, 0, 1 |
679%
anthony9eb4f742010-05-18 02:45:54 +0000680% Compass:{angle}
681% Prewitt's "Compass" convolution kernel (3x3)
anthonyc40ac1e2010-06-06 11:49:31 +0000682% | -1, 1, 1 |
683% | -1,-2, 1 |
684% | -1, 1, 1 |
685%
anthony9eb4f742010-05-18 02:45:54 +0000686% Kirsch:{angle}
687% Kirsch's "Compass" convolution kernel (3x3)
anthonyc40ac1e2010-06-06 11:49:31 +0000688% | -3,-3, 5 |
689% | -3, 0, 5 |
690% | -3,-3, 5 |
anthony3c10fc82010-05-13 02:40:51 +0000691%
anthonyc40ac1e2010-06-06 11:49:31 +0000692% FreiChen:{angle}
anthony1d5e6702010-05-31 10:19:12 +0000693% Frei-Chen Edge Detector is based on a kernel that is similar to
694% the Sobel Kernel, but is designed to be isotropic. That is it takes
695% into account the distance of the diagonal in the kernel.
anthonyc3cd15b2010-05-27 06:05:40 +0000696%
anthonyc40ac1e2010-06-06 11:49:31 +0000697% | 1, 0, -1 |
698% | sqrt(2), 0, -sqrt(2) |
699% | 1, 0, -1 |
700%
701% FreiChen:{type},{angle}
702%
703% Frei-Chen Pre-weighted kernels...
704%
705% Type 0: default un-nomalized version shown above.
706%
707% Type 1: Orthogonal Kernel (same as type 11 below)
708% | 1, 0, -1 |
709% | sqrt(2), 0, -sqrt(2) | / 2*sqrt(2)
710% | 1, 0, -1 |
711%
712% Type 2: Diagonal form of Kernel...
713% | 1, sqrt(2), 0 |
714% | sqrt(2), 0, -sqrt(2) | / 2*sqrt(2)
715% | 0, -sqrt(2) -1 |
anthonyc3cd15b2010-05-27 06:05:40 +0000716%
anthony1d5e6702010-05-31 10:19:12 +0000717% However this kernel is als at the heart of the FreiChen Edge Detection
718% Process which uses a set of 9 specially weighted kernel. These 9
719% kernels not be normalized, but directly applied to the image. The
720% results is then added together, to produce the intensity of an edge in
721% a specific direction. The square root of the pixel value can then be
722% taken as the cosine of the edge, and at least 2 such runs at 90 degrees
723% from each other, both the direction and the strength of the edge can be
724% determined.
anthonyc3cd15b2010-05-27 06:05:40 +0000725%
anthonyc40ac1e2010-06-06 11:49:31 +0000726% Type 10: All 9 of the following pre-weighted kernels...
anthonye2a60ce2010-05-19 12:30:40 +0000727%
anthonyc40ac1e2010-06-06 11:49:31 +0000728% Type 11: | 1, 0, -1 |
729% | sqrt(2), 0, -sqrt(2) | / 2*sqrt(2)
730% | 1, 0, -1 |
anthonye2a60ce2010-05-19 12:30:40 +0000731%
anthonyc40ac1e2010-06-06 11:49:31 +0000732% Type 12: | 1, sqrt(2), 1 |
733% | 0, 0, 0 | / 2*sqrt(2)
734% | 1, sqrt(2), 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000735%
anthonyc40ac1e2010-06-06 11:49:31 +0000736% Type 13: | sqrt(2), -1, 0 |
737% | -1, 0, 1 | / 2*sqrt(2)
738% | 0, 1, -sqrt(2) |
anthonye2a60ce2010-05-19 12:30:40 +0000739%
anthonyc40ac1e2010-06-06 11:49:31 +0000740% Type 14: | 0, 1, -sqrt(2) |
741% | -1, 0, 1 | / 2*sqrt(2)
742% | sqrt(2), -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000743%
anthonyc40ac1e2010-06-06 11:49:31 +0000744% Type 15: | 0, -1, 0 |
745% | 1, 0, 1 | / 2
746% | 0, -1, 0 |
anthonye2a60ce2010-05-19 12:30:40 +0000747%
anthonyc40ac1e2010-06-06 11:49:31 +0000748% Type 16: | 1, 0, -1 |
749% | 0, 0, 0 | / 2
750% | -1, 0, 1 |
anthony501c2f92010-06-02 10:55:14 +0000751%
anthonyc40ac1e2010-06-06 11:49:31 +0000752% Type 17: | 1, -2, 1 |
753% | -2, 4, -2 | / 6
754% | -1, -2, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000755%
anthonyc40ac1e2010-06-06 11:49:31 +0000756% Type 18: | -2, 1, -2 |
757% | 1, 4, 1 | / 6
758% | -2, 1, -2 |
759%
760% Type 19: | 1, 1, 1 |
761% | 1, 1, 1 | / 3
762% | 1, 1, 1 |
anthonye2a60ce2010-05-19 12:30:40 +0000763%
764% The first 4 are for edge detection, the next 4 are for line detection
765% and the last is to add a average component to the results.
766%
anthonyc3cd15b2010-05-27 06:05:40 +0000767% Using a special type of '-1' will return all 9 pre-weighted kernels
768% as a multi-kernel list, so that you can use them directly (without
769% normalization) with the special "-set option:morphology:compose Plus"
770% setting to apply the full FreiChen Edge Detection Technique.
771%
anthony1dd091a2010-05-27 06:31:15 +0000772% If 'type' is large it will be taken to be an actual rotation angle for
773% the default FreiChen (type 0) kernel. As such FreiChen:45 will look
774% like a Sobel:45 but with 'sqrt(2)' instead of '2' values.
775%
anthony501c2f92010-06-02 10:55:14 +0000776% WARNING: The above was layed out as per
777% http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf
778% But rotated 90 degrees so direction is from left rather than the top.
779% I have yet to find any secondary confirmation of the above. The only
780% other source found was actual source code at
781% http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf
782% Neigher paper defineds the kernels in a way that looks locical or
783% correct when taken as a whole.
anthonye2a60ce2010-05-19 12:30:40 +0000784%
anthony602ab9b2010-01-05 08:06:50 +0000785% Boolean Kernels
786%
anthony3c10fc82010-05-13 02:40:51 +0000787% Diamond:[{radius}[,{scale}]]
anthony1b2bc0a2010-05-12 05:25:22 +0000788% Generate a diamond shaped kernel with given radius to the points.
anthony602ab9b2010-01-05 08:06:50 +0000789% Kernel size will again be radius*2+1 square and defaults to radius 1,
790% generating a 3x3 kernel that is slightly larger than a square.
791%
anthony3c10fc82010-05-13 02:40:51 +0000792% Square:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000793% Generate a square shaped kernel of size radius*2+1, and defaulting
794% to a 3x3 (radius 1).
795%
anthonyc1061722010-05-14 06:23:49 +0000796% Note that using a larger radius for the "Square" or the "Diamond" is
797% also equivelent to iterating the basic morphological method that many
798% times. However iterating with the smaller radius is actually faster
799% than using a larger kernel radius.
800%
801% Rectangle:{geometry}
802% Simply generate a rectangle of 1's with the size given. You can also
803% specify the location of the 'control point', otherwise the closest
804% pixel to the center of the rectangle is selected.
805%
806% Properly centered and odd sized rectangles work the best.
anthony602ab9b2010-01-05 08:06:50 +0000807%
anthony3c10fc82010-05-13 02:40:51 +0000808% Disk:[{radius}[,{scale}]]
anthony602ab9b2010-01-05 08:06:50 +0000809% Generate a binary disk of the radius given, radius may be a float.
810% Kernel size will be ceil(radius)*2+1 square.
811% NOTE: Here are some disk shapes of specific interest
anthonyc1061722010-05-14 06:23:49 +0000812% "Disk:1" => "diamond" or "cross:1"
813% "Disk:1.5" => "square"
814% "Disk:2" => "diamond:2"
815% "Disk:2.5" => a general disk shape of radius 2
816% "Disk:2.9" => "square:2"
817% "Disk:3.5" => default - octagonal/disk shape of radius 3
818% "Disk:4.2" => roughly octagonal shape of radius 4
819% "Disk:4.3" => a general disk shape of radius 4
anthony602ab9b2010-01-05 08:06:50 +0000820% After this all the kernel shape becomes more and more circular.
821%
822% Because a "disk" is more circular when using a larger radius, using a
823% larger radius is preferred over iterating the morphological operation.
824%
anthonyc1061722010-05-14 06:23:49 +0000825% Symbol Dilation Kernels
826%
827% These kernel is not a good general morphological kernel, but is used
828% more for highlighting and marking any single pixels in an image using,
829% a "Dilate" method as appropriate.
830%
831% For the same reasons iterating these kernels does not produce the
832% same result as using a larger radius for the symbol.
833%
anthony3c10fc82010-05-13 02:40:51 +0000834% Plus:[{radius}[,{scale}]]
anthony3dd0f622010-05-13 12:57:32 +0000835% Cross:[{radius}[,{scale}]]
anthonyc1061722010-05-14 06:23:49 +0000836% Generate a kernel in the shape of a 'plus' or a 'cross' with
837% a each arm the length of the given radius (default 2).
anthony3dd0f622010-05-13 12:57:32 +0000838%
839% NOTE: "plus:1" is equivelent to a "Diamond" kernel.
anthony602ab9b2010-01-05 08:06:50 +0000840%
anthonyc1061722010-05-14 06:23:49 +0000841% Ring:{radius1},{radius2}[,{scale}]
842% A ring of the values given that falls between the two radii.
843% Defaults to a ring of approximataly 3 radius in a 7x7 kernel.
844% This is the 'edge' pixels of the default "Disk" kernel,
845% More specifically, "Ring" -> "Ring:2.5,3.5,1.0"
anthony602ab9b2010-01-05 08:06:50 +0000846%
anthony3dd0f622010-05-13 12:57:32 +0000847% Hit and Miss Kernels
848%
849% Peak:radius1,radius2
anthonyc1061722010-05-14 06:23:49 +0000850% Find any peak larger than the pixels the fall between the two radii.
851% The default ring of pixels is as per "Ring".
anthony43c49252010-05-18 10:59:50 +0000852% Edges
anthony1d45eb92010-05-25 11:13:23 +0000853% Find edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000854% Corners
855% Find corners of a binary shape
anthonyc40ac1e2010-06-06 11:49:31 +0000856% Ridges:type
anthony1d45eb92010-05-25 11:13:23 +0000857% Find single pixel ridges or thin lines
anthony3dd0f622010-05-13 12:57:32 +0000858% LineEnds
859% Find end points of lines (for pruning a skeletion)
860% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000861% Find three line junctions (within a skeletion)
anthony3dd0f622010-05-13 12:57:32 +0000862% ConvexHull
863% Octagonal thicken kernel, to generate convex hulls of 45 degrees
anthonyc40ac1e2010-06-06 11:49:31 +0000864% Skeleton:type
865% Traditional skeleton generating kernels.
anthony602ab9b2010-01-05 08:06:50 +0000866%
867% Distance Measuring Kernels
868%
anthonyc1061722010-05-14 06:23:49 +0000869% Different types of distance measuring methods, which are used with the
870% a 'Distance' morphology method for generating a gradient based on
871% distance from an edge of a binary shape, though there is a technique
872% for handling a anti-aliased shape.
873%
874% See the 'Distance' Morphological Method, for information of how it is
875% applied.
876%
anthony3dd0f622010-05-13 12:57:32 +0000877% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000878% Chebyshev Distance (also known as Tchebychev Distance) is a value of
879% one to any neighbour, orthogonal or diagonal. One why of thinking of
880% it is the number of squares a 'King' or 'Queen' in chess needs to
881% traverse reach any other position on a chess board. It results in a
882% 'square' like distance function, but one where diagonals are closer
883% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000884%
anthonybee715c2010-06-04 01:25:57 +0000885% Manhattan:[{radius}][x{scale}[%!]]
886% Manhattan Distance (also known as Rectilinear Distance, or the Taxi
anthonyc94cdb02010-01-06 08:15:29 +0000887% Cab metric), is the distance needed when you can only travel in
888% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
889% in chess would travel. It results in a diamond like distances, where
890% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000891%
anthonyc1061722010-05-14 06:23:49 +0000892% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000893% Euclidean Distance is the 'direct' or 'as the crow flys distance.
894% However by default the kernel size only has a radius of 1, which
895% limits the distance to 'Knight' like moves, with only orthogonal and
896% diagonal measurements being correct. As such for the default kernel
897% you will get octagonal like distance function, which is reasonally
898% accurate.
899%
900% However if you use a larger radius such as "Euclidean:4" you will
901% get a much smoother distance gradient from the edge of the shape.
902% Of course a larger kernel is slower to use, and generally not needed.
903%
904% To allow the use of fractional distances that you get with diagonals
905% the actual distance is scaled by a fixed value which the user can
906% provide. This is not actually nessary for either ""Chebyshev" or
anthonybee715c2010-06-04 01:25:57 +0000907% "Manhattan" distance kernels, but is done for all three distance
anthonyc94cdb02010-01-06 08:15:29 +0000908% kernels. If no scale is provided it is set to a value of 100,
909% allowing for a maximum distance measurement of 655 pixels using a Q16
910% version of IM, from any edge. However for small images this can
911% result in quite a dark gradient.
912%
anthony602ab9b2010-01-05 08:06:50 +0000913*/
914
cristy2be15382010-01-21 02:38:03 +0000915MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000916 const GeometryInfo *args)
917{
cristy2be15382010-01-21 02:38:03 +0000918 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000919 *kernel;
920
cristybb503372010-05-27 20:51:26 +0000921 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000922 i;
923
cristybb503372010-05-27 20:51:26 +0000924 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000925 u,
926 v;
927
928 double
929 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
930
anthonyc1061722010-05-14 06:23:49 +0000931 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000932 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000933 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000934 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000935 case UserDefinedKernel:
anthony1dd091a2010-05-27 06:31:15 +0000936 case TestKernel:
anthony9eb4f742010-05-18 02:45:54 +0000937 break;
anthony1dd091a2010-05-27 06:31:15 +0000938 case UnityKernel: /* Named Descrete Convolution Kernels */
939 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000940 case SobelKernel:
941 case RobertsKernel:
942 case PrewittKernel:
943 case CompassKernel:
944 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000945 case FreiChenKernel:
anthony9eb4f742010-05-18 02:45:54 +0000946 case CornersKernel: /* Hit and Miss kernels */
947 case LineEndsKernel:
948 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000949 case EdgesKernel:
950 case RidgesKernel:
anthony9eb4f742010-05-18 02:45:54 +0000951 case ConvexHullKernel:
952 case SkeletonKernel:
anthonyc40ac1e2010-06-06 11:49:31 +0000953 break; /* A pre-generated kernel is not needed */
954#if 0
955 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000956 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +0000957 case DoGKernel:
958 case LoGKernel:
anthonyc1061722010-05-14 06:23:49 +0000959 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +0000960 case CometKernel:
961 case DiamondKernel:
962 case SquareKernel:
963 case RectangleKernel:
964 case DiskKernel:
965 case PlusKernel:
966 case CrossKernel:
967 case RingKernel:
968 case PeaksKernel:
969 case ChebyshevKernel:
anthonybee715c2010-06-04 01:25:57 +0000970 case ManhattanKernel:
anthonyc1061722010-05-14 06:23:49 +0000971 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000972#else
anthony9eb4f742010-05-18 02:45:54 +0000973 default:
anthony1dd091a2010-05-27 06:31:15 +0000974#endif
anthony9eb4f742010-05-18 02:45:54 +0000975 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000976 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
977 if (kernel == (KernelInfo *) NULL)
978 return(kernel);
979 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000980 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000981 kernel->negative_range = kernel->positive_range = 0.0;
982 kernel->type = type;
983 kernel->next = (KernelInfo *) NULL;
984 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +0000985 break;
986 }
anthony602ab9b2010-01-05 08:06:50 +0000987
988 switch(type) {
989 /* Convolution Kernels */
990 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +0000991 case DoGKernel:
992 case LoGKernel:
anthony602ab9b2010-01-05 08:06:50 +0000993 { double
anthonyc1061722010-05-14 06:23:49 +0000994 sigma = fabs(args->sigma),
995 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +0000996 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +0000997
anthonyc1061722010-05-14 06:23:49 +0000998 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +0000999 kernel->width = (size_t)args->rho*2+1;
anthony501c2f92010-06-02 10:55:14 +00001000 else if ( (type != DoGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +00001001 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
1002 else
1003 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
1004 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001005 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001006 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1007 kernel->height*sizeof(double));
1008 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001009 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001010
anthony46a369d2010-05-19 02:41:48 +00001011 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +00001012 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +00001013 *
1014 * How to do this is currently not known, but appears to be
1015 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +00001016 */
1017
anthony501c2f92010-06-02 10:55:14 +00001018 if ( type == GaussianKernel || type == DoGKernel )
1019 { /* Calculate a Gaussian, OR positive half of a DoG */
anthony9eb4f742010-05-18 02:45:54 +00001020 if ( sigma > MagickEpsilon )
1021 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1022 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001023 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1024 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001025 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
1026 }
1027 else /* limiting case - a unity (normalized Dirac) kernel */
1028 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1029 kernel->width*kernel->height*sizeof(double));
1030 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1031 }
anthonyc1061722010-05-14 06:23:49 +00001032 }
anthony9eb4f742010-05-18 02:45:54 +00001033
anthony501c2f92010-06-02 10:55:14 +00001034 if ( type == DoGKernel )
anthonyc1061722010-05-14 06:23:49 +00001035 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
1036 if ( sigma2 > MagickEpsilon )
1037 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001038 A = 1.0/(2.0*sigma*sigma);
1039 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001040 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1041 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001042 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001043 }
anthony9eb4f742010-05-18 02:45:54 +00001044 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001045 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1046 }
anthony9eb4f742010-05-18 02:45:54 +00001047
anthony501c2f92010-06-02 10:55:14 +00001048 if ( type == LoGKernel )
anthony9eb4f742010-05-18 02:45:54 +00001049 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1050 if ( sigma > MagickEpsilon )
1051 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1052 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001053 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1054 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001055 { R = ((double)(u*u+v*v))*A;
1056 kernel->values[i] = (1-R)*exp(-R)*B;
1057 }
1058 }
1059 else /* special case - generate a unity kernel */
1060 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1061 kernel->width*kernel->height*sizeof(double));
1062 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1063 }
1064 }
1065
1066 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001067 ** radius, producing a smaller (darker) kernel. Also for very small
1068 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1069 ** producing a very bright kernel.
1070 **
1071 ** Normalization will still be needed.
1072 */
anthony602ab9b2010-01-05 08:06:50 +00001073
anthony3dd0f622010-05-13 12:57:32 +00001074 /* Normalize the 2D Gaussian Kernel
1075 **
anthonyc1061722010-05-14 06:23:49 +00001076 ** NB: a CorrelateNormalize performs a normal Normalize if
1077 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001078 */
anthony46a369d2010-05-19 02:41:48 +00001079 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001080 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001081
1082 break;
1083 }
1084 case BlurKernel:
1085 { double
anthonyc1061722010-05-14 06:23:49 +00001086 sigma = fabs(args->sigma),
anthony501c2f92010-06-02 10:55:14 +00001087 alpha, beta;
anthony602ab9b2010-01-05 08:06:50 +00001088
anthonyc1061722010-05-14 06:23:49 +00001089 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001090 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001091 else
anthony501c2f92010-06-02 10:55:14 +00001092 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
anthony602ab9b2010-01-05 08:06:50 +00001093 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001094 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001095 kernel->y = 0;
1096 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001097 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1098 kernel->height*sizeof(double));
1099 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001100 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001101
1102#if 1
1103#define KernelRank 3
1104 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1105 ** It generates a gaussian 3 times the width, and compresses it into
1106 ** the expected range. This produces a closer normalization of the
1107 ** resulting kernel, especially for very low sigma values.
1108 ** As such while wierd it is prefered.
1109 **
1110 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001111 **
1112 ** A properly normalized curve is generated (apart from edge clipping)
1113 ** even though we later normalize the result (for edge clipping)
1114 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001115 */
anthonyc1061722010-05-14 06:23:49 +00001116
1117 /* initialize */
cristybb503372010-05-27 20:51:26 +00001118 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001119 (void) ResetMagickMemory(kernel->values,0, (size_t)
1120 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001121 /* Calculate a Positive 1D Gaussian */
1122 if ( sigma > MagickEpsilon )
1123 { sigma *= KernelRank; /* simplify loop expressions */
anthony501c2f92010-06-02 10:55:14 +00001124 alpha = 1.0/(2.0*sigma*sigma);
1125 beta= 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001126 for ( u=-v; u <= v; u++) {
anthony501c2f92010-06-02 10:55:14 +00001127 kernel->values[(u+v)/KernelRank] +=
1128 exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001129 }
1130 }
1131 else /* special case - generate a unity kernel */
1132 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001133#else
anthonyc1061722010-05-14 06:23:49 +00001134 /* Direct calculation without curve averaging */
1135
1136 /* Calculate a Positive Gaussian */
1137 if ( sigma > MagickEpsilon )
anthony501c2f92010-06-02 10:55:14 +00001138 { alpha = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1139 beta = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001140 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony501c2f92010-06-02 10:55:14 +00001141 kernel->values[i] = exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001142 }
1143 else /* special case - generate a unity kernel */
1144 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1145 kernel->width*kernel->height*sizeof(double));
1146 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1147 }
anthony602ab9b2010-01-05 08:06:50 +00001148#endif
anthonyc1061722010-05-14 06:23:49 +00001149 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001150 ** radius, producing a smaller (darker) kernel. Also for very small
1151 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1152 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001153 **
1154 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001155 */
anthonycc6c8362010-01-25 04:14:01 +00001156
anthony602ab9b2010-01-05 08:06:50 +00001157 /* Normalize the 1D Gaussian Kernel
1158 **
anthonyc1061722010-05-14 06:23:49 +00001159 ** NB: a CorrelateNormalize performs a normal Normalize if
1160 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001161 */
anthony46a369d2010-05-19 02:41:48 +00001162 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1163 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001164
anthonyc1061722010-05-14 06:23:49 +00001165 /* rotate the 1D kernel by given angle */
anthony501c2f92010-06-02 10:55:14 +00001166 RotateKernelInfo(kernel, args->xi );
anthony602ab9b2010-01-05 08:06:50 +00001167 break;
1168 }
1169 case CometKernel:
1170 { double
anthony9eb4f742010-05-18 02:45:54 +00001171 sigma = fabs(args->sigma),
1172 A;
anthony602ab9b2010-01-05 08:06:50 +00001173
anthony602ab9b2010-01-05 08:06:50 +00001174 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001175 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001176 else
cristybb503372010-05-27 20:51:26 +00001177 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001178 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001179 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001180 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001181 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1182 kernel->height*sizeof(double));
1183 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001184 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001185
anthonyc1061722010-05-14 06:23:49 +00001186 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001187 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001188 ** curve to use so may change in the future. The function must be
1189 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001190 **
1191 ** As we are normalizing and not subtracting gaussians,
1192 ** there is no need for a divisor in the gaussian formula
1193 **
anthony43c49252010-05-18 10:59:50 +00001194 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001195 */
anthony9eb4f742010-05-18 02:45:54 +00001196 if ( sigma > MagickEpsilon )
1197 {
anthony602ab9b2010-01-05 08:06:50 +00001198#if 1
1199#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001200 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001201 (void) ResetMagickMemory(kernel->values,0, (size_t)
1202 kernel->width*sizeof(double));
1203 sigma *= KernelRank; /* simplify the loop expression */
1204 A = 1.0/(2.0*sigma*sigma);
1205 /* B = 1.0/(MagickSQ2PI*sigma); */
1206 for ( u=0; u < v; u++) {
1207 kernel->values[u/KernelRank] +=
1208 exp(-((double)(u*u))*A);
1209 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1210 }
cristybb503372010-05-27 20:51:26 +00001211 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001212 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001213#else
anthony9eb4f742010-05-18 02:45:54 +00001214 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1215 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001216 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001217 kernel->positive_range +=
1218 kernel->values[i] =
1219 exp(-((double)(i*i))*A);
1220 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001221#endif
anthony9eb4f742010-05-18 02:45:54 +00001222 }
1223 else /* special case - generate a unity kernel */
1224 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1225 kernel->width*kernel->height*sizeof(double));
1226 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1227 kernel->positive_range = 1.0;
1228 }
anthony46a369d2010-05-19 02:41:48 +00001229
1230 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001231 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001232 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001233
anthony999bb2c2010-02-18 12:38:01 +00001234 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1235 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001236 break;
1237 }
anthonyc1061722010-05-14 06:23:49 +00001238
anthony3c10fc82010-05-13 02:40:51 +00001239 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001240 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001241 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001242 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001243 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001244 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001245 break;
anthony9eb4f742010-05-18 02:45:54 +00001246 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001247 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001248 break;
1249 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001250 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1251 break;
1252 case 3:
anthonyc1061722010-05-14 06:23:49 +00001253 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001254 break;
anthony9eb4f742010-05-18 02:45:54 +00001255 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001256 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001257 "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 +00001258 break;
anthony9eb4f742010-05-18 02:45:54 +00001259 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001260 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001261 "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 +00001262 break;
anthony501c2f92010-06-02 10:55:14 +00001263 case 15: /* a 5x5 LoG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001264 kernel=ParseKernelArray(
1265 "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");
1266 break;
anthony501c2f92010-06-02 10:55:14 +00001267 case 19: /* a 9x9 LoG (sigma approx 1.4) */
anthony43c49252010-05-18 10:59:50 +00001268 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1269 kernel=ParseKernelArray(
anthonybfb635a2010-06-04 00:18:04 +00001270 "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");
anthony43c49252010-05-18 10:59:50 +00001271 break;
anthony3c10fc82010-05-13 02:40:51 +00001272 }
1273 if (kernel == (KernelInfo *) NULL)
1274 return(kernel);
1275 kernel->type = type;
1276 break;
1277 }
anthonyc1061722010-05-14 06:23:49 +00001278 case SobelKernel:
anthonyc40ac1e2010-06-06 11:49:31 +00001279 { switch ( (int) args->rho ) {
1280 default:
1281 case 0:
1282 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
1283 if (kernel == (KernelInfo *) NULL)
1284 return(kernel);
1285 kernel->type = type;
1286 break;
1287 case 1:
1288 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
1289 if (kernel == (KernelInfo *) NULL)
1290 return(kernel);
1291 kernel->type = type;
1292 ScaleKernelInfo(kernel, 0.25, NoValue);
1293 break;
1294 case 2:
1295 kernel=ParseKernelArray("3: 1,2,0 2,0,-2 0,-2,-1");
1296 if (kernel == (KernelInfo *) NULL)
1297 return(kernel);
1298 kernel->type = type;
1299 ScaleKernelInfo(kernel, 0.25, NoValue);
1300 break;
1301 }
anthonyc1061722010-05-14 06:23:49 +00001302 break;
1303 }
1304 case RobertsKernel:
1305 {
anthony501c2f92010-06-02 10:55:14 +00001306 kernel=ParseKernelArray("3: 0,0,0 1,-1,0 0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001307 if (kernel == (KernelInfo *) NULL)
1308 return(kernel);
1309 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001310 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001311 break;
1312 }
1313 case PrewittKernel:
1314 {
anthony501c2f92010-06-02 10:55:14 +00001315 kernel=ParseKernelArray("3: 1,0,-1 1,0,-1 1,0,-1");
anthonyc1061722010-05-14 06:23:49 +00001316 if (kernel == (KernelInfo *) NULL)
1317 return(kernel);
1318 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001319 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001320 break;
1321 }
1322 case CompassKernel:
1323 {
anthony501c2f92010-06-02 10:55:14 +00001324 kernel=ParseKernelArray("3: 1,1,-1 1,-2,-1 1,1,-1");
anthonyc1061722010-05-14 06:23:49 +00001325 if (kernel == (KernelInfo *) NULL)
1326 return(kernel);
1327 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001328 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001329 break;
1330 }
anthony9eb4f742010-05-18 02:45:54 +00001331 case KirschKernel:
1332 {
anthony501c2f92010-06-02 10:55:14 +00001333 kernel=ParseKernelArray("3: 5,-3,-3 5,0,-3 5,-3,-3");
anthony9eb4f742010-05-18 02:45:54 +00001334 if (kernel == (KernelInfo *) NULL)
1335 return(kernel);
1336 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001337 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001338 break;
1339 }
anthonye2a60ce2010-05-19 12:30:40 +00001340 case FreiChenKernel:
anthony501c2f92010-06-02 10:55:14 +00001341 /* Direction is set to be left to right positive */
1342 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf -- RIGHT? */
1343 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf -- WRONG? */
anthony1dd091a2010-05-27 06:31:15 +00001344 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001345 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001346 case 0:
anthony501c2f92010-06-02 10:55:14 +00001347 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonyc3cd15b2010-05-27 06:05:40 +00001348 if (kernel == (KernelInfo *) NULL)
1349 return(kernel);
anthonyef33d9f2010-06-02 12:27:01 +00001350 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001351 kernel->values[3] = +MagickSQ2;
1352 kernel->values[5] = -MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001353 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001354 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001355 case 2:
1356 kernel=ParseKernelArray("3: 1,2,0 2,0,-2 0,-2,-1");
1357 if (kernel == (KernelInfo *) NULL)
1358 return(kernel);
1359 kernel->type = type;
1360 kernel->values[1] = kernel->values[3] = +MagickSQ2;
1361 kernel->values[5] = kernel->values[7] = -MagickSQ2;
1362 CalcKernelMetaData(kernel); /* recalculate meta-data */
1363 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1364 break;
1365 case 10:
1366 kernel=AcquireKernelInfo("FreiChen:11;FreiChen:12;FreiChen:13;FreiChen:14;FreiChen:15;FreiChen:16;FreiChen:17;FreiChen:18;FreiChen:19");
1367 if (kernel == (KernelInfo *) NULL)
1368 return(kernel);
1369 break;
anthonye2a60ce2010-05-19 12:30:40 +00001370 case 1:
anthonyc40ac1e2010-06-06 11:49:31 +00001371 case 11:
anthony501c2f92010-06-02 10:55:14 +00001372 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonye2a60ce2010-05-19 12:30:40 +00001373 if (kernel == (KernelInfo *) NULL)
1374 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001375 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001376 kernel->values[3] = +MagickSQ2;
1377 kernel->values[5] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001378 CalcKernelMetaData(kernel); /* recalculate meta-data */
1379 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1380 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001381 case 12:
anthony501c2f92010-06-02 10:55:14 +00001382 kernel=ParseKernelArray("3: 1,2,1 0,0,0 1,2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001383 if (kernel == (KernelInfo *) NULL)
1384 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001385 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001386 kernel->values[1] = +MagickSQ2;
1387 kernel->values[7] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001388 CalcKernelMetaData(kernel);
1389 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1390 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001391 case 13:
anthony501c2f92010-06-02 10:55:14 +00001392 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001393 if (kernel == (KernelInfo *) NULL)
1394 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001395 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001396 kernel->values[0] = +MagickSQ2;
1397 kernel->values[8] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001398 CalcKernelMetaData(kernel);
1399 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1400 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001401 case 14:
anthony1d5e6702010-05-31 10:19:12 +00001402 kernel=ParseKernelArray("3: 0,1,-2 -1,0,1 2,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001403 if (kernel == (KernelInfo *) NULL)
1404 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001405 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001406 kernel->values[2] = -MagickSQ2;
1407 kernel->values[6] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001408 CalcKernelMetaData(kernel);
1409 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1410 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001411 case 15:
anthony501c2f92010-06-02 10:55:14 +00001412 kernel=ParseKernelArray("3: 0,-1,0 1,0,1 0,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001413 if (kernel == (KernelInfo *) NULL)
1414 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001415 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001416 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1417 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001418 case 16:
anthony1d5e6702010-05-31 10:19:12 +00001419 kernel=ParseKernelArray("3: 1,0,-1 0,0,0 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001420 if (kernel == (KernelInfo *) NULL)
1421 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001422 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001423 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1424 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001425 case 17:
anthony501c2f92010-06-02 10:55:14 +00001426 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 -1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001427 if (kernel == (KernelInfo *) NULL)
1428 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001429 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001430 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1431 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001432 case 18:
anthony501c2f92010-06-02 10:55:14 +00001433 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001434 if (kernel == (KernelInfo *) NULL)
1435 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001436 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001437 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1438 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001439 case 19:
anthonyc3cd15b2010-05-27 06:05:40 +00001440 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001441 if (kernel == (KernelInfo *) NULL)
1442 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001443 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001444 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1445 break;
1446 }
anthonyc3cd15b2010-05-27 06:05:40 +00001447 if ( fabs(args->sigma) > MagickEpsilon )
1448 /* Rotate by correctly supplied 'angle' */
1449 RotateKernelInfo(kernel, args->sigma);
1450 else if ( args->rho > 30.0 || args->rho < -30.0 )
1451 /* Rotate by out of bounds 'type' */
1452 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001453 break;
1454 }
1455
anthonyc1061722010-05-14 06:23:49 +00001456 /* Boolean Kernels */
1457 case DiamondKernel:
1458 {
1459 if (args->rho < 1.0)
1460 kernel->width = kernel->height = 3; /* default radius = 1 */
1461 else
cristybb503372010-05-27 20:51:26 +00001462 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1463 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001464
1465 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1466 kernel->height*sizeof(double));
1467 if (kernel->values == (double *) NULL)
1468 return(DestroyKernelInfo(kernel));
1469
1470 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001471 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1472 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony1d5e6702010-05-31 10:19:12 +00001473 if ( (labs((long) u)+labs((long) v)) <= (long) kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001474 kernel->positive_range += kernel->values[i] = args->sigma;
1475 else
1476 kernel->values[i] = nan;
1477 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1478 break;
1479 }
1480 case SquareKernel:
1481 case RectangleKernel:
1482 { double
1483 scale;
anthony602ab9b2010-01-05 08:06:50 +00001484 if ( type == SquareKernel )
1485 {
1486 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001487 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001488 else
cristybb503372010-05-27 20:51:26 +00001489 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1490 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001491 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001492 }
1493 else {
cristy2be15382010-01-21 02:38:03 +00001494 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001495 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001496 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001497 kernel->width = (size_t)args->rho;
1498 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001499 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1500 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001501 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001502 kernel->x = (ssize_t) args->xi;
1503 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001504 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001505 }
1506 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1507 kernel->height*sizeof(double));
1508 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001509 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001510
anthony3dd0f622010-05-13 12:57:32 +00001511 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001512 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001513 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001514 kernel->values[i] = scale;
1515 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1516 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001517 break;
anthony602ab9b2010-01-05 08:06:50 +00001518 }
anthony602ab9b2010-01-05 08:06:50 +00001519 case DiskKernel:
1520 {
anthonye4d89962010-05-29 10:53:11 +00001521 ssize_t
1522 limit = (ssize_t)(args->rho*args->rho);
1523
1524 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001525 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001526 else
anthonye4d89962010-05-29 10:53:11 +00001527 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001528 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001529
1530 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1531 kernel->height*sizeof(double));
1532 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001533 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001534
anthony3dd0f622010-05-13 12:57:32 +00001535 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001536 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1537 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001538 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001539 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001540 else
1541 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001542 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001543 break;
1544 }
1545 case PlusKernel:
1546 {
1547 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001548 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001549 else
cristybb503372010-05-27 20:51:26 +00001550 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1551 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001552
1553 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1554 kernel->height*sizeof(double));
1555 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001556 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001557
cristycee97112010-05-28 00:44:52 +00001558 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001559 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1560 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001561 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1562 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1563 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001564 break;
1565 }
anthony3dd0f622010-05-13 12:57:32 +00001566 case CrossKernel:
1567 {
1568 if (args->rho < 1.0)
1569 kernel->width = kernel->height = 5; /* default radius 2 */
1570 else
cristybb503372010-05-27 20:51:26 +00001571 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1572 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001573
1574 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1575 kernel->height*sizeof(double));
1576 if (kernel->values == (double *) NULL)
1577 return(DestroyKernelInfo(kernel));
1578
cristycee97112010-05-28 00:44:52 +00001579 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001580 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1581 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001582 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1583 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1584 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1585 break;
1586 }
1587 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001588 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001589 case PeaksKernel:
1590 {
cristybb503372010-05-27 20:51:26 +00001591 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001592 limit1,
anthonyc1061722010-05-14 06:23:49 +00001593 limit2,
1594 scale;
anthony3dd0f622010-05-13 12:57:32 +00001595
1596 if (args->rho < args->sigma)
1597 {
cristybb503372010-05-27 20:51:26 +00001598 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001599 limit1 = (ssize_t)(args->rho*args->rho);
1600 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001601 }
1602 else
1603 {
cristybb503372010-05-27 20:51:26 +00001604 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001605 limit1 = (ssize_t)(args->sigma*args->sigma);
1606 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001607 }
anthonyc1061722010-05-14 06:23:49 +00001608 if ( limit2 <= 0 )
1609 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1610
anthony3dd0f622010-05-13 12:57:32 +00001611 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001612 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001613 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1614 kernel->height*sizeof(double));
1615 if (kernel->values == (double *) NULL)
1616 return(DestroyKernelInfo(kernel));
1617
anthonyc1061722010-05-14 06:23:49 +00001618 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001619 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1620 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1621 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1622 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001623 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001624 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001625 else
1626 kernel->values[i] = nan;
1627 }
cristye96405a2010-05-19 02:24:31 +00001628 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001629 if ( type == PeaksKernel ) {
1630 /* set the central point in the middle */
1631 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1632 kernel->positive_range = 1.0;
1633 kernel->maximum = 1.0;
1634 }
anthony3dd0f622010-05-13 12:57:32 +00001635 break;
1636 }
anthony43c49252010-05-18 10:59:50 +00001637 case EdgesKernel:
1638 {
1639 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1640 if (kernel == (KernelInfo *) NULL)
1641 return(kernel);
1642 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001643 ExpandMirrorKernelInfo(kernel); /* mirror expansion of other kernels */
anthony43c49252010-05-18 10:59:50 +00001644 break;
1645 }
anthony3dd0f622010-05-13 12:57:32 +00001646 case CornersKernel:
1647 {
anthony4f1dcb72010-05-14 08:43:10 +00001648 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001649 if (kernel == (KernelInfo *) NULL)
1650 return(kernel);
1651 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001652 ExpandRotateKernelInfo(kernel, 90.0); /* Expand 90 degree rotations */
anthony3dd0f622010-05-13 12:57:32 +00001653 break;
1654 }
1655 case LineEndsKernel:
1656 {
anthony43c49252010-05-18 10:59:50 +00001657 KernelInfo
1658 *new_kernel;
1659 kernel=ParseKernelArray("3: 0,0,0 0,1,0 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001660 if (kernel == (KernelInfo *) NULL)
1661 return(kernel);
1662 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001663 ExpandRotateKernelInfo(kernel, 90.0);
anthony43c49252010-05-18 10:59:50 +00001664 /* append second set of 4 kernels */
1665 new_kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1666 if (new_kernel == (KernelInfo *) NULL)
1667 return(DestroyKernelInfo(kernel));
1668 new_kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001669 ExpandRotateKernelInfo(new_kernel, 90.0);
anthony43c49252010-05-18 10:59:50 +00001670 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001671 break;
1672 }
1673 case LineJunctionsKernel:
1674 {
1675 KernelInfo
1676 *new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001677 /* first set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001678 kernel=ParseKernelArray("3: -,1,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001679 if (kernel == (KernelInfo *) NULL)
1680 return(kernel);
1681 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001682 ExpandRotateKernelInfo(kernel, 45.0);
anthony3dd0f622010-05-13 12:57:32 +00001683 /* append second set of 4 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001684 new_kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
anthony3dd0f622010-05-13 12:57:32 +00001685 if (new_kernel == (KernelInfo *) NULL)
1686 return(DestroyKernelInfo(kernel));
anthony43c49252010-05-18 10:59:50 +00001687 new_kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001688 ExpandRotateKernelInfo(new_kernel, 90.0);
anthony3dd0f622010-05-13 12:57:32 +00001689 LastKernelInfo(kernel)->next = new_kernel;
anthony4f1dcb72010-05-14 08:43:10 +00001690 break;
1691 }
anthonyc40ac1e2010-06-06 11:49:31 +00001692 case RidgesKernel:
1693 { /* Ridges - Ridge finding kernels */
1694 KernelInfo
1695 *new_kernel;
1696 switch ( (int) args->rho ) {
1697 case 1:
1698 default:
1699 kernel=ParseKernelArray("3x1:0,1,0");
1700 if (kernel == (KernelInfo *) NULL)
1701 return(kernel);
1702 kernel->type = type;
1703 ExpandRotateKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1704 break;
1705 case 2:
1706 kernel=ParseKernelArray("4x1:0,1,1,0");
1707 if (kernel == (KernelInfo *) NULL)
1708 return(kernel);
1709 kernel->type = type;
1710 ExpandRotateKernelInfo(kernel, 90.0); /* 4 rotated kernels */
1711#if 0
1712 /* 2 pixel diagonaly thick - 4 rotates - not needed? */
1713 new_kernel=ParseKernelArray("4x4>:0,-,-,- -,1,-,- -,-,1,- -,-,-,0'");
1714 if (new_kernel == (KernelInfo *) NULL)
1715 return(DestroyKernelInfo(kernel));
1716 new_kernel->type = type;
1717 ExpandRotateKernelInfo(new_kernel, 90.0); /* 4 rotated kernels */
1718 LastKernelInfo(kernel)->next = new_kernel;
1719#endif
1720 /* kernels to find a stepped 'thick' line, 4 rotates + mirrors */
1721 /* Unfortunatally we can not yet rotate a non-square kernel */
1722 /* But then we can't flip a non-symetrical kernel either */
1723 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1724 if (new_kernel == (KernelInfo *) NULL)
1725 return(DestroyKernelInfo(kernel));
1726 new_kernel->type = type;
1727 LastKernelInfo(kernel)->next = new_kernel;
1728 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
1729 if (new_kernel == (KernelInfo *) NULL)
1730 return(DestroyKernelInfo(kernel));
1731 new_kernel->type = type;
1732 LastKernelInfo(kernel)->next = new_kernel;
1733 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
1734 if (new_kernel == (KernelInfo *) NULL)
1735 return(DestroyKernelInfo(kernel));
1736 new_kernel->type = type;
1737 LastKernelInfo(kernel)->next = new_kernel;
1738 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
1739 if (new_kernel == (KernelInfo *) NULL)
1740 return(DestroyKernelInfo(kernel));
1741 new_kernel->type = type;
1742 LastKernelInfo(kernel)->next = new_kernel;
1743 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
1744 if (new_kernel == (KernelInfo *) NULL)
1745 return(DestroyKernelInfo(kernel));
1746 new_kernel->type = type;
1747 LastKernelInfo(kernel)->next = new_kernel;
1748 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
1749 if (new_kernel == (KernelInfo *) NULL)
1750 return(DestroyKernelInfo(kernel));
1751 new_kernel->type = type;
1752 LastKernelInfo(kernel)->next = new_kernel;
1753 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
1754 if (new_kernel == (KernelInfo *) NULL)
1755 return(DestroyKernelInfo(kernel));
1756 new_kernel->type = type;
1757 LastKernelInfo(kernel)->next = new_kernel;
1758 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
1759 if (new_kernel == (KernelInfo *) NULL)
1760 return(DestroyKernelInfo(kernel));
1761 new_kernel->type = type;
1762 LastKernelInfo(kernel)->next = new_kernel;
1763 break;
1764 }
1765 break;
1766 }
anthony3dd0f622010-05-13 12:57:32 +00001767 case ConvexHullKernel:
1768 {
anthony3928ec62010-05-27 14:03:29 +00001769 KernelInfo
1770 *new_kernel;
1771 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001772 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001773 if (kernel == (KernelInfo *) NULL)
1774 return(kernel);
1775 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001776 ExpandRotateKernelInfo(kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001777 /* append the mirror versions too */
1778 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1779 if (new_kernel == (KernelInfo *) NULL)
1780 return(DestroyKernelInfo(kernel));
1781 new_kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001782 ExpandRotateKernelInfo(new_kernel, 45.0);
anthony5b93cbe2010-05-27 13:54:14 +00001783 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001784 break;
1785 }
anthony47f5d062010-05-23 07:47:50 +00001786 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001787 {
1788 KernelInfo
1789 *new_kernel;
anthonyc40ac1e2010-06-06 11:49:31 +00001790 switch ( (int) args->rho ) {
1791 case 1:
1792 default:
1793 /* Traditional Skeleton...
1794 ** A cyclically rotated single kernel
1795 */
1796 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1797 if (kernel == (KernelInfo *) NULL)
1798 return(kernel);
1799 kernel->type = type;
1800 ExpandRotateKernelInfo(kernel, 45.0); /* 8 rotations */
1801 break;
1802 case 2:
1803 /* HIPR Variation of the cyclic skeleton
1804 ** Corners of the traditional method made more forgiving,
1805 ** but the retain the same cyclic order.
1806 */
1807 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1808 if (kernel == (KernelInfo *) NULL)
1809 return(kernel);
1810 kernel->type = type;
1811 new_kernel=ParseKernelArray("3: -,0,0 1,1,0 -,1,-");
1812 if (new_kernel == (KernelInfo *) NULL)
1813 return(new_kernel);
1814 new_kernel->type = type;
1815 LastKernelInfo(kernel)->next = new_kernel;
1816 ExpandRotateKernelInfo(kernel, 90.0); /* 4 rotations of the 2 kernels */
1817 break;
1818 case 3:
1819 /* Jittered Skeleton: do top, then bottom, then each sides */
1820 /* Do top edge */
1821 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1822 if (kernel == (KernelInfo *) NULL)
1823 return(kernel);
1824 kernel->type = type;
1825 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
1826 if (new_kernel == (KernelInfo *) NULL)
1827 return(new_kernel);
1828 new_kernel->type = type;
1829 LastKernelInfo(kernel)->next = new_kernel;
1830 new_kernel=ParseKernelArray("3: -,0,0 1,1,0 -,1,-");
1831 if (new_kernel == (KernelInfo *) NULL)
1832 return(new_kernel);
1833 new_kernel->type = type;
1834 LastKernelInfo(kernel)->next = new_kernel;
1835 /* Do Bottom edge */
1836 new_kernel=ParseKernelArray("3: 1,1,1 -,1,- 0,0,0");
1837 if (new_kernel == (KernelInfo *) NULL)
1838 return(new_kernel);
1839 new_kernel->type = type;
1840 LastKernelInfo(kernel)->next = new_kernel;
1841 new_kernel=ParseKernelArray("3: -,1,- 1,1,0 -,0,0");
1842 if (new_kernel == (KernelInfo *) NULL)
1843 return(new_kernel);
1844 new_kernel->type = type;
1845 LastKernelInfo(kernel)->next = new_kernel;
1846 new_kernel=ParseKernelArray("3: -,1,- 0,1,1 0,0,-");
1847 if (new_kernel == (KernelInfo *) NULL)
1848 return(new_kernel);
1849 new_kernel->type = type;
1850 LastKernelInfo(kernel)->next = new_kernel;
1851 /* Last the two sides */
1852 new_kernel=ParseKernelArray("3: 0,-,1 0,1,1 0,-,1");
1853 if (new_kernel == (KernelInfo *) NULL)
1854 return(new_kernel);
1855 new_kernel->type = type;
1856 LastKernelInfo(kernel)->next = new_kernel;
1857 new_kernel=ParseKernelArray("3: 1,-,0 1,1,0 1,-,0");
1858 if (new_kernel == (KernelInfo *) NULL)
1859 return(new_kernel);
1860 new_kernel->type = type;
1861 LastKernelInfo(kernel)->next = new_kernel;
1862 break;
1863 case 4:
1864 /* Just a simple 'Edge' kernel, but with a extra two kernels
1865 ** to finish off diagonal lines, top then bottom then sides.
1866 ** Works well for test case but fails for general case.
1867 */
1868 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1869 if (kernel == (KernelInfo *) NULL)
1870 return(kernel);
1871 kernel->type = type;
1872 new_kernel=ParseKernelArray("3: 0,0,0 0,1,1 1,1,-");
1873 if (new_kernel == (KernelInfo *) NULL)
1874 return(DestroyKernelInfo(kernel));
1875 new_kernel->type = type;
1876 LastKernelInfo(kernel)->next = new_kernel;
1877 new_kernel=ParseKernelArray("3: 0,0,0 1,1,0 -,1,1");
1878 if (new_kernel == (KernelInfo *) NULL)
1879 return(DestroyKernelInfo(kernel));
1880 new_kernel->type = type;
1881 LastKernelInfo(kernel)->next = new_kernel;
1882 ExpandMirrorKernelInfo(kernel);
1883 break;
1884 }
anthonya648a302010-05-27 02:14:36 +00001885 break;
1886 }
anthony602ab9b2010-01-05 08:06:50 +00001887 /* Distance Measuring Kernels */
1888 case ChebyshevKernel:
1889 {
anthony602ab9b2010-01-05 08:06:50 +00001890 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001891 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001892 else
cristybb503372010-05-27 20:51:26 +00001893 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1894 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001895
1896 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1897 kernel->height*sizeof(double));
1898 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001899 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001900
cristybb503372010-05-27 20:51:26 +00001901 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1902 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001903 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001904 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001905 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001906 break;
1907 }
anthonybee715c2010-06-04 01:25:57 +00001908 case ManhattanKernel:
anthony602ab9b2010-01-05 08:06:50 +00001909 {
anthony602ab9b2010-01-05 08:06:50 +00001910 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001911 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001912 else
cristybb503372010-05-27 20:51:26 +00001913 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1914 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001915
1916 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1917 kernel->height*sizeof(double));
1918 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001919 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001920
cristybb503372010-05-27 20:51:26 +00001921 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1922 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001923 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00001924 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00001925 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001926 break;
1927 }
1928 case EuclideanKernel:
1929 {
anthony602ab9b2010-01-05 08:06:50 +00001930 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001931 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001932 else
cristybb503372010-05-27 20:51:26 +00001933 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1934 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001935
1936 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1937 kernel->height*sizeof(double));
1938 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001939 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001940
cristybb503372010-05-27 20:51:26 +00001941 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1942 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00001943 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00001944 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00001945 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00001946 break;
1947 }
anthony46a369d2010-05-19 02:41:48 +00001948 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00001949 default:
anthonyc1061722010-05-14 06:23:49 +00001950 {
anthony46a369d2010-05-19 02:41:48 +00001951 /* Unity or No-Op Kernel - 3x3 with 1 in center */
1952 kernel=ParseKernelArray("3:0,0,0,0,1,0,0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001953 if (kernel == (KernelInfo *) NULL)
1954 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00001955 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00001956 break;
1957 }
anthony602ab9b2010-01-05 08:06:50 +00001958 break;
1959 }
1960
1961 return(kernel);
1962}
anthonyc94cdb02010-01-06 08:15:29 +00001963
anthony602ab9b2010-01-05 08:06:50 +00001964/*
1965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1966% %
1967% %
1968% %
cristy6771f1e2010-03-05 19:43:39 +00001969% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00001970% %
1971% %
1972% %
1973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1974%
anthony1b2bc0a2010-05-12 05:25:22 +00001975% CloneKernelInfo() creates a new clone of the given Kernel List so that its
1976% can be modified without effecting the original. The cloned kernel should
cristybb503372010-05-27 20:51:26 +00001977% be destroyed using DestoryKernelInfo() when no ssize_ter needed.
anthony7a01dcf2010-05-11 12:25:52 +00001978%
cristye6365592010-04-02 17:31:23 +00001979% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00001980%
anthony930be612010-02-08 04:26:15 +00001981% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001982%
1983% A description of each parameter follows:
1984%
1985% o kernel: the Morphology/Convolution kernel to be cloned
1986%
1987*/
cristyef656912010-03-05 19:54:59 +00001988MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00001989{
cristybb503372010-05-27 20:51:26 +00001990 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00001991 i;
1992
cristy19eb6412010-04-23 14:42:29 +00001993 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00001994 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00001995
1996 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00001997 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
1998 if (new_kernel == (KernelInfo *) NULL)
1999 return(new_kernel);
2000 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00002001
2002 /* replace the values with a copy of the values */
2003 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00002004 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00002005 if (new_kernel->values == (double *) NULL)
2006 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00002007 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00002008 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00002009
2010 /* Also clone the next kernel in the kernel list */
2011 if ( kernel->next != (KernelInfo *) NULL ) {
2012 new_kernel->next = CloneKernelInfo(kernel->next);
2013 if ( new_kernel->next == (KernelInfo *) NULL )
2014 return(DestroyKernelInfo(new_kernel));
2015 }
2016
anthony7a01dcf2010-05-11 12:25:52 +00002017 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00002018}
2019
2020/*
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022% %
2023% %
2024% %
anthony83ba99b2010-01-24 08:48:15 +00002025% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00002026% %
2027% %
2028% %
2029%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2030%
anthony83ba99b2010-01-24 08:48:15 +00002031% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
2032% kernel.
anthony602ab9b2010-01-05 08:06:50 +00002033%
anthony83ba99b2010-01-24 08:48:15 +00002034% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00002035%
anthony83ba99b2010-01-24 08:48:15 +00002036% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00002037%
2038% A description of each parameter follows:
2039%
2040% o kernel: the Morphology/Convolution kernel to be destroyed
2041%
2042*/
anthony83ba99b2010-01-24 08:48:15 +00002043MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00002044{
cristy2be15382010-01-21 02:38:03 +00002045 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00002046
anthony7a01dcf2010-05-11 12:25:52 +00002047 if ( kernel->next != (KernelInfo *) NULL )
2048 kernel->next = DestroyKernelInfo(kernel->next);
2049
2050 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
2051 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00002052 return(kernel);
2053}
anthonyc94cdb02010-01-06 08:15:29 +00002054
2055/*
2056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2057% %
2058% %
2059% %
anthonybfb635a2010-06-04 00:18:04 +00002060% E x p a n d M i r r o r K e r n e l I n f o %
anthony3c10fc82010-05-13 02:40:51 +00002061% %
2062% %
2063% %
2064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065%
anthonybfb635a2010-06-04 00:18:04 +00002066% ExpandMirrorKernelInfo() takes a single kernel, and expands it into a
2067% sequence of 90-degree rotated kernels but providing a reflected 180
2068% rotatation, before the -/+ 90-degree rotations.
2069%
2070% This special rotation order produces a better, more symetrical thinning of
2071% objects.
2072%
2073% The format of the ExpandMirrorKernelInfo method is:
2074%
2075% void ExpandMirrorKernelInfo(KernelInfo *kernel)
2076%
2077% A description of each parameter follows:
2078%
2079% o kernel: the Morphology/Convolution kernel
2080%
2081% This function is only internel to this module, as it is not finalized,
2082% especially with regard to non-orthogonal angles, and rotation of larger
2083% 2D kernels.
2084*/
2085
2086#if 0
2087static void FlopKernelInfo(KernelInfo *kernel)
2088 { /* Do a Flop by reversing each row. */
2089 size_t
2090 y;
2091 register ssize_t
2092 x,r;
2093 register double
2094 *k,t;
2095
2096 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
2097 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
2098 t=k[x], k[x]=k[r], k[r]=t;
2099
2100 kernel->x = kernel->width - kernel->x - 1;
2101 angle = fmod(angle+180.0, 360.0);
2102 }
2103#endif
2104
2105static void ExpandMirrorKernelInfo(KernelInfo *kernel)
2106{
2107 KernelInfo
2108 *clone,
2109 *last;
2110
2111 last = kernel;
2112
2113 clone = CloneKernelInfo(last);
2114 RotateKernelInfo(clone, 180); /* flip */
2115 LastKernelInfo(last)->next = clone;
2116 last = clone;
2117
2118 clone = CloneKernelInfo(last);
2119 RotateKernelInfo(clone, 90); /* transpose */
2120 LastKernelInfo(last)->next = clone;
2121 last = clone;
2122
2123 clone = CloneKernelInfo(last);
2124 RotateKernelInfo(clone, 180); /* flop */
2125 LastKernelInfo(last)->next = clone;
2126
2127 return;
2128}
2129
2130/*
2131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2132% %
2133% %
2134% %
2135% E x p a n d R o t a t e K e r n e l I n f o %
2136% %
2137% %
2138% %
2139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2140%
2141% ExpandRotateKernelInfo() takes a kernel list, and expands it by rotating
2142% incrementally by the angle given, until the first kernel repeats.
anthony3c10fc82010-05-13 02:40:51 +00002143%
2144% WARNING: 45 degree rotations only works for 3x3 kernels.
2145% While 90 degree roatations only works for linear and square kernels
2146%
anthonybfb635a2010-06-04 00:18:04 +00002147% The format of the ExpandRotateKernelInfo method is:
anthony3c10fc82010-05-13 02:40:51 +00002148%
anthonybfb635a2010-06-04 00:18:04 +00002149% void ExpandRotateKernelInfo(KernelInfo *kernel, double angle)
anthony3c10fc82010-05-13 02:40:51 +00002150%
2151% A description of each parameter follows:
2152%
2153% o kernel: the Morphology/Convolution kernel
2154%
2155% o angle: angle to rotate in degrees
2156%
2157% This function is only internel to this module, as it is not finalized,
2158% especially with regard to non-orthogonal angles, and rotation of larger
2159% 2D kernels.
2160*/
anthony47f5d062010-05-23 07:47:50 +00002161
2162/* Internal Routine - Return true if two kernels are the same */
2163static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
2164 const KernelInfo *kernel2)
2165{
cristybb503372010-05-27 20:51:26 +00002166 register size_t
anthony47f5d062010-05-23 07:47:50 +00002167 i;
anthony1d45eb92010-05-25 11:13:23 +00002168
2169 /* check size and origin location */
2170 if ( kernel1->width != kernel2->width
2171 || kernel1->height != kernel2->height
2172 || kernel1->x != kernel2->x
2173 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00002174 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002175
2176 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00002177 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00002178 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00002179 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
2180 return MagickFalse;
2181 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2182 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002183 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002184 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2185 return MagickFalse;
2186 }
anthony1d45eb92010-05-25 11:13:23 +00002187
anthony47f5d062010-05-23 07:47:50 +00002188 return MagickTrue;
2189}
2190
anthonybfb635a2010-06-04 00:18:04 +00002191static void ExpandRotateKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002192{
2193 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002194 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002195 *last;
cristya9a61ad2010-05-13 12:47:41 +00002196
anthony3c10fc82010-05-13 02:40:51 +00002197 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002198 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002199 clone = CloneKernelInfo(last);
2200 RotateKernelInfo(clone, angle);
2201 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002202 break;
anthonybfb635a2010-06-04 00:18:04 +00002203 LastKernelInfo(last)->next = clone;
cristy84d9b552010-05-24 18:23:54 +00002204 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002205 }
anthonybfb635a2010-06-04 00:18:04 +00002206 clone = DestroyKernelInfo(clone); /* kernel has repeated - junk the clone */
anthony47f5d062010-05-23 07:47:50 +00002207 return;
anthony3c10fc82010-05-13 02:40:51 +00002208}
2209
2210/*
2211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212% %
2213% %
2214% %
anthony46a369d2010-05-19 02:41:48 +00002215+ C a l c M e t a K e r n a l I n f o %
2216% %
2217% %
2218% %
2219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220%
2221% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2222% using the kernel values. This should only ne used if it is not posible to
2223% calculate that meta-data in some easier way.
2224%
2225% It is important that the meta-data is correct before ScaleKernelInfo() is
2226% used to perform kernel normalization.
2227%
2228% The format of the CalcKernelMetaData method is:
2229%
2230% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2231%
2232% A description of each parameter follows:
2233%
2234% o kernel: the Morphology/Convolution kernel to modify
2235%
2236% WARNING: Minimum and Maximum values are assumed to include zero, even if
2237% zero is not part of the kernel (as in Gaussian Derived kernels). This
2238% however is not true for flat-shaped morphological kernels.
2239%
2240% WARNING: Only the specific kernel pointed to is modified, not a list of
2241% multiple kernels.
2242%
2243% This is an internal function and not expected to be useful outside this
2244% module. This could change however.
2245*/
2246static void CalcKernelMetaData(KernelInfo *kernel)
2247{
cristybb503372010-05-27 20:51:26 +00002248 register size_t
anthony46a369d2010-05-19 02:41:48 +00002249 i;
2250
2251 kernel->minimum = kernel->maximum = 0.0;
2252 kernel->negative_range = kernel->positive_range = 0.0;
2253 for (i=0; i < (kernel->width*kernel->height); i++)
2254 {
2255 if ( fabs(kernel->values[i]) < MagickEpsilon )
2256 kernel->values[i] = 0.0;
2257 ( kernel->values[i] < 0)
2258 ? ( kernel->negative_range += kernel->values[i] )
2259 : ( kernel->positive_range += kernel->values[i] );
2260 Minimize(kernel->minimum, kernel->values[i]);
2261 Maximize(kernel->maximum, kernel->values[i]);
2262 }
2263
2264 return;
2265}
2266
2267/*
2268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2269% %
2270% %
2271% %
anthony9eb4f742010-05-18 02:45:54 +00002272% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002273% %
2274% %
2275% %
2276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2277%
anthony9eb4f742010-05-18 02:45:54 +00002278% MorphologyApply() applies a morphological method, multiple times using
2279% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002280%
anthony9eb4f742010-05-18 02:45:54 +00002281% It is basically equivelent to as MorphologyImageChannel() (see below) but
anthonye8d2f552010-06-05 10:43:25 +00002282% without any user controls. This allows internel programs to use this
2283% function, to actually perform a specific task without posible interference
2284% by any API user supplied settings.
2285%
2286% It is MorphologyImageChannel() task to extract any such user controls, and
2287% pass them to this function for processing.
anthony9eb4f742010-05-18 02:45:54 +00002288%
2289% More specifically kernels are not normalized/scaled/blended by the
anthonye8d2f552010-06-05 10:43:25 +00002290% 'convolve:scale' Image Artifact (setting), nor is the convolve bias
2291% (-bias setting or image->bias) loooked at, but must be supplied from the
2292% function arguments.
anthony602ab9b2010-01-05 08:06:50 +00002293%
anthony47f5d062010-05-23 07:47:50 +00002294% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002295%
anthony9eb4f742010-05-18 02:45:54 +00002296% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002297% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002298% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002299% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002300%
2301% A description of each parameter follows:
2302%
2303% o image: the image.
2304%
2305% o method: the morphology method to be applied.
2306%
2307% o iterations: apply the operation this many times (or no change).
2308% A value of -1 means loop until no change found.
2309% How this is applied may depend on the morphology method.
2310% Typically this is a value of 1.
2311%
2312% o channel: the channel type.
2313%
2314% o kernel: An array of double representing the morphology kernel.
anthony29188a82010-01-22 10:12:34 +00002315% Warning: kernel may be normalized for the Convolve method.
anthony602ab9b2010-01-05 08:06:50 +00002316%
anthony47f5d062010-05-23 07:47:50 +00002317% o compose: How to handle or merge multi-kernel results.
2318% If 'Undefined' use default of the Morphology method.
2319% If 'No' force image to be re-iterated by each kernel.
2320% Otherwise merge the results using the mathematical compose
2321% method given.
2322%
2323% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002324%
anthony602ab9b2010-01-05 08:06:50 +00002325% o exception: return any errors or warnings in this structure.
2326%
anthony602ab9b2010-01-05 08:06:50 +00002327*/
2328
anthony930be612010-02-08 04:26:15 +00002329
anthony9eb4f742010-05-18 02:45:54 +00002330/* Apply a Morphology Primative to an image using the given kernel.
2331** Two pre-created images must be provided, no image is created.
2332** Returning the number of pixels that changed.
2333*/
cristybb503372010-05-27 20:51:26 +00002334static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002335 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002336 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002337{
cristy2be15382010-01-21 02:38:03 +00002338#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002339
cristy5f959472010-05-27 22:19:46 +00002340 CacheView
2341 *p_view,
2342 *q_view;
2343
cristybb503372010-05-27 20:51:26 +00002344 ssize_t
anthony29188a82010-01-22 10:12:34 +00002345 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002346 changed;
2347
2348 MagickBooleanType
2349 status;
2350
cristy5f959472010-05-27 22:19:46 +00002351 MagickOffsetType
2352 progress;
anthony602ab9b2010-01-05 08:06:50 +00002353
anthonye4d89962010-05-29 10:53:11 +00002354 assert(image != (Image *) NULL);
2355 assert(image->signature == MagickSignature);
2356 assert(result_image != (Image *) NULL);
2357 assert(result_image->signature == MagickSignature);
2358 assert(kernel != (KernelInfo *) NULL);
2359 assert(kernel->signature == MagickSignature);
2360 assert(exception != (ExceptionInfo *) NULL);
2361 assert(exception->signature == MagickSignature);
2362
anthony602ab9b2010-01-05 08:06:50 +00002363 status=MagickTrue;
2364 changed=0;
2365 progress=0;
2366
anthony602ab9b2010-01-05 08:06:50 +00002367 p_view=AcquireCacheView(image);
2368 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002369
anthonycc6c8362010-01-25 04:14:01 +00002370 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002371 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002372 */
cristyc99304f2010-02-01 15:26:27 +00002373 offx = kernel->x;
2374 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002375 switch(method) {
anthony930be612010-02-08 04:26:15 +00002376 case ConvolveMorphology:
2377 case DilateMorphology:
2378 case DilateIntensityMorphology:
2379 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002380 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002381 offx = (ssize_t) kernel->width-offx-1;
2382 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002383 break;
anthony5ef8e942010-05-11 06:51:12 +00002384 case ErodeMorphology:
2385 case ErodeIntensityMorphology:
2386 case HitAndMissMorphology:
2387 case ThinningMorphology:
2388 case ThickenMorphology:
2389 /* kernel is user as is, without reflection */
2390 break;
anthony930be612010-02-08 04:26:15 +00002391 default:
anthony9eb4f742010-05-18 02:45:54 +00002392 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002393 break;
anthony29188a82010-01-22 10:12:34 +00002394 }
2395
anthony602ab9b2010-01-05 08:06:50 +00002396#if defined(MAGICKCORE_OPENMP_SUPPORT)
2397 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2398#endif
cristybb503372010-05-27 20:51:26 +00002399 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002400 {
2401 MagickBooleanType
2402 sync;
2403
2404 register const PixelPacket
2405 *restrict p;
2406
2407 register const IndexPacket
2408 *restrict p_indexes;
2409
2410 register PixelPacket
2411 *restrict q;
2412
2413 register IndexPacket
2414 *restrict q_indexes;
2415
cristybb503372010-05-27 20:51:26 +00002416 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002417 x;
2418
cristybb503372010-05-27 20:51:26 +00002419 size_t
anthony602ab9b2010-01-05 08:06:50 +00002420 r;
2421
2422 if (status == MagickFalse)
2423 continue;
anthony29188a82010-01-22 10:12:34 +00002424 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2425 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002426 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2427 exception);
2428 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2429 {
2430 status=MagickFalse;
2431 continue;
2432 }
2433 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2434 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony29188a82010-01-22 10:12:34 +00002435 r = (image->columns+kernel->width)*offy+offx; /* constant */
2436
cristybb503372010-05-27 20:51:26 +00002437 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002438 {
cristybb503372010-05-27 20:51:26 +00002439 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002440 v;
2441
cristybb503372010-05-27 20:51:26 +00002442 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002443 u;
2444
2445 register const double
2446 *restrict k;
2447
2448 register const PixelPacket
2449 *restrict k_pixels;
2450
2451 register const IndexPacket
2452 *restrict k_indexes;
2453
2454 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002455 result,
2456 min,
2457 max;
anthony602ab9b2010-01-05 08:06:50 +00002458
anthony29188a82010-01-22 10:12:34 +00002459 /* Copy input to ouput image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002460 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002461 */
anthony602ab9b2010-01-05 08:06:50 +00002462 *q = p[r];
2463 if (image->colorspace == CMYKColorspace)
2464 q_indexes[x] = p_indexes[r];
2465
anthony5ef8e942010-05-11 06:51:12 +00002466 /* Defaults */
2467 min.red =
2468 min.green =
2469 min.blue =
2470 min.opacity =
2471 min.index = (MagickRealType) QuantumRange;
2472 max.red =
2473 max.green =
2474 max.blue =
2475 max.opacity =
2476 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002477 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002478 result.red = (MagickRealType) p[r].red;
2479 result.green = (MagickRealType) p[r].green;
2480 result.blue = (MagickRealType) p[r].blue;
2481 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002482 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002483 if ( image->colorspace == CMYKColorspace)
2484 result.index = (MagickRealType) p_indexes[r];
2485
anthony602ab9b2010-01-05 08:06:50 +00002486 switch (method) {
2487 case ConvolveMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002488 /* Set the user defined bias of the weighted average output */
2489 result.red =
2490 result.green =
2491 result.blue =
2492 result.opacity =
2493 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002494 break;
anthony4fd27e22010-02-07 08:17:18 +00002495 case DilateIntensityMorphology:
2496 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002497 /* use a boolean flag indicating when first match found */
2498 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002499 break;
anthony602ab9b2010-01-05 08:06:50 +00002500 default:
anthony602ab9b2010-01-05 08:06:50 +00002501 break;
2502 }
2503
2504 switch ( method ) {
2505 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002506 /* Weighted Average of pixels using reflected kernel
2507 **
2508 ** NOTE for correct working of this operation for asymetrical
2509 ** kernels, the kernel needs to be applied in its reflected form.
2510 ** That is its values needs to be reversed.
2511 **
2512 ** Correlation is actually the same as this but without reflecting
2513 ** the kernel, and thus 'lower-level' that Convolution. However
2514 ** as Convolution is the more common method used, and it does not
2515 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002516 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002517 **
2518 ** Correlation will have its kernel reflected before calling
2519 ** this function to do a Convolve.
2520 **
2521 ** For more details of Correlation vs Convolution see
2522 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2523 */
anthony5ef8e942010-05-11 06:51:12 +00002524 if (((channel & SyncChannels) != 0 ) &&
2525 (image->matte == MagickTrue))
2526 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
2527 ** Weight the color channels with Alpha Channel so that
2528 ** transparent pixels are not part of the results.
2529 */
anthony602ab9b2010-01-05 08:06:50 +00002530 MagickRealType
anthony5ef8e942010-05-11 06:51:12 +00002531 alpha, /* color channel weighting : kernel*alpha */
2532 gamma; /* divisor, sum of weighting values */
anthony602ab9b2010-01-05 08:06:50 +00002533
2534 gamma=0.0;
anthony29188a82010-01-22 10:12:34 +00002535 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002536 k_pixels = p;
2537 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002538 for (v=0; v < (ssize_t) kernel->height; v++) {
2539 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002540 if ( IsNan(*k) ) continue;
2541 alpha=(*k)*(QuantumScale*(QuantumRange-
2542 k_pixels[u].opacity));
2543 gamma += alpha;
2544 result.red += alpha*k_pixels[u].red;
2545 result.green += alpha*k_pixels[u].green;
2546 result.blue += alpha*k_pixels[u].blue;
anthony83ba99b2010-01-24 08:48:15 +00002547 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002548 if ( image->colorspace == CMYKColorspace)
2549 result.index += alpha*k_indexes[u];
2550 }
2551 k_pixels += image->columns+kernel->width;
2552 k_indexes += image->columns+kernel->width;
2553 }
2554 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
anthony83ba99b2010-01-24 08:48:15 +00002555 result.red *= gamma;
2556 result.green *= gamma;
2557 result.blue *= gamma;
2558 result.opacity *= gamma;
2559 result.index *= gamma;
anthony602ab9b2010-01-05 08:06:50 +00002560 }
anthony5ef8e942010-05-11 06:51:12 +00002561 else
2562 {
2563 /* No 'Sync' flag, or no Alpha involved.
2564 ** Convolution is simple individual channel weigthed sum.
2565 */
2566 k = &kernel->values[ kernel->width*kernel->height-1 ];
2567 k_pixels = p;
2568 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002569 for (v=0; v < (ssize_t) kernel->height; v++) {
2570 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002571 if ( IsNan(*k) ) continue;
2572 result.red += (*k)*k_pixels[u].red;
2573 result.green += (*k)*k_pixels[u].green;
2574 result.blue += (*k)*k_pixels[u].blue;
2575 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
2576 if ( image->colorspace == CMYKColorspace)
2577 result.index += (*k)*k_indexes[u];
2578 }
2579 k_pixels += image->columns+kernel->width;
2580 k_indexes += image->columns+kernel->width;
2581 }
2582 }
anthony602ab9b2010-01-05 08:06:50 +00002583 break;
2584
anthony4fd27e22010-02-07 08:17:18 +00002585 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002586 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002587 **
2588 ** NOTE that the kernel is not reflected for this operation!
2589 **
2590 ** NOTE: in normal Greyscale Morphology, the kernel value should
2591 ** be added to the real value, this is currently not done, due to
2592 ** the nature of the boolean kernels being used.
2593 */
anthony4fd27e22010-02-07 08:17:18 +00002594 k = kernel->values;
2595 k_pixels = p;
2596 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002597 for (v=0; v < (ssize_t) kernel->height; v++) {
2598 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002599 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002600 Minimize(min.red, (double) k_pixels[u].red);
2601 Minimize(min.green, (double) k_pixels[u].green);
2602 Minimize(min.blue, (double) k_pixels[u].blue);
2603 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002604 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002605 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002606 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002607 }
2608 k_pixels += image->columns+kernel->width;
2609 k_indexes += image->columns+kernel->width;
2610 }
2611 break;
2612
anthony5ef8e942010-05-11 06:51:12 +00002613
anthony83ba99b2010-01-24 08:48:15 +00002614 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002615 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002616 **
2617 ** NOTE for correct working of this operation for asymetrical
2618 ** kernels, the kernel needs to be applied in its reflected form.
2619 ** That is its values needs to be reversed.
2620 **
2621 ** NOTE: in normal Greyscale Morphology, the kernel value should
2622 ** be added to the real value, this is currently not done, due to
2623 ** the nature of the boolean kernels being used.
2624 **
2625 */
anthony29188a82010-01-22 10:12:34 +00002626 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002627 k_pixels = p;
2628 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002629 for (v=0; v < (ssize_t) kernel->height; v++) {
2630 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002631 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002632 Maximize(max.red, (double) k_pixels[u].red);
2633 Maximize(max.green, (double) k_pixels[u].green);
2634 Maximize(max.blue, (double) k_pixels[u].blue);
2635 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002636 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002637 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002638 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002639 }
2640 k_pixels += image->columns+kernel->width;
2641 k_indexes += image->columns+kernel->width;
2642 }
anthony602ab9b2010-01-05 08:06:50 +00002643 break;
2644
anthony5ef8e942010-05-11 06:51:12 +00002645 case HitAndMissMorphology:
2646 case ThinningMorphology:
2647 case ThickenMorphology:
2648 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2649 **
2650 ** NOTE that the kernel is not reflected for this operation,
2651 ** and consists of both foreground and background pixel
2652 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2653 ** with either Nan or 0.5 values for don't care.
2654 **
anthony4c827ef2010-06-05 23:56:10 +00002655 ** Note that this will never produce a meaningless negative
2656 ** result. Such results can cause Thinning/Thicken to not work
2657 ** correctly when used against a greyscale image.
anthony5ef8e942010-05-11 06:51:12 +00002658 */
2659 k = kernel->values;
2660 k_pixels = p;
2661 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002662 for (v=0; v < (ssize_t) kernel->height; v++) {
2663 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002664 if ( IsNan(*k) ) continue;
2665 if ( (*k) > 0.7 )
2666 { /* minimim of foreground pixels */
2667 Minimize(min.red, (double) k_pixels[u].red);
2668 Minimize(min.green, (double) k_pixels[u].green);
2669 Minimize(min.blue, (double) k_pixels[u].blue);
2670 Minimize(min.opacity,
2671 QuantumRange-(double) k_pixels[u].opacity);
2672 if ( image->colorspace == CMYKColorspace)
2673 Minimize(min.index, (double) k_indexes[u]);
2674 }
2675 else if ( (*k) < 0.3 )
2676 { /* maximum of background pixels */
2677 Maximize(max.red, (double) k_pixels[u].red);
2678 Maximize(max.green, (double) k_pixels[u].green);
2679 Maximize(max.blue, (double) k_pixels[u].blue);
2680 Maximize(max.opacity,
2681 QuantumRange-(double) k_pixels[u].opacity);
2682 if ( image->colorspace == CMYKColorspace)
2683 Maximize(max.index, (double) k_indexes[u]);
2684 }
2685 }
2686 k_pixels += image->columns+kernel->width;
2687 k_indexes += image->columns+kernel->width;
2688 }
anthony4c827ef2010-06-05 23:56:10 +00002689 /* Pattern Match if difference is positive */
anthony5ef8e942010-05-11 06:51:12 +00002690 min.red -= max.red; Maximize( min.red, 0.0 );
2691 min.green -= max.green; Maximize( min.green, 0.0 );
2692 min.blue -= max.blue; Maximize( min.blue, 0.0 );
2693 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
2694 min.index -= max.index; Maximize( min.index, 0.0 );
2695 break;
2696
anthony4fd27e22010-02-07 08:17:18 +00002697 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002698 /* Select Pixel with Minimum Intensity within kernel neighbourhood
2699 **
2700 ** WARNING: the intensity test fails for CMYK and does not
2701 ** take into account the moderating effect of teh alpha channel
2702 ** on the intensity.
2703 **
2704 ** NOTE that the kernel is not reflected for this operation!
2705 */
anthony602ab9b2010-01-05 08:06:50 +00002706 k = kernel->values;
2707 k_pixels = p;
2708 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002709 for (v=0; v < (ssize_t) kernel->height; v++) {
2710 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00002711 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00002712 if ( result.red == 0.0 ||
2713 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
2714 /* copy the whole pixel - no channel selection */
2715 *q = k_pixels[u];
2716 if ( result.red > 0.0 ) changed++;
2717 result.red = 1.0;
2718 }
anthony602ab9b2010-01-05 08:06:50 +00002719 }
2720 k_pixels += image->columns+kernel->width;
2721 k_indexes += image->columns+kernel->width;
2722 }
anthony602ab9b2010-01-05 08:06:50 +00002723 break;
2724
anthony83ba99b2010-01-24 08:48:15 +00002725 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002726 /* Select Pixel with Maximum Intensity within kernel neighbourhood
2727 **
2728 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00002729 ** take into account the moderating effect of the alpha channel
2730 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00002731 **
2732 ** NOTE for correct working of this operation for asymetrical
2733 ** kernels, the kernel needs to be applied in its reflected form.
2734 ** That is its values needs to be reversed.
2735 */
anthony29188a82010-01-22 10:12:34 +00002736 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002737 k_pixels = p;
2738 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002739 for (v=0; v < (ssize_t) kernel->height; v++) {
2740 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00002741 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
2742 if ( result.red == 0.0 ||
2743 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
2744 /* copy the whole pixel - no channel selection */
2745 *q = k_pixels[u];
2746 if ( result.red > 0.0 ) changed++;
2747 result.red = 1.0;
2748 }
anthony602ab9b2010-01-05 08:06:50 +00002749 }
2750 k_pixels += image->columns+kernel->width;
2751 k_indexes += image->columns+kernel->width;
2752 }
anthony602ab9b2010-01-05 08:06:50 +00002753 break;
2754
anthony5ef8e942010-05-11 06:51:12 +00002755
anthony602ab9b2010-01-05 08:06:50 +00002756 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00002757 /* Add kernel Value and select the minimum value found.
2758 ** The result is a iterative distance from edge of image shape.
2759 **
2760 ** All Distance Kernels are symetrical, but that may not always
2761 ** be the case. For example how about a distance from left edges?
2762 ** To work correctly with asymetrical kernels the reflected kernel
2763 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00002764 **
2765 ** Actually this is really a GreyErode with a negative kernel!
2766 **
anthony930be612010-02-08 04:26:15 +00002767 */
anthony29188a82010-01-22 10:12:34 +00002768 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002769 k_pixels = p;
2770 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002771 for (v=0; v < (ssize_t) kernel->height; v++) {
2772 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002773 if ( IsNan(*k) ) continue;
2774 Minimize(result.red, (*k)+k_pixels[u].red);
2775 Minimize(result.green, (*k)+k_pixels[u].green);
2776 Minimize(result.blue, (*k)+k_pixels[u].blue);
2777 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
2778 if ( image->colorspace == CMYKColorspace)
2779 Minimize(result.index, (*k)+k_indexes[u]);
2780 }
2781 k_pixels += image->columns+kernel->width;
2782 k_indexes += image->columns+kernel->width;
2783 }
anthony602ab9b2010-01-05 08:06:50 +00002784 break;
2785
2786 case UndefinedMorphology:
2787 default:
2788 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00002789 }
anthony5ef8e942010-05-11 06:51:12 +00002790 /* Final mathematics of results (combine with original image?)
2791 **
2792 ** NOTE: Difference Morphology operators Edge* and *Hat could also
2793 ** be done here but works better with iteration as a image difference
2794 ** in the controling function (below). Thicken and Thinning however
2795 ** should be done here so thay can be iterated correctly.
2796 */
2797 switch ( method ) {
2798 case HitAndMissMorphology:
2799 case ErodeMorphology:
2800 result = min; /* minimum of neighbourhood */
2801 break;
2802 case DilateMorphology:
2803 result = max; /* maximum of neighbourhood */
2804 break;
2805 case ThinningMorphology:
2806 /* subtract pattern match from original */
2807 result.red -= min.red;
2808 result.green -= min.green;
2809 result.blue -= min.blue;
2810 result.opacity -= min.opacity;
2811 result.index -= min.index;
2812 break;
2813 case ThickenMorphology:
anthony4c827ef2010-06-05 23:56:10 +00002814 /* Add the pattern matchs to the original */
2815 result.red += min.red;
2816 result.green += min.green;
2817 result.blue += min.blue;
2818 result.opacity += min.opacity;
2819 result.index += min.index;
anthony5ef8e942010-05-11 06:51:12 +00002820 break;
2821 default:
2822 /* result directly calculated or assigned */
2823 break;
2824 }
2825 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00002826 switch ( method ) {
2827 case UndefinedMorphology:
2828 case DilateIntensityMorphology:
2829 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00002830 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00002831 default:
anthony83ba99b2010-01-24 08:48:15 +00002832 if ((channel & RedChannel) != 0)
2833 q->red = ClampToQuantum(result.red);
2834 if ((channel & GreenChannel) != 0)
2835 q->green = ClampToQuantum(result.green);
2836 if ((channel & BlueChannel) != 0)
2837 q->blue = ClampToQuantum(result.blue);
2838 if ((channel & OpacityChannel) != 0
2839 && image->matte == MagickTrue )
2840 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
2841 if ((channel & IndexChannel) != 0
2842 && image->colorspace == CMYKColorspace)
2843 q_indexes[x] = ClampToQuantum(result.index);
2844 break;
2845 }
anthony5ef8e942010-05-11 06:51:12 +00002846 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00002847 if ( ( p[r].red != q->red )
2848 || ( p[r].green != q->green )
2849 || ( p[r].blue != q->blue )
2850 || ( p[r].opacity != q->opacity )
2851 || ( image->colorspace == CMYKColorspace &&
2852 p_indexes[r] != q_indexes[x] ) )
2853 changed++; /* The pixel had some value changed! */
anthony602ab9b2010-01-05 08:06:50 +00002854 p++;
2855 q++;
anthony83ba99b2010-01-24 08:48:15 +00002856 } /* x */
anthony602ab9b2010-01-05 08:06:50 +00002857 sync=SyncCacheViewAuthenticPixels(q_view,exception);
2858 if (sync == MagickFalse)
2859 status=MagickFalse;
2860 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2861 {
2862 MagickBooleanType
2863 proceed;
2864
2865#if defined(MAGICKCORE_OPENMP_SUPPORT)
2866 #pragma omp critical (MagickCore_MorphologyImage)
2867#endif
2868 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2869 if (proceed == MagickFalse)
2870 status=MagickFalse;
2871 }
anthony83ba99b2010-01-24 08:48:15 +00002872 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00002873 result_image->type=image->type;
2874 q_view=DestroyCacheView(q_view);
2875 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00002876 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00002877}
2878
anthony4fd27e22010-02-07 08:17:18 +00002879
anthony9eb4f742010-05-18 02:45:54 +00002880MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00002881 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00002882 const KernelInfo *kernel, const CompositeOperator compose,
2883 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00002884{
2885 Image
anthony47f5d062010-05-23 07:47:50 +00002886 *curr_image, /* Image we are working with or iterating */
2887 *work_image, /* secondary image for primative iteration */
2888 *save_image, /* saved image - for 'edge' method only */
2889 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00002890
anthony4fd27e22010-02-07 08:17:18 +00002891 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00002892 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
2893 *norm_kernel, /* the current normal un-reflected kernel */
2894 *rflt_kernel, /* the current reflected kernel (if needed) */
2895 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00002896
2897 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00002898 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00002899
2900 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00002901 rslt_compose; /* multi-kernel compose method for results to use */
2902
2903 MagickBooleanType
2904 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00002905
cristybb503372010-05-27 20:51:26 +00002906 size_t
anthony47f5d062010-05-23 07:47:50 +00002907 method_loop, /* Loop 1: number of compound method iterations */
2908 method_limit, /* maximum number of compound method iterations */
2909 kernel_number, /* Loop 2: the kernel number being applied */
2910 stage_loop, /* Loop 3: primative loop for compound morphology */
2911 stage_limit, /* how many primatives in this compound */
2912 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
2913 kernel_limit, /* number of times to iterate kernel */
2914 count, /* total count of primative steps applied */
2915 changed, /* number pixels changed by last primative operation */
2916 kernel_changed, /* total count of changed using iterated kernel */
2917 method_changed; /* total count of changed over method iteration */
2918
2919 char
2920 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00002921
anthony602ab9b2010-01-05 08:06:50 +00002922 assert(image != (Image *) NULL);
2923 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00002924 assert(kernel != (KernelInfo *) NULL);
2925 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00002926 assert(exception != (ExceptionInfo *) NULL);
2927 assert(exception->signature == MagickSignature);
2928
anthonyc3e48252010-05-24 12:43:11 +00002929 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00002930 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00002931 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00002932
cristybb503372010-05-27 20:51:26 +00002933 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00002934 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
2935 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00002936
cristye96405a2010-05-19 02:24:31 +00002937 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
2938 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00002939
anthony9eb4f742010-05-18 02:45:54 +00002940 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00002941 curr_image = (Image *) image;
2942 work_image = save_image = rslt_image = (Image *) NULL;
2943 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00002944
anthony47f5d062010-05-23 07:47:50 +00002945 /* Initialize specific methods
2946 * + which loop should use the given iteratations
2947 * + how many primatives make up the compound morphology
2948 * + multi-kernel compose method to use (by default)
2949 */
2950 method_limit = 1; /* just do method once, unless otherwise set */
2951 stage_limit = 1; /* assume method is not a compount */
2952 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00002953 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00002954 case SmoothMorphology: /* 4 primative compound morphology */
2955 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00002956 break;
anthony47f5d062010-05-23 07:47:50 +00002957 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00002958 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002959 case TopHatMorphology:
2960 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002961 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00002962 case BottomHatMorphology:
2963 case EdgeMorphology:
2964 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00002965 break;
2966 case HitAndMissMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002967 kernel_limit = 1; /* no method or kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00002968 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony9eb4f742010-05-18 02:45:54 +00002969 break;
anthonyc3e48252010-05-24 12:43:11 +00002970 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002971 case ThickenMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002972 method_limit = kernel_limit; /* iterate method with each kernel */
2973 kernel_limit = 1; /* do not do kernel iteration */
anthonye4d89962010-05-29 10:53:11 +00002974 case DistanceMorphology:
anthonyc3e48252010-05-24 12:43:11 +00002975 rslt_compose = NoCompositeOp; /* Re-iterate with multiple kernels */
anthony47f5d062010-05-23 07:47:50 +00002976 break;
2977 default:
anthony930be612010-02-08 04:26:15 +00002978 break;
anthony602ab9b2010-01-05 08:06:50 +00002979 }
2980
anthonyc3e48252010-05-24 12:43:11 +00002981 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00002982 if ( compose != UndefinedCompositeOp )
2983 rslt_compose = compose; /* override default composition for method */
2984 if ( rslt_compose == UndefinedCompositeOp )
2985 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
2986
anthonyc3e48252010-05-24 12:43:11 +00002987 /* Some methods require a reflected kernel to use with primatives.
2988 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00002989 switch ( method ) {
2990 case CorrelateMorphology:
2991 case CloseMorphology:
2992 case CloseIntensityMorphology:
2993 case BottomHatMorphology:
2994 case SmoothMorphology:
2995 reflected_kernel = CloneKernelInfo(kernel);
2996 if (reflected_kernel == (KernelInfo *) NULL)
2997 goto error_cleanup;
2998 RotateKernelInfo(reflected_kernel,180);
2999 break;
3000 default:
3001 break;
anthony9eb4f742010-05-18 02:45:54 +00003002 }
anthony7a01dcf2010-05-11 12:25:52 +00003003
anthony47f5d062010-05-23 07:47:50 +00003004 /* Loop 1: iterate the compound method */
3005 method_loop = 0;
3006 method_changed = 1;
3007 while ( method_loop < method_limit && method_changed > 0 ) {
3008 method_loop++;
3009 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00003010
anthony47f5d062010-05-23 07:47:50 +00003011 /* Loop 2: iterate over each kernel in a multi-kernel list */
3012 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00003013 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00003014 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00003015
anthony47f5d062010-05-23 07:47:50 +00003016 kernel_number = 0;
3017 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003018
anthony47f5d062010-05-23 07:47:50 +00003019 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
3020 stage_loop = 0; /* the compound morphology stage number */
3021 while ( stage_loop < stage_limit ) {
3022 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00003023
anthony47f5d062010-05-23 07:47:50 +00003024 /* Select primative morphology for this stage of compound method */
3025 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00003026 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00003027 switch( method ) {
3028 case ErodeMorphology: /* just erode */
3029 case EdgeInMorphology: /* erode and image difference */
3030 primative = ErodeMorphology;
3031 break;
3032 case DilateMorphology: /* just dilate */
3033 case EdgeOutMorphology: /* dilate and image difference */
3034 primative = DilateMorphology;
3035 break;
3036 case OpenMorphology: /* erode then dialate */
3037 case TopHatMorphology: /* open and image difference */
3038 primative = ErodeMorphology;
3039 if ( stage_loop == 2 )
3040 primative = DilateMorphology;
3041 break;
3042 case OpenIntensityMorphology:
3043 primative = ErodeIntensityMorphology;
3044 if ( stage_loop == 2 )
3045 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00003046 break;
anthony47f5d062010-05-23 07:47:50 +00003047 case CloseMorphology: /* dilate, then erode */
3048 case BottomHatMorphology: /* close and image difference */
3049 this_kernel = rflt_kernel; /* use the reflected kernel */
3050 primative = DilateMorphology;
3051 if ( stage_loop == 2 )
3052 primative = ErodeMorphology;
3053 break;
3054 case CloseIntensityMorphology:
3055 this_kernel = rflt_kernel; /* use the reflected kernel */
3056 primative = DilateIntensityMorphology;
3057 if ( stage_loop == 2 )
3058 primative = ErodeIntensityMorphology;
3059 break;
3060 case SmoothMorphology: /* open, close */
3061 switch ( stage_loop ) {
3062 case 1: /* start an open method, which starts with Erode */
3063 primative = ErodeMorphology;
3064 break;
3065 case 2: /* now Dilate the Erode */
3066 primative = DilateMorphology;
3067 break;
3068 case 3: /* Reflect kernel a close */
3069 this_kernel = rflt_kernel; /* use the reflected kernel */
3070 primative = DilateMorphology;
3071 break;
3072 case 4: /* Finish the Close */
3073 this_kernel = rflt_kernel; /* use the reflected kernel */
3074 primative = ErodeMorphology;
3075 break;
3076 }
3077 break;
3078 case EdgeMorphology: /* dilate and erode difference */
3079 primative = DilateMorphology;
3080 if ( stage_loop == 2 ) {
3081 save_image = curr_image; /* save the image difference */
3082 curr_image = (Image *) image;
3083 primative = ErodeMorphology;
3084 }
3085 break;
3086 case CorrelateMorphology:
3087 /* A Correlation is a Convolution with a reflected kernel.
3088 ** However a Convolution is a weighted sum using a reflected
3089 ** kernel. It may seem stange to convert a Correlation into a
3090 ** Convolution as the Correlation is the simplier method, but
3091 ** Convolution is much more commonly used, and it makes sense to
3092 ** implement it directly so as to avoid the need to duplicate the
3093 ** kernel when it is not required (which is typically the
3094 ** default).
3095 */
3096 this_kernel = rflt_kernel; /* use the reflected kernel */
3097 primative = ConvolveMorphology;
3098 break;
3099 default:
anthony47f5d062010-05-23 07:47:50 +00003100 break;
3101 }
anthonye4d89962010-05-29 10:53:11 +00003102 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00003103
anthony47f5d062010-05-23 07:47:50 +00003104 /* Extra information for debugging compound operations */
3105 if ( verbose == MagickTrue ) {
3106 if ( stage_limit > 1 )
cristye8c25f92010-06-03 00:53:06 +00003107 (void) FormatMagickString(v_info,MaxTextExtent,"%s:%.20g.%.20g -> ",
3108 MagickOptionToMnemonic(MagickMorphologyOptions,method),(double)
3109 method_loop,(double) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00003110 else if ( primative != method )
cristye8c25f92010-06-03 00:53:06 +00003111 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%.20g -> ",
3112 MagickOptionToMnemonic(MagickMorphologyOptions, method),(double)
3113 method_loop);
anthony47f5d062010-05-23 07:47:50 +00003114 else
3115 v_info[0] = '\0';
3116 }
3117
3118 /* Loop 4: Iterate the kernel with primative */
3119 kernel_loop = 0;
3120 kernel_changed = 0;
3121 changed = 1;
3122 while ( kernel_loop < kernel_limit && changed > 0 ) {
3123 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00003124
3125 /* Create a destination image, if not yet defined */
3126 if ( work_image == (Image *) NULL )
3127 {
3128 work_image=CloneImage(image,0,0,MagickTrue,exception);
3129 if (work_image == (Image *) NULL)
3130 goto error_cleanup;
3131 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
3132 {
3133 InheritException(exception,&work_image->exception);
3134 goto error_cleanup;
3135 }
3136 }
3137
anthony501c2f92010-06-02 10:55:14 +00003138 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00003139 count++;
anthony47f5d062010-05-23 07:47:50 +00003140 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00003141 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00003142 kernel_changed += changed;
3143 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00003144
anthony47f5d062010-05-23 07:47:50 +00003145 if ( verbose == MagickTrue ) {
3146 if ( kernel_loop > 1 )
3147 fprintf(stderr, "\n"); /* add end-of-line from previous */
cristye8c25f92010-06-03 00:53:06 +00003148 (void) fprintf(stderr, "%s%s%s:%.20g.%.20g #%.20g => Changed %.20g",
3149 v_info,MagickOptionToMnemonic(MagickMorphologyOptions,
3150 primative),(this_kernel == rflt_kernel ) ? "*" : "",
3151 (double) (method_loop+kernel_loop-1),(double) kernel_number,
3152 (double) count,(double) changed);
anthony47f5d062010-05-23 07:47:50 +00003153 }
anthony9eb4f742010-05-18 02:45:54 +00003154 /* prepare next loop */
3155 { Image *tmp = work_image; /* swap images for iteration */
3156 work_image = curr_image;
3157 curr_image = tmp;
3158 }
3159 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00003160 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00003161
anthony47f5d062010-05-23 07:47:50 +00003162 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00003163
anthony47f5d062010-05-23 07:47:50 +00003164 if ( verbose == MagickTrue && kernel_changed != changed )
cristye8c25f92010-06-03 00:53:06 +00003165 fprintf(stderr, " Total %.20g",(double) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00003166 if ( verbose == MagickTrue && stage_loop < stage_limit )
3167 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00003168
3169#if 0
anthonye4d89962010-05-29 10:53:11 +00003170 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
3171 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
3172 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
3173 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
3174 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003175#endif
3176
anthony47f5d062010-05-23 07:47:50 +00003177 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00003178
anthony47f5d062010-05-23 07:47:50 +00003179 /* Final Post-processing for some Compound Methods
3180 **
3181 ** The removal of any 'Sync' channel flag in the Image Compositon
3182 ** below ensures the methematical compose method is applied in a
3183 ** purely mathematical way, and only to the selected channels.
3184 ** Turn off SVG composition 'alpha blending'.
3185 */
3186 switch( method ) {
3187 case EdgeOutMorphology:
3188 case EdgeInMorphology:
3189 case TopHatMorphology:
3190 case BottomHatMorphology:
3191 if ( verbose == MagickTrue )
3192 fprintf(stderr, "\n%s: Difference with original image",
3193 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3194 (void) CompositeImageChannel(curr_image,
3195 (ChannelType) (channel & ~SyncChannels),
3196 DifferenceCompositeOp, image, 0, 0);
3197 break;
3198 case EdgeMorphology:
3199 if ( verbose == MagickTrue )
3200 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3201 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3202 (void) CompositeImageChannel(curr_image,
3203 (ChannelType) (channel & ~SyncChannels),
3204 DifferenceCompositeOp, save_image, 0, 0);
3205 save_image = DestroyImage(save_image); /* finished with save image */
3206 break;
3207 default:
3208 break;
3209 }
3210
3211 /* multi-kernel handling: re-iterate, or compose results */
3212 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003213 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003214 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003215 { if ( verbose == MagickTrue ) {
3216 if ( this_kernel->next != (KernelInfo *) NULL )
3217 fprintf(stderr, " (re-iterate)");
3218 else
3219 fprintf(stderr, " (done)");
3220 }
3221 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003222 }
anthony47f5d062010-05-23 07:47:50 +00003223 else if ( rslt_image == (Image *) NULL)
3224 { if ( verbose == MagickTrue )
3225 fprintf(stderr, " (save for compose)");
3226 rslt_image = curr_image;
3227 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003228 }
anthony47f5d062010-05-23 07:47:50 +00003229 else
3230 { /* add the new 'current' result to the composition
3231 **
3232 ** The removal of any 'Sync' channel flag in the Image Compositon
3233 ** below ensures the methematical compose method is applied in a
3234 ** purely mathematical way, and only to the selected channels.
3235 ** Turn off SVG composition 'alpha blending'.
3236 */
3237 if ( verbose == MagickTrue )
3238 fprintf(stderr, " (compose \"%s\")",
3239 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
3240 (void) CompositeImageChannel(rslt_image,
3241 (ChannelType) (channel & ~SyncChannels), rslt_compose,
3242 curr_image, 0, 0);
3243 curr_image = (Image *) image; /* continue with original image */
3244 }
3245 if ( verbose == MagickTrue )
3246 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003247
anthony47f5d062010-05-23 07:47:50 +00003248 /* loop to the next kernel in a multi-kernel list */
3249 norm_kernel = norm_kernel->next;
3250 if ( rflt_kernel != (KernelInfo *) NULL )
3251 rflt_kernel = rflt_kernel->next;
3252 kernel_number++;
3253 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003254
anthony47f5d062010-05-23 07:47:50 +00003255 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003256
anthony9eb4f742010-05-18 02:45:54 +00003257 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003258
anthony47f5d062010-05-23 07:47:50 +00003259 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003260error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003261 if ( curr_image != (Image *) NULL &&
3262 curr_image != rslt_image &&
3263 curr_image != image )
3264 curr_image = DestroyImage(curr_image);
3265 if ( rslt_image != (Image *) NULL )
3266 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003267exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003268 if ( curr_image != (Image *) NULL &&
3269 curr_image != rslt_image &&
3270 curr_image != image )
3271 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003272 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003273 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003274 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003275 save_image = DestroyImage(save_image);
3276 if ( reflected_kernel != (KernelInfo *) NULL )
3277 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3278 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003279}
3280
3281/*
3282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3283% %
3284% %
3285% %
3286% M o r p h o l o g y I m a g e C h a n n e l %
3287% %
3288% %
3289% %
3290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3291%
3292% MorphologyImageChannel() applies a user supplied kernel to the image
3293% according to the given mophology method.
3294%
3295% This function applies any and all user defined settings before calling
3296% the above internal function MorphologyApply().
3297%
3298% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003299% * Output Bias for Convolution and correlation ("-bias")
3300% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3301% This can also includes the addition of a scaled unity kernel.
3302% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003303%
3304% The format of the MorphologyImage method is:
3305%
3306% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003307% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003308%
3309% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003310% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003311% KernelInfo *kernel,ExceptionInfo *exception)
3312%
3313% A description of each parameter follows:
3314%
3315% o image: the image.
3316%
3317% o method: the morphology method to be applied.
3318%
3319% o iterations: apply the operation this many times (or no change).
3320% A value of -1 means loop until no change found.
3321% How this is applied may depend on the morphology method.
3322% Typically this is a value of 1.
3323%
3324% o channel: the channel type.
3325%
3326% o kernel: An array of double representing the morphology kernel.
3327% Warning: kernel may be normalized for the Convolve method.
3328%
3329% o exception: return any errors or warnings in this structure.
3330%
3331*/
3332
3333MagickExport Image *MorphologyImageChannel(const Image *image,
3334 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003335 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003336{
3337 const char
3338 *artifact;
3339
3340 KernelInfo
3341 *curr_kernel;
3342
anthony47f5d062010-05-23 07:47:50 +00003343 CompositeOperator
3344 compose;
3345
anthony9eb4f742010-05-18 02:45:54 +00003346 Image
3347 *morphology_image;
3348
3349
anthony46a369d2010-05-19 02:41:48 +00003350 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3351 * This is done BEFORE the ShowKernelInfo() function is called so that
3352 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003353 */
3354 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003355 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003356 {
3357 artifact = GetImageArtifact(image,"convolve:scale");
anthonye8d2f552010-06-05 10:43:25 +00003358 if ( artifact != (const char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003359 if ( curr_kernel == kernel )
3360 curr_kernel = CloneKernelInfo(kernel);
3361 if (curr_kernel == (KernelInfo *) NULL) {
3362 curr_kernel=DestroyKernelInfo(curr_kernel);
3363 return((Image *) NULL);
3364 }
anthony46a369d2010-05-19 02:41:48 +00003365 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003366 }
3367 }
3368
3369 /* display the (normalized) kernel via stderr */
3370 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003371 if ( artifact == (const char *) NULL)
3372 artifact = GetImageArtifact(image,"convolve:showkernel");
3373 if ( artifact == (const char *) NULL)
3374 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003375 if ( artifact != (const char *) NULL)
3376 ShowKernelInfo(curr_kernel);
3377
anthony47f5d062010-05-23 07:47:50 +00003378 /* override the default handling of multi-kernel morphology results
3379 * if 'Undefined' use the default method
3380 * if 'None' (default for 'Convolve') re-iterate previous result
3381 * otherwise merge resulting images using compose method given
3382 */
3383 compose = UndefinedCompositeOp; /* use default for method */
3384 artifact = GetImageArtifact(image,"morphology:compose");
3385 if ( artifact != (const char *) NULL)
3386 compose = (CompositeOperator) ParseMagickOption(
3387 MagickComposeOptions,MagickFalse,artifact);
3388
anthony9eb4f742010-05-18 02:45:54 +00003389 /* Apply the Morphology */
3390 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003391 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003392
3393 /* Cleanup and Exit */
3394 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003395 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003396 return(morphology_image);
3397}
3398
3399MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003400 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003401 *exception)
3402{
3403 Image
3404 *morphology_image;
3405
3406 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3407 iterations,kernel,exception);
3408 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003409}
anthony83ba99b2010-01-24 08:48:15 +00003410
3411/*
3412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3413% %
3414% %
3415% %
anthony4fd27e22010-02-07 08:17:18 +00003416+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003417% %
3418% %
3419% %
3420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3421%
anthony46a369d2010-05-19 02:41:48 +00003422% RotateKernelInfo() rotates the kernel by the angle given.
3423%
3424% Currently it is restricted to 90 degree angles, of either 1D kernels
3425% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3426% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003427%
anthony4fd27e22010-02-07 08:17:18 +00003428% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003429%
anthony4fd27e22010-02-07 08:17:18 +00003430% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003431%
3432% A description of each parameter follows:
3433%
3434% o kernel: the Morphology/Convolution kernel
3435%
3436% o angle: angle to rotate in degrees
3437%
anthony46a369d2010-05-19 02:41:48 +00003438% This function is currently internal to this module only, but can be exported
3439% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003440*/
anthony4fd27e22010-02-07 08:17:18 +00003441static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003442{
anthony1b2bc0a2010-05-12 05:25:22 +00003443 /* angle the lower kernels first */
3444 if ( kernel->next != (KernelInfo *) NULL)
3445 RotateKernelInfo(kernel->next, angle);
3446
anthony83ba99b2010-01-24 08:48:15 +00003447 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3448 **
3449 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3450 */
3451
3452 /* Modulus the angle */
3453 angle = fmod(angle, 360.0);
3454 if ( angle < 0 )
3455 angle += 360.0;
3456
anthony3c10fc82010-05-13 02:40:51 +00003457 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003458 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003459
anthony3dd0f622010-05-13 12:57:32 +00003460 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003461 switch (kernel->type) {
3462 /* These built-in kernels are cylindrical kernels, rotating is useless */
3463 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +00003464 case DoGKernel:
3465 case LoGKernel:
anthony83ba99b2010-01-24 08:48:15 +00003466 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003467 case PeaksKernel:
3468 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003469 case ChebyshevKernel:
anthonybee715c2010-06-04 01:25:57 +00003470 case ManhattanKernel:
anthony83ba99b2010-01-24 08:48:15 +00003471 case EuclideanKernel:
3472 return;
3473
3474 /* These may be rotatable at non-90 angles in the future */
3475 /* but simply rotating them in multiples of 90 degrees is useless */
3476 case SquareKernel:
3477 case DiamondKernel:
3478 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003479 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003480 return;
3481
3482 /* These only allows a +/-90 degree rotation (by transpose) */
3483 /* A 180 degree rotation is useless */
3484 case BlurKernel:
3485 case RectangleKernel:
3486 if ( 135.0 < angle && angle <= 225.0 )
3487 return;
3488 if ( 225.0 < angle && angle <= 315.0 )
3489 angle -= 180;
3490 break;
3491
anthony3dd0f622010-05-13 12:57:32 +00003492 default:
anthony83ba99b2010-01-24 08:48:15 +00003493 break;
3494 }
anthony3c10fc82010-05-13 02:40:51 +00003495 /* Attempt rotations by 45 degrees */
3496 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3497 {
3498 if ( kernel->width == 3 && kernel->height == 3 )
3499 { /* Rotate a 3x3 square by 45 degree angle */
3500 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003501 kernel->values[0] = kernel->values[3];
3502 kernel->values[3] = kernel->values[6];
3503 kernel->values[6] = kernel->values[7];
3504 kernel->values[7] = kernel->values[8];
3505 kernel->values[8] = kernel->values[5];
3506 kernel->values[5] = kernel->values[2];
3507 kernel->values[2] = kernel->values[1];
3508 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003509 /* rotate non-centered origin */
3510 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003511 ssize_t x,y;
3512 x = (ssize_t) kernel->x-1;
3513 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003514 if ( x == y ) x = 0;
3515 else if ( x == 0 ) x = -y;
3516 else if ( x == -y ) y = 0;
3517 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003518 kernel->x = (ssize_t) x+1;
3519 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003520 }
anthony43c49252010-05-18 10:59:50 +00003521 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3522 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003523 }
3524 else
3525 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3526 }
3527 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3528 {
3529 if ( kernel->width == 1 || kernel->height == 1 )
anthonybfb635a2010-06-04 00:18:04 +00003530 { /* Do a transpose of a 1 dimentional kernel,
3531 ** which results in a fast 90 degree rotation of some type.
anthony3c10fc82010-05-13 02:40:51 +00003532 */
cristybb503372010-05-27 20:51:26 +00003533 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003534 t;
cristybb503372010-05-27 20:51:26 +00003535 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003536 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003537 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003538 t = kernel->x;
3539 kernel->x = kernel->y;
3540 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003541 if ( kernel->width == 1 ) {
3542 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3543 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3544 } else {
3545 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3546 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3547 }
anthony3c10fc82010-05-13 02:40:51 +00003548 }
3549 else if ( kernel->width == kernel->height )
3550 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003551 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003552 i,j,x,y;
3553 register MagickRealType
3554 *k,t;
3555 k=kernel->values;
3556 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3557 for( j=0, y=kernel->height-1; j<y; j++, y--)
3558 { t = k[i+j*kernel->width];
3559 k[i+j*kernel->width] = k[j+x*kernel->width];
3560 k[j+x*kernel->width] = k[x+y*kernel->width];
3561 k[x+y*kernel->width] = k[y+i*kernel->width];
3562 k[y+i*kernel->width] = t;
3563 }
3564 }
3565 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003566 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003567 x = (ssize_t) (kernel->x*2-kernel->width+1);
3568 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003569 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3570 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003571 }
anthony43c49252010-05-18 10:59:50 +00003572 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3573 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003574 }
3575 else
3576 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3577 }
anthony83ba99b2010-01-24 08:48:15 +00003578 if ( 135.0 < angle && angle <= 225.0 )
3579 {
anthony43c49252010-05-18 10:59:50 +00003580 /* For a 180 degree rotation - also know as a reflection
3581 * This is actually a very very common operation!
3582 * Basically all that is needed is a reversal of the kernel data!
3583 * And a reflection of the origon
3584 */
cristybb503372010-05-27 20:51:26 +00003585 size_t
anthony83ba99b2010-01-24 08:48:15 +00003586 i,j;
3587 register double
3588 *k,t;
3589
3590 k=kernel->values;
3591 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3592 t=k[i], k[i]=k[j], k[j]=t;
3593
cristybb503372010-05-27 20:51:26 +00003594 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3595 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003596 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3597 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003598 }
anthony3c10fc82010-05-13 02:40:51 +00003599 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003600 * In the future some form of non-orthogonal angled rotates could be
3601 * performed here, posibily with a linear kernel restriction.
3602 */
3603
anthony83ba99b2010-01-24 08:48:15 +00003604 return;
3605}
3606
3607/*
3608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3609% %
3610% %
3611% %
anthony46a369d2010-05-19 02:41:48 +00003612% S c a l e G e o m e t r y K e r n e l I n f o %
3613% %
3614% %
3615% %
3616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3617%
3618% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3619% provided as a "-set option:convolve:scale {geometry}" user setting,
3620% and modifies the kernel according to the parsed arguments of that setting.
3621%
3622% The first argument (and any normalization flags) are passed to
3623% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3624% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3625% into the scaled/normalized kernel.
3626%
3627% The format of the ScaleKernelInfo method is:
3628%
3629% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3630% const MagickStatusType normalize_flags )
3631%
3632% A description of each parameter follows:
3633%
3634% o kernel: the Morphology/Convolution kernel to modify
3635%
3636% o geometry:
3637% The geometry string to parse, typically from the user provided
3638% "-set option:convolve:scale {geometry}" setting.
3639%
3640*/
3641MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3642 const char *geometry)
3643{
3644 GeometryFlags
3645 flags;
3646 GeometryInfo
3647 args;
3648
3649 SetGeometryInfo(&args);
3650 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3651
3652#if 0
3653 /* For Debugging Geometry Input */
3654 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3655 flags, args.rho, args.sigma, args.xi, args.psi );
3656#endif
3657
3658 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3659 args.rho *= 0.01, args.sigma *= 0.01;
3660
3661 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3662 args.rho = 1.0;
3663 if ( (flags & SigmaValue) == 0 )
3664 args.sigma = 0.0;
3665
3666 /* Scale/Normalize the input kernel */
3667 ScaleKernelInfo(kernel, args.rho, flags);
3668
3669 /* Add Unity Kernel, for blending with original */
3670 if ( (flags & SigmaValue) != 0 )
3671 UnityAddKernelInfo(kernel, args.sigma);
3672
3673 return;
3674}
3675/*
3676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3677% %
3678% %
3679% %
cristy6771f1e2010-03-05 19:43:39 +00003680% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00003681% %
3682% %
3683% %
3684%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3685%
anthony1b2bc0a2010-05-12 05:25:22 +00003686% ScaleKernelInfo() scales the given kernel list by the given amount, with or
3687% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00003688%
anthony999bb2c2010-02-18 12:38:01 +00003689% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00003690% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00003691%
anthony46a369d2010-05-19 02:41:48 +00003692% If either of the two 'normalize_flags' are given the kernel will first be
3693% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00003694%
3695% Kernel normalization ('normalize_flags' given) is designed to ensure that
3696% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00003697% morphology methods will fall into -1.0 to +1.0 range. Note that for
3698% non-HDRI versions of IM this may cause images to have any negative results
3699% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00003700%
3701% More specifically. Kernels which only contain positive values (such as a
3702% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00003703% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00003704%
3705% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
3706% the kernel will be scaled by the absolute of the sum of kernel values, so
3707% that it will generally fall within the +/- 1.0 range.
3708%
3709% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
3710% will be scaled by just the sum of the postive values, so that its output
3711% range will again fall into the +/- 1.0 range.
3712%
3713% For special kernels designed for locating shapes using 'Correlate', (often
3714% only containing +1 and -1 values, representing foreground/brackground
3715% matching) a special normalization method is provided to scale the positive
3716% values seperatally to those of the negative values, so the kernel will be
3717% forced to become a zero-sum kernel better suited to such searches.
3718%
anthony1b2bc0a2010-05-12 05:25:22 +00003719% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00003720% attributes within the kernel structure have been correctly set during the
3721% kernels creation.
3722%
3723% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00003724% to match the use of geometry options, so that '!' means NormalizeValue, '^'
3725% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00003726%
anthony4fd27e22010-02-07 08:17:18 +00003727% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00003728%
anthony999bb2c2010-02-18 12:38:01 +00003729% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3730% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00003731%
3732% A description of each parameter follows:
3733%
3734% o kernel: the Morphology/Convolution kernel
3735%
anthony999bb2c2010-02-18 12:38:01 +00003736% o scaling_factor:
3737% multiply all values (after normalization) by this factor if not
3738% zero. If the kernel is normalized regardless of any flags.
3739%
3740% o normalize_flags:
3741% GeometryFlags defining normalization method to use.
3742% specifically: NormalizeValue, CorrelateNormalizeValue,
3743% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00003744%
3745*/
cristy6771f1e2010-03-05 19:43:39 +00003746MagickExport void ScaleKernelInfo(KernelInfo *kernel,
3747 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00003748{
cristybb503372010-05-27 20:51:26 +00003749 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00003750 i;
3751
anthony999bb2c2010-02-18 12:38:01 +00003752 register double
3753 pos_scale,
3754 neg_scale;
3755
anthony46a369d2010-05-19 02:41:48 +00003756 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003757 if ( kernel->next != (KernelInfo *) NULL)
3758 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
3759
anthony46a369d2010-05-19 02:41:48 +00003760 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00003761 pos_scale = 1.0;
3762 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00003763 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00003764 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00003765 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00003766 else
anthonyf4e00312010-05-20 12:06:35 +00003767 /* zero-summing kernel */
3768 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00003769 }
anthony46a369d2010-05-19 02:41:48 +00003770 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00003771 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
3772 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
3773 ? kernel->positive_range : 1.0;
3774 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
3775 ? -kernel->negative_range : 1.0;
3776 }
3777 else
3778 neg_scale = pos_scale;
3779
3780 /* finialize scaling_factor for positive and negative components */
3781 pos_scale = scaling_factor/pos_scale;
3782 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003783
cristybb503372010-05-27 20:51:26 +00003784 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003785 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00003786 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00003787
anthony999bb2c2010-02-18 12:38:01 +00003788 /* convolution output range */
3789 kernel->positive_range *= pos_scale;
3790 kernel->negative_range *= neg_scale;
3791 /* maximum and minimum values in kernel */
3792 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
3793 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
3794
anthony46a369d2010-05-19 02:41:48 +00003795 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00003796 if ( scaling_factor < MagickEpsilon ) {
3797 double t;
3798 t = kernel->positive_range;
3799 kernel->positive_range = kernel->negative_range;
3800 kernel->negative_range = t;
3801 t = kernel->maximum;
3802 kernel->maximum = kernel->minimum;
3803 kernel->minimum = 1;
3804 }
anthonycc6c8362010-01-25 04:14:01 +00003805
3806 return;
3807}
3808
3809/*
3810%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3811% %
3812% %
3813% %
anthony46a369d2010-05-19 02:41:48 +00003814% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003815% %
3816% %
3817% %
3818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3819%
anthony4fd27e22010-02-07 08:17:18 +00003820% ShowKernelInfo() outputs the details of the given kernel defination to
3821% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00003822%
3823% The format of the ShowKernel method is:
3824%
anthony4fd27e22010-02-07 08:17:18 +00003825% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003826%
3827% A description of each parameter follows:
3828%
3829% o kernel: the Morphology/Convolution kernel
3830%
anthony83ba99b2010-01-24 08:48:15 +00003831*/
anthony4fd27e22010-02-07 08:17:18 +00003832MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00003833{
anthony7a01dcf2010-05-11 12:25:52 +00003834 KernelInfo
3835 *k;
anthony83ba99b2010-01-24 08:48:15 +00003836
cristybb503372010-05-27 20:51:26 +00003837 size_t
anthony7a01dcf2010-05-11 12:25:52 +00003838 c, i, u, v;
3839
3840 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
3841
anthony46a369d2010-05-19 02:41:48 +00003842 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00003843 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00003844 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00003845 fprintf(stderr, " \"%s",
3846 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
3847 if ( fabs(k->angle) > MagickEpsilon )
3848 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00003849 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
3850 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00003851 fprintf(stderr,
3852 " with values from %.*lg to %.*lg\n",
3853 GetMagickPrecision(), k->minimum,
3854 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00003855 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00003856 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00003857 GetMagickPrecision(), k->positive_range);
3858 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
3859 fprintf(stderr, " (Zero-Summing)\n");
3860 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
3861 fprintf(stderr, " (Normalized)\n");
3862 else
3863 fprintf(stderr, " (Sum %.*lg)\n",
3864 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00003865 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00003866 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00003867 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00003868 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00003869 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00003870 else
anthonyf4e00312010-05-20 12:06:35 +00003871 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00003872 GetMagickPrecision(), k->values[i]);
3873 fprintf(stderr,"\n");
3874 }
anthony83ba99b2010-01-24 08:48:15 +00003875 }
3876}
anthonycc6c8362010-01-25 04:14:01 +00003877
3878/*
3879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3880% %
3881% %
3882% %
anthony43c49252010-05-18 10:59:50 +00003883% U n i t y A d d K e r n a l I n f o %
3884% %
3885% %
3886% %
3887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3888%
3889% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
3890% to the given pre-scaled and normalized Kernel. This in effect adds that
3891% amount of the original image into the resulting convolution kernel. This
3892% value is usually provided by the user as a percentage value in the
3893% 'convolve:scale' setting.
3894%
anthony501c2f92010-06-02 10:55:14 +00003895% The resulting effect is to convert the defined kernels into blended
3896% soft-blurs, unsharp kernels or into sharpening kernels.
anthony43c49252010-05-18 10:59:50 +00003897%
anthony46a369d2010-05-19 02:41:48 +00003898% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00003899%
3900% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
3901%
3902% A description of each parameter follows:
3903%
3904% o kernel: the Morphology/Convolution kernel
3905%
3906% o scale:
3907% scaling factor for the unity kernel to be added to
3908% the given kernel.
3909%
anthony43c49252010-05-18 10:59:50 +00003910*/
3911MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
3912 const double scale)
3913{
anthony46a369d2010-05-19 02:41:48 +00003914 /* do the other kernels in a multi-kernel list first */
3915 if ( kernel->next != (KernelInfo *) NULL)
3916 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00003917
anthony46a369d2010-05-19 02:41:48 +00003918 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00003919 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00003920 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00003921
3922 return;
3923}
3924
3925/*
3926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3927% %
3928% %
3929% %
3930% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00003931% %
3932% %
3933% %
3934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3935%
3936% ZeroKernelNans() replaces any special 'nan' value that may be present in
3937% the kernel with a zero value. This is typically done when the kernel will
3938% be used in special hardware (GPU) convolution processors, to simply
3939% matters.
3940%
3941% The format of the ZeroKernelNans method is:
3942%
anthony46a369d2010-05-19 02:41:48 +00003943% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003944%
3945% A description of each parameter follows:
3946%
3947% o kernel: the Morphology/Convolution kernel
3948%
anthonycc6c8362010-01-25 04:14:01 +00003949*/
anthonyc4c86e02010-01-27 09:30:32 +00003950MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00003951{
cristybb503372010-05-27 20:51:26 +00003952 register size_t
anthonycc6c8362010-01-25 04:14:01 +00003953 i;
3954
anthony46a369d2010-05-19 02:41:48 +00003955 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00003956 if ( kernel->next != (KernelInfo *) NULL)
3957 ZeroKernelNans(kernel->next);
3958
anthony43c49252010-05-18 10:59:50 +00003959 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00003960 if ( IsNan(kernel->values[i]) )
3961 kernel->values[i] = 0.0;
3962
3963 return;
3964}