blob: cdaf5242548cc78a35b00d3b56a7ca6c888b09d7 [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
anthony19910ef2010-06-25 01:15:40 +0000151% center as origin, this is no longer 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
anthony694934f2010-06-07 10:30:40 +0000853% Find flat orthogonal edges of a binary shape
anthony3dd0f622010-05-13 12:57:32 +0000854% Corners
anthony694934f2010-06-07 10:30:40 +0000855% Find 90 degree corners of a binary shape
856% LineEnds:type
anthony3dd0f622010-05-13 12:57:32 +0000857% Find end points of lines (for pruning a skeletion)
anthony694934f2010-06-07 10:30:40 +0000858% Two types of lines ends (default to both) can be searched for
859% Type 0: All line ends
860% Type 1: single kernel for 4-conneected line ends
861% Type 2: single kernel for simple line ends
anthony3dd0f622010-05-13 12:57:32 +0000862% LineJunctions
anthony43c49252010-05-18 10:59:50 +0000863% Find three line junctions (within a skeletion)
anthony694934f2010-06-07 10:30:40 +0000864% Type 0: all line junctions
865% Type 1: Y Junction kernel
866% Type 2: Diagonal T Junction kernel
867% Type 3: Orthogonal T Junction kernel
868% Type 4: Diagonal X Junction kernel
869% Type 5: Orthogonal + Junction kernel
870% Ridges:type
871% Find single pixel ridges or thin lines
872% Type 1: Fine single pixel thick lines and ridges
873% Type 2: Find two pixel thick lines and ridges
anthony3dd0f622010-05-13 12:57:32 +0000874% ConvexHull
875% Octagonal thicken kernel, to generate convex hulls of 45 degrees
anthonyc40ac1e2010-06-06 11:49:31 +0000876% Skeleton:type
877% Traditional skeleton generating kernels.
anthony694934f2010-06-07 10:30:40 +0000878% Type 1: Tradional Skeleton kernel (4 connected skeleton)
879% Type 2: HIPR2 Skeleton kernel (8 connected skeleton)
880% Type 3: Experimental Variation to try to present left-right symmetry
881% Type 4: Experimental Variation to preserve left-right symmetry
anthony602ab9b2010-01-05 08:06:50 +0000882%
883% Distance Measuring Kernels
884%
anthonyc1061722010-05-14 06:23:49 +0000885% Different types of distance measuring methods, which are used with the
886% a 'Distance' morphology method for generating a gradient based on
887% distance from an edge of a binary shape, though there is a technique
888% for handling a anti-aliased shape.
889%
890% See the 'Distance' Morphological Method, for information of how it is
891% applied.
892%
anthony3dd0f622010-05-13 12:57:32 +0000893% Chebyshev:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000894% Chebyshev Distance (also known as Tchebychev Distance) is a value of
895% one to any neighbour, orthogonal or diagonal. One why of thinking of
896% it is the number of squares a 'King' or 'Queen' in chess needs to
897% traverse reach any other position on a chess board. It results in a
898% 'square' like distance function, but one where diagonals are closer
899% than expected.
anthony602ab9b2010-01-05 08:06:50 +0000900%
anthonybee715c2010-06-04 01:25:57 +0000901% Manhattan:[{radius}][x{scale}[%!]]
902% Manhattan Distance (also known as Rectilinear Distance, or the Taxi
anthonyc94cdb02010-01-06 08:15:29 +0000903% Cab metric), is the distance needed when you can only travel in
904% orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
905% in chess would travel. It results in a diamond like distances, where
906% diagonals are further than expected.
anthony602ab9b2010-01-05 08:06:50 +0000907%
anthonyc1061722010-05-14 06:23:49 +0000908% Euclidean:[{radius}][x{scale}[%!]]
anthonyc94cdb02010-01-06 08:15:29 +0000909% Euclidean Distance is the 'direct' or 'as the crow flys distance.
910% However by default the kernel size only has a radius of 1, which
911% limits the distance to 'Knight' like moves, with only orthogonal and
912% diagonal measurements being correct. As such for the default kernel
913% you will get octagonal like distance function, which is reasonally
914% accurate.
915%
916% However if you use a larger radius such as "Euclidean:4" you will
917% get a much smoother distance gradient from the edge of the shape.
918% Of course a larger kernel is slower to use, and generally not needed.
919%
920% To allow the use of fractional distances that you get with diagonals
921% the actual distance is scaled by a fixed value which the user can
922% provide. This is not actually nessary for either ""Chebyshev" or
anthonybee715c2010-06-04 01:25:57 +0000923% "Manhattan" distance kernels, but is done for all three distance
anthonyc94cdb02010-01-06 08:15:29 +0000924% kernels. If no scale is provided it is set to a value of 100,
925% allowing for a maximum distance measurement of 655 pixels using a Q16
926% version of IM, from any edge. However for small images this can
927% result in quite a dark gradient.
928%
anthony602ab9b2010-01-05 08:06:50 +0000929*/
930
cristy2be15382010-01-21 02:38:03 +0000931MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
anthony602ab9b2010-01-05 08:06:50 +0000932 const GeometryInfo *args)
933{
cristy2be15382010-01-21 02:38:03 +0000934 KernelInfo
anthony602ab9b2010-01-05 08:06:50 +0000935 *kernel;
936
cristybb503372010-05-27 20:51:26 +0000937 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000938 i;
939
cristybb503372010-05-27 20:51:26 +0000940 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +0000941 u,
942 v;
943
944 double
945 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
946
anthonyc1061722010-05-14 06:23:49 +0000947 /* Generate a new empty kernel if needed */
cristye96405a2010-05-19 02:24:31 +0000948 kernel=(KernelInfo *) NULL;
anthonyc1061722010-05-14 06:23:49 +0000949 switch(type) {
anthony1dd091a2010-05-27 06:31:15 +0000950 case UndefinedKernel: /* These should not call this function */
anthony9eb4f742010-05-18 02:45:54 +0000951 case UserDefinedKernel:
952 break;
anthony1dd091a2010-05-27 06:31:15 +0000953 case UnityKernel: /* Named Descrete Convolution Kernels */
954 case LaplacianKernel:
anthony9eb4f742010-05-18 02:45:54 +0000955 case SobelKernel:
956 case RobertsKernel:
957 case PrewittKernel:
958 case CompassKernel:
959 case KirschKernel:
anthony1dd091a2010-05-27 06:31:15 +0000960 case FreiChenKernel:
anthony694934f2010-06-07 10:30:40 +0000961 case EdgesKernel: /* Hit and Miss kernels */
962 case CornersKernel:
anthony68cf70d2010-06-13 12:51:53 +0000963 case ThinDiagonalsKernel:
anthony9eb4f742010-05-18 02:45:54 +0000964 case LineEndsKernel:
965 case LineJunctionsKernel:
anthony1dd091a2010-05-27 06:31:15 +0000966 case RidgesKernel:
anthony9eb4f742010-05-18 02:45:54 +0000967 case ConvexHullKernel:
968 case SkeletonKernel:
anthonyc40ac1e2010-06-06 11:49:31 +0000969 break; /* A pre-generated kernel is not needed */
970#if 0
971 /* set to 1 to do a compile-time check that we haven't missed anything */
anthonyc1061722010-05-14 06:23:49 +0000972 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +0000973 case DoGKernel:
974 case LoGKernel:
anthonyc1061722010-05-14 06:23:49 +0000975 case BlurKernel:
anthonyc1061722010-05-14 06:23:49 +0000976 case CometKernel:
977 case DiamondKernel:
978 case SquareKernel:
979 case RectangleKernel:
980 case DiskKernel:
981 case PlusKernel:
982 case CrossKernel:
983 case RingKernel:
984 case PeaksKernel:
985 case ChebyshevKernel:
anthonybee715c2010-06-04 01:25:57 +0000986 case ManhattanKernel:
anthonyc1061722010-05-14 06:23:49 +0000987 case EuclideanKernel:
anthony1dd091a2010-05-27 06:31:15 +0000988#else
anthony9eb4f742010-05-18 02:45:54 +0000989 default:
anthony1dd091a2010-05-27 06:31:15 +0000990#endif
anthony9eb4f742010-05-18 02:45:54 +0000991 /* Generate the base Kernel Structure */
anthonyc1061722010-05-14 06:23:49 +0000992 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
993 if (kernel == (KernelInfo *) NULL)
994 return(kernel);
995 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
anthony43c49252010-05-18 10:59:50 +0000996 kernel->minimum = kernel->maximum = kernel->angle = 0.0;
anthonyc1061722010-05-14 06:23:49 +0000997 kernel->negative_range = kernel->positive_range = 0.0;
998 kernel->type = type;
999 kernel->next = (KernelInfo *) NULL;
1000 kernel->signature = MagickSignature;
anthonyc1061722010-05-14 06:23:49 +00001001 break;
1002 }
anthony602ab9b2010-01-05 08:06:50 +00001003
1004 switch(type) {
1005 /* Convolution Kernels */
1006 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +00001007 case DoGKernel:
1008 case LoGKernel:
anthony602ab9b2010-01-05 08:06:50 +00001009 { double
anthonyc1061722010-05-14 06:23:49 +00001010 sigma = fabs(args->sigma),
1011 sigma2 = fabs(args->xi),
anthony9eb4f742010-05-18 02:45:54 +00001012 A, B, R;
anthony602ab9b2010-01-05 08:06:50 +00001013
anthonyc1061722010-05-14 06:23:49 +00001014 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001015 kernel->width = (size_t)args->rho*2+1;
anthony501c2f92010-06-02 10:55:14 +00001016 else if ( (type != DoGKernel) || (sigma >= sigma2) )
anthonyc1061722010-05-14 06:23:49 +00001017 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma);
1018 else
1019 kernel->width = GetOptimalKernelWidth2D(args->rho,sigma2);
1020 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001021 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001022 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1023 kernel->height*sizeof(double));
1024 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001025 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001026
anthony46a369d2010-05-19 02:41:48 +00001027 /* WARNING: The following generates a 'sampled gaussian' kernel.
anthony9eb4f742010-05-18 02:45:54 +00001028 * What we really want is a 'discrete gaussian' kernel.
anthony46a369d2010-05-19 02:41:48 +00001029 *
1030 * How to do this is currently not known, but appears to be
1031 * basied on the Error Function 'erf()' (intergral of a gaussian)
anthony9eb4f742010-05-18 02:45:54 +00001032 */
1033
anthony501c2f92010-06-02 10:55:14 +00001034 if ( type == GaussianKernel || type == DoGKernel )
1035 { /* Calculate a Gaussian, OR positive half of a DoG */
anthony9eb4f742010-05-18 02:45:54 +00001036 if ( sigma > MagickEpsilon )
1037 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1038 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001039 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1040 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001041 kernel->values[i] = exp(-((double)(u*u+v*v))*A)*B;
1042 }
1043 else /* limiting case - a unity (normalized Dirac) kernel */
1044 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1045 kernel->width*kernel->height*sizeof(double));
1046 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1047 }
anthonyc1061722010-05-14 06:23:49 +00001048 }
anthony9eb4f742010-05-18 02:45:54 +00001049
anthony501c2f92010-06-02 10:55:14 +00001050 if ( type == DoGKernel )
anthonyc1061722010-05-14 06:23:49 +00001051 { /* Subtract a Negative Gaussian for "Difference of Gaussian" */
1052 if ( sigma2 > MagickEpsilon )
1053 { sigma = sigma2; /* simplify loop expressions */
anthony9eb4f742010-05-18 02:45:54 +00001054 A = 1.0/(2.0*sigma*sigma);
1055 B = 1.0/(Magick2PI*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001056 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1057 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001058 kernel->values[i] -= exp(-((double)(u*u+v*v))*A)*B;
anthonyc1061722010-05-14 06:23:49 +00001059 }
anthony9eb4f742010-05-18 02:45:54 +00001060 else /* limiting case - a unity (normalized Dirac) kernel */
anthonyc1061722010-05-14 06:23:49 +00001061 kernel->values[kernel->x+kernel->y*kernel->width] -= 1.0;
1062 }
anthony9eb4f742010-05-18 02:45:54 +00001063
anthony501c2f92010-06-02 10:55:14 +00001064 if ( type == LoGKernel )
anthony9eb4f742010-05-18 02:45:54 +00001065 { /* Calculate a Laplacian of a Gaussian - Or Mexician Hat */
1066 if ( sigma > MagickEpsilon )
1067 { A = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1068 B = 1.0/(MagickPI*sigma*sigma*sigma*sigma);
cristybb503372010-05-27 20:51:26 +00001069 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1070 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony9eb4f742010-05-18 02:45:54 +00001071 { R = ((double)(u*u+v*v))*A;
1072 kernel->values[i] = (1-R)*exp(-R)*B;
1073 }
1074 }
1075 else /* special case - generate a unity kernel */
1076 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1077 kernel->width*kernel->height*sizeof(double));
1078 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1079 }
1080 }
1081
1082 /* Note the above kernels may have been 'clipped' by a user defined
anthonyc1061722010-05-14 06:23:49 +00001083 ** radius, producing a smaller (darker) kernel. Also for very small
1084 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1085 ** producing a very bright kernel.
1086 **
1087 ** Normalization will still be needed.
1088 */
anthony602ab9b2010-01-05 08:06:50 +00001089
anthony3dd0f622010-05-13 12:57:32 +00001090 /* Normalize the 2D Gaussian Kernel
1091 **
anthonyc1061722010-05-14 06:23:49 +00001092 ** NB: a CorrelateNormalize performs a normal Normalize if
1093 ** there are no negative values.
anthony3dd0f622010-05-13 12:57:32 +00001094 */
anthony46a369d2010-05-19 02:41:48 +00001095 CalcKernelMetaData(kernel); /* the other kernel meta-data */
anthonyc1061722010-05-14 06:23:49 +00001096 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthony602ab9b2010-01-05 08:06:50 +00001097
1098 break;
1099 }
1100 case BlurKernel:
1101 { double
anthonyc1061722010-05-14 06:23:49 +00001102 sigma = fabs(args->sigma),
anthony501c2f92010-06-02 10:55:14 +00001103 alpha, beta;
anthony602ab9b2010-01-05 08:06:50 +00001104
anthonyc1061722010-05-14 06:23:49 +00001105 if ( args->rho >= 1.0 )
cristybb503372010-05-27 20:51:26 +00001106 kernel->width = (size_t)args->rho*2+1;
anthonyc1061722010-05-14 06:23:49 +00001107 else
anthony501c2f92010-06-02 10:55:14 +00001108 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
anthony602ab9b2010-01-05 08:06:50 +00001109 kernel->height = 1;
cristybb503372010-05-27 20:51:26 +00001110 kernel->x = (ssize_t) (kernel->width-1)/2;
cristyc99304f2010-02-01 15:26:27 +00001111 kernel->y = 0;
1112 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001113 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1114 kernel->height*sizeof(double));
1115 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001116 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001117
1118#if 1
1119#define KernelRank 3
1120 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
1121 ** It generates a gaussian 3 times the width, and compresses it into
1122 ** the expected range. This produces a closer normalization of the
1123 ** resulting kernel, especially for very low sigma values.
1124 ** As such while wierd it is prefered.
1125 **
1126 ** I am told this method originally came from Photoshop.
anthony9eb4f742010-05-18 02:45:54 +00001127 **
1128 ** A properly normalized curve is generated (apart from edge clipping)
1129 ** even though we later normalize the result (for edge clipping)
1130 ** to allow the correct generation of a "Difference of Blurs".
anthony602ab9b2010-01-05 08:06:50 +00001131 */
anthonyc1061722010-05-14 06:23:49 +00001132
1133 /* initialize */
cristybb503372010-05-27 20:51:26 +00001134 v = (ssize_t) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
anthony9eb4f742010-05-18 02:45:54 +00001135 (void) ResetMagickMemory(kernel->values,0, (size_t)
1136 kernel->width*kernel->height*sizeof(double));
anthonyc1061722010-05-14 06:23:49 +00001137 /* Calculate a Positive 1D Gaussian */
1138 if ( sigma > MagickEpsilon )
1139 { sigma *= KernelRank; /* simplify loop expressions */
anthony501c2f92010-06-02 10:55:14 +00001140 alpha = 1.0/(2.0*sigma*sigma);
1141 beta= 1.0/(MagickSQ2PI*sigma );
anthonyc1061722010-05-14 06:23:49 +00001142 for ( u=-v; u <= v; u++) {
anthony501c2f92010-06-02 10:55:14 +00001143 kernel->values[(u+v)/KernelRank] +=
1144 exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001145 }
1146 }
1147 else /* special case - generate a unity kernel */
1148 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001149#else
anthonyc1061722010-05-14 06:23:49 +00001150 /* Direct calculation without curve averaging */
1151
1152 /* Calculate a Positive Gaussian */
1153 if ( sigma > MagickEpsilon )
anthony501c2f92010-06-02 10:55:14 +00001154 { alpha = 1.0/(2.0*sigma*sigma); /* simplify loop expressions */
1155 beta = 1.0/(MagickSQ2PI*sigma);
cristybb503372010-05-27 20:51:26 +00001156 for ( i=0, u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony501c2f92010-06-02 10:55:14 +00001157 kernel->values[i] = exp(-((double)(u*u))*alpha)*beta;
anthonyc1061722010-05-14 06:23:49 +00001158 }
1159 else /* special case - generate a unity kernel */
1160 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1161 kernel->width*kernel->height*sizeof(double));
1162 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1163 }
anthony602ab9b2010-01-05 08:06:50 +00001164#endif
anthonyc1061722010-05-14 06:23:49 +00001165 /* Note the above kernel may have been 'clipped' by a user defined
anthonycc6c8362010-01-25 04:14:01 +00001166 ** radius, producing a smaller (darker) kernel. Also for very small
1167 ** sigma's (> 0.1) the central value becomes larger than one, and thus
1168 ** producing a very bright kernel.
anthonyc1061722010-05-14 06:23:49 +00001169 **
1170 ** Normalization will still be needed.
anthony602ab9b2010-01-05 08:06:50 +00001171 */
anthonycc6c8362010-01-25 04:14:01 +00001172
anthony602ab9b2010-01-05 08:06:50 +00001173 /* Normalize the 1D Gaussian Kernel
1174 **
anthonyc1061722010-05-14 06:23:49 +00001175 ** NB: a CorrelateNormalize performs a normal Normalize if
1176 ** there are no negative values.
anthony602ab9b2010-01-05 08:06:50 +00001177 */
anthony46a369d2010-05-19 02:41:48 +00001178 CalcKernelMetaData(kernel); /* the other kernel meta-data */
1179 ScaleKernelInfo(kernel, 1.0, CorrelateNormalizeValue);
anthonycc6c8362010-01-25 04:14:01 +00001180
anthonyc1061722010-05-14 06:23:49 +00001181 /* rotate the 1D kernel by given angle */
anthony501c2f92010-06-02 10:55:14 +00001182 RotateKernelInfo(kernel, args->xi );
anthony602ab9b2010-01-05 08:06:50 +00001183 break;
1184 }
1185 case CometKernel:
1186 { double
anthony9eb4f742010-05-18 02:45:54 +00001187 sigma = fabs(args->sigma),
1188 A;
anthony602ab9b2010-01-05 08:06:50 +00001189
anthony602ab9b2010-01-05 08:06:50 +00001190 if ( args->rho < 1.0 )
anthonye1cf9462010-05-19 03:50:26 +00001191 kernel->width = (GetOptimalKernelWidth1D(args->rho,sigma)-1)/2+1;
anthony602ab9b2010-01-05 08:06:50 +00001192 else
cristybb503372010-05-27 20:51:26 +00001193 kernel->width = (size_t)args->rho;
cristyc99304f2010-02-01 15:26:27 +00001194 kernel->x = kernel->y = 0;
anthony602ab9b2010-01-05 08:06:50 +00001195 kernel->height = 1;
cristyc99304f2010-02-01 15:26:27 +00001196 kernel->negative_range = kernel->positive_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001197 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1198 kernel->height*sizeof(double));
1199 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001200 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001201
anthonyc1061722010-05-14 06:23:49 +00001202 /* A comet blur is half a 1D gaussian curve, so that the object is
anthony602ab9b2010-01-05 08:06:50 +00001203 ** blurred in one direction only. This may not be quite the right
anthony3dd0f622010-05-13 12:57:32 +00001204 ** curve to use so may change in the future. The function must be
1205 ** normalised after generation, which also resolves any clipping.
anthonyc1061722010-05-14 06:23:49 +00001206 **
1207 ** As we are normalizing and not subtracting gaussians,
1208 ** there is no need for a divisor in the gaussian formula
1209 **
anthony43c49252010-05-18 10:59:50 +00001210 ** It is less comples
anthony602ab9b2010-01-05 08:06:50 +00001211 */
anthony9eb4f742010-05-18 02:45:54 +00001212 if ( sigma > MagickEpsilon )
1213 {
anthony602ab9b2010-01-05 08:06:50 +00001214#if 1
1215#define KernelRank 3
cristybb503372010-05-27 20:51:26 +00001216 v = (ssize_t) kernel->width*KernelRank; /* start/end points */
anthony9eb4f742010-05-18 02:45:54 +00001217 (void) ResetMagickMemory(kernel->values,0, (size_t)
1218 kernel->width*sizeof(double));
1219 sigma *= KernelRank; /* simplify the loop expression */
1220 A = 1.0/(2.0*sigma*sigma);
1221 /* B = 1.0/(MagickSQ2PI*sigma); */
1222 for ( u=0; u < v; u++) {
1223 kernel->values[u/KernelRank] +=
1224 exp(-((double)(u*u))*A);
1225 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
1226 }
cristybb503372010-05-27 20:51:26 +00001227 for (i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001228 kernel->positive_range += kernel->values[i];
anthony602ab9b2010-01-05 08:06:50 +00001229#else
anthony9eb4f742010-05-18 02:45:54 +00001230 A = 1.0/(2.0*sigma*sigma); /* simplify the loop expression */
1231 /* B = 1.0/(MagickSQ2PI*sigma); */
cristybb503372010-05-27 20:51:26 +00001232 for ( i=0; i < (ssize_t) kernel->width; i++)
anthony9eb4f742010-05-18 02:45:54 +00001233 kernel->positive_range +=
1234 kernel->values[i] =
1235 exp(-((double)(i*i))*A);
1236 /* exp(-((double)(i*i))/2.0*sigma*sigma)/(MagickSQ2PI*sigma); */
anthony602ab9b2010-01-05 08:06:50 +00001237#endif
anthony9eb4f742010-05-18 02:45:54 +00001238 }
1239 else /* special case - generate a unity kernel */
1240 { (void) ResetMagickMemory(kernel->values,0, (size_t)
1241 kernel->width*kernel->height*sizeof(double));
1242 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1243 kernel->positive_range = 1.0;
1244 }
anthony46a369d2010-05-19 02:41:48 +00001245
1246 kernel->minimum = 0.0;
cristyc99304f2010-02-01 15:26:27 +00001247 kernel->maximum = kernel->values[0];
anthony46a369d2010-05-19 02:41:48 +00001248 kernel->negative_range = 0.0;
anthony602ab9b2010-01-05 08:06:50 +00001249
anthony999bb2c2010-02-18 12:38:01 +00001250 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
1251 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
anthony602ab9b2010-01-05 08:06:50 +00001252 break;
1253 }
anthonyc1061722010-05-14 06:23:49 +00001254
anthony3c10fc82010-05-13 02:40:51 +00001255 /* Convolution Kernels - Well Known Constants */
anthony3c10fc82010-05-13 02:40:51 +00001256 case LaplacianKernel:
anthonye2a60ce2010-05-19 12:30:40 +00001257 { switch ( (int) args->rho ) {
anthony3dd0f622010-05-13 12:57:32 +00001258 case 0:
anthony9eb4f742010-05-18 02:45:54 +00001259 default: /* laplacian square filter -- default */
anthonyc1061722010-05-14 06:23:49 +00001260 kernel=ParseKernelArray("3: -1,-1,-1 -1,8,-1 -1,-1,-1");
anthony3dd0f622010-05-13 12:57:32 +00001261 break;
anthony9eb4f742010-05-18 02:45:54 +00001262 case 1: /* laplacian diamond filter */
anthonyc1061722010-05-14 06:23:49 +00001263 kernel=ParseKernelArray("3: 0,-1,0 -1,4,-1 0,-1,0");
anthony3c10fc82010-05-13 02:40:51 +00001264 break;
1265 case 2:
anthony9eb4f742010-05-18 02:45:54 +00001266 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
1267 break;
1268 case 3:
anthonyc1061722010-05-14 06:23:49 +00001269 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 1,-2,1");
anthony3c10fc82010-05-13 02:40:51 +00001270 break;
anthony9eb4f742010-05-18 02:45:54 +00001271 case 5: /* a 5x5 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001272 kernel=ParseKernelArray(
anthony9eb4f742010-05-18 02:45:54 +00001273 "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 +00001274 break;
anthony9eb4f742010-05-18 02:45:54 +00001275 case 7: /* a 7x7 laplacian */
anthony3c10fc82010-05-13 02:40:51 +00001276 kernel=ParseKernelArray(
anthonyc1061722010-05-14 06:23:49 +00001277 "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 +00001278 break;
anthony501c2f92010-06-02 10:55:14 +00001279 case 15: /* a 5x5 LoG (sigma approx 1.4) */
anthony9eb4f742010-05-18 02:45:54 +00001280 kernel=ParseKernelArray(
1281 "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");
1282 break;
anthony501c2f92010-06-02 10:55:14 +00001283 case 19: /* a 9x9 LoG (sigma approx 1.4) */
anthony43c49252010-05-18 10:59:50 +00001284 /* http://www.cscjournals.org/csc/manuscript/Journals/IJIP/volume3/Issue1/IJIP-15.pdf */
1285 kernel=ParseKernelArray(
anthonybfb635a2010-06-04 00:18:04 +00001286 "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 +00001287 break;
anthony3c10fc82010-05-13 02:40:51 +00001288 }
1289 if (kernel == (KernelInfo *) NULL)
1290 return(kernel);
1291 kernel->type = type;
1292 break;
1293 }
anthonyc1061722010-05-14 06:23:49 +00001294 case SobelKernel:
anthonydcc2a472010-06-10 07:13:20 +00001295#if 0
1296 { /* Sobel with optional 'sub-types' */
1297 switch ( (int) args->rho ) {
anthonyc40ac1e2010-06-06 11:49:31 +00001298 default:
1299 case 0:
1300 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
1301 if (kernel == (KernelInfo *) NULL)
1302 return(kernel);
1303 kernel->type = type;
1304 break;
1305 case 1:
1306 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
1307 if (kernel == (KernelInfo *) NULL)
1308 return(kernel);
1309 kernel->type = type;
1310 ScaleKernelInfo(kernel, 0.25, NoValue);
1311 break;
1312 case 2:
1313 kernel=ParseKernelArray("3: 1,2,0 2,0,-2 0,-2,-1");
1314 if (kernel == (KernelInfo *) NULL)
1315 return(kernel);
1316 kernel->type = type;
1317 ScaleKernelInfo(kernel, 0.25, NoValue);
1318 break;
1319 }
anthony32066782010-06-08 13:46:27 +00001320 if ( fabs(args->sigma) > MagickEpsilon )
1321 /* Rotate by correctly supplied 'angle' */
1322 RotateKernelInfo(kernel, args->sigma);
1323 else if ( args->rho > 30.0 || args->rho < -30.0 )
1324 /* Rotate by out of bounds 'type' */
1325 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001326 break;
1327 }
anthonycceb6f02010-06-10 22:57:38 +00001328#else
1329 { /* Simple Sobel Kernel */
1330 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
1331 if (kernel == (KernelInfo *) NULL)
1332 return(kernel);
1333 kernel->type = type;
1334 RotateKernelInfo(kernel, args->rho);
1335 break;
1336 }
1337#endif
anthonyc1061722010-05-14 06:23:49 +00001338 case RobertsKernel:
1339 {
anthony501c2f92010-06-02 10:55:14 +00001340 kernel=ParseKernelArray("3: 0,0,0 1,-1,0 0,0,0");
anthonyc1061722010-05-14 06:23:49 +00001341 if (kernel == (KernelInfo *) NULL)
1342 return(kernel);
1343 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001344 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001345 break;
1346 }
1347 case PrewittKernel:
1348 {
anthony501c2f92010-06-02 10:55:14 +00001349 kernel=ParseKernelArray("3: 1,0,-1 1,0,-1 1,0,-1");
anthonyc1061722010-05-14 06:23:49 +00001350 if (kernel == (KernelInfo *) NULL)
1351 return(kernel);
1352 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001353 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001354 break;
1355 }
1356 case CompassKernel:
1357 {
anthony501c2f92010-06-02 10:55:14 +00001358 kernel=ParseKernelArray("3: 1,1,-1 1,-2,-1 1,1,-1");
anthonyc1061722010-05-14 06:23:49 +00001359 if (kernel == (KernelInfo *) NULL)
1360 return(kernel);
1361 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001362 RotateKernelInfo(kernel, args->rho);
anthonyc1061722010-05-14 06:23:49 +00001363 break;
1364 }
anthony9eb4f742010-05-18 02:45:54 +00001365 case KirschKernel:
1366 {
anthony501c2f92010-06-02 10:55:14 +00001367 kernel=ParseKernelArray("3: 5,-3,-3 5,0,-3 5,-3,-3");
anthony9eb4f742010-05-18 02:45:54 +00001368 if (kernel == (KernelInfo *) NULL)
1369 return(kernel);
1370 kernel->type = type;
anthony46a369d2010-05-19 02:41:48 +00001371 RotateKernelInfo(kernel, args->rho);
anthony9eb4f742010-05-18 02:45:54 +00001372 break;
1373 }
anthonye2a60ce2010-05-19 12:30:40 +00001374 case FreiChenKernel:
anthony501c2f92010-06-02 10:55:14 +00001375 /* Direction is set to be left to right positive */
1376 /* http://www.math.tau.ac.il/~turkel/notes/edge_detectors.pdf -- RIGHT? */
1377 /* http://ltswww.epfl.ch/~courstiv/exos_labos/sol3.pdf -- WRONG? */
anthony1dd091a2010-05-27 06:31:15 +00001378 { switch ( (int) args->rho ) {
anthonye2a60ce2010-05-19 12:30:40 +00001379 default:
anthonyc3cd15b2010-05-27 06:05:40 +00001380 case 0:
anthony501c2f92010-06-02 10:55:14 +00001381 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonyc3cd15b2010-05-27 06:05:40 +00001382 if (kernel == (KernelInfo *) NULL)
1383 return(kernel);
anthonyef33d9f2010-06-02 12:27:01 +00001384 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001385 kernel->values[3] = +MagickSQ2;
1386 kernel->values[5] = -MagickSQ2;
anthonyc3cd15b2010-05-27 06:05:40 +00001387 CalcKernelMetaData(kernel); /* recalculate meta-data */
anthonyc3cd15b2010-05-27 06:05:40 +00001388 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001389 case 2:
1390 kernel=ParseKernelArray("3: 1,2,0 2,0,-2 0,-2,-1");
1391 if (kernel == (KernelInfo *) NULL)
1392 return(kernel);
1393 kernel->type = type;
1394 kernel->values[1] = kernel->values[3] = +MagickSQ2;
1395 kernel->values[5] = kernel->values[7] = -MagickSQ2;
1396 CalcKernelMetaData(kernel); /* recalculate meta-data */
1397 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1398 break;
1399 case 10:
1400 kernel=AcquireKernelInfo("FreiChen:11;FreiChen:12;FreiChen:13;FreiChen:14;FreiChen:15;FreiChen:16;FreiChen:17;FreiChen:18;FreiChen:19");
1401 if (kernel == (KernelInfo *) NULL)
1402 return(kernel);
1403 break;
anthonye2a60ce2010-05-19 12:30:40 +00001404 case 1:
anthonyc40ac1e2010-06-06 11:49:31 +00001405 case 11:
anthony501c2f92010-06-02 10:55:14 +00001406 kernel=ParseKernelArray("3: 1,0,-1 2,0,-2 1,0,-1");
anthonye2a60ce2010-05-19 12:30:40 +00001407 if (kernel == (KernelInfo *) NULL)
1408 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001409 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001410 kernel->values[3] = +MagickSQ2;
1411 kernel->values[5] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001412 CalcKernelMetaData(kernel); /* recalculate meta-data */
1413 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1414 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001415 case 12:
anthony501c2f92010-06-02 10:55:14 +00001416 kernel=ParseKernelArray("3: 1,2,1 0,0,0 1,2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001417 if (kernel == (KernelInfo *) NULL)
1418 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001419 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001420 kernel->values[1] = +MagickSQ2;
1421 kernel->values[7] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001422 CalcKernelMetaData(kernel);
1423 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1424 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001425 case 13:
anthony501c2f92010-06-02 10:55:14 +00001426 kernel=ParseKernelArray("3: 2,-1,0 -1,0,1 0,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001427 if (kernel == (KernelInfo *) NULL)
1428 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001429 kernel->type = type;
anthony501c2f92010-06-02 10:55:14 +00001430 kernel->values[0] = +MagickSQ2;
1431 kernel->values[8] = -MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001432 CalcKernelMetaData(kernel);
1433 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1434 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001435 case 14:
anthony1d5e6702010-05-31 10:19:12 +00001436 kernel=ParseKernelArray("3: 0,1,-2 -1,0,1 2,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001437 if (kernel == (KernelInfo *) NULL)
1438 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001439 kernel->type = type;
anthony1d5e6702010-05-31 10:19:12 +00001440 kernel->values[2] = -MagickSQ2;
1441 kernel->values[6] = +MagickSQ2;
anthonye2a60ce2010-05-19 12:30:40 +00001442 CalcKernelMetaData(kernel);
1443 ScaleKernelInfo(kernel, 1.0/2.0*MagickSQ2, NoValue);
1444 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001445 case 15:
anthony501c2f92010-06-02 10:55:14 +00001446 kernel=ParseKernelArray("3: 0,-1,0 1,0,1 0,-1,0");
anthonye2a60ce2010-05-19 12:30:40 +00001447 if (kernel == (KernelInfo *) NULL)
1448 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001449 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001450 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1451 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001452 case 16:
anthony1d5e6702010-05-31 10:19:12 +00001453 kernel=ParseKernelArray("3: 1,0,-1 0,0,0 -1,0,1");
anthonye2a60ce2010-05-19 12:30:40 +00001454 if (kernel == (KernelInfo *) NULL)
1455 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001456 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001457 ScaleKernelInfo(kernel, 1.0/2.0, NoValue);
1458 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001459 case 17:
anthony501c2f92010-06-02 10:55:14 +00001460 kernel=ParseKernelArray("3: 1,-2,1 -2,4,-2 -1,-2,1");
anthonye2a60ce2010-05-19 12:30:40 +00001461 if (kernel == (KernelInfo *) NULL)
1462 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001463 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001464 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1465 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001466 case 18:
anthony501c2f92010-06-02 10:55:14 +00001467 kernel=ParseKernelArray("3: -2,1,-2 1,4,1 -2,1,-2");
anthonye2a60ce2010-05-19 12:30:40 +00001468 if (kernel == (KernelInfo *) NULL)
1469 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001470 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001471 ScaleKernelInfo(kernel, 1.0/6.0, NoValue);
1472 break;
anthonyc40ac1e2010-06-06 11:49:31 +00001473 case 19:
anthonyc3cd15b2010-05-27 06:05:40 +00001474 kernel=ParseKernelArray("3: 1,1,1 1,1,1 1,1,1");
anthonye2a60ce2010-05-19 12:30:40 +00001475 if (kernel == (KernelInfo *) NULL)
1476 return(kernel);
anthonyc3cd15b2010-05-27 06:05:40 +00001477 kernel->type = type;
anthonye2a60ce2010-05-19 12:30:40 +00001478 ScaleKernelInfo(kernel, 1.0/3.0, NoValue);
1479 break;
1480 }
anthonyc3cd15b2010-05-27 06:05:40 +00001481 if ( fabs(args->sigma) > MagickEpsilon )
1482 /* Rotate by correctly supplied 'angle' */
1483 RotateKernelInfo(kernel, args->sigma);
1484 else if ( args->rho > 30.0 || args->rho < -30.0 )
1485 /* Rotate by out of bounds 'type' */
1486 RotateKernelInfo(kernel, args->rho);
anthonye2a60ce2010-05-19 12:30:40 +00001487 break;
1488 }
1489
anthonyc1061722010-05-14 06:23:49 +00001490 /* Boolean Kernels */
1491 case DiamondKernel:
1492 {
1493 if (args->rho < 1.0)
1494 kernel->width = kernel->height = 3; /* default radius = 1 */
1495 else
cristybb503372010-05-27 20:51:26 +00001496 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1497 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthonyc1061722010-05-14 06:23:49 +00001498
1499 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1500 kernel->height*sizeof(double));
1501 if (kernel->values == (double *) NULL)
1502 return(DestroyKernelInfo(kernel));
1503
1504 /* set all kernel values within diamond area to scale given */
cristybb503372010-05-27 20:51:26 +00001505 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1506 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony1d5e6702010-05-31 10:19:12 +00001507 if ( (labs((long) u)+labs((long) v)) <= (long) kernel->x)
anthonyc1061722010-05-14 06:23:49 +00001508 kernel->positive_range += kernel->values[i] = args->sigma;
1509 else
1510 kernel->values[i] = nan;
1511 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1512 break;
1513 }
1514 case SquareKernel:
1515 case RectangleKernel:
1516 { double
1517 scale;
anthony602ab9b2010-01-05 08:06:50 +00001518 if ( type == SquareKernel )
1519 {
1520 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001521 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00001522 else
cristybb503372010-05-27 20:51:26 +00001523 kernel->width = kernel->height = (size_t) (2*args->rho+1);
1524 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony4fd27e22010-02-07 08:17:18 +00001525 scale = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001526 }
1527 else {
cristy2be15382010-01-21 02:38:03 +00001528 /* NOTE: user defaults set in "AcquireKernelInfo()" */
anthony602ab9b2010-01-05 08:06:50 +00001529 if ( args->rho < 1.0 || args->sigma < 1.0 )
anthony83ba99b2010-01-24 08:48:15 +00001530 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001531 kernel->width = (size_t)args->rho;
1532 kernel->height = (size_t)args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001533 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
1534 args->psi < 0.0 || args->psi > (double)kernel->height )
anthony83ba99b2010-01-24 08:48:15 +00001535 return(DestroyKernelInfo(kernel)); /* invalid args given */
cristybb503372010-05-27 20:51:26 +00001536 kernel->x = (ssize_t) args->xi;
1537 kernel->y = (ssize_t) args->psi;
anthony4fd27e22010-02-07 08:17:18 +00001538 scale = 1.0;
anthony602ab9b2010-01-05 08:06:50 +00001539 }
1540 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1541 kernel->height*sizeof(double));
1542 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001543 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001544
anthony3dd0f622010-05-13 12:57:32 +00001545 /* set all kernel values to scale given */
cristyeaedf062010-05-29 22:36:02 +00001546 u=(ssize_t) (kernel->width*kernel->height);
cristy150989e2010-02-01 14:59:39 +00001547 for ( i=0; i < u; i++)
anthony4fd27e22010-02-07 08:17:18 +00001548 kernel->values[i] = scale;
1549 kernel->minimum = kernel->maximum = scale; /* a flat shape */
1550 kernel->positive_range = scale*u;
anthonycc6c8362010-01-25 04:14:01 +00001551 break;
anthony602ab9b2010-01-05 08:06:50 +00001552 }
anthony602ab9b2010-01-05 08:06:50 +00001553 case DiskKernel:
1554 {
anthonye4d89962010-05-29 10:53:11 +00001555 ssize_t
1556 limit = (ssize_t)(args->rho*args->rho);
1557
1558 if (args->rho < 0.4) /* default radius approx 3.5 */
anthony83ba99b2010-01-24 08:48:15 +00001559 kernel->width = kernel->height = 7L, limit = 10L;
anthony602ab9b2010-01-05 08:06:50 +00001560 else
anthonye4d89962010-05-29 10:53:11 +00001561 kernel->width = kernel->height = (size_t)fabs(args->rho)*2+1;
cristybb503372010-05-27 20:51:26 +00001562 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001563
1564 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1565 kernel->height*sizeof(double));
1566 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001567 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001568
anthony3dd0f622010-05-13 12:57:32 +00001569 /* set all kernel values within disk area to scale given */
cristybb503372010-05-27 20:51:26 +00001570 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1571 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony602ab9b2010-01-05 08:06:50 +00001572 if ((u*u+v*v) <= limit)
anthony4fd27e22010-02-07 08:17:18 +00001573 kernel->positive_range += kernel->values[i] = args->sigma;
anthony602ab9b2010-01-05 08:06:50 +00001574 else
1575 kernel->values[i] = nan;
anthony4fd27e22010-02-07 08:17:18 +00001576 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
anthony602ab9b2010-01-05 08:06:50 +00001577 break;
1578 }
1579 case PlusKernel:
1580 {
1581 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00001582 kernel->width = kernel->height = 5; /* default radius 2 */
anthony602ab9b2010-01-05 08:06:50 +00001583 else
cristybb503372010-05-27 20:51:26 +00001584 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1585 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00001586
1587 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1588 kernel->height*sizeof(double));
1589 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00001590 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00001591
cristycee97112010-05-28 00:44:52 +00001592 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001593 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1594 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony4fd27e22010-02-07 08:17:18 +00001595 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
1596 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1597 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
anthony602ab9b2010-01-05 08:06:50 +00001598 break;
1599 }
anthony3dd0f622010-05-13 12:57:32 +00001600 case CrossKernel:
1601 {
1602 if (args->rho < 1.0)
1603 kernel->width = kernel->height = 5; /* default radius 2 */
1604 else
cristybb503372010-05-27 20:51:26 +00001605 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
1606 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001607
1608 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1609 kernel->height*sizeof(double));
1610 if (kernel->values == (double *) NULL)
1611 return(DestroyKernelInfo(kernel));
1612
cristycee97112010-05-28 00:44:52 +00001613 /* set all kernel values along axises to given scale */
cristybb503372010-05-27 20:51:26 +00001614 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
1615 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
anthony3dd0f622010-05-13 12:57:32 +00001616 kernel->values[i] = (u == v || u == -v) ? args->sigma : nan;
1617 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
1618 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
1619 break;
1620 }
1621 /* HitAndMiss Kernels */
anthonyc1061722010-05-14 06:23:49 +00001622 case RingKernel:
anthony3dd0f622010-05-13 12:57:32 +00001623 case PeaksKernel:
1624 {
cristybb503372010-05-27 20:51:26 +00001625 ssize_t
anthony3dd0f622010-05-13 12:57:32 +00001626 limit1,
anthonyc1061722010-05-14 06:23:49 +00001627 limit2,
1628 scale;
anthony3dd0f622010-05-13 12:57:32 +00001629
1630 if (args->rho < args->sigma)
1631 {
cristybb503372010-05-27 20:51:26 +00001632 kernel->width = ((size_t)args->sigma)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001633 limit1 = (ssize_t)(args->rho*args->rho);
1634 limit2 = (ssize_t)(args->sigma*args->sigma);
anthony3dd0f622010-05-13 12:57:32 +00001635 }
1636 else
1637 {
cristybb503372010-05-27 20:51:26 +00001638 kernel->width = ((size_t)args->rho)*2+1;
anthonye4d89962010-05-29 10:53:11 +00001639 limit1 = (ssize_t)(args->sigma*args->sigma);
1640 limit2 = (ssize_t)(args->rho*args->rho);
anthony3dd0f622010-05-13 12:57:32 +00001641 }
anthonyc1061722010-05-14 06:23:49 +00001642 if ( limit2 <= 0 )
1643 kernel->width = 7L, limit1 = 7L, limit2 = 11L;
1644
anthony3dd0f622010-05-13 12:57:32 +00001645 kernel->height = kernel->width;
cristybb503372010-05-27 20:51:26 +00001646 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony3dd0f622010-05-13 12:57:32 +00001647 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1648 kernel->height*sizeof(double));
1649 if (kernel->values == (double *) NULL)
1650 return(DestroyKernelInfo(kernel));
1651
anthonyc1061722010-05-14 06:23:49 +00001652 /* set a ring of points of 'scale' ( 0.0 for PeaksKernel ) */
cristybb503372010-05-27 20:51:26 +00001653 scale = (ssize_t) (( type == PeaksKernel) ? 0.0 : args->xi);
1654 for ( i=0, v= -kernel->y; v <= (ssize_t)kernel->y; v++)
1655 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
1656 { ssize_t radius=u*u+v*v;
anthonyc1061722010-05-14 06:23:49 +00001657 if (limit1 < radius && radius <= limit2)
cristye96405a2010-05-19 02:24:31 +00001658 kernel->positive_range += kernel->values[i] = (double) scale;
anthony3dd0f622010-05-13 12:57:32 +00001659 else
1660 kernel->values[i] = nan;
1661 }
cristye96405a2010-05-19 02:24:31 +00001662 kernel->minimum = kernel->minimum = (double) scale;
anthonyc1061722010-05-14 06:23:49 +00001663 if ( type == PeaksKernel ) {
1664 /* set the central point in the middle */
1665 kernel->values[kernel->x+kernel->y*kernel->width] = 1.0;
1666 kernel->positive_range = 1.0;
1667 kernel->maximum = 1.0;
1668 }
anthony3dd0f622010-05-13 12:57:32 +00001669 break;
1670 }
anthony43c49252010-05-18 10:59:50 +00001671 case EdgesKernel:
1672 {
1673 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1674 if (kernel == (KernelInfo *) NULL)
1675 return(kernel);
1676 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001677 ExpandMirrorKernelInfo(kernel); /* mirror expansion of other kernels */
anthony43c49252010-05-18 10:59:50 +00001678 break;
1679 }
anthony3dd0f622010-05-13 12:57:32 +00001680 case CornersKernel:
1681 {
anthony4f1dcb72010-05-14 08:43:10 +00001682 kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
anthony3dd0f622010-05-13 12:57:32 +00001683 if (kernel == (KernelInfo *) NULL)
1684 return(kernel);
1685 kernel->type = type;
anthonybfb635a2010-06-04 00:18:04 +00001686 ExpandRotateKernelInfo(kernel, 90.0); /* Expand 90 degree rotations */
anthony3dd0f622010-05-13 12:57:32 +00001687 break;
1688 }
anthony68cf70d2010-06-13 12:51:53 +00001689 case ThinDiagonalsKernel:
1690 {
1691 switch ( (int) args->rho ) {
1692 case 0:
1693 default:
1694 { KernelInfo
1695 *new_kernel;
1696 kernel=ParseKernelArray("3: 0,0,0 0,1,1 1,1,-");
1697 if (kernel == (KernelInfo *) NULL)
1698 return(kernel);
1699 kernel->type = type;
1700 new_kernel=ParseKernelArray("3: 0,0,1 0,1,1 0,1,-");
1701 if (new_kernel == (KernelInfo *) NULL)
1702 return(DestroyKernelInfo(kernel));
1703 new_kernel->type = type;
1704 LastKernelInfo(kernel)->next = new_kernel;
1705 ExpandMirrorKernelInfo(kernel);
1706 break;
1707 }
1708 case 1:
1709 kernel=ParseKernelArray("3: 0,0,0 0,1,1 1,1,-");
1710 if (kernel == (KernelInfo *) NULL)
1711 return(kernel);
1712 kernel->type = type;
1713 RotateKernelInfo(kernel, args->sigma);
1714 break;
1715 case 2:
1716 kernel=ParseKernelArray("3: 0,0,1 0,1,1 0,1,-");
1717 if (kernel == (KernelInfo *) NULL)
1718 return(kernel);
1719 kernel->type = type;
1720 RotateKernelInfo(kernel, args->sigma);
1721 break;
1722 }
1723 break;
1724 }
anthony3dd0f622010-05-13 12:57:32 +00001725 case LineEndsKernel:
anthony694934f2010-06-07 10:30:40 +00001726 { /* Kernels for finding the end of thin lines */
1727 switch ( (int) args->rho ) {
1728 case 0:
1729 default:
1730 /* set of kernels to find all end of lines */
1731 kernel=AcquireKernelInfo("LineEnds:1>;LineEnds:2>");
1732 if (kernel == (KernelInfo *) NULL)
1733 return(kernel);
1734 break;
1735 case 1:
1736 /* kernel for 4-connected line ends - no rotation */
anthonye5719282010-06-11 13:23:00 +00001737 kernel=ParseKernelArray("3: 0,0,- 0,1,1 0,0,-");
anthony694934f2010-06-07 10:30:40 +00001738 if (kernel == (KernelInfo *) NULL)
1739 return(kernel);
1740 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001741 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001742 break;
1743 case 2:
1744 /* kernel to add for 8-connected lines - no rotation */
1745 kernel=ParseKernelArray("3: 0,0,0 0,1,0 0,0,1");
1746 if (kernel == (KernelInfo *) NULL)
1747 return(kernel);
1748 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001749 RotateKernelInfo(kernel, args->sigma);
1750 break;
1751 case 3:
1752 /* kernel to add for orthogonal line ends - does not find corners */
anthonye5719282010-06-11 13:23:00 +00001753 kernel=ParseKernelArray("3: 0,0,0 0,1,1 0,0,0");
anthonye85b4762010-06-11 12:04:19 +00001754 if (kernel == (KernelInfo *) NULL)
1755 return(kernel);
1756 kernel->type = type;
1757 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001758 break;
anthonyb4503542010-06-11 13:19:28 +00001759 case 4:
1760 /* traditional line end - fails on last T end */
anthonye5719282010-06-11 13:23:00 +00001761 kernel=ParseKernelArray("3: 0,0,0 0,1,- 0,0,-");
anthonyb4503542010-06-11 13:19:28 +00001762 if (kernel == (KernelInfo *) NULL)
1763 return(kernel);
1764 kernel->type = type;
1765 RotateKernelInfo(kernel, args->sigma);
1766 break;
anthony694934f2010-06-07 10:30:40 +00001767 }
anthony3dd0f622010-05-13 12:57:32 +00001768 break;
1769 }
1770 case LineJunctionsKernel:
anthony694934f2010-06-07 10:30:40 +00001771 { /* kernels for finding the junctions of multiple lines */
1772 switch ( (int) args->rho ) {
1773 case 0:
1774 default:
1775 /* set of kernels to find all line junctions */
1776 kernel=AcquireKernelInfo("LineJunctions:1@;LineJunctions:2>");
1777 if (kernel == (KernelInfo *) NULL)
1778 return(kernel);
1779 break;
1780 case 1:
1781 /* Y Junction */
1782 kernel=ParseKernelArray("3: 1,-,1 -,1,- -,1,-");
1783 if (kernel == (KernelInfo *) NULL)
1784 return(kernel);
1785 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001786 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001787 break;
1788 case 2:
1789 /* Diagonal T Junctions */
1790 kernel=ParseKernelArray("3: 1,-,- -,1,- 1,-,1");
1791 if (kernel == (KernelInfo *) NULL)
1792 return(kernel);
1793 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001794 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001795 break;
1796 case 3:
1797 /* Orthogonal T Junctions */
1798 kernel=ParseKernelArray("3: -,-,- 1,1,1 -,1,-");
1799 if (kernel == (KernelInfo *) NULL)
1800 return(kernel);
1801 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001802 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001803 break;
1804 case 4:
1805 /* Diagonal X Junctions */
1806 kernel=ParseKernelArray("3: 1,-,1 -,1,- 1,-,1");
1807 if (kernel == (KernelInfo *) NULL)
1808 return(kernel);
1809 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001810 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001811 break;
1812 case 5:
1813 /* Orthogonal X Junctions - minimal diamond kernel */
1814 kernel=ParseKernelArray("3: -,1,- 1,1,1 -,1,-");
1815 if (kernel == (KernelInfo *) NULL)
1816 return(kernel);
1817 kernel->type = type;
anthonye85b4762010-06-11 12:04:19 +00001818 RotateKernelInfo(kernel, args->sigma);
anthony694934f2010-06-07 10:30:40 +00001819 break;
1820 }
anthony4f1dcb72010-05-14 08:43:10 +00001821 break;
1822 }
anthonyc40ac1e2010-06-06 11:49:31 +00001823 case RidgesKernel:
1824 { /* Ridges - Ridge finding kernels */
1825 KernelInfo
1826 *new_kernel;
1827 switch ( (int) args->rho ) {
1828 case 1:
1829 default:
1830 kernel=ParseKernelArray("3x1:0,1,0");
1831 if (kernel == (KernelInfo *) NULL)
1832 return(kernel);
1833 kernel->type = type;
1834 ExpandRotateKernelInfo(kernel, 90.0); /* 2 rotated kernels (symmetrical) */
1835 break;
1836 case 2:
1837 kernel=ParseKernelArray("4x1:0,1,1,0");
1838 if (kernel == (KernelInfo *) NULL)
1839 return(kernel);
1840 kernel->type = type;
1841 ExpandRotateKernelInfo(kernel, 90.0); /* 4 rotated kernels */
anthony694934f2010-06-07 10:30:40 +00001842
1843 /* Kernels to find a stepped 'thick' line, 4 rotates + mirrors */
anthonyc40ac1e2010-06-06 11:49:31 +00001844 /* Unfortunatally we can not yet rotate a non-square kernel */
1845 /* But then we can't flip a non-symetrical kernel either */
1846 new_kernel=ParseKernelArray("4x3+1+1:0,1,1,- -,1,1,- -,1,1,0");
1847 if (new_kernel == (KernelInfo *) NULL)
1848 return(DestroyKernelInfo(kernel));
1849 new_kernel->type = type;
1850 LastKernelInfo(kernel)->next = new_kernel;
1851 new_kernel=ParseKernelArray("4x3+2+1:0,1,1,- -,1,1,- -,1,1,0");
1852 if (new_kernel == (KernelInfo *) NULL)
1853 return(DestroyKernelInfo(kernel));
1854 new_kernel->type = type;
1855 LastKernelInfo(kernel)->next = new_kernel;
1856 new_kernel=ParseKernelArray("4x3+1+1:-,1,1,0 -,1,1,- 0,1,1,-");
1857 if (new_kernel == (KernelInfo *) NULL)
1858 return(DestroyKernelInfo(kernel));
1859 new_kernel->type = type;
1860 LastKernelInfo(kernel)->next = new_kernel;
1861 new_kernel=ParseKernelArray("4x3+2+1:-,1,1,0 -,1,1,- 0,1,1,-");
1862 if (new_kernel == (KernelInfo *) NULL)
1863 return(DestroyKernelInfo(kernel));
1864 new_kernel->type = type;
1865 LastKernelInfo(kernel)->next = new_kernel;
1866 new_kernel=ParseKernelArray("3x4+1+1:0,-,- 1,1,1 1,1,1 -,-,0");
1867 if (new_kernel == (KernelInfo *) NULL)
1868 return(DestroyKernelInfo(kernel));
1869 new_kernel->type = type;
1870 LastKernelInfo(kernel)->next = new_kernel;
1871 new_kernel=ParseKernelArray("3x4+1+2:0,-,- 1,1,1 1,1,1 -,-,0");
1872 if (new_kernel == (KernelInfo *) NULL)
1873 return(DestroyKernelInfo(kernel));
1874 new_kernel->type = type;
1875 LastKernelInfo(kernel)->next = new_kernel;
1876 new_kernel=ParseKernelArray("3x4+1+1:-,-,0 1,1,1 1,1,1 0,-,-");
1877 if (new_kernel == (KernelInfo *) NULL)
1878 return(DestroyKernelInfo(kernel));
1879 new_kernel->type = type;
1880 LastKernelInfo(kernel)->next = new_kernel;
1881 new_kernel=ParseKernelArray("3x4+1+2:-,-,0 1,1,1 1,1,1 0,-,-");
1882 if (new_kernel == (KernelInfo *) NULL)
1883 return(DestroyKernelInfo(kernel));
1884 new_kernel->type = type;
1885 LastKernelInfo(kernel)->next = new_kernel;
1886 break;
1887 }
1888 break;
1889 }
anthony3dd0f622010-05-13 12:57:32 +00001890 case ConvexHullKernel:
1891 {
anthony3928ec62010-05-27 14:03:29 +00001892 KernelInfo
1893 *new_kernel;
1894 /* first set of 8 kernels */
anthony4f1dcb72010-05-14 08:43:10 +00001895 kernel=ParseKernelArray("3: 1,1,- 1,0,- 1,-,0");
anthony3dd0f622010-05-13 12:57:32 +00001896 if (kernel == (KernelInfo *) NULL)
1897 return(kernel);
1898 kernel->type = type;
anthony1192faa2010-06-07 22:52:06 +00001899 ExpandRotateKernelInfo(kernel, 90.0);
anthony694934f2010-06-07 10:30:40 +00001900 /* append the mirror versions too - no flip function yet */
anthony5b93cbe2010-05-27 13:54:14 +00001901 new_kernel=ParseKernelArray("3: 1,1,1 1,0,- -,-,0");
1902 if (new_kernel == (KernelInfo *) NULL)
1903 return(DestroyKernelInfo(kernel));
1904 new_kernel->type = type;
anthony1192faa2010-06-07 22:52:06 +00001905 ExpandRotateKernelInfo(new_kernel, 90.0);
anthony5b93cbe2010-05-27 13:54:14 +00001906 LastKernelInfo(kernel)->next = new_kernel;
anthony3dd0f622010-05-13 12:57:32 +00001907 break;
1908 }
anthony47f5d062010-05-23 07:47:50 +00001909 case SkeletonKernel:
anthonya648a302010-05-27 02:14:36 +00001910 {
1911 KernelInfo
1912 *new_kernel;
anthonyc40ac1e2010-06-06 11:49:31 +00001913 switch ( (int) args->rho ) {
1914 case 1:
1915 default:
1916 /* Traditional Skeleton...
1917 ** A cyclically rotated single kernel
1918 */
1919 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1920 if (kernel == (KernelInfo *) NULL)
1921 return(kernel);
1922 kernel->type = type;
1923 ExpandRotateKernelInfo(kernel, 45.0); /* 8 rotations */
1924 break;
1925 case 2:
1926 /* HIPR Variation of the cyclic skeleton
1927 ** Corners of the traditional method made more forgiving,
1928 ** but the retain the same cyclic order.
1929 */
1930 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1931 if (kernel == (KernelInfo *) NULL)
1932 return(kernel);
1933 kernel->type = type;
1934 new_kernel=ParseKernelArray("3: -,0,0 1,1,0 -,1,-");
1935 if (new_kernel == (KernelInfo *) NULL)
1936 return(new_kernel);
1937 new_kernel->type = type;
1938 LastKernelInfo(kernel)->next = new_kernel;
1939 ExpandRotateKernelInfo(kernel, 90.0); /* 4 rotations of the 2 kernels */
1940 break;
1941 case 3:
1942 /* Jittered Skeleton: do top, then bottom, then each sides */
1943 /* Do top edge */
1944 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1945 if (kernel == (KernelInfo *) NULL)
1946 return(kernel);
1947 kernel->type = type;
1948 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
1949 if (new_kernel == (KernelInfo *) NULL)
1950 return(new_kernel);
1951 new_kernel->type = type;
1952 LastKernelInfo(kernel)->next = new_kernel;
1953 new_kernel=ParseKernelArray("3: -,0,0 1,1,0 -,1,-");
1954 if (new_kernel == (KernelInfo *) NULL)
1955 return(new_kernel);
1956 new_kernel->type = type;
1957 LastKernelInfo(kernel)->next = new_kernel;
1958 /* Do Bottom edge */
1959 new_kernel=ParseKernelArray("3: 1,1,1 -,1,- 0,0,0");
1960 if (new_kernel == (KernelInfo *) NULL)
1961 return(new_kernel);
1962 new_kernel->type = type;
1963 LastKernelInfo(kernel)->next = new_kernel;
1964 new_kernel=ParseKernelArray("3: -,1,- 1,1,0 -,0,0");
1965 if (new_kernel == (KernelInfo *) NULL)
1966 return(new_kernel);
1967 new_kernel->type = type;
1968 LastKernelInfo(kernel)->next = new_kernel;
1969 new_kernel=ParseKernelArray("3: -,1,- 0,1,1 0,0,-");
1970 if (new_kernel == (KernelInfo *) NULL)
1971 return(new_kernel);
1972 new_kernel->type = type;
1973 LastKernelInfo(kernel)->next = new_kernel;
1974 /* Last the two sides */
1975 new_kernel=ParseKernelArray("3: 0,-,1 0,1,1 0,-,1");
1976 if (new_kernel == (KernelInfo *) NULL)
1977 return(new_kernel);
1978 new_kernel->type = type;
1979 LastKernelInfo(kernel)->next = new_kernel;
1980 new_kernel=ParseKernelArray("3: 1,-,0 1,1,0 1,-,0");
1981 if (new_kernel == (KernelInfo *) NULL)
1982 return(new_kernel);
1983 new_kernel->type = type;
1984 LastKernelInfo(kernel)->next = new_kernel;
1985 break;
1986 case 4:
1987 /* Just a simple 'Edge' kernel, but with a extra two kernels
1988 ** to finish off diagonal lines, top then bottom then sides.
1989 ** Works well for test case but fails for general case.
1990 */
1991 kernel=ParseKernelArray("3: 0,0,0 -,1,- 1,1,1");
1992 if (kernel == (KernelInfo *) NULL)
1993 return(kernel);
1994 kernel->type = type;
1995 new_kernel=ParseKernelArray("3: 0,0,0 0,1,1 1,1,-");
1996 if (new_kernel == (KernelInfo *) NULL)
1997 return(DestroyKernelInfo(kernel));
1998 new_kernel->type = type;
1999 LastKernelInfo(kernel)->next = new_kernel;
2000 new_kernel=ParseKernelArray("3: 0,0,0 1,1,0 -,1,1");
2001 if (new_kernel == (KernelInfo *) NULL)
2002 return(DestroyKernelInfo(kernel));
2003 new_kernel->type = type;
2004 LastKernelInfo(kernel)->next = new_kernel;
2005 ExpandMirrorKernelInfo(kernel);
anthony694934f2010-06-07 10:30:40 +00002006 /* Append a set of corner kernels */
2007 new_kernel=ParseKernelArray("3: 0,0,- 0,1,1 -,1,-");
2008 if (new_kernel == (KernelInfo *) NULL)
2009 return(DestroyKernelInfo(kernel));
2010 new_kernel->type = type;
2011 ExpandRotateKernelInfo(new_kernel, 90.0);
2012 LastKernelInfo(kernel)->next = new_kernel;
anthonyc40ac1e2010-06-06 11:49:31 +00002013 break;
2014 }
anthonya648a302010-05-27 02:14:36 +00002015 break;
2016 }
anthony602ab9b2010-01-05 08:06:50 +00002017 /* Distance Measuring Kernels */
2018 case ChebyshevKernel:
2019 {
anthony602ab9b2010-01-05 08:06:50 +00002020 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00002021 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00002022 else
cristybb503372010-05-27 20:51:26 +00002023 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
2024 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00002025
2026 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
2027 kernel->height*sizeof(double));
2028 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00002029 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00002030
cristybb503372010-05-27 20:51:26 +00002031 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
2032 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00002033 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00002034 args->sigma*((labs((long) u)>labs((long) v)) ? labs((long) u) : labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00002035 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00002036 break;
2037 }
anthonybee715c2010-06-04 01:25:57 +00002038 case ManhattanKernel:
anthony602ab9b2010-01-05 08:06:50 +00002039 {
anthony602ab9b2010-01-05 08:06:50 +00002040 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00002041 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00002042 else
cristybb503372010-05-27 20:51:26 +00002043 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
2044 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00002045
2046 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
2047 kernel->height*sizeof(double));
2048 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00002049 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00002050
cristybb503372010-05-27 20:51:26 +00002051 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
2052 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00002053 kernel->positive_range += ( kernel->values[i] =
cristyecd0ab52010-05-30 14:59:20 +00002054 args->sigma*(labs((long) u)+labs((long) v)) );
cristyc99304f2010-02-01 15:26:27 +00002055 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00002056 break;
2057 }
2058 case EuclideanKernel:
2059 {
anthony602ab9b2010-01-05 08:06:50 +00002060 if (args->rho < 1.0)
anthonyc94cdb02010-01-06 08:15:29 +00002061 kernel->width = kernel->height = 3; /* default radius = 1 */
anthony602ab9b2010-01-05 08:06:50 +00002062 else
cristybb503372010-05-27 20:51:26 +00002063 kernel->width = kernel->height = ((size_t)args->rho)*2+1;
2064 kernel->x = kernel->y = (ssize_t) (kernel->width-1)/2;
anthony602ab9b2010-01-05 08:06:50 +00002065
2066 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
2067 kernel->height*sizeof(double));
2068 if (kernel->values == (double *) NULL)
anthony83ba99b2010-01-24 08:48:15 +00002069 return(DestroyKernelInfo(kernel));
anthony602ab9b2010-01-05 08:06:50 +00002070
cristybb503372010-05-27 20:51:26 +00002071 for ( i=0, v=-kernel->y; v <= (ssize_t)kernel->y; v++)
2072 for ( u=-kernel->x; u <= (ssize_t)kernel->x; u++, i++)
cristyc99304f2010-02-01 15:26:27 +00002073 kernel->positive_range += ( kernel->values[i] =
anthonyc84dce52010-05-07 05:42:23 +00002074 args->sigma*sqrt((double)(u*u+v*v)) );
cristyc99304f2010-02-01 15:26:27 +00002075 kernel->maximum = kernel->values[0];
anthony602ab9b2010-01-05 08:06:50 +00002076 break;
2077 }
anthony46a369d2010-05-19 02:41:48 +00002078 case UnityKernel:
anthony602ab9b2010-01-05 08:06:50 +00002079 default:
anthonyc1061722010-05-14 06:23:49 +00002080 {
anthony3ca9ec12010-06-08 07:16:04 +00002081 /* Unity or No-Op Kernel - Basically just a single pixel on its own */
2082 kernel=ParseKernelArray("1:1");
anthonyc1061722010-05-14 06:23:49 +00002083 if (kernel == (KernelInfo *) NULL)
2084 return(kernel);
anthony46a369d2010-05-19 02:41:48 +00002085 kernel->type = ( type == UnityKernel ) ? UnityKernel : UndefinedKernel;
anthonyc1061722010-05-14 06:23:49 +00002086 break;
2087 }
anthony602ab9b2010-01-05 08:06:50 +00002088 break;
2089 }
2090
2091 return(kernel);
2092}
anthonyc94cdb02010-01-06 08:15:29 +00002093
anthony602ab9b2010-01-05 08:06:50 +00002094/*
2095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2096% %
2097% %
2098% %
cristy6771f1e2010-03-05 19:43:39 +00002099% C l o n e K e r n e l I n f o %
anthony4fd27e22010-02-07 08:17:18 +00002100% %
2101% %
2102% %
2103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2104%
anthony1b2bc0a2010-05-12 05:25:22 +00002105% CloneKernelInfo() creates a new clone of the given Kernel List so that its
2106% can be modified without effecting the original. The cloned kernel should
anthony19910ef2010-06-25 01:15:40 +00002107% be destroyed using DestoryKernelInfo() when no longer needed.
anthony7a01dcf2010-05-11 12:25:52 +00002108%
cristye6365592010-04-02 17:31:23 +00002109% The format of the CloneKernelInfo method is:
anthony4fd27e22010-02-07 08:17:18 +00002110%
anthony930be612010-02-08 04:26:15 +00002111% KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00002112%
2113% A description of each parameter follows:
2114%
2115% o kernel: the Morphology/Convolution kernel to be cloned
2116%
2117*/
cristyef656912010-03-05 19:54:59 +00002118MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
anthony4fd27e22010-02-07 08:17:18 +00002119{
cristybb503372010-05-27 20:51:26 +00002120 register ssize_t
anthony4fd27e22010-02-07 08:17:18 +00002121 i;
2122
cristy19eb6412010-04-23 14:42:29 +00002123 KernelInfo
anthony7a01dcf2010-05-11 12:25:52 +00002124 *new_kernel;
anthony4fd27e22010-02-07 08:17:18 +00002125
2126 assert(kernel != (KernelInfo *) NULL);
anthony7a01dcf2010-05-11 12:25:52 +00002127 new_kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
2128 if (new_kernel == (KernelInfo *) NULL)
2129 return(new_kernel);
2130 *new_kernel=(*kernel); /* copy values in structure */
anthony7a01dcf2010-05-11 12:25:52 +00002131
2132 /* replace the values with a copy of the values */
2133 new_kernel->values=(double *) AcquireQuantumMemory(kernel->width,
cristy19eb6412010-04-23 14:42:29 +00002134 kernel->height*sizeof(double));
anthony7a01dcf2010-05-11 12:25:52 +00002135 if (new_kernel->values == (double *) NULL)
2136 return(DestroyKernelInfo(new_kernel));
cristybb503372010-05-27 20:51:26 +00002137 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthony7a01dcf2010-05-11 12:25:52 +00002138 new_kernel->values[i]=kernel->values[i];
anthony1b2bc0a2010-05-12 05:25:22 +00002139
2140 /* Also clone the next kernel in the kernel list */
2141 if ( kernel->next != (KernelInfo *) NULL ) {
2142 new_kernel->next = CloneKernelInfo(kernel->next);
2143 if ( new_kernel->next == (KernelInfo *) NULL )
2144 return(DestroyKernelInfo(new_kernel));
2145 }
2146
anthony7a01dcf2010-05-11 12:25:52 +00002147 return(new_kernel);
anthony4fd27e22010-02-07 08:17:18 +00002148}
2149
2150/*
2151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2152% %
2153% %
2154% %
anthony83ba99b2010-01-24 08:48:15 +00002155% D e s t r o y K e r n e l I n f o %
anthony602ab9b2010-01-05 08:06:50 +00002156% %
2157% %
2158% %
2159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2160%
anthony83ba99b2010-01-24 08:48:15 +00002161% DestroyKernelInfo() frees the memory used by a Convolution/Morphology
2162% kernel.
anthony602ab9b2010-01-05 08:06:50 +00002163%
anthony83ba99b2010-01-24 08:48:15 +00002164% The format of the DestroyKernelInfo method is:
anthony602ab9b2010-01-05 08:06:50 +00002165%
anthony83ba99b2010-01-24 08:48:15 +00002166% KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00002167%
2168% A description of each parameter follows:
2169%
2170% o kernel: the Morphology/Convolution kernel to be destroyed
2171%
2172*/
anthony83ba99b2010-01-24 08:48:15 +00002173MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
anthony602ab9b2010-01-05 08:06:50 +00002174{
cristy2be15382010-01-21 02:38:03 +00002175 assert(kernel != (KernelInfo *) NULL);
anthony4fd27e22010-02-07 08:17:18 +00002176
anthony7a01dcf2010-05-11 12:25:52 +00002177 if ( kernel->next != (KernelInfo *) NULL )
2178 kernel->next = DestroyKernelInfo(kernel->next);
2179
2180 kernel->values = (double *)RelinquishMagickMemory(kernel->values);
2181 kernel = (KernelInfo *) RelinquishMagickMemory(kernel);
anthony602ab9b2010-01-05 08:06:50 +00002182 return(kernel);
2183}
anthonyc94cdb02010-01-06 08:15:29 +00002184
2185/*
2186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2187% %
2188% %
2189% %
anthonyce0fd952010-06-25 01:26:40 +00002190+ 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 +00002191% %
2192% %
2193% %
2194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2195%
anthonybfb635a2010-06-04 00:18:04 +00002196% ExpandMirrorKernelInfo() takes a single kernel, and expands it into a
2197% sequence of 90-degree rotated kernels but providing a reflected 180
2198% rotatation, before the -/+ 90-degree rotations.
2199%
2200% This special rotation order produces a better, more symetrical thinning of
2201% objects.
2202%
2203% The format of the ExpandMirrorKernelInfo method is:
2204%
2205% void ExpandMirrorKernelInfo(KernelInfo *kernel)
2206%
2207% A description of each parameter follows:
2208%
2209% o kernel: the Morphology/Convolution kernel
2210%
2211% This function is only internel to this module, as it is not finalized,
2212% especially with regard to non-orthogonal angles, and rotation of larger
2213% 2D kernels.
2214*/
2215
2216#if 0
2217static void FlopKernelInfo(KernelInfo *kernel)
2218 { /* Do a Flop by reversing each row. */
2219 size_t
2220 y;
2221 register ssize_t
2222 x,r;
2223 register double
2224 *k,t;
2225
2226 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
2227 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
2228 t=k[x], k[x]=k[r], k[r]=t;
2229
2230 kernel->x = kernel->width - kernel->x - 1;
2231 angle = fmod(angle+180.0, 360.0);
2232 }
2233#endif
2234
2235static void ExpandMirrorKernelInfo(KernelInfo *kernel)
2236{
2237 KernelInfo
2238 *clone,
2239 *last;
2240
2241 last = kernel;
2242
2243 clone = CloneKernelInfo(last);
2244 RotateKernelInfo(clone, 180); /* flip */
2245 LastKernelInfo(last)->next = clone;
2246 last = clone;
2247
2248 clone = CloneKernelInfo(last);
2249 RotateKernelInfo(clone, 90); /* transpose */
2250 LastKernelInfo(last)->next = clone;
2251 last = clone;
2252
2253 clone = CloneKernelInfo(last);
2254 RotateKernelInfo(clone, 180); /* flop */
2255 LastKernelInfo(last)->next = clone;
2256
2257 return;
2258}
2259
2260/*
2261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262% %
2263% %
2264% %
anthonyce0fd952010-06-25 01:26:40 +00002265+ E x p a n d R o t a t e K e r n e l I n f o %
anthonybfb635a2010-06-04 00:18:04 +00002266% %
2267% %
2268% %
2269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2270%
2271% ExpandRotateKernelInfo() takes a kernel list, and expands it by rotating
2272% incrementally by the angle given, until the first kernel repeats.
anthony3c10fc82010-05-13 02:40:51 +00002273%
2274% WARNING: 45 degree rotations only works for 3x3 kernels.
2275% While 90 degree roatations only works for linear and square kernels
2276%
anthonybfb635a2010-06-04 00:18:04 +00002277% The format of the ExpandRotateKernelInfo method is:
anthony3c10fc82010-05-13 02:40:51 +00002278%
anthonybfb635a2010-06-04 00:18:04 +00002279% void ExpandRotateKernelInfo(KernelInfo *kernel, double angle)
anthony3c10fc82010-05-13 02:40:51 +00002280%
2281% A description of each parameter follows:
2282%
2283% o kernel: the Morphology/Convolution kernel
2284%
2285% o angle: angle to rotate in degrees
2286%
2287% This function is only internel to this module, as it is not finalized,
2288% especially with regard to non-orthogonal angles, and rotation of larger
2289% 2D kernels.
2290*/
anthony47f5d062010-05-23 07:47:50 +00002291
2292/* Internal Routine - Return true if two kernels are the same */
2293static MagickBooleanType SameKernelInfo(const KernelInfo *kernel1,
2294 const KernelInfo *kernel2)
2295{
cristybb503372010-05-27 20:51:26 +00002296 register size_t
anthony47f5d062010-05-23 07:47:50 +00002297 i;
anthony1d45eb92010-05-25 11:13:23 +00002298
2299 /* check size and origin location */
2300 if ( kernel1->width != kernel2->width
2301 || kernel1->height != kernel2->height
2302 || kernel1->x != kernel2->x
2303 || kernel1->y != kernel2->y )
anthony47f5d062010-05-23 07:47:50 +00002304 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002305
2306 /* check actual kernel values */
anthony47f5d062010-05-23 07:47:50 +00002307 for (i=0; i < (kernel1->width*kernel1->height); i++) {
anthony1d45eb92010-05-25 11:13:23 +00002308 /* Test for Nan equivelence */
anthony47f5d062010-05-23 07:47:50 +00002309 if ( IsNan(kernel1->values[i]) && !IsNan(kernel2->values[i]) )
2310 return MagickFalse;
2311 if ( IsNan(kernel2->values[i]) && !IsNan(kernel1->values[i]) )
2312 return MagickFalse;
anthony1d45eb92010-05-25 11:13:23 +00002313 /* Test actual values are equivelent */
anthony47f5d062010-05-23 07:47:50 +00002314 if ( fabs(kernel1->values[i] - kernel2->values[i]) > MagickEpsilon )
2315 return MagickFalse;
2316 }
anthony1d45eb92010-05-25 11:13:23 +00002317
anthony47f5d062010-05-23 07:47:50 +00002318 return MagickTrue;
2319}
2320
anthonybfb635a2010-06-04 00:18:04 +00002321static void ExpandRotateKernelInfo(KernelInfo *kernel, const double angle)
anthony3c10fc82010-05-13 02:40:51 +00002322{
2323 KernelInfo
cristy84d9b552010-05-24 18:23:54 +00002324 *clone,
anthony3c10fc82010-05-13 02:40:51 +00002325 *last;
cristya9a61ad2010-05-13 12:47:41 +00002326
anthony3c10fc82010-05-13 02:40:51 +00002327 last = kernel;
anthony47f5d062010-05-23 07:47:50 +00002328 while(1) {
cristy84d9b552010-05-24 18:23:54 +00002329 clone = CloneKernelInfo(last);
2330 RotateKernelInfo(clone, angle);
2331 if ( SameKernelInfo(kernel, clone) == MagickTrue )
anthony47f5d062010-05-23 07:47:50 +00002332 break;
anthonybfb635a2010-06-04 00:18:04 +00002333 LastKernelInfo(last)->next = clone;
cristy84d9b552010-05-24 18:23:54 +00002334 last = clone;
anthony3c10fc82010-05-13 02:40:51 +00002335 }
anthonybfb635a2010-06-04 00:18:04 +00002336 clone = DestroyKernelInfo(clone); /* kernel has repeated - junk the clone */
anthony47f5d062010-05-23 07:47:50 +00002337 return;
anthony3c10fc82010-05-13 02:40:51 +00002338}
2339
2340/*
2341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2342% %
2343% %
2344% %
anthony46a369d2010-05-19 02:41:48 +00002345+ C a l c M e t a K e r n a l I n f o %
2346% %
2347% %
2348% %
2349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2350%
2351% CalcKernelMetaData() recalculate the KernelInfo meta-data of this kernel only,
2352% using the kernel values. This should only ne used if it is not posible to
2353% calculate that meta-data in some easier way.
2354%
2355% It is important that the meta-data is correct before ScaleKernelInfo() is
2356% used to perform kernel normalization.
2357%
2358% The format of the CalcKernelMetaData method is:
2359%
2360% void CalcKernelMetaData(KernelInfo *kernel, const double scale )
2361%
2362% A description of each parameter follows:
2363%
2364% o kernel: the Morphology/Convolution kernel to modify
2365%
2366% WARNING: Minimum and Maximum values are assumed to include zero, even if
2367% zero is not part of the kernel (as in Gaussian Derived kernels). This
2368% however is not true for flat-shaped morphological kernels.
2369%
2370% WARNING: Only the specific kernel pointed to is modified, not a list of
2371% multiple kernels.
2372%
2373% This is an internal function and not expected to be useful outside this
2374% module. This could change however.
2375*/
2376static void CalcKernelMetaData(KernelInfo *kernel)
2377{
cristybb503372010-05-27 20:51:26 +00002378 register size_t
anthony46a369d2010-05-19 02:41:48 +00002379 i;
2380
2381 kernel->minimum = kernel->maximum = 0.0;
2382 kernel->negative_range = kernel->positive_range = 0.0;
2383 for (i=0; i < (kernel->width*kernel->height); i++)
2384 {
2385 if ( fabs(kernel->values[i]) < MagickEpsilon )
2386 kernel->values[i] = 0.0;
2387 ( kernel->values[i] < 0)
2388 ? ( kernel->negative_range += kernel->values[i] )
2389 : ( kernel->positive_range += kernel->values[i] );
2390 Minimize(kernel->minimum, kernel->values[i]);
2391 Maximize(kernel->maximum, kernel->values[i]);
2392 }
2393
2394 return;
2395}
2396
2397/*
2398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2399% %
2400% %
2401% %
anthony9eb4f742010-05-18 02:45:54 +00002402% M o r p h o l o g y A p p l y %
anthony602ab9b2010-01-05 08:06:50 +00002403% %
2404% %
2405% %
2406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2407%
anthony9eb4f742010-05-18 02:45:54 +00002408% MorphologyApply() applies a morphological method, multiple times using
2409% a list of multiple kernels.
anthony602ab9b2010-01-05 08:06:50 +00002410%
anthony9eb4f742010-05-18 02:45:54 +00002411% It is basically equivelent to as MorphologyImageChannel() (see below) but
anthonye8d2f552010-06-05 10:43:25 +00002412% without any user controls. This allows internel programs to use this
2413% function, to actually perform a specific task without posible interference
2414% by any API user supplied settings.
2415%
2416% It is MorphologyImageChannel() task to extract any such user controls, and
2417% pass them to this function for processing.
anthony9eb4f742010-05-18 02:45:54 +00002418%
2419% More specifically kernels are not normalized/scaled/blended by the
anthonye8d2f552010-06-05 10:43:25 +00002420% 'convolve:scale' Image Artifact (setting), nor is the convolve bias
2421% (-bias setting or image->bias) loooked at, but must be supplied from the
2422% function arguments.
anthony602ab9b2010-01-05 08:06:50 +00002423%
anthony47f5d062010-05-23 07:47:50 +00002424% The format of the MorphologyApply method is:
anthony602ab9b2010-01-05 08:06:50 +00002425%
anthony9eb4f742010-05-18 02:45:54 +00002426% Image *MorphologyApply(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00002427% const ssize_t iterations,const KernelInfo *kernel,
anthony47f5d062010-05-23 07:47:50 +00002428% const CompositeMethod compose, const double bias,
anthony9eb4f742010-05-18 02:45:54 +00002429% ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002430%
2431% A description of each parameter follows:
2432%
anthony8d188502010-06-14 04:33:35 +00002433% o image: the source image
anthony602ab9b2010-01-05 08:06:50 +00002434%
2435% o method: the morphology method to be applied.
2436%
2437% o iterations: apply the operation this many times (or no change).
2438% A value of -1 means loop until no change found.
2439% How this is applied may depend on the morphology method.
2440% Typically this is a value of 1.
2441%
2442% o channel: the channel type.
2443%
2444% o kernel: An array of double representing the morphology kernel.
anthony602ab9b2010-01-05 08:06:50 +00002445%
anthony47f5d062010-05-23 07:47:50 +00002446% o compose: How to handle or merge multi-kernel results.
anthony8d188502010-06-14 04:33:35 +00002447% If 'UndefinedCompositeOp' use default for the Morphology method.
2448% If 'NoCompositeOp' force image to be re-iterated by each kernel.
2449% Otherwise merge the results using the compose method given.
anthony47f5d062010-05-23 07:47:50 +00002450%
2451% o bias: Convolution Output Bias.
anthony9eb4f742010-05-18 02:45:54 +00002452%
anthony602ab9b2010-01-05 08:06:50 +00002453% o exception: return any errors or warnings in this structure.
2454%
anthony602ab9b2010-01-05 08:06:50 +00002455*/
2456
anthony930be612010-02-08 04:26:15 +00002457
anthony9eb4f742010-05-18 02:45:54 +00002458/* Apply a Morphology Primative to an image using the given kernel.
2459** Two pre-created images must be provided, no image is created.
anthony8d188502010-06-14 04:33:35 +00002460** It returns the number of pixels that changed betwene the images
2461** for convergence determination.
anthony9eb4f742010-05-18 02:45:54 +00002462*/
cristybb503372010-05-27 20:51:26 +00002463static size_t MorphologyPrimitive(const Image *image, Image
anthony602ab9b2010-01-05 08:06:50 +00002464 *result_image, const MorphologyMethod method, const ChannelType channel,
anthony9eb4f742010-05-18 02:45:54 +00002465 const KernelInfo *kernel,const double bias,ExceptionInfo *exception)
anthony602ab9b2010-01-05 08:06:50 +00002466{
cristy2be15382010-01-21 02:38:03 +00002467#define MorphologyTag "Morphology/Image"
anthony602ab9b2010-01-05 08:06:50 +00002468
cristy5f959472010-05-27 22:19:46 +00002469 CacheView
2470 *p_view,
2471 *q_view;
2472
cristybb503372010-05-27 20:51:26 +00002473 ssize_t
anthony29188a82010-01-22 10:12:34 +00002474 y, offx, offy,
anthony602ab9b2010-01-05 08:06:50 +00002475 changed;
2476
2477 MagickBooleanType
2478 status;
2479
cristy5f959472010-05-27 22:19:46 +00002480 MagickOffsetType
2481 progress;
anthony602ab9b2010-01-05 08:06:50 +00002482
anthonye4d89962010-05-29 10:53:11 +00002483 assert(image != (Image *) NULL);
2484 assert(image->signature == MagickSignature);
2485 assert(result_image != (Image *) NULL);
2486 assert(result_image->signature == MagickSignature);
2487 assert(kernel != (KernelInfo *) NULL);
2488 assert(kernel->signature == MagickSignature);
2489 assert(exception != (ExceptionInfo *) NULL);
2490 assert(exception->signature == MagickSignature);
2491
anthony602ab9b2010-01-05 08:06:50 +00002492 status=MagickTrue;
2493 changed=0;
2494 progress=0;
2495
anthony602ab9b2010-01-05 08:06:50 +00002496 p_view=AcquireCacheView(image);
2497 q_view=AcquireCacheView(result_image);
anthony29188a82010-01-22 10:12:34 +00002498
anthonycc6c8362010-01-25 04:14:01 +00002499 /* Some methods (including convolve) needs use a reflected kernel.
anthony9eb4f742010-05-18 02:45:54 +00002500 * Adjust 'origin' offsets to loop though kernel as a reflection.
anthony29188a82010-01-22 10:12:34 +00002501 */
cristyc99304f2010-02-01 15:26:27 +00002502 offx = kernel->x;
2503 offy = kernel->y;
anthony29188a82010-01-22 10:12:34 +00002504 switch(method) {
anthony930be612010-02-08 04:26:15 +00002505 case ConvolveMorphology:
2506 case DilateMorphology:
2507 case DilateIntensityMorphology:
2508 case DistanceMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002509 /* kernel needs to used with reflection about origin */
cristybb503372010-05-27 20:51:26 +00002510 offx = (ssize_t) kernel->width-offx-1;
2511 offy = (ssize_t) kernel->height-offy-1;
anthony29188a82010-01-22 10:12:34 +00002512 break;
anthony5ef8e942010-05-11 06:51:12 +00002513 case ErodeMorphology:
2514 case ErodeIntensityMorphology:
2515 case HitAndMissMorphology:
2516 case ThinningMorphology:
2517 case ThickenMorphology:
anthony3ca9ec12010-06-08 07:16:04 +00002518 /* kernel is used as is, without reflection */
anthony5ef8e942010-05-11 06:51:12 +00002519 break;
anthony930be612010-02-08 04:26:15 +00002520 default:
anthony9eb4f742010-05-18 02:45:54 +00002521 assert("Not a Primitive Morphology Method" != (char *) NULL);
anthony930be612010-02-08 04:26:15 +00002522 break;
anthony29188a82010-01-22 10:12:34 +00002523 }
2524
anthony8d188502010-06-14 04:33:35 +00002525
2526 if ( method == ConvolveMorphology && kernel->width == 1 )
2527 { /* Special handling (for speed) of vertical (blur) kernels.
2528 ** This performs its handling in columns rather than in rows.
2529 ** This is only done fo convolve as it is the only method that
2530 ** generates very large 1-D vertical kernels (such as a 'BlurKernel')
2531 **
2532 ** Timing tests (on single CPU laptop)
2533 ** Using a vertical 1-d Blue with normal row-by-row (below)
2534 ** time convert logo: -morphology Convolve Blur:0x10+90 null:
2535 ** 0.807u
2536 ** Using this column method
2537 ** time convert logo: -morphology Convolve Blur:0x10+90 null:
2538 ** 0.620u
2539 **
2540 ** Anthony Thyssen, 14 June 2010
2541 */
cristyfd117592010-06-14 23:40:02 +00002542 register ssize_t
2543 x;
2544
anthony8d188502010-06-14 04:33:35 +00002545#if defined(MAGICKCORE_OPENMP_SUPPORT)
2546#pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2547#endif
anthony8d188502010-06-14 04:33:35 +00002548 for (x=0; x < (ssize_t) image->columns; x++)
2549 {
2550 register const PixelPacket
2551 *restrict p;
2552
2553 register const IndexPacket
2554 *restrict p_indexes;
2555
2556 register PixelPacket
2557 *restrict q;
2558
2559 register IndexPacket
2560 *restrict q_indexes;
2561
2562 register ssize_t
2563 y;
2564
2565 size_t
2566 r;
2567
2568 if (status == MagickFalse)
2569 continue;
2570 p=GetCacheViewVirtualPixels(p_view, x, -offy,1,
2571 image->rows+kernel->height, exception);
2572 q=GetCacheViewAuthenticPixels(q_view,x,0,1,result_image->rows,exception);
2573 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2574 {
2575 status=MagickFalse;
2576 continue;
2577 }
2578 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2579 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
2580 r = offy; /* offset to the origin pixel in 'p' */
2581
2582 for (y=0; y < (ssize_t) image->rows; y++)
2583 {
2584 register ssize_t
2585 v;
2586
2587 register const double
2588 *restrict k;
2589
2590 register const PixelPacket
2591 *restrict k_pixels;
2592
2593 register const IndexPacket
2594 *restrict k_indexes;
2595
2596 MagickPixelPacket
2597 result;
2598
2599 /* Copy input image to the output image for unused channels
2600 * This removes need for 'cloning' a new image every iteration
2601 */
2602 *q = p[r];
2603 if (image->colorspace == CMYKColorspace)
anthony85757172010-06-14 22:28:04 +00002604 q_indexes[y] = p_indexes[r];
anthony8d188502010-06-14 04:33:35 +00002605
2606 /* Set the bias of the weighted average output */
2607 result.red =
2608 result.green =
2609 result.blue =
2610 result.opacity =
2611 result.index = bias;
2612
2613
2614 /* Weighted Average of pixels using reflected kernel
2615 **
2616 ** NOTE for correct working of this operation for asymetrical
2617 ** kernels, the kernel needs to be applied in its reflected form.
2618 ** That is its values needs to be reversed.
2619 */
2620 k = &kernel->values[ kernel->height-1 ];
2621 k_pixels = p;
2622 k_indexes = p_indexes;
2623 if ( ((channel & SyncChannels) == 0 ) ||
2624 (image->matte == MagickFalse) )
2625 { /* No 'Sync' involved.
2626 ** Convolution is simple greyscale channel operation
2627 */
2628 for (v=0; v < (ssize_t) kernel->height; v++) {
2629 if ( IsNan(*k) ) continue;
2630 result.red += (*k)*k_pixels->red;
2631 result.green += (*k)*k_pixels->green;
2632 result.blue += (*k)*k_pixels->blue;
2633 result.opacity += (*k)*k_pixels->opacity;
2634 if ( image->colorspace == CMYKColorspace)
2635 result.index += (*k)*(*k_indexes);
2636 k--;
2637 k_pixels++;
2638 k_indexes++;
2639 }
2640 if ((channel & RedChannel) != 0)
2641 q->red = ClampToQuantum(result.red);
2642 if ((channel & GreenChannel) != 0)
2643 q->green = ClampToQuantum(result.green);
2644 if ((channel & BlueChannel) != 0)
2645 q->blue = ClampToQuantum(result.blue);
2646 if ((channel & OpacityChannel) != 0
2647 && image->matte == MagickTrue )
2648 q->opacity = ClampToQuantum(result.opacity);
2649 if ((channel & IndexChannel) != 0
2650 && image->colorspace == CMYKColorspace)
2651 q_indexes[x] = ClampToQuantum(result.index);
2652 }
2653 else
2654 { /* Channel 'Sync' Flag, and Alpha Channel enabled.
2655 ** Weight the color channels with Alpha Channel so that
2656 ** transparent pixels are not part of the results.
2657 */
2658 MagickRealType
2659 alpha, /* alpha weighting of colors : kernel*alpha */
2660 gamma; /* divisor, sum of color weighting values */
2661
2662 gamma=0.0;
2663 for (v=0; v < (ssize_t) kernel->height; v++) {
2664 if ( IsNan(*k) ) continue;
2665 alpha=(*k)*(QuantumScale*(QuantumRange-k_pixels->opacity));
2666 gamma += alpha;
2667 result.red += alpha*k_pixels->red;
2668 result.green += alpha*k_pixels->green;
2669 result.blue += alpha*k_pixels->blue;
2670 result.opacity += (*k)*k_pixels->opacity;
2671 if ( image->colorspace == CMYKColorspace)
2672 result.index += alpha*(*k_indexes);
2673 k--;
2674 k_pixels++;
2675 k_indexes++;
2676 }
2677 /* Sync'ed channels, all channels are modified */
2678 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2679 q->red = ClampToQuantum(gamma*result.red);
2680 q->green = ClampToQuantum(gamma*result.green);
2681 q->blue = ClampToQuantum(gamma*result.blue);
2682 q->opacity = ClampToQuantum(result.opacity);
2683 if (image->colorspace == CMYKColorspace)
2684 q_indexes[x] = ClampToQuantum(gamma*result.index);
2685 }
2686
2687 /* Count up changed pixels */
2688 if ( ( p[r].red != q->red )
2689 || ( p[r].green != q->green )
2690 || ( p[r].blue != q->blue )
2691 || ( p[r].opacity != q->opacity )
2692 || ( image->colorspace == CMYKColorspace &&
2693 p_indexes[r] != q_indexes[x] ) )
2694 changed++; /* The pixel was changed in some way! */
2695 p++;
2696 q++;
2697 } /* y */
2698 if ( SyncCacheViewAuthenticPixels(q_view,exception) == MagickFalse)
2699 status=MagickFalse;
2700 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2701 {
2702 MagickBooleanType
2703 proceed;
2704
2705#if defined(MAGICKCORE_OPENMP_SUPPORT)
2706 #pragma omp critical (MagickCore_MorphologyImage)
2707#endif
2708 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
2709 if (proceed == MagickFalse)
2710 status=MagickFalse;
2711 }
2712 } /* x */
2713 result_image->type=image->type;
2714 q_view=DestroyCacheView(q_view);
2715 p_view=DestroyCacheView(p_view);
2716 return(status ? (size_t) changed : 0);
2717 }
2718
2719 /*
2720 ** Normal handling of horizontal or rectangular kernels (row by row)
2721 */
anthony602ab9b2010-01-05 08:06:50 +00002722#if defined(MAGICKCORE_OPENMP_SUPPORT)
2723 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2724#endif
cristybb503372010-05-27 20:51:26 +00002725 for (y=0; y < (ssize_t) image->rows; y++)
anthony602ab9b2010-01-05 08:06:50 +00002726 {
anthony602ab9b2010-01-05 08:06:50 +00002727 register const PixelPacket
2728 *restrict p;
2729
2730 register const IndexPacket
2731 *restrict p_indexes;
2732
2733 register PixelPacket
2734 *restrict q;
2735
2736 register IndexPacket
2737 *restrict q_indexes;
2738
cristybb503372010-05-27 20:51:26 +00002739 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002740 x;
2741
cristybb503372010-05-27 20:51:26 +00002742 size_t
anthony602ab9b2010-01-05 08:06:50 +00002743 r;
2744
2745 if (status == MagickFalse)
2746 continue;
anthony29188a82010-01-22 10:12:34 +00002747 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
2748 image->columns+kernel->width, kernel->height, exception);
anthony602ab9b2010-01-05 08:06:50 +00002749 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
2750 exception);
2751 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2752 {
2753 status=MagickFalse;
2754 continue;
2755 }
2756 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
2757 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
anthony8d188502010-06-14 04:33:35 +00002758 r = (image->columns+kernel->width)*offy+offx; /* offset to origin in 'p' */
anthony29188a82010-01-22 10:12:34 +00002759
cristybb503372010-05-27 20:51:26 +00002760 for (x=0; x < (ssize_t) image->columns; x++)
anthony602ab9b2010-01-05 08:06:50 +00002761 {
cristybb503372010-05-27 20:51:26 +00002762 ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002763 v;
2764
cristybb503372010-05-27 20:51:26 +00002765 register ssize_t
anthony602ab9b2010-01-05 08:06:50 +00002766 u;
2767
2768 register const double
2769 *restrict k;
2770
2771 register const PixelPacket
2772 *restrict k_pixels;
2773
2774 register const IndexPacket
2775 *restrict k_indexes;
2776
2777 MagickPixelPacket
anthony5ef8e942010-05-11 06:51:12 +00002778 result,
2779 min,
2780 max;
anthony602ab9b2010-01-05 08:06:50 +00002781
anthonyc406ea42010-06-12 01:01:49 +00002782 /* Copy input image to the output image for unused channels
anthony83ba99b2010-01-24 08:48:15 +00002783 * This removes need for 'cloning' a new image every iteration
anthony29188a82010-01-22 10:12:34 +00002784 */
anthony602ab9b2010-01-05 08:06:50 +00002785 *q = p[r];
2786 if (image->colorspace == CMYKColorspace)
2787 q_indexes[x] = p_indexes[r];
2788
anthony5ef8e942010-05-11 06:51:12 +00002789 /* Defaults */
2790 min.red =
2791 min.green =
2792 min.blue =
2793 min.opacity =
2794 min.index = (MagickRealType) QuantumRange;
2795 max.red =
2796 max.green =
2797 max.blue =
2798 max.opacity =
2799 max.index = (MagickRealType) 0;
anthony9eb4f742010-05-18 02:45:54 +00002800 /* default result is the original pixel value */
anthony5ef8e942010-05-11 06:51:12 +00002801 result.red = (MagickRealType) p[r].red;
2802 result.green = (MagickRealType) p[r].green;
2803 result.blue = (MagickRealType) p[r].blue;
2804 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
cristye96405a2010-05-19 02:24:31 +00002805 result.index = 0.0;
anthony5ef8e942010-05-11 06:51:12 +00002806 if ( image->colorspace == CMYKColorspace)
2807 result.index = (MagickRealType) p_indexes[r];
2808
anthony602ab9b2010-01-05 08:06:50 +00002809 switch (method) {
2810 case ConvolveMorphology:
anthony8d188502010-06-14 04:33:35 +00002811 /* Set the bias of the weighted average output */
anthony9eb4f742010-05-18 02:45:54 +00002812 result.red =
2813 result.green =
2814 result.blue =
2815 result.opacity =
2816 result.index = bias;
anthony930be612010-02-08 04:26:15 +00002817 break;
anthony4fd27e22010-02-07 08:17:18 +00002818 case DilateIntensityMorphology:
2819 case ErodeIntensityMorphology:
anthony9eb4f742010-05-18 02:45:54 +00002820 /* use a boolean flag indicating when first match found */
2821 result.red = 0.0; /* result is not used otherwise */
anthony4fd27e22010-02-07 08:17:18 +00002822 break;
anthony602ab9b2010-01-05 08:06:50 +00002823 default:
anthony602ab9b2010-01-05 08:06:50 +00002824 break;
2825 }
2826
2827 switch ( method ) {
2828 case ConvolveMorphology:
anthony930be612010-02-08 04:26:15 +00002829 /* Weighted Average of pixels using reflected kernel
2830 **
2831 ** NOTE for correct working of this operation for asymetrical
2832 ** kernels, the kernel needs to be applied in its reflected form.
2833 ** That is its values needs to be reversed.
2834 **
2835 ** Correlation is actually the same as this but without reflecting
2836 ** the kernel, and thus 'lower-level' that Convolution. However
2837 ** as Convolution is the more common method used, and it does not
2838 ** really cost us much in terms of processing to use a reflected
anthony5ef8e942010-05-11 06:51:12 +00002839 ** kernel, so it is Convolution that is implemented.
anthony930be612010-02-08 04:26:15 +00002840 **
2841 ** Correlation will have its kernel reflected before calling
2842 ** this function to do a Convolve.
2843 **
2844 ** For more details of Correlation vs Convolution see
2845 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
2846 */
anthony8d188502010-06-14 04:33:35 +00002847 k = &kernel->values[ kernel->width*kernel->height-1 ];
2848 k_pixels = p;
2849 k_indexes = p_indexes;
2850 if ( ((channel & SyncChannels) == 0 ) ||
2851 (image->matte == MagickFalse) )
anthonyc406ea42010-06-12 01:01:49 +00002852 { /* No 'Sync' involved.
2853 ** Convolution is simple greyscale channel operation
anthony5ef8e942010-05-11 06:51:12 +00002854 */
cristybb503372010-05-27 20:51:26 +00002855 for (v=0; v < (ssize_t) kernel->height; v++) {
2856 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony5ef8e942010-05-11 06:51:12 +00002857 if ( IsNan(*k) ) continue;
2858 result.red += (*k)*k_pixels[u].red;
2859 result.green += (*k)*k_pixels[u].green;
2860 result.blue += (*k)*k_pixels[u].blue;
anthonyc406ea42010-06-12 01:01:49 +00002861 result.opacity += (*k)*k_pixels[u].opacity;
anthony5ef8e942010-05-11 06:51:12 +00002862 if ( image->colorspace == CMYKColorspace)
2863 result.index += (*k)*k_indexes[u];
2864 }
2865 k_pixels += image->columns+kernel->width;
2866 k_indexes += image->columns+kernel->width;
2867 }
anthonyd7f02562010-06-12 02:20:07 +00002868 if ((channel & RedChannel) != 0)
anthonyc406ea42010-06-12 01:01:49 +00002869 q->red = ClampToQuantum(result.red);
anthonyd7f02562010-06-12 02:20:07 +00002870 if ((channel & GreenChannel) != 0)
anthonyc406ea42010-06-12 01:01:49 +00002871 q->green = ClampToQuantum(result.green);
anthonyd7f02562010-06-12 02:20:07 +00002872 if ((channel & BlueChannel) != 0)
anthonyc406ea42010-06-12 01:01:49 +00002873 q->blue = ClampToQuantum(result.blue);
anthonyd7f02562010-06-12 02:20:07 +00002874 if ((channel & OpacityChannel) != 0
anthonyc406ea42010-06-12 01:01:49 +00002875 && image->matte == MagickTrue )
2876 q->opacity = ClampToQuantum(result.opacity);
anthonyd7f02562010-06-12 02:20:07 +00002877 if ((channel & IndexChannel) != 0
anthonyc406ea42010-06-12 01:01:49 +00002878 && image->colorspace == CMYKColorspace)
2879 q_indexes[x] = ClampToQuantum(result.index);
anthony5ef8e942010-05-11 06:51:12 +00002880 }
anthony8d188502010-06-14 04:33:35 +00002881 else
2882 { /* Channel 'Sync' Flag, and Alpha Channel enabled.
2883 ** Weight the color channels with Alpha Channel so that
2884 ** transparent pixels are not part of the results.
2885 */
2886 MagickRealType
2887 alpha, /* alpha weighting of colors : kernel*alpha */
2888 gamma; /* divisor, sum of color weighting values */
2889
2890 gamma=0.0;
2891 for (v=0; v < (ssize_t) kernel->height; v++) {
2892 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
2893 if ( IsNan(*k) ) continue;
2894 alpha=(*k)*(QuantumScale*(QuantumRange-
2895 k_pixels[u].opacity));
2896 gamma += alpha;
2897 result.red += alpha*k_pixels[u].red;
2898 result.green += alpha*k_pixels[u].green;
2899 result.blue += alpha*k_pixels[u].blue;
2900 result.opacity += (*k)*k_pixels[u].opacity;
2901 if ( image->colorspace == CMYKColorspace)
2902 result.index += alpha*k_indexes[u];
2903 }
2904 k_pixels += image->columns+kernel->width;
2905 k_indexes += image->columns+kernel->width;
2906 }
2907 /* Sync'ed channels, all channels are modified */
2908 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2909 q->red = ClampToQuantum(gamma*result.red);
2910 q->green = ClampToQuantum(gamma*result.green);
2911 q->blue = ClampToQuantum(gamma*result.blue);
2912 q->opacity = ClampToQuantum(result.opacity);
2913 if (image->colorspace == CMYKColorspace)
2914 q_indexes[x] = ClampToQuantum(gamma*result.index);
2915 }
anthony602ab9b2010-01-05 08:06:50 +00002916 break;
2917
anthony4fd27e22010-02-07 08:17:18 +00002918 case ErodeMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002919 /* Minimum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002920 **
2921 ** NOTE that the kernel is not reflected for this operation!
2922 **
2923 ** NOTE: in normal Greyscale Morphology, the kernel value should
2924 ** be added to the real value, this is currently not done, due to
2925 ** the nature of the boolean kernels being used.
2926 */
anthony4fd27e22010-02-07 08:17:18 +00002927 k = kernel->values;
2928 k_pixels = p;
2929 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002930 for (v=0; v < (ssize_t) kernel->height; v++) {
2931 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony4fd27e22010-02-07 08:17:18 +00002932 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002933 Minimize(min.red, (double) k_pixels[u].red);
2934 Minimize(min.green, (double) k_pixels[u].green);
2935 Minimize(min.blue, (double) k_pixels[u].blue);
2936 Minimize(min.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002937 QuantumRange-(double) k_pixels[u].opacity);
anthony4fd27e22010-02-07 08:17:18 +00002938 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002939 Minimize(min.index, (double) k_indexes[u]);
anthony4fd27e22010-02-07 08:17:18 +00002940 }
2941 k_pixels += image->columns+kernel->width;
2942 k_indexes += image->columns+kernel->width;
2943 }
2944 break;
2945
anthony83ba99b2010-01-24 08:48:15 +00002946 case DilateMorphology:
anthony5ef8e942010-05-11 06:51:12 +00002947 /* Maximum Value within kernel neighbourhood
anthony930be612010-02-08 04:26:15 +00002948 **
2949 ** NOTE for correct working of this operation for asymetrical
2950 ** kernels, the kernel needs to be applied in its reflected form.
2951 ** That is its values needs to be reversed.
2952 **
2953 ** NOTE: in normal Greyscale Morphology, the kernel value should
2954 ** be added to the real value, this is currently not done, due to
2955 ** the nature of the boolean kernels being used.
2956 **
2957 */
anthony29188a82010-01-22 10:12:34 +00002958 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00002959 k_pixels = p;
2960 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002961 for (v=0; v < (ssize_t) kernel->height; v++) {
2962 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00002963 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony5ef8e942010-05-11 06:51:12 +00002964 Maximize(max.red, (double) k_pixels[u].red);
2965 Maximize(max.green, (double) k_pixels[u].green);
2966 Maximize(max.blue, (double) k_pixels[u].blue);
2967 Maximize(max.opacity,
anthonyd37a5cb2010-05-07 06:37:03 +00002968 QuantumRange-(double) k_pixels[u].opacity);
anthony602ab9b2010-01-05 08:06:50 +00002969 if ( image->colorspace == CMYKColorspace)
anthony5ef8e942010-05-11 06:51:12 +00002970 Maximize(max.index, (double) k_indexes[u]);
anthony602ab9b2010-01-05 08:06:50 +00002971 }
2972 k_pixels += image->columns+kernel->width;
2973 k_indexes += image->columns+kernel->width;
2974 }
anthony602ab9b2010-01-05 08:06:50 +00002975 break;
2976
anthony5ef8e942010-05-11 06:51:12 +00002977 case HitAndMissMorphology:
2978 case ThinningMorphology:
2979 case ThickenMorphology:
2980 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
2981 **
2982 ** NOTE that the kernel is not reflected for this operation,
2983 ** and consists of both foreground and background pixel
2984 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
2985 ** with either Nan or 0.5 values for don't care.
2986 **
anthony4c827ef2010-06-05 23:56:10 +00002987 ** Note that this will never produce a meaningless negative
2988 ** result. Such results can cause Thinning/Thicken to not work
2989 ** correctly when used against a greyscale image.
anthony5ef8e942010-05-11 06:51:12 +00002990 */
2991 k = kernel->values;
2992 k_pixels = p;
2993 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00002994 for (v=0; v < (ssize_t) kernel->height; v++) {
2995 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony5ef8e942010-05-11 06:51:12 +00002996 if ( IsNan(*k) ) continue;
2997 if ( (*k) > 0.7 )
2998 { /* minimim of foreground pixels */
2999 Minimize(min.red, (double) k_pixels[u].red);
3000 Minimize(min.green, (double) k_pixels[u].green);
3001 Minimize(min.blue, (double) k_pixels[u].blue);
3002 Minimize(min.opacity,
3003 QuantumRange-(double) k_pixels[u].opacity);
3004 if ( image->colorspace == CMYKColorspace)
3005 Minimize(min.index, (double) k_indexes[u]);
3006 }
3007 else if ( (*k) < 0.3 )
3008 { /* maximum of background pixels */
3009 Maximize(max.red, (double) k_pixels[u].red);
3010 Maximize(max.green, (double) k_pixels[u].green);
3011 Maximize(max.blue, (double) k_pixels[u].blue);
3012 Maximize(max.opacity,
3013 QuantumRange-(double) k_pixels[u].opacity);
3014 if ( image->colorspace == CMYKColorspace)
3015 Maximize(max.index, (double) k_indexes[u]);
3016 }
3017 }
3018 k_pixels += image->columns+kernel->width;
3019 k_indexes += image->columns+kernel->width;
3020 }
anthony4c827ef2010-06-05 23:56:10 +00003021 /* Pattern Match if difference is positive */
anthony5ef8e942010-05-11 06:51:12 +00003022 min.red -= max.red; Maximize( min.red, 0.0 );
3023 min.green -= max.green; Maximize( min.green, 0.0 );
3024 min.blue -= max.blue; Maximize( min.blue, 0.0 );
3025 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
3026 min.index -= max.index; Maximize( min.index, 0.0 );
3027 break;
3028
anthony4fd27e22010-02-07 08:17:18 +00003029 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00003030 /* Select Pixel with Minimum Intensity within kernel neighbourhood
3031 **
3032 ** WARNING: the intensity test fails for CMYK and does not
anthonyc406ea42010-06-12 01:01:49 +00003033 ** take into account the moderating effect of the alpha channel
anthony930be612010-02-08 04:26:15 +00003034 ** on the intensity.
3035 **
3036 ** NOTE that the kernel is not reflected for this operation!
3037 */
anthony602ab9b2010-01-05 08:06:50 +00003038 k = kernel->values;
3039 k_pixels = p;
3040 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00003041 for (v=0; v < (ssize_t) kernel->height; v++) {
3042 for (u=0; u < (ssize_t) kernel->width; u++, k++) {
anthony602ab9b2010-01-05 08:06:50 +00003043 if ( IsNan(*k) || (*k) < 0.5 ) continue;
anthony4fd27e22010-02-07 08:17:18 +00003044 if ( result.red == 0.0 ||
3045 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
3046 /* copy the whole pixel - no channel selection */
3047 *q = k_pixels[u];
3048 if ( result.red > 0.0 ) changed++;
3049 result.red = 1.0;
3050 }
anthony602ab9b2010-01-05 08:06:50 +00003051 }
3052 k_pixels += image->columns+kernel->width;
3053 k_indexes += image->columns+kernel->width;
3054 }
anthony602ab9b2010-01-05 08:06:50 +00003055 break;
3056
anthony83ba99b2010-01-24 08:48:15 +00003057 case DilateIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00003058 /* Select Pixel with Maximum Intensity within kernel neighbourhood
3059 **
3060 ** WARNING: the intensity test fails for CMYK and does not
anthony9eb4f742010-05-18 02:45:54 +00003061 ** take into account the moderating effect of the alpha channel
3062 ** on the intensity (yet).
anthony930be612010-02-08 04:26:15 +00003063 **
3064 ** NOTE for correct working of this operation for asymetrical
3065 ** kernels, the kernel needs to be applied in its reflected form.
3066 ** That is its values needs to be reversed.
3067 */
anthony29188a82010-01-22 10:12:34 +00003068 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00003069 k_pixels = p;
3070 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00003071 for (v=0; v < (ssize_t) kernel->height; v++) {
3072 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony29188a82010-01-22 10:12:34 +00003073 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
3074 if ( result.red == 0.0 ||
3075 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
3076 /* copy the whole pixel - no channel selection */
3077 *q = k_pixels[u];
3078 if ( result.red > 0.0 ) changed++;
3079 result.red = 1.0;
3080 }
anthony602ab9b2010-01-05 08:06:50 +00003081 }
3082 k_pixels += image->columns+kernel->width;
3083 k_indexes += image->columns+kernel->width;
3084 }
anthony602ab9b2010-01-05 08:06:50 +00003085 break;
3086
anthony5ef8e942010-05-11 06:51:12 +00003087
anthony602ab9b2010-01-05 08:06:50 +00003088 case DistanceMorphology:
anthony930be612010-02-08 04:26:15 +00003089 /* Add kernel Value and select the minimum value found.
3090 ** The result is a iterative distance from edge of image shape.
3091 **
3092 ** All Distance Kernels are symetrical, but that may not always
3093 ** be the case. For example how about a distance from left edges?
3094 ** To work correctly with asymetrical kernels the reflected kernel
3095 ** needs to be applied.
anthony5ef8e942010-05-11 06:51:12 +00003096 **
3097 ** Actually this is really a GreyErode with a negative kernel!
3098 **
anthony930be612010-02-08 04:26:15 +00003099 */
anthony29188a82010-01-22 10:12:34 +00003100 k = &kernel->values[ kernel->width*kernel->height-1 ];
anthony602ab9b2010-01-05 08:06:50 +00003101 k_pixels = p;
3102 k_indexes = p_indexes;
cristybb503372010-05-27 20:51:26 +00003103 for (v=0; v < (ssize_t) kernel->height; v++) {
3104 for (u=0; u < (ssize_t) kernel->width; u++, k--) {
anthony602ab9b2010-01-05 08:06:50 +00003105 if ( IsNan(*k) ) continue;
3106 Minimize(result.red, (*k)+k_pixels[u].red);
3107 Minimize(result.green, (*k)+k_pixels[u].green);
3108 Minimize(result.blue, (*k)+k_pixels[u].blue);
3109 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
3110 if ( image->colorspace == CMYKColorspace)
3111 Minimize(result.index, (*k)+k_indexes[u]);
3112 }
3113 k_pixels += image->columns+kernel->width;
3114 k_indexes += image->columns+kernel->width;
3115 }
anthony602ab9b2010-01-05 08:06:50 +00003116 break;
3117
3118 case UndefinedMorphology:
3119 default:
3120 break; /* Do nothing */
anthony83ba99b2010-01-24 08:48:15 +00003121 }
anthony5ef8e942010-05-11 06:51:12 +00003122 /* Final mathematics of results (combine with original image?)
3123 **
3124 ** NOTE: Difference Morphology operators Edge* and *Hat could also
3125 ** be done here but works better with iteration as a image difference
3126 ** in the controling function (below). Thicken and Thinning however
3127 ** should be done here so thay can be iterated correctly.
3128 */
3129 switch ( method ) {
3130 case HitAndMissMorphology:
3131 case ErodeMorphology:
3132 result = min; /* minimum of neighbourhood */
3133 break;
3134 case DilateMorphology:
3135 result = max; /* maximum of neighbourhood */
3136 break;
3137 case ThinningMorphology:
3138 /* subtract pattern match from original */
3139 result.red -= min.red;
3140 result.green -= min.green;
3141 result.blue -= min.blue;
3142 result.opacity -= min.opacity;
3143 result.index -= min.index;
3144 break;
3145 case ThickenMorphology:
anthony4c827ef2010-06-05 23:56:10 +00003146 /* Add the pattern matchs to the original */
3147 result.red += min.red;
3148 result.green += min.green;
3149 result.blue += min.blue;
3150 result.opacity += min.opacity;
3151 result.index += min.index;
anthony5ef8e942010-05-11 06:51:12 +00003152 break;
3153 default:
3154 /* result directly calculated or assigned */
3155 break;
3156 }
3157 /* Assign the resulting pixel values - Clamping Result */
anthony83ba99b2010-01-24 08:48:15 +00003158 switch ( method ) {
3159 case UndefinedMorphology:
anthonyc406ea42010-06-12 01:01:49 +00003160 case ConvolveMorphology:
anthony83ba99b2010-01-24 08:48:15 +00003161 case DilateIntensityMorphology:
3162 case ErodeIntensityMorphology:
anthony930be612010-02-08 04:26:15 +00003163 break; /* full pixel was directly assigned - not a channel method */
anthony83ba99b2010-01-24 08:48:15 +00003164 default:
anthony83ba99b2010-01-24 08:48:15 +00003165 if ((channel & RedChannel) != 0)
3166 q->red = ClampToQuantum(result.red);
3167 if ((channel & GreenChannel) != 0)
3168 q->green = ClampToQuantum(result.green);
3169 if ((channel & BlueChannel) != 0)
3170 q->blue = ClampToQuantum(result.blue);
3171 if ((channel & OpacityChannel) != 0
3172 && image->matte == MagickTrue )
3173 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
3174 if ((channel & IndexChannel) != 0
3175 && image->colorspace == CMYKColorspace)
3176 q_indexes[x] = ClampToQuantum(result.index);
3177 break;
3178 }
anthony5ef8e942010-05-11 06:51:12 +00003179 /* Count up changed pixels */
anthony83ba99b2010-01-24 08:48:15 +00003180 if ( ( p[r].red != q->red )
3181 || ( p[r].green != q->green )
3182 || ( p[r].blue != q->blue )
3183 || ( p[r].opacity != q->opacity )
3184 || ( image->colorspace == CMYKColorspace &&
3185 p_indexes[r] != q_indexes[x] ) )
anthonyc406ea42010-06-12 01:01:49 +00003186 changed++; /* The pixel was changed in some way! */
anthony602ab9b2010-01-05 08:06:50 +00003187 p++;
3188 q++;
anthony83ba99b2010-01-24 08:48:15 +00003189 } /* x */
anthony8d188502010-06-14 04:33:35 +00003190 if ( SyncCacheViewAuthenticPixels(q_view,exception) == MagickFalse)
anthony602ab9b2010-01-05 08:06:50 +00003191 status=MagickFalse;
3192 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3193 {
3194 MagickBooleanType
3195 proceed;
3196
3197#if defined(MAGICKCORE_OPENMP_SUPPORT)
3198 #pragma omp critical (MagickCore_MorphologyImage)
3199#endif
3200 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
3201 if (proceed == MagickFalse)
3202 status=MagickFalse;
3203 }
anthony83ba99b2010-01-24 08:48:15 +00003204 } /* y */
anthony602ab9b2010-01-05 08:06:50 +00003205 result_image->type=image->type;
3206 q_view=DestroyCacheView(q_view);
3207 p_view=DestroyCacheView(p_view);
cristybb503372010-05-27 20:51:26 +00003208 return(status ? (size_t) changed : 0);
anthony602ab9b2010-01-05 08:06:50 +00003209}
3210
anthony4fd27e22010-02-07 08:17:18 +00003211
anthony9eb4f742010-05-18 02:45:54 +00003212MagickExport Image *MorphologyApply(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003213 channel,const MorphologyMethod method, const ssize_t iterations,
anthony47f5d062010-05-23 07:47:50 +00003214 const KernelInfo *kernel, const CompositeOperator compose,
3215 const double bias, ExceptionInfo *exception)
cristy2be15382010-01-21 02:38:03 +00003216{
3217 Image
anthony47f5d062010-05-23 07:47:50 +00003218 *curr_image, /* Image we are working with or iterating */
3219 *work_image, /* secondary image for primative iteration */
3220 *save_image, /* saved image - for 'edge' method only */
3221 *rslt_image; /* resultant image - after multi-kernel handling */
anthony602ab9b2010-01-05 08:06:50 +00003222
anthony4fd27e22010-02-07 08:17:18 +00003223 KernelInfo
anthony47f5d062010-05-23 07:47:50 +00003224 *reflected_kernel, /* A reflected copy of the kernel (if needed) */
3225 *norm_kernel, /* the current normal un-reflected kernel */
3226 *rflt_kernel, /* the current reflected kernel (if needed) */
3227 *this_kernel; /* the kernel being applied */
anthony4fd27e22010-02-07 08:17:18 +00003228
3229 MorphologyMethod
anthony47f5d062010-05-23 07:47:50 +00003230 primative; /* the current morphology primative being applied */
anthony9eb4f742010-05-18 02:45:54 +00003231
3232 CompositeOperator
anthony47f5d062010-05-23 07:47:50 +00003233 rslt_compose; /* multi-kernel compose method for results to use */
3234
3235 MagickBooleanType
3236 verbose; /* verbose output of results */
anthony4fd27e22010-02-07 08:17:18 +00003237
cristybb503372010-05-27 20:51:26 +00003238 size_t
anthony47f5d062010-05-23 07:47:50 +00003239 method_loop, /* Loop 1: number of compound method iterations */
3240 method_limit, /* maximum number of compound method iterations */
3241 kernel_number, /* Loop 2: the kernel number being applied */
3242 stage_loop, /* Loop 3: primative loop for compound morphology */
3243 stage_limit, /* how many primatives in this compound */
3244 kernel_loop, /* Loop 4: iterate the kernel (basic morphology) */
3245 kernel_limit, /* number of times to iterate kernel */
3246 count, /* total count of primative steps applied */
3247 changed, /* number pixels changed by last primative operation */
3248 kernel_changed, /* total count of changed using iterated kernel */
3249 method_changed; /* total count of changed over method iteration */
3250
3251 char
3252 v_info[80];
anthony1b2bc0a2010-05-12 05:25:22 +00003253
anthony602ab9b2010-01-05 08:06:50 +00003254 assert(image != (Image *) NULL);
3255 assert(image->signature == MagickSignature);
anthony4fd27e22010-02-07 08:17:18 +00003256 assert(kernel != (KernelInfo *) NULL);
3257 assert(kernel->signature == MagickSignature);
anthony602ab9b2010-01-05 08:06:50 +00003258 assert(exception != (ExceptionInfo *) NULL);
3259 assert(exception->signature == MagickSignature);
3260
anthonyc3e48252010-05-24 12:43:11 +00003261 count = 0; /* number of low-level morphology primatives performed */
anthony602ab9b2010-01-05 08:06:50 +00003262 if ( iterations == 0 )
anthony47f5d062010-05-23 07:47:50 +00003263 return((Image *)NULL); /* null operation - nothing to do! */
anthony602ab9b2010-01-05 08:06:50 +00003264
cristybb503372010-05-27 20:51:26 +00003265 kernel_limit = (size_t) iterations;
anthony47f5d062010-05-23 07:47:50 +00003266 if ( iterations < 0 ) /* negative interations = infinite (well alomst) */
3267 kernel_limit = image->columns > image->rows ? image->columns : image->rows;
anthony602ab9b2010-01-05 08:06:50 +00003268
cristye96405a2010-05-19 02:24:31 +00003269 verbose = ( GetImageArtifact(image,"verbose") != (const char *) NULL ) ?
3270 MagickTrue : MagickFalse;
anthony4f1dcb72010-05-14 08:43:10 +00003271
anthony9eb4f742010-05-18 02:45:54 +00003272 /* initialise for cleanup */
anthony47f5d062010-05-23 07:47:50 +00003273 curr_image = (Image *) image;
3274 work_image = save_image = rslt_image = (Image *) NULL;
3275 reflected_kernel = (KernelInfo *) NULL;
anthony4fd27e22010-02-07 08:17:18 +00003276
anthony47f5d062010-05-23 07:47:50 +00003277 /* Initialize specific methods
3278 * + which loop should use the given iteratations
3279 * + how many primatives make up the compound morphology
3280 * + multi-kernel compose method to use (by default)
3281 */
3282 method_limit = 1; /* just do method once, unless otherwise set */
3283 stage_limit = 1; /* assume method is not a compount */
3284 rslt_compose = compose; /* and we are composing multi-kernels as given */
anthony9eb4f742010-05-18 02:45:54 +00003285 switch( method ) {
anthony47f5d062010-05-23 07:47:50 +00003286 case SmoothMorphology: /* 4 primative compound morphology */
3287 stage_limit = 4;
anthony9eb4f742010-05-18 02:45:54 +00003288 break;
anthony47f5d062010-05-23 07:47:50 +00003289 case OpenMorphology: /* 2 primative compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00003290 case OpenIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00003291 case TopHatMorphology:
3292 case CloseMorphology:
anthony9eb4f742010-05-18 02:45:54 +00003293 case CloseIntensityMorphology:
anthony47f5d062010-05-23 07:47:50 +00003294 case BottomHatMorphology:
3295 case EdgeMorphology:
3296 stage_limit = 2;
anthony9eb4f742010-05-18 02:45:54 +00003297 break;
3298 case HitAndMissMorphology:
anthony47f5d062010-05-23 07:47:50 +00003299 rslt_compose = LightenCompositeOp; /* Union of multi-kernel results */
anthony3ca9ec12010-06-08 07:16:04 +00003300 /* FALL THUR */
anthonyc3e48252010-05-24 12:43:11 +00003301 case ThinningMorphology:
anthony9eb4f742010-05-18 02:45:54 +00003302 case ThickenMorphology:
anthony3ca9ec12010-06-08 07:16:04 +00003303 method_limit = kernel_limit; /* iterate the whole method */
anthonyc3e48252010-05-24 12:43:11 +00003304 kernel_limit = 1; /* do not do kernel iteration */
anthony47f5d062010-05-23 07:47:50 +00003305 break;
3306 default:
anthony930be612010-02-08 04:26:15 +00003307 break;
anthony602ab9b2010-01-05 08:06:50 +00003308 }
3309
anthonyc3e48252010-05-24 12:43:11 +00003310 /* Handle user (caller) specified multi-kernel composition method */
anthony47f5d062010-05-23 07:47:50 +00003311 if ( compose != UndefinedCompositeOp )
3312 rslt_compose = compose; /* override default composition for method */
3313 if ( rslt_compose == UndefinedCompositeOp )
3314 rslt_compose = NoCompositeOp; /* still not defined! Then re-iterate */
3315
anthonyc3e48252010-05-24 12:43:11 +00003316 /* Some methods require a reflected kernel to use with primatives.
3317 * Create the reflected kernel for those methods. */
anthony47f5d062010-05-23 07:47:50 +00003318 switch ( method ) {
3319 case CorrelateMorphology:
3320 case CloseMorphology:
3321 case CloseIntensityMorphology:
3322 case BottomHatMorphology:
3323 case SmoothMorphology:
3324 reflected_kernel = CloneKernelInfo(kernel);
3325 if (reflected_kernel == (KernelInfo *) NULL)
3326 goto error_cleanup;
3327 RotateKernelInfo(reflected_kernel,180);
3328 break;
3329 default:
3330 break;
anthony9eb4f742010-05-18 02:45:54 +00003331 }
anthony7a01dcf2010-05-11 12:25:52 +00003332
anthony47f5d062010-05-23 07:47:50 +00003333 /* Loop 1: iterate the compound method */
3334 method_loop = 0;
3335 method_changed = 1;
3336 while ( method_loop < method_limit && method_changed > 0 ) {
3337 method_loop++;
3338 method_changed = 0;
anthony9eb4f742010-05-18 02:45:54 +00003339
anthony47f5d062010-05-23 07:47:50 +00003340 /* Loop 2: iterate over each kernel in a multi-kernel list */
3341 norm_kernel = (KernelInfo *) kernel;
cristyf2faecf2010-05-28 19:19:36 +00003342 this_kernel = (KernelInfo *) kernel;
anthony47f5d062010-05-23 07:47:50 +00003343 rflt_kernel = reflected_kernel;
anthonye4d89962010-05-29 10:53:11 +00003344
anthony47f5d062010-05-23 07:47:50 +00003345 kernel_number = 0;
3346 while ( norm_kernel != NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003347
anthony47f5d062010-05-23 07:47:50 +00003348 /* Loop 3: Compound Morphology Staging - Select Primative to apply */
3349 stage_loop = 0; /* the compound morphology stage number */
3350 while ( stage_loop < stage_limit ) {
3351 stage_loop++; /* The stage of the compound morphology */
anthony9eb4f742010-05-18 02:45:54 +00003352
anthony47f5d062010-05-23 07:47:50 +00003353 /* Select primative morphology for this stage of compound method */
3354 this_kernel = norm_kernel; /* default use unreflected kernel */
anthonybd0f5562010-05-24 13:05:02 +00003355 primative = method; /* Assume method is a primative */
anthony47f5d062010-05-23 07:47:50 +00003356 switch( method ) {
3357 case ErodeMorphology: /* just erode */
3358 case EdgeInMorphology: /* erode and image difference */
3359 primative = ErodeMorphology;
3360 break;
3361 case DilateMorphology: /* just dilate */
3362 case EdgeOutMorphology: /* dilate and image difference */
3363 primative = DilateMorphology;
3364 break;
3365 case OpenMorphology: /* erode then dialate */
3366 case TopHatMorphology: /* open and image difference */
3367 primative = ErodeMorphology;
3368 if ( stage_loop == 2 )
3369 primative = DilateMorphology;
3370 break;
3371 case OpenIntensityMorphology:
3372 primative = ErodeIntensityMorphology;
3373 if ( stage_loop == 2 )
3374 primative = DilateIntensityMorphology;
anthonye4d89962010-05-29 10:53:11 +00003375 break;
anthony47f5d062010-05-23 07:47:50 +00003376 case CloseMorphology: /* dilate, then erode */
3377 case BottomHatMorphology: /* close and image difference */
3378 this_kernel = rflt_kernel; /* use the reflected kernel */
3379 primative = DilateMorphology;
3380 if ( stage_loop == 2 )
3381 primative = ErodeMorphology;
3382 break;
3383 case CloseIntensityMorphology:
3384 this_kernel = rflt_kernel; /* use the reflected kernel */
3385 primative = DilateIntensityMorphology;
3386 if ( stage_loop == 2 )
3387 primative = ErodeIntensityMorphology;
3388 break;
3389 case SmoothMorphology: /* open, close */
3390 switch ( stage_loop ) {
3391 case 1: /* start an open method, which starts with Erode */
3392 primative = ErodeMorphology;
3393 break;
3394 case 2: /* now Dilate the Erode */
3395 primative = DilateMorphology;
3396 break;
3397 case 3: /* Reflect kernel a close */
3398 this_kernel = rflt_kernel; /* use the reflected kernel */
3399 primative = DilateMorphology;
3400 break;
3401 case 4: /* Finish the Close */
3402 this_kernel = rflt_kernel; /* use the reflected kernel */
3403 primative = ErodeMorphology;
3404 break;
3405 }
3406 break;
3407 case EdgeMorphology: /* dilate and erode difference */
3408 primative = DilateMorphology;
3409 if ( stage_loop == 2 ) {
3410 save_image = curr_image; /* save the image difference */
3411 curr_image = (Image *) image;
3412 primative = ErodeMorphology;
3413 }
3414 break;
3415 case CorrelateMorphology:
3416 /* A Correlation is a Convolution with a reflected kernel.
3417 ** However a Convolution is a weighted sum using a reflected
3418 ** kernel. It may seem stange to convert a Correlation into a
3419 ** Convolution as the Correlation is the simplier method, but
3420 ** Convolution is much more commonly used, and it makes sense to
3421 ** implement it directly so as to avoid the need to duplicate the
3422 ** kernel when it is not required (which is typically the
3423 ** default).
3424 */
3425 this_kernel = rflt_kernel; /* use the reflected kernel */
3426 primative = ConvolveMorphology;
3427 break;
3428 default:
anthony47f5d062010-05-23 07:47:50 +00003429 break;
3430 }
anthonye4d89962010-05-29 10:53:11 +00003431 assert( this_kernel != (KernelInfo *) NULL );
anthony9eb4f742010-05-18 02:45:54 +00003432
anthony47f5d062010-05-23 07:47:50 +00003433 /* Extra information for debugging compound operations */
3434 if ( verbose == MagickTrue ) {
3435 if ( stage_limit > 1 )
cristye8c25f92010-06-03 00:53:06 +00003436 (void) FormatMagickString(v_info,MaxTextExtent,"%s:%.20g.%.20g -> ",
3437 MagickOptionToMnemonic(MagickMorphologyOptions,method),(double)
3438 method_loop,(double) stage_loop);
anthony47f5d062010-05-23 07:47:50 +00003439 else if ( primative != method )
cristye8c25f92010-06-03 00:53:06 +00003440 (void) FormatMagickString(v_info, MaxTextExtent, "%s:%.20g -> ",
3441 MagickOptionToMnemonic(MagickMorphologyOptions, method),(double)
3442 method_loop);
anthony47f5d062010-05-23 07:47:50 +00003443 else
3444 v_info[0] = '\0';
3445 }
3446
3447 /* Loop 4: Iterate the kernel with primative */
3448 kernel_loop = 0;
3449 kernel_changed = 0;
3450 changed = 1;
3451 while ( kernel_loop < kernel_limit && changed > 0 ) {
3452 kernel_loop++; /* the iteration of this kernel */
anthony9eb4f742010-05-18 02:45:54 +00003453
3454 /* Create a destination image, if not yet defined */
3455 if ( work_image == (Image *) NULL )
3456 {
3457 work_image=CloneImage(image,0,0,MagickTrue,exception);
3458 if (work_image == (Image *) NULL)
3459 goto error_cleanup;
3460 if (SetImageStorageClass(work_image,DirectClass) == MagickFalse)
3461 {
3462 InheritException(exception,&work_image->exception);
3463 goto error_cleanup;
3464 }
3465 }
3466
anthony501c2f92010-06-02 10:55:14 +00003467 /* APPLY THE MORPHOLOGICAL PRIMITIVE (curr -> work) */
anthony9eb4f742010-05-18 02:45:54 +00003468 count++;
anthony47f5d062010-05-23 07:47:50 +00003469 changed = MorphologyPrimitive(curr_image, work_image, primative,
anthony9eb4f742010-05-18 02:45:54 +00003470 channel, this_kernel, bias, exception);
anthony47f5d062010-05-23 07:47:50 +00003471 kernel_changed += changed;
3472 method_changed += changed;
anthony9eb4f742010-05-18 02:45:54 +00003473
anthony47f5d062010-05-23 07:47:50 +00003474 if ( verbose == MagickTrue ) {
3475 if ( kernel_loop > 1 )
3476 fprintf(stderr, "\n"); /* add end-of-line from previous */
cristye8c25f92010-06-03 00:53:06 +00003477 (void) fprintf(stderr, "%s%s%s:%.20g.%.20g #%.20g => Changed %.20g",
3478 v_info,MagickOptionToMnemonic(MagickMorphologyOptions,
3479 primative),(this_kernel == rflt_kernel ) ? "*" : "",
3480 (double) (method_loop+kernel_loop-1),(double) kernel_number,
3481 (double) count,(double) changed);
anthony47f5d062010-05-23 07:47:50 +00003482 }
anthony9eb4f742010-05-18 02:45:54 +00003483 /* prepare next loop */
3484 { Image *tmp = work_image; /* swap images for iteration */
3485 work_image = curr_image;
3486 curr_image = tmp;
3487 }
3488 if ( work_image == image )
anthony47f5d062010-05-23 07:47:50 +00003489 work_image = (Image *) NULL; /* replace input 'image' */
anthony7a01dcf2010-05-11 12:25:52 +00003490
anthony47f5d062010-05-23 07:47:50 +00003491 } /* End Loop 4: Iterate the kernel with primative */
anthony1b2bc0a2010-05-12 05:25:22 +00003492
anthony47f5d062010-05-23 07:47:50 +00003493 if ( verbose == MagickTrue && kernel_changed != changed )
cristye8c25f92010-06-03 00:53:06 +00003494 fprintf(stderr, " Total %.20g",(double) kernel_changed);
anthony47f5d062010-05-23 07:47:50 +00003495 if ( verbose == MagickTrue && stage_loop < stage_limit )
3496 fprintf(stderr, "\n"); /* add end-of-line before looping */
anthony9eb4f742010-05-18 02:45:54 +00003497
3498#if 0
anthonye4d89962010-05-29 10:53:11 +00003499 fprintf(stderr, "--E-- image=0x%lx\n", (unsigned long)image);
3500 fprintf(stderr, " curr =0x%lx\n", (unsigned long)curr_image);
3501 fprintf(stderr, " work =0x%lx\n", (unsigned long)work_image);
3502 fprintf(stderr, " save =0x%lx\n", (unsigned long)save_image);
3503 fprintf(stderr, " union=0x%lx\n", (unsigned long)rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003504#endif
3505
anthony47f5d062010-05-23 07:47:50 +00003506 } /* End Loop 3: Primative (staging) Loop for Coumpound Methods */
anthony9eb4f742010-05-18 02:45:54 +00003507
anthony47f5d062010-05-23 07:47:50 +00003508 /* Final Post-processing for some Compound Methods
3509 **
3510 ** The removal of any 'Sync' channel flag in the Image Compositon
3511 ** below ensures the methematical compose method is applied in a
3512 ** purely mathematical way, and only to the selected channels.
3513 ** Turn off SVG composition 'alpha blending'.
3514 */
3515 switch( method ) {
3516 case EdgeOutMorphology:
3517 case EdgeInMorphology:
3518 case TopHatMorphology:
3519 case BottomHatMorphology:
3520 if ( verbose == MagickTrue )
3521 fprintf(stderr, "\n%s: Difference with original image",
3522 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3523 (void) CompositeImageChannel(curr_image,
3524 (ChannelType) (channel & ~SyncChannels),
3525 DifferenceCompositeOp, image, 0, 0);
3526 break;
3527 case EdgeMorphology:
3528 if ( verbose == MagickTrue )
3529 fprintf(stderr, "\n%s: Difference of Dilate and Erode",
3530 MagickOptionToMnemonic(MagickMorphologyOptions, method) );
3531 (void) CompositeImageChannel(curr_image,
3532 (ChannelType) (channel & ~SyncChannels),
3533 DifferenceCompositeOp, save_image, 0, 0);
3534 save_image = DestroyImage(save_image); /* finished with save image */
3535 break;
3536 default:
3537 break;
3538 }
3539
3540 /* multi-kernel handling: re-iterate, or compose results */
3541 if ( kernel->next == (KernelInfo *) NULL )
anthonyc3e48252010-05-24 12:43:11 +00003542 rslt_image = curr_image; /* just return the resulting image */
anthony47f5d062010-05-23 07:47:50 +00003543 else if ( rslt_compose == NoCompositeOp )
anthonyc3e48252010-05-24 12:43:11 +00003544 { if ( verbose == MagickTrue ) {
3545 if ( this_kernel->next != (KernelInfo *) NULL )
3546 fprintf(stderr, " (re-iterate)");
3547 else
3548 fprintf(stderr, " (done)");
3549 }
3550 rslt_image = curr_image; /* return result, and re-iterate */
anthony9eb4f742010-05-18 02:45:54 +00003551 }
anthony47f5d062010-05-23 07:47:50 +00003552 else if ( rslt_image == (Image *) NULL)
3553 { if ( verbose == MagickTrue )
3554 fprintf(stderr, " (save for compose)");
3555 rslt_image = curr_image;
3556 curr_image = (Image *) image; /* continue with original image */
anthony9eb4f742010-05-18 02:45:54 +00003557 }
anthony47f5d062010-05-23 07:47:50 +00003558 else
3559 { /* add the new 'current' result to the composition
3560 **
3561 ** The removal of any 'Sync' channel flag in the Image Compositon
3562 ** below ensures the methematical compose method is applied in a
3563 ** purely mathematical way, and only to the selected channels.
3564 ** Turn off SVG composition 'alpha blending'.
anthony3ca9ec12010-06-08 07:16:04 +00003565 **
3566 ** The compose image order is specifically so that the new image can
3567 ** be subtarcted 'Minus' from the collected result, to allow you to
3568 ** convert a HitAndMiss methd into a Thinning method.
anthony47f5d062010-05-23 07:47:50 +00003569 */
3570 if ( verbose == MagickTrue )
3571 fprintf(stderr, " (compose \"%s\")",
3572 MagickOptionToMnemonic(MagickComposeOptions, rslt_compose) );
anthony3ca9ec12010-06-08 07:16:04 +00003573 (void) CompositeImageChannel(curr_image,
anthony47f5d062010-05-23 07:47:50 +00003574 (ChannelType) (channel & ~SyncChannels), rslt_compose,
anthony3ca9ec12010-06-08 07:16:04 +00003575 rslt_image, 0, 0);
3576 rslt_image = DestroyImage(rslt_image);
3577 rslt_image = curr_image;
anthony47f5d062010-05-23 07:47:50 +00003578 curr_image = (Image *) image; /* continue with original image */
3579 }
3580 if ( verbose == MagickTrue )
3581 fprintf(stderr, "\n");
anthony9eb4f742010-05-18 02:45:54 +00003582
anthony47f5d062010-05-23 07:47:50 +00003583 /* loop to the next kernel in a multi-kernel list */
3584 norm_kernel = norm_kernel->next;
3585 if ( rflt_kernel != (KernelInfo *) NULL )
3586 rflt_kernel = rflt_kernel->next;
3587 kernel_number++;
3588 } /* End Loop 2: Loop over each kernel */
anthony9eb4f742010-05-18 02:45:54 +00003589
anthony47f5d062010-05-23 07:47:50 +00003590 } /* End Loop 1: compound method interation */
anthony602ab9b2010-01-05 08:06:50 +00003591
anthony9eb4f742010-05-18 02:45:54 +00003592 goto exit_cleanup;
anthony1b2bc0a2010-05-12 05:25:22 +00003593
anthony47f5d062010-05-23 07:47:50 +00003594 /* Yes goto's are bad, but it makes cleanup lot more efficient */
anthony1b2bc0a2010-05-12 05:25:22 +00003595error_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003596 if ( curr_image != (Image *) NULL &&
3597 curr_image != rslt_image &&
3598 curr_image != image )
3599 curr_image = DestroyImage(curr_image);
3600 if ( rslt_image != (Image *) NULL )
3601 rslt_image = DestroyImage(rslt_image);
anthony1b2bc0a2010-05-12 05:25:22 +00003602exit_cleanup:
anthony47f5d062010-05-23 07:47:50 +00003603 if ( curr_image != (Image *) NULL &&
3604 curr_image != rslt_image &&
3605 curr_image != image )
3606 curr_image = DestroyImage(curr_image);
anthony9eb4f742010-05-18 02:45:54 +00003607 if ( work_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003608 work_image = DestroyImage(work_image);
anthony9eb4f742010-05-18 02:45:54 +00003609 if ( save_image != (Image *) NULL )
anthony47f5d062010-05-23 07:47:50 +00003610 save_image = DestroyImage(save_image);
3611 if ( reflected_kernel != (KernelInfo *) NULL )
3612 reflected_kernel = DestroyKernelInfo(reflected_kernel);
3613 return(rslt_image);
anthony9eb4f742010-05-18 02:45:54 +00003614}
3615
3616/*
3617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3618% %
3619% %
3620% %
3621% M o r p h o l o g y I m a g e C h a n n e l %
3622% %
3623% %
3624% %
3625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3626%
3627% MorphologyImageChannel() applies a user supplied kernel to the image
3628% according to the given mophology method.
3629%
3630% This function applies any and all user defined settings before calling
3631% the above internal function MorphologyApply().
3632%
3633% User defined settings include...
anthony46a369d2010-05-19 02:41:48 +00003634% * Output Bias for Convolution and correlation ("-bias")
3635% * Kernel Scale/normalize settings ("-set 'option:convolve:scale'")
3636% This can also includes the addition of a scaled unity kernel.
3637% * Show Kernel being applied ("-set option:showkernel 1")
anthony9eb4f742010-05-18 02:45:54 +00003638%
3639% The format of the MorphologyImage method is:
3640%
3641% Image *MorphologyImage(const Image *image,MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003642% const ssize_t iterations,KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003643%
3644% Image *MorphologyImageChannel(const Image *image, const ChannelType
cristybb503372010-05-27 20:51:26 +00003645% channel,MorphologyMethod method,const ssize_t iterations,
anthony9eb4f742010-05-18 02:45:54 +00003646% KernelInfo *kernel,ExceptionInfo *exception)
3647%
3648% A description of each parameter follows:
3649%
3650% o image: the image.
3651%
3652% o method: the morphology method to be applied.
3653%
3654% o iterations: apply the operation this many times (or no change).
3655% A value of -1 means loop until no change found.
3656% How this is applied may depend on the morphology method.
3657% Typically this is a value of 1.
3658%
3659% o channel: the channel type.
3660%
3661% o kernel: An array of double representing the morphology kernel.
3662% Warning: kernel may be normalized for the Convolve method.
3663%
3664% o exception: return any errors or warnings in this structure.
3665%
3666*/
3667
3668MagickExport Image *MorphologyImageChannel(const Image *image,
3669 const ChannelType channel,const MorphologyMethod method,
cristybb503372010-05-27 20:51:26 +00003670 const ssize_t iterations,const KernelInfo *kernel,ExceptionInfo *exception)
anthony9eb4f742010-05-18 02:45:54 +00003671{
3672 const char
3673 *artifact;
3674
3675 KernelInfo
3676 *curr_kernel;
3677
anthony47f5d062010-05-23 07:47:50 +00003678 CompositeOperator
3679 compose;
3680
anthony9eb4f742010-05-18 02:45:54 +00003681 Image
3682 *morphology_image;
3683
3684
anthony46a369d2010-05-19 02:41:48 +00003685 /* Apply Convolve/Correlate Normalization and Scaling Factors.
3686 * This is done BEFORE the ShowKernelInfo() function is called so that
3687 * users can see the results of the 'option:convolve:scale' option.
anthony9eb4f742010-05-18 02:45:54 +00003688 */
3689 curr_kernel = (KernelInfo *) kernel;
anthonyf71ca292010-05-19 04:08:43 +00003690 if ( method == ConvolveMorphology || method == CorrelateMorphology )
anthony9eb4f742010-05-18 02:45:54 +00003691 {
3692 artifact = GetImageArtifact(image,"convolve:scale");
anthonye8d2f552010-06-05 10:43:25 +00003693 if ( artifact != (const char *)NULL ) {
anthony9eb4f742010-05-18 02:45:54 +00003694 if ( curr_kernel == kernel )
3695 curr_kernel = CloneKernelInfo(kernel);
3696 if (curr_kernel == (KernelInfo *) NULL) {
3697 curr_kernel=DestroyKernelInfo(curr_kernel);
3698 return((Image *) NULL);
3699 }
anthony46a369d2010-05-19 02:41:48 +00003700 ScaleGeometryKernelInfo(curr_kernel, artifact);
anthony9eb4f742010-05-18 02:45:54 +00003701 }
3702 }
3703
3704 /* display the (normalized) kernel via stderr */
3705 artifact = GetImageArtifact(image,"showkernel");
anthony47f5d062010-05-23 07:47:50 +00003706 if ( artifact == (const char *) NULL)
3707 artifact = GetImageArtifact(image,"convolve:showkernel");
3708 if ( artifact == (const char *) NULL)
3709 artifact = GetImageArtifact(image,"morphology:showkernel");
anthony9eb4f742010-05-18 02:45:54 +00003710 if ( artifact != (const char *) NULL)
3711 ShowKernelInfo(curr_kernel);
3712
anthony32066782010-06-08 13:46:27 +00003713 /* Override the default handling of multi-kernel morphology results
3714 * If 'Undefined' use the default method
3715 * If 'None' (default for 'Convolve') re-iterate previous result
3716 * Otherwise merge resulting images using compose method given.
3717 * Default for 'HitAndMiss' is 'Lighten'.
anthony47f5d062010-05-23 07:47:50 +00003718 */
3719 compose = UndefinedCompositeOp; /* use default for method */
3720 artifact = GetImageArtifact(image,"morphology:compose");
3721 if ( artifact != (const char *) NULL)
3722 compose = (CompositeOperator) ParseMagickOption(
3723 MagickComposeOptions,MagickFalse,artifact);
3724
anthony9eb4f742010-05-18 02:45:54 +00003725 /* Apply the Morphology */
3726 morphology_image = MorphologyApply(image, channel, method, iterations,
anthony47f5d062010-05-23 07:47:50 +00003727 curr_kernel, compose, image->bias, exception);
anthony9eb4f742010-05-18 02:45:54 +00003728
3729 /* Cleanup and Exit */
3730 if ( curr_kernel != kernel )
anthony1b2bc0a2010-05-12 05:25:22 +00003731 curr_kernel=DestroyKernelInfo(curr_kernel);
anthony9eb4f742010-05-18 02:45:54 +00003732 return(morphology_image);
3733}
3734
3735MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
cristybb503372010-05-27 20:51:26 +00003736 method, const ssize_t iterations,const KernelInfo *kernel, ExceptionInfo
anthony9eb4f742010-05-18 02:45:54 +00003737 *exception)
3738{
3739 Image
3740 *morphology_image;
3741
3742 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
3743 iterations,kernel,exception);
3744 return(morphology_image);
anthony602ab9b2010-01-05 08:06:50 +00003745}
anthony83ba99b2010-01-24 08:48:15 +00003746
3747/*
3748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3749% %
3750% %
3751% %
anthony4fd27e22010-02-07 08:17:18 +00003752+ R o t a t e K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00003753% %
3754% %
3755% %
3756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3757%
anthony46a369d2010-05-19 02:41:48 +00003758% RotateKernelInfo() rotates the kernel by the angle given.
3759%
3760% Currently it is restricted to 90 degree angles, of either 1D kernels
3761% or square kernels. And 'circular' rotations of 45 degrees for 3x3 kernels.
3762% It will ignore usless rotations for specific 'named' built-in kernels.
anthony83ba99b2010-01-24 08:48:15 +00003763%
anthony4fd27e22010-02-07 08:17:18 +00003764% The format of the RotateKernelInfo method is:
anthony83ba99b2010-01-24 08:48:15 +00003765%
anthony4fd27e22010-02-07 08:17:18 +00003766% void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003767%
3768% A description of each parameter follows:
3769%
3770% o kernel: the Morphology/Convolution kernel
3771%
3772% o angle: angle to rotate in degrees
3773%
anthony46a369d2010-05-19 02:41:48 +00003774% This function is currently internal to this module only, but can be exported
3775% to other modules if needed.
anthony83ba99b2010-01-24 08:48:15 +00003776*/
anthony4fd27e22010-02-07 08:17:18 +00003777static void RotateKernelInfo(KernelInfo *kernel, double angle)
anthony83ba99b2010-01-24 08:48:15 +00003778{
anthony1b2bc0a2010-05-12 05:25:22 +00003779 /* angle the lower kernels first */
3780 if ( kernel->next != (KernelInfo *) NULL)
3781 RotateKernelInfo(kernel->next, angle);
3782
anthony83ba99b2010-01-24 08:48:15 +00003783 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
3784 **
3785 ** TODO: expand beyond simple 90 degree rotates, flips and flops
3786 */
3787
3788 /* Modulus the angle */
3789 angle = fmod(angle, 360.0);
3790 if ( angle < 0 )
3791 angle += 360.0;
3792
anthony3c10fc82010-05-13 02:40:51 +00003793 if ( 337.5 < angle || angle <= 22.5 )
anthony43c49252010-05-18 10:59:50 +00003794 return; /* Near zero angle - no change! - At least not at this time */
anthony83ba99b2010-01-24 08:48:15 +00003795
anthony3dd0f622010-05-13 12:57:32 +00003796 /* Handle special cases */
anthony83ba99b2010-01-24 08:48:15 +00003797 switch (kernel->type) {
3798 /* These built-in kernels are cylindrical kernels, rotating is useless */
3799 case GaussianKernel:
anthony501c2f92010-06-02 10:55:14 +00003800 case DoGKernel:
3801 case LoGKernel:
anthony83ba99b2010-01-24 08:48:15 +00003802 case DiskKernel:
anthony3dd0f622010-05-13 12:57:32 +00003803 case PeaksKernel:
3804 case LaplacianKernel:
anthony83ba99b2010-01-24 08:48:15 +00003805 case ChebyshevKernel:
anthonybee715c2010-06-04 01:25:57 +00003806 case ManhattanKernel:
anthony83ba99b2010-01-24 08:48:15 +00003807 case EuclideanKernel:
3808 return;
3809
3810 /* These may be rotatable at non-90 angles in the future */
3811 /* but simply rotating them in multiples of 90 degrees is useless */
3812 case SquareKernel:
3813 case DiamondKernel:
3814 case PlusKernel:
anthony3dd0f622010-05-13 12:57:32 +00003815 case CrossKernel:
anthony83ba99b2010-01-24 08:48:15 +00003816 return;
3817
3818 /* These only allows a +/-90 degree rotation (by transpose) */
3819 /* A 180 degree rotation is useless */
3820 case BlurKernel:
3821 case RectangleKernel:
3822 if ( 135.0 < angle && angle <= 225.0 )
3823 return;
3824 if ( 225.0 < angle && angle <= 315.0 )
3825 angle -= 180;
3826 break;
3827
anthony3dd0f622010-05-13 12:57:32 +00003828 default:
anthony83ba99b2010-01-24 08:48:15 +00003829 break;
3830 }
anthony3c10fc82010-05-13 02:40:51 +00003831 /* Attempt rotations by 45 degrees */
3832 if ( 22.5 < fmod(angle,90.0) && fmod(angle,90.0) <= 67.5 )
3833 {
3834 if ( kernel->width == 3 && kernel->height == 3 )
3835 { /* Rotate a 3x3 square by 45 degree angle */
3836 MagickRealType t = kernel->values[0];
anthony43c49252010-05-18 10:59:50 +00003837 kernel->values[0] = kernel->values[3];
3838 kernel->values[3] = kernel->values[6];
3839 kernel->values[6] = kernel->values[7];
3840 kernel->values[7] = kernel->values[8];
3841 kernel->values[8] = kernel->values[5];
3842 kernel->values[5] = kernel->values[2];
3843 kernel->values[2] = kernel->values[1];
3844 kernel->values[1] = t;
anthony1d45eb92010-05-25 11:13:23 +00003845 /* rotate non-centered origin */
3846 if ( kernel->x != 1 || kernel->y != 1 ) {
cristybb503372010-05-27 20:51:26 +00003847 ssize_t x,y;
3848 x = (ssize_t) kernel->x-1;
3849 y = (ssize_t) kernel->y-1;
anthony1d45eb92010-05-25 11:13:23 +00003850 if ( x == y ) x = 0;
3851 else if ( x == 0 ) x = -y;
3852 else if ( x == -y ) y = 0;
3853 else if ( y == 0 ) y = x;
cristyecd0ab52010-05-30 14:59:20 +00003854 kernel->x = (ssize_t) x+1;
3855 kernel->y = (ssize_t) y+1;
anthony1d45eb92010-05-25 11:13:23 +00003856 }
anthony43c49252010-05-18 10:59:50 +00003857 angle = fmod(angle+315.0, 360.0); /* angle reduced 45 degrees */
3858 kernel->angle = fmod(kernel->angle+45.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003859 }
3860 else
3861 perror("Unable to rotate non-3x3 kernel by 45 degrees");
3862 }
3863 if ( 45.0 < fmod(angle, 180.0) && fmod(angle,180.0) <= 135.0 )
3864 {
3865 if ( kernel->width == 1 || kernel->height == 1 )
anthonybfb635a2010-06-04 00:18:04 +00003866 { /* Do a transpose of a 1 dimentional kernel,
3867 ** which results in a fast 90 degree rotation of some type.
anthony3c10fc82010-05-13 02:40:51 +00003868 */
cristybb503372010-05-27 20:51:26 +00003869 ssize_t
anthony3c10fc82010-05-13 02:40:51 +00003870 t;
cristybb503372010-05-27 20:51:26 +00003871 t = (ssize_t) kernel->width;
anthony3c10fc82010-05-13 02:40:51 +00003872 kernel->width = kernel->height;
cristybb503372010-05-27 20:51:26 +00003873 kernel->height = (size_t) t;
anthony3c10fc82010-05-13 02:40:51 +00003874 t = kernel->x;
3875 kernel->x = kernel->y;
3876 kernel->y = t;
anthony43c49252010-05-18 10:59:50 +00003877 if ( kernel->width == 1 ) {
3878 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3879 kernel->angle = fmod(kernel->angle+90.0, 360.0);
3880 } else {
3881 angle = fmod(angle+90.0, 360.0); /* angle increased 90 degrees */
3882 kernel->angle = fmod(kernel->angle+270.0, 360.0);
3883 }
anthony3c10fc82010-05-13 02:40:51 +00003884 }
3885 else if ( kernel->width == kernel->height )
3886 { /* Rotate a square array of values by 90 degrees */
cristybb503372010-05-27 20:51:26 +00003887 { register size_t
anthony1d45eb92010-05-25 11:13:23 +00003888 i,j,x,y;
3889 register MagickRealType
3890 *k,t;
3891 k=kernel->values;
3892 for( i=0, x=kernel->width-1; i<=x; i++, x--)
3893 for( j=0, y=kernel->height-1; j<y; j++, y--)
3894 { t = k[i+j*kernel->width];
3895 k[i+j*kernel->width] = k[j+x*kernel->width];
3896 k[j+x*kernel->width] = k[x+y*kernel->width];
3897 k[x+y*kernel->width] = k[y+i*kernel->width];
3898 k[y+i*kernel->width] = t;
3899 }
3900 }
3901 /* rotate the origin - relative to center of array */
cristybb503372010-05-27 20:51:26 +00003902 { register ssize_t x,y;
cristyeaedf062010-05-29 22:36:02 +00003903 x = (ssize_t) (kernel->x*2-kernel->width+1);
3904 y = (ssize_t) (kernel->y*2-kernel->height+1);
cristyecd0ab52010-05-30 14:59:20 +00003905 kernel->x = (ssize_t) ( -y +(ssize_t) kernel->width-1)/2;
3906 kernel->y = (ssize_t) ( +x +(ssize_t) kernel->height-1)/2;
anthony1d45eb92010-05-25 11:13:23 +00003907 }
anthony43c49252010-05-18 10:59:50 +00003908 angle = fmod(angle+270.0, 360.0); /* angle reduced 90 degrees */
3909 kernel->angle = fmod(kernel->angle+90.0, 360.0);
anthony3c10fc82010-05-13 02:40:51 +00003910 }
3911 else
3912 perror("Unable to rotate a non-square, non-linear kernel 90 degrees");
3913 }
anthony83ba99b2010-01-24 08:48:15 +00003914 if ( 135.0 < angle && angle <= 225.0 )
3915 {
anthony43c49252010-05-18 10:59:50 +00003916 /* For a 180 degree rotation - also know as a reflection
3917 * This is actually a very very common operation!
3918 * Basically all that is needed is a reversal of the kernel data!
3919 * And a reflection of the origon
3920 */
cristybb503372010-05-27 20:51:26 +00003921 size_t
anthony83ba99b2010-01-24 08:48:15 +00003922 i,j;
3923 register double
3924 *k,t;
3925
3926 k=kernel->values;
3927 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
3928 t=k[i], k[i]=k[j], k[j]=t;
3929
cristybb503372010-05-27 20:51:26 +00003930 kernel->x = (ssize_t) kernel->width - kernel->x - 1;
3931 kernel->y = (ssize_t) kernel->height - kernel->y - 1;
anthony43c49252010-05-18 10:59:50 +00003932 angle = fmod(angle-180.0, 360.0); /* angle+180 degrees */
3933 kernel->angle = fmod(kernel->angle+180.0, 360.0);
anthony83ba99b2010-01-24 08:48:15 +00003934 }
anthony3c10fc82010-05-13 02:40:51 +00003935 /* At this point angle should at least between -45 (315) and +45 degrees
anthony83ba99b2010-01-24 08:48:15 +00003936 * In the future some form of non-orthogonal angled rotates could be
3937 * performed here, posibily with a linear kernel restriction.
3938 */
3939
anthony83ba99b2010-01-24 08:48:15 +00003940 return;
3941}
3942
3943/*
3944%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3945% %
3946% %
3947% %
anthony46a369d2010-05-19 02:41:48 +00003948% S c a l e G e o m e t r y K e r n e l I n f o %
3949% %
3950% %
3951% %
3952%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3953%
3954% ScaleGeometryKernelInfo() takes a geometry argument string, typically
3955% provided as a "-set option:convolve:scale {geometry}" user setting,
3956% and modifies the kernel according to the parsed arguments of that setting.
3957%
3958% The first argument (and any normalization flags) are passed to
3959% ScaleKernelInfo() to scale/normalize the kernel. The second argument
3960% is then passed to UnityAddKernelInfo() to add a scled unity kernel
3961% into the scaled/normalized kernel.
3962%
3963% The format of the ScaleKernelInfo method is:
3964%
3965% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
3966% const MagickStatusType normalize_flags )
3967%
3968% A description of each parameter follows:
3969%
3970% o kernel: the Morphology/Convolution kernel to modify
3971%
3972% o geometry:
3973% The geometry string to parse, typically from the user provided
3974% "-set option:convolve:scale {geometry}" setting.
3975%
3976*/
3977MagickExport void ScaleGeometryKernelInfo (KernelInfo *kernel,
3978 const char *geometry)
3979{
3980 GeometryFlags
3981 flags;
3982 GeometryInfo
3983 args;
3984
3985 SetGeometryInfo(&args);
3986 flags = (GeometryFlags) ParseGeometry(geometry, &args);
3987
3988#if 0
3989 /* For Debugging Geometry Input */
3990 fprintf(stderr, "Geometry = 0x%04X : %lg x %lg %+lg %+lg\n",
3991 flags, args.rho, args.sigma, args.xi, args.psi );
3992#endif
3993
3994 if ( (flags & PercentValue) != 0 ) /* Handle Percentage flag*/
3995 args.rho *= 0.01, args.sigma *= 0.01;
3996
3997 if ( (flags & RhoValue) == 0 ) /* Set Defaults for missing args */
3998 args.rho = 1.0;
3999 if ( (flags & SigmaValue) == 0 )
4000 args.sigma = 0.0;
4001
4002 /* Scale/Normalize the input kernel */
4003 ScaleKernelInfo(kernel, args.rho, flags);
4004
4005 /* Add Unity Kernel, for blending with original */
4006 if ( (flags & SigmaValue) != 0 )
4007 UnityAddKernelInfo(kernel, args.sigma);
4008
4009 return;
4010}
4011/*
4012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4013% %
4014% %
4015% %
cristy6771f1e2010-03-05 19:43:39 +00004016% S c a l e K e r n e l I n f o %
anthonycc6c8362010-01-25 04:14:01 +00004017% %
4018% %
4019% %
4020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4021%
anthony1b2bc0a2010-05-12 05:25:22 +00004022% ScaleKernelInfo() scales the given kernel list by the given amount, with or
4023% without normalization of the sum of the kernel values (as per given flags).
anthonycc6c8362010-01-25 04:14:01 +00004024%
anthony999bb2c2010-02-18 12:38:01 +00004025% By default (no flags given) the values within the kernel is scaled
anthony1b2bc0a2010-05-12 05:25:22 +00004026% directly using given scaling factor without change.
anthonycc6c8362010-01-25 04:14:01 +00004027%
anthony46a369d2010-05-19 02:41:48 +00004028% If either of the two 'normalize_flags' are given the kernel will first be
4029% normalized and then further scaled by the scaling factor value given.
anthony999bb2c2010-02-18 12:38:01 +00004030%
4031% Kernel normalization ('normalize_flags' given) is designed to ensure that
4032% any use of the kernel scaling factor with 'Convolve' or 'Correlate'
anthony1b2bc0a2010-05-12 05:25:22 +00004033% morphology methods will fall into -1.0 to +1.0 range. Note that for
4034% non-HDRI versions of IM this may cause images to have any negative results
4035% clipped, unless some 'bias' is used.
anthony999bb2c2010-02-18 12:38:01 +00004036%
4037% More specifically. Kernels which only contain positive values (such as a
4038% 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
anthony1b2bc0a2010-05-12 05:25:22 +00004039% ensuring a 0.0 to +1.0 output range for non-HDRI images.
anthony999bb2c2010-02-18 12:38:01 +00004040%
4041% For Kernels that contain some negative values, (such as 'Sharpen' kernels)
4042% the kernel will be scaled by the absolute of the sum of kernel values, so
4043% that it will generally fall within the +/- 1.0 range.
4044%
4045% For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
4046% will be scaled by just the sum of the postive values, so that its output
4047% range will again fall into the +/- 1.0 range.
4048%
4049% For special kernels designed for locating shapes using 'Correlate', (often
4050% only containing +1 and -1 values, representing foreground/brackground
4051% matching) a special normalization method is provided to scale the positive
4052% values seperatally to those of the negative values, so the kernel will be
4053% forced to become a zero-sum kernel better suited to such searches.
4054%
anthony1b2bc0a2010-05-12 05:25:22 +00004055% WARNING: Correct normalization of the kernel assumes that the '*_range'
anthony999bb2c2010-02-18 12:38:01 +00004056% attributes within the kernel structure have been correctly set during the
4057% kernels creation.
4058%
4059% NOTE: The values used for 'normalize_flags' have been selected specifically
anthony46a369d2010-05-19 02:41:48 +00004060% to match the use of geometry options, so that '!' means NormalizeValue, '^'
4061% means CorrelateNormalizeValue. All other GeometryFlags values are ignored.
anthonycc6c8362010-01-25 04:14:01 +00004062%
anthony4fd27e22010-02-07 08:17:18 +00004063% The format of the ScaleKernelInfo method is:
anthonycc6c8362010-01-25 04:14:01 +00004064%
anthony999bb2c2010-02-18 12:38:01 +00004065% void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
4066% const MagickStatusType normalize_flags )
anthonycc6c8362010-01-25 04:14:01 +00004067%
4068% A description of each parameter follows:
4069%
4070% o kernel: the Morphology/Convolution kernel
4071%
anthony999bb2c2010-02-18 12:38:01 +00004072% o scaling_factor:
4073% multiply all values (after normalization) by this factor if not
4074% zero. If the kernel is normalized regardless of any flags.
4075%
4076% o normalize_flags:
4077% GeometryFlags defining normalization method to use.
4078% specifically: NormalizeValue, CorrelateNormalizeValue,
4079% and/or PercentValue
anthonycc6c8362010-01-25 04:14:01 +00004080%
4081*/
cristy6771f1e2010-03-05 19:43:39 +00004082MagickExport void ScaleKernelInfo(KernelInfo *kernel,
4083 const double scaling_factor,const GeometryFlags normalize_flags)
anthonycc6c8362010-01-25 04:14:01 +00004084{
cristybb503372010-05-27 20:51:26 +00004085 register ssize_t
anthonycc6c8362010-01-25 04:14:01 +00004086 i;
4087
anthony999bb2c2010-02-18 12:38:01 +00004088 register double
4089 pos_scale,
4090 neg_scale;
4091
anthony46a369d2010-05-19 02:41:48 +00004092 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00004093 if ( kernel->next != (KernelInfo *) NULL)
4094 ScaleKernelInfo(kernel->next, scaling_factor, normalize_flags);
4095
anthony46a369d2010-05-19 02:41:48 +00004096 /* Normalization of Kernel */
anthony999bb2c2010-02-18 12:38:01 +00004097 pos_scale = 1.0;
4098 if ( (normalize_flags&NormalizeValue) != 0 ) {
anthony999bb2c2010-02-18 12:38:01 +00004099 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
anthonyf4e00312010-05-20 12:06:35 +00004100 /* non-zero-summing kernel (generally positive) */
anthony999bb2c2010-02-18 12:38:01 +00004101 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
anthonycc6c8362010-01-25 04:14:01 +00004102 else
anthonyf4e00312010-05-20 12:06:35 +00004103 /* zero-summing kernel */
4104 pos_scale = kernel->positive_range;
anthony999bb2c2010-02-18 12:38:01 +00004105 }
anthony46a369d2010-05-19 02:41:48 +00004106 /* Force kernel into a normalized zero-summing kernel */
anthony999bb2c2010-02-18 12:38:01 +00004107 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
4108 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
4109 ? kernel->positive_range : 1.0;
4110 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
4111 ? -kernel->negative_range : 1.0;
4112 }
4113 else
4114 neg_scale = pos_scale;
4115
4116 /* finialize scaling_factor for positive and negative components */
4117 pos_scale = scaling_factor/pos_scale;
4118 neg_scale = scaling_factor/neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00004119
cristybb503372010-05-27 20:51:26 +00004120 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00004121 if ( ! IsNan(kernel->values[i]) )
anthony999bb2c2010-02-18 12:38:01 +00004122 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
anthonycc6c8362010-01-25 04:14:01 +00004123
anthony999bb2c2010-02-18 12:38:01 +00004124 /* convolution output range */
4125 kernel->positive_range *= pos_scale;
4126 kernel->negative_range *= neg_scale;
4127 /* maximum and minimum values in kernel */
4128 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
4129 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
4130
anthony46a369d2010-05-19 02:41:48 +00004131 /* swap kernel settings if user's scaling factor is negative */
anthony999bb2c2010-02-18 12:38:01 +00004132 if ( scaling_factor < MagickEpsilon ) {
4133 double t;
4134 t = kernel->positive_range;
4135 kernel->positive_range = kernel->negative_range;
4136 kernel->negative_range = t;
4137 t = kernel->maximum;
4138 kernel->maximum = kernel->minimum;
4139 kernel->minimum = 1;
4140 }
anthonycc6c8362010-01-25 04:14:01 +00004141
4142 return;
4143}
4144
4145/*
4146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4147% %
4148% %
4149% %
anthony46a369d2010-05-19 02:41:48 +00004150% S h o w K e r n e l I n f o %
anthony83ba99b2010-01-24 08:48:15 +00004151% %
4152% %
4153% %
4154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4155%
anthony4fd27e22010-02-07 08:17:18 +00004156% ShowKernelInfo() outputs the details of the given kernel defination to
4157% standard error, generally due to a users 'showkernel' option request.
anthony83ba99b2010-01-24 08:48:15 +00004158%
4159% The format of the ShowKernel method is:
4160%
anthony4fd27e22010-02-07 08:17:18 +00004161% void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00004162%
4163% A description of each parameter follows:
4164%
4165% o kernel: the Morphology/Convolution kernel
4166%
anthony83ba99b2010-01-24 08:48:15 +00004167*/
anthony4fd27e22010-02-07 08:17:18 +00004168MagickExport void ShowKernelInfo(KernelInfo *kernel)
anthony83ba99b2010-01-24 08:48:15 +00004169{
anthony7a01dcf2010-05-11 12:25:52 +00004170 KernelInfo
4171 *k;
anthony83ba99b2010-01-24 08:48:15 +00004172
cristybb503372010-05-27 20:51:26 +00004173 size_t
anthony7a01dcf2010-05-11 12:25:52 +00004174 c, i, u, v;
4175
4176 for (c=0, k=kernel; k != (KernelInfo *) NULL; c++, k=k->next ) {
4177
anthony46a369d2010-05-19 02:41:48 +00004178 fprintf(stderr, "Kernel");
anthony7a01dcf2010-05-11 12:25:52 +00004179 if ( kernel->next != (KernelInfo *) NULL )
cristyf2faecf2010-05-28 19:19:36 +00004180 fprintf(stderr, " #%lu", (unsigned long) c );
anthony43c49252010-05-18 10:59:50 +00004181 fprintf(stderr, " \"%s",
4182 MagickOptionToMnemonic(MagickKernelOptions, k->type) );
4183 if ( fabs(k->angle) > MagickEpsilon )
4184 fprintf(stderr, "@%lg", k->angle);
cristyf2faecf2010-05-28 19:19:36 +00004185 fprintf(stderr, "\" of size %lux%lu%+ld%+ld",(unsigned long) k->width,
4186 (unsigned long) k->height,(long) k->x,(long) k->y);
anthony7a01dcf2010-05-11 12:25:52 +00004187 fprintf(stderr,
4188 " with values from %.*lg to %.*lg\n",
4189 GetMagickPrecision(), k->minimum,
4190 GetMagickPrecision(), k->maximum);
anthony46a369d2010-05-19 02:41:48 +00004191 fprintf(stderr, "Forming a output range from %.*lg to %.*lg",
anthony7a01dcf2010-05-11 12:25:52 +00004192 GetMagickPrecision(), k->negative_range,
anthony46a369d2010-05-19 02:41:48 +00004193 GetMagickPrecision(), k->positive_range);
4194 if ( fabs(k->positive_range+k->negative_range) < MagickEpsilon )
4195 fprintf(stderr, " (Zero-Summing)\n");
4196 else if ( fabs(k->positive_range+k->negative_range-1.0) < MagickEpsilon )
4197 fprintf(stderr, " (Normalized)\n");
4198 else
4199 fprintf(stderr, " (Sum %.*lg)\n",
4200 GetMagickPrecision(), k->positive_range+k->negative_range);
anthony43c49252010-05-18 10:59:50 +00004201 for (i=v=0; v < k->height; v++) {
cristyf2faecf2010-05-28 19:19:36 +00004202 fprintf(stderr, "%2lu:", (unsigned long) v );
anthony43c49252010-05-18 10:59:50 +00004203 for (u=0; u < k->width; u++, i++)
anthony7a01dcf2010-05-11 12:25:52 +00004204 if ( IsNan(k->values[i]) )
anthonyf4e00312010-05-20 12:06:35 +00004205 fprintf(stderr," %*s", GetMagickPrecision()+3, "nan");
anthony7a01dcf2010-05-11 12:25:52 +00004206 else
anthonyf4e00312010-05-20 12:06:35 +00004207 fprintf(stderr," %*.*lg", GetMagickPrecision()+3,
anthony7a01dcf2010-05-11 12:25:52 +00004208 GetMagickPrecision(), k->values[i]);
4209 fprintf(stderr,"\n");
4210 }
anthony83ba99b2010-01-24 08:48:15 +00004211 }
4212}
anthonycc6c8362010-01-25 04:14:01 +00004213
4214/*
4215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4216% %
4217% %
4218% %
anthony43c49252010-05-18 10:59:50 +00004219% U n i t y A d d K e r n a l I n f o %
4220% %
4221% %
4222% %
4223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4224%
4225% UnityAddKernelInfo() Adds a given amount of the 'Unity' Convolution Kernel
4226% to the given pre-scaled and normalized Kernel. This in effect adds that
4227% amount of the original image into the resulting convolution kernel. This
4228% value is usually provided by the user as a percentage value in the
4229% 'convolve:scale' setting.
4230%
anthony501c2f92010-06-02 10:55:14 +00004231% The resulting effect is to convert the defined kernels into blended
4232% soft-blurs, unsharp kernels or into sharpening kernels.
anthony43c49252010-05-18 10:59:50 +00004233%
anthony46a369d2010-05-19 02:41:48 +00004234% The format of the UnityAdditionKernelInfo method is:
anthony43c49252010-05-18 10:59:50 +00004235%
4236% void UnityAdditionKernelInfo(KernelInfo *kernel, const double scale )
4237%
4238% A description of each parameter follows:
4239%
4240% o kernel: the Morphology/Convolution kernel
4241%
4242% o scale:
4243% scaling factor for the unity kernel to be added to
4244% the given kernel.
4245%
anthony43c49252010-05-18 10:59:50 +00004246*/
4247MagickExport void UnityAddKernelInfo(KernelInfo *kernel,
4248 const double scale)
4249{
anthony46a369d2010-05-19 02:41:48 +00004250 /* do the other kernels in a multi-kernel list first */
4251 if ( kernel->next != (KernelInfo *) NULL)
4252 UnityAddKernelInfo(kernel->next, scale);
anthony43c49252010-05-18 10:59:50 +00004253
anthony46a369d2010-05-19 02:41:48 +00004254 /* Add the scaled unity kernel to the existing kernel */
anthony43c49252010-05-18 10:59:50 +00004255 kernel->values[kernel->x+kernel->y*kernel->width] += scale;
anthony46a369d2010-05-19 02:41:48 +00004256 CalcKernelMetaData(kernel); /* recalculate the meta-data */
anthony43c49252010-05-18 10:59:50 +00004257
4258 return;
4259}
4260
4261/*
4262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4263% %
4264% %
4265% %
4266% Z e r o K e r n e l N a n s %
anthonycc6c8362010-01-25 04:14:01 +00004267% %
4268% %
4269% %
4270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4271%
4272% ZeroKernelNans() replaces any special 'nan' value that may be present in
4273% the kernel with a zero value. This is typically done when the kernel will
4274% be used in special hardware (GPU) convolution processors, to simply
4275% matters.
4276%
4277% The format of the ZeroKernelNans method is:
4278%
anthony46a369d2010-05-19 02:41:48 +00004279% void ZeroKernelNans (KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00004280%
4281% A description of each parameter follows:
4282%
4283% o kernel: the Morphology/Convolution kernel
4284%
anthonycc6c8362010-01-25 04:14:01 +00004285*/
anthonyc4c86e02010-01-27 09:30:32 +00004286MagickExport void ZeroKernelNans(KernelInfo *kernel)
anthonycc6c8362010-01-25 04:14:01 +00004287{
cristybb503372010-05-27 20:51:26 +00004288 register size_t
anthonycc6c8362010-01-25 04:14:01 +00004289 i;
4290
anthony46a369d2010-05-19 02:41:48 +00004291 /* do the other kernels in a multi-kernel list first */
anthony1b2bc0a2010-05-12 05:25:22 +00004292 if ( kernel->next != (KernelInfo *) NULL)
4293 ZeroKernelNans(kernel->next);
4294
anthony43c49252010-05-18 10:59:50 +00004295 for (i=0; i < (kernel->width*kernel->height); i++)
anthonycc6c8362010-01-25 04:14:01 +00004296 if ( IsNan(kernel->values[i]) )
4297 kernel->values[i] = 0.0;
4298
4299 return;
4300}