MagickCore  7.1.0
Convert, Edit, Or Compose Bitmap Images
effect.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
7 % E F F E C T %
8 % EEE FFF FFF EEE C T %
9 % E F F E C T %
10 % EEEEE F F EEEEE CCCC T %
11 % %
12 % %
13 % MagickCore Image Effects Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % October 1996 %
18 % %
19 % %
20 % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21 % 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 % https://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 %
36 %
37 %
38 */
39 
40 /*
41  Include declarations.
42 */
43 #include "MagickCore/studio.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/gem-private.h"
61 #include "MagickCore/geometry.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/matrix.h"
66 #include "MagickCore/memory_.h"
68 #include "MagickCore/monitor.h"
70 #include "MagickCore/montage.h"
71 #include "MagickCore/morphology.h"
73 #include "MagickCore/paint.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/quantize.h"
77 #include "MagickCore/quantum.h"
79 #include "MagickCore/random_.h"
81 #include "MagickCore/resample.h"
83 #include "MagickCore/resize.h"
84 #include "MagickCore/resource_.h"
85 #include "MagickCore/segment.h"
86 #include "MagickCore/shear.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
91 #include "MagickCore/transform.h"
92 #include "MagickCore/threshold.h"
93 
94 /*
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % %
97 % %
98 % %
99 % A d a p t i v e B l u r I m a g e %
100 % %
101 % %
102 % %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 %
105 % AdaptiveBlurImage() adaptively blurs the image by blurring less
106 % intensely near image edges and more intensely far from edges. We blur the
107 % image with a Gaussian operator of the given radius and standard deviation
108 % (sigma). For reasonable results, radius should be larger than sigma. Use a
109 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
110 %
111 % The format of the AdaptiveBlurImage method is:
112 %
113 % Image *AdaptiveBlurImage(const Image *image,const double radius,
114 % const double sigma,ExceptionInfo *exception)
115 %
116 % A description of each parameter follows:
117 %
118 % o image: the image.
119 %
120 % o radius: the radius of the Gaussian, in pixels, not counting the center
121 % pixel.
122 %
123 % o sigma: the standard deviation of the Laplacian, in pixels.
124 %
125 % o exception: return any errors or warnings in this structure.
126 %
127 */
128 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
129  const double sigma,ExceptionInfo *exception)
130 {
131 #define AdaptiveBlurImageTag "Convolve/Image"
132 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
133 
134  CacheView
135  *blur_view,
136  *edge_view,
137  *image_view;
138 
139  double
140  normalize,
141  **kernel;
142 
143  Image
144  *blur_image,
145  *edge_image,
146  *gaussian_image;
147 
149  status;
150 
152  progress;
153 
154  size_t
155  width;
156 
157  ssize_t
158  w,
159  y;
160 
161  assert(image != (const Image *) NULL);
162  assert(image->signature == MagickCoreSignature);
163  assert(exception != (ExceptionInfo *) NULL);
164  assert(exception->signature == MagickCoreSignature);
165  if (IsEventLogging() != MagickFalse)
166  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167  blur_image=CloneImage(image,0,0,MagickTrue,exception);
168  if (blur_image == (Image *) NULL)
169  return((Image *) NULL);
170  if (fabs(sigma) < MagickEpsilon)
171  return(blur_image);
172  if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
173  {
174  blur_image=DestroyImage(blur_image);
175  return((Image *) NULL);
176  }
177  /*
178  Edge detect the image brightness channel, level, blur, and level again.
179  */
180  edge_image=EdgeImage(image,radius,exception);
181  if (edge_image == (Image *) NULL)
182  {
183  blur_image=DestroyImage(blur_image);
184  return((Image *) NULL);
185  }
186  (void) AutoLevelImage(edge_image,exception);
187  gaussian_image=BlurImage(edge_image,radius,sigma,exception);
188  if (gaussian_image != (Image *) NULL)
189  {
190  edge_image=DestroyImage(edge_image);
191  edge_image=gaussian_image;
192  }
193  (void) AutoLevelImage(edge_image,exception);
194  /*
195  Create a set of kernels from maximum (radius,sigma) to minimum.
196  */
197  width=GetOptimalKernelWidth2D(radius,sigma);
198  kernel=(double **) MagickAssumeAligned(AcquireAlignedMemory((size_t) width,
199  sizeof(*kernel)));
200  if (kernel == (double **) NULL)
201  {
202  edge_image=DestroyImage(edge_image);
203  blur_image=DestroyImage(blur_image);
204  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
205  }
206  (void) memset(kernel,0,(size_t) width*sizeof(*kernel));
207  for (w=0; w < (ssize_t) width; w+=2)
208  {
209  ssize_t
210  j,
211  k,
212  u,
213  v;
214 
215  kernel[w]=(double *) MagickAssumeAligned(AcquireAlignedMemory(
216  (size_t) (width-w),(width-w)*sizeof(**kernel)));
217  if (kernel[w] == (double *) NULL)
218  break;
219  normalize=0.0;
220  j=(ssize_t) (width-w-1)/2;
221  k=0;
222  for (v=(-j); v <= j; v++)
223  {
224  for (u=(-j); u <= j; u++)
225  {
226  kernel[w][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
228  normalize+=kernel[w][k];
229  k++;
230  }
231  }
232  kernel[w][(k-1)/2]+=(double) (1.0-normalize);
233  if (sigma < MagickEpsilon)
234  kernel[w][(k-1)/2]=1.0;
235  }
236  if (w < (ssize_t) width)
237  {
238  for (w-=2; w >= 0; w-=2)
239  kernel[w]=(double *) RelinquishAlignedMemory(kernel[w]);
240  kernel=(double **) RelinquishAlignedMemory(kernel);
241  edge_image=DestroyImage(edge_image);
242  blur_image=DestroyImage(blur_image);
243  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
244  }
245  /*
246  Adaptively blur image.
247  */
248  status=MagickTrue;
249  progress=0;
250  image_view=AcquireVirtualCacheView(image,exception);
251  edge_view=AcquireVirtualCacheView(edge_image,exception);
252  blur_view=AcquireAuthenticCacheView(blur_image,exception);
253 #if defined(MAGICKCORE_OPENMP_SUPPORT)
254  #pragma omp parallel for schedule(static) shared(progress,status) \
255  magick_number_threads(image,blur_image,blur_image->rows,1)
256 #endif
257  for (y=0; y < (ssize_t) blur_image->rows; y++)
258  {
259  const Quantum
260  *magick_restrict r;
261 
262  Quantum
263  *magick_restrict q;
264 
265  ssize_t
266  x;
267 
268  if (status == MagickFalse)
269  continue;
270  r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
271  q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
272  exception);
273  if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
274  {
275  status=MagickFalse;
276  continue;
277  }
278  for (x=0; x < (ssize_t) blur_image->columns; x++)
279  {
280  const Quantum
281  *magick_restrict p;
282 
283  ssize_t
284  i;
285 
286  ssize_t
287  center,
288  j;
289 
290  j=CastDoubleToLong(ceil((double) width*(1.0-QuantumScale*
291  GetPixelIntensity(edge_image,r))-0.5));
292  if (j < 0)
293  j=0;
294  else
295  if (j > (ssize_t) width)
296  j=(ssize_t) width;
297  if ((j & 0x01) != 0)
298  j--;
299  p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
300  (ssize_t) ((width-j)/2L),width-j,width-j,exception);
301  if (p == (const Quantum *) NULL)
302  break;
303  center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
304  GetPixelChannels(image)*((width-j)/2);
305  for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
306  {
307  double
308  alpha,
309  gamma,
310  pixel;
311 
313  channel;
314 
315  PixelTrait
316  blur_traits,
317  traits;
318 
319  const double
320  *magick_restrict k;
321 
322  const Quantum
323  *magick_restrict pixels;
324 
325  ssize_t
326  u;
327 
328  ssize_t
329  v;
330 
331  channel=GetPixelChannelChannel(image,i);
332  traits=GetPixelChannelTraits(image,channel);
333  blur_traits=GetPixelChannelTraits(blur_image,channel);
334  if ((traits == UndefinedPixelTrait) ||
335  (blur_traits == UndefinedPixelTrait))
336  continue;
337  if ((blur_traits & CopyPixelTrait) != 0)
338  {
339  SetPixelChannel(blur_image,channel,p[center+i],q);
340  continue;
341  }
342  k=kernel[j];
343  pixels=p;
344  pixel=0.0;
345  gamma=0.0;
346  if ((blur_traits & BlendPixelTrait) == 0)
347  {
348  /*
349  No alpha blending.
350  */
351  for (v=0; v < (ssize_t) (width-j); v++)
352  {
353  for (u=0; u < (ssize_t) (width-j); u++)
354  {
355  pixel+=(*k)*pixels[i];
356  gamma+=(*k);
357  k++;
358  pixels+=GetPixelChannels(image);
359  }
360  }
361  gamma=PerceptibleReciprocal(gamma);
362  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
363  continue;
364  }
365  /*
366  Alpha blending.
367  */
368  for (v=0; v < (ssize_t) (width-j); v++)
369  {
370  for (u=0; u < (ssize_t) (width-j); u++)
371  {
372  alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
373  pixel+=(*k)*alpha*pixels[i];
374  gamma+=(*k)*alpha;
375  k++;
376  pixels+=GetPixelChannels(image);
377  }
378  }
379  gamma=PerceptibleReciprocal(gamma);
380  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
381  }
382  q+=GetPixelChannels(blur_image);
383  r+=GetPixelChannels(edge_image);
384  }
385  if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
386  status=MagickFalse;
387  if (image->progress_monitor != (MagickProgressMonitor) NULL)
388  {
390  proceed;
391 
392 #if defined(MAGICKCORE_OPENMP_SUPPORT)
393  #pragma omp atomic
394 #endif
395  progress++;
396  proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress,
397  image->rows);
398  if (proceed == MagickFalse)
399  status=MagickFalse;
400  }
401  }
402  blur_image->type=image->type;
403  blur_view=DestroyCacheView(blur_view);
404  edge_view=DestroyCacheView(edge_view);
405  image_view=DestroyCacheView(image_view);
406  edge_image=DestroyImage(edge_image);
407  for (w=0; w < (ssize_t) width; w+=2)
408  kernel[w]=(double *) RelinquishAlignedMemory(kernel[w]);
409  kernel=(double **) RelinquishAlignedMemory(kernel);
410  if (status == MagickFalse)
411  blur_image=DestroyImage(blur_image);
412  return(blur_image);
413 }
414 
415 /*
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % %
418 % %
419 % %
420 % A d a p t i v e S h a r p e n I m a g e %
421 % %
422 % %
423 % %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 %
426 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
427 % intensely near image edges and less intensely far from edges. We sharpen the
428 % image with a Gaussian operator of the given radius and standard deviation
429 % (sigma). For reasonable results, radius should be larger than sigma. Use a
430 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
431 %
432 % The format of the AdaptiveSharpenImage method is:
433 %
434 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
435 % const double sigma,ExceptionInfo *exception)
436 %
437 % A description of each parameter follows:
438 %
439 % o image: the image.
440 %
441 % o radius: the radius of the Gaussian, in pixels, not counting the center
442 % pixel.
443 %
444 % o sigma: the standard deviation of the Laplacian, in pixels.
445 %
446 % o exception: return any errors or warnings in this structure.
447 %
448 */
449 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
450  const double sigma,ExceptionInfo *exception)
451 {
452 #define AdaptiveSharpenImageTag "Convolve/Image"
453 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
454 
455  CacheView
456  *sharp_view,
457  *edge_view,
458  *image_view;
459 
460  double
461  normalize,
462  **kernel;
463 
464  Image
465  *sharp_image,
466  *edge_image,
467  *gaussian_image;
468 
470  status;
471 
473  progress;
474 
475  size_t
476  width;
477 
478  ssize_t
479  w,
480  y;
481 
482  assert(image != (const Image *) NULL);
483  assert(image->signature == MagickCoreSignature);
484  assert(exception != (ExceptionInfo *) NULL);
485  assert(exception->signature == MagickCoreSignature);
486  if (IsEventLogging() != MagickFalse)
487  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
488  sharp_image=CloneImage(image,0,0,MagickTrue,exception);
489  if (sharp_image == (Image *) NULL)
490  return((Image *) NULL);
491  if (fabs(sigma) < MagickEpsilon)
492  return(sharp_image);
493  if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
494  {
495  sharp_image=DestroyImage(sharp_image);
496  return((Image *) NULL);
497  }
498  /*
499  Edge detect the image brightness channel, level, sharp, and level again.
500  */
501  edge_image=EdgeImage(image,radius,exception);
502  if (edge_image == (Image *) NULL)
503  {
504  sharp_image=DestroyImage(sharp_image);
505  return((Image *) NULL);
506  }
507  (void) AutoLevelImage(edge_image,exception);
508  gaussian_image=BlurImage(edge_image,radius,sigma,exception);
509  if (gaussian_image != (Image *) NULL)
510  {
511  edge_image=DestroyImage(edge_image);
512  edge_image=gaussian_image;
513  }
514  (void) AutoLevelImage(edge_image,exception);
515  /*
516  Create a set of kernels from maximum (radius,sigma) to minimum.
517  */
518  width=GetOptimalKernelWidth2D(radius,sigma);
519  kernel=(double **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
520  width,sizeof(*kernel)));
521  if (kernel == (double **) NULL)
522  {
523  edge_image=DestroyImage(edge_image);
524  sharp_image=DestroyImage(sharp_image);
525  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
526  }
527  (void) memset(kernel,0,(size_t) width*sizeof(*kernel));
528  for (w=0; w < (ssize_t) width; w+=2)
529  {
530  ssize_t
531  j,
532  k,
533  u,
534  v;
535 
536  kernel[w]=(double *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
537  (width-w),(width-w)*sizeof(**kernel)));
538  if (kernel[w] == (double *) NULL)
539  break;
540  normalize=0.0;
541  j=(ssize_t) (width-w-1)/2;
542  k=0;
543  for (v=(-j); v <= j; v++)
544  {
545  for (u=(-j); u <= j; u++)
546  {
547  kernel[w][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
549  normalize+=kernel[w][k];
550  k++;
551  }
552  }
553  kernel[w][(k-1)/2]=(double) ((-2.0)*normalize);
554  if (sigma < MagickEpsilon)
555  kernel[w][(k-1)/2]=1.0;
556  }
557  if (w < (ssize_t) width)
558  {
559  for (w-=2; w >= 0; w-=2)
560  kernel[w]=(double *) RelinquishAlignedMemory(kernel[w]);
561  kernel=(double **) RelinquishAlignedMemory(kernel);
562  edge_image=DestroyImage(edge_image);
563  sharp_image=DestroyImage(sharp_image);
564  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
565  }
566  /*
567  Adaptively sharpen image.
568  */
569  status=MagickTrue;
570  progress=0;
571  image_view=AcquireVirtualCacheView(image,exception);
572  edge_view=AcquireVirtualCacheView(edge_image,exception);
573  sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
574 #if defined(MAGICKCORE_OPENMP_SUPPORT)
575  #pragma omp parallel for schedule(static) shared(progress,status) \
576  magick_number_threads(image,sharp_image,sharp_image->rows,1)
577 #endif
578  for (y=0; y < (ssize_t) sharp_image->rows; y++)
579  {
580  const Quantum
581  *magick_restrict r;
582 
583  Quantum
584  *magick_restrict q;
585 
586  ssize_t
587  x;
588 
589  if (status == MagickFalse)
590  continue;
591  r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
592  q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
593  exception);
594  if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
595  {
596  status=MagickFalse;
597  continue;
598  }
599  for (x=0; x < (ssize_t) sharp_image->columns; x++)
600  {
601  const Quantum
602  *magick_restrict p;
603 
604  ssize_t
605  i;
606 
607  ssize_t
608  center,
609  j;
610 
611  j=CastDoubleToLong(ceil((double) width*(1.0-QuantumScale*
612  GetPixelIntensity(edge_image,r))-0.5));
613  if (j < 0)
614  j=0;
615  else
616  if (j > (ssize_t) width)
617  j=(ssize_t) width;
618  if ((j & 0x01) != 0)
619  j--;
620  p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
621  (ssize_t) ((width-j)/2L),width-j,width-j,exception);
622  if (p == (const Quantum *) NULL)
623  break;
624  center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
625  GetPixelChannels(image)*((width-j)/2);
626  for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
627  {
628  double
629  alpha,
630  gamma,
631  pixel;
632 
634  channel;
635 
636  PixelTrait
637  sharp_traits,
638  traits;
639 
640  const double
641  *magick_restrict k;
642 
643  const Quantum
644  *magick_restrict pixels;
645 
646  ssize_t
647  u;
648 
649  ssize_t
650  v;
651 
652  channel=GetPixelChannelChannel(image,i);
653  traits=GetPixelChannelTraits(image,channel);
654  sharp_traits=GetPixelChannelTraits(sharp_image,channel);
655  if ((traits == UndefinedPixelTrait) ||
656  (sharp_traits == UndefinedPixelTrait))
657  continue;
658  if ((sharp_traits & CopyPixelTrait) != 0)
659  {
660  SetPixelChannel(sharp_image,channel,p[center+i],q);
661  continue;
662  }
663  k=kernel[j];
664  pixels=p;
665  pixel=0.0;
666  gamma=0.0;
667  if ((sharp_traits & BlendPixelTrait) == 0)
668  {
669  /*
670  No alpha blending.
671  */
672  for (v=0; v < (ssize_t) (width-j); v++)
673  {
674  for (u=0; u < (ssize_t) (width-j); u++)
675  {
676  pixel+=(*k)*pixels[i];
677  gamma+=(*k);
678  k++;
679  pixels+=GetPixelChannels(image);
680  }
681  }
682  gamma=PerceptibleReciprocal(gamma);
683  SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
684  continue;
685  }
686  /*
687  Alpha blending.
688  */
689  for (v=0; v < (ssize_t) (width-j); v++)
690  {
691  for (u=0; u < (ssize_t) (width-j); u++)
692  {
693  alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
694  pixel+=(*k)*alpha*pixels[i];
695  gamma+=(*k)*alpha;
696  k++;
697  pixels+=GetPixelChannels(image);
698  }
699  }
700  gamma=PerceptibleReciprocal(gamma);
701  SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
702  }
703  q+=GetPixelChannels(sharp_image);
704  r+=GetPixelChannels(edge_image);
705  }
706  if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
707  status=MagickFalse;
708  if (image->progress_monitor != (MagickProgressMonitor) NULL)
709  {
711  proceed;
712 
713 #if defined(MAGICKCORE_OPENMP_SUPPORT)
714  #pragma omp atomic
715 #endif
716  progress++;
717  proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress,
718  image->rows);
719  if (proceed == MagickFalse)
720  status=MagickFalse;
721  }
722  }
723  sharp_image->type=image->type;
724  sharp_view=DestroyCacheView(sharp_view);
725  edge_view=DestroyCacheView(edge_view);
726  image_view=DestroyCacheView(image_view);
727  edge_image=DestroyImage(edge_image);
728  for (w=0; w < (ssize_t) width; w+=2)
729  kernel[w]=(double *) RelinquishAlignedMemory(kernel[w]);
730  kernel=(double **) RelinquishAlignedMemory(kernel);
731  if (status == MagickFalse)
732  sharp_image=DestroyImage(sharp_image);
733  return(sharp_image);
734 }
735 
736 /*
737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 % %
739 % %
740 % %
741 % B l u r I m a g e %
742 % %
743 % %
744 % %
745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
746 %
747 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
748 % of the given radius and standard deviation (sigma). For reasonable results,
749 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
750 % selects a suitable radius for you.
751 %
752 % The format of the BlurImage method is:
753 %
754 % Image *BlurImage(const Image *image,const double radius,
755 % const double sigma,ExceptionInfo *exception)
756 %
757 % A description of each parameter follows:
758 %
759 % o image: the image.
760 %
761 % o radius: the radius of the Gaussian, in pixels, not counting the center
762 % pixel.
763 %
764 % o sigma: the standard deviation of the Gaussian, in pixels.
765 %
766 % o exception: return any errors or warnings in this structure.
767 %
768 */
769 MagickExport Image *BlurImage(const Image *image,const double radius,
770  const double sigma,ExceptionInfo *exception)
771 {
772  char
773  geometry[MagickPathExtent];
774 
775  KernelInfo
776  *kernel_info;
777 
778  Image
779  *blur_image;
780 
781  assert(image != (const Image *) NULL);
782  assert(image->signature == MagickCoreSignature);
783  assert(exception != (ExceptionInfo *) NULL);
784  assert(exception->signature == MagickCoreSignature);
785  if (IsEventLogging() != MagickFalse)
786  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
787 #if defined(MAGICKCORE_OPENCL_SUPPORT)
788  blur_image=AccelerateBlurImage(image,radius,sigma,exception);
789  if (blur_image != (Image *) NULL)
790  return(blur_image);
791 #endif
792  (void) FormatLocaleString(geometry,MagickPathExtent,
793  "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
794  kernel_info=AcquireKernelInfo(geometry,exception);
795  if (kernel_info == (KernelInfo *) NULL)
796  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
797  blur_image=ConvolveImage(image,kernel_info,exception);
798  kernel_info=DestroyKernelInfo(kernel_info);
799  return(blur_image);
800 }
801 
802 /*
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % %
805 % %
806 % %
807 % B i l a t e r a l B l u r I m a g e %
808 % %
809 % %
810 % %
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 %
813 % BilateralBlurImage() is a non-linear, edge-preserving, and noise-reducing
814 % smoothing filter for images. It replaces the intensity of each pixel with
815 % a weighted average of intensity values from nearby pixels. This weight is
816 % based on a Gaussian distribution. The weights depend not only on Euclidean
817 % distance of pixels, but also on the radiometric differences (e.g., range
818 % differences, such as color intensity, depth distance, etc.). This preserves
819 % sharp edges.
820 %
821 % The format of the BilateralBlurImage method is:
822 %
823 % Image *BilateralBlurImage(const Image *image,const size_t width,
824 % const size_t height,const double intensity_sigma,
825 % const double spatial_sigma,ExceptionInfo *exception)
826 %
827 % A description of each parameter follows:
828 %
829 % o image: the image.
830 %
831 % o width: the width of the neighborhood in pixels.
832 %
833 % o height: the height of the neighborhood in pixels.
834 %
835 % o intensity_sigma: sigma in the intensity space. A larger value means
836 % that farther colors within the pixel neighborhood (see spatial_sigma)
837 % will be mixed together, resulting in larger areas of semi-equal color.
838 %
839 % o spatial_sigma: sigma in the coordinate space. A larger value means that
840 % farther pixels influence each other as long as their colors are close
841 % enough (see intensity_sigma ). When the neigborhood diameter is greater
842 % than zero, it specifies the neighborhood size regardless of
843 % spatial_sigma. Otherwise, the neigborhood diameter is proportional to
844 % spatial_sigma.
845 %
846 % o exception: return any errors or warnings in this structure.
847 %
848 */
849 
850 static inline double BlurDistance(const ssize_t x,const ssize_t y,
851  const ssize_t u,const ssize_t v)
852 {
853  return(sqrt(((double) x-u)*((double) x-u)+((double) y-v)*((double) y-v)));
854 }
855 
856 static inline double BlurGaussian(const double x,const double sigma)
857 {
858  return(exp(-((double) x*x)*PerceptibleReciprocal(2.0*sigma*sigma))*
859  PerceptibleReciprocal(Magick2PI*sigma*sigma));
860 }
861 
862 static double **DestroyBilateralTLS(const ssize_t number_threads,
863  double **weights)
864 {
865  ssize_t
866  i;
867 
868  assert(weights != (double **) NULL);
869  for (i=0; i <= (ssize_t) number_threads; i++)
870  if (weights[i] != (double *) NULL)
871  weights[i]=(double *) RelinquishMagickMemory(weights[i]);
872  weights=(double **) RelinquishMagickMemory(weights);
873  return(weights);
874 }
875 
876 static double **AcquireBilateralTLS(const size_t number_threads,
877  const size_t width,const size_t height)
878 {
879  double
880  **weights;
881 
882  ssize_t
883  i;
884 
885  weights=(double **) AcquireQuantumMemory(number_threads+1,sizeof(*weights));
886  if (weights == (double **) NULL)
887  return((double **) NULL);
888  (void) memset(weights,0,number_threads*sizeof(*weights));
889  for (i=0; i <= (ssize_t) number_threads; i++)
890  {
891  weights[i]=(double *) AcquireQuantumMemory(width,height*sizeof(**weights));
892  if (weights[i] == (double *) NULL)
893  return(DestroyBilateralTLS(number_threads,weights));
894  }
895  return(weights);
896 }
897 
898 MagickExport Image *BilateralBlurImage(const Image *image,const size_t width,
899  const size_t height,const double intensity_sigma,const double spatial_sigma,
900  ExceptionInfo *exception)
901 {
902 #define MaxIntensity (255)
903 #define BilateralBlurImageTag "Blur/Image"
904 
905  CacheView
906  *blur_view,
907  *image_view;
908 
909  double
910  intensity_gaussian[2*(MaxIntensity+1)],
911  *spatial_gaussian,
912  **weights;
913 
914  Image
915  *blur_image;
916 
918  status;
919 
921  progress;
922 
923  OffsetInfo
924  mid;
925 
926  ssize_t
927  number_threads,
928  w,
929  y;
930 
931  assert(image != (const Image *) NULL);
932  assert(image->signature == MagickCoreSignature);
933  assert(exception != (ExceptionInfo *) NULL);
934  assert(exception->signature == MagickCoreSignature);
935  if (IsEventLogging() != MagickFalse)
936  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
937  blur_image=CloneImage(image,0,0,MagickTrue,exception);
938  if (blur_image == (Image *) NULL)
939  return((Image *) NULL);
940  if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
941  {
942  blur_image=DestroyImage(blur_image);
943  return((Image *) NULL);
944  }
945  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
946  weights=AcquireBilateralTLS(number_threads,MagickMax(width,1),
947  MagickMax(height,1));
948  if (weights == (double **) NULL)
949  {
950  blur_image=DestroyImage(blur_image);
951  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
952  }
953  for (w=(-MaxIntensity); w < MaxIntensity; w++)
954  intensity_gaussian[w+MaxIntensity]=BlurGaussian((double) w,intensity_sigma);
955  spatial_gaussian=weights[number_threads];
956  {
957  ssize_t
958  n,
959  v;
960 
961  n=0;
962  mid.x=(ssize_t) (MagickMax(width,1)/2L);
963  mid.y=(ssize_t) (MagickMax(height,1)/2L);
964  for (v=0; v < (ssize_t) MagickMax(height,1); v++)
965  {
966  ssize_t
967  u;
968 
969  for (u=0; u < (ssize_t) MagickMax(width,1); u++)
970  spatial_gaussian[n++]=BlurGaussian(BlurDistance(0,0,u-mid.x,v-mid.y),
971  spatial_sigma);
972  }
973  }
974  /*
975  Bilateral blur image.
976  */
977  status=MagickTrue;
978  progress=0;
979  image_view=AcquireVirtualCacheView(image,exception);
980  blur_view=AcquireAuthenticCacheView(blur_image,exception);
981 #if defined(MAGICKCORE_OPENMP_SUPPORT)
982  #pragma omp parallel for schedule(static) shared(progress,status) \
983  magick_number_threads(image,blur_image,blur_image->rows,1)
984 #endif
985  for (y=0; y < (ssize_t) blur_image->rows; y++)
986  {
987  const int
988  id = GetOpenMPThreadId();
989 
990  Quantum
991  *magick_restrict q;
992 
993  ssize_t
994  x;
995 
996  if (status == MagickFalse)
997  continue;
998  q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
999  exception);
1000  if (q == (Quantum *) NULL)
1001  {
1002  status=MagickFalse;
1003  continue;
1004  }
1005  for (x=0; x < (ssize_t) blur_image->columns; x++)
1006  {
1007  double
1008  gamma,
1009  pixel;
1010 
1011  const Quantum
1012  *magick_restrict p,
1013  *magick_restrict r;
1014 
1015  ssize_t
1016  i,
1017  u;
1018 
1019  ssize_t
1020  n,
1021  v;
1022 
1023  /*
1024  Tonal weighting preserves edges while smoothing in the flat regions.
1025  */
1026  p=GetCacheViewVirtualPixels(image_view,x-mid.x,y-mid.y,MagickMax(width,1),
1027  MagickMax(height,1),exception);
1028  if (p == (const Quantum *) NULL)
1029  break;
1030  p+=(ssize_t) GetPixelChannels(image)*MagickMax(width,1)*mid.y+
1031  GetPixelChannels(image)*mid.x;
1032  n=0;
1033  for (v=0; v < (ssize_t) MagickMax(height,1); v++)
1034  {
1035  for (u=0; u < (ssize_t) MagickMax(width,1); u++)
1036  {
1037  double
1038  intensity;
1039 
1040  r=p+(ssize_t) GetPixelChannels(image)*(ssize_t) MagickMax(width,1)*
1041  (mid.y-v)+GetPixelChannels(image)*(mid.x-u);
1042  intensity=ScaleQuantumToChar(GetPixelIntensity(image,r))-
1043  (double) ScaleQuantumToChar(GetPixelIntensity(image,p));
1044  if ((intensity >= -MaxIntensity) && (intensity <= MaxIntensity))
1045  weights[id][n]=intensity_gaussian[(ssize_t) intensity+MaxIntensity]*
1046  spatial_gaussian[n];
1047  else
1048  weights[id][n]=BlurGaussian(intensity,intensity_sigma)*
1049  BlurGaussian(BlurDistance(x,y,x+u-mid.x,y+v-mid.y),spatial_sigma);
1050  n++;
1051  }
1052  }
1053  for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1054  {
1055  PixelChannel
1056  channel;
1057 
1058  PixelTrait
1059  blur_traits,
1060  traits;
1061 
1062  channel=GetPixelChannelChannel(image,i);
1063  traits=GetPixelChannelTraits(image,channel);
1064  blur_traits=GetPixelChannelTraits(blur_image,channel);
1065  if ((traits == UndefinedPixelTrait) ||
1066  (blur_traits == UndefinedPixelTrait))
1067  continue;
1068  if ((blur_traits & CopyPixelTrait) != 0)
1069  {
1070  SetPixelChannel(blur_image,channel,p[i],q);
1071  continue;
1072  }
1073  pixel=0.0;
1074  gamma=0.0;
1075  n=0;
1076  if ((blur_traits & BlendPixelTrait) == 0)
1077  {
1078  /*
1079  No alpha blending.
1080  */
1081  for (v=0; v < (ssize_t) MagickMax(height,1); v++)
1082  {
1083  for (u=0; u < (ssize_t) MagickMax(width,1); u++)
1084  {
1085  r=p+(ssize_t) GetPixelChannels(image)*MagickMax(width,1)*
1086  (mid.y-v)+GetPixelChannels(image)*(mid.x-u);
1087  pixel+=weights[id][n]*r[i];
1088  gamma+=weights[id][n];
1089  n++;
1090  }
1091  }
1092  SetPixelChannel(blur_image,channel,ClampToQuantum(
1093  PerceptibleReciprocal(gamma)*pixel),q);
1094  continue;
1095  }
1096  /*
1097  Alpha blending.
1098  */
1099  for (v=0; v < (ssize_t) MagickMax(height,1); v++)
1100  {
1101  for (u=0; u < (ssize_t) MagickMax(width,1); u++)
1102  {
1103  double
1104  alpha,
1105  beta;
1106 
1107  r=p+(ssize_t) GetPixelChannels(image)*MagickMax(width,1)*(mid.y-v)+
1108  GetPixelChannels(image)*(mid.x-u);
1109  alpha=(double) (QuantumScale*GetPixelAlpha(image,p));
1110  beta=(double) (QuantumScale*GetPixelAlpha(image,r));
1111  pixel+=weights[id][n]*r[i];
1112  gamma+=weights[id][n]*alpha*beta;
1113  n++;
1114  }
1115  }
1116  SetPixelChannel(blur_image,channel,ClampToQuantum(
1117  PerceptibleReciprocal(gamma)*pixel),q);
1118  }
1119  q+=GetPixelChannels(blur_image);
1120  }
1121  if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1122  status=MagickFalse;
1123  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1124  {
1126  proceed;
1127 
1128 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1129  #pragma omp atomic
1130 #endif
1131  progress++;
1132  proceed=SetImageProgress(image,BilateralBlurImageTag,progress,
1133  image->rows);
1134  if (proceed == MagickFalse)
1135  status=MagickFalse;
1136  }
1137  }
1138  blur_image->type=image->type;
1139  blur_view=DestroyCacheView(blur_view);
1140  image_view=DestroyCacheView(image_view);
1141  weights=DestroyBilateralTLS(number_threads,weights);
1142  if (status == MagickFalse)
1143  blur_image=DestroyImage(blur_image);
1144  return(blur_image);
1145 }
1146 
1147 /*
1148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1149 % %
1150 % %
1151 % %
1152 % C o n v o l v e I m a g e %
1153 % %
1154 % %
1155 % %
1156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1157 %
1158 % ConvolveImage() applies a custom convolution kernel to the image.
1159 %
1160 % The format of the ConvolveImage method is:
1161 %
1162 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1163 % ExceptionInfo *exception)
1164 %
1165 % A description of each parameter follows:
1166 %
1167 % o image: the image.
1168 %
1169 % o kernel: the filtering kernel.
1170 %
1171 % o exception: return any errors or warnings in this structure.
1172 %
1173 */
1175  const KernelInfo *kernel_info,ExceptionInfo *exception)
1176 {
1177  Image
1178  *convolve_image;
1179 
1180 #if defined(MAGICKCORE_OPENCL_SUPPORT)
1181  convolve_image=AccelerateConvolveImage(image,kernel_info,exception);
1182  if (convolve_image != (Image *) NULL)
1183  return(convolve_image);
1184 #endif
1185 
1186  convolve_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
1187  exception);
1188  return(convolve_image);
1189 }
1190 
1191 /*
1192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193 % %
1194 % %
1195 % %
1196 % D e s p e c k l e I m a g e %
1197 % %
1198 % %
1199 % %
1200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201 %
1202 % DespeckleImage() reduces the speckle noise in an image while perserving the
1203 % edges of the original image. A speckle removing filter uses a complementary
1204 % hulling technique (raising pixels that are darker than their surrounding
1205 % neighbors, then complementarily lowering pixels that are brighter than their
1206 % surrounding neighbors) to reduce the speckle index of that image (reference
1207 % Crimmins speckle removal).
1208 %
1209 % The format of the DespeckleImage method is:
1210 %
1211 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1212 %
1213 % A description of each parameter follows:
1214 %
1215 % o image: the image.
1216 %
1217 % o exception: return any errors or warnings in this structure.
1218 %
1219 */
1220 
1221 static void Hull(const Image *image,const ssize_t x_offset,
1222  const ssize_t y_offset,const size_t columns,const size_t rows,
1223  const int polarity,Quantum *magick_restrict f,Quantum *magick_restrict g)
1224 {
1225  Quantum
1226  *p,
1227  *q,
1228  *r,
1229  *s;
1230 
1231  ssize_t
1232  y;
1233 
1234  assert(image != (const Image *) NULL);
1235  assert(image->signature == MagickCoreSignature);
1236  assert(f != (Quantum *) NULL);
1237  assert(g != (Quantum *) NULL);
1238  if (IsEventLogging() != MagickFalse)
1239  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1240  p=f+(columns+2);
1241  q=g+(columns+2);
1242  r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1243 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1244  #pragma omp parallel for schedule(static) \
1245  magick_number_threads(image,image,rows,1)
1246 #endif
1247  for (y=0; y < (ssize_t) rows; y++)
1248  {
1250  v;
1251 
1252  ssize_t
1253  i,
1254  x;
1255 
1256  i=(2*y+1)+y*columns;
1257  if (polarity > 0)
1258  for (x=0; x < (ssize_t) columns; x++)
1259  {
1260  v=(MagickRealType) p[i];
1261  if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1262  v+=ScaleCharToQuantum(1);
1263  q[i]=(Quantum) v;
1264  i++;
1265  }
1266  else
1267  for (x=0; x < (ssize_t) columns; x++)
1268  {
1269  v=(MagickRealType) p[i];
1270  if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1271  v-=ScaleCharToQuantum(1);
1272  q[i]=(Quantum) v;
1273  i++;
1274  }
1275  }
1276  p=f+(columns+2);
1277  q=g+(columns+2);
1278  r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1279  s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1280 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1281  #pragma omp parallel for schedule(static) \
1282  magick_number_threads(image,image,rows,1)
1283 #endif
1284  for (y=0; y < (ssize_t) rows; y++)
1285  {
1286  ssize_t
1287  i,
1288  x;
1289 
1291  v;
1292 
1293  i=(2*y+1)+y*columns;
1294  if (polarity > 0)
1295  for (x=0; x < (ssize_t) columns; x++)
1296  {
1297  v=(MagickRealType) q[i];
1298  if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1299  ((MagickRealType) r[i] > v))
1300  v+=ScaleCharToQuantum(1);
1301  p[i]=(Quantum) v;
1302  i++;
1303  }
1304  else
1305  for (x=0; x < (ssize_t) columns; x++)
1306  {
1307  v=(MagickRealType) q[i];
1308  if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1309  ((MagickRealType) r[i] < v))
1310  v-=ScaleCharToQuantum(1);
1311  p[i]=(Quantum) v;
1312  i++;
1313  }
1314  }
1315 }
1316 
1318 {
1319 #define DespeckleImageTag "Despeckle/Image"
1320 
1321  CacheView
1322  *despeckle_view,
1323  *image_view;
1324 
1325  Image
1326  *despeckle_image;
1327 
1329  status;
1330 
1331  MemoryInfo
1332  *buffer_info,
1333  *pixel_info;
1334 
1335  Quantum
1336  *magick_restrict buffer,
1337  *magick_restrict pixels;
1338 
1339  ssize_t
1340  i;
1341 
1342  size_t
1343  length;
1344 
1345  static const ssize_t
1346  X[4] = {0, 1, 1,-1},
1347  Y[4] = {1, 0, 1, 1};
1348 
1349  /*
1350  Allocate despeckled image.
1351  */
1352  assert(image != (const Image *) NULL);
1353  assert(image->signature == MagickCoreSignature);
1354  assert(exception != (ExceptionInfo *) NULL);
1355  assert(exception->signature == MagickCoreSignature);
1356  if (IsEventLogging() != MagickFalse)
1357  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1358 #if defined(MAGICKCORE_OPENCL_SUPPORT)
1359  despeckle_image=AccelerateDespeckleImage(image,exception);
1360  if (despeckle_image != (Image *) NULL)
1361  return(despeckle_image);
1362 #endif
1363  despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1364  if (despeckle_image == (Image *) NULL)
1365  return((Image *) NULL);
1366  status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1367  if (status == MagickFalse)
1368  {
1369  despeckle_image=DestroyImage(despeckle_image);
1370  return((Image *) NULL);
1371  }
1372  /*
1373  Allocate image buffer.
1374  */
1375  length=(size_t) ((image->columns+2)*(image->rows+2));
1376  pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1377  buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1378  if ((pixel_info == (MemoryInfo *) NULL) ||
1379  (buffer_info == (MemoryInfo *) NULL))
1380  {
1381  if (buffer_info != (MemoryInfo *) NULL)
1382  buffer_info=RelinquishVirtualMemory(buffer_info);
1383  if (pixel_info != (MemoryInfo *) NULL)
1384  pixel_info=RelinquishVirtualMemory(pixel_info);
1385  despeckle_image=DestroyImage(despeckle_image);
1386  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1387  }
1388  pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1389  buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1390  /*
1391  Reduce speckle in the image.
1392  */
1393  status=MagickTrue;
1394  image_view=AcquireVirtualCacheView(image,exception);
1395  despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1396  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1397  {
1398  PixelChannel
1399  channel;
1400 
1401  PixelTrait
1402  despeckle_traits,
1403  traits;
1404 
1405  ssize_t
1406  k,
1407  x;
1408 
1409  ssize_t
1410  j,
1411  y;
1412 
1413  if (status == MagickFalse)
1414  continue;
1415  channel=GetPixelChannelChannel(image,i);
1416  traits=GetPixelChannelTraits(image,channel);
1417  despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1418  if ((traits == UndefinedPixelTrait) ||
1419  (despeckle_traits == UndefinedPixelTrait))
1420  continue;
1421  if ((despeckle_traits & CopyPixelTrait) != 0)
1422  continue;
1423  (void) memset(pixels,0,length*sizeof(*pixels));
1424  j=(ssize_t) image->columns+2;
1425  for (y=0; y < (ssize_t) image->rows; y++)
1426  {
1427  const Quantum
1428  *magick_restrict p;
1429 
1430  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1431  if (p == (const Quantum *) NULL)
1432  {
1433  status=MagickFalse;
1434  continue;
1435  }
1436  j++;
1437  for (x=0; x < (ssize_t) image->columns; x++)
1438  {
1439  pixels[j++]=p[i];
1440  p+=GetPixelChannels(image);
1441  }
1442  j++;
1443  }
1444  (void) memset(buffer,0,length*sizeof(*buffer));
1445  for (k=0; k < 4; k++)
1446  {
1447  Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1448  Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1449  Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1450  Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1451  }
1452  j=(ssize_t) image->columns+2;
1453  for (y=0; y < (ssize_t) image->rows; y++)
1454  {
1456  sync;
1457 
1458  Quantum
1459  *magick_restrict q;
1460 
1461  q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1462  1,exception);
1463  if (q == (Quantum *) NULL)
1464  {
1465  status=MagickFalse;
1466  continue;
1467  }
1468  j++;
1469  for (x=0; x < (ssize_t) image->columns; x++)
1470  {
1471  SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1472  q+=GetPixelChannels(despeckle_image);
1473  }
1474  sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1475  if (sync == MagickFalse)
1476  status=MagickFalse;
1477  j++;
1478  }
1479  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1480  {
1482  proceed;
1483 
1485  GetPixelChannels(image));
1486  if (proceed == MagickFalse)
1487  status=MagickFalse;
1488  }
1489  }
1490  despeckle_view=DestroyCacheView(despeckle_view);
1491  image_view=DestroyCacheView(image_view);
1492  buffer_info=RelinquishVirtualMemory(buffer_info);
1493  pixel_info=RelinquishVirtualMemory(pixel_info);
1494  despeckle_image->type=image->type;
1495  if (status == MagickFalse)
1496  despeckle_image=DestroyImage(despeckle_image);
1497  return(despeckle_image);
1498 }
1499 
1500 /*
1501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % %
1503 % %
1504 % %
1505 % E d g e I m a g e %
1506 % %
1507 % %
1508 % %
1509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1510 %
1511 % EdgeImage() finds edges in an image. Radius defines the radius of the
1512 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1513 % radius for you.
1514 %
1515 % The format of the EdgeImage method is:
1516 %
1517 % Image *EdgeImage(const Image *image,const double radius,
1518 % ExceptionInfo *exception)
1519 %
1520 % A description of each parameter follows:
1521 %
1522 % o image: the image.
1523 %
1524 % o radius: the radius of the pixel neighborhood.
1525 %
1526 % o exception: return any errors or warnings in this structure.
1527 %
1528 */
1529 MagickExport Image *EdgeImage(const Image *image,const double radius,
1530  ExceptionInfo *exception)
1531 {
1532  Image
1533  *edge_image;
1534 
1535  KernelInfo
1536  *kernel_info;
1537 
1538  ssize_t
1539  i;
1540 
1541  size_t
1542  width;
1543 
1544  assert(image != (const Image *) NULL);
1545  assert(image->signature == MagickCoreSignature);
1546  assert(exception != (ExceptionInfo *) NULL);
1547  assert(exception->signature == MagickCoreSignature);
1548  if (IsEventLogging() != MagickFalse)
1549  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1550  width=GetOptimalKernelWidth1D(radius,0.5);
1551  kernel_info=AcquireKernelInfo((const char *) NULL,exception);
1552  if (kernel_info == (KernelInfo *) NULL)
1553  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1554  (void) memset(kernel_info,0,sizeof(*kernel_info));
1555  kernel_info->width=width;
1556  kernel_info->height=width;
1557  kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1558  kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1559  kernel_info->signature=MagickCoreSignature;
1560  kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1561  AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1562  sizeof(*kernel_info->values)));
1563  if (kernel_info->values == (MagickRealType *) NULL)
1564  {
1565  kernel_info=DestroyKernelInfo(kernel_info);
1566  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1567  }
1568  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1569  kernel_info->values[i]=(-1.0);
1570  kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1571  edge_image=ConvolveImage(image,kernel_info,exception);
1572  kernel_info=DestroyKernelInfo(kernel_info);
1573  return(edge_image);
1574 }
1575 
1576 /*
1577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1578 % %
1579 % %
1580 % %
1581 % E m b o s s I m a g e %
1582 % %
1583 % %
1584 % %
1585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1586 %
1587 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1588 % We convolve the image with a Gaussian operator of the given radius and
1589 % standard deviation (sigma). For reasonable results, radius should be
1590 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1591 % radius for you.
1592 %
1593 % The format of the EmbossImage method is:
1594 %
1595 % Image *EmbossImage(const Image *image,const double radius,
1596 % const double sigma,ExceptionInfo *exception)
1597 %
1598 % A description of each parameter follows:
1599 %
1600 % o image: the image.
1601 %
1602 % o radius: the radius of the pixel neighborhood.
1603 %
1604 % o sigma: the standard deviation of the Gaussian, in pixels.
1605 %
1606 % o exception: return any errors or warnings in this structure.
1607 %
1608 */
1609 MagickExport Image *EmbossImage(const Image *image,const double radius,
1610  const double sigma,ExceptionInfo *exception)
1611 {
1612  double
1613  gamma,
1614  normalize;
1615 
1616  Image
1617  *emboss_image;
1618 
1619  KernelInfo
1620  *kernel_info;
1621 
1622  ssize_t
1623  i;
1624 
1625  size_t
1626  width;
1627 
1628  ssize_t
1629  j,
1630  k,
1631  u,
1632  v;
1633 
1634  assert(image != (const Image *) NULL);
1635  assert(image->signature == MagickCoreSignature);
1636  assert(exception != (ExceptionInfo *) NULL);
1637  assert(exception->signature == MagickCoreSignature);
1638  if (IsEventLogging() != MagickFalse)
1639  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1640  width=GetOptimalKernelWidth1D(radius,sigma);
1641  kernel_info=AcquireKernelInfo((const char *) NULL,exception);
1642  if (kernel_info == (KernelInfo *) NULL)
1643  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1644  kernel_info->width=width;
1645  kernel_info->height=width;
1646  kernel_info->x=(ssize_t) (width-1)/2;
1647  kernel_info->y=(ssize_t) (width-1)/2;
1648  kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1649  AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1650  sizeof(*kernel_info->values)));
1651  if (kernel_info->values == (MagickRealType *) NULL)
1652  {
1653  kernel_info=DestroyKernelInfo(kernel_info);
1654  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1655  }
1656  j=(ssize_t) (kernel_info->width-1)/2;
1657  k=j;
1658  i=0;
1659  for (v=(-j); v <= j; v++)
1660  {
1661  for (u=(-j); u <= j; u++)
1662  {
1663  kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1664  8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1666  if (u != k)
1667  kernel_info->values[i]=0.0;
1668  i++;
1669  }
1670  k--;
1671  }
1672  normalize=0.0;
1673  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1674  normalize+=kernel_info->values[i];
1675  gamma=PerceptibleReciprocal(normalize);
1676  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1677  kernel_info->values[i]*=gamma;
1678  emboss_image=ConvolveImage(image,kernel_info,exception);
1679  kernel_info=DestroyKernelInfo(kernel_info);
1680  if (emboss_image != (Image *) NULL)
1681  (void) EqualizeImage(emboss_image,exception);
1682  return(emboss_image);
1683 }
1684 
1685 /*
1686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687 % %
1688 % %
1689 % %
1690 % G a u s s i a n B l u r I m a g e %
1691 % %
1692 % %
1693 % %
1694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695 %
1696 % GaussianBlurImage() blurs an image. We convolve the image with a
1697 % Gaussian operator of the given radius and standard deviation (sigma).
1698 % For reasonable results, the radius should be larger than sigma. Use a
1699 % radius of 0 and GaussianBlurImage() selects a suitable radius for you.
1700 %
1701 % The format of the GaussianBlurImage method is:
1702 %
1703 % Image *GaussianBlurImage(const Image *image,onst double radius,
1704 % const double sigma,ExceptionInfo *exception)
1705 %
1706 % A description of each parameter follows:
1707 %
1708 % o image: the image.
1709 %
1710 % o radius: the radius of the Gaussian, in pixels, not counting the center
1711 % pixel.
1712 %
1713 % o sigma: the standard deviation of the Gaussian, in pixels.
1714 %
1715 % o exception: return any errors or warnings in this structure.
1716 %
1717 */
1718 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1719  const double sigma,ExceptionInfo *exception)
1720 {
1721  char
1722  geometry[MagickPathExtent];
1723 
1724  KernelInfo
1725  *kernel_info;
1726 
1727  Image
1728  *blur_image;
1729 
1730  assert(image != (const Image *) NULL);
1731  assert(image->signature == MagickCoreSignature);
1732  assert(exception != (ExceptionInfo *) NULL);
1733  assert(exception->signature == MagickCoreSignature);
1734  if (IsEventLogging() != MagickFalse)
1735  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1736  (void) FormatLocaleString(geometry,MagickPathExtent,"gaussian:%.20gx%.20g",
1737  radius,sigma);
1738  kernel_info=AcquireKernelInfo(geometry,exception);
1739  if (kernel_info == (KernelInfo *) NULL)
1740  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1741  blur_image=ConvolveImage(image,kernel_info,exception);
1742  kernel_info=DestroyKernelInfo(kernel_info);
1743  return(blur_image);
1744 }
1745 
1746 /*
1747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1748 % %
1749 % %
1750 % %
1751 % K u w a h a r a I m a g e %
1752 % %
1753 % %
1754 % %
1755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1756 %
1757 % KuwaharaImage() is an edge preserving noise reduction filter.
1758 %
1759 % The format of the KuwaharaImage method is:
1760 %
1761 % Image *KuwaharaImage(const Image *image,const double radius,
1762 % const double sigma,ExceptionInfo *exception)
1763 %
1764 % A description of each parameter follows:
1765 %
1766 % o image: the image.
1767 %
1768 % o radius: the square window radius.
1769 %
1770 % o sigma: the standard deviation of the Gaussian, in pixels.
1771 %
1772 % o exception: return any errors or warnings in this structure.
1773 %
1774 */
1775 
1777  const double *magick_restrict pixel)
1778 {
1779  return(0.212656f*pixel[image->channel_map[RedPixelChannel].offset]+
1780  0.715158f*pixel[image->channel_map[GreenPixelChannel].offset]+
1781  0.072186f*pixel[image->channel_map[BluePixelChannel].offset]); /* Rec709 */
1782 }
1783 
1784 MagickExport Image *KuwaharaImage(const Image *image,const double radius,
1785  const double sigma,ExceptionInfo *exception)
1786 {
1787 #define KuwaharaImageTag "Kuwahara/Image"
1788 
1789  CacheView
1790  *image_view,
1791  *kuwahara_view;
1792 
1793  Image
1794  *gaussian_image,
1795  *kuwahara_image;
1796 
1798  status;
1799 
1801  progress;
1802 
1803  size_t
1804  width;
1805 
1806  ssize_t
1807  y;
1808 
1809  /*
1810  Initialize Kuwahara image attributes.
1811  */
1812  assert(image != (Image *) NULL);
1813  assert(image->signature == MagickCoreSignature);
1814  assert(exception != (ExceptionInfo *) NULL);
1815  assert(exception->signature == MagickCoreSignature);
1816  if (IsEventLogging() != MagickFalse)
1817  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1818  width=(size_t) radius+1;
1819  gaussian_image=BlurImage(image,radius,sigma,exception);
1820  if (gaussian_image == (Image *) NULL)
1821  return((Image *) NULL);
1822  kuwahara_image=CloneImage(image,0,0,MagickTrue,exception);
1823  if (kuwahara_image == (Image *) NULL)
1824  {
1825  gaussian_image=DestroyImage(gaussian_image);
1826  return((Image *) NULL);
1827  }
1828  if (SetImageStorageClass(kuwahara_image,DirectClass,exception) == MagickFalse)
1829  {
1830  gaussian_image=DestroyImage(gaussian_image);
1831  kuwahara_image=DestroyImage(kuwahara_image);
1832  return((Image *) NULL);
1833  }
1834  /*
1835  Edge preserving noise reduction filter.
1836  */
1837  status=MagickTrue;
1838  progress=0;
1839  image_view=AcquireVirtualCacheView(gaussian_image,exception);
1840  kuwahara_view=AcquireAuthenticCacheView(kuwahara_image,exception);
1841 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1842  #pragma omp parallel for schedule(static) shared(progress,status) \
1843  magick_number_threads(image,kuwahara_image,gaussian_image->rows,1)
1844 #endif
1845  for (y=0; y < (ssize_t) gaussian_image->rows; y++)
1846  {
1847  Quantum
1848  *magick_restrict q;
1849 
1850  ssize_t
1851  x;
1852 
1853  if (status == MagickFalse)
1854  continue;
1855  q=QueueCacheViewAuthenticPixels(kuwahara_view,0,y,kuwahara_image->columns,1,
1856  exception);
1857  if (q == (Quantum *) NULL)
1858  {
1859  status=MagickFalse;
1860  continue;
1861  }
1862  for (x=0; x < (ssize_t) gaussian_image->columns; x++)
1863  {
1864  const Quantum
1865  *magick_restrict p;
1866 
1867  double
1868  min_variance;
1869 
1871  quadrant,
1872  target;
1873 
1874  size_t
1875  i;
1876 
1877  min_variance=MagickMaximumValue;
1878  SetGeometry(gaussian_image,&target);
1879  quadrant.width=width;
1880  quadrant.height=width;
1881  for (i=0; i < 4; i++)
1882  {
1883  const Quantum
1884  *magick_restrict k;
1885 
1886  double
1887  mean[MaxPixelChannels],
1888  variance;
1889 
1890  ssize_t
1891  n;
1892 
1893  ssize_t
1894  j;
1895 
1896  quadrant.x=x;
1897  quadrant.y=y;
1898  switch (i)
1899  {
1900  case 0:
1901  {
1902  quadrant.x=x-(ssize_t) (width-1);
1903  quadrant.y=y-(ssize_t) (width-1);
1904  break;
1905  }
1906  case 1:
1907  {
1908  quadrant.y=y-(ssize_t) (width-1);
1909  break;
1910  }
1911  case 2:
1912  {
1913  quadrant.x=x-(ssize_t) (width-1);
1914  break;
1915  }
1916  case 3:
1917  default:
1918  break;
1919  }
1920  p=GetCacheViewVirtualPixels(image_view,quadrant.x,quadrant.y,
1921  quadrant.width,quadrant.height,exception);
1922  if (p == (const Quantum *) NULL)
1923  break;
1924  for (j=0; j < (ssize_t) GetPixelChannels(gaussian_image); j++)
1925  mean[j]=0.0;
1926  k=p;
1927  for (n=0; n < (ssize_t) (width*width); n++)
1928  {
1929  for (j=0; j < (ssize_t) GetPixelChannels(gaussian_image); j++)
1930  mean[j]+=(double) k[j];
1931  k+=GetPixelChannels(gaussian_image);
1932  }
1933  for (j=0; j < (ssize_t) GetPixelChannels(gaussian_image); j++)
1934  mean[j]/=(double) (width*width);
1935  k=p;
1936  variance=0.0;
1937  for (n=0; n < (ssize_t) (width*width); n++)
1938  {
1939  double
1940  luma;
1941 
1942  luma=GetPixelLuma(gaussian_image,k);
1943  variance+=(luma-GetMeanLuma(gaussian_image,mean))*
1944  (luma-GetMeanLuma(gaussian_image,mean));
1945  k+=GetPixelChannels(gaussian_image);
1946  }
1947  if (variance < min_variance)
1948  {
1949  min_variance=variance;
1950  target=quadrant;
1951  }
1952  }
1953  if (i < 4)
1954  {
1955  status=MagickFalse;
1956  break;
1957  }
1958  status=InterpolatePixelChannels(gaussian_image,image_view,kuwahara_image,
1959  UndefinedInterpolatePixel,(double) target.x+target.width/2.0,(double)
1960  target.y+target.height/2.0,q,exception);
1961  if (status == MagickFalse)
1962  break;
1963  q+=GetPixelChannels(kuwahara_image);
1964  }
1965  if (SyncCacheViewAuthenticPixels(kuwahara_view,exception) == MagickFalse)
1966  status=MagickFalse;
1967  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1968  {
1970  proceed;
1971 
1972 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1973  #pragma omp atomic
1974 #endif
1975  progress++;
1976  proceed=SetImageProgress(image,KuwaharaImageTag,progress,image->rows);
1977  if (proceed == MagickFalse)
1978  status=MagickFalse;
1979  }
1980  }
1981  kuwahara_view=DestroyCacheView(kuwahara_view);
1982  image_view=DestroyCacheView(image_view);
1983  gaussian_image=DestroyImage(gaussian_image);
1984  if (status == MagickFalse)
1985  kuwahara_image=DestroyImage(kuwahara_image);
1986  return(kuwahara_image);
1987 }
1988 
1989 /*
1990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1991 % %
1992 % %
1993 % %
1994 % L o c a l C o n t r a s t I m a g e %
1995 % %
1996 % %
1997 % %
1998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1999 %
2000 % LocalContrastImage() attempts to increase the appearance of large-scale
2001 % light-dark transitions. Local contrast enhancement works similarly to
2002 % sharpening with an unsharp mask, however the mask is instead created using
2003 % an image with a greater blur distance.
2004 %
2005 % The format of the LocalContrastImage method is:
2006 %
2007 % Image *LocalContrastImage(const Image *image, const double radius,
2008 % const double strength,ExceptionInfo *exception)
2009 %
2010 % A description of each parameter follows:
2011 %
2012 % o image: the image.
2013 %
2014 % o radius: the radius of the Gaussian blur, in percentage with 100%
2015 % resulting in a blur radius of 20% of largest dimension.
2016 %
2017 % o strength: the strength of the blur mask in percentage.
2018 %
2019 % o exception: return any errors or warnings in this structure.
2020 %
2021 */
2022 MagickExport Image *LocalContrastImage(const Image *image,const double radius,
2023  const double strength,ExceptionInfo *exception)
2024 {
2025 #define LocalContrastImageTag "LocalContrast/Image"
2026 
2027  CacheView
2028  *image_view,
2029  *contrast_view;
2030 
2031  float
2032  *interImage,
2033  *scanline,
2034  totalWeight;
2035 
2036  Image
2037  *contrast_image;
2038 
2040  status;
2041 
2042  MemoryInfo
2043  *scanline_info,
2044  *interImage_info;
2045 
2046  ssize_t
2047  scanLineSize,
2048  width;
2049 
2050  /*
2051  Initialize contrast image attributes.
2052  */
2053  assert(image != (const Image *) NULL);
2054  assert(image->signature == MagickCoreSignature);
2055  assert(exception != (ExceptionInfo *) NULL);
2056  assert(exception->signature == MagickCoreSignature);
2057  if (IsEventLogging() != MagickFalse)
2058  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2059 #if defined(MAGICKCORE_OPENCL_SUPPORT)
2060  contrast_image=AccelerateLocalContrastImage(image,radius,strength,exception);
2061  if (contrast_image != (Image *) NULL)
2062  return(contrast_image);
2063 #endif
2064  contrast_image=CloneImage(image,0,0,MagickTrue,exception);
2065  if (contrast_image == (Image *) NULL)
2066  return((Image *) NULL);
2067  if (SetImageStorageClass(contrast_image,DirectClass,exception) == MagickFalse)
2068  {
2069  contrast_image=DestroyImage(contrast_image);
2070  return((Image *) NULL);
2071  }
2072  image_view=AcquireVirtualCacheView(image,exception);
2073  contrast_view=AcquireAuthenticCacheView(contrast_image,exception);
2074  scanLineSize=(ssize_t) MagickMax(image->columns,image->rows);
2075  width=(ssize_t) scanLineSize*0.002f*fabs(radius);
2076  scanLineSize+=(2*width);
2077  scanline_info=AcquireVirtualMemory((size_t) GetOpenMPMaximumThreads()*
2078  scanLineSize,sizeof(*scanline));
2079  if (scanline_info == (MemoryInfo *) NULL)
2080  {
2081  contrast_view=DestroyCacheView(contrast_view);
2082  image_view=DestroyCacheView(image_view);
2083  contrast_image=DestroyImage(contrast_image);
2084  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2085  }
2086  scanline=(float *) GetVirtualMemoryBlob(scanline_info);
2087  /*
2088  Create intermediate buffer.
2089  */
2090  interImage_info=AcquireVirtualMemory(image->rows*(image->columns+(2*width)),
2091  sizeof(*interImage));
2092  if (interImage_info == (MemoryInfo *) NULL)
2093  {
2094  scanline_info=RelinquishVirtualMemory(scanline_info);
2095  contrast_view=DestroyCacheView(contrast_view);
2096  image_view=DestroyCacheView(image_view);
2097  contrast_image=DestroyImage(contrast_image);
2098  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2099  }
2100  interImage=(float *) GetVirtualMemoryBlob(interImage_info);
2101  totalWeight=(float) ((width+1)*(width+1));
2102  /*
2103  Vertical pass.
2104  */
2105  status=MagickTrue;
2106  {
2107  ssize_t
2108  x;
2109 
2110 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2111 #pragma omp parallel for schedule(static) \
2112  magick_number_threads(image,image,image->columns,1)
2113 #endif
2114  for (x=0; x < (ssize_t) image->columns; x++)
2115  {
2116  const int
2117  id = GetOpenMPThreadId();
2118 
2119  const Quantum
2120  *magick_restrict p;
2121 
2122  float
2123  *out,
2124  *pix,
2125  *pixels;
2126 
2127  ssize_t
2128  y;
2129 
2130  ssize_t
2131  i;
2132 
2133  if (status == MagickFalse)
2134  continue;
2135  pixels=scanline;
2136  pixels+=id*scanLineSize;
2137  pix=pixels;
2138  p=GetCacheViewVirtualPixels(image_view,x,-width,1,image->rows+(2*width),
2139  exception);
2140  if (p == (const Quantum *) NULL)
2141  {
2142  status=MagickFalse;
2143  continue;
2144  }
2145  for (y=0; y < (ssize_t) image->rows+(2*width); y++)
2146  {
2147  *pix++=(float)GetPixelLuma(image,p);
2148  p+=image->number_channels;
2149  }
2150  out=interImage+x+width;
2151  for (y=0; y < (ssize_t) image->rows; y++)
2152  {
2153  float
2154  sum,
2155  weight;
2156 
2157  weight=1.0f;
2158  sum=0;
2159  pix=pixels+y;
2160  for (i=0; i < width; i++)
2161  {
2162  sum+=weight*(*pix++);
2163  weight+=1.0f;
2164  }
2165  for (i=width+1; i < (2*width); i++)
2166  {
2167  sum+=weight*(*pix++);
2168  weight-=1.0f;
2169  }
2170  /* write to output */
2171  *out=sum/totalWeight;
2172  /* mirror into padding */
2173  if (x <= width && x != 0)
2174  *(out-(x*2))=*out;
2175  if ((x > (ssize_t) image->columns-width-2) &&
2176  (x != (ssize_t) image->columns-1))
2177  *(out+((image->columns-x-1)*2))=*out;
2178  out+=image->columns+(width*2);
2179  }
2180  }
2181  }
2182  /*
2183  Horizontal pass.
2184  */
2185  {
2186  ssize_t
2187  y;
2188 
2189 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2190 #pragma omp parallel for schedule(static) \
2191  magick_number_threads(image,image,image->rows,1)
2192 #endif
2193  for (y=0; y < (ssize_t) image->rows; y++)
2194  {
2195  const int
2196  id = GetOpenMPThreadId();
2197 
2198  const Quantum
2199  *magick_restrict p;
2200 
2201  float
2202  *pix,
2203  *pixels;
2204 
2205  Quantum
2206  *magick_restrict q;
2207 
2208  ssize_t
2209  x;
2210 
2211  ssize_t
2212  i;
2213 
2214  if (status == MagickFalse)
2215  continue;
2216  pixels=scanline;
2217  pixels+=id*scanLineSize;
2218  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2219  q=GetCacheViewAuthenticPixels(contrast_view,0,y,image->columns,1,
2220  exception);
2221  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2222  {
2223  status=MagickFalse;
2224  continue;
2225  }
2226  memcpy(pixels,interImage+(y*(image->columns+(2*width))),(image->columns+
2227  (2*width))*sizeof(float));
2228  for (x=0; x < (ssize_t) image->columns; x++)
2229  {
2230  float
2231  mult,
2232  srcVal,
2233  sum,
2234  weight;
2235 
2236  PixelTrait
2237  traits;
2238 
2239  weight=1.0f;
2240  sum=0;
2241  pix=pixels+x;
2242  for (i=0; i < width; i++)
2243  {
2244  sum+=weight*(*pix++);
2245  weight+=1.0f;
2246  }
2247  for (i=width+1; i < (2*width); i++)
2248  {
2249  sum+=weight*(*pix++);
2250  weight-=1.0f;
2251  }
2252  /* Apply and write */
2253  srcVal=(float) GetPixelLuma(image,p);
2254  mult=(srcVal-(sum/totalWeight))*(strength/100.0f);
2255  mult=(srcVal+mult)/srcVal;
2256  traits=GetPixelChannelTraits(image,RedPixelChannel);
2257  if ((traits & UpdatePixelTrait) != 0)
2258  SetPixelRed(contrast_image,ClampToQuantum((MagickRealType)
2259  GetPixelRed(image,p)*mult),q);
2261  if ((traits & UpdatePixelTrait) != 0)
2263  GetPixelGreen(image,p)*mult),q);
2265  if ((traits & UpdatePixelTrait) != 0)
2266  SetPixelBlue(contrast_image,ClampToQuantum((MagickRealType)
2267  GetPixelBlue(image,p)*mult),q);
2268  p+=image->number_channels;
2269  q+=contrast_image->number_channels;
2270  }
2271  if (SyncCacheViewAuthenticPixels(contrast_view,exception) == MagickFalse)
2272  status=MagickFalse;
2273  }
2274  }
2275  scanline_info=RelinquishVirtualMemory(scanline_info);
2276  interImage_info=RelinquishVirtualMemory(interImage_info);
2277  contrast_view=DestroyCacheView(contrast_view);
2278  image_view=DestroyCacheView(image_view);
2279  if (status == MagickFalse)
2280  contrast_image=DestroyImage(contrast_image);
2281  return(contrast_image);
2282 }
2283 
2284 /*
2285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286 % %
2287 % %
2288 % %
2289 % M o t i o n B l u r I m a g e %
2290 % %
2291 % %
2292 % %
2293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2294 %
2295 % MotionBlurImage() simulates motion blur. We convolve the image with a
2296 % Gaussian operator of the given radius and standard deviation (sigma).
2297 % For reasonable results, radius should be larger than sigma. Use a
2298 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2299 % Angle gives the angle of the blurring motion.
2300 %
2301 % Andrew Protano contributed this effect.
2302 %
2303 % The format of the MotionBlurImage method is:
2304 %
2305 % Image *MotionBlurImage(const Image *image,const double radius,
2306 % const double sigma,const double angle,ExceptionInfo *exception)
2307 %
2308 % A description of each parameter follows:
2309 %
2310 % o image: the image.
2311 %
2312 % o radius: the radius of the Gaussian, in pixels, not counting
2313 % the center pixel.
2314 %
2315 % o sigma: the standard deviation of the Gaussian, in pixels.
2316 %
2317 % o angle: Apply the effect along this angle.
2318 %
2319 % o exception: return any errors or warnings in this structure.
2320 %
2321 */
2322 
2323 static MagickRealType *GetMotionBlurKernel(const size_t width,
2324  const double sigma)
2325 {
2327  *kernel,
2328  normalize;
2329 
2330  ssize_t
2331  i;
2332 
2333  /*
2334  Generate a 1-D convolution kernel.
2335  */
2336  if (IsEventLogging() != MagickFalse)
2337  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2339  width,sizeof(*kernel)));
2340  if (kernel == (MagickRealType *) NULL)
2341  return(kernel);
2342  normalize=0.0;
2343  for (i=0; i < (ssize_t) width; i++)
2344  {
2345  kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2347  normalize+=kernel[i];
2348  }
2349  for (i=0; i < (ssize_t) width; i++)
2350  kernel[i]/=normalize;
2351  return(kernel);
2352 }
2353 
2354 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2355  const double sigma,const double angle,ExceptionInfo *exception)
2356 {
2357 #define BlurImageTag "Blur/Image"
2358 
2359  CacheView
2360  *blur_view,
2361  *image_view,
2362  *motion_view;
2363 
2364  Image
2365  *blur_image;
2366 
2368  status;
2369 
2371  progress;
2372 
2374  *kernel;
2375 
2376  OffsetInfo
2377  *offset;
2378 
2379  PointInfo
2380  point;
2381 
2382  size_t
2383  width;
2384 
2385  ssize_t
2386  w,
2387  y;
2388 
2389  assert(image != (Image *) NULL);
2390  assert(image->signature == MagickCoreSignature);
2391  assert(exception != (ExceptionInfo *) NULL);
2392  assert(exception->signature == MagickCoreSignature);
2393  if (IsEventLogging() != MagickFalse)
2394  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2395  width=GetOptimalKernelWidth1D(radius,sigma);
2396  kernel=GetMotionBlurKernel(width,sigma);
2397  if (kernel == (MagickRealType *) NULL)
2398  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2399  offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2400  if (offset == (OffsetInfo *) NULL)
2401  {
2402  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2403  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2404  }
2405  point.x=(double) width*sin(DegreesToRadians(angle));
2406  point.y=(double) width*cos(DegreesToRadians(angle));
2407  for (w=0; w < (ssize_t) width; w++)
2408  {
2409  offset[w].x=CastDoubleToLong(ceil((double) (w*point.y)/
2410  hypot(point.x,point.y)-0.5));
2411  offset[w].y=CastDoubleToLong(ceil((double) (w*point.x)/
2412  hypot(point.x,point.y)-0.5));
2413  }
2414  /*
2415  Motion blur image.
2416  */
2417 #if defined(MAGICKCORE_OPENCL_SUPPORT)
2418  blur_image=AccelerateMotionBlurImage(image,kernel,width,offset,exception);
2419  if (blur_image != (Image *) NULL)
2420  {
2421  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2422  offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2423  return(blur_image);
2424  }
2425 #endif
2426  blur_image=CloneImage(image,0,0,MagickTrue,exception);
2427  if (blur_image == (Image *) NULL)
2428  {
2429  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2430  offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2431  return((Image *) NULL);
2432  }
2433  if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2434  {
2435  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2436  offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2437  blur_image=DestroyImage(blur_image);
2438  return((Image *) NULL);
2439  }
2440  status=MagickTrue;
2441  progress=0;
2442  image_view=AcquireVirtualCacheView(image,exception);
2443  motion_view=AcquireVirtualCacheView(image,exception);
2444  blur_view=AcquireAuthenticCacheView(blur_image,exception);
2445 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2446  #pragma omp parallel for schedule(static) shared(progress,status) \
2447  magick_number_threads(image,blur_image,image->rows,1)
2448 #endif
2449  for (y=0; y < (ssize_t) image->rows; y++)
2450  {
2451  const Quantum
2452  *magick_restrict p;
2453 
2454  Quantum
2455  *magick_restrict q;
2456 
2457  ssize_t
2458  x;
2459 
2460  if (status == MagickFalse)
2461  continue;
2462  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2463  q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2464  exception);
2465  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2466  {
2467  status=MagickFalse;
2468  continue;
2469  }
2470  for (x=0; x < (ssize_t) image->columns; x++)
2471  {
2472  ssize_t
2473  i;
2474 
2475  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2476  {
2477  double
2478  alpha,
2479  gamma,
2480  pixel;
2481 
2482  PixelChannel
2483  channel;
2484 
2485  PixelTrait
2486  blur_traits,
2487  traits;
2488 
2489  const Quantum
2490  *magick_restrict r;
2491 
2493  *magick_restrict k;
2494 
2495  ssize_t
2496  j;
2497 
2498  channel=GetPixelChannelChannel(image,i);
2499  traits=GetPixelChannelTraits(image,channel);
2500  blur_traits=GetPixelChannelTraits(blur_image,channel);
2501  if ((traits == UndefinedPixelTrait) ||
2502  (blur_traits == UndefinedPixelTrait))
2503  continue;
2504  if ((blur_traits & CopyPixelTrait) != 0)
2505  {
2506  SetPixelChannel(blur_image,channel,p[i],q);
2507  continue;
2508  }
2509  k=kernel;
2510  pixel=0.0;
2511  if ((blur_traits & BlendPixelTrait) == 0)
2512  {
2513  for (j=0; j < (ssize_t) width; j++)
2514  {
2515  r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2516  offset[j].y,1,1,exception);
2517  if (r == (const Quantum *) NULL)
2518  {
2519  status=MagickFalse;
2520  continue;
2521  }
2522  pixel+=(*k)*r[i];
2523  k++;
2524  }
2525  SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2526  continue;
2527  }
2528  alpha=0.0;
2529  gamma=0.0;
2530  for (j=0; j < (ssize_t) width; j++)
2531  {
2532  r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2533  1,exception);
2534  if (r == (const Quantum *) NULL)
2535  {
2536  status=MagickFalse;
2537  continue;
2538  }
2539  alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2540  pixel+=(*k)*alpha*r[i];
2541  gamma+=(*k)*alpha;
2542  k++;
2543  }
2544  gamma=PerceptibleReciprocal(gamma);
2545  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2546  }
2547  p+=GetPixelChannels(image);
2548  q+=GetPixelChannels(blur_image);
2549  }
2550  if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2551  status=MagickFalse;
2552  if (image->progress_monitor != (MagickProgressMonitor) NULL)
2553  {
2555  proceed;
2556 
2557 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2558  #pragma omp atomic
2559 #endif
2560  progress++;
2561  proceed=SetImageProgress(image,BlurImageTag,progress,image->rows);
2562  if (proceed == MagickFalse)
2563  status=MagickFalse;
2564  }
2565  }
2566  blur_view=DestroyCacheView(blur_view);
2567  motion_view=DestroyCacheView(motion_view);
2568  image_view=DestroyCacheView(image_view);
2569  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2570  offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2571  if (status == MagickFalse)
2572  blur_image=DestroyImage(blur_image);
2573  return(blur_image);
2574 }
2575 
2576 /*
2577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2578 % %
2579 % %
2580 % %
2581 % P r e v i e w I m a g e %
2582 % %
2583 % %
2584 % %
2585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2586 %
2587 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2588 % processing operation applied with varying parameters. This may be helpful
2589 % pin-pointing an appropriate parameter for a particular image processing
2590 % operation.
2591 %
2592 % The format of the PreviewImages method is:
2593 %
2594 % Image *PreviewImages(const Image *image,const PreviewType preview,
2595 % ExceptionInfo *exception)
2596 %
2597 % A description of each parameter follows:
2598 %
2599 % o image: the image.
2600 %
2601 % o preview: the image processing operation.
2602 %
2603 % o exception: return any errors or warnings in this structure.
2604 %
2605 */
2606 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2607  ExceptionInfo *exception)
2608 {
2609 #define NumberTiles 9
2610 #define PreviewImageTag "Preview/Image"
2611 #define DefaultPreviewGeometry "204x204+10+10"
2612 
2613  char
2614  factor[MagickPathExtent],
2615  label[MagickPathExtent];
2616 
2617  double
2618  degrees,
2619  gamma,
2620  percentage,
2621  radius,
2622  sigma,
2623  threshold;
2624 
2625  Image
2626  *images,
2627  *montage_image,
2628  *preview_image,
2629  *thumbnail;
2630 
2631  ImageInfo
2632  *preview_info;
2633 
2635  proceed;
2636 
2637  MontageInfo
2638  *montage_info;
2639 
2640  QuantizeInfo
2641  quantize_info;
2642 
2644  geometry;
2645 
2646  ssize_t
2647  i,
2648  x;
2649 
2650  size_t
2651  colors;
2652 
2653  ssize_t
2654  y;
2655 
2656  /*
2657  Open output image file.
2658  */
2659  assert(image != (Image *) NULL);
2660  assert(image->signature == MagickCoreSignature);
2661  if (IsEventLogging() != MagickFalse)
2662  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2663  colors=2;
2664  degrees=0.0;
2665  gamma=(-0.2f);
2666  preview_info=AcquireImageInfo();
2667  SetGeometry(image,&geometry);
2668  (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2669  &geometry.width,&geometry.height);
2670  images=NewImageList();
2671  percentage=12.5;
2672  GetQuantizeInfo(&quantize_info);
2673  radius=0.0;
2674  sigma=1.0;
2675  threshold=0.0;
2676  x=0;
2677  y=0;
2678  for (i=0; i < NumberTiles; i++)
2679  {
2680  thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2681  if (thumbnail == (Image *) NULL)
2682  break;
2683  (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2684  (void *) NULL);
2685  (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2686  if (i == (NumberTiles/2))
2687  {
2688  (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2689  &thumbnail->matte_color,exception);
2690  AppendImageToList(&images,thumbnail);
2691  continue;
2692  }
2693  switch (preview)
2694  {
2695  case RotatePreview:
2696  {
2697  degrees+=45.0;
2698  preview_image=RotateImage(thumbnail,degrees,exception);
2699  (void) FormatLocaleString(label,MagickPathExtent,"rotate %g",degrees);
2700  break;
2701  }
2702  case ShearPreview:
2703  {
2704  degrees+=5.0;
2705  preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2706  (void) FormatLocaleString(label,MagickPathExtent,"shear %gx%g",degrees,
2707  2.0*degrees);
2708  break;
2709  }
2710  case RollPreview:
2711  {
2712  x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2713  y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2714  preview_image=RollImage(thumbnail,x,y,exception);
2715  (void) FormatLocaleString(label,MagickPathExtent,"roll %+.20gx%+.20g",
2716  (double) x,(double) y);
2717  break;
2718  }
2719  case HuePreview:
2720  {
2721  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2722  if (preview_image == (Image *) NULL)
2723  break;
2724  (void) FormatLocaleString(factor,MagickPathExtent,"100,100,%g",2.0*
2725  percentage);
2726  (void) ModulateImage(preview_image,factor,exception);
2727  (void) FormatLocaleString(label,MagickPathExtent,"modulate %s",factor);
2728  break;
2729  }
2730  case SaturationPreview:
2731  {
2732  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2733  if (preview_image == (Image *) NULL)
2734  break;
2735  (void) FormatLocaleString(factor,MagickPathExtent,"100,%g",2.0*
2736  percentage);
2737  (void) ModulateImage(preview_image,factor,exception);
2738  (void) FormatLocaleString(label,MagickPathExtent,"modulate %s",factor);
2739  break;
2740  }
2741  case BrightnessPreview:
2742  {
2743  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2744  if (preview_image == (Image *) NULL)
2745  break;
2746  (void) FormatLocaleString(factor,MagickPathExtent,"%g",2.0*percentage);
2747  (void) ModulateImage(preview_image,factor,exception);
2748  (void) FormatLocaleString(label,MagickPathExtent,"modulate %s",factor);
2749  break;
2750  }
2751  case GammaPreview:
2752  default:
2753  {
2754  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2755  if (preview_image == (Image *) NULL)
2756  break;
2757  gamma+=0.4f;
2758  (void) GammaImage(preview_image,gamma,exception);
2759  (void) FormatLocaleString(label,MagickPathExtent,"gamma %g",gamma);
2760  break;
2761  }
2762  case SpiffPreview:
2763  {
2764  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2765  if (preview_image != (Image *) NULL)
2766  for (x=0; x < i; x++)
2767  (void) ContrastImage(preview_image,MagickTrue,exception);
2768  (void) FormatLocaleString(label,MagickPathExtent,"contrast (%.20g)",
2769  (double) i+1);
2770  break;
2771  }
2772  case DullPreview:
2773  {
2774  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2775  if (preview_image == (Image *) NULL)
2776  break;
2777  for (x=0; x < i; x++)
2778  (void) ContrastImage(preview_image,MagickFalse,exception);
2779  (void) FormatLocaleString(label,MagickPathExtent,"+contrast (%.20g)",
2780  (double) i+1);
2781  break;
2782  }
2783  case GrayscalePreview:
2784  {
2785  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2786  if (preview_image == (Image *) NULL)
2787  break;
2788  colors<<=1;
2789  quantize_info.number_colors=colors;
2790  quantize_info.colorspace=GRAYColorspace;
2791  (void) QuantizeImage(&quantize_info,preview_image,exception);
2792  (void) FormatLocaleString(label,MagickPathExtent,
2793  "-colorspace gray -colors %.20g",(double) colors);
2794  break;
2795  }
2796  case QuantizePreview:
2797  {
2798  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2799  if (preview_image == (Image *) NULL)
2800  break;
2801  colors<<=1;
2802  quantize_info.number_colors=colors;
2803  (void) QuantizeImage(&quantize_info,preview_image,exception);
2804  (void) FormatLocaleString(label,MagickPathExtent,"colors %.20g",
2805  (double) colors);
2806  break;
2807  }
2808  case DespecklePreview:
2809  {
2810  for (x=0; x < (i-1); x++)
2811  {
2812  preview_image=DespeckleImage(thumbnail,exception);
2813  if (preview_image == (Image *) NULL)
2814  break;
2815  thumbnail=DestroyImage(thumbnail);
2816  thumbnail=preview_image;
2817  }
2818  preview_image=DespeckleImage(thumbnail,exception);
2819  if (preview_image == (Image *) NULL)
2820  break;
2821  (void) FormatLocaleString(label,MagickPathExtent,"despeckle (%.20g)",
2822  (double) i+1);
2823  break;
2824  }
2825  case ReduceNoisePreview:
2826  {
2827  preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t)
2828  radius,(size_t) radius,exception);
2829  (void) FormatLocaleString(label,MagickPathExtent,"noise %g",radius);
2830  break;
2831  }
2832  case AddNoisePreview:
2833  {
2834  switch ((int) i)
2835  {
2836  case 0:
2837  {
2838  (void) CopyMagickString(factor,"uniform",MagickPathExtent);
2839  break;
2840  }
2841  case 1:
2842  {
2843  (void) CopyMagickString(factor,"gaussian",MagickPathExtent);
2844  break;
2845  }
2846  case 2:
2847  {
2848  (void) CopyMagickString(factor,"multiplicative",MagickPathExtent);
2849  break;
2850  }
2851  case 3:
2852  {
2853  (void) CopyMagickString(factor,"impulse",MagickPathExtent);
2854  break;
2855  }
2856  case 5:
2857  {
2858  (void) CopyMagickString(factor,"laplacian",MagickPathExtent);
2859  break;
2860  }
2861  case 6:
2862  {
2863  (void) CopyMagickString(factor,"Poisson",MagickPathExtent);
2864  break;
2865  }
2866  default:
2867  {
2868  (void) CopyMagickString(thumbnail->magick,"NULL",MagickPathExtent);
2869  break;
2870  }
2871  }
2872  preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2873  (size_t) i,exception);
2874  (void) FormatLocaleString(label,MagickPathExtent,"+noise %s",factor);
2875  break;
2876  }
2877  case SharpenPreview:
2878  {
2879  preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2880  (void) FormatLocaleString(label,MagickPathExtent,"sharpen %gx%g",
2881  radius,sigma);
2882  break;
2883  }
2884  case BlurPreview:
2885  {
2886  preview_image=BlurImage(thumbnail,radius,sigma,exception);
2887  (void) FormatLocaleString(label,MagickPathExtent,"blur %gx%g",radius,
2888  sigma);
2889  break;
2890  }
2891  case ThresholdPreview:
2892  {
2893  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2894  if (preview_image == (Image *) NULL)
2895  break;
2896  (void) BilevelImage(thumbnail,(double) (percentage*((double)
2897  QuantumRange+1.0))/100.0,exception);
2898  (void) FormatLocaleString(label,MagickPathExtent,"threshold %g",
2899  (double) (percentage*((double) QuantumRange+1.0))/100.0);
2900  break;
2901  }
2902  case EdgeDetectPreview:
2903  {
2904  preview_image=EdgeImage(thumbnail,radius,exception);
2905  (void) FormatLocaleString(label,MagickPathExtent,"edge %g",radius);
2906  break;
2907  }
2908  case SpreadPreview:
2909  {
2910  preview_image=SpreadImage(thumbnail,image->interpolate,radius,
2911  exception);
2912  (void) FormatLocaleString(label,MagickPathExtent,"spread %g",
2913  radius+0.5);
2914  break;
2915  }
2916  case SolarizePreview:
2917  {
2918  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2919  if (preview_image == (Image *) NULL)
2920  break;
2921  (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2922  100.0,exception);
2923  (void) FormatLocaleString(label,MagickPathExtent,"solarize %g",
2924  (QuantumRange*percentage)/100.0);
2925  break;
2926  }
2927  case ShadePreview:
2928  {
2929  degrees+=10.0;
2930  preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2931  exception);
2932  (void) FormatLocaleString(label,MagickPathExtent,"shade %gx%g",degrees,
2933  degrees);
2934  break;
2935  }
2936  case RaisePreview:
2937  {
2939  raise;
2940 
2941  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2942  if (preview_image == (Image *) NULL)
2943  break;
2944  raise.width=(size_t) (2*i+2);
2945  raise.height=(size_t) (2*i+2);
2946  raise.x=(i-1)/2;
2947  raise.y=(i-1)/2;
2948  (void) RaiseImage(preview_image,&raise,MagickTrue,exception);
2949  (void) FormatLocaleString(label,MagickPathExtent,
2950  "raise %.20gx%.20g%+.20g%+.20g",(double) raise.width,(double)
2951  raise.height,(double) raise.x,(double) raise.y);
2952  break;
2953  }
2954  case SegmentPreview:
2955  {
2956  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2957  if (preview_image == (Image *) NULL)
2958  break;
2959  threshold+=0.4f;
2960  (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2961  threshold,exception);
2962  (void) FormatLocaleString(label,MagickPathExtent,"segment %gx%g",
2963  threshold,threshold);
2964  break;
2965  }
2966  case SwirlPreview:
2967  {
2968  preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2969  exception);
2970  (void) FormatLocaleString(label,MagickPathExtent,"swirl %g",degrees);
2971  degrees+=45.0;
2972  break;
2973  }
2974  case ImplodePreview:
2975  {
2976  degrees+=0.1f;
2977  preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2978  exception);
2979  (void) FormatLocaleString(label,MagickPathExtent,"implode %g",degrees);
2980  break;
2981  }
2982  case WavePreview:
2983  {
2984  degrees+=5.0f;
2985  preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2986  image->interpolate,exception);
2987  (void) FormatLocaleString(label,MagickPathExtent,"wave %gx%g",0.5*
2988  degrees,2.0*degrees);
2989  break;
2990  }
2991  case OilPaintPreview:
2992  {
2993  preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2994  exception);
2995  (void) FormatLocaleString(label,MagickPathExtent,"charcoal %gx%g",
2996  radius,sigma);
2997  break;
2998  }
3000  {
3001  preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3002  exception);
3003  (void) FormatLocaleString(label,MagickPathExtent,"charcoal %gx%g",
3004  radius,sigma);
3005  break;
3006  }
3007  case JPEGPreview:
3008  {
3009  char
3010  filename[MagickPathExtent];
3011 
3012  int
3013  file;
3014 
3016  status;
3017 
3018  preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3019  if (preview_image == (Image *) NULL)
3020  break;
3021  preview_info->quality=(size_t) percentage;
3022  (void) FormatLocaleString(factor,MagickPathExtent,"%.20g",(double)
3023  preview_info->quality);
3024  file=AcquireUniqueFileResource(filename);
3025  if (file != -1)
3026  file=close(file)-1;
3027  (void) FormatLocaleString(preview_image->filename,MagickPathExtent,
3028  "jpeg:%s",filename);
3029  status=WriteImage(preview_info,preview_image,exception);
3030  if (status != MagickFalse)
3031  {
3032  Image
3033  *quality_image;
3034 
3035  (void) CopyMagickString(preview_info->filename,
3036  preview_image->filename,MagickPathExtent);
3037  quality_image=ReadImage(preview_info,exception);
3038  if (quality_image != (Image *) NULL)
3039  {
3040  preview_image=DestroyImage(preview_image);
3041  preview_image=quality_image;
3042  }
3043  }
3044  (void) RelinquishUniqueFileResource(preview_image->filename);
3045  if ((GetBlobSize(preview_image)/1024) >= 1024)
3046  (void) FormatLocaleString(label,MagickPathExtent,"quality %s\n%gmb ",
3047  factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3048  1024.0/1024.0);
3049  else
3050  if (GetBlobSize(preview_image) >= 1024)
3051  (void) FormatLocaleString(label,MagickPathExtent,
3052  "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
3053  GetBlobSize(preview_image))/1024.0);
3054  else
3055  (void) FormatLocaleString(label,MagickPathExtent,
3056  "quality %s\n%.20gb ",factor,(double) ((MagickOffsetType)
3057  GetBlobSize(thumbnail)));
3058  break;
3059  }
3060  }
3061  thumbnail=DestroyImage(thumbnail);
3062  percentage+=12.5;
3063  radius+=0.5;
3064  sigma+=0.25;
3065  if (preview_image == (Image *) NULL)
3066  break;
3067  preview_image->alpha_trait=UndefinedPixelTrait;
3068  (void) DeleteImageProperty(preview_image,"label");
3069  (void) SetImageProperty(preview_image,"label",label,exception);
3070  AppendImageToList(&images,preview_image);
3072  NumberTiles);
3073  if (proceed == MagickFalse)
3074  break;
3075  }
3076  if (images == (Image *) NULL)
3077  {
3078  preview_info=DestroyImageInfo(preview_info);
3079  return((Image *) NULL);
3080  }
3081  /*
3082  Create the montage.
3083  */
3084  montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
3085  (void) CopyMagickString(montage_info->filename,image->filename,
3087  montage_info->shadow=MagickTrue;
3088  (void) CloneString(&montage_info->tile,"3x3");
3089  (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
3090  (void) CloneString(&montage_info->frame,DefaultTileFrame);
3091  montage_image=MontageImages(images,montage_info,exception);
3092  montage_info=DestroyMontageInfo(montage_info);
3093  images=DestroyImageList(images);
3094  if (montage_image == (Image *) NULL)
3095  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3096  if (montage_image->montage != (char *) NULL)
3097  {
3098  /*
3099  Free image directory.
3100  */
3101  montage_image->montage=(char *) RelinquishMagickMemory(
3102  montage_image->montage);
3103  if (image->directory != (char *) NULL)
3104  montage_image->directory=(char *) RelinquishMagickMemory(
3105  montage_image->directory);
3106  }
3107  preview_info=DestroyImageInfo(preview_info);
3108  return(montage_image);
3109 }
3110 
3111 /*
3112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3113 % %
3114 % %
3115 % %
3116 % R o t a t i o n a l B l u r I m a g e %
3117 % %
3118 % %
3119 % %
3120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 %
3122 % RotationalBlurImage() applies a radial blur to the image.
3123 %
3124 % Andrew Protano contributed this effect.
3125 %
3126 % The format of the RotationalBlurImage method is:
3127 %
3128 % Image *RotationalBlurImage(const Image *image,const double angle,
3129 % ExceptionInfo *exception)
3130 %
3131 % A description of each parameter follows:
3132 %
3133 % o image: the image.
3134 %
3135 % o angle: the angle of the radial blur.
3136 %
3137 % o blur: the blur.
3138 %
3139 % o exception: return any errors or warnings in this structure.
3140 %
3141 */
3142 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
3143  ExceptionInfo *exception)
3144 {
3145  CacheView
3146  *blur_view,
3147  *image_view,
3148  *radial_view;
3149 
3150  double
3151  blur_radius,
3152  *cos_theta,
3153  offset,
3154  *sin_theta,
3155  theta;
3156 
3157  Image
3158  *blur_image;
3159 
3161  status;
3162 
3164  progress;
3165 
3166  PointInfo
3167  blur_center;
3168 
3169  size_t
3170  n;
3171 
3172  ssize_t
3173  w,
3174  y;
3175 
3176  /*
3177  Allocate blur image.
3178  */
3179  assert(image != (Image *) NULL);
3180  assert(image->signature == MagickCoreSignature);
3181  assert(exception != (ExceptionInfo *) NULL);
3182  assert(exception->signature == MagickCoreSignature);
3183  if (IsEventLogging() != MagickFalse)
3184  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3185 #if defined(MAGICKCORE_OPENCL_SUPPORT)
3186  blur_image=AccelerateRotationalBlurImage(image,angle,exception);
3187  if (blur_image != (Image *) NULL)
3188  return(blur_image);
3189 #endif
3190  blur_image=CloneImage(image,0,0,MagickTrue,exception);
3191  if (blur_image == (Image *) NULL)
3192  return((Image *) NULL);
3193  if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3194  {
3195  blur_image=DestroyImage(blur_image);
3196  return((Image *) NULL);
3197  }
3198  blur_center.x=(double) (image->columns-1)/2.0;
3199  blur_center.y=(double) (image->rows-1)/2.0;
3200  blur_radius=hypot(blur_center.x,blur_center.y);
3201  n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
3202  theta=DegreesToRadians(angle)/(double) (n-1);
3203  cos_theta=(double *) AcquireQuantumMemory((size_t) n,sizeof(*cos_theta));
3204  sin_theta=(double *) AcquireQuantumMemory((size_t) n,sizeof(*sin_theta));
3205  if ((cos_theta == (double *) NULL) || (sin_theta == (double *) NULL))
3206  {
3207  if (cos_theta != (double *) NULL)
3208  cos_theta=(double *) RelinquishMagickMemory(cos_theta);
3209  if (sin_theta != (double *) NULL)
3210  sin_theta=(double *) RelinquishMagickMemory(sin_theta);
3211  blur_image=DestroyImage(blur_image);
3212  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3213  }
3214  offset=theta*(double) (n-1)/2.0;
3215  for (w=0; w < (ssize_t) n; w++)
3216  {
3217  cos_theta[w]=cos((double) (theta*w-offset));
3218  sin_theta[w]=sin((double) (theta*w-offset));
3219  }
3220  /*
3221  Radial blur image.
3222  */
3223  status=MagickTrue;
3224  progress=0;
3225  image_view=AcquireVirtualCacheView(image,exception);
3226  radial_view=AcquireVirtualCacheView(image,exception);
3227  blur_view=AcquireAuthenticCacheView(blur_image,exception);
3228 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3229  #pragma omp parallel for schedule(static) shared(progress,status) \
3230  magick_number_threads(image,blur_image,image->rows,1)
3231 #endif
3232  for (y=0; y < (ssize_t) image->rows; y++)
3233  {
3234  const Quantum
3235  *magick_restrict p;
3236 
3237  Quantum
3238  *magick_restrict q;
3239 
3240  ssize_t
3241  x;
3242 
3243  if (status == MagickFalse)
3244  continue;
3245  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3246  q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3247  exception);
3248  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3249  {
3250  status=MagickFalse;
3251  continue;
3252  }
3253  for (x=0; x < (ssize_t) image->columns; x++)
3254  {
3255  double
3256  radius;
3257 
3258  PointInfo
3259  center;
3260 
3261  ssize_t
3262  i;
3263 
3264  size_t
3265  step;
3266 
3267  center.x=(double) x-blur_center.x;
3268  center.y=(double) y-blur_center.y;
3269  radius=hypot((double) center.x,center.y);
3270  if (radius == 0)
3271  step=1;
3272  else
3273  {
3274  step=(size_t) (blur_radius/radius);
3275  if (step == 0)
3276  step=1;
3277  else
3278  if (step >= n)
3279  step=n-1;
3280  }
3281  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3282  {
3283  double
3284  gamma,
3285  pixel;
3286 
3287  PixelChannel
3288  channel;
3289 
3290  PixelTrait
3291  blur_traits,
3292  traits;
3293 
3294  const Quantum
3295  *magick_restrict r;
3296 
3297  ssize_t
3298  j;
3299 
3300  channel=GetPixelChannelChannel(image,i);
3301  traits=GetPixelChannelTraits(image,channel);
3302  blur_traits=GetPixelChannelTraits(blur_image,channel);
3303  if ((traits == UndefinedPixelTrait) ||
3304  (blur_traits == UndefinedPixelTrait))
3305  continue;
3306  if ((blur_traits & CopyPixelTrait) != 0)
3307  {
3308  SetPixelChannel(blur_image,channel,p[i],q);
3309  continue;
3310  }
3311  gamma=0.0;
3312  pixel=0.0;
3314  (channel == AlphaPixelChannel))
3315  {
3316  for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3317  {
3318  r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
3319  center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3320  (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3321  1,1,exception);
3322  if (r == (const Quantum *) NULL)
3323  {
3324  status=MagickFalse;
3325  continue;
3326  }
3327  pixel+=r[i];
3328  gamma++;
3329  }
3330  gamma=PerceptibleReciprocal(gamma);
3331  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3332  continue;
3333  }
3334  for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3335  {
3336  double
3337  alpha;
3338 
3339  r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
3340  center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3341  (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3342  1,1,exception);
3343  if (r == (const Quantum *) NULL)
3344  {
3345  status=MagickFalse;
3346  continue;
3347  }
3348  alpha=(double) QuantumScale*GetPixelAlpha(image,r);
3349  pixel+=alpha*r[i];
3350  gamma+=alpha;
3351  }
3352  gamma=PerceptibleReciprocal(gamma);
3353  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3354  }
3355  p+=GetPixelChannels(image);
3356  q+=GetPixelChannels(blur_image);
3357  }
3358  if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3359  status=MagickFalse;
3360  if (image->progress_monitor != (MagickProgressMonitor) NULL)
3361  {
3363  proceed;
3364 
3365 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3366  #pragma omp atomic
3367 #endif
3368  progress++;
3369  proceed=SetImageProgress(image,BlurImageTag,progress,image->rows);
3370  if (proceed == MagickFalse)
3371  status=MagickFalse;
3372  }
3373  }
3374  blur_view=DestroyCacheView(blur_view);
3375  radial_view=DestroyCacheView(radial_view);
3376  image_view=DestroyCacheView(image_view);
3377  cos_theta=(double *) RelinquishMagickMemory(cos_theta);
3378  sin_theta=(double *) RelinquishMagickMemory(sin_theta);
3379  if (status == MagickFalse)
3380  blur_image=DestroyImage(blur_image);
3381  return(blur_image);
3382 }
3383 
3384 /*
3385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3386 % %
3387 % %
3388 % %
3389 % S e l e c t i v e B l u r I m a g e %
3390 % %
3391 % %
3392 % %
3393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3394 %
3395 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3396 % It is similar to the unsharpen mask that sharpens everything with contrast
3397 % above a certain threshold.
3398 %
3399 % The format of the SelectiveBlurImage method is:
3400 %
3401 % Image *SelectiveBlurImage(const Image *image,const double radius,
3402 % const double sigma,const double threshold,ExceptionInfo *exception)
3403 %
3404 % A description of each parameter follows:
3405 %
3406 % o image: the image.
3407 %
3408 % o radius: the radius of the Gaussian, in pixels, not counting the center
3409 % pixel.
3410 %
3411 % o sigma: the standard deviation of the Gaussian, in pixels.
3412 %
3413 % o threshold: only pixels within this contrast threshold are included
3414 % in the blur operation.
3415 %
3416 % o exception: return any errors or warnings in this structure.
3417 %
3418 */
3419 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3420  const double sigma,const double threshold,ExceptionInfo *exception)
3421 {
3422 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3423 
3424  CacheView
3425  *blur_view,
3426  *image_view,
3427  *luminance_view;
3428 
3429  Image
3430  *blur_image,
3431  *luminance_image;
3432 
3434  status;
3435 
3437  progress;
3438 
3440  *kernel;
3441 
3442  size_t
3443  width;
3444 
3445  ssize_t
3446  center,
3447  y;
3448 
3449  /*
3450  Initialize blur image attributes.
3451  */
3452  assert(image != (Image *) NULL);
3453  assert(image->signature == MagickCoreSignature);
3454  assert(exception != (ExceptionInfo *) NULL);
3455  assert(exception->signature == MagickCoreSignature);
3456  if (IsEventLogging() != MagickFalse)
3457  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3458  width=GetOptimalKernelWidth1D(radius,sigma);
3460  width,width*sizeof(*kernel)));
3461  if (kernel == (MagickRealType *) NULL)
3462  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3463  {
3464  ssize_t
3465  i,
3466  j,
3467  v;
3468 
3469  j=(ssize_t) (width-1)/2;
3470  i=0;
3471  for (v=(-j); v <= j; v++)
3472  {
3473  ssize_t
3474  u;
3475 
3476  for (u=(-j); u <= j; u++)
3477  kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3479  }
3480  }
3481  if (image->debug != MagickFalse)
3482  {
3483  char
3484  format[MagickPathExtent],
3485  *message;
3486 
3487  const MagickRealType
3488  *k;
3489 
3490  ssize_t
3491  u,
3492  v;
3493 
3495  " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3496  width);
3497  message=AcquireString("");
3498  k=kernel;
3499  for (v=0; v < (ssize_t) width; v++)
3500  {
3501  *message='\0';
3502  (void) FormatLocaleString(format,MagickPathExtent,"%.20g: ",(double) v);
3503  (void) ConcatenateString(&message,format);
3504  for (u=0; u < (ssize_t) width; u++)
3505  {
3506  (void) FormatLocaleString(format,MagickPathExtent,"%+f ",(double)
3507  *k++);
3508  (void) ConcatenateString(&message,format);
3509  }
3510  (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3511  }
3512  message=DestroyString(message);
3513  }
3514  blur_image=CloneImage(image,0,0,MagickTrue,exception);
3515  if (blur_image == (Image *) NULL)
3516  return((Image *) NULL);
3517  if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3518  {
3519  blur_image=DestroyImage(blur_image);
3520  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3521  return((Image *) NULL);
3522  }
3523  luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3524  if (luminance_image == (Image *) NULL)
3525  {
3526  blur_image=DestroyImage(blur_image);
3527  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3528  return((Image *) NULL);
3529  }
3530  status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3531  if (status == MagickFalse)
3532  {
3533  luminance_image=DestroyImage(luminance_image);
3534  blur_image=DestroyImage(blur_image);
3535  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3536  return((Image *) NULL);
3537  }
3538  /*
3539  Threshold blur image.
3540  */
3541  status=MagickTrue;
3542  progress=0;
3543  center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3544  ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3545  image_view=AcquireVirtualCacheView(image,exception);
3546  luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3547  blur_view=AcquireAuthenticCacheView(blur_image,exception);
3548 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3549  #pragma omp parallel for schedule(static) shared(progress,status) \
3550  magick_number_threads(image,blur_image,image->rows,1)
3551 #endif
3552  for (y=0; y < (ssize_t) image->rows; y++)
3553  {
3554  double
3555  contrast;
3556 
3558  sync;
3559 
3560  const Quantum
3561  *magick_restrict l,
3562  *magick_restrict p;
3563 
3564  Quantum
3565  *magick_restrict q;
3566 
3567  ssize_t
3568  x;
3569 
3570  if (status == MagickFalse)
3571  continue;
3572  p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3573  ((width-1)/2L),image->columns+width,width,exception);
3574  l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3575  (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3576  q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3577  exception);
3578  if ((p == (const Quantum *) NULL) || (l == (const Quantum *) NULL) ||
3579  (q == (Quantum *) NULL))
3580  {
3581  status=MagickFalse;
3582  continue;
3583  }
3584  for (x=0; x < (ssize_t) image->columns; x++)
3585  {
3586  double
3587  intensity;
3588 
3589  ssize_t
3590  i;
3591 
3592  intensity=GetPixelIntensity(image,p+center);
3593  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3594  {
3595  double
3596  alpha,
3597  gamma,
3598  pixel;
3599 
3600  PixelChannel
3601  channel;
3602 
3603  PixelTrait
3604  blur_traits,
3605  traits;
3606 
3607  const MagickRealType
3608  *magick_restrict k;
3609 
3610  const Quantum
3611  *magick_restrict luminance_pixels,
3612  *magick_restrict pixels;
3613 
3614  ssize_t
3615  u;
3616 
3617  ssize_t
3618  v;
3619 
3620  channel=GetPixelChannelChannel(image,i);
3621  traits=GetPixelChannelTraits(image,channel);
3622  blur_traits=GetPixelChannelTraits(blur_image,channel);
3623  if ((traits == UndefinedPixelTrait) ||
3624  (blur_traits == UndefinedPixelTrait))
3625  continue;
3626  if ((blur_traits & CopyPixelTrait) != 0)
3627  {
3628  SetPixelChannel(blur_image,channel,p[center+i],q);
3629  continue;
3630  }
3631  k=kernel;
3632  pixel=0.0;
3633  pixels=p;
3634  luminance_pixels=l;
3635  gamma=0.0;
3636  if ((blur_traits & BlendPixelTrait) == 0)
3637  {
3638  for (v=0; v < (ssize_t) width; v++)
3639  {
3640  for (u=0; u < (ssize_t) width; u++)
3641  {
3642  contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3643  intensity;
3644  if (fabs(contrast) < threshold)
3645  {
3646  pixel+=(*k)*pixels[i];
3647  gamma+=(*k);
3648  }
3649  k++;
3650  pixels+=GetPixelChannels(image);
3651  luminance_pixels+=GetPixelChannels(luminance_image);
3652  }
3653  pixels+=GetPixelChannels(image)*image->columns;
3654  luminance_pixels+=GetPixelChannels(luminance_image)*
3655  luminance_image->columns;
3656  }
3657  if (fabs((double) gamma) < MagickEpsilon)
3658  {
3659  SetPixelChannel(blur_image,channel,p[center+i],q);
3660  continue;
3661  }
3662  gamma=PerceptibleReciprocal(gamma);
3663  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3664  continue;
3665  }
3666  for (v=0; v < (ssize_t) width; v++)
3667  {
3668  for (u=0; u < (ssize_t) width; u++)
3669  {
3670  contrast=GetPixelIntensity(image,pixels)-intensity;
3671  if (fabs(contrast) < threshold)
3672  {
3673  alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3674  pixel+=(*k)*alpha*pixels[i];
3675  gamma+=(*k)*alpha;
3676  }
3677  k++;
3678  pixels+=GetPixelChannels(image);
3679  luminance_pixels+=GetPixelChannels(luminance_image);
3680  }
3681  pixels+=GetPixelChannels(image)*image->columns;
3682  luminance_pixels+=GetPixelChannels(luminance_image)*
3683  luminance_image->columns;
3684  }
3685  if (fabs((double) gamma) < MagickEpsilon)
3686  {
3687  SetPixelChannel(blur_image,channel,p[center+i],q);
3688  continue;
3689  }
3690  gamma=PerceptibleReciprocal(gamma);
3691  SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3692  }
3693  p+=GetPixelChannels(image);
3694  l+=GetPixelChannels(luminance_image);
3695  q+=GetPixelChannels(blur_image);
3696  }
3697  sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3698  if (sync == MagickFalse)
3699  status=MagickFalse;
3700  if (image->progress_monitor != (MagickProgressMonitor) NULL)
3701  {
3703  proceed;
3704 
3705 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3706  #pragma omp atomic
3707 #endif
3708  progress++;
3709  proceed=SetImageProgress(image,SelectiveBlurImageTag,progress,
3710  image->rows);
3711  if (proceed == MagickFalse)
3712  status=MagickFalse;
3713  }
3714  }
3715  blur_image->type=image->type;
3716  blur_view=DestroyCacheView(blur_view);
3717  luminance_view=DestroyCacheView(luminance_view);
3718  image_view=DestroyCacheView(image_view);
3719  luminance_image=DestroyImage(luminance_image);
3720  kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3721  if (status == MagickFalse)
3722  blur_image=DestroyImage(blur_image);
3723  return(blur_image);
3724 }
3725 
3726 /*
3727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3728 % %
3729 % %
3730 % %
3731 % S h a d e I m a g e %
3732 % %
3733 % %
3734 % %
3735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3736 %
3737 % ShadeImage() shines a distant light on an image to create a
3738 % three-dimensional effect. You control the positioning of the light with
3739 % azimuth and elevation; azimuth is measured in degrees off the x axis
3740 % and elevation is measured in pixels above the Z axis.
3741 %
3742 % The format of the ShadeImage method is:
3743 %
3744 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3745 % const double azimuth,const double elevation,ExceptionInfo *exception)
3746 %
3747 % A description of each parameter follows:
3748 %
3749 % o image: the image.
3750 %
3751 % o gray: A value other than zero shades the intensity of each pixel.
3752 %
3753 % o azimuth, elevation: Define the light source direction.
3754 %
3755 % o exception: return any errors or warnings in this structure.
3756 %
3757 */
3759  const double azimuth,const double elevation,ExceptionInfo *exception)
3760 {
3761 #define GetShadeIntensity(image,pixel) \
3762  ClampPixel(GetPixelIntensity((image),(pixel)))
3763 #define ShadeImageTag "Shade/Image"
3764 
3765  CacheView
3766  *image_view,
3767  *shade_view;
3768 
3769  Image
3770  *linear_image,
3771  *shade_image;
3772 
3774  status;
3775 
3777  progress;
3778 
3779  PrimaryInfo
3780  light;
3781 
3782  ssize_t
3783  y;
3784 
3785  /*
3786  Initialize shaded image attributes.
3787  */
3788  assert(image != (const Image *) NULL);
3789  assert(image->signature == MagickCoreSignature);
3790  assert(exception != (ExceptionInfo *) NULL);
3791  assert(exception->signature == MagickCoreSignature);
3792  if (IsEventLogging() != MagickFalse)
3793  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3794  linear_image=CloneImage(image,0,0,MagickTrue,exception);
3795  shade_image=CloneImage(image,0,0,MagickTrue,exception);
3796  if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3797  {
3798  if (linear_image != (Image *) NULL)
3799  linear_image=DestroyImage(linear_image);
3800  if (shade_image != (Image *) NULL)
3801  shade_image=DestroyImage(shade_image);
3802  return((Image *) NULL);
3803  }
3804  if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3805  {
3806  linear_image=DestroyImage(linear_image);
3807  shade_image=DestroyImage(shade_image);
3808  return((Image *) NULL);
3809  }
3810  /*
3811  Compute the light vector.
3812  */
3813  light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3814  cos(DegreesToRadians(elevation));
3815  light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3816  cos(DegreesToRadians(elevation));
3817  light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3818  /*
3819  Shade image.
3820  */
3821  status=MagickTrue;
3822  progress=0;
3823  image_view=AcquireVirtualCacheView(linear_image,exception);
3824  shade_view=AcquireAuthenticCacheView(shade_image,exception);
3825 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3826  #pragma omp parallel for schedule(static) shared(progress,status) \
3827  magick_number_threads(linear_image,shade_image,linear_image->rows,1)
3828 #endif
3829  for (y=0; y < (ssize_t) linear_image->rows; y++)
3830  {
3831  double
3832  distance,
3833  normal_distance,
3834  shade;
3835 
3836  PrimaryInfo
3837  normal;
3838 
3839  const Quantum
3840  *magick_restrict center,
3841  *magick_restrict p,
3842  *magick_restrict post,
3843  *magick_restrict pre;
3844 
3845  Quantum
3846  *magick_restrict q;
3847 
3848  ssize_t
3849  x;
3850 
3851  if (status == MagickFalse)
3852  continue;
3853  p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3854  exception);
3855  q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3856  exception);
3857  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3858  {
3859  status=MagickFalse;
3860  continue;
3861  }
3862  /*
3863  Shade this row of pixels.
3864  */
3865  normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3866  for (x=0; x < (ssize_t) linear_image->columns; x++)
3867  {
3868  ssize_t
3869  i;
3870 
3871  /*
3872  Determine the surface normal and compute shading.
3873  */
3874  pre=p+GetPixelChannels(linear_image);
3875  center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3876  post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3877  normal.x=(double) (
3878  GetShadeIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3879  GetShadeIntensity(linear_image,center-GetPixelChannels(linear_image))+
3880  GetShadeIntensity(linear_image,post-GetPixelChannels(linear_image))-
3881  GetShadeIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3882  GetShadeIntensity(linear_image,center+GetPixelChannels(linear_image))-
3883  GetShadeIntensity(linear_image,post+GetPixelChannels(linear_image)));
3884  normal.y=(double) (
3885  GetShadeIntensity(linear_image,post-GetPixelChannels(linear_image))+
3886  GetShadeIntensity(linear_image,post)+
3887  GetShadeIntensity(linear_image,post+GetPixelChannels(linear_image))-
3888  GetShadeIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3889  GetShadeIntensity(linear_image,pre)-
3890  GetShadeIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3891  if ((fabs(normal.x) <= MagickEpsilon) &&
3892  (fabs(normal.y) <= MagickEpsilon))
3893  shade=light.z;
3894  else
3895  {
3896  shade=0.0;
3897  distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3898  if (distance > MagickEpsilon)
3899  {
3900  normal_distance=normal.x*normal.x+normal.y*normal.y+
3901  normal.z*normal.z;
3902  if (normal_distance > (MagickEpsilon*MagickEpsilon))
3903  shade=distance/sqrt((double) normal_distance);
3904  }
3905  }
3906  for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3907  {
3908  PixelChannel
3909  channel;
3910 
3911  PixelTrait
3912  shade_traits,
3913  traits;
3914 
3915  channel=GetPixelChannelChannel(linear_image,i);
3916  traits=GetPixelChannelTraits(linear_image,channel);
3917  shade_traits=GetPixelChannelTraits(shade_image,channel);
3918  if ((traits == UndefinedPixelTrait) ||
3919  (shade_traits == UndefinedPixelTrait))
3920  continue;
3921  if ((shade_traits & CopyPixelTrait) != 0)
3922  {
3923  SetPixelChannel(shade_image,channel,center[i],q);
3924  continue;
3925  }
3926  if ((traits & UpdatePixelTrait) == 0)
3927  {
3928  SetPixelChannel(shade_image,channel,center[i],q);
3929  continue;
3930  }
3931  if (gray != MagickFalse)
3932  {
3933  SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3934  continue;
3935  }
3936  SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3937  center[i]),q);
3938  }
3939  p+=GetPixelChannels(linear_image);
3940  q+=GetPixelChannels(shade_image);
3941  }
3942  if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3943  status=MagickFalse;
3944  if (image->progress_monitor != (MagickProgressMonitor) NULL)
3945  {
3947  proceed;
3948 
3949 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3950  #pragma omp atomic
3951 #endif
3952  progress++;
3953  proceed=SetImageProgress(image,ShadeImageTag,progress,image->rows);
3954  if (proceed == MagickFalse)
3955  status=MagickFalse;
3956  }
3957  }
3958  shade_view=DestroyCacheView(shade_view);
3959  image_view=DestroyCacheView(image_view);
3960  linear_image=DestroyImage(linear_image);
3961  if (status == MagickFalse)
3962  shade_image=DestroyImage(shade_image);
3963  return(shade_image);
3964 }
3965 
3966 /*
3967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3968 % %
3969 % %
3970 % %
3971 % S h a r p e n I m a g e %
3972 % %
3973 % %
3974 % %
3975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3976 %
3977 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3978 % operator of the given radius and standard deviation (sigma). For
3979 % reasonable results, radius should be larger than sigma. Use a radius of 0
3980 % and SharpenImage() selects a suitable radius for you.
3981 %
3982 % Using a separable kernel would be faster, but the negative weights cancel
3983 % out on the corners of the kernel producing often undesirable ringing in the
3984 % filtered result; this can be avoided by using a 2D gaussian shaped image
3985 % sharpening kernel instead.
3986 %
3987 % The format of the SharpenImage method is:
3988 %
3989 % Image *SharpenImage(const Image *image,const double radius,
3990 % const double sigma,ExceptionInfo *exception)
3991 %
3992 % A description of each parameter follows:
3993 %
3994 % o image: the image.
3995 %
3996 % o radius: the radius of the Gaussian, in pixels, not counting the center
3997 % pixel.
3998 %
3999 % o sigma: the standard deviation of the Laplacian, in pixels.
4000 %
4001 % o exception: return any errors or warnings in this structure.
4002 %
4003 */
4004 MagickExport Image *SharpenImage(const Image *image,const double radius,
4005  const double sigma,ExceptionInfo *exception)
4006 {
4007  double
4008  gamma,
4009  normalize;
4010 
4011  Image
4012  *sharp_image;
4013 
4014  KernelInfo
4015  *kernel_info;
4016 
4017  ssize_t
4018  i;
4019 
4020  size_t
4021  width;
4022 
4023  ssize_t
4024  j,
4025  u,
4026  v;
4027 
4028  assert(image != (const Image *) NULL);
4029  assert(image->signature == MagickCoreSignature);
4030  assert(exception != (ExceptionInfo *) NULL);
4031  assert(exception->signature == MagickCoreSignature);
4032  if (IsEventLogging() != MagickFalse)
4033  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4034  width=GetOptimalKernelWidth2D(radius,sigma);
4035  kernel_info=AcquireKernelInfo((const char *) NULL,exception);
4036  if (kernel_info == (KernelInfo *) NULL)
4037  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4038  (void) memset(kernel_info,0,sizeof(*kernel_info));
4039  kernel_info->width=width;
4040  kernel_info->height=width;
4041  kernel_info->x=(ssize_t) (width-1)/2;
4042  kernel_info->y=(ssize_t) (width-1)/2;
4043  kernel_info->signature=MagickCoreSignature;
4044  kernel_info->values=(MagickRealType *) MagickAssumeAligned(
4045  AcquireAlignedMemory(kernel_info->width,kernel_info->height*
4046  sizeof(*kernel_info->values)));
4047  if (kernel_info->values == (MagickRealType *) NULL)
4048  {
4049  kernel_info=DestroyKernelInfo(kernel_info);
4050  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4051  }
4052  normalize=0.0;
4053  j=(ssize_t) (kernel_info->width-1)/2;
4054  i=0;
4055  for (v=(-j); v <= j; v++)
4056  {
4057  for (u=(-j); u <= j; u++)
4058  {
4059  kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
4061  normalize+=kernel_info->values[i];
4062  i++;
4063  }
4064  }
4065  kernel_info->values[i/2]=(double) ((-2.0)*normalize);
4066  normalize=0.0;
4067  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
4068  normalize+=kernel_info->values[i];
4069  gamma=PerceptibleReciprocal(normalize);
4070  for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
4071  kernel_info->values[i]*=gamma;
4072  sharp_image=ConvolveImage(image,kernel_info,exception);
4073  kernel_info=DestroyKernelInfo(kernel_info);
4074  return(sharp_image);
4075 }
4076 
4077 /*
4078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4079 % %
4080 % %
4081 % %
4082 % S p r e a d I m a g e %
4083 % %
4084 % %
4085 % %
4086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4087 %
4088 % SpreadImage() is a special effects method that randomly displaces each
4089 % pixel in a square area defined by the radius parameter.
4090 %
4091 % The format of the SpreadImage method is:
4092 %
4093 % Image *SpreadImage(const Image *image,
4094 % const PixelInterpolateMethod method,const double radius,
4095 % ExceptionInfo *exception)
4096 %
4097 % A description of each parameter follows:
4098 %
4099 % o image: the image.
4100 %
4101 % o method: intepolation method.
4102 %
4103 % o radius: choose a random pixel in a neighborhood of this extent.
4104 %
4105 % o exception: return any errors or warnings in this structure.
4106 %
4107 */
4109  const PixelInterpolateMethod method,const double radius,
4110  ExceptionInfo *exception)
4111 {
4112 #define SpreadImageTag "Spread/Image"
4113 
4114  CacheView
4115  *image_view,
4116  *spread_view;
4117 
4118  Image
4119  *spread_image;
4120 
4122  status;
4123 
4125  progress;
4126 
4127  RandomInfo
4129 
4130  size_t
4131  width;
4132 
4133  ssize_t
4134  y;
4135 
4136 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4137  unsigned long
4138  key;
4139 #endif
4140 
4141  /*
4142  Initialize spread image attributes.
4143  */
4144  assert(image != (Image *) NULL);
4145  assert(image->signature == MagickCoreSignature);
4146  assert(exception != (ExceptionInfo *) NULL);
4147  assert(exception->signature == MagickCoreSignature);
4148  if (IsEventLogging() != MagickFalse)
4149  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4150  spread_image=CloneImage(image,0,0,MagickTrue,exception);
4151  if (spread_image == (Image *) NULL)
4152  return((Image *) NULL);
4153  if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
4154  {
4155  spread_image=DestroyImage(spread_image);
4156  return((Image *) NULL);
4157  }
4158  /*
4159  Spread image.
4160  */
4161  status=MagickTrue;
4162  progress=0;
4163  width=GetOptimalKernelWidth1D(radius,0.5);
4165  image_view=AcquireVirtualCacheView(image,exception);
4166  spread_view=AcquireAuthenticCacheView(spread_image,exception);
4167 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4169  #pragma omp parallel for schedule(static) shared(progress,status) \
4170  magick_number_threads(image,spread_image,image->rows,key == ~0UL)
4171 #endif
4172  for (y=0; y < (ssize_t) image->rows; y++)
4173  {
4174  const int
4175  id = GetOpenMPThreadId();
4176 
4177  Quantum
4178  *magick_restrict q;
4179 
4180  ssize_t
4181  x;
4182 
4183  if (status == MagickFalse)
4184  continue;
4185  q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
4186  exception);
4187  if (q == (Quantum *) NULL)
4188  {
4189  status=MagickFalse;
4190  continue;
4191  }
4192  for (x=0; x < (ssize_t) image->columns; x++)
4193  {
4194  PointInfo
4195  point;
4196 
4197  point.x=GetPseudoRandomValue(random_info[id]);
4198  point.y=GetPseudoRandomValue(random_info[id]);
4199  status=InterpolatePixelChannels(image,image_view,spread_image,method,
4200  (double) x+width*(point.x-0.5),(double) y+width*(point.y-0.5),q,
4201  exception);
4202  if (status == MagickFalse)
4203  break;
4204  q+=GetPixelChannels(spread_image);
4205  }
4206  if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
4207  status=MagickFalse;
4208  if (image->progress_monitor != (MagickProgressMonitor) NULL)
4209  {
4211  proceed;
4212 
4213 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4214  #pragma omp atomic
4215 #endif
4216  progress++;
4217  proceed=SetImageProgress(image,SpreadImageTag,progress,image->rows);
4218  if (proceed == MagickFalse)
4219  status=MagickFalse;
4220  }
4221  }
4222  spread_view=DestroyCacheView(spread_view);
4223  image_view=DestroyCacheView(image_view);
4225  if (status == MagickFalse)
4226  spread_image=DestroyImage(spread_image);
4227  return(spread_image);
4228 }
4229 
4230 /*
4231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4232 % %
4233 % %
4234 % %
4235 % U n s h a r p M a s k I m a g e %
4236 % %
4237 % %
4238 % %
4239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4240 %
4241 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
4242 % image with a Gaussian operator of the given radius and standard deviation
4243 % (sigma). For reasonable results, radius should be larger than sigma. Use a
4244 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4245 %
4246 % The format of the UnsharpMaskImage method is:
4247 %
4248 % Image *UnsharpMaskImage(const Image *image,const double radius,
4249 % const double sigma,const double amount,const double threshold,
4250 % ExceptionInfo *exception)
4251 %
4252 % A description of each parameter follows:
4253 %
4254 % o image: the image.
4255 %
4256 % o radius: the radius of the Gaussian, in pixels, not counting the center
4257 % pixel.
4258 %
4259 % o sigma: the standard deviation of the Gaussian, in pixels.
4260 %
4261 % o gain: the percentage of the difference between the original and the
4262 % blur image that is added back into the original.
4263 %
4264 % o threshold: the threshold in pixels needed to apply the diffence gain.
4265 %
4266 % o exception: return any errors or warnings in this structure.
4267 %
4268 */
4269 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
4270  const double sigma,const double gain,const double threshold,
4271  ExceptionInfo *exception)
4272 {
4273 #define SharpenImageTag "Sharpen/Image"
4274 
4275  CacheView
4276  *image_view,
4277  *unsharp_view;
4278 
4279  Image
4280  *unsharp_image;
4281 
4283  status;
4284 
4286  progress;
4287 
4288  double
4289  quantum_threshold;
4290 
4291  ssize_t
4292  y;
4293 
4294  assert(image != (const Image *) NULL);
4295  assert(image->signature == MagickCoreSignature);
4296  assert(exception != (ExceptionInfo *) NULL);
4297  assert(exception->signature == MagickCoreSignature);
4298  if (IsEventLogging() != MagickFalse)
4299  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4300 /* This kernel appears to be broken.
4301 #if defined(MAGICKCORE_OPENCL_SUPPORT)
4302  unsharp_image=AccelerateUnsharpMaskImage(image,radius,sigma,gain,threshold,
4303  exception);
4304  if (unsharp_image != (Image *) NULL)
4305  return(unsharp_image);
4306 #endif
4307 */
4308  unsharp_image=BlurImage(image,radius,sigma,exception);
4309  if (unsharp_image == (Image *) NULL)
4310  return((Image *) NULL);
4311  quantum_threshold=(double) QuantumRange*threshold;
4312  /*
4313  Unsharp-mask image.
4314  */
4315  status=MagickTrue;
4316  progress=0;
4317  image_view=AcquireVirtualCacheView(image,exception);
4318  unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
4319 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4320  #pragma omp parallel for schedule(static) shared(progress,status) \
4321  magick_number_threads(image,unsharp_image,image->rows,1)
4322 #endif
4323  for (y=0; y < (ssize_t) image->rows; y++)
4324  {
4325  const Quantum
4326  *magick_restrict p;
4327 
4328  Quantum
4329  *magick_restrict q;
4330 
4331  ssize_t
4332  x;
4333 
4334  if (status == MagickFalse)
4335  continue;
4336  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4337  q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4338  exception);
4339  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4340  {
4341  status=MagickFalse;
4342  continue;
4343  }
4344  for (x=0; x < (ssize_t) image->columns; x++)
4345  {
4346  ssize_t
4347  i;
4348 
4349  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4350  {
4351  double
4352  pixel;
4353 
4354  PixelChannel
4355  channel;
4356 
4357  PixelTrait
4358  traits,
4359  unsharp_traits;
4360 
4361  channel=GetPixelChannelChannel(image,i);
4362  traits=GetPixelChannelTraits(image,channel);
4363  unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
4364  if ((traits == UndefinedPixelTrait) ||
4365  (unsharp_traits == UndefinedPixelTrait))
4366  continue;
4367  if ((unsharp_traits & CopyPixelTrait) != 0)
4368  {
4369  SetPixelChannel(unsharp_image,channel,p[i],q);
4370  continue;
4371  }
4372  pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
4373  if (fabs(2.0*pixel) < quantum_threshold)
4374  pixel=(double) p[i];
4375  else
4376  pixel=(double) p[i]+gain*pixel;
4377  SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
4378  }
4379  p+=GetPixelChannels(image);
4380  q+=GetPixelChannels(unsharp_image);
4381  }
4382  if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4383  status=MagickFalse;
4384  if (image->progress_monitor != (MagickProgressMonitor) NULL)
4385  {
4387  proceed;
4388 
4389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4390  #pragma omp atomic
4391 #endif
4392  progress++;
4393  proceed=SetImageProgress(image,SharpenImageTag,progress,image->rows);
4394  if (proceed == MagickFalse)
4395  status=MagickFalse;
4396  }
4397  }
4398  unsharp_image->type=image->type;
4399  unsharp_view=DestroyCacheView(unsharp_view);
4400  image_view=DestroyCacheView(image_view);
4401  if (status == MagickFalse)
4402  unsharp_image=DestroyImage(unsharp_image);
4403  return(unsharp_image);
4404 }
size_t rows
Definition: image.h:172
#define magick_restrict
Definition: MagickCore.h:41
MagickExport Image * SelectiveBlurImage(const Image *image, const double radius, const double sigma, const double threshold, ExceptionInfo *exception)
Definition: effect.c:3419
MagickExport Image * BlurImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:769
PixelInfo matte_color
Definition: image.h:357
MagickDoubleType MagickRealType
Definition: magick-type.h:124
#define DefaultTileLabel
Definition: image-private.h:32
MagickExport CacheView * DestroyCacheView(CacheView *cache_view)
Definition: cache-view.c:253
MagickExport Image * MotionBlurImage(const Image *image, const double radius, const double sigma, const double angle, ExceptionInfo *exception)
Definition: effect.c:2354
#define BlurImageTag
MagickExport MemoryInfo * RelinquishVirtualMemory(MemoryInfo *memory_info)
Definition: memory.c:1229
MagickExport ImageInfo * AcquireImageInfo(void)
Definition: image.c:328
#define PreviewImageTag
MagickExport Image * MontageImages(const Image *images, const MontageInfo *montage_info, ExceptionInfo *exception)
Definition: montage.c:306
MagickProgressMonitor progress_monitor
Definition: image.h:303
ImageType type
Definition: image.h:264
static Quantum GetPixelAlpha(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
MagickExport MagickBooleanType TransformImageColorspace(Image *image, const ColorspaceType colorspace, ExceptionInfo *exception)
Definition: colorspace.c:1607
static double ** DestroyBilateralTLS(const ssize_t number_threads, double **weights)
Definition: effect.c:862
#define MagickAssumeAligned(address)
ssize_t y
Definition: geometry.h:118
static MagickRealType GetMeanLuma(const Image *magick_restrict image, const double *magick_restrict pixel)
Definition: effect.c:1776
char * geometry
Definition: montage.h:36
static Quantum GetPixelRed(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
static RandomInfo ** AcquireRandomInfoTLS(void)
char * tile
Definition: montage.h:36
ColorspaceType colorspace
Definition: quantize.h:44
MagickExport Image * ShadeImage(const Image *image, const MagickBooleanType gray, const double azimuth, const double elevation, ExceptionInfo *exception)
Definition: effect.c:3758
MagickExport Image * UnsharpMaskImage(const Image *image, const double radius, const double sigma, const double gain, const double threshold, ExceptionInfo *exception)
Definition: effect.c:4269
MagickExport Image * BilateralBlurImage(const Image *image, const size_t width, const size_t height, const double intensity_sigma, const double spatial_sigma, ExceptionInfo *exception)
Definition: effect.c:898
size_t height
Definition: morphology.h:108
MagickExport MagickBooleanType RaiseImage(Image *image, const RectangleInfo *raise_info, const MagickBooleanType raise, ExceptionInfo *exception)
Definition: decorate.c:617
PixelInterpolateMethod
Definition: pixel.h:114
MagickExport KernelInfo * DestroyKernelInfo(KernelInfo *kernel)
Definition: morphology.c:2268
PixelInterpolateMethod interpolate
Definition: image.h:255
double x
Definition: image.h:99
MagickExport MemoryInfo * AcquireVirtualMemory(const size_t count, const size_t quantum)
Definition: memory.c:705
MagickExport Image * AdaptiveSharpenImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:449
size_t signature
Definition: exception.h:123
static size_t GetOpenMPMaximumThreads(void)
MagickExport Image * MorphologyImage(const Image *image, const MorphologyMethod method, const ssize_t iterations, const KernelInfo *kernel, ExceptionInfo *exception)
Definition: morphology.c:4137
MagickExport Image * PreviewImage(const Image *image, const PreviewType preview, ExceptionInfo *exception)
Definition: effect.c:2606
static double ** AcquireBilateralTLS(const size_t number_threads, const size_t width, const size_t height)
Definition: effect.c:876
static Quantum GetPixelChannel(const Image *magick_restrict image, const PixelChannel channel, const Quantum *magick_restrict pixel)
MagickExport MagickStatusType ParseMetaGeometry(const char *geometry, ssize_t *x, ssize_t *y, size_t *width, size_t *height)
Definition: geometry.c:1418
#define AdaptiveSharpenImageTag
MagickExport MagickBooleanType InterpolatePixelChannels(const Image *magick_restrict source, const CacheView_ *source_view, const Image *magick_restrict destination, const PixelInterpolateMethod method, const double x, const double y, Quantum *pixel, ExceptionInfo *exception)
Definition: pixel.c:4918
PreviewType
Definition: effect.h:27
double z
Definition: image.h:99
ssize_t x
Definition: morphology.h:112
static PixelTrait GetPixelChannelTraits(const Image *magick_restrict image, const PixelChannel channel)
#define MagickPI
Definition: image-private.h:42
MagickExport MagickBooleanType EqualizeImage(Image *image, ExceptionInfo *exception)
Definition: enhance.c:2033
MagickExport Image * RotationalBlurImage(const Image *image, const double angle, ExceptionInfo *exception)
Definition: effect.c:3142
#define GetShadeIntensity(image, pixel)
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:466
MagickPrivate size_t GetOptimalKernelWidth1D(const double, const double)
MagickExport size_t CopyMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:731
char magick[MagickPathExtent]
Definition: image.h:319
MagickExport const Quantum * GetCacheViewVirtualPixels(const CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:652
size_t signature
Definition: morphology.h:129
#define SharpenImageTag
static MagickRealType GetPixelLuma(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
char * montage
Definition: image.h:201
#define MagickEpsilon
Definition: magick-type.h:114
size_t width
Definition: geometry.h:132
Definition: log.h:52
ssize_t MagickOffsetType
Definition: magick-type.h:133
MagickExport Image * ThumbnailImage(const Image *image, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: resize.c:4523
MagickExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
Definition: random.c:733
static Quantum ClampToQuantum(const MagickRealType quantum)
Definition: quantum.h:86
Definition: image.h:151
MagickExport Image * ImplodeImage(const Image *image, const double amount, const PixelInterpolateMethod method, ExceptionInfo *exception)
#define DespeckleImageTag
MagickExport MagickBooleanType ContrastImage(Image *image, const MagickBooleanType sharpen, ExceptionInfo *exception)
Definition: enhance.c:1399
char * frame
Definition: montage.h:36
double x
Definition: geometry.h:125
MagickExport KernelInfo * AcquireKernelInfo(const char *kernel_string, ExceptionInfo *exception)
Definition: morphology.c:485
#define MagickCoreSignature
MagickExport Quantum * GetCacheViewAuthenticPixels(CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:300
#define SpreadImageTag
MagickBooleanType
Definition: magick-type.h:161
MagickExport Image * NewImageList(void)
Definition: list.c:953
MagickExport char * AcquireString(const char *source)
Definition: string.c:94
static double PerceptibleReciprocal(const double x)
MagickExport int AcquireUniqueFileResource(char *path)
Definition: resource.c:551
MagickExport Image * LocalContrastImage(const Image *image, const double radius, const double strength, ExceptionInfo *exception)
Definition: effect.c:2022
MagickExport Image * SpreadImage(const Image *image, const PixelInterpolateMethod method, const double radius, ExceptionInfo *exception)
Definition: effect.c:4108
MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info, Image *image, ExceptionInfo *exception)
Definition: constitute.c:1204
#define BilateralBlurImageTag
static ssize_t CastDoubleToLong(const double x)
Definition: image-private.h:55
#define Magick2PI
Definition: image-private.h:36
char filename[MagickPathExtent]
Definition: montage.h:63
MagickExport MagickBooleanType SegmentImage(Image *image, const ColorspaceType colorspace, const MagickBooleanType verbose, const double cluster_threshold, const double smooth_threshold, ExceptionInfo *exception)
Definition: segment.c:1795
#define DefaultTileFrame
Definition: image-private.h:30
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:665
char filename[MagickPathExtent]
Definition: image.h:480
MagickPrivate size_t GetOptimalKernelWidth2D(const double, const double)
Definition: gem.c:1683
static double DegreesToRadians(const double degrees)
Definition: image-private.h:79
double y
Definition: geometry.h:125
static int GetOpenMPThreadId(void)
MagickExport MagickBooleanType ModulateImage(Image *image, const char *modulate, ExceptionInfo *exception)
Definition: enhance.c:3625
#define MagickSQ2PI
Definition: image-private.h:45
MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
Definition: resource.c:1100
MagickExport Image * EmbossImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:1609
MagickExport MagickBooleanType SetImageProperty(Image *image, const char *property, const char *value, ExceptionInfo *exception)
Definition: property.c:4360
size_t number_colors
Definition: quantize.h:38
#define MagickSigma
static MagickRealType * GetMotionBlurKernel(const size_t width, const double sigma)
Definition: effect.c:2323
MagickExport Image * RollImage(const Image *image, const ssize_t x_offset, const ssize_t y_offset, ExceptionInfo *exception)
Definition: transform.c:1535
#define MagickPathExtent
static Quantum GetPixelGreen(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
MagickExport void * RelinquishAlignedMemory(void *memory)
Definition: memory.c:1120
MagickExport MagickBooleanType IsEventLogging(void)
Definition: log.c:763
PixelTrait alpha_trait
Definition: image.h:280
MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info)
Definition: quantize.c:2311
#define MagickMaximumValue
Definition: magick-type.h:115
MagickExport Image * ReadImage(const ImageInfo *image_info, ExceptionInfo *exception)
Definition: constitute.c:601
MagickExport Quantum * QueueCacheViewAuthenticPixels(CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:978
#define NumberTiles
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1699
MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info, Image *image, ExceptionInfo *exception)
Definition: quantize.c:3089
MagickExport Image * RotateImage(const Image *image, const double degrees, ExceptionInfo *exception)
Definition: distort.c:2948
size_t width
Definition: morphology.h:108
MagickExport Image * AdaptiveBlurImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:128
size_t signature
Definition: image.h:354
MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
Definition: resource.c:795
#define QuantumScale
Definition: magick-type.h:119
size_t columns
Definition: image.h:172
#define SelectiveBlurImageTag
ssize_t x
Definition: geometry.h:136
MagickBooleanType(* MagickProgressMonitor)(const char *, const MagickOffsetType, const MagickSizeType, void *)
Definition: monitor.h:26
MagickExport Image * DespeckleImage(const Image *image, ExceptionInfo *exception)
Definition: effect.c:1317
size_t height
Definition: geometry.h:132
MagickExport Image * SwirlImage(const Image *image, double degrees, const PixelInterpolateMethod method, ExceptionInfo *exception)
MagickExport MontageInfo * DestroyMontageInfo(MontageInfo *montage_info)
Definition: montage.c:164
MagickExport MagickBooleanType QueryColorCompliance(const char *name, const ComplianceType compliance, PixelInfo *color, ExceptionInfo *exception)
Definition: color.c:2270
static void SetPixelBlue(const Image *magick_restrict image, const Quantum blue, Quantum *magick_restrict pixel)
MagickExport MagickBooleanType SetImageStorageClass(Image *image, const ClassType storage_class, ExceptionInfo *exception)
Definition: image.c:2620
MagickExport MagickProgressMonitor SetImageProgressMonitor(Image *image, const MagickProgressMonitor progress_monitor, void *client_data)
Definition: monitor.c:194
MagickExport Image * DestroyImageList(Image *images)
Definition: list.c:477
PixelChannel
Definition: pixel.h:70
MagickExport void * AcquireAlignedMemory(const size_t count, const size_t quantum)
Definition: memory.c:365
double y
Definition: image.h:99
#define MagickMax(x, y)
Definition: image-private.h:38
static size_t GetPixelChannels(const Image *magick_restrict image)
#define KuwaharaImageTag
char filename[MagickPathExtent]
Definition: image.h:319
#define GetMagickModule()
Definition: log.h:28
size_t quality
Definition: image.h:410
#define ThrowImageException(severity, tag)
static PixelChannel GetPixelChannelChannel(const Image *magick_restrict image, const ssize_t offset)
MagickExport CacheView * AcquireVirtualCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:149
MagickExport Image * ShearImage(const Image *image, const double x_shear, const double y_shear, ExceptionInfo *exception)
Definition: shear.c:1575
MagickExport MagickSizeType GetBlobSize(const Image *image)
Definition: blob.c:1848
MagickExport ImageInfo * DestroyImageInfo(ImageInfo *image_info)
Definition: image.c:1252
static void Hull(const Image *image, const ssize_t x_offset, const ssize_t y_offset, const size_t columns, const size_t rows, const int polarity, Quantum *magick_restrict f, Quantum *magick_restrict g)
Definition: effect.c:1221
unsigned short Quantum
Definition: magick-type.h:86
MagickExport Image * KuwaharaImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:1784
MagickExport char * DestroyString(char *string)
Definition: string.c:788
MagickExport MagickBooleanType DeleteImageProperty(Image *image, const char *property)
Definition: property.c:280
MagickExport double GetPseudoRandomValue(RandomInfo *magick_restrict random_info)
Definition: random.c:602
#define MaxIntensity
size_t number_channels
Definition: image.h:283
static void SetPixelChannel(const Image *magick_restrict image, const PixelChannel channel, const Quantum quantum, Quantum *magick_restrict pixel)
char * directory
Definition: image.h:201
MagickExport void AppendImageToList(Image **images, const Image *append)
Definition: list.c:80
MagickExport Image * CharcoalImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
MagickExport Image * ConvolveImage(const Image *image, const KernelInfo *kernel_info, ExceptionInfo *exception)
Definition: effect.c:1174
MagickExport void SetGeometry(const Image *image, RectangleInfo *geometry)
Definition: geometry.c:1734
ssize_t x
Definition: geometry.h:118
MagickExport MagickBooleanType ConcatenateString(char **magick_restrict destination, const char *magick_restrict source)
Definition: string.c:458
MagickExport MagickBooleanType BilevelImage(Image *image, const double threshold, ExceptionInfo *exception)
Definition: threshold.c:806
static RandomInfo * random_info
Definition: resource.c:113
MagickExport Image * SharpenImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:4004
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1162
#define MaxPixelChannels
Definition: pixel.h:27
MagickBooleanType shadow
Definition: montage.h:50
MagickExport Image * WaveImage(const Image *image, const double amplitude, const double wave_length, const PixelInterpolateMethod method, ExceptionInfo *exception)
#define AdaptiveBlurImageTag
MagickExport MagickBooleanType GammaImage(Image *image, const double gamma, ExceptionInfo *exception)
Definition: enhance.c:2315
MagickExport char * CloneString(char **destination, const char *source)
Definition: string.c:250
static void SetPixelRed(const Image *magick_restrict image, const Quantum red, Quantum *magick_restrict pixel)
#define ShadeImageTag
MagickExport MagickBooleanType AutoLevelImage(Image *image, ExceptionInfo *exception)
Definition: enhance.c:185
static double BlurDistance(const ssize_t x, const ssize_t y, const ssize_t u, const ssize_t v)
Definition: effect.c:850
#define MagickExport
ssize_t y
Definition: morphology.h:112
MagickExport MagickBooleanType SyncCacheViewAuthenticPixels(CacheView *magick_restrict cache_view, ExceptionInfo *exception)
Definition: cache-view.c:1101
ssize_t y
Definition: geometry.h:136
MagickExport MagickBooleanType SolarizeImage(Image *image, const double threshold, ExceptionInfo *exception)
MagickExport Image * OilPaintImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: paint.c:690
MagickExport CacheView * AcquireAuthenticCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:112
MagickExport Image * EdgeImage(const Image *image, const double radius, ExceptionInfo *exception)
Definition: effect.c:1529
static Quantum GetPixelBlue(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
PixelTrait
Definition: pixel.h:138
MagickExport void * GetVirtualMemoryBlob(const MemoryInfo *memory_info)
Definition: memory.c:1090
MagickExport MagickRealType GetPixelIntensity(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
Definition: pixel.c:2358
MagickExport MontageInfo * CloneMontageInfo(const ImageInfo *image_info, const MontageInfo *montage_info)
Definition: montage.c:104
#define DefaultPreviewGeometry
MagickExport Image * DestroyImage(Image *image)
Definition: image.c:1181
MagickExport Image * GaussianBlurImage(const Image *image, const double radius, const double sigma, ExceptionInfo *exception)
Definition: effect.c:1718
MagickExport Image * CloneImage(const Image *image, const size_t columns, const size_t rows, const MagickBooleanType detach, ExceptionInfo *exception)
Definition: image.c:789
MagickExport Image * StatisticImage(const Image *image, const StatisticType type, const size_t width, const size_t height, ExceptionInfo *exception)
Definition: statistic.c:2838
#define QuantumRange
Definition: magick-type.h:87
MagickExport MagickBooleanType SetImageProgress(const Image *image, const char *tag, const MagickOffsetType offset, const MagickSizeType extent)
Definition: monitor.c:136
MagickRealType * values
Definition: morphology.h:116
MagickBooleanType debug
Definition: image.h:334
static void SetPixelGreen(const Image *magick_restrict image, const Quantum green, Quantum *magick_restrict pixel)
static double BlurGaussian(const double x, const double sigma)
Definition: effect.c:856
static RandomInfo ** DestroyRandomInfoTLS(RandomInfo **random_info)