2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Morphology Methods %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % Morpology is the the application of various kernals, of any size and even
37 % shape, to a image in various ways (typically binary, but not always).
39 % 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.
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.
52 #include "magick/studio.h"
53 #include "magick/artifact.h"
54 #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"
59 #include "magick/gem.h"
60 #include "magick/hashmap.h"
61 #include "magick/image.h"
62 #include "magick/image-private.h"
63 #include "magick/list.h"
64 #include "magick/magick.h"
65 #include "magick/memory_.h"
66 #include "magick/monitor-private.h"
67 #include "magick/morphology.h"
68 #include "magick/option.h"
69 #include "magick/pixel-private.h"
70 #include "magick/prepress.h"
71 #include "magick/quantize.h"
72 #include "magick/registry.h"
73 #include "magick/semaphore.h"
74 #include "magick/splay-tree.h"
75 #include "magick/statistic.h"
76 #include "magick/string_.h"
77 #include "magick/string-private.h"
78 #include "magick/token.h"
81 The following test is for special floating point numbers of value NaN (not
82 a number), that may be used within a Kernel Definition. NaN's are defined
83 as part of the IEEE standard for floating point number representation.
85 These are used a Kernel value of NaN means that that kernal position is not
86 part of the normal convolution or morphology process, and thus allowing the
87 use of 'shaped' kernels.
89 Special properities two NaN's are never equal, even if they are from the
90 same variable That is the IsNaN() macro is only true if the value is NaN.
92 #define IsNan(a) ((a)!=(a))
95 Other global definitions used by module.
97 static inline double MagickMin(const double x,const double y)
99 return( x < y ? x : y);
101 static inline double MagickMax(const double x,const double y)
103 return( x > y ? x : y);
105 #define Minimize(assign,value) assign=MagickMin(assign,value)
106 #define Maximize(assign,value) assign=MagickMax(assign,value)
108 /* Currently these are only internal to this module */
110 RotateKernelInfo(KernelInfo *, double);
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % A c q u i r e K e r n e l I n f o %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % AcquireKernelInfo() takes the given string (generally supplied by the
124 % user) and converts it into a Morphology/Convolution Kernel. This allows
125 % users to specify a kernel from a number of pre-defined kernels, or to fully
126 % specify their own kernel for a specific Convolution or Morphology
129 % The kernel so generated can be any rectangular array of floating point
130 % values (doubles) with the 'control point' or 'pixel being affected'
131 % anywhere within that array of values.
133 % Previously IM was restricted to a square of odd size using the exact
134 % center as origin, this is no longer the case, and any rectangular kernel
135 % with any value being declared the origin. This in turn allows the use of
136 % highly asymmetrical kernels.
138 % The floating point values in the kernel can also include a special value
139 % known as 'nan' or 'not a number' to indicate that this value is not part
140 % of the kernel array. This allows you to shaped the kernel within its
141 % rectangular area. That is 'nan' values provide a 'mask' for the kernel
142 % shape. However at least one non-nan value must be provided for correct
143 % working of a kernel.
145 % The returned kernel should be free using the DestroyKernelInfo() when you
146 % are finished with it.
148 % Input kernel defintion strings can consist of any of three types.
151 % Select from one of the built in kernels, using the name and
152 % geometry arguments supplied. See AcquireKernelBuiltIn()
154 % "WxH[+X+Y]:num, num, num ..."
155 % a kernal of size W by H, with W*H floating point numbers following.
156 % the 'center' can be optionally be defined at +X+Y (such that +0+0
157 % is top left corner). If not defined the pixel in the center, for
158 % odd sizes, or to the immediate top or left of center for even sizes
159 % is automatically selected.
161 % "num, num, num, num, ..."
162 % list of floating point numbers defining an 'old style' odd sized
163 % square kernel. At least 9 values should be provided for a 3x3
164 % square kernel, 25 for a 5x5 square kernel, 49 for 7x7, etc.
165 % Values can be space or comma separated. This is not recommended.
167 % Note that 'name' kernels will start with an alphabetic character while the
168 % new kernel specification has a ':' character in its specification string.
169 % If neither is the case, it is assumed an old style of a simple list of
170 % numbers generating a odd-sized square kernel has been given.
172 % The format of the AcquireKernal method is:
174 % KernelInfo *AcquireKernelInfo(const char *kernel_string)
176 % A description of each parameter follows:
178 % o kernel_string: the Morphology/Convolution kernel wanted.
182 /* This was separated so that it could be used as a separate
183 ** array input handling function, such as for -color-matrix
185 static KernelInfo *ParseKernelArray(const char *kernel_string)
191 token[MaxTextExtent];
201 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
203 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
204 if (kernel == (KernelInfo *)NULL)
206 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
207 kernel->type = UserDefinedKernel;
208 kernel->signature = MagickSignature;
210 /* find end of this specific kernel definition string */
211 end = strchr(kernel_string, ';');
212 if ( end == (char *) NULL )
213 end = strchr(kernel_string, '\0');
215 /* Has a ':' in argument - New user kernel specification */
216 p = strchr(kernel_string, ':');
217 if ( p != (char *) NULL && p < end)
225 /* ParseGeometry() needs the geometry separated! -- Arrgghh */
226 memcpy(token, kernel_string, (size_t) (p-kernel_string));
227 token[p-kernel_string] = '\0';
228 SetGeometryInfo(&args);
229 flags = ParseGeometry(token, &args);
231 /* Size handling and checks of geometry settings */
232 if ( (flags & WidthValue) == 0 ) /* if no width then */
233 args.rho = args.sigma; /* then width = height */
234 if ( args.rho < 1.0 ) /* if width too small */
235 args.rho = 1.0; /* then width = 1 */
236 if ( args.sigma < 1.0 ) /* if height too small */
237 args.sigma = args.rho; /* then height = width */
238 kernel->width = (unsigned long)args.rho;
239 kernel->height = (unsigned long)args.sigma;
241 /* Offset Handling and Checks */
242 if ( args.xi < 0.0 || args.psi < 0.0 )
243 return(DestroyKernelInfo(kernel));
244 kernel->x = ((flags & XValue)!=0) ? (long)args.xi
245 : (long) (kernel->width-1)/2;
246 kernel->y = ((flags & YValue)!=0) ? (long)args.psi
247 : (long) (kernel->height-1)/2;
248 if ( kernel->x >= (long) kernel->width ||
249 kernel->y >= (long) kernel->height )
250 return(DestroyKernelInfo(kernel));
252 p++; /* advance beyond the ':' */
255 { /* ELSE - Old old specification, forming odd-square kernel */
256 /* count up number of values given */
257 p=(const char *) kernel_string;
258 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
259 p++; /* ignore "'" chars for convolve filter usage - Cristy */
260 for (i=0; p < end; i++)
262 GetMagickToken(p,&p,token);
264 GetMagickToken(p,&p,token);
266 /* set the size of the kernel - old sized square */
267 kernel->width = kernel->height= (unsigned long) sqrt((double) i+1.0);
268 kernel->x = kernel->y = (long) (kernel->width-1)/2;
269 p=(const char *) kernel_string;
270 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == '\''))
271 p++; /* ignore "'" chars for convolve filter usage - Cristy */
274 /* Read in the kernel values from rest of input string argument */
275 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
276 kernel->height*sizeof(double));
277 if (kernel->values == (double *) NULL)
278 return(DestroyKernelInfo(kernel));
280 kernel->minimum = +MagickHuge;
281 kernel->maximum = -MagickHuge;
282 kernel->negative_range = kernel->positive_range = 0.0;
284 for (i=0; (i < (long) (kernel->width*kernel->height)) && (p < end); i++)
286 GetMagickToken(p,&p,token);
288 GetMagickToken(p,&p,token);
289 if ( LocaleCompare("nan",token) == 0
290 || LocaleCompare("-",token) == 0 ) {
291 kernel->values[i] = nan; /* do not include this value in kernel */
294 kernel->values[i] = StringToDouble(token);
295 ( kernel->values[i] < 0)
296 ? ( kernel->negative_range += kernel->values[i] )
297 : ( kernel->positive_range += kernel->values[i] );
298 Minimize(kernel->minimum, kernel->values[i]);
299 Maximize(kernel->maximum, kernel->values[i]);
303 /* sanity check -- no more values in kernel definition */
304 GetMagickToken(p,&p,token);
305 if ( *token != '\0' && *token != ';' && *token != '\'' )
306 return(DestroyKernelInfo(kernel));
309 /* this was the old method of handling a incomplete kernel */
310 if ( i < (long) (kernel->width*kernel->height) ) {
311 Minimize(kernel->minimum, kernel->values[i]);
312 Maximize(kernel->maximum, kernel->values[i]);
313 for ( ; i < (long) (kernel->width*kernel->height); i++)
314 kernel->values[i]=0.0;
317 /* Number of values for kernel was not enough - Report Error */
318 if ( i < (long) (kernel->width*kernel->height) )
319 return(DestroyKernelInfo(kernel));
322 /* check that we recieved at least one real (non-nan) value! */
323 if ( kernel->minimum == MagickHuge )
324 return(DestroyKernelInfo(kernel));
329 static KernelInfo *ParseNamedKernel(const char *kernel_string)
332 token[MaxTextExtent];
346 /* Parse special 'named' kernel */
347 GetMagickToken(kernel_string,&p,token);
348 type=ParseMagickOption(MagickKernelOptions,MagickFalse,token);
349 if ( type < 0 || type == UserDefinedKernel )
350 return((KernelInfo *)NULL); /* not a valid named kernel */
352 while (((isspace((int) ((unsigned char) *p)) != 0) ||
353 (*p == ',') || (*p == ':' )) && (*p != '\0') && (*p != ';'))
355 SetGeometryInfo(&args);
356 flags = ParseGeometry(p, &args);
358 /* special handling of missing values in input string */
360 case RectangleKernel:
361 if ( (flags & WidthValue) == 0 ) /* if no width then */
362 args.rho = args.sigma; /* then width = height */
363 if ( args.rho < 1.0 ) /* if width too small */
364 args.rho = 3; /* then width = 3 */
365 if ( args.sigma < 1.0 ) /* if height too small */
366 args.sigma = args.rho; /* then height = width */
367 if ( (flags & XValue) == 0 ) /* center offset if not defined */
368 args.xi = (double)(((long)args.rho-1)/2);
369 if ( (flags & YValue) == 0 )
370 args.psi = (double)(((long)args.sigma-1)/2);
376 /* If no scale given (a 0 scale is valid! - set it to 1.0 */
377 if ( (flags & HeightValue) == 0 )
380 case ChebyshevKernel:
381 case ManhattenKernel:
382 case EuclideanKernel:
383 if ( (flags & HeightValue) == 0 )
384 args.sigma = 100.0; /* default distance scaling */
385 else if ( (flags & AspectValue ) != 0 ) /* '!' flag */
386 args.sigma = QuantumRange/args.sigma; /* maximum pixel distance */
387 else if ( (flags & PercentValue ) != 0 ) /* '%' flag */
388 args.sigma *= QuantumRange/100.0; /* percentage of color range */
394 return(AcquireKernelBuiltIn((KernelInfoType)type, &args));
397 MagickExport KernelInfo *AcquireKernelInfo(const char *kernel_string)
400 token[MaxTextExtent];
402 /* If it does not start with an alpha - Its is a user defined kernel array */
403 GetMagickToken(kernel_string,NULL,token);
404 if (isalpha((int) ((unsigned char) *token)) == 0)
405 return(ParseKernelArray(kernel_string));
407 return(ParseNamedKernel(kernel_string));
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % A c q u i r e K e r n e l B u i l t I n %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % AcquireKernelBuiltIn() returned one of the 'named' built-in types of
423 % kernels used for special purposes such as gaussian blurring, skeleton
424 % pruning, and edge distance determination.
426 % They take a KernelType, and a set of geometry style arguments, which were
427 % typically decoded from a user supplied string, or from a more complex
428 % Morphology Method that was requested.
430 % The format of the AcquireKernalBuiltIn method is:
432 % KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
433 % const GeometryInfo args)
435 % A description of each parameter follows:
437 % o type: the pre-defined type of kernel wanted
439 % o args: arguments defining or modifying the kernel
441 % Convolution Kernels
443 % Gaussian "{radius},{sigma}"
444 % Generate a two-dimentional gaussian kernel, as used by -gaussian
445 % A sigma is required, (with the 'x'), due to historical reasons.
447 % NOTE: that the 'radius' is optional, but if provided can limit (clip)
448 % the final size of the resulting kernel to a square 2*radius+1 in size.
449 % The radius should be at least 2 times that of the sigma value, or
450 % sever clipping and aliasing may result. If not given or set to 0 the
451 % radius will be determined so as to produce the best minimal error
452 % result, which is usally much larger than is normally needed.
454 % Blur "{radius},{sigma},{angle}"
455 % As per Gaussian, but generates a 1 dimensional or linear gaussian
456 % blur, at the angle given (current restricted to orthogonal angles).
457 % If a 'radius' is given the kernel is clipped to a width of 2*radius+1.
459 % NOTE that two such blurs perpendicular to each other is equivelent to
460 % -blur and the previous gaussian, but is often 10 or more times faster.
462 % Comet "{width},{sigma},{angle}"
463 % Blur in one direction only, mush like how a bright object leaves
464 % a comet like trail. The Kernel is actually half a gaussian curve,
465 % Adding two such blurs in oppiste directions produces a Linear Blur.
467 % NOTE: that the first argument is the width of the kernel and not the
468 % radius of the kernel.
470 % # Still to be implemented...
472 % # Sharpen "{radius},{sigma}
473 % # Negated Gaussian (center zeroed and re-normalized),
474 % # with a 2 unit positive peak. -- Check On line documentation
476 % # Laplacian "{radius},{sigma}"
477 % # Laplacian (a mexican hat like) Function
479 % # LOG "{radius},{sigma1},{sigma2}
480 % # Laplacian of Gaussian
482 % # DOG "{radius},{sigma1},{sigma2}
483 % # Difference of two Gaussians
487 % # Set kernel values using a resize filter, and given scale (sigma)
488 % # Cylindrical or Linear. Is this posible with an image?
493 % Rectangle "{geometry}"
494 % Simply generate a rectangle of 1's with the size given. You can also
495 % specify the location of the 'control point', otherwise the closest
496 % pixel to the center of the rectangle is selected.
498 % Properly centered and odd sized rectangles work the best.
500 % Diamond "[{radius}[,{scale}]]"
501 % Generate a diamond shaped kernal with given radius to the points.
502 % Kernel size will again be radius*2+1 square and defaults to radius 1,
503 % generating a 3x3 kernel that is slightly larger than a square.
505 % Square "[{radius}[,{scale}]]"
506 % Generate a square shaped kernel of size radius*2+1, and defaulting
507 % to a 3x3 (radius 1).
509 % Note that using a larger radius for the "Square" or the "Diamond"
510 % is also equivelent to iterating the basic morphological method
511 % that many times. However However iterating with the smaller radius 1
512 % default is actually faster than using a larger kernel radius.
514 % Disk "[{radius}[,{scale}]]
515 % Generate a binary disk of the radius given, radius may be a float.
516 % Kernel size will be ceil(radius)*2+1 square.
517 % NOTE: Here are some disk shapes of specific interest
518 % "disk:1" => "diamond" or "cross:1"
519 % "disk:1.5" => "square"
520 % "disk:2" => "diamond:2"
521 % "disk:2.5" => a general disk shape of radius 2
522 % "disk:2.9" => "square:2"
523 % "disk:3.5" => default - octagonal/disk shape of radius 3
524 % "disk:4.2" => roughly octagonal shape of radius 4
525 % "disk:4.3" => a general disk shape of radius 4
526 % After this all the kernel shape becomes more and more circular.
528 % Because a "disk" is more circular when using a larger radius, using a
529 % larger radius is preferred over iterating the morphological operation.
531 % Plus "[{radius}[,{scale}]]"
532 % Generate a kernel in the shape of a 'plus' sign. The length of each
533 % arm is also the radius, which defaults to 2.
535 % This kernel is not a good general morphological kernel, but is used
536 % more for highlighting and marking any single pixels in an image using,
537 % a "Dilate" or "Erode" method as appropriate.
539 % NOTE: "plus:1" is equivelent to a "Diamond" kernel.
541 % Note that unlike other kernels iterating a plus does not produce the
542 % same result as using a larger radius for the cross.
544 % Distance Measuring Kernels
546 % Chebyshev "[{radius}][x{scale}[%!]]"
547 % Manhatten "[{radius}][x{scale}[%!]]"
548 % Euclidean "[{radius}][x{scale}[%!]]"
550 % Different types of distance measuring methods, which are used with the
551 % a 'Distance' morphology method for generating a gradient based on
552 % distance from an edge of a binary shape, though there is a technique
553 % for handling a anti-aliased shape.
555 % Chebyshev Distance (also known as Tchebychev Distance) is a value of
556 % one to any neighbour, orthogonal or diagonal. One why of thinking of
557 % it is the number of squares a 'King' or 'Queen' in chess needs to
558 % traverse reach any other position on a chess board. It results in a
559 % 'square' like distance function, but one where diagonals are closer
562 % Manhatten Distance (also known as Rectilinear Distance, or the Taxi
563 % Cab metric), is the distance needed when you can only travel in
564 % orthogonal (horizontal or vertical) only. It is the distance a 'Rook'
565 % in chess would travel. It results in a diamond like distances, where
566 % diagonals are further than expected.
568 % Euclidean Distance is the 'direct' or 'as the crow flys distance.
569 % However by default the kernel size only has a radius of 1, which
570 % limits the distance to 'Knight' like moves, with only orthogonal and
571 % diagonal measurements being correct. As such for the default kernel
572 % you will get octagonal like distance function, which is reasonally
575 % However if you use a larger radius such as "Euclidean:4" you will
576 % get a much smoother distance gradient from the edge of the shape.
577 % Of course a larger kernel is slower to use, and generally not needed.
579 % To allow the use of fractional distances that you get with diagonals
580 % the actual distance is scaled by a fixed value which the user can
581 % provide. This is not actually nessary for either ""Chebyshev" or
582 % "Manhatten" distance kernels, but is done for all three distance
583 % kernels. If no scale is provided it is set to a value of 100,
584 % allowing for a maximum distance measurement of 655 pixels using a Q16
585 % version of IM, from any edge. However for small images this can
586 % result in quite a dark gradient.
588 % See the 'Distance' Morphological Method, for information of how it is
591 % # Hit-n-Miss Kernel-Lists -- Still to be implemented
593 % # specifically for Pruning, Thinning, Thickening
597 MagickExport KernelInfo *AcquireKernelBuiltIn(const KernelInfoType type,
598 const GeometryInfo *args)
611 nan = sqrt((double)-1.0); /* Special Value : Not A Number */
613 kernel=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
614 if (kernel == (KernelInfo *) NULL)
616 (void) ResetMagickMemory(kernel,0,sizeof(*kernel));
617 kernel->minimum = kernel->maximum = 0.0;
618 kernel->negative_range = kernel->positive_range = 0.0;
620 kernel->signature = MagickSignature;
623 /* Convolution Kernels */
626 sigma = fabs(args->sigma);
628 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
630 kernel->width = kernel->height =
631 GetOptimalKernelWidth2D(args->rho,sigma);
632 kernel->x = kernel->y = (long) (kernel->width-1)/2;
633 kernel->negative_range = kernel->positive_range = 0.0;
634 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
635 kernel->height*sizeof(double));
636 if (kernel->values == (double *) NULL)
637 return(DestroyKernelInfo(kernel));
639 sigma = 2.0*sigma*sigma; /* simplify the expression */
640 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
641 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
642 kernel->positive_range += (
644 exp(-((double)(u*u+v*v))/sigma)
645 /* / (MagickPI*sigma) */ );
647 kernel->maximum = kernel->values[
648 kernel->y*kernel->width+kernel->x ];
650 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
656 sigma = fabs(args->sigma);
658 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
660 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
661 kernel->x = (long) (kernel->width-1)/2;
664 kernel->negative_range = kernel->positive_range = 0.0;
665 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
666 kernel->height*sizeof(double));
667 if (kernel->values == (double *) NULL)
668 return(DestroyKernelInfo(kernel));
672 /* Formula derived from GetBlurKernel() in "effect.c" (plus bug fix).
673 ** It generates a gaussian 3 times the width, and compresses it into
674 ** the expected range. This produces a closer normalization of the
675 ** resulting kernel, especially for very low sigma values.
676 ** As such while wierd it is prefered.
678 ** I am told this method originally came from Photoshop.
680 sigma *= KernelRank; /* simplify expanded curve */
681 v = (long) (kernel->width*KernelRank-1)/2; /* start/end points to fit range */
682 (void) ResetMagickMemory(kernel->values,0, (size_t)
683 kernel->width*sizeof(double));
684 for ( u=-v; u <= v; u++) {
685 kernel->values[(u+v)/KernelRank] +=
686 exp(-((double)(u*u))/(2.0*sigma*sigma))
687 /* / (MagickSQ2PI*sigma/KernelRank) */ ;
689 for (i=0; i < (long) kernel->width; i++)
690 kernel->positive_range += kernel->values[i];
692 for ( i=0, u=-kernel->x; i < kernel->width; i++, u++)
693 kernel->positive_range += (
695 exp(-((double)(u*u))/(2.0*sigma*sigma))
696 /* / (MagickSQ2PI*sigma) */ );
699 kernel->maximum = kernel->values[ kernel->x ];
700 /* Note that neither methods above generate a normalized kernel,
701 ** though it gets close. The kernel may be 'clipped' by a user defined
702 ** radius, producing a smaller (darker) kernel. Also for very small
703 ** sigma's (> 0.1) the central value becomes larger than one, and thus
704 ** producing a very bright kernel.
707 /* Normalize the 1D Gaussian Kernel
709 ** Because of this the divisor in the above kernel generator is
710 ** not needed, so is not done above.
712 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
714 /* rotate the kernel by given angle */
715 RotateKernelInfo(kernel, args->xi);
720 sigma = fabs(args->sigma);
722 sigma = (sigma <= MagickEpsilon) ? 1.0 : sigma;
724 if ( args->rho < 1.0 )
725 kernel->width = GetOptimalKernelWidth1D(args->rho,sigma);
727 kernel->width = (unsigned long)args->rho;
728 kernel->x = kernel->y = 0;
730 kernel->negative_range = kernel->positive_range = 0.0;
731 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
732 kernel->height*sizeof(double));
733 if (kernel->values == (double *) NULL)
734 return(DestroyKernelInfo(kernel));
736 /* A comet blur is half a gaussian curve, so that the object is
737 ** blurred in one direction only. This may not be quite the right
738 ** curve so may change in the future. The function must be normalised.
742 sigma *= KernelRank; /* simplify expanded curve */
743 v = (long) kernel->width*KernelRank; /* start/end points to fit range */
744 (void) ResetMagickMemory(kernel->values,0, (size_t)
745 kernel->width*sizeof(double));
746 for ( u=0; u < v; u++) {
747 kernel->values[u/KernelRank] +=
748 exp(-((double)(u*u))/(2.0*sigma*sigma))
749 /* / (MagickSQ2PI*sigma/KernelRank) */ ;
751 for (i=0; i < (long) kernel->width; i++)
752 kernel->positive_range += kernel->values[i];
754 for ( i=0; i < (long) kernel->width; i++)
755 kernel->positive_range += (
757 exp(-((double)(i*i))/(2.0*sigma*sigma))
758 /* / (MagickSQ2PI*sigma) */ );
761 kernel->maximum = kernel->values[0];
763 ScaleKernelInfo(kernel, 1.0, NormalizeValue); /* Normalize */
764 RotateKernelInfo(kernel, args->xi); /* Rotate by angle */
767 /* Boolean Kernels */
768 case RectangleKernel:
772 if ( type == SquareKernel )
775 kernel->width = kernel->height = 3; /* default radius = 1 */
777 kernel->width = kernel->height = (unsigned long) (2*args->rho+1);
778 kernel->x = kernel->y = (long) (kernel->width-1)/2;
782 /* NOTE: user defaults set in "AcquireKernelInfo()" */
783 if ( args->rho < 1.0 || args->sigma < 1.0 )
784 return(DestroyKernelInfo(kernel)); /* invalid args given */
785 kernel->width = (unsigned long)args->rho;
786 kernel->height = (unsigned long)args->sigma;
787 if ( args->xi < 0.0 || args->xi > (double)kernel->width ||
788 args->psi < 0.0 || args->psi > (double)kernel->height )
789 return(DestroyKernelInfo(kernel)); /* invalid args given */
790 kernel->x = (long) args->xi;
791 kernel->y = (long) args->psi;
794 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
795 kernel->height*sizeof(double));
796 if (kernel->values == (double *) NULL)
797 return(DestroyKernelInfo(kernel));
799 /* set all kernel values to 1.0 */
800 u=(long) kernel->width*kernel->height;
801 for ( i=0; i < u; i++)
802 kernel->values[i] = scale;
803 kernel->minimum = kernel->maximum = scale; /* a flat shape */
804 kernel->positive_range = scale*u;
810 kernel->width = kernel->height = 3; /* default radius = 1 */
812 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
813 kernel->x = kernel->y = (long) (kernel->width-1)/2;
815 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
816 kernel->height*sizeof(double));
817 if (kernel->values == (double *) NULL)
818 return(DestroyKernelInfo(kernel));
820 /* set all kernel values within diamond area to scale given */
821 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
822 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
823 if ((labs(u)+labs(v)) <= (long)kernel->x)
824 kernel->positive_range += kernel->values[i] = args->sigma;
826 kernel->values[i] = nan;
827 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
835 limit = (long)(args->rho*args->rho);
836 if (args->rho < 0.1) /* default radius approx 3.5 */
837 kernel->width = kernel->height = 7L, limit = 10L;
839 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
840 kernel->x = kernel->y = (long) (kernel->width-1)/2;
842 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
843 kernel->height*sizeof(double));
844 if (kernel->values == (double *) NULL)
845 return(DestroyKernelInfo(kernel));
847 /* set all kernel values within disk area to 1.0 */
848 for ( i=0, v= -kernel->y; v <= (long)kernel->y; v++)
849 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
850 if ((u*u+v*v) <= limit)
851 kernel->positive_range += kernel->values[i] = args->sigma;
853 kernel->values[i] = nan;
854 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
860 kernel->width = kernel->height = 5; /* default radius 2 */
862 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
863 kernel->x = kernel->y = (long) (kernel->width-1)/2;
865 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
866 kernel->height*sizeof(double));
867 if (kernel->values == (double *) NULL)
868 return(DestroyKernelInfo(kernel));
870 /* set all kernel values along axises to 1.0 */
871 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
872 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
873 kernel->values[i] = (u == 0 || v == 0) ? args->sigma : nan;
874 kernel->minimum = kernel->maximum = args->sigma; /* a flat shape */
875 kernel->positive_range = args->sigma*(kernel->width*2.0 - 1.0);
878 /* Distance Measuring Kernels */
879 case ChebyshevKernel:
882 kernel->width = kernel->height = 3; /* default radius = 1 */
884 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
885 kernel->x = kernel->y = (long) (kernel->width-1)/2;
887 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
888 kernel->height*sizeof(double));
889 if (kernel->values == (double *) NULL)
890 return(DestroyKernelInfo(kernel));
892 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
893 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
894 kernel->positive_range += ( kernel->values[i] =
895 args->sigma*((labs(u)>labs(v)) ? labs(u) : labs(v)) );
896 kernel->maximum = kernel->values[0];
899 case ManhattenKernel:
902 kernel->width = kernel->height = 3; /* default radius = 1 */
904 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
905 kernel->x = kernel->y = (long) (kernel->width-1)/2;
907 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
908 kernel->height*sizeof(double));
909 if (kernel->values == (double *) NULL)
910 return(DestroyKernelInfo(kernel));
912 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
913 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
914 kernel->positive_range += ( kernel->values[i] =
915 args->sigma*(labs(u)+labs(v)) );
916 kernel->maximum = kernel->values[0];
919 case EuclideanKernel:
922 kernel->width = kernel->height = 3; /* default radius = 1 */
924 kernel->width = kernel->height = ((unsigned long)args->rho)*2+1;
925 kernel->x = kernel->y = (long) (kernel->width-1)/2;
927 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
928 kernel->height*sizeof(double));
929 if (kernel->values == (double *) NULL)
930 return(DestroyKernelInfo(kernel));
932 for ( i=0, v=-kernel->y; v <= (long)kernel->y; v++)
933 for ( u=-kernel->x; u <= (long)kernel->x; u++, i++)
934 kernel->positive_range += ( kernel->values[i] =
935 args->sigma*sqrt((double)(u*u+v*v)) );
936 kernel->maximum = kernel->values[0];
939 /* Undefined Kernels */
940 case LaplacianKernel:
943 perror("Kernel Type has not been defined yet");
946 /* Generate a No-Op minimal kernel - 1x1 pixel */
947 kernel->values=(double *)AcquireQuantumMemory((size_t)1,sizeof(double));
948 if (kernel->values == (double *) NULL)
949 return(DestroyKernelInfo(kernel));
950 kernel->width = kernel->height = 1;
951 kernel->x = kernel->x = 0;
952 kernel->type = UndefinedKernel;
954 kernel->positive_range =
955 kernel->values[0] = 1.0; /* a flat single-point no-op kernel! */
963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967 % C l o n e K e r n e l I n f o %
971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % CloneKernelInfo() creates a new clone of the given Kernel so that its can
974 % be modified without effecting the original. The cloned kernel should be
975 % destroyed using DestoryKernelInfo() when no longer needed.
977 % The format of the CloneKernelInfo method is:
979 % KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
981 % A description of each parameter follows:
983 % o kernel: the Morphology/Convolution kernel to be cloned
986 MagickExport KernelInfo *CloneKernelInfo(const KernelInfo *kernel)
994 assert(kernel != (KernelInfo *) NULL);
995 kernel_info=(KernelInfo *) AcquireMagickMemory(sizeof(*kernel));
996 if (kernel_info == (KernelInfo *) NULL)
998 *kernel_info=(*kernel); /* copy values in structure */
999 kernel_info->values=(double *) AcquireQuantumMemory(kernel->width,
1000 kernel->height*sizeof(double));
1001 if (kernel_info->values == (double *) NULL)
1002 return(DestroyKernelInfo(kernel_info));
1003 for (i=0; i < (long) (kernel->width*kernel->height); i++)
1004 kernel_info->values[i]=kernel->values[i];
1005 return(kernel_info);
1009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013 % D e s t r o y K e r n e l I n f o %
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019 % DestroyKernelInfo() frees the memory used by a Convolution/Morphology
1022 % The format of the DestroyKernelInfo method is:
1024 % KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
1026 % A description of each parameter follows:
1028 % o kernel: the Morphology/Convolution kernel to be destroyed
1032 MagickExport KernelInfo *DestroyKernelInfo(KernelInfo *kernel)
1034 assert(kernel != (KernelInfo *) NULL);
1036 kernel->values=(double *) AcquireQuantumMemory(kernel->width,
1037 kernel->height*sizeof(double));
1038 kernel->values=(double *)RelinquishMagickMemory(kernel->values);
1039 kernel=(KernelInfo *) RelinquishMagickMemory(kernel);
1044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048 % M o r p h o l o g y I m a g e C h a n n e l %
1052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054 % MorphologyImageChannel() applies a user supplied kernel to the image
1055 % according to the given mophology method.
1057 % The given kernel is assumed to have been pre-scaled appropriatally, usally
1058 % by the kernel generator.
1060 % The format of the MorphologyImage method is:
1062 % Image *MorphologyImage(const Image *image,MorphologyMethod method,
1063 % const long iterations,KernelInfo *kernel,ExceptionInfo *exception)
1064 % Image *MorphologyImageChannel(const Image *image, const ChannelType
1065 % channel,MorphologyMethod method,const long iterations,
1066 % KernelInfo *kernel,ExceptionInfo *exception)
1068 % A description of each parameter follows:
1070 % o image: the image.
1072 % o method: the morphology method to be applied.
1074 % o iterations: apply the operation this many times (or no change).
1075 % A value of -1 means loop until no change found.
1076 % How this is applied may depend on the morphology method.
1077 % Typically this is a value of 1.
1079 % o channel: the channel type.
1081 % o kernel: An array of double representing the morphology kernel.
1082 % Warning: kernel may be normalized for the Convolve method.
1084 % o exception: return any errors or warnings in this structure.
1087 % TODO: bias and auto-scale handling of the kernel for convolution
1088 % The given kernel is assumed to have been pre-scaled appropriatally, usally
1089 % by the kernel generator.
1094 /* Internal function
1095 * Apply the Low-Level Morphology Method using the given Kernel
1096 * Returning the number of pixels that changed.
1097 * Two pre-created images must be provided, no image is created.
1099 static unsigned long MorphologyApply(const Image *image, Image
1100 *result_image, const MorphologyMethod method, const ChannelType channel,
1101 const KernelInfo *kernel, ExceptionInfo *exception)
1103 #define MorphologyTag "Morphology/Image"
1120 /* Only the most basic morphology is actually performed by this routine */
1123 Apply Basic Morphology to Image.
1129 GetMagickPixelPacket(image,&bias);
1130 SetMagickPixelPacketBias(image,&bias);
1131 /* Future: handle auto-bias from user, based on kernel input */
1133 p_view=AcquireCacheView(image);
1134 q_view=AcquireCacheView(result_image);
1136 /* Some methods (including convolve) needs use a reflected kernel.
1137 * Adjust 'origin' offsets for this reflected kernel.
1142 case ConvolveMorphology:
1143 case DilateMorphology:
1144 case DilateIntensityMorphology:
1145 case DistanceMorphology:
1146 /* kernel needs to used with reflection about origin */
1147 offx = (long) kernel->width-offx-1;
1148 offy = (long) kernel->height-offy-1;
1150 case ErodeMorphology:
1151 case ErodeIntensityMorphology:
1152 case HitAndMissMorphology:
1153 case ThinningMorphology:
1154 case ThickenMorphology:
1155 /* kernel is user as is, without reflection */
1158 perror("Not a low level Morphology Method");
1162 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1163 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1165 for (y=0; y < (long) image->rows; y++)
1170 register const PixelPacket
1173 register const IndexPacket
1174 *restrict p_indexes;
1176 register PixelPacket
1179 register IndexPacket
1180 *restrict q_indexes;
1188 if (status == MagickFalse)
1190 p=GetCacheViewVirtualPixels(p_view, -offx, y-offy,
1191 image->columns+kernel->width, kernel->height, exception);
1192 q=GetCacheViewAuthenticPixels(q_view,0,y,result_image->columns,1,
1194 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1199 p_indexes=GetCacheViewVirtualIndexQueue(p_view);
1200 q_indexes=GetCacheViewAuthenticIndexQueue(q_view);
1201 r = (image->columns+kernel->width)*offy+offx; /* constant */
1203 for (x=0; x < (long) image->columns; x++)
1211 register const double
1214 register const PixelPacket
1217 register const IndexPacket
1218 *restrict k_indexes;
1225 /* Copy input to ouput image for unused channels
1226 * This removes need for 'cloning' a new image every iteration
1229 if (image->colorspace == CMYKColorspace)
1230 q_indexes[x] = p_indexes[r];
1237 min.index = (MagickRealType) QuantumRange;
1242 max.index = (MagickRealType) 0;
1243 /* original pixel value */
1244 result.red = (MagickRealType) p[r].red;
1245 result.green = (MagickRealType) p[r].green;
1246 result.blue = (MagickRealType) p[r].blue;
1247 result.opacity = QuantumRange - (MagickRealType) p[r].opacity;
1248 if ( image->colorspace == CMYKColorspace)
1249 result.index = (MagickRealType) p_indexes[r];
1252 case ConvolveMorphology:
1253 /* Set the user defined bias of the weighted average output
1255 ** FUTURE: provide some way for internal functions to disable
1256 ** user provided bias and scaling effects.
1260 case DilateIntensityMorphology:
1261 case ErodeIntensityMorphology:
1262 result.red = 0.0; /* flag indicating when first match found */
1269 case ConvolveMorphology:
1270 /* Weighted Average of pixels using reflected kernel
1272 ** NOTE for correct working of this operation for asymetrical
1273 ** kernels, the kernel needs to be applied in its reflected form.
1274 ** That is its values needs to be reversed.
1276 ** Correlation is actually the same as this but without reflecting
1277 ** the kernel, and thus 'lower-level' that Convolution. However
1278 ** as Convolution is the more common method used, and it does not
1279 ** really cost us much in terms of processing to use a reflected
1280 ** kernel, so it is Convolution that is implemented.
1282 ** Correlation will have its kernel reflected before calling
1283 ** this function to do a Convolve.
1285 ** For more details of Correlation vs Convolution see
1286 ** http://www.cs.umd.edu/~djacobs/CMSC426/Convolution.pdf
1288 if (((channel & SyncChannels) != 0 ) &&
1289 (image->matte == MagickTrue))
1290 { /* Channel has a 'Sync' Flag, and Alpha Channel enabled.
1291 ** Weight the color channels with Alpha Channel so that
1292 ** transparent pixels are not part of the results.
1295 alpha, /* color channel weighting : kernel*alpha */
1296 gamma; /* divisor, sum of weighting values */
1299 k = &kernel->values[ kernel->width*kernel->height-1 ];
1301 k_indexes = p_indexes;
1302 for (v=0; v < (long) kernel->height; v++) {
1303 for (u=0; u < (long) kernel->width; u++, k--) {
1304 if ( IsNan(*k) ) continue;
1305 alpha=(*k)*(QuantumScale*(QuantumRange-
1306 k_pixels[u].opacity));
1308 result.red += alpha*k_pixels[u].red;
1309 result.green += alpha*k_pixels[u].green;
1310 result.blue += alpha*k_pixels[u].blue;
1311 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
1312 if ( image->colorspace == CMYKColorspace)
1313 result.index += alpha*k_indexes[u];
1315 k_pixels += image->columns+kernel->width;
1316 k_indexes += image->columns+kernel->width;
1318 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1319 result.red *= gamma;
1320 result.green *= gamma;
1321 result.blue *= gamma;
1322 result.opacity *= gamma;
1323 result.index *= gamma;
1327 /* No 'Sync' flag, or no Alpha involved.
1328 ** Convolution is simple individual channel weigthed sum.
1330 k = &kernel->values[ kernel->width*kernel->height-1 ];
1332 k_indexes = p_indexes;
1333 for (v=0; v < (long) kernel->height; v++) {
1334 for (u=0; u < (long) kernel->width; u++, k--) {
1335 if ( IsNan(*k) ) continue;
1336 result.red += (*k)*k_pixels[u].red;
1337 result.green += (*k)*k_pixels[u].green;
1338 result.blue += (*k)*k_pixels[u].blue;
1339 result.opacity += (*k)*(QuantumRange-k_pixels[u].opacity);
1340 if ( image->colorspace == CMYKColorspace)
1341 result.index += (*k)*k_indexes[u];
1343 k_pixels += image->columns+kernel->width;
1344 k_indexes += image->columns+kernel->width;
1349 case ErodeMorphology:
1350 /* Minimum Value within kernel neighbourhood
1352 ** NOTE that the kernel is not reflected for this operation!
1354 ** NOTE: in normal Greyscale Morphology, the kernel value should
1355 ** be added to the real value, this is currently not done, due to
1356 ** the nature of the boolean kernels being used.
1360 k_indexes = p_indexes;
1361 for (v=0; v < (long) kernel->height; v++) {
1362 for (u=0; u < (long) kernel->width; u++, k++) {
1363 if ( IsNan(*k) || (*k) < 0.5 ) continue;
1364 Minimize(min.red, (double) k_pixels[u].red);
1365 Minimize(min.green, (double) k_pixels[u].green);
1366 Minimize(min.blue, (double) k_pixels[u].blue);
1367 Minimize(min.opacity,
1368 QuantumRange-(double) k_pixels[u].opacity);
1369 if ( image->colorspace == CMYKColorspace)
1370 Minimize(min.index, (double) k_indexes[u]);
1372 k_pixels += image->columns+kernel->width;
1373 k_indexes += image->columns+kernel->width;
1378 case DilateMorphology:
1379 /* Maximum Value within kernel neighbourhood
1381 ** NOTE for correct working of this operation for asymetrical
1382 ** kernels, the kernel needs to be applied in its reflected form.
1383 ** That is its values needs to be reversed.
1385 ** NOTE: in normal Greyscale Morphology, the kernel value should
1386 ** be added to the real value, this is currently not done, due to
1387 ** the nature of the boolean kernels being used.
1390 k = &kernel->values[ kernel->width*kernel->height-1 ];
1392 k_indexes = p_indexes;
1393 for (v=0; v < (long) kernel->height; v++) {
1394 for (u=0; u < (long) kernel->width; u++, k--) {
1395 if ( IsNan(*k) || (*k) < 0.5 ) continue;
1396 Maximize(max.red, (double) k_pixels[u].red);
1397 Maximize(max.green, (double) k_pixels[u].green);
1398 Maximize(max.blue, (double) k_pixels[u].blue);
1399 Maximize(max.opacity,
1400 QuantumRange-(double) k_pixels[u].opacity);
1401 if ( image->colorspace == CMYKColorspace)
1402 Maximize(max.index, (double) k_indexes[u]);
1404 k_pixels += image->columns+kernel->width;
1405 k_indexes += image->columns+kernel->width;
1409 case HitAndMissMorphology:
1410 case ThinningMorphology:
1411 case ThickenMorphology:
1412 /* Minimum of Foreground Pixel minus Maxumum of Background Pixels
1414 ** NOTE that the kernel is not reflected for this operation,
1415 ** and consists of both foreground and background pixel
1416 ** neighbourhoods, 0.0 for background, and 1.0 for foreground
1417 ** with either Nan or 0.5 values for don't care.
1419 ** Note that this can produce negative results, though really
1420 ** only a positive match has any real value.
1424 k_indexes = p_indexes;
1425 for (v=0; v < (long) kernel->height; v++) {
1426 for (u=0; u < (long) kernel->width; u++, k++) {
1427 if ( IsNan(*k) ) continue;
1429 { /* minimim of foreground pixels */
1430 Minimize(min.red, (double) k_pixels[u].red);
1431 Minimize(min.green, (double) k_pixels[u].green);
1432 Minimize(min.blue, (double) k_pixels[u].blue);
1433 Minimize(min.opacity,
1434 QuantumRange-(double) k_pixels[u].opacity);
1435 if ( image->colorspace == CMYKColorspace)
1436 Minimize(min.index, (double) k_indexes[u]);
1438 else if ( (*k) < 0.3 )
1439 { /* maximum of background pixels */
1440 Maximize(max.red, (double) k_pixels[u].red);
1441 Maximize(max.green, (double) k_pixels[u].green);
1442 Maximize(max.blue, (double) k_pixels[u].blue);
1443 Maximize(max.opacity,
1444 QuantumRange-(double) k_pixels[u].opacity);
1445 if ( image->colorspace == CMYKColorspace)
1446 Maximize(max.index, (double) k_indexes[u]);
1449 k_pixels += image->columns+kernel->width;
1450 k_indexes += image->columns+kernel->width;
1452 /* Pattern Match only if min fg larger than min bg pixels */
1453 min.red -= max.red; Maximize( min.red, 0.0 );
1454 min.green -= max.green; Maximize( min.green, 0.0 );
1455 min.blue -= max.blue; Maximize( min.blue, 0.0 );
1456 min.opacity -= max.opacity; Maximize( min.opacity, 0.0 );
1457 min.index -= max.index; Maximize( min.index, 0.0 );
1460 case ErodeIntensityMorphology:
1461 /* Select Pixel with Minimum Intensity within kernel neighbourhood
1463 ** WARNING: the intensity test fails for CMYK and does not
1464 ** take into account the moderating effect of teh alpha channel
1465 ** on the intensity.
1467 ** NOTE that the kernel is not reflected for this operation!
1471 k_indexes = p_indexes;
1472 for (v=0; v < (long) kernel->height; v++) {
1473 for (u=0; u < (long) kernel->width; u++, k++) {
1474 if ( IsNan(*k) || (*k) < 0.5 ) continue;
1475 if ( result.red == 0.0 ||
1476 PixelIntensity(&(k_pixels[u])) < PixelIntensity(q) ) {
1477 /* copy the whole pixel - no channel selection */
1479 if ( result.red > 0.0 ) changed++;
1483 k_pixels += image->columns+kernel->width;
1484 k_indexes += image->columns+kernel->width;
1488 case DilateIntensityMorphology:
1489 /* Select Pixel with Maximum Intensity within kernel neighbourhood
1491 ** WARNING: the intensity test fails for CMYK and does not
1492 ** take into account the moderating effect of teh alpha channel
1493 ** on the intensity.
1495 ** NOTE for correct working of this operation for asymetrical
1496 ** kernels, the kernel needs to be applied in its reflected form.
1497 ** That is its values needs to be reversed.
1499 k = &kernel->values[ kernel->width*kernel->height-1 ];
1501 k_indexes = p_indexes;
1502 for (v=0; v < (long) kernel->height; v++) {
1503 for (u=0; u < (long) kernel->width; u++, k--) {
1504 if ( IsNan(*k) || (*k) < 0.5 ) continue; /* boolean kernel */
1505 if ( result.red == 0.0 ||
1506 PixelIntensity(&(k_pixels[u])) > PixelIntensity(q) ) {
1507 /* copy the whole pixel - no channel selection */
1509 if ( result.red > 0.0 ) changed++;
1513 k_pixels += image->columns+kernel->width;
1514 k_indexes += image->columns+kernel->width;
1519 case DistanceMorphology:
1520 /* Add kernel Value and select the minimum value found.
1521 ** The result is a iterative distance from edge of image shape.
1523 ** All Distance Kernels are symetrical, but that may not always
1524 ** be the case. For example how about a distance from left edges?
1525 ** To work correctly with asymetrical kernels the reflected kernel
1526 ** needs to be applied.
1528 ** Actually this is really a GreyErode with a negative kernel!
1532 /* No need to do distance morphology if original value is zero
1533 ** Unfortunatally I have not been able to get this right
1534 ** when channel selection also becomes involved. -- Arrgghhh
1536 if ( ((channel & RedChannel) == 0 && p[r].red == 0)
1537 || ((channel & GreenChannel) == 0 && p[r].green == 0)
1538 || ((channel & BlueChannel) == 0 && p[r].blue == 0)
1539 || ((channel & OpacityChannel) == 0 && p[r].opacity == 0)
1540 || (( (channel & IndexChannel) == 0
1541 || image->colorspace != CMYKColorspace
1542 ) && p_indexes[x] ==0 )
1546 k = &kernel->values[ kernel->width*kernel->height-1 ];
1548 k_indexes = p_indexes;
1549 for (v=0; v < (long) kernel->height; v++) {
1550 for (u=0; u < (long) kernel->width; u++, k--) {
1551 if ( IsNan(*k) ) continue;
1552 Minimize(result.red, (*k)+k_pixels[u].red);
1553 Minimize(result.green, (*k)+k_pixels[u].green);
1554 Minimize(result.blue, (*k)+k_pixels[u].blue);
1555 Minimize(result.opacity, (*k)+QuantumRange-k_pixels[u].opacity);
1556 if ( image->colorspace == CMYKColorspace)
1557 Minimize(result.index, (*k)+k_indexes[u]);
1559 k_pixels += image->columns+kernel->width;
1560 k_indexes += image->columns+kernel->width;
1564 case UndefinedMorphology:
1566 break; /* Do nothing */
1568 /* Final mathematics of results (combine with original image?)
1570 ** NOTE: Difference Morphology operators Edge* and *Hat could also
1571 ** be done here but works better with iteration as a image difference
1572 ** in the controling function (below). Thicken and Thinning however
1573 ** should be done here so thay can be iterated correctly.
1576 case HitAndMissMorphology:
1577 case ErodeMorphology:
1578 result = min; /* minimum of neighbourhood */
1580 case DilateMorphology:
1581 result = max; /* maximum of neighbourhood */
1583 case ThinningMorphology:
1584 /* subtract pattern match from original */
1585 result.red -= min.red;
1586 result.green -= min.green;
1587 result.blue -= min.blue;
1588 result.opacity -= min.opacity;
1589 result.index -= min.index;
1591 case ThickenMorphology:
1592 /* Union with original image (maximize) - or should this be + */
1593 Maximize( result.red, min.red );
1594 Maximize( result.green, min.green );
1595 Maximize( result.blue, min.blue );
1596 Maximize( result.opacity, min.opacity );
1597 Maximize( result.index, min.index );
1600 /* result directly calculated or assigned */
1603 /* Assign the resulting pixel values - Clamping Result */
1605 case UndefinedMorphology:
1606 case DilateIntensityMorphology:
1607 case ErodeIntensityMorphology:
1608 break; /* full pixel was directly assigned - not a channel method */
1610 if ((channel & RedChannel) != 0)
1611 q->red = ClampToQuantum(result.red);
1612 if ((channel & GreenChannel) != 0)
1613 q->green = ClampToQuantum(result.green);
1614 if ((channel & BlueChannel) != 0)
1615 q->blue = ClampToQuantum(result.blue);
1616 if ((channel & OpacityChannel) != 0
1617 && image->matte == MagickTrue )
1618 q->opacity = ClampToQuantum(QuantumRange-result.opacity);
1619 if ((channel & IndexChannel) != 0
1620 && image->colorspace == CMYKColorspace)
1621 q_indexes[x] = ClampToQuantum(result.index);
1624 /* Count up changed pixels */
1625 if ( ( p[r].red != q->red )
1626 || ( p[r].green != q->green )
1627 || ( p[r].blue != q->blue )
1628 || ( p[r].opacity != q->opacity )
1629 || ( image->colorspace == CMYKColorspace &&
1630 p_indexes[r] != q_indexes[x] ) )
1631 changed++; /* The pixel had some value changed! */
1635 sync=SyncCacheViewAuthenticPixels(q_view,exception);
1636 if (sync == MagickFalse)
1638 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1643 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1644 #pragma omp critical (MagickCore_MorphologyImage)
1646 proceed=SetImageProgress(image,MorphologyTag,progress++,image->rows);
1647 if (proceed == MagickFalse)
1651 result_image->type=image->type;
1652 q_view=DestroyCacheView(q_view);
1653 p_view=DestroyCacheView(p_view);
1654 return(status ? (unsigned long) changed : 0);
1658 MagickExport Image *MorphologyImage(const Image *image, const MorphologyMethod
1659 method, const long iterations,const KernelInfo *kernel, ExceptionInfo
1665 morphology_image=MorphologyImageChannel(image,DefaultChannels,method,
1666 iterations,kernel,exception);
1667 return(morphology_image);
1671 MagickExport Image *MorphologyImageChannel(const Image *image,
1672 const ChannelType channel,const MorphologyMethod method,
1673 const long iterations,const KernelInfo *kernel,ExceptionInfo *exception)
1696 assert(image != (Image *) NULL);
1697 assert(image->signature == MagickSignature);
1698 assert(kernel != (KernelInfo *) NULL);
1699 assert(kernel->signature == MagickSignature);
1700 assert(exception != (ExceptionInfo *) NULL);
1701 assert(exception->signature == MagickSignature);
1703 if ( iterations == 0 )
1704 return((Image *)NULL); /* null operation - nothing to do! */
1706 /* kernel must be valid at this point
1707 * (except maybe for posible future morphology methods like "Prune"
1709 assert(kernel != (KernelInfo *)NULL);
1711 count = 0; /* interation count */
1712 changed = 1; /* if compound method assume image was changed */
1713 curr_kernel = (KernelInfo *)kernel; /* allow kernel and method */
1714 curr_method = method; /* to be changed as nessary */
1716 limit = (unsigned long) iterations;
1717 if ( iterations < 0 )
1718 limit = image->columns > image->rows ? image->columns : image->rows;
1720 /* Third-level morphology methods */
1721 grad_image=(Image *) NULL;
1722 switch( curr_method ) {
1723 case EdgeMorphology:
1724 grad_image = MorphologyImageChannel(image, channel,
1725 DilateMorphology, iterations, curr_kernel, exception);
1727 case EdgeInMorphology:
1728 curr_method = ErodeMorphology;
1730 case EdgeOutMorphology:
1731 curr_method = DilateMorphology;
1733 case TopHatMorphology:
1734 curr_method = OpenMorphology;
1736 case BottomHatMorphology:
1737 curr_method = CloseMorphology;
1740 break; /* not a third-level method */
1743 /* Second-level morphology methods */
1744 switch( curr_method ) {
1745 case OpenMorphology:
1746 /* Open is a Erode then a Dilate without reflection */
1747 new_image = MorphologyImageChannel(image, channel,
1748 ErodeMorphology, iterations, curr_kernel, exception);
1749 if (new_image == (Image *) NULL)
1750 return((Image *) NULL);
1751 curr_method = DilateMorphology;
1753 case OpenIntensityMorphology:
1754 new_image = MorphologyImageChannel(image, channel,
1755 ErodeIntensityMorphology, iterations, curr_kernel, exception);
1756 if (new_image == (Image *) NULL)
1757 return((Image *) NULL);
1758 curr_method = DilateIntensityMorphology;
1761 case CloseMorphology:
1762 /* Close is a Dilate then Erode using reflected kernel */
1763 /* A reflected kernel is needed for a Close */
1764 if ( curr_kernel == kernel )
1765 curr_kernel = CloneKernelInfo(kernel);
1766 RotateKernelInfo(curr_kernel,180);
1767 new_image = MorphologyImageChannel(image, channel,
1768 DilateMorphology, iterations, curr_kernel, exception);
1769 if (new_image == (Image *) NULL)
1770 return((Image *) NULL);
1771 curr_method = ErodeMorphology;
1773 case CloseIntensityMorphology:
1774 /* A reflected kernel is needed for a Close */
1775 if ( curr_kernel == kernel )
1776 curr_kernel = CloneKernelInfo(kernel);
1777 RotateKernelInfo(curr_kernel,180);
1778 new_image = MorphologyImageChannel(image, channel,
1779 DilateIntensityMorphology, iterations, curr_kernel, exception);
1780 if (new_image == (Image *) NULL)
1781 return((Image *) NULL);
1782 curr_method = ErodeIntensityMorphology;
1785 case CorrelateMorphology:
1786 /* A Correlation is actually a Convolution with a reflected kernel.
1787 ** However a Convolution is a weighted sum with a reflected kernel.
1788 ** It may seem stange to convert a Correlation into a Convolution
1789 ** as the Correleation is the simplier method, but Convolution is
1790 ** much more commonly used, and it makes sense to implement it directly
1791 ** so as to avoid the need to duplicate the kernel when it is not
1792 ** required (which is typically the default).
1794 if ( curr_kernel == kernel )
1795 curr_kernel = CloneKernelInfo(kernel);
1796 RotateKernelInfo(curr_kernel,180);
1797 curr_method = ConvolveMorphology;
1798 /* FALL-THRU into Correlate (weigthed sum without reflection) */
1800 case ConvolveMorphology:
1801 /* Scale or Normalize kernel, according to user wishes
1802 ** before using it for the Convolve/Correlate method.
1804 ** FUTURE: provide some way for internal functions to disable
1805 ** user bias and scaling effects.
1807 artifact = GetImageArtifact(image,"convolve:scale");
1808 if ( artifact != (char *)NULL ) {
1814 if ( curr_kernel == kernel )
1815 curr_kernel = CloneKernelInfo(kernel);
1818 flags = (GeometryFlags) ParseGeometry(artifact, &args);
1819 ScaleKernelInfo(curr_kernel, args.rho, flags);
1821 /* FALL-THRU to do the first, and typically the only iteration */
1824 /* Do a single iteration using the Low-Level Morphology method!
1825 ** This ensures a "new_image" has been generated, but allows us to skip
1826 ** the creation of 'old_image' if no more iterations are needed.
1828 ** The "curr_method" should also be set to a low-level method that is
1829 ** understood by the MorphologyApply() internal function.
1831 new_image=CloneImage(image,0,0,MagickTrue,exception);
1832 if (new_image == (Image *) NULL)
1833 return((Image *) NULL);
1834 if (SetImageStorageClass(new_image,DirectClass) == MagickFalse)
1836 InheritException(exception,&new_image->exception);
1837 new_image=DestroyImage(new_image);
1838 return((Image *) NULL);
1840 changed = MorphologyApply(image,new_image,curr_method,channel,curr_kernel,
1843 if ( GetImageArtifact(image,"verbose") != (const char *) NULL )
1844 fprintf(stderr, "Morphology %s:%ld => Changed %lu\n",
1845 MagickOptionToMnemonic(MagickMorphologyOptions, curr_method),
1850 /* At this point the "curr_method" should not only be set to a low-level
1851 ** method that is understood by the MorphologyApply() internal function,
1852 ** but "new_image" should now be defined, as the image to apply the
1853 ** "curr_method" to.
1856 /* Repeat the low-level morphology until count or no change reached */
1857 if ( count < (long) limit && changed > 0 ) {
1858 old_image = CloneImage(new_image,0,0,MagickTrue,exception);
1859 if (old_image == (Image *) NULL)
1860 return(DestroyImage(new_image));
1861 if (SetImageStorageClass(old_image,DirectClass) == MagickFalse)
1863 InheritException(exception,&old_image->exception);
1864 old_image=DestroyImage(old_image);
1865 return(DestroyImage(new_image));
1867 while( count < (long) limit && changed != 0 )
1869 Image *tmp = old_image;
1870 old_image = new_image;
1872 changed = MorphologyApply(old_image,new_image,curr_method,channel,
1873 curr_kernel, exception);
1875 if ( GetImageArtifact(image,"verbose") != (const char *) NULL )
1876 fprintf(stderr, "Morphology %s:%ld => Changed %lu\n",
1877 MagickOptionToMnemonic(MagickMorphologyOptions, curr_method),
1880 old_image=DestroyImage(old_image);
1883 /* We are finished with kernel - destroy it if we made a clone */
1884 if ( curr_kernel != kernel )
1885 curr_kernel=DestroyKernelInfo(curr_kernel);
1887 /* Third-level Subtractive methods post-processing
1889 ** The removal of any 'Sync' channel flag in the Image Compositon below
1890 ** ensures the compose method is applied in a purely mathematical way, only
1891 ** the selected channels, without any normal 'alpha blending' normally
1892 ** associated with the compose method.
1894 ** Note "method" here is the 'original' morphological method, and not the
1895 ** 'current' morphological method used above to generate "new_image".
1898 case EdgeOutMorphology:
1899 case EdgeInMorphology:
1900 case TopHatMorphology:
1901 case BottomHatMorphology:
1902 /* Get Difference relative to the original image */
1903 (void) CompositeImageChannel(new_image, (ChannelType) (channel & ~SyncChannels),
1904 DifferenceCompositeOp, image, 0, 0);
1906 case EdgeMorphology:
1907 /* Difference the Eroded image from the saved Dilated image */
1908 (void) CompositeImageChannel(new_image, (ChannelType) (channel & ~SyncChannels),
1909 DifferenceCompositeOp, grad_image, 0, 0);
1910 grad_image=DestroyImage(grad_image);
1920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924 + R o t a t e K e r n e l I n f o %
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1930 % RotateKernelInfo() rotates the kernel by the angle given. Currently it is
1931 % restricted to 90 degree angles, but this may be improved in the future.
1933 % The format of the RotateKernelInfo method is:
1935 % void RotateKernelInfo(KernelInfo *kernel, double angle)
1937 % A description of each parameter follows:
1939 % o kernel: the Morphology/Convolution kernel
1941 % o angle: angle to rotate in degrees
1943 % This function is only internel to this module, as it is not finalized,
1944 % especially with regard to non-orthogonal angles, and rotation of larger
1947 static void RotateKernelInfo(KernelInfo *kernel, double angle)
1949 /* WARNING: Currently assumes the kernel (rightly) is horizontally symetrical
1951 ** TODO: expand beyond simple 90 degree rotates, flips and flops
1954 /* Modulus the angle */
1955 angle = fmod(angle, 360.0);
1959 if ( 315.0 < angle || angle <= 45.0 )
1960 return; /* no change! - At least at this time */
1962 switch (kernel->type) {
1963 /* These built-in kernels are cylindrical kernels, rotating is useless */
1964 case GaussianKernel:
1965 case LaplacianKernel:
1969 case ChebyshevKernel:
1970 case ManhattenKernel:
1971 case EuclideanKernel:
1974 /* These may be rotatable at non-90 angles in the future */
1975 /* but simply rotating them in multiples of 90 degrees is useless */
1981 /* These only allows a +/-90 degree rotation (by transpose) */
1982 /* A 180 degree rotation is useless */
1984 case RectangleKernel:
1985 if ( 135.0 < angle && angle <= 225.0 )
1987 if ( 225.0 < angle && angle <= 315.0 )
1991 /* these are freely rotatable in 90 degree units */
1993 case UndefinedKernel:
1994 case UserDefinedKernel:
1997 if ( 135.0 < angle && angle <= 225.0 )
1999 /* For a 180 degree rotation - also know as a reflection */
2000 /* This is actually a very very common operation! */
2001 /* Basically all that is needed is a reversal of the kernel data! */
2008 for ( i=0, j=kernel->width*kernel->height-1; i<j; i++, j--)
2009 t=k[i], k[i]=k[j], k[j]=t;
2011 kernel->x = (long) kernel->width - kernel->x - 1;
2012 kernel->y = (long) kernel->height - kernel->y - 1;
2013 angle = fmod(angle+180.0, 360.0);
2015 if ( 45.0 < angle && angle <= 135.0 )
2016 { /* Do a transpose and a flop, of the image, which results in a 90
2017 * degree rotation using two mirror operations.
2019 * WARNING: this assumes the original image was a 1 dimentional image
2020 * but currently that is the only built-ins it is applied to.
2024 t = (long) kernel->width;
2025 kernel->width = kernel->height;
2026 kernel->height = (unsigned long) t;
2028 kernel->x = kernel->y;
2030 angle = fmod(450.0 - angle, 360.0);
2032 /* At this point angle should be between -45 (315) and +45 degrees
2033 * In the future some form of non-orthogonal angled rotates could be
2034 * performed here, posibily with a linear kernel restriction.
2038 Not currently in use!
2039 { /* Do a flop, this assumes kernel is horizontally symetrical.
2040 * Each row of the kernel needs to be reversed!
2049 for ( y=0, k=kernel->values; y < kernel->height; y++, k+=kernel->width)
2050 for ( x=0, r=kernel->width-1; x<kernel->width/2; x++, r--)
2051 t=k[x], k[x]=k[r], k[r]=t;
2053 kernel->x = kernel->width - kernel->x - 1;
2054 angle = fmod(angle+180.0, 360.0);
2061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065 % S c a l e K e r n e l I n f o %
2069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2071 % ScaleKernelInfo() scales the kernel by the given amount, with or without
2072 % normalization of the sum of the kernel values.
2074 % By default (no flags given) the values within the kernel is scaled
2075 % according the given scaling factor.
2077 % If any 'normalize_flags' are given the kernel will be normalized and then
2078 % further scaled by the scaleing factor value given. A 'PercentValue' flag
2079 % will cause the given scaling factor to be divided by one hundred percent.
2081 % Kernel normalization ('normalize_flags' given) is designed to ensure that
2082 % any use of the kernel scaling factor with 'Convolve' or 'Correlate'
2083 % morphology methods will fall into -1.0 to +1.0 range. Note however that
2084 % for non-HDRI versions of IM this may cause images to have any negative
2085 % results clipped, unless some 'clip' any negative output from 'Convolve'
2086 % with the use of some kernels.
2088 % More specifically. Kernels which only contain positive values (such as a
2089 % 'Gaussian' kernel) will be scaled so that those values sum to +1.0,
2090 % ensuring a 0.0 to +1.0 convolution output range for non-HDRI images.
2092 % For Kernels that contain some negative values, (such as 'Sharpen' kernels)
2093 % the kernel will be scaled by the absolute of the sum of kernel values, so
2094 % that it will generally fall within the +/- 1.0 range.
2096 % For kernels whose values sum to zero, (such as 'Laplician' kernels) kernel
2097 % will be scaled by just the sum of the postive values, so that its output
2098 % range will again fall into the +/- 1.0 range.
2100 % For special kernels designed for locating shapes using 'Correlate', (often
2101 % only containing +1 and -1 values, representing foreground/brackground
2102 % matching) a special normalization method is provided to scale the positive
2103 % values seperatally to those of the negative values, so the kernel will be
2104 % forced to become a zero-sum kernel better suited to such searches.
2106 % WARNING: Correct normalization of the kernal assumes that the '*_range'
2107 % attributes within the kernel structure have been correctly set during the
2110 % NOTE: The values used for 'normalize_flags' have been selected specifically
2111 % to match the use of geometry options, so that '!' means NormalizeValue, '^'
2112 % means CorrelateNormalizeValue, and '%' means PercentValue. All other
2113 % GeometryFlags values are ignored.
2115 % The format of the ScaleKernelInfo method is:
2117 % void ScaleKernelInfo(KernelInfo *kernel, const double scaling_factor,
2118 % const MagickStatusType normalize_flags )
2120 % A description of each parameter follows:
2122 % o kernel: the Morphology/Convolution kernel
2125 % multiply all values (after normalization) by this factor if not
2126 % zero. If the kernel is normalized regardless of any flags.
2128 % o normalize_flags:
2129 % GeometryFlags defining normalization method to use.
2130 % specifically: NormalizeValue, CorrelateNormalizeValue,
2131 % and/or PercentValue
2133 % This function is internal to this module only at this time, but can be
2134 % exported to other modules if needed.
2136 MagickExport void ScaleKernelInfo(KernelInfo *kernel,
2137 const double scaling_factor,const GeometryFlags normalize_flags)
2147 if ( (normalize_flags&NormalizeValue) != 0 ) {
2148 /* normalize kernel appropriately */
2149 if ( fabs(kernel->positive_range + kernel->negative_range) > MagickEpsilon )
2150 pos_scale = fabs(kernel->positive_range + kernel->negative_range);
2152 pos_scale = kernel->positive_range; /* special zero-summing kernel */
2154 /* force kernel into being a normalized zero-summing kernel */
2155 if ( (normalize_flags&CorrelateNormalizeValue) != 0 ) {
2156 pos_scale = ( fabs(kernel->positive_range) > MagickEpsilon )
2157 ? kernel->positive_range : 1.0;
2158 neg_scale = ( fabs(kernel->negative_range) > MagickEpsilon )
2159 ? -kernel->negative_range : 1.0;
2162 neg_scale = pos_scale;
2164 /* finialize scaling_factor for positive and negative components */
2165 pos_scale = scaling_factor/pos_scale;
2166 neg_scale = scaling_factor/neg_scale;
2167 if ( (normalize_flags&PercentValue) != 0 ) {
2172 for (i=0; i < (long) (kernel->width*kernel->height); i++)
2173 if ( ! IsNan(kernel->values[i]) )
2174 kernel->values[i] *= (kernel->values[i] >= 0) ? pos_scale : neg_scale;
2176 /* convolution output range */
2177 kernel->positive_range *= pos_scale;
2178 kernel->negative_range *= neg_scale;
2179 /* maximum and minimum values in kernel */
2180 kernel->maximum *= (kernel->maximum >= 0.0) ? pos_scale : neg_scale;
2181 kernel->minimum *= (kernel->minimum >= 0.0) ? pos_scale : neg_scale;
2183 /* swap kernel settings if user scaling factor is negative */
2184 if ( scaling_factor < MagickEpsilon ) {
2186 t = kernel->positive_range;
2187 kernel->positive_range = kernel->negative_range;
2188 kernel->negative_range = t;
2189 t = kernel->maximum;
2190 kernel->maximum = kernel->minimum;
2191 kernel->minimum = 1;
2198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2202 + S h o w K e r n e l I n f o %
2206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2208 % ShowKernelInfo() outputs the details of the given kernel defination to
2209 % standard error, generally due to a users 'showkernel' option request.
2211 % The format of the ShowKernel method is:
2213 % void ShowKernelInfo(KernelInfo *kernel)
2215 % A description of each parameter follows:
2217 % o kernel: the Morphology/Convolution kernel
2219 % This function is internal to this module only at this time. That may change
2222 MagickExport void ShowKernelInfo(KernelInfo *kernel)
2228 "Kernel \"%s\" of size %lux%lu%+ld%+ld with values from %.*lg to %.*lg\n",
2229 MagickOptionToMnemonic(MagickKernelOptions, kernel->type),
2230 kernel->width, kernel->height,
2231 kernel->x, kernel->y,
2232 GetMagickPrecision(), kernel->minimum,
2233 GetMagickPrecision(), kernel->maximum);
2234 fprintf(stderr, "Forming convolution output range from %.*lg to %.*lg%s\n",
2235 GetMagickPrecision(), kernel->negative_range,
2236 GetMagickPrecision(), kernel->positive_range,
2237 /*kernel->normalized == MagickTrue ? " (normalized)" : */ "" );
2238 for (i=v=0; v < (long) kernel->height; v++) {
2239 fprintf(stderr,"%2ld:",v);
2240 for (u=0; u < (long) kernel->width; u++, i++)
2241 if ( IsNan(kernel->values[i]) )
2242 fprintf(stderr," %*s", GetMagickPrecision()+2, "nan");
2244 fprintf(stderr," %*.*lg", GetMagickPrecision()+2,
2245 GetMagickPrecision(), kernel->values[i]);
2246 fprintf(stderr,"\n");
2251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2255 + Z e r o K e r n e l N a n s %
2259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2261 % ZeroKernelNans() replaces any special 'nan' value that may be present in
2262 % the kernel with a zero value. This is typically done when the kernel will
2263 % be used in special hardware (GPU) convolution processors, to simply
2266 % The format of the ZeroKernelNans method is:
2268 % voidZeroKernelNans (KernelInfo *kernel)
2270 % A description of each parameter follows:
2272 % o kernel: the Morphology/Convolution kernel
2274 % FUTURE: return the information in a string for API usage.
2276 MagickExport void ZeroKernelNans(KernelInfo *kernel)
2281 for (i=0; i < (long) (kernel->width*kernel->height); i++)
2282 if ( IsNan(kernel->values[i]) )
2283 kernel->values[i] = 0.0;