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