blob: 464b3b99d52319dfbd354e41d167afb1ec81dd14 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
17% October 1996 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "magick/studio.h"
44#include "magick/property.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/draw.h"
53#include "magick/enhance.h"
54#include "magick/exception.h"
55#include "magick/exception-private.h"
56#include "magick/effect.h"
57#include "magick/fx.h"
58#include "magick/gem.h"
59#include "magick/geometry.h"
60#include "magick/image-private.h"
61#include "magick/list.h"
62#include "magick/log.h"
63#include "magick/memory_.h"
64#include "magick/monitor.h"
65#include "magick/monitor-private.h"
66#include "magick/montage.h"
67#include "magick/paint.h"
68#include "magick/pixel-private.h"
69#include "magick/property.h"
70#include "magick/quantize.h"
71#include "magick/quantum.h"
72#include "magick/random_.h"
73#include "magick/random-private.h"
74#include "magick/resample.h"
75#include "magick/resample-private.h"
76#include "magick/resize.h"
77#include "magick/resource_.h"
78#include "magick/segment.h"
79#include "magick/shear.h"
80#include "magick/signature-private.h"
81#include "magick/string_.h"
82#include "magick/thread-private.h"
83#include "magick/transform.h"
84#include "magick/threshold.h"
85
86/*
87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88% %
89% %
90% %
91% A d a p t i v e B l u r I m a g e %
92% %
93% %
94% %
95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96%
97% AdaptiveBlurImage() adaptively blurs the image by blurring less
98% intensely near image edges and more intensely far from edges. We blur the
99% image with a Gaussian operator of the given radius and standard deviation
100% (sigma). For reasonable results, radius should be larger than sigma. Use a
101% radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
102%
103% The format of the AdaptiveBlurImage method is:
104%
105% Image *AdaptiveBlurImage(const Image *image,const double radius,
106% const double sigma,ExceptionInfo *exception)
107% Image *AdaptiveBlurImageChannel(const Image *image,
108% const ChannelType channel,double radius,const double sigma,
109% ExceptionInfo *exception)
110%
111% A description of each parameter follows:
112%
113% o image: the image.
114%
115% o channel: the channel type.
116%
117% o radius: the radius of the Gaussian, in pixels, not counting the center
118% pixel.
119%
120% o sigma: the standard deviation of the Laplacian, in pixels.
121%
122% o exception: return any errors or warnings in this structure.
123%
124*/
125
126MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
127 const double sigma,ExceptionInfo *exception)
128{
129 Image
130 *blur_image;
131
132 blur_image=AdaptiveBlurImageChannel(image,DefaultChannels,radius,sigma,
133 exception);
134 return(blur_image);
135}
136
137MagickExport Image *AdaptiveBlurImageChannel(const Image *image,
138 const ChannelType channel,const double radius,const double sigma,
139 ExceptionInfo *exception)
140{
141#define AdaptiveBlurImageTag "Convolve/Image"
142#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
143
cristyc4c8d132010-01-07 01:58:38 +0000144 CacheView
145 *blur_view,
146 *edge_view,
147 *image_view;
148
cristy3ed852e2009-09-05 21:47:34 +0000149 double
cristy47e00502009-12-17 19:19:57 +0000150 **kernel,
151 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000152
153 Image
154 *blur_image,
155 *edge_image,
156 *gaussian_image;
157
158 long
159 j,
cristy47e00502009-12-17 19:19:57 +0000160 k,
cristy3ed852e2009-09-05 21:47:34 +0000161 progress,
cristy47e00502009-12-17 19:19:57 +0000162 u,
163 v,
cristy3ed852e2009-09-05 21:47:34 +0000164 y;
165
166 MagickBooleanType
167 status;
168
169 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +0000170 bias;
cristy3ed852e2009-09-05 21:47:34 +0000171
cristy3ed852e2009-09-05 21:47:34 +0000172 register long
cristy47e00502009-12-17 19:19:57 +0000173 i;
cristy3ed852e2009-09-05 21:47:34 +0000174
175 unsigned long
176 width;
177
cristy3ed852e2009-09-05 21:47:34 +0000178 assert(image != (const Image *) NULL);
179 assert(image->signature == MagickSignature);
180 if (image->debug != MagickFalse)
181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
182 assert(exception != (ExceptionInfo *) NULL);
183 assert(exception->signature == MagickSignature);
184 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
185 if (blur_image == (Image *) NULL)
186 return((Image *) NULL);
187 if (fabs(sigma) <= MagickEpsilon)
188 return(blur_image);
189 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
190 {
191 InheritException(exception,&blur_image->exception);
192 blur_image=DestroyImage(blur_image);
193 return((Image *) NULL);
194 }
195 /*
196 Edge detect the image brighness channel, level, blur, and level again.
197 */
198 edge_image=EdgeImage(image,radius,exception);
199 if (edge_image == (Image *) NULL)
200 {
201 blur_image=DestroyImage(blur_image);
202 return((Image *) NULL);
203 }
204 (void) LevelImage(edge_image,"20%,95%");
205 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
206 if (gaussian_image != (Image *) NULL)
207 {
208 edge_image=DestroyImage(edge_image);
209 edge_image=gaussian_image;
210 }
211 (void) LevelImage(edge_image,"10%,95%");
212 /*
213 Create a set of kernels from maximum (radius,sigma) to minimum.
214 */
215 width=GetOptimalKernelWidth2D(radius,sigma);
216 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
217 if (kernel == (double **) NULL)
218 {
219 edge_image=DestroyImage(edge_image);
220 blur_image=DestroyImage(blur_image);
221 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
222 }
223 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
224 for (i=0; i < (long) width; i+=2)
225 {
226 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
227 sizeof(**kernel));
228 if (kernel[i] == (double *) NULL)
229 break;
cristy47e00502009-12-17 19:19:57 +0000230 normalize=0.0;
231 j=(long) (width-i)/2;
232 k=0;
233 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000234 {
cristy47e00502009-12-17 19:19:57 +0000235 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000236 {
cristy47e00502009-12-17 19:19:57 +0000237 kernel[i][k]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
238 (2.0*MagickPI*MagickSigma*MagickSigma);
239 normalize+=kernel[i][k];
240 k++;
cristy3ed852e2009-09-05 21:47:34 +0000241 }
242 }
cristy3ed852e2009-09-05 21:47:34 +0000243 if (fabs(normalize) <= MagickEpsilon)
244 normalize=1.0;
245 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000246 for (k=0; k < (j*j); k++)
247 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000248 }
249 if (i < (long) width)
250 {
251 for (i-=2; i >= 0; i-=2)
252 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
253 kernel=(double **) RelinquishMagickMemory(kernel);
254 edge_image=DestroyImage(edge_image);
255 blur_image=DestroyImage(blur_image);
256 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
257 }
258 /*
259 Adaptively blur image.
260 */
261 status=MagickTrue;
262 progress=0;
cristyddd82202009-11-03 20:14:50 +0000263 GetMagickPixelPacket(image,&bias);
264 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000265 image_view=AcquireCacheView(image);
266 edge_view=AcquireCacheView(edge_image);
267 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000268#if defined(MAGICKCORE_OPENMP_SUPPORT)
269 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000270#endif
271 for (y=0; y < (long) blur_image->rows; y++)
272 {
273 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000274 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000275
276 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000277 *restrict p,
278 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000279
280 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000281 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000282
283 register long
284 x;
285
286 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000287 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000288
289 if (status == MagickFalse)
290 continue;
291 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
292 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
293 exception);
294 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
295 {
296 status=MagickFalse;
297 continue;
298 }
299 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
300 for (x=0; x < (long) blur_image->columns; x++)
301 {
302 MagickPixelPacket
303 pixel;
304
305 MagickRealType
306 alpha,
307 gamma;
308
309 register const double
cristyc47d1f82009-11-26 01:44:43 +0000310 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000311
312 register long
313 i,
314 u,
315 v;
316
317 gamma=0.0;
318 i=(long) (width*QuantumScale*PixelIntensity(r)+0.5);
319 if (i < 0)
320 i=0;
321 else
322 if (i > (long) width)
323 i=(long) width;
324 if ((i & 0x01) != 0)
325 i--;
326 p=GetCacheViewVirtualPixels(image_view,x-((long) (width-i)/2L),y-(long)
327 ((width-i)/2L),width-i,width-i,exception);
328 if (p == (const PixelPacket *) NULL)
329 break;
330 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristyddd82202009-11-03 20:14:50 +0000331 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000332 k=kernel[i];
333 for (v=0; v < (long) (width-i); v++)
334 {
335 for (u=0; u < (long) (width-i); u++)
336 {
337 alpha=1.0;
338 if (((channel & OpacityChannel) != 0) &&
339 (image->matte != MagickFalse))
cristyc4c8d132010-01-07 01:58:38 +0000340 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
cristyce70c172010-01-07 17:15:30 +0000341 GetOpacityPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +0000342 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000343 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000344 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000345 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000346 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000347 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000348 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000349 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000350 if (((channel & IndexChannel) != 0) &&
351 (image->colorspace == CMYKColorspace))
352 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
353 gamma+=(*k)*alpha;
354 k++;
355 p++;
356 }
357 }
358 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
359 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000360 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000361 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000362 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000363 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000364 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000365 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000366 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000367 if (((channel & IndexChannel) != 0) &&
368 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +0000369 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000370 q++;
371 r++;
372 }
373 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
374 status=MagickFalse;
375 if (image->progress_monitor != (MagickProgressMonitor) NULL)
376 {
377 MagickBooleanType
378 proceed;
379
cristyb5d5f722009-11-04 03:03:49 +0000380#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000381 #pragma omp critical (MagickCore_AdaptiveBlurImageChannel)
382#endif
383 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
384 image->rows);
385 if (proceed == MagickFalse)
386 status=MagickFalse;
387 }
388 }
389 blur_image->type=image->type;
390 blur_view=DestroyCacheView(blur_view);
391 edge_view=DestroyCacheView(edge_view);
392 image_view=DestroyCacheView(image_view);
393 edge_image=DestroyImage(edge_image);
394 for (i=0; i < (long) width; i+=2)
395 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
396 kernel=(double **) RelinquishMagickMemory(kernel);
397 if (status == MagickFalse)
398 blur_image=DestroyImage(blur_image);
399 return(blur_image);
400}
401
402/*
403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404% %
405% %
406% %
407% A d a p t i v e S h a r p e n I m a g e %
408% %
409% %
410% %
411%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412%
413% AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
414% intensely near image edges and less intensely far from edges. We sharpen the
415% image with a Gaussian operator of the given radius and standard deviation
416% (sigma). For reasonable results, radius should be larger than sigma. Use a
417% radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
418%
419% The format of the AdaptiveSharpenImage method is:
420%
421% Image *AdaptiveSharpenImage(const Image *image,const double radius,
422% const double sigma,ExceptionInfo *exception)
423% Image *AdaptiveSharpenImageChannel(const Image *image,
424% const ChannelType channel,double radius,const double sigma,
425% ExceptionInfo *exception)
426%
427% A description of each parameter follows:
428%
429% o image: the image.
430%
431% o channel: the channel type.
432%
433% o radius: the radius of the Gaussian, in pixels, not counting the center
434% pixel.
435%
436% o sigma: the standard deviation of the Laplacian, in pixels.
437%
438% o exception: return any errors or warnings in this structure.
439%
440*/
441
442MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
443 const double sigma,ExceptionInfo *exception)
444{
445 Image
446 *sharp_image;
447
448 sharp_image=AdaptiveSharpenImageChannel(image,DefaultChannels,radius,sigma,
449 exception);
450 return(sharp_image);
451}
452
453MagickExport Image *AdaptiveSharpenImageChannel(const Image *image,
454 const ChannelType channel,const double radius,const double sigma,
455 ExceptionInfo *exception)
456{
457#define AdaptiveSharpenImageTag "Convolve/Image"
458#define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
459
cristyc4c8d132010-01-07 01:58:38 +0000460 CacheView
461 *sharp_view,
462 *edge_view,
463 *image_view;
464
cristy3ed852e2009-09-05 21:47:34 +0000465 double
cristy47e00502009-12-17 19:19:57 +0000466 **kernel,
467 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000468
469 Image
470 *sharp_image,
471 *edge_image,
472 *gaussian_image;
473
474 long
475 j,
cristy47e00502009-12-17 19:19:57 +0000476 k,
cristy3ed852e2009-09-05 21:47:34 +0000477 progress,
cristy47e00502009-12-17 19:19:57 +0000478 u,
479 v,
cristy3ed852e2009-09-05 21:47:34 +0000480 y;
481
482 MagickBooleanType
483 status;
484
485 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +0000486 bias;
cristy3ed852e2009-09-05 21:47:34 +0000487
cristy3ed852e2009-09-05 21:47:34 +0000488 register long
cristy47e00502009-12-17 19:19:57 +0000489 i;
cristy3ed852e2009-09-05 21:47:34 +0000490
491 unsigned long
492 width;
493
cristy3ed852e2009-09-05 21:47:34 +0000494 assert(image != (const Image *) NULL);
495 assert(image->signature == MagickSignature);
496 if (image->debug != MagickFalse)
497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
498 assert(exception != (ExceptionInfo *) NULL);
499 assert(exception->signature == MagickSignature);
500 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
501 if (sharp_image == (Image *) NULL)
502 return((Image *) NULL);
503 if (fabs(sigma) <= MagickEpsilon)
504 return(sharp_image);
505 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
506 {
507 InheritException(exception,&sharp_image->exception);
508 sharp_image=DestroyImage(sharp_image);
509 return((Image *) NULL);
510 }
511 /*
512 Edge detect the image brighness channel, level, sharp, and level again.
513 */
514 edge_image=EdgeImage(image,radius,exception);
515 if (edge_image == (Image *) NULL)
516 {
517 sharp_image=DestroyImage(sharp_image);
518 return((Image *) NULL);
519 }
520 (void) LevelImage(edge_image,"20%,95%");
521 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
522 if (gaussian_image != (Image *) NULL)
523 {
524 edge_image=DestroyImage(edge_image);
525 edge_image=gaussian_image;
526 }
527 (void) LevelImage(edge_image,"10%,95%");
528 /*
529 Create a set of kernels from maximum (radius,sigma) to minimum.
530 */
531 width=GetOptimalKernelWidth2D(radius,sigma);
532 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
533 if (kernel == (double **) NULL)
534 {
535 edge_image=DestroyImage(edge_image);
536 sharp_image=DestroyImage(sharp_image);
537 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
538 }
539 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
540 for (i=0; i < (long) width; i+=2)
541 {
542 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
543 sizeof(**kernel));
544 if (kernel[i] == (double *) NULL)
545 break;
cristy47e00502009-12-17 19:19:57 +0000546 normalize=0.0;
547 j=(long) (width-i)/2;
548 k=0;
549 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +0000550 {
cristy47e00502009-12-17 19:19:57 +0000551 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +0000552 {
cristy47e00502009-12-17 19:19:57 +0000553 kernel[i][k]=(-exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
554 (2.0*MagickPI*MagickSigma*MagickSigma));
555 normalize+=kernel[i][k];
556 k++;
cristy3ed852e2009-09-05 21:47:34 +0000557 }
558 }
cristy3ed852e2009-09-05 21:47:34 +0000559 if (fabs(normalize) <= MagickEpsilon)
560 normalize=1.0;
561 normalize=1.0/normalize;
cristy47e00502009-12-17 19:19:57 +0000562 for (k=0; k < (j*j); k++)
563 kernel[i][k]=normalize*kernel[i][k];
cristy3ed852e2009-09-05 21:47:34 +0000564 }
565 if (i < (long) width)
566 {
567 for (i-=2; i >= 0; i-=2)
568 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
569 kernel=(double **) RelinquishMagickMemory(kernel);
570 edge_image=DestroyImage(edge_image);
571 sharp_image=DestroyImage(sharp_image);
572 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
573 }
574 /*
575 Adaptively sharpen image.
576 */
577 status=MagickTrue;
578 progress=0;
cristyddd82202009-11-03 20:14:50 +0000579 GetMagickPixelPacket(image,&bias);
580 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000581 image_view=AcquireCacheView(image);
582 edge_view=AcquireCacheView(edge_image);
583 sharp_view=AcquireCacheView(sharp_image);
cristyb5d5f722009-11-04 03:03:49 +0000584#if defined(MAGICKCORE_OPENMP_SUPPORT)
585 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000586#endif
587 for (y=0; y < (long) sharp_image->rows; y++)
588 {
589 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000590 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000591
592 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000593 *restrict p,
594 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000595
596 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000597 *restrict sharp_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000598
599 register long
600 x;
601
602 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000603 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000604
605 if (status == MagickFalse)
606 continue;
607 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
608 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
609 exception);
610 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
611 {
612 status=MagickFalse;
613 continue;
614 }
615 sharp_indexes=GetCacheViewAuthenticIndexQueue(sharp_view);
616 for (x=0; x < (long) sharp_image->columns; x++)
617 {
618 MagickPixelPacket
619 pixel;
620
621 MagickRealType
622 alpha,
623 gamma;
624
625 register const double
cristyc47d1f82009-11-26 01:44:43 +0000626 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000627
628 register long
629 i,
630 u,
631 v;
632
633 gamma=0.0;
634 i=(long) (width*(QuantumRange-QuantumScale*PixelIntensity(r))+0.5);
635 if (i < 0)
636 i=0;
637 else
638 if (i > (long) width)
639 i=(long) width;
640 if ((i & 0x01) != 0)
641 i--;
642 p=GetCacheViewVirtualPixels(image_view,x-((long) (width-i)/2L),y-(long)
643 ((width-i)/2L),width-i,width-i,exception);
644 if (p == (const PixelPacket *) NULL)
645 break;
646 indexes=GetCacheViewVirtualIndexQueue(image_view);
647 k=kernel[i];
cristyddd82202009-11-03 20:14:50 +0000648 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000649 for (v=0; v < (long) (width-i); v++)
650 {
651 for (u=0; u < (long) (width-i); u++)
652 {
653 alpha=1.0;
654 if (((channel & OpacityChannel) != 0) &&
655 (image->matte != MagickFalse))
cristyce70c172010-01-07 17:15:30 +0000656 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
657 GetOpacityPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +0000658 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000659 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000660 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000661 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000662 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000663 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000664 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000665 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000666 if (((channel & IndexChannel) != 0) &&
667 (image->colorspace == CMYKColorspace))
668 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
669 gamma+=(*k)*alpha;
670 k++;
671 p++;
672 }
673 }
674 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
675 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000676 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000677 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000678 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000679 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000680 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000681 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000682 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000683 if (((channel & IndexChannel) != 0) &&
684 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +0000685 sharp_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000686 q++;
687 r++;
688 }
689 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
690 status=MagickFalse;
691 if (image->progress_monitor != (MagickProgressMonitor) NULL)
692 {
693 MagickBooleanType
694 proceed;
695
cristyb5d5f722009-11-04 03:03:49 +0000696#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000697 #pragma omp critical (MagickCore_AdaptiveSharpenImageChannel)
698#endif
699 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
700 image->rows);
701 if (proceed == MagickFalse)
702 status=MagickFalse;
703 }
704 }
705 sharp_image->type=image->type;
706 sharp_view=DestroyCacheView(sharp_view);
707 edge_view=DestroyCacheView(edge_view);
708 image_view=DestroyCacheView(image_view);
709 edge_image=DestroyImage(edge_image);
710 for (i=0; i < (long) width; i+=2)
711 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
712 kernel=(double **) RelinquishMagickMemory(kernel);
713 if (status == MagickFalse)
714 sharp_image=DestroyImage(sharp_image);
715 return(sharp_image);
716}
717
718/*
719%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720% %
721% %
722% %
723% B l u r I m a g e %
724% %
725% %
726% %
727%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728%
729% BlurImage() blurs an image. We convolve the image with a Gaussian operator
730% of the given radius and standard deviation (sigma). For reasonable results,
731% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
732% selects a suitable radius for you.
733%
734% BlurImage() differs from GaussianBlurImage() in that it uses a separable
735% kernel which is faster but mathematically equivalent to the non-separable
736% kernel.
737%
738% The format of the BlurImage method is:
739%
740% Image *BlurImage(const Image *image,const double radius,
741% const double sigma,ExceptionInfo *exception)
742% Image *BlurImageChannel(const Image *image,const ChannelType channel,
743% const double radius,const double sigma,ExceptionInfo *exception)
744%
745% A description of each parameter follows:
746%
747% o image: the image.
748%
749% o channel: the channel type.
750%
751% o radius: the radius of the Gaussian, in pixels, not counting the center
752% pixel.
753%
754% o sigma: the standard deviation of the Gaussian, in pixels.
755%
756% o exception: return any errors or warnings in this structure.
757%
758*/
759
760MagickExport Image *BlurImage(const Image *image,const double radius,
761 const double sigma,ExceptionInfo *exception)
762{
763 Image
764 *blur_image;
765
766 blur_image=BlurImageChannel(image,DefaultChannels,radius,sigma,exception);
767 return(blur_image);
768}
769
cristy47e00502009-12-17 19:19:57 +0000770static double *GetBlurKernel(const unsigned long width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +0000771{
cristy3ed852e2009-09-05 21:47:34 +0000772 double
cristy47e00502009-12-17 19:19:57 +0000773 *kernel,
774 normalize;
cristy3ed852e2009-09-05 21:47:34 +0000775
776 long
cristy47e00502009-12-17 19:19:57 +0000777 j,
778 k;
cristy3ed852e2009-09-05 21:47:34 +0000779
780 register long
781 i;
782
783 /*
784 Generate a 1-D convolution kernel.
785 */
786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
787 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
788 if (kernel == (double *) NULL)
789 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000790 normalize=0.0;
cristy47e00502009-12-17 19:19:57 +0000791 j=(long) width/2;
792 i=0;
793 for (k=(-j); k <= j; k++)
794 {
cristyf267c722009-12-18 00:07:22 +0000795 kernel[i]=exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
cristy47e00502009-12-17 19:19:57 +0000796 (MagickSQ2PI*MagickSigma);
cristy3ed852e2009-09-05 21:47:34 +0000797 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +0000798 i++;
799 }
cristy3ed852e2009-09-05 21:47:34 +0000800 for (i=0; i < (long) width; i++)
801 kernel[i]/=normalize;
802 return(kernel);
803}
804
805MagickExport Image *BlurImageChannel(const Image *image,
806 const ChannelType channel,const double radius,const double sigma,
807 ExceptionInfo *exception)
808{
809#define BlurImageTag "Blur/Image"
810
cristyc4c8d132010-01-07 01:58:38 +0000811 CacheView
812 *blur_view,
813 *image_view;
814
cristy3ed852e2009-09-05 21:47:34 +0000815 double
816 *kernel;
817
818 Image
819 *blur_image;
820
821 long
822 progress,
823 x,
824 y;
825
826 MagickBooleanType
827 status;
828
829 MagickPixelPacket
cristy3ed852e2009-09-05 21:47:34 +0000830 bias;
831
832 register long
833 i;
834
835 unsigned long
836 width;
837
cristy3ed852e2009-09-05 21:47:34 +0000838 /*
839 Initialize blur image attributes.
840 */
841 assert(image != (Image *) NULL);
842 assert(image->signature == MagickSignature);
843 if (image->debug != MagickFalse)
844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
845 assert(exception != (ExceptionInfo *) NULL);
846 assert(exception->signature == MagickSignature);
847 blur_image=CloneImage(image,0,0,MagickTrue,exception);
848 if (blur_image == (Image *) NULL)
849 return((Image *) NULL);
850 if (fabs(sigma) <= MagickEpsilon)
851 return(blur_image);
852 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
853 {
854 InheritException(exception,&blur_image->exception);
855 blur_image=DestroyImage(blur_image);
856 return((Image *) NULL);
857 }
858 width=GetOptimalKernelWidth1D(radius,sigma);
859 kernel=GetBlurKernel(width,sigma);
860 if (kernel == (double *) NULL)
861 {
862 blur_image=DestroyImage(blur_image);
863 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
864 }
865 if (image->debug != MagickFalse)
866 {
867 char
868 format[MaxTextExtent],
869 *message;
870
871 register const double
872 *k;
873
874 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
875 " BlurImage with %ld kernel:",width);
876 message=AcquireString("");
877 k=kernel;
878 for (i=0; i < (long) width; i++)
879 {
880 *message='\0';
881 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",i);
882 (void) ConcatenateString(&message,format);
cristy8cd5b312010-01-07 01:10:24 +0000883 (void) FormatMagickString(format,MaxTextExtent,"%.15g ",*k++);
cristy3ed852e2009-09-05 21:47:34 +0000884 (void) ConcatenateString(&message,format);
885 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
886 }
887 message=DestroyString(message);
888 }
889 /*
890 Blur rows.
891 */
892 status=MagickTrue;
893 progress=0;
cristyddd82202009-11-03 20:14:50 +0000894 GetMagickPixelPacket(image,&bias);
895 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +0000896 image_view=AcquireCacheView(image);
897 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +0000898#if defined(MAGICKCORE_OPENMP_SUPPORT)
899 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000900#endif
901 for (y=0; y < (long) blur_image->rows; y++)
902 {
903 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000904 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000905
906 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000907 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000908
909 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000910 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000911
912 register long
913 x;
914
915 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000916 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000917
918 if (status == MagickFalse)
919 continue;
920 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y,image->columns+
921 width,1,exception);
922 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
923 exception);
924 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
925 {
926 status=MagickFalse;
927 continue;
928 }
929 indexes=GetCacheViewVirtualIndexQueue(image_view);
930 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
931 for (x=0; x < (long) blur_image->columns; x++)
932 {
933 MagickPixelPacket
934 pixel;
935
936 register const double
cristyc47d1f82009-11-26 01:44:43 +0000937 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +0000938
939 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000940 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000941
942 register long
943 i;
944
cristyddd82202009-11-03 20:14:50 +0000945 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +0000946 k=kernel;
947 kernel_pixels=p;
948 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
949 {
950 for (i=0; i < (long) width; i++)
951 {
952 pixel.red+=(*k)*kernel_pixels->red;
953 pixel.green+=(*k)*kernel_pixels->green;
954 pixel.blue+=(*k)*kernel_pixels->blue;
955 k++;
956 kernel_pixels++;
957 }
958 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000959 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000960 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000961 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000962 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +0000963 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000964 if ((channel & OpacityChannel) != 0)
965 {
966 k=kernel;
967 kernel_pixels=p;
968 for (i=0; i < (long) width; i++)
969 {
970 pixel.opacity+=(*k)*kernel_pixels->opacity;
971 k++;
972 kernel_pixels++;
973 }
cristyce70c172010-01-07 17:15:30 +0000974 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +0000975 }
976 if (((channel & IndexChannel) != 0) &&
977 (image->colorspace == CMYKColorspace))
978 {
979 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000980 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +0000981
982 k=kernel;
983 kernel_indexes=indexes;
984 for (i=0; i < (long) width; i++)
985 {
986 pixel.index+=(*k)*(*kernel_indexes);
987 k++;
988 kernel_indexes++;
989 }
cristyce70c172010-01-07 17:15:30 +0000990 blur_indexes[x]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +0000991 }
992 }
993 else
994 {
995 MagickRealType
996 alpha,
997 gamma;
998
999 gamma=0.0;
1000 for (i=0; i < (long) width; i++)
1001 {
1002 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1003 kernel_pixels->opacity));
1004 pixel.red+=(*k)*alpha*kernel_pixels->red;
1005 pixel.green+=(*k)*alpha*kernel_pixels->green;
1006 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1007 gamma+=(*k)*alpha;
1008 k++;
1009 kernel_pixels++;
1010 }
1011 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1012 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001013 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001014 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001015 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001016 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001017 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001018 if ((channel & OpacityChannel) != 0)
1019 {
1020 k=kernel;
1021 kernel_pixels=p;
1022 for (i=0; i < (long) width; i++)
1023 {
1024 pixel.opacity+=(*k)*kernel_pixels->opacity;
1025 k++;
1026 kernel_pixels++;
1027 }
cristyce70c172010-01-07 17:15:30 +00001028 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001029 }
1030 if (((channel & IndexChannel) != 0) &&
1031 (image->colorspace == CMYKColorspace))
1032 {
1033 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001034 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001035
1036 k=kernel;
1037 kernel_pixels=p;
1038 kernel_indexes=indexes;
1039 for (i=0; i < (long) width; i++)
1040 {
1041 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1042 kernel_pixels->opacity));
1043 pixel.index+=(*k)*alpha*(*kernel_indexes);
1044 k++;
1045 kernel_pixels++;
1046 kernel_indexes++;
1047 }
cristyce70c172010-01-07 17:15:30 +00001048 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001049 }
1050 }
1051 p++;
1052 q++;
1053 }
1054 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1055 status=MagickFalse;
1056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1057 {
1058 MagickBooleanType
1059 proceed;
1060
cristyb5d5f722009-11-04 03:03:49 +00001061#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001062 #pragma omp critical (MagickCore_BlurImageChannel)
1063#endif
1064 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1065 blur_image->columns);
1066 if (proceed == MagickFalse)
1067 status=MagickFalse;
1068 }
1069 }
1070 blur_view=DestroyCacheView(blur_view);
1071 image_view=DestroyCacheView(image_view);
1072 /*
1073 Blur columns.
1074 */
1075 image_view=AcquireCacheView(blur_image);
1076 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00001077#if defined(MAGICKCORE_OPENMP_SUPPORT)
1078 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001079#endif
1080 for (x=0; x < (long) blur_image->columns; x++)
1081 {
1082 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001083 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001084
1085 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001086 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001087
1088 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001089 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001090
1091 register long
1092 y;
1093
1094 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001095 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001096
1097 if (status == MagickFalse)
1098 continue;
1099 p=GetCacheViewVirtualPixels(image_view,x,-((long) width/2L),1,image->rows+
1100 width,exception);
1101 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1102 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1103 {
1104 status=MagickFalse;
1105 continue;
1106 }
1107 indexes=GetCacheViewVirtualIndexQueue(image_view);
1108 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
1109 for (y=0; y < (long) blur_image->rows; y++)
1110 {
1111 MagickPixelPacket
1112 pixel;
1113
1114 register const double
cristyc47d1f82009-11-26 01:44:43 +00001115 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00001116
1117 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001118 *restrict kernel_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001119
1120 register long
1121 i;
1122
cristyddd82202009-11-03 20:14:50 +00001123 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00001124 k=kernel;
1125 kernel_pixels=p;
1126 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1127 {
1128 for (i=0; i < (long) width; i++)
1129 {
1130 pixel.red+=(*k)*kernel_pixels->red;
1131 pixel.green+=(*k)*kernel_pixels->green;
1132 pixel.blue+=(*k)*kernel_pixels->blue;
1133 k++;
1134 kernel_pixels++;
1135 }
1136 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001137 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001138 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001139 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001140 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001141 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001142 if ((channel & OpacityChannel) != 0)
1143 {
1144 k=kernel;
1145 kernel_pixels=p;
1146 for (i=0; i < (long) width; i++)
1147 {
1148 pixel.opacity+=(*k)*kernel_pixels->opacity;
1149 k++;
1150 kernel_pixels++;
1151 }
cristyce70c172010-01-07 17:15:30 +00001152 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001153 }
1154 if (((channel & IndexChannel) != 0) &&
1155 (image->colorspace == CMYKColorspace))
1156 {
1157 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001158 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001159
1160 k=kernel;
1161 kernel_indexes=indexes;
1162 for (i=0; i < (long) width; i++)
1163 {
1164 pixel.index+=(*k)*(*kernel_indexes);
1165 k++;
1166 kernel_indexes++;
1167 }
cristyce70c172010-01-07 17:15:30 +00001168 blur_indexes[y]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00001169 }
1170 }
1171 else
1172 {
1173 MagickRealType
1174 alpha,
1175 gamma;
1176
1177 gamma=0.0;
1178 for (i=0; i < (long) width; i++)
1179 {
1180 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1181 kernel_pixels->opacity));
1182 pixel.red+=(*k)*alpha*kernel_pixels->red;
1183 pixel.green+=(*k)*alpha*kernel_pixels->green;
1184 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1185 gamma+=(*k)*alpha;
1186 k++;
1187 kernel_pixels++;
1188 }
1189 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1190 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001191 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001192 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001193 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001194 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001195 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001196 if ((channel & OpacityChannel) != 0)
1197 {
1198 k=kernel;
1199 kernel_pixels=p;
1200 for (i=0; i < (long) width; i++)
1201 {
1202 pixel.opacity+=(*k)*kernel_pixels->opacity;
1203 k++;
1204 kernel_pixels++;
1205 }
cristyce70c172010-01-07 17:15:30 +00001206 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001207 }
1208 if (((channel & IndexChannel) != 0) &&
1209 (image->colorspace == CMYKColorspace))
1210 {
1211 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001212 *restrict kernel_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001213
1214 k=kernel;
1215 kernel_pixels=p;
1216 kernel_indexes=indexes;
1217 for (i=0; i < (long) width; i++)
1218 {
1219 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1220 kernel_pixels->opacity));
1221 pixel.index+=(*k)*alpha*(*kernel_indexes);
1222 k++;
1223 kernel_pixels++;
1224 kernel_indexes++;
1225 }
cristyce70c172010-01-07 17:15:30 +00001226 blur_indexes[y]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001227 }
1228 }
1229 p++;
1230 q++;
1231 }
1232 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1233 status=MagickFalse;
1234 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1235 {
1236 MagickBooleanType
1237 proceed;
1238
cristyb5d5f722009-11-04 03:03:49 +00001239#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001240 #pragma omp critical (MagickCore_BlurImageChannel)
1241#endif
1242 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1243 blur_image->columns);
1244 if (proceed == MagickFalse)
1245 status=MagickFalse;
1246 }
1247 }
1248 blur_view=DestroyCacheView(blur_view);
1249 image_view=DestroyCacheView(image_view);
1250 kernel=(double *) RelinquishMagickMemory(kernel);
1251 if (status == MagickFalse)
1252 blur_image=DestroyImage(blur_image);
1253 blur_image->type=image->type;
1254 return(blur_image);
1255}
1256
1257/*
1258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259% %
1260% %
1261% %
cristyfccdab92009-11-30 16:43:57 +00001262% C o n v o l v e I m a g e %
1263% %
1264% %
1265% %
1266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267%
1268% ConvolveImage() applies a custom convolution kernel to the image.
1269%
1270% The format of the ConvolveImage method is:
1271%
1272% Image *ConvolveImage(const Image *image,const unsigned long order,
1273% const double *kernel,ExceptionInfo *exception)
1274% Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
1275% const unsigned long order,const double *kernel,
1276% ExceptionInfo *exception)
1277%
1278% A description of each parameter follows:
1279%
1280% o image: the image.
1281%
1282% o channel: the channel type.
1283%
1284% o order: the number of columns and rows in the filter kernel.
1285%
1286% o kernel: An array of double representing the convolution kernel.
1287%
1288% o exception: return any errors or warnings in this structure.
1289%
1290*/
1291
1292MagickExport Image *ConvolveImage(const Image *image,const unsigned long order,
1293 const double *kernel,ExceptionInfo *exception)
1294{
1295 Image
1296 *convolve_image;
1297
1298 convolve_image=ConvolveImageChannel(image,DefaultChannels,order,kernel,
1299 exception);
1300 return(convolve_image);
1301}
1302
1303MagickExport Image *ConvolveImageChannel(const Image *image,
1304 const ChannelType channel,const unsigned long order,const double *kernel,
1305 ExceptionInfo *exception)
1306{
1307#define ConvolveImageTag "Convolve/Image"
1308
cristyc4c8d132010-01-07 01:58:38 +00001309 CacheView
1310 *convolve_view,
1311 *image_view;
1312
cristyfccdab92009-11-30 16:43:57 +00001313 double
1314 *normal_kernel;
1315
1316 Image
1317 *convolve_image;
1318
1319 long
1320 progress,
1321 y;
1322
1323 MagickBooleanType
1324 status;
1325
1326 MagickPixelPacket
1327 bias;
1328
1329 MagickRealType
1330 gamma;
1331
1332 register long
1333 i;
1334
1335 unsigned long
1336 width;
1337
cristyfccdab92009-11-30 16:43:57 +00001338 /*
1339 Initialize convolve image attributes.
1340 */
1341 assert(image != (Image *) NULL);
1342 assert(image->signature == MagickSignature);
1343 if (image->debug != MagickFalse)
1344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1345 assert(exception != (ExceptionInfo *) NULL);
1346 assert(exception->signature == MagickSignature);
1347 width=order;
1348 if ((width % 2) == 0)
1349 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1350 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1351 if (convolve_image == (Image *) NULL)
1352 return((Image *) NULL);
1353 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1354 {
1355 InheritException(exception,&convolve_image->exception);
1356 convolve_image=DestroyImage(convolve_image);
1357 return((Image *) NULL);
1358 }
1359 if (image->debug != MagickFalse)
1360 {
1361 char
1362 format[MaxTextExtent],
1363 *message;
1364
1365 long
1366 u,
1367 v;
1368
1369 register const double
1370 *k;
1371
1372 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1373 " ConvolveImage with %ldx%ld kernel:",width,width);
1374 message=AcquireString("");
1375 k=kernel;
1376 for (v=0; v < (long) width; v++)
1377 {
1378 *message='\0';
1379 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
1380 (void) ConcatenateString(&message,format);
1381 for (u=0; u < (long) width; u++)
1382 {
cristy8cd5b312010-01-07 01:10:24 +00001383 (void) FormatMagickString(format,MaxTextExtent,"%.15g ",*k++);
cristyfccdab92009-11-30 16:43:57 +00001384 (void) ConcatenateString(&message,format);
1385 }
1386 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1387 }
1388 message=DestroyString(message);
1389 }
1390 /*
1391 Normalize kernel.
1392 */
1393 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1394 sizeof(*normal_kernel));
1395 if (normal_kernel == (double *) NULL)
1396 {
1397 convolve_image=DestroyImage(convolve_image);
1398 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1399 }
1400 gamma=0.0;
1401 for (i=0; i < (long) (width*width); i++)
1402 gamma+=kernel[i];
1403 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1404 for (i=0; i < (long) (width*width); i++)
1405 normal_kernel[i]=gamma*kernel[i];
1406 /*
1407 Convolve image.
1408 */
1409 status=MagickTrue;
1410 progress=0;
1411 GetMagickPixelPacket(image,&bias);
1412 SetMagickPixelPacketBias(image,&bias);
1413 image_view=AcquireCacheView(image);
1414 convolve_view=AcquireCacheView(convolve_image);
1415#if defined(MAGICKCORE_OPENMP_SUPPORT)
1416 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1417#endif
1418 for (y=0; y < (long) image->rows; y++)
1419 {
1420 MagickBooleanType
1421 sync;
1422
1423 register const IndexPacket
1424 *restrict indexes;
1425
1426 register const PixelPacket
1427 *restrict p;
1428
1429 register IndexPacket
1430 *restrict convolve_indexes;
1431
1432 register long
1433 x;
1434
1435 register PixelPacket
1436 *restrict q;
1437
1438 if (status == MagickFalse)
1439 continue;
1440 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
1441 2L),image->columns+width,width,exception);
1442 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1443 exception);
1444 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1445 {
1446 status=MagickFalse;
1447 continue;
1448 }
1449 indexes=GetCacheViewVirtualIndexQueue(image_view);
1450 convolve_indexes=GetCacheViewAuthenticIndexQueue(convolve_view);
1451 for (x=0; x < (long) image->columns; x++)
1452 {
1453 long
1454 v;
1455
1456 MagickPixelPacket
1457 pixel;
1458
1459 register const double
1460 *restrict k;
1461
1462 register const PixelPacket
1463 *restrict kernel_pixels;
1464
1465 register long
1466 u;
1467
1468 pixel=bias;
1469 k=normal_kernel;
1470 kernel_pixels=p;
1471 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1472 {
1473 for (v=0; v < (long) width; v++)
1474 {
1475 for (u=0; u < (long) width; u++)
1476 {
1477 pixel.red+=(*k)*kernel_pixels[u].red;
1478 pixel.green+=(*k)*kernel_pixels[u].green;
1479 pixel.blue+=(*k)*kernel_pixels[u].blue;
1480 k++;
1481 }
1482 kernel_pixels+=image->columns+width;
1483 }
1484 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001485 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001486 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001487 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001488 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001489 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001490 if ((channel & OpacityChannel) != 0)
1491 {
1492 k=normal_kernel;
1493 kernel_pixels=p;
1494 for (v=0; v < (long) width; v++)
1495 {
1496 for (u=0; u < (long) width; u++)
1497 {
1498 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1499 k++;
1500 }
1501 kernel_pixels+=image->columns+width;
1502 }
cristyce70c172010-01-07 17:15:30 +00001503 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001504 }
1505 if (((channel & IndexChannel) != 0) &&
1506 (image->colorspace == CMYKColorspace))
1507 {
1508 register const IndexPacket
1509 *restrict kernel_indexes;
1510
1511 k=normal_kernel;
1512 kernel_indexes=indexes;
1513 for (v=0; v < (long) width; v++)
1514 {
1515 for (u=0; u < (long) width; u++)
1516 {
1517 pixel.index+=(*k)*kernel_indexes[u];
1518 k++;
1519 }
1520 kernel_indexes+=image->columns+width;
1521 }
cristyce70c172010-01-07 17:15:30 +00001522 convolve_indexes[x]=ClampToQuantum(pixel.index);
cristyfccdab92009-11-30 16:43:57 +00001523 }
1524 }
1525 else
1526 {
1527 MagickRealType
1528 alpha,
1529 gamma;
1530
1531 gamma=0.0;
1532 for (v=0; v < (long) width; v++)
1533 {
1534 for (u=0; u < (long) width; u++)
1535 {
1536 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1537 kernel_pixels[u].opacity));
1538 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
1539 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
1540 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
cristyfccdab92009-11-30 16:43:57 +00001541 gamma+=(*k)*alpha;
1542 k++;
1543 }
1544 kernel_pixels+=image->columns+width;
1545 }
1546 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1547 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001548 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001549 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001550 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001551 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00001552 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001553 if ((channel & OpacityChannel) != 0)
1554 {
1555 k=normal_kernel;
1556 kernel_pixels=p;
1557 for (v=0; v < (long) width; v++)
1558 {
1559 for (u=0; u < (long) width; u++)
1560 {
1561 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1562 k++;
1563 }
1564 kernel_pixels+=image->columns+width;
1565 }
cristyce70c172010-01-07 17:15:30 +00001566 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001567 }
1568 if (((channel & IndexChannel) != 0) &&
1569 (image->colorspace == CMYKColorspace))
1570 {
1571 register const IndexPacket
1572 *restrict kernel_indexes;
1573
1574 k=normal_kernel;
1575 kernel_pixels=p;
1576 kernel_indexes=indexes;
1577 for (v=0; v < (long) width; v++)
1578 {
1579 for (u=0; u < (long) width; u++)
1580 {
1581 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1582 kernel_pixels[u].opacity));
1583 pixel.index+=(*k)*alpha*kernel_indexes[u];
1584 k++;
1585 }
1586 kernel_pixels+=image->columns+width;
1587 kernel_indexes+=image->columns+width;
1588 }
cristy24b06da2010-01-09 23:05:56 +00001589 convolve_indexes[x]=ClampToQuantum(gamma*
1590 GetIndexPixelComponent(&pixel));
cristyfccdab92009-11-30 16:43:57 +00001591 }
1592 }
1593 p++;
1594 q++;
1595 }
1596 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1597 if (sync == MagickFalse)
1598 status=MagickFalse;
1599 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1600 {
1601 MagickBooleanType
1602 proceed;
1603
1604#if defined(MAGICKCORE_OPENMP_SUPPORT)
1605 #pragma omp critical (MagickCore_ConvolveImageChannel)
1606#endif
1607 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1608 if (proceed == MagickFalse)
1609 status=MagickFalse;
1610 }
1611 }
1612 convolve_image->type=image->type;
1613 convolve_view=DestroyCacheView(convolve_view);
1614 image_view=DestroyCacheView(image_view);
1615 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1616 if (status == MagickFalse)
1617 convolve_image=DestroyImage(convolve_image);
1618 return(convolve_image);
1619}
1620
1621/*
1622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1623% %
1624% %
1625% %
cristy3ed852e2009-09-05 21:47:34 +00001626% D e s p e c k l e I m a g e %
1627% %
1628% %
1629% %
1630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631%
1632% DespeckleImage() reduces the speckle noise in an image while perserving the
1633% edges of the original image.
1634%
1635% The format of the DespeckleImage method is:
1636%
1637% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1638%
1639% A description of each parameter follows:
1640%
1641% o image: the image.
1642%
1643% o exception: return any errors or warnings in this structure.
1644%
1645*/
1646
1647static Quantum **DestroyPixelThreadSet(Quantum **pixels)
1648{
1649 register long
1650 i;
1651
1652 assert(pixels != (Quantum **) NULL);
1653 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
1654 if (pixels[i] != (Quantum *) NULL)
1655 pixels[i]=(Quantum *) RelinquishMagickMemory(pixels[i]);
1656 pixels=(Quantum **) RelinquishAlignedMemory(pixels);
1657 return(pixels);
1658}
1659
1660static Quantum **AcquirePixelThreadSet(const size_t count)
1661{
1662 register long
1663 i;
1664
1665 Quantum
1666 **pixels;
1667
1668 unsigned long
1669 number_threads;
1670
1671 number_threads=GetOpenMPMaximumThreads();
1672 pixels=(Quantum **) AcquireAlignedMemory(number_threads,sizeof(*pixels));
1673 if (pixels == (Quantum **) NULL)
1674 return((Quantum **) NULL);
1675 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1676 for (i=0; i < (long) number_threads; i++)
1677 {
1678 pixels[i]=(Quantum *) AcquireQuantumMemory(count,sizeof(**pixels));
1679 if (pixels[i] == (Quantum *) NULL)
1680 return(DestroyPixelThreadSet(pixels));
1681 }
1682 return(pixels);
1683}
1684
1685static void Hull(const long x_offset,const long y_offset,
1686 const unsigned long columns,const unsigned long rows,Quantum *f,Quantum *g,
1687 const int polarity)
1688{
1689 long
1690 y;
1691
1692 MagickRealType
1693 v;
1694
1695 register long
1696 x;
1697
1698 register Quantum
1699 *p,
1700 *q,
1701 *r,
1702 *s;
1703
1704 assert(f != (Quantum *) NULL);
1705 assert(g != (Quantum *) NULL);
1706 p=f+(columns+2);
1707 q=g+(columns+2);
1708 r=p+(y_offset*((long) columns+2)+x_offset);
1709 for (y=0; y < (long) rows; y++)
1710 {
1711 p++;
1712 q++;
1713 r++;
1714 if (polarity > 0)
1715 for (x=(long) columns; x != 0; x--)
1716 {
1717 v=(MagickRealType) (*p);
1718 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1719 v+=ScaleCharToQuantum(1);
1720 *q=(Quantum) v;
1721 p++;
1722 q++;
1723 r++;
1724 }
1725 else
1726 for (x=(long) columns; x != 0; x--)
1727 {
1728 v=(MagickRealType) (*p);
1729 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1730 v-=(long) ScaleCharToQuantum(1);
1731 *q=(Quantum) v;
1732 p++;
1733 q++;
1734 r++;
1735 }
1736 p++;
1737 q++;
1738 r++;
1739 }
1740 p=f+(columns+2);
1741 q=g+(columns+2);
1742 r=q+(y_offset*((long) columns+2)+x_offset);
1743 s=q-(y_offset*((long) columns+2)+x_offset);
1744 for (y=0; y < (long) rows; y++)
1745 {
1746 p++;
1747 q++;
1748 r++;
1749 s++;
1750 if (polarity > 0)
1751 for (x=(long) columns; x != 0; x--)
1752 {
1753 v=(MagickRealType) (*q);
1754 if (((MagickRealType) *s >=
1755 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1756 ((MagickRealType) *r > v))
1757 v+=ScaleCharToQuantum(1);
1758 *p=(Quantum) v;
1759 p++;
1760 q++;
1761 r++;
1762 s++;
1763 }
1764 else
1765 for (x=(long) columns; x != 0; x--)
1766 {
1767 v=(MagickRealType) (*q);
1768 if (((MagickRealType) *s <=
1769 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1770 ((MagickRealType) *r < v))
1771 v-=(MagickRealType) ScaleCharToQuantum(1);
1772 *p=(Quantum) v;
1773 p++;
1774 q++;
1775 r++;
1776 s++;
1777 }
1778 p++;
1779 q++;
1780 r++;
1781 s++;
1782 }
1783}
1784
1785MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1786{
1787#define DespeckleImageTag "Despeckle/Image"
1788
cristy2407fc22009-09-11 00:55:25 +00001789 CacheView
1790 *despeckle_view,
1791 *image_view;
1792
cristy3ed852e2009-09-05 21:47:34 +00001793 Image
1794 *despeckle_image;
1795
1796 long
1797 channel;
1798
1799 MagickBooleanType
1800 status;
1801
1802 Quantum
cristyfa112112010-01-04 17:48:07 +00001803 **restrict buffers,
1804 **restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001805
1806 size_t
1807 length;
1808
1809 static const int
cristy691a29e2009-09-11 00:44:10 +00001810 X[4] = {0, 1, 1,-1},
1811 Y[4] = {1, 0, 1, 1};
cristy3ed852e2009-09-05 21:47:34 +00001812
cristy3ed852e2009-09-05 21:47:34 +00001813 /*
1814 Allocate despeckled image.
1815 */
1816 assert(image != (const Image *) NULL);
1817 assert(image->signature == MagickSignature);
1818 if (image->debug != MagickFalse)
1819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1820 assert(exception != (ExceptionInfo *) NULL);
1821 assert(exception->signature == MagickSignature);
1822 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1823 exception);
1824 if (despeckle_image == (Image *) NULL)
1825 return((Image *) NULL);
1826 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1827 {
1828 InheritException(exception,&despeckle_image->exception);
1829 despeckle_image=DestroyImage(despeckle_image);
1830 return((Image *) NULL);
1831 }
1832 /*
1833 Allocate image buffers.
1834 */
1835 length=(size_t) ((image->columns+2)*(image->rows+2));
1836 pixels=AcquirePixelThreadSet(length);
1837 buffers=AcquirePixelThreadSet(length);
1838 if ((pixels == (Quantum **) NULL) || (buffers == (Quantum **) NULL))
1839 {
1840 if (buffers != (Quantum **) NULL)
1841 buffers=DestroyPixelThreadSet(buffers);
1842 if (pixels != (Quantum **) NULL)
1843 pixels=DestroyPixelThreadSet(pixels);
1844 despeckle_image=DestroyImage(despeckle_image);
1845 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1846 }
1847 /*
1848 Reduce speckle in the image.
1849 */
1850 status=MagickTrue;
1851 image_view=AcquireCacheView(image);
1852 despeckle_view=AcquireCacheView(despeckle_image);
cristyb5d5f722009-11-04 03:03:49 +00001853#if defined(MAGICKCORE_OPENMP_SUPPORT)
1854 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00001855#endif
1856 for (channel=0; channel <= 3; channel++)
1857 {
1858 long
1859 j,
1860 y;
1861
1862 register long
1863 i,
cristy691a29e2009-09-11 00:44:10 +00001864 id,
cristy3ed852e2009-09-05 21:47:34 +00001865 x;
1866
1867 register Quantum
1868 *buffer,
1869 *pixel;
1870
1871 if (status == MagickFalse)
1872 continue;
cristy691a29e2009-09-11 00:44:10 +00001873 id=GetOpenMPThreadId();
1874 pixel=pixels[id];
cristy3ed852e2009-09-05 21:47:34 +00001875 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
cristy691a29e2009-09-11 00:44:10 +00001876 buffer=buffers[id];
cristy3ed852e2009-09-05 21:47:34 +00001877 j=(long) image->columns+2;
1878 for (y=0; y < (long) image->rows; y++)
1879 {
1880 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001881 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001882
1883 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1884 if (p == (const PixelPacket *) NULL)
1885 break;
1886 j++;
1887 for (x=0; x < (long) image->columns; x++)
1888 {
1889 switch (channel)
1890 {
cristyce70c172010-01-07 17:15:30 +00001891 case 0: pixel[j]=GetRedPixelComponent(p); break;
1892 case 1: pixel[j]=GetGreenPixelComponent(p); break;
1893 case 2: pixel[j]=GetBluePixelComponent(p); break;
1894 case 3: pixel[j]=GetOpacityPixelComponent(p); break;
cristy3ed852e2009-09-05 21:47:34 +00001895 default: break;
1896 }
1897 p++;
1898 j++;
1899 }
1900 j++;
1901 }
cristy3ed852e2009-09-05 21:47:34 +00001902 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1903 for (i=0; i < 4; i++)
1904 {
1905 Hull(X[i],Y[i],image->columns,image->rows,pixel,buffer,1);
1906 Hull(-X[i],-Y[i],image->columns,image->rows,pixel,buffer,1);
1907 Hull(-X[i],-Y[i],image->columns,image->rows,pixel,buffer,-1);
1908 Hull(X[i],Y[i],image->columns,image->rows,pixel,buffer,-1);
1909 }
1910 j=(long) image->columns+2;
1911 for (y=0; y < (long) image->rows; y++)
1912 {
1913 MagickBooleanType
1914 sync;
1915
1916 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001917 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001918
1919 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1920 1,exception);
1921 if (q == (PixelPacket *) NULL)
1922 break;
1923 j++;
1924 for (x=0; x < (long) image->columns; x++)
1925 {
1926 switch (channel)
1927 {
1928 case 0: q->red=pixel[j]; break;
1929 case 1: q->green=pixel[j]; break;
1930 case 2: q->blue=pixel[j]; break;
1931 case 3: q->opacity=pixel[j]; break;
1932 default: break;
1933 }
1934 q++;
1935 j++;
1936 }
1937 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1938 if (sync == MagickFalse)
1939 {
1940 status=MagickFalse;
1941 break;
1942 }
1943 j++;
1944 }
1945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1946 {
1947 MagickBooleanType
1948 proceed;
1949
cristyb5d5f722009-11-04 03:03:49 +00001950#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001951 #pragma omp critical (MagickCore_DespeckleImage)
1952#endif
1953 proceed=SetImageProgress(image,DespeckleImageTag,channel,3);
1954 if (proceed == MagickFalse)
1955 status=MagickFalse;
1956 }
1957 }
1958 despeckle_view=DestroyCacheView(despeckle_view);
1959 image_view=DestroyCacheView(image_view);
1960 buffers=DestroyPixelThreadSet(buffers);
1961 pixels=DestroyPixelThreadSet(pixels);
1962 despeckle_image->type=image->type;
1963 if (status == MagickFalse)
1964 despeckle_image=DestroyImage(despeckle_image);
1965 return(despeckle_image);
1966}
1967
1968/*
1969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970% %
1971% %
1972% %
1973% E d g e I m a g e %
1974% %
1975% %
1976% %
1977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1978%
1979% EdgeImage() finds edges in an image. Radius defines the radius of the
1980% convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1981% radius for you.
1982%
1983% The format of the EdgeImage method is:
1984%
1985% Image *EdgeImage(const Image *image,const double radius,
1986% ExceptionInfo *exception)
1987%
1988% A description of each parameter follows:
1989%
1990% o image: the image.
1991%
1992% o radius: the radius of the pixel neighborhood.
1993%
1994% o exception: return any errors or warnings in this structure.
1995%
1996*/
1997MagickExport Image *EdgeImage(const Image *image,const double radius,
1998 ExceptionInfo *exception)
1999{
2000 Image
2001 *edge_image;
2002
2003 double
2004 *kernel;
2005
2006 register long
2007 i;
2008
2009 unsigned long
2010 width;
2011
2012 assert(image != (const Image *) NULL);
2013 assert(image->signature == MagickSignature);
2014 if (image->debug != MagickFalse)
2015 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2016 assert(exception != (ExceptionInfo *) NULL);
2017 assert(exception->signature == MagickSignature);
2018 width=GetOptimalKernelWidth1D(radius,0.5);
2019 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2020 if (kernel == (double *) NULL)
2021 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2022 for (i=0; i < (long) (width*width); i++)
2023 kernel[i]=(-1.0);
2024 kernel[i/2]=(double) (width*width-1.0);
2025 edge_image=ConvolveImage(image,width,kernel,exception);
2026 kernel=(double *) RelinquishMagickMemory(kernel);
2027 return(edge_image);
2028}
2029
2030/*
2031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032% %
2033% %
2034% %
2035% E m b o s s I m a g e %
2036% %
2037% %
2038% %
2039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2040%
2041% EmbossImage() returns a grayscale image with a three-dimensional effect.
2042% We convolve the image with a Gaussian operator of the given radius and
2043% standard deviation (sigma). For reasonable results, radius should be
2044% larger than sigma. Use a radius of 0 and Emboss() selects a suitable
2045% radius for you.
2046%
2047% The format of the EmbossImage method is:
2048%
2049% Image *EmbossImage(const Image *image,const double radius,
2050% const double sigma,ExceptionInfo *exception)
2051%
2052% A description of each parameter follows:
2053%
2054% o image: the image.
2055%
2056% o radius: the radius of the pixel neighborhood.
2057%
2058% o sigma: the standard deviation of the Gaussian, in pixels.
2059%
2060% o exception: return any errors or warnings in this structure.
2061%
2062*/
2063MagickExport Image *EmbossImage(const Image *image,const double radius,
2064 const double sigma,ExceptionInfo *exception)
2065{
2066 double
2067 *kernel;
2068
2069 Image
2070 *emboss_image;
2071
2072 long
cristy47e00502009-12-17 19:19:57 +00002073 j,
2074 k,
cristy3ed852e2009-09-05 21:47:34 +00002075 u,
2076 v;
2077
cristy47e00502009-12-17 19:19:57 +00002078 register long
2079 i;
2080
cristy3ed852e2009-09-05 21:47:34 +00002081 unsigned long
2082 width;
2083
2084 assert(image != (Image *) NULL);
2085 assert(image->signature == MagickSignature);
2086 if (image->debug != MagickFalse)
2087 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2088 assert(exception != (ExceptionInfo *) NULL);
2089 assert(exception->signature == MagickSignature);
2090 width=GetOptimalKernelWidth2D(radius,sigma);
2091 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2092 if (kernel == (double *) NULL)
2093 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002094 j=(long) width/2;
cristy47e00502009-12-17 19:19:57 +00002095 k=j;
2096 i=0;
2097 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002098 {
cristy47e00502009-12-17 19:19:57 +00002099 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00002100 {
cristy47e00502009-12-17 19:19:57 +00002101 kernel[i]=((u < 0) || (v < 0) ? -8.0 : 8.0)*
2102 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
2103 (2.0*MagickPI*MagickSigma*MagickSigma);
2104 if (u != k)
cristy3ed852e2009-09-05 21:47:34 +00002105 kernel[i]=0.0;
2106 i++;
2107 }
cristy47e00502009-12-17 19:19:57 +00002108 k--;
cristy3ed852e2009-09-05 21:47:34 +00002109 }
2110 emboss_image=ConvolveImage(image,width,kernel,exception);
2111 if (emboss_image != (Image *) NULL)
2112 (void) EqualizeImage(emboss_image);
2113 kernel=(double *) RelinquishMagickMemory(kernel);
2114 return(emboss_image);
2115}
2116
2117/*
2118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119% %
2120% %
2121% %
cristy56a9e512010-01-06 18:18:55 +00002122% F i l t e r I m a g e %
2123% %
2124% %
2125% %
2126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2127%
2128% FilterImage() applies a custom convolution kernel to the image.
2129%
2130% The format of the FilterImage method is:
2131%
2132% Image *FilterImage(const Image *image,const MagickKernel *kernel,
2133% ExceptionInfo *exception)
2134% Image *FilterImageChannel(const Image *image,const ChannelType channel,
2135% const MagickKernel *kernel,ExceptionInfo *exception)
2136%
2137% A description of each parameter follows:
2138%
2139% o image: the image.
2140%
2141% o channel: the channel type.
2142%
2143% o kernel: the filtering kernel.
2144%
2145% o exception: return any errors or warnings in this structure.
2146%
2147*/
2148
2149MagickExport Image *FilterImage(const Image *image,const MagickKernel *kernel,
2150 ExceptionInfo *exception)
2151{
2152 Image
2153 *filter_image;
2154
2155 filter_image=FilterImageChannel(image,DefaultChannels,kernel,exception);
2156 return(filter_image);
2157}
2158
2159MagickExport Image *FilterImageChannel(const Image *image,
2160 const ChannelType channel,const MagickKernel *kernel,ExceptionInfo *exception)
2161{
2162#define FilterImageTag "Filter/Image"
2163
2164 CacheView
2165 *filter_view,
2166 *image_view;
2167
2168 double
2169 *normal_kernel;
2170
2171 Image
2172 *filter_image;
2173
2174 long
2175 progress,
2176 y;
2177
2178 MagickBooleanType
2179 status;
2180
2181 MagickPixelPacket
2182 bias;
2183
2184 MagickRealType
2185 gamma;
2186
2187 register long
2188 i;
2189
2190 /*
2191 Initialize filter image attributes.
2192 */
2193 assert(image != (Image *) NULL);
2194 assert(image->signature == MagickSignature);
2195 if (image->debug != MagickFalse)
2196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2197 assert(exception != (ExceptionInfo *) NULL);
2198 assert(exception->signature == MagickSignature);
2199 if ((kernel->width % 2) == 0)
2200 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2201 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2202 if (filter_image == (Image *) NULL)
2203 return((Image *) NULL);
2204 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2205 {
2206 InheritException(exception,&filter_image->exception);
2207 filter_image=DestroyImage(filter_image);
2208 return((Image *) NULL);
2209 }
2210 if (image->debug != MagickFalse)
2211 {
2212 char
2213 format[MaxTextExtent],
2214 *message;
2215
2216 long
2217 u,
2218 v;
2219
2220 register const double
2221 *k;
2222
2223 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2224 " FilterImage with %ldx%ld kernel:",kernel->width,kernel->height);
2225 message=AcquireString("");
2226 k=kernel->values;
2227 for (v=0; v < (long) kernel->height; v++)
2228 {
2229 *message='\0';
2230 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
2231 (void) ConcatenateString(&message,format);
2232 for (u=0; u < (long) kernel->width; u++)
2233 {
cristy8cd5b312010-01-07 01:10:24 +00002234 (void) FormatMagickString(format,MaxTextExtent,"%.15g ",*k++);
cristy56a9e512010-01-06 18:18:55 +00002235 (void) ConcatenateString(&message,format);
2236 }
2237 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2238 }
2239 message=DestroyString(message);
2240 }
2241 /*
2242 Normalize kernel.
2243 */
2244 normal_kernel=(double *) AcquireQuantumMemory(kernel->width*kernel->height,
2245 sizeof(*normal_kernel));
2246 if (normal_kernel == (double *) NULL)
2247 {
2248 filter_image=DestroyImage(filter_image);
2249 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2250 }
2251 gamma=0.0;
2252 for (i=0; i < (long) (kernel->width*kernel->height); i++)
2253 gamma+=kernel->values[i];
2254 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2255 for (i=0; i < (long) (kernel->width*kernel->height); i++)
2256 normal_kernel[i]=gamma*kernel->values[i];
2257 /*
2258 Filter image.
2259 */
2260 status=MagickTrue;
2261 progress=0;
2262 GetMagickPixelPacket(image,&bias);
2263 SetMagickPixelPacketBias(image,&bias);
2264 image_view=AcquireCacheView(image);
2265 filter_view=AcquireCacheView(filter_image);
2266#if defined(MAGICKCORE_OPENMP_SUPPORT)
2267 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2268#endif
2269 for (y=0; y < (long) image->rows; y++)
2270 {
2271 MagickBooleanType
2272 sync;
2273
2274 register const IndexPacket
2275 *restrict indexes;
2276
2277 register const PixelPacket
2278 *restrict p;
2279
2280 register IndexPacket
2281 *restrict filter_indexes;
2282
2283 register long
2284 x;
2285
2286 register PixelPacket
2287 *restrict q;
2288
2289 if (status == MagickFalse)
2290 continue;
2291 p=GetCacheViewVirtualPixels(image_view,-((long) kernel->width/2L),
2292 y-(long) (kernel->height/2L),image->columns+kernel->width,kernel->height,
2293 exception);
2294 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2295 exception);
2296 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2297 {
2298 status=MagickFalse;
2299 continue;
2300 }
2301 indexes=GetCacheViewVirtualIndexQueue(image_view);
2302 filter_indexes=GetCacheViewAuthenticIndexQueue(filter_view);
2303 for (x=0; x < (long) image->columns; x++)
2304 {
2305 long
2306 v;
2307
2308 MagickPixelPacket
2309 pixel;
2310
2311 register const double
2312 *restrict k;
2313
2314 register const PixelPacket
2315 *restrict kernel_pixels;
2316
2317 register long
2318 u;
2319
2320 pixel=bias;
2321 k=normal_kernel;
2322 kernel_pixels=p;
2323 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2324 {
2325 for (v=0; v < (long) kernel->width; v++)
2326 {
2327 for (u=0; u < (long) kernel->height; u++)
2328 {
2329 pixel.red+=(*k)*kernel_pixels[u].red;
2330 pixel.green+=(*k)*kernel_pixels[u].green;
2331 pixel.blue+=(*k)*kernel_pixels[u].blue;
2332 k++;
2333 }
2334 kernel_pixels+=image->columns+kernel->width;
2335 }
2336 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002337 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002338 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002339 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002340 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002341 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002342 if ((channel & OpacityChannel) != 0)
2343 {
2344 k=normal_kernel;
2345 kernel_pixels=p;
2346 for (v=0; v < (long) kernel->width; v++)
2347 {
2348 for (u=0; u < (long) kernel->height; u++)
2349 {
2350 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2351 k++;
2352 }
2353 kernel_pixels+=image->columns+kernel->width;
2354 }
cristyce70c172010-01-07 17:15:30 +00002355 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002356 }
2357 if (((channel & IndexChannel) != 0) &&
2358 (image->colorspace == CMYKColorspace))
2359 {
2360 register const IndexPacket
2361 *restrict kernel_indexes;
2362
2363 k=normal_kernel;
2364 kernel_indexes=indexes;
2365 for (v=0; v < (long) kernel->width; v++)
2366 {
2367 for (u=0; u < (long) kernel->height; u++)
2368 {
2369 pixel.index+=(*k)*kernel_indexes[u];
2370 k++;
2371 }
2372 kernel_indexes+=image->columns+kernel->width;
2373 }
cristyce70c172010-01-07 17:15:30 +00002374 filter_indexes[x]=ClampToQuantum(pixel.index);
cristy56a9e512010-01-06 18:18:55 +00002375 }
2376 }
2377 else
2378 {
2379 MagickRealType
2380 alpha,
2381 gamma;
2382
2383 gamma=0.0;
2384 for (v=0; v < (long) kernel->width; v++)
2385 {
2386 for (u=0; u < (long) kernel->height; u++)
2387 {
2388 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2389 kernel_pixels[u].opacity));
2390 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
2391 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
2392 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
2393 gamma+=(*k)*alpha;
2394 k++;
2395 }
2396 kernel_pixels+=image->columns+kernel->width;
2397 }
2398 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2399 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002400 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002401 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002402 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002403 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00002404 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002405 if ((channel & OpacityChannel) != 0)
2406 {
2407 k=normal_kernel;
2408 kernel_pixels=p;
2409 for (v=0; v < (long) kernel->width; v++)
2410 {
2411 for (u=0; u < (long) kernel->height; u++)
2412 {
2413 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2414 k++;
2415 }
2416 kernel_pixels+=image->columns+kernel->width;
2417 }
cristyce70c172010-01-07 17:15:30 +00002418 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002419 }
2420 if (((channel & IndexChannel) != 0) &&
2421 (image->colorspace == CMYKColorspace))
2422 {
2423 register const IndexPacket
2424 *restrict kernel_indexes;
2425
2426 k=normal_kernel;
2427 kernel_pixels=p;
2428 kernel_indexes=indexes;
2429 for (v=0; v < (long) kernel->width; v++)
2430 {
2431 for (u=0; u < (long) kernel->height; u++)
2432 {
2433 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2434 kernel_pixels[u].opacity));
2435 pixel.index+=(*k)*alpha*kernel_indexes[u];
2436 k++;
2437 }
2438 kernel_pixels+=image->columns+kernel->width;
2439 kernel_indexes+=image->columns+kernel->width;
2440 }
cristy2115aea2010-01-09 23:16:08 +00002441 filter_indexes[x]=ClampToQuantum(gamma*
2442 GetIndexPixelComponent(&pixel));
cristy56a9e512010-01-06 18:18:55 +00002443 }
2444 }
2445 p++;
2446 q++;
2447 }
2448 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2449 if (sync == MagickFalse)
2450 status=MagickFalse;
2451 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2452 {
2453 MagickBooleanType
2454 proceed;
2455
2456#if defined(MAGICKCORE_OPENMP_SUPPORT)
2457 #pragma omp critical (MagickCore_FilterImageChannel)
2458#endif
2459 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2460 if (proceed == MagickFalse)
2461 status=MagickFalse;
2462 }
2463 }
2464 filter_image->type=image->type;
2465 filter_view=DestroyCacheView(filter_view);
2466 image_view=DestroyCacheView(image_view);
2467 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
2468 if (status == MagickFalse)
2469 filter_image=DestroyImage(filter_image);
2470 return(filter_image);
2471}
2472
2473/*
2474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2475% %
2476% %
2477% %
cristy3ed852e2009-09-05 21:47:34 +00002478% G a u s s i a n B l u r I m a g e %
2479% %
2480% %
2481% %
2482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2483%
2484% GaussianBlurImage() blurs an image. We convolve the image with a
2485% Gaussian operator of the given radius and standard deviation (sigma).
2486% For reasonable results, the radius should be larger than sigma. Use a
2487% radius of 0 and GaussianBlurImage() selects a suitable radius for you
2488%
2489% The format of the GaussianBlurImage method is:
2490%
2491% Image *GaussianBlurImage(const Image *image,onst double radius,
2492% const double sigma,ExceptionInfo *exception)
2493% Image *GaussianBlurImageChannel(const Image *image,
2494% const ChannelType channel,const double radius,const double sigma,
2495% ExceptionInfo *exception)
2496%
2497% A description of each parameter follows:
2498%
2499% o image: the image.
2500%
2501% o channel: the channel type.
2502%
2503% o radius: the radius of the Gaussian, in pixels, not counting the center
2504% pixel.
2505%
2506% o sigma: the standard deviation of the Gaussian, in pixels.
2507%
2508% o exception: return any errors or warnings in this structure.
2509%
2510*/
2511
2512MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
2513 const double sigma,ExceptionInfo *exception)
2514{
2515 Image
2516 *blur_image;
2517
2518 blur_image=GaussianBlurImageChannel(image,DefaultChannels,radius,sigma,
2519 exception);
2520 return(blur_image);
2521}
2522
2523MagickExport Image *GaussianBlurImageChannel(const Image *image,
2524 const ChannelType channel,const double radius,const double sigma,
2525 ExceptionInfo *exception)
2526{
2527 double
2528 *kernel;
2529
2530 Image
2531 *blur_image;
2532
cristy47e00502009-12-17 19:19:57 +00002533 long
2534 j,
cristy3ed852e2009-09-05 21:47:34 +00002535 u,
2536 v;
2537
cristy47e00502009-12-17 19:19:57 +00002538 register long
2539 i;
2540
cristy3ed852e2009-09-05 21:47:34 +00002541 unsigned long
2542 width;
2543
2544 assert(image != (const Image *) NULL);
2545 assert(image->signature == MagickSignature);
2546 if (image->debug != MagickFalse)
2547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2548 assert(exception != (ExceptionInfo *) NULL);
2549 assert(exception->signature == MagickSignature);
2550 width=GetOptimalKernelWidth2D(radius,sigma);
2551 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2552 if (kernel == (double *) NULL)
2553 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy47e00502009-12-17 19:19:57 +00002554 j=(long) width/2;
cristy3ed852e2009-09-05 21:47:34 +00002555 i=0;
cristy47e00502009-12-17 19:19:57 +00002556 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00002557 {
cristy47e00502009-12-17 19:19:57 +00002558 for (u=(-j); u <= j; u++)
2559 kernel[i++]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
2560 (2.0*MagickPI*MagickSigma*MagickSigma);
cristy3ed852e2009-09-05 21:47:34 +00002561 }
2562 blur_image=ConvolveImageChannel(image,channel,width,kernel,exception);
2563 kernel=(double *) RelinquishMagickMemory(kernel);
2564 return(blur_image);
2565}
2566
2567/*
2568%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2569% %
2570% %
2571% %
2572% M e d i a n F i l t e r I m a g e %
2573% %
2574% %
2575% %
2576%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2577%
2578% MedianFilterImage() applies a digital filter that improves the quality
2579% of a noisy image. Each pixel is replaced by the median in a set of
2580% neighboring pixels as defined by radius.
2581%
2582% The algorithm was contributed by Mike Edmonds and implements an insertion
2583% sort for selecting median color-channel values. For more on this algorithm
2584% see "Skip Lists: A probabilistic Alternative to Balanced Trees" by William
2585% Pugh in the June 1990 of Communications of the ACM.
2586%
2587% The format of the MedianFilterImage method is:
2588%
2589% Image *MedianFilterImage(const Image *image,const double radius,
2590% ExceptionInfo *exception)
2591%
2592% A description of each parameter follows:
2593%
2594% o image: the image.
2595%
2596% o radius: the radius of the pixel neighborhood.
2597%
2598% o exception: return any errors or warnings in this structure.
2599%
2600*/
2601
2602#define MedianListChannels 5
2603
2604typedef struct _MedianListNode
2605{
2606 unsigned long
2607 next[9],
2608 count,
2609 signature;
2610} MedianListNode;
2611
2612typedef struct _MedianSkipList
2613{
2614 long
2615 level;
2616
2617 MedianListNode
2618 *nodes;
2619} MedianSkipList;
2620
2621typedef struct _MedianPixelList
2622{
2623 unsigned long
2624 center,
2625 seed,
2626 signature;
2627
2628 MedianSkipList
2629 lists[MedianListChannels];
2630} MedianPixelList;
2631
2632static MedianPixelList *DestroyMedianPixelList(MedianPixelList *pixel_list)
2633{
2634 register long
2635 i;
2636
2637 if (pixel_list == (MedianPixelList *) NULL)
2638 return((MedianPixelList *) NULL);
2639 for (i=0; i < MedianListChannels; i++)
2640 if (pixel_list->lists[i].nodes != (MedianListNode *) NULL)
2641 pixel_list->lists[i].nodes=(MedianListNode *) RelinquishMagickMemory(
2642 pixel_list->lists[i].nodes);
2643 pixel_list=(MedianPixelList *) RelinquishAlignedMemory(pixel_list);
2644 return(pixel_list);
2645}
2646
2647static MedianPixelList **DestroyMedianPixelListThreadSet(
2648 MedianPixelList **pixel_list)
2649{
2650 register long
2651 i;
2652
2653 assert(pixel_list != (MedianPixelList **) NULL);
2654 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
2655 if (pixel_list[i] != (MedianPixelList *) NULL)
2656 pixel_list[i]=DestroyMedianPixelList(pixel_list[i]);
2657 pixel_list=(MedianPixelList **) RelinquishAlignedMemory(pixel_list);
2658 return(pixel_list);
2659}
2660
2661static MedianPixelList *AcquireMedianPixelList(const unsigned long width)
2662{
2663 MedianPixelList
2664 *pixel_list;
2665
2666 register long
2667 i;
2668
2669 pixel_list=(MedianPixelList *) AcquireAlignedMemory(1,sizeof(*pixel_list));
2670 if (pixel_list == (MedianPixelList *) NULL)
2671 return(pixel_list);
2672 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2673 pixel_list->center=width*width/2;
2674 for (i=0; i < MedianListChannels; i++)
2675 {
2676 pixel_list->lists[i].nodes=(MedianListNode *) AcquireQuantumMemory(65537UL,
2677 sizeof(*pixel_list->lists[i].nodes));
2678 if (pixel_list->lists[i].nodes == (MedianListNode *) NULL)
2679 return(DestroyMedianPixelList(pixel_list));
2680 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
2681 sizeof(*pixel_list->lists[i].nodes));
2682 }
2683 pixel_list->signature=MagickSignature;
2684 return(pixel_list);
2685}
2686
2687static MedianPixelList **AcquireMedianPixelListThreadSet(
2688 const unsigned long width)
2689{
2690 register long
2691 i;
2692
2693 MedianPixelList
2694 **pixel_list;
2695
2696 unsigned long
2697 number_threads;
2698
2699 number_threads=GetOpenMPMaximumThreads();
2700 pixel_list=(MedianPixelList **) AcquireAlignedMemory(number_threads,
2701 sizeof(*pixel_list));
2702 if (pixel_list == (MedianPixelList **) NULL)
2703 return((MedianPixelList **) NULL);
2704 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
2705 for (i=0; i < (long) number_threads; i++)
2706 {
2707 pixel_list[i]=AcquireMedianPixelList(width);
2708 if (pixel_list[i] == (MedianPixelList *) NULL)
2709 return(DestroyMedianPixelListThreadSet(pixel_list));
2710 }
2711 return(pixel_list);
2712}
2713
2714static void AddNodeMedianPixelList(MedianPixelList *pixel_list,
2715 const long channel,const unsigned long color)
2716{
2717 register long
2718 level;
2719
2720 register MedianSkipList
2721 *list;
2722
2723 unsigned long
2724 search,
2725 update[9];
2726
2727 /*
2728 Initialize the node.
2729 */
2730 list=pixel_list->lists+channel;
2731 list->nodes[color].signature=pixel_list->signature;
2732 list->nodes[color].count=1;
2733 /*
2734 Determine where it belongs in the list.
2735 */
2736 search=65536UL;
2737 for (level=list->level; level >= 0; level--)
2738 {
2739 while (list->nodes[search].next[level] < color)
2740 search=list->nodes[search].next[level];
2741 update[level]=search;
2742 }
2743 /*
2744 Generate a pseudo-random level for this node.
2745 */
2746 for (level=0; ; level++)
2747 {
2748 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2749 if ((pixel_list->seed & 0x300) != 0x300)
2750 break;
2751 }
2752 if (level > 8)
2753 level=8;
2754 if (level > (list->level+2))
2755 level=list->level+2;
2756 /*
2757 If we're raising the list's level, link back to the root node.
2758 */
2759 while (level > list->level)
2760 {
2761 list->level++;
2762 update[list->level]=65536UL;
2763 }
2764 /*
2765 Link the node into the skip-list.
2766 */
2767 do
2768 {
2769 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
2770 list->nodes[update[level]].next[level]=color;
2771 }
2772 while (level-- > 0);
2773}
2774
2775static MagickPixelPacket GetMedianPixelList(MedianPixelList *pixel_list)
2776{
2777 MagickPixelPacket
2778 pixel;
2779
2780 register long
2781 channel;
2782
2783 register MedianSkipList
2784 *list;
2785
2786 unsigned long
2787 center,
2788 color,
2789 count;
2790
2791 unsigned short
2792 channels[MedianListChannels];
2793
2794 /*
2795 Find the median value for each of the color.
2796 */
2797 center=pixel_list->center;
2798 for (channel=0; channel < 5; channel++)
2799 {
2800 list=pixel_list->lists+channel;
2801 color=65536UL;
2802 count=0;
2803 do
2804 {
2805 color=list->nodes[color].next[0];
2806 count+=list->nodes[color].count;
2807 }
2808 while (count <= center);
2809 channels[channel]=(unsigned short) color;
2810 }
2811 GetMagickPixelPacket((const Image *) NULL,&pixel);
2812 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
2813 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
2814 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
2815 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
2816 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
2817 return(pixel);
2818}
2819
2820static inline void InsertMedianPixelList(const Image *image,
2821 const PixelPacket *pixel,const IndexPacket *indexes,
2822 MedianPixelList *pixel_list)
2823{
2824 unsigned long
2825 signature;
2826
2827 unsigned short
2828 index;
2829
2830 index=ScaleQuantumToShort(pixel->red);
2831 signature=pixel_list->lists[0].nodes[index].signature;
2832 if (signature == pixel_list->signature)
2833 pixel_list->lists[0].nodes[index].count++;
2834 else
2835 AddNodeMedianPixelList(pixel_list,0,index);
2836 index=ScaleQuantumToShort(pixel->green);
2837 signature=pixel_list->lists[1].nodes[index].signature;
2838 if (signature == pixel_list->signature)
2839 pixel_list->lists[1].nodes[index].count++;
2840 else
2841 AddNodeMedianPixelList(pixel_list,1,index);
2842 index=ScaleQuantumToShort(pixel->blue);
2843 signature=pixel_list->lists[2].nodes[index].signature;
2844 if (signature == pixel_list->signature)
2845 pixel_list->lists[2].nodes[index].count++;
2846 else
2847 AddNodeMedianPixelList(pixel_list,2,index);
2848 index=ScaleQuantumToShort(pixel->opacity);
2849 signature=pixel_list->lists[3].nodes[index].signature;
2850 if (signature == pixel_list->signature)
2851 pixel_list->lists[3].nodes[index].count++;
2852 else
2853 AddNodeMedianPixelList(pixel_list,3,index);
2854 if (image->colorspace == CMYKColorspace)
2855 index=ScaleQuantumToShort(*indexes);
2856 signature=pixel_list->lists[4].nodes[index].signature;
2857 if (signature == pixel_list->signature)
2858 pixel_list->lists[4].nodes[index].count++;
2859 else
2860 AddNodeMedianPixelList(pixel_list,4,index);
2861}
2862
2863static void ResetMedianPixelList(MedianPixelList *pixel_list)
2864{
2865 int
2866 level;
2867
2868 register long
2869 channel;
2870
2871 register MedianListNode
2872 *root;
2873
2874 register MedianSkipList
2875 *list;
2876
2877 /*
2878 Reset the skip-list.
2879 */
2880 for (channel=0; channel < 5; channel++)
2881 {
2882 list=pixel_list->lists+channel;
2883 root=list->nodes+65536UL;
2884 list->level=0;
2885 for (level=0; level < 9; level++)
2886 root->next[level]=65536UL;
2887 }
2888 pixel_list->seed=pixel_list->signature++;
2889}
2890
2891MagickExport Image *MedianFilterImage(const Image *image,const double radius,
2892 ExceptionInfo *exception)
2893{
2894#define MedianFilterImageTag "MedianFilter/Image"
2895
cristyc4c8d132010-01-07 01:58:38 +00002896 CacheView
2897 *image_view,
2898 *median_view;
2899
cristy3ed852e2009-09-05 21:47:34 +00002900 Image
2901 *median_image;
2902
2903 long
2904 progress,
2905 y;
2906
2907 MagickBooleanType
2908 status;
2909
2910 MedianPixelList
cristyfa112112010-01-04 17:48:07 +00002911 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00002912
2913 unsigned long
2914 width;
2915
cristy3ed852e2009-09-05 21:47:34 +00002916 /*
2917 Initialize median image attributes.
2918 */
2919 assert(image != (Image *) NULL);
2920 assert(image->signature == MagickSignature);
2921 if (image->debug != MagickFalse)
2922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2923 assert(exception != (ExceptionInfo *) NULL);
2924 assert(exception->signature == MagickSignature);
2925 width=GetOptimalKernelWidth2D(radius,0.5);
2926 if ((image->columns < width) || (image->rows < width))
2927 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
2928 median_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2929 exception);
2930 if (median_image == (Image *) NULL)
2931 return((Image *) NULL);
2932 if (SetImageStorageClass(median_image,DirectClass) == MagickFalse)
2933 {
2934 InheritException(exception,&median_image->exception);
2935 median_image=DestroyImage(median_image);
2936 return((Image *) NULL);
2937 }
2938 pixel_list=AcquireMedianPixelListThreadSet(width);
2939 if (pixel_list == (MedianPixelList **) NULL)
2940 {
2941 median_image=DestroyImage(median_image);
2942 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2943 }
2944 /*
2945 Median filter each image row.
2946 */
2947 status=MagickTrue;
2948 progress=0;
2949 image_view=AcquireCacheView(image);
2950 median_view=AcquireCacheView(median_image);
cristyb5d5f722009-11-04 03:03:49 +00002951#if defined(MAGICKCORE_OPENMP_SUPPORT)
2952 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002953#endif
2954 for (y=0; y < (long) median_image->rows; y++)
2955 {
2956 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002957 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00002958
2959 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002960 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002961
2962 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002963 *restrict median_indexes;
cristy3ed852e2009-09-05 21:47:34 +00002964
2965 register long
2966 id,
2967 x;
2968
2969 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002970 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002971
2972 if (status == MagickFalse)
2973 continue;
2974 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
2975 2L),image->columns+width,width,exception);
2976 q=QueueCacheViewAuthenticPixels(median_view,0,y,median_image->columns,1,
2977 exception);
2978 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2979 {
2980 status=MagickFalse;
2981 continue;
2982 }
2983 indexes=GetCacheViewVirtualIndexQueue(image_view);
2984 median_indexes=GetCacheViewAuthenticIndexQueue(median_view);
2985 id=GetOpenMPThreadId();
2986 for (x=0; x < (long) median_image->columns; x++)
2987 {
2988 MagickPixelPacket
2989 pixel;
2990
2991 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002992 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00002993
2994 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00002995 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00002996
2997 register long
2998 u,
2999 v;
3000
3001 r=p;
3002 s=indexes+x;
3003 ResetMedianPixelList(pixel_list[id]);
3004 for (v=0; v < (long) width; v++)
3005 {
3006 for (u=0; u < (long) width; u++)
3007 InsertMedianPixelList(image,r+u,s+u,pixel_list[id]);
3008 r+=image->columns+width;
3009 s+=image->columns+width;
3010 }
3011 pixel=GetMedianPixelList(pixel_list[id]);
3012 SetPixelPacket(median_image,&pixel,q,median_indexes+x);
3013 p++;
3014 q++;
3015 }
3016 if (SyncCacheViewAuthenticPixels(median_view,exception) == MagickFalse)
3017 status=MagickFalse;
3018 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3019 {
3020 MagickBooleanType
3021 proceed;
3022
cristyb5d5f722009-11-04 03:03:49 +00003023#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003024 #pragma omp critical (MagickCore_MedianFilterImage)
3025#endif
3026 proceed=SetImageProgress(image,MedianFilterImageTag,progress++,
3027 image->rows);
3028 if (proceed == MagickFalse)
3029 status=MagickFalse;
3030 }
3031 }
3032 median_view=DestroyCacheView(median_view);
3033 image_view=DestroyCacheView(image_view);
3034 pixel_list=DestroyMedianPixelListThreadSet(pixel_list);
3035 return(median_image);
3036}
3037
3038/*
3039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3040% %
3041% %
3042% %
3043% M o t i o n B l u r I m a g e %
3044% %
3045% %
3046% %
3047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3048%
3049% MotionBlurImage() simulates motion blur. We convolve the image with a
3050% Gaussian operator of the given radius and standard deviation (sigma).
3051% For reasonable results, radius should be larger than sigma. Use a
3052% radius of 0 and MotionBlurImage() selects a suitable radius for you.
3053% Angle gives the angle of the blurring motion.
3054%
3055% Andrew Protano contributed this effect.
3056%
3057% The format of the MotionBlurImage method is:
3058%
3059% Image *MotionBlurImage(const Image *image,const double radius,
3060% const double sigma,const double angle,ExceptionInfo *exception)
3061% Image *MotionBlurImageChannel(const Image *image,const ChannelType channel,
3062% const double radius,const double sigma,const double angle,
3063% ExceptionInfo *exception)
3064%
3065% A description of each parameter follows:
3066%
3067% o image: the image.
3068%
3069% o channel: the channel type.
3070%
3071% o radius: the radius of the Gaussian, in pixels, not counting the center
3072% o radius: the radius of the Gaussian, in pixels, not counting
3073% the center pixel.
3074%
3075% o sigma: the standard deviation of the Gaussian, in pixels.
3076%
3077% o angle: Apply the effect along this angle.
3078%
3079% o exception: return any errors or warnings in this structure.
3080%
3081*/
3082
cristy47e00502009-12-17 19:19:57 +00003083static double *GetMotionBlurKernel(const unsigned long width,const double sigma)
cristy3ed852e2009-09-05 21:47:34 +00003084{
cristy3ed852e2009-09-05 21:47:34 +00003085 double
cristy47e00502009-12-17 19:19:57 +00003086 *kernel,
cristy3ed852e2009-09-05 21:47:34 +00003087 normalize;
3088
3089 register long
3090 i;
3091
3092 /*
cristy47e00502009-12-17 19:19:57 +00003093 Generate a 1-D convolution kernel.
cristy3ed852e2009-09-05 21:47:34 +00003094 */
3095 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3096 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
3097 if (kernel == (double *) NULL)
3098 return(kernel);
cristy3ed852e2009-09-05 21:47:34 +00003099 normalize=0.0;
3100 for (i=0; i < (long) width; i++)
cristy47e00502009-12-17 19:19:57 +00003101 {
3102 kernel[i]=exp((-((double) i*i)/(double) (2.0*MagickSigma*MagickSigma)))/
3103 (MagickSQ2PI*MagickSigma);
cristy3ed852e2009-09-05 21:47:34 +00003104 normalize+=kernel[i];
cristy47e00502009-12-17 19:19:57 +00003105 }
cristy3ed852e2009-09-05 21:47:34 +00003106 for (i=0; i < (long) width; i++)
3107 kernel[i]/=normalize;
3108 return(kernel);
3109}
3110
3111MagickExport Image *MotionBlurImage(const Image *image,const double radius,
3112 const double sigma,const double angle,ExceptionInfo *exception)
3113{
3114 Image
3115 *motion_blur;
3116
3117 motion_blur=MotionBlurImageChannel(image,DefaultChannels,radius,sigma,angle,
3118 exception);
3119 return(motion_blur);
3120}
3121
3122MagickExport Image *MotionBlurImageChannel(const Image *image,
3123 const ChannelType channel,const double radius,const double sigma,
3124 const double angle,ExceptionInfo *exception)
3125{
3126 typedef struct _OffsetInfo
3127 {
3128 long
3129 x,
3130 y;
3131 } OffsetInfo;
3132
cristyc4c8d132010-01-07 01:58:38 +00003133 CacheView
3134 *blur_view,
3135 *image_view;
3136
cristy3ed852e2009-09-05 21:47:34 +00003137 double
3138 *kernel;
3139
3140 Image
3141 *blur_image;
3142
3143 long
3144 progress,
3145 y;
3146
3147 MagickBooleanType
3148 status;
3149
3150 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00003151 bias;
cristy3ed852e2009-09-05 21:47:34 +00003152
3153 OffsetInfo
3154 *offset;
3155
3156 PointInfo
3157 point;
3158
3159 register long
3160 i;
3161
3162 unsigned long
3163 width;
3164
cristy3ed852e2009-09-05 21:47:34 +00003165 assert(image != (Image *) NULL);
3166 assert(image->signature == MagickSignature);
3167 if (image->debug != MagickFalse)
3168 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3169 assert(exception != (ExceptionInfo *) NULL);
3170 width=GetOptimalKernelWidth1D(radius,sigma);
3171 kernel=GetMotionBlurKernel(width,sigma);
3172 if (kernel == (double *) NULL)
3173 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3174 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
3175 if (offset == (OffsetInfo *) NULL)
3176 {
3177 kernel=(double *) RelinquishMagickMemory(kernel);
3178 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3179 }
3180 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3181 if (blur_image == (Image *) NULL)
3182 {
3183 kernel=(double *) RelinquishMagickMemory(kernel);
3184 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3185 return((Image *) NULL);
3186 }
3187 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3188 {
3189 kernel=(double *) RelinquishMagickMemory(kernel);
3190 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3191 InheritException(exception,&blur_image->exception);
3192 blur_image=DestroyImage(blur_image);
3193 return((Image *) NULL);
3194 }
3195 point.x=(double) width*sin(DegreesToRadians(angle));
3196 point.y=(double) width*cos(DegreesToRadians(angle));
3197 for (i=0; i < (long) width; i++)
3198 {
3199 offset[i].x=(long) ((i*point.y)/hypot(point.x,point.y)+0.5);
3200 offset[i].y=(long) ((i*point.x)/hypot(point.x,point.y)+0.5);
3201 }
3202 /*
3203 Motion blur image.
3204 */
3205 status=MagickTrue;
3206 progress=0;
cristyddd82202009-11-03 20:14:50 +00003207 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003208 image_view=AcquireCacheView(image);
3209 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003210#if defined(MAGICKCORE_OPENMP_SUPPORT)
3211 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003212#endif
3213 for (y=0; y < (long) image->rows; y++)
3214 {
3215 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003216 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00003217
3218 register long
3219 x;
3220
3221 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003222 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003223
3224 if (status == MagickFalse)
3225 continue;
3226 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3227 exception);
3228 if (q == (PixelPacket *) NULL)
3229 {
3230 status=MagickFalse;
3231 continue;
3232 }
3233 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
3234 for (x=0; x < (long) image->columns; x++)
3235 {
3236 MagickPixelPacket
3237 qixel;
3238
3239 PixelPacket
3240 pixel;
3241
3242 register double
cristyc47d1f82009-11-26 01:44:43 +00003243 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00003244
3245 register long
3246 i;
3247
3248 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003249 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00003250
3251 k=kernel;
cristyddd82202009-11-03 20:14:50 +00003252 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00003253 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3254 {
3255 for (i=0; i < (long) width; i++)
3256 {
3257 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3258 offset[i].y,&pixel,exception);
3259 qixel.red+=(*k)*pixel.red;
3260 qixel.green+=(*k)*pixel.green;
3261 qixel.blue+=(*k)*pixel.blue;
3262 qixel.opacity+=(*k)*pixel.opacity;
3263 if (image->colorspace == CMYKColorspace)
3264 {
3265 indexes=GetCacheViewVirtualIndexQueue(image_view);
3266 qixel.index+=(*k)*(*indexes);
3267 }
3268 k++;
3269 }
3270 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003271 q->red=ClampToQuantum(qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003272 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003273 q->green=ClampToQuantum(qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003274 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003275 q->blue=ClampToQuantum(qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003276 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003277 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003278 if (((channel & IndexChannel) != 0) &&
3279 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003280 blur_indexes[x]=(IndexPacket) ClampToQuantum(qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003281 }
3282 else
3283 {
3284 MagickRealType
3285 alpha,
3286 gamma;
3287
3288 alpha=0.0;
3289 gamma=0.0;
3290 for (i=0; i < (long) width; i++)
3291 {
3292 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3293 offset[i].y,&pixel,exception);
3294 alpha=(MagickRealType) (QuantumScale*(QuantumRange-pixel.opacity));
3295 qixel.red+=(*k)*alpha*pixel.red;
3296 qixel.green+=(*k)*alpha*pixel.green;
3297 qixel.blue+=(*k)*alpha*pixel.blue;
3298 qixel.opacity+=(*k)*pixel.opacity;
3299 if (image->colorspace == CMYKColorspace)
3300 {
3301 indexes=GetCacheViewVirtualIndexQueue(image_view);
3302 qixel.index+=(*k)*alpha*(*indexes);
3303 }
3304 gamma+=(*k)*alpha;
3305 k++;
3306 }
3307 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3308 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003309 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00003310 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003311 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00003312 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003313 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00003314 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00003315 q->opacity=ClampToQuantum(qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003316 if (((channel & IndexChannel) != 0) &&
3317 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00003318 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00003319 }
3320 q++;
3321 }
3322 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3323 status=MagickFalse;
3324 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3325 {
3326 MagickBooleanType
3327 proceed;
3328
cristyb5d5f722009-11-04 03:03:49 +00003329#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003330 #pragma omp critical (MagickCore_MotionBlurImageChannel)
3331#endif
3332 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3333 if (proceed == MagickFalse)
3334 status=MagickFalse;
3335 }
3336 }
3337 blur_view=DestroyCacheView(blur_view);
3338 image_view=DestroyCacheView(image_view);
3339 kernel=(double *) RelinquishMagickMemory(kernel);
3340 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3341 if (status == MagickFalse)
3342 blur_image=DestroyImage(blur_image);
3343 return(blur_image);
3344}
3345
3346/*
3347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3348% %
3349% %
3350% %
3351% P r e v i e w I m a g e %
3352% %
3353% %
3354% %
3355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3356%
3357% PreviewImage() tiles 9 thumbnails of the specified image with an image
3358% processing operation applied with varying parameters. This may be helpful
3359% pin-pointing an appropriate parameter for a particular image processing
3360% operation.
3361%
3362% The format of the PreviewImages method is:
3363%
3364% Image *PreviewImages(const Image *image,const PreviewType preview,
3365% ExceptionInfo *exception)
3366%
3367% A description of each parameter follows:
3368%
3369% o image: the image.
3370%
3371% o preview: the image processing operation.
3372%
3373% o exception: return any errors or warnings in this structure.
3374%
3375*/
3376MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
3377 ExceptionInfo *exception)
3378{
3379#define NumberTiles 9
3380#define PreviewImageTag "Preview/Image"
3381#define DefaultPreviewGeometry "204x204+10+10"
3382
3383 char
3384 factor[MaxTextExtent],
3385 label[MaxTextExtent];
3386
3387 double
3388 degrees,
3389 gamma,
3390 percentage,
3391 radius,
3392 sigma,
3393 threshold;
3394
3395 Image
3396 *images,
3397 *montage_image,
3398 *preview_image,
3399 *thumbnail;
3400
3401 ImageInfo
3402 *preview_info;
3403
3404 long
3405 y;
3406
3407 MagickBooleanType
3408 proceed;
3409
3410 MontageInfo
3411 *montage_info;
3412
3413 QuantizeInfo
3414 quantize_info;
3415
3416 RectangleInfo
3417 geometry;
3418
3419 register long
3420 i,
3421 x;
3422
3423 unsigned long
3424 colors;
3425
3426 /*
3427 Open output image file.
3428 */
3429 assert(image != (Image *) NULL);
3430 assert(image->signature == MagickSignature);
3431 if (image->debug != MagickFalse)
3432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3433 colors=2;
3434 degrees=0.0;
3435 gamma=(-0.2f);
3436 preview_info=AcquireImageInfo();
3437 SetGeometry(image,&geometry);
3438 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
3439 &geometry.width,&geometry.height);
3440 images=NewImageList();
3441 percentage=12.5;
3442 GetQuantizeInfo(&quantize_info);
3443 radius=0.0;
3444 sigma=1.0;
3445 threshold=0.0;
3446 x=0;
3447 y=0;
3448 for (i=0; i < NumberTiles; i++)
3449 {
3450 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
3451 if (thumbnail == (Image *) NULL)
3452 break;
3453 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
3454 (void *) NULL);
3455 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
3456 if (i == (NumberTiles/2))
3457 {
3458 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
3459 AppendImageToList(&images,thumbnail);
3460 continue;
3461 }
3462 switch (preview)
3463 {
3464 case RotatePreview:
3465 {
3466 degrees+=45.0;
3467 preview_image=RotateImage(thumbnail,degrees,exception);
cristy8cd5b312010-01-07 01:10:24 +00003468 (void) FormatMagickString(label,MaxTextExtent,"rotate %.15g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003469 break;
3470 }
3471 case ShearPreview:
3472 {
3473 degrees+=5.0;
3474 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
cristy8cd5b312010-01-07 01:10:24 +00003475 (void) FormatMagickString(label,MaxTextExtent,"shear %.15gx%.15g",
cristy3ed852e2009-09-05 21:47:34 +00003476 degrees,2.0*degrees);
3477 break;
3478 }
3479 case RollPreview:
3480 {
3481 x=(long) ((i+1)*thumbnail->columns)/NumberTiles;
3482 y=(long) ((i+1)*thumbnail->rows)/NumberTiles;
3483 preview_image=RollImage(thumbnail,x,y,exception);
3484 (void) FormatMagickString(label,MaxTextExtent,"roll %ldx%ld",x,y);
3485 break;
3486 }
3487 case HuePreview:
3488 {
3489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3490 if (preview_image == (Image *) NULL)
3491 break;
cristy8cd5b312010-01-07 01:10:24 +00003492 (void) FormatMagickString(factor,MaxTextExtent,"100,100,%.15g",
cristy3ed852e2009-09-05 21:47:34 +00003493 2.0*percentage);
3494 (void) ModulateImage(preview_image,factor);
3495 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3496 break;
3497 }
3498 case SaturationPreview:
3499 {
3500 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3501 if (preview_image == (Image *) NULL)
3502 break;
cristy8cd5b312010-01-07 01:10:24 +00003503 (void) FormatMagickString(factor,MaxTextExtent,"100,%.15g",
3504 2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003505 (void) ModulateImage(preview_image,factor);
3506 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3507 break;
3508 }
3509 case BrightnessPreview:
3510 {
3511 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3512 if (preview_image == (Image *) NULL)
3513 break;
cristy8cd5b312010-01-07 01:10:24 +00003514 (void) FormatMagickString(factor,MaxTextExtent,"%.15g",2.0*percentage);
cristy3ed852e2009-09-05 21:47:34 +00003515 (void) ModulateImage(preview_image,factor);
3516 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3517 break;
3518 }
3519 case GammaPreview:
3520 default:
3521 {
3522 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3523 if (preview_image == (Image *) NULL)
3524 break;
3525 gamma+=0.4f;
3526 (void) GammaImageChannel(preview_image,DefaultChannels,gamma);
cristy8cd5b312010-01-07 01:10:24 +00003527 (void) FormatMagickString(label,MaxTextExtent,"gamma %.15g",gamma);
cristy3ed852e2009-09-05 21:47:34 +00003528 break;
3529 }
3530 case SpiffPreview:
3531 {
3532 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3533 if (preview_image != (Image *) NULL)
3534 for (x=0; x < i; x++)
3535 (void) ContrastImage(preview_image,MagickTrue);
3536 (void) FormatMagickString(label,MaxTextExtent,"contrast (%ld)",i+1);
3537 break;
3538 }
3539 case DullPreview:
3540 {
3541 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3542 if (preview_image == (Image *) NULL)
3543 break;
3544 for (x=0; x < i; x++)
3545 (void) ContrastImage(preview_image,MagickFalse);
3546 (void) FormatMagickString(label,MaxTextExtent,"+contrast (%ld)",i+1);
3547 break;
3548 }
3549 case GrayscalePreview:
3550 {
3551 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3552 if (preview_image == (Image *) NULL)
3553 break;
3554 colors<<=1;
3555 quantize_info.number_colors=colors;
3556 quantize_info.colorspace=GRAYColorspace;
3557 (void) QuantizeImage(&quantize_info,preview_image);
3558 (void) FormatMagickString(label,MaxTextExtent,
3559 "-colorspace gray -colors %ld",colors);
3560 break;
3561 }
3562 case QuantizePreview:
3563 {
3564 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3565 if (preview_image == (Image *) NULL)
3566 break;
3567 colors<<=1;
3568 quantize_info.number_colors=colors;
3569 (void) QuantizeImage(&quantize_info,preview_image);
3570 (void) FormatMagickString(label,MaxTextExtent,"colors %ld",colors);
3571 break;
3572 }
3573 case DespecklePreview:
3574 {
3575 for (x=0; x < (i-1); x++)
3576 {
3577 preview_image=DespeckleImage(thumbnail,exception);
3578 if (preview_image == (Image *) NULL)
3579 break;
3580 thumbnail=DestroyImage(thumbnail);
3581 thumbnail=preview_image;
3582 }
3583 preview_image=DespeckleImage(thumbnail,exception);
3584 if (preview_image == (Image *) NULL)
3585 break;
3586 (void) FormatMagickString(label,MaxTextExtent,"despeckle (%ld)",i+1);
3587 break;
3588 }
3589 case ReduceNoisePreview:
3590 {
3591 preview_image=ReduceNoiseImage(thumbnail,radius,exception);
cristy8cd5b312010-01-07 01:10:24 +00003592 (void) FormatMagickString(label,MaxTextExtent,"noise %.15g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003593 break;
3594 }
3595 case AddNoisePreview:
3596 {
3597 switch ((int) i)
3598 {
3599 case 0:
3600 {
3601 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
3602 break;
3603 }
3604 case 1:
3605 {
3606 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
3607 break;
3608 }
3609 case 2:
3610 {
3611 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
3612 break;
3613 }
3614 case 3:
3615 {
3616 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
3617 break;
3618 }
3619 case 4:
3620 {
3621 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
3622 break;
3623 }
3624 case 5:
3625 {
3626 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
3627 break;
3628 }
3629 default:
3630 {
3631 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
3632 break;
3633 }
3634 }
3635 preview_image=ReduceNoiseImage(thumbnail,(double) i,exception);
3636 (void) FormatMagickString(label,MaxTextExtent,"+noise %s",factor);
3637 break;
3638 }
3639 case SharpenPreview:
3640 {
3641 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
cristy8cd5b312010-01-07 01:10:24 +00003642 (void) FormatMagickString(label,MaxTextExtent,"sharpen %.15gx%.15g",
3643 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003644 break;
3645 }
3646 case BlurPreview:
3647 {
3648 preview_image=BlurImage(thumbnail,radius,sigma,exception);
cristy8cd5b312010-01-07 01:10:24 +00003649 (void) FormatMagickString(label,MaxTextExtent,"blur %.15gx%.15g",radius,
cristy3ed852e2009-09-05 21:47:34 +00003650 sigma);
3651 break;
3652 }
3653 case ThresholdPreview:
3654 {
3655 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3656 if (preview_image == (Image *) NULL)
3657 break;
3658 (void) BilevelImage(thumbnail,
3659 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
cristy8cd5b312010-01-07 01:10:24 +00003660 (void) FormatMagickString(label,MaxTextExtent,"threshold %.15g",
cristy3ed852e2009-09-05 21:47:34 +00003661 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3662 break;
3663 }
3664 case EdgeDetectPreview:
3665 {
3666 preview_image=EdgeImage(thumbnail,radius,exception);
cristy8cd5b312010-01-07 01:10:24 +00003667 (void) FormatMagickString(label,MaxTextExtent,"edge %.15g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003668 break;
3669 }
3670 case SpreadPreview:
3671 {
3672 preview_image=SpreadImage(thumbnail,radius,exception);
cristy8cd5b312010-01-07 01:10:24 +00003673 (void) FormatMagickString(label,MaxTextExtent,"spread %.15g",
3674 radius+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003675 break;
3676 }
3677 case SolarizePreview:
3678 {
3679 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3680 if (preview_image == (Image *) NULL)
3681 break;
3682 (void) SolarizeImage(preview_image,(double) QuantumRange*
3683 percentage/100.0);
cristy8cd5b312010-01-07 01:10:24 +00003684 (void) FormatMagickString(label,MaxTextExtent,"solarize %.15g",
cristy3ed852e2009-09-05 21:47:34 +00003685 (QuantumRange*percentage)/100.0);
3686 break;
3687 }
3688 case ShadePreview:
3689 {
3690 degrees+=10.0;
3691 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
3692 exception);
cristy8cd5b312010-01-07 01:10:24 +00003693 (void) FormatMagickString(label,MaxTextExtent,"shade %.15gx%.15g",
3694 degrees,degrees);
cristy3ed852e2009-09-05 21:47:34 +00003695 break;
3696 }
3697 case RaisePreview:
3698 {
3699 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3700 if (preview_image == (Image *) NULL)
3701 break;
3702 geometry.width=(unsigned long) (2*i+2);
3703 geometry.height=(unsigned long) (2*i+2);
3704 geometry.x=i/2;
3705 geometry.y=i/2;
3706 (void) RaiseImage(preview_image,&geometry,MagickTrue);
3707 (void) FormatMagickString(label,MaxTextExtent,"raise %lux%lu%+ld%+ld",
3708 geometry.width,geometry.height,geometry.x,geometry.y);
3709 break;
3710 }
3711 case SegmentPreview:
3712 {
3713 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3714 if (preview_image == (Image *) NULL)
3715 break;
3716 threshold+=0.4f;
3717 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
3718 threshold);
cristy8cd5b312010-01-07 01:10:24 +00003719 (void) FormatMagickString(label,MaxTextExtent,"segment %.15gx%.15g",
cristy3ed852e2009-09-05 21:47:34 +00003720 threshold,threshold);
3721 break;
3722 }
3723 case SwirlPreview:
3724 {
3725 preview_image=SwirlImage(thumbnail,degrees,exception);
cristy8cd5b312010-01-07 01:10:24 +00003726 (void) FormatMagickString(label,MaxTextExtent,"swirl %.15g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003727 degrees+=45.0;
3728 break;
3729 }
3730 case ImplodePreview:
3731 {
3732 degrees+=0.1f;
3733 preview_image=ImplodeImage(thumbnail,degrees,exception);
cristy8cd5b312010-01-07 01:10:24 +00003734 (void) FormatMagickString(label,MaxTextExtent,"implode %.15g",degrees);
cristy3ed852e2009-09-05 21:47:34 +00003735 break;
3736 }
3737 case WavePreview:
3738 {
3739 degrees+=5.0f;
3740 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
cristy8cd5b312010-01-07 01:10:24 +00003741 (void) FormatMagickString(label,MaxTextExtent,"wave %.15gx%.15g",
3742 0.5*degrees,2.0*degrees);
cristy3ed852e2009-09-05 21:47:34 +00003743 break;
3744 }
3745 case OilPaintPreview:
3746 {
3747 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
cristy8cd5b312010-01-07 01:10:24 +00003748 (void) FormatMagickString(label,MaxTextExtent,"paint %.15g",radius);
cristy3ed852e2009-09-05 21:47:34 +00003749 break;
3750 }
3751 case CharcoalDrawingPreview:
3752 {
3753 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3754 exception);
cristy8cd5b312010-01-07 01:10:24 +00003755 (void) FormatMagickString(label,MaxTextExtent,"charcoal %.15gx%.15g",
3756 radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +00003757 break;
3758 }
3759 case JPEGPreview:
3760 {
3761 char
3762 filename[MaxTextExtent];
3763
3764 int
3765 file;
3766
3767 MagickBooleanType
3768 status;
3769
3770 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3771 if (preview_image == (Image *) NULL)
3772 break;
3773 preview_info->quality=(unsigned long) percentage;
3774 (void) FormatMagickString(factor,MaxTextExtent,"%lu",
3775 preview_info->quality);
3776 file=AcquireUniqueFileResource(filename);
3777 if (file != -1)
3778 file=close(file)-1;
3779 (void) FormatMagickString(preview_image->filename,MaxTextExtent,
3780 "jpeg:%s",filename);
3781 status=WriteImage(preview_info,preview_image);
3782 if (status != MagickFalse)
3783 {
3784 Image
3785 *quality_image;
3786
3787 (void) CopyMagickString(preview_info->filename,
3788 preview_image->filename,MaxTextExtent);
3789 quality_image=ReadImage(preview_info,exception);
3790 if (quality_image != (Image *) NULL)
3791 {
3792 preview_image=DestroyImage(preview_image);
3793 preview_image=quality_image;
3794 }
3795 }
3796 (void) RelinquishUniqueFileResource(preview_image->filename);
3797 if ((GetBlobSize(preview_image)/1024) >= 1024)
cristy8cd5b312010-01-07 01:10:24 +00003798 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%.15gmb ",
cristy3ed852e2009-09-05 21:47:34 +00003799 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3800 1024.0/1024.0);
3801 else
3802 if (GetBlobSize(preview_image) >= 1024)
cristy8cd5b312010-01-07 01:10:24 +00003803 (void) FormatMagickString(label,MaxTextExtent,
3804 "quality %s\n%.15gkb ",factor,(double) ((MagickOffsetType)
3805 GetBlobSize(preview_image))/1024.0);
cristy3ed852e2009-09-05 21:47:34 +00003806 else
3807 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%lub ",
3808 factor,(unsigned long) GetBlobSize(thumbnail));
3809 break;
3810 }
3811 }
3812 thumbnail=DestroyImage(thumbnail);
3813 percentage+=12.5;
3814 radius+=0.5;
3815 sigma+=0.25;
3816 if (preview_image == (Image *) NULL)
3817 break;
3818 (void) DeleteImageProperty(preview_image,"label");
3819 (void) SetImageProperty(preview_image,"label",label);
3820 AppendImageToList(&images,preview_image);
3821 proceed=SetImageProgress(image,PreviewImageTag,i,NumberTiles);
3822 if (proceed == MagickFalse)
3823 break;
3824 }
3825 if (images == (Image *) NULL)
3826 {
3827 preview_info=DestroyImageInfo(preview_info);
3828 return((Image *) NULL);
3829 }
3830 /*
3831 Create the montage.
3832 */
3833 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
3834 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
3835 montage_info->shadow=MagickTrue;
3836 (void) CloneString(&montage_info->tile,"3x3");
3837 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
3838 (void) CloneString(&montage_info->frame,DefaultTileFrame);
3839 montage_image=MontageImages(images,montage_info,exception);
3840 montage_info=DestroyMontageInfo(montage_info);
3841 images=DestroyImageList(images);
3842 if (montage_image == (Image *) NULL)
3843 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3844 if (montage_image->montage != (char *) NULL)
3845 {
3846 /*
3847 Free image directory.
3848 */
3849 montage_image->montage=(char *) RelinquishMagickMemory(
3850 montage_image->montage);
3851 if (image->directory != (char *) NULL)
3852 montage_image->directory=(char *) RelinquishMagickMemory(
3853 montage_image->directory);
3854 }
3855 preview_info=DestroyImageInfo(preview_info);
3856 return(montage_image);
3857}
3858
3859/*
3860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3861% %
3862% %
3863% %
3864% R a d i a l B l u r I m a g e %
3865% %
3866% %
3867% %
3868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3869%
3870% RadialBlurImage() applies a radial blur to the image.
3871%
3872% Andrew Protano contributed this effect.
3873%
3874% The format of the RadialBlurImage method is:
3875%
3876% Image *RadialBlurImage(const Image *image,const double angle,
3877% ExceptionInfo *exception)
3878% Image *RadialBlurImageChannel(const Image *image,const ChannelType channel,
3879% const double angle,ExceptionInfo *exception)
3880%
3881% A description of each parameter follows:
3882%
3883% o image: the image.
3884%
3885% o channel: the channel type.
3886%
3887% o angle: the angle of the radial blur.
3888%
3889% o exception: return any errors or warnings in this structure.
3890%
3891*/
3892
3893MagickExport Image *RadialBlurImage(const Image *image,const double angle,
3894 ExceptionInfo *exception)
3895{
3896 Image
3897 *blur_image;
3898
3899 blur_image=RadialBlurImageChannel(image,DefaultChannels,angle,exception);
3900 return(blur_image);
3901}
3902
3903MagickExport Image *RadialBlurImageChannel(const Image *image,
3904 const ChannelType channel,const double angle,ExceptionInfo *exception)
3905{
cristyc4c8d132010-01-07 01:58:38 +00003906 CacheView
3907 *blur_view,
3908 *image_view;
3909
cristy3ed852e2009-09-05 21:47:34 +00003910 Image
3911 *blur_image;
3912
3913 long
3914 progress,
3915 y;
3916
3917 MagickBooleanType
3918 status;
3919
3920 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00003921 bias;
cristy3ed852e2009-09-05 21:47:34 +00003922
3923 MagickRealType
3924 blur_radius,
3925 *cos_theta,
3926 offset,
3927 *sin_theta,
3928 theta;
3929
3930 PointInfo
3931 blur_center;
3932
3933 register long
3934 i;
3935
3936 unsigned long
3937 n;
3938
cristy3ed852e2009-09-05 21:47:34 +00003939 /*
3940 Allocate blur image.
3941 */
3942 assert(image != (Image *) NULL);
3943 assert(image->signature == MagickSignature);
3944 if (image->debug != MagickFalse)
3945 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3946 assert(exception != (ExceptionInfo *) NULL);
3947 assert(exception->signature == MagickSignature);
3948 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3949 if (blur_image == (Image *) NULL)
3950 return((Image *) NULL);
3951 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3952 {
3953 InheritException(exception,&blur_image->exception);
3954 blur_image=DestroyImage(blur_image);
3955 return((Image *) NULL);
3956 }
3957 blur_center.x=(double) image->columns/2.0;
3958 blur_center.y=(double) image->rows/2.0;
3959 blur_radius=hypot(blur_center.x,blur_center.y);
3960 n=(unsigned long) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+
3961 2UL);
3962 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
3963 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3964 sizeof(*cos_theta));
3965 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3966 sizeof(*sin_theta));
3967 if ((cos_theta == (MagickRealType *) NULL) ||
3968 (sin_theta == (MagickRealType *) NULL))
3969 {
3970 blur_image=DestroyImage(blur_image);
3971 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3972 }
3973 offset=theta*(MagickRealType) (n-1)/2.0;
3974 for (i=0; i < (long) n; i++)
3975 {
3976 cos_theta[i]=cos((double) (theta*i-offset));
3977 sin_theta[i]=sin((double) (theta*i-offset));
3978 }
3979 /*
3980 Radial blur image.
3981 */
3982 status=MagickTrue;
3983 progress=0;
cristyddd82202009-11-03 20:14:50 +00003984 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00003985 image_view=AcquireCacheView(image);
3986 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00003987#if defined(MAGICKCORE_OPENMP_SUPPORT)
3988 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003989#endif
3990 for (y=0; y < (long) blur_image->rows; y++)
3991 {
3992 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003993 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00003994
3995 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003996 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00003997
3998 register long
3999 x;
4000
4001 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004002 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004003
4004 if (status == MagickFalse)
4005 continue;
4006 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4007 exception);
4008 if (q == (PixelPacket *) NULL)
4009 {
4010 status=MagickFalse;
4011 continue;
4012 }
4013 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
4014 for (x=0; x < (long) blur_image->columns; x++)
4015 {
4016 MagickPixelPacket
4017 qixel;
4018
4019 MagickRealType
4020 normalize,
4021 radius;
4022
4023 PixelPacket
4024 pixel;
4025
4026 PointInfo
4027 center;
4028
4029 register long
4030 i;
4031
4032 unsigned long
4033 step;
4034
4035 center.x=(double) x-blur_center.x;
4036 center.y=(double) y-blur_center.y;
4037 radius=hypot((double) center.x,center.y);
4038 if (radius == 0)
4039 step=1;
4040 else
4041 {
4042 step=(unsigned long) (blur_radius/radius);
4043 if (step == 0)
4044 step=1;
4045 else
4046 if (step >= n)
4047 step=n-1;
4048 }
4049 normalize=0.0;
cristyddd82202009-11-03 20:14:50 +00004050 qixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004051 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4052 {
4053 for (i=0; i < (long) n; i+=step)
4054 {
4055 (void) GetOneCacheViewVirtualPixel(image_view,(long) (blur_center.x+
4056 center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),(long) (
4057 blur_center.y+center.x*sin_theta[i]+center.y*cos_theta[i]+0.5),
4058 &pixel,exception);
4059 qixel.red+=pixel.red;
4060 qixel.green+=pixel.green;
4061 qixel.blue+=pixel.blue;
4062 qixel.opacity+=pixel.opacity;
4063 if (image->colorspace == CMYKColorspace)
4064 {
4065 indexes=GetCacheViewVirtualIndexQueue(image_view);
4066 qixel.index+=(*indexes);
4067 }
4068 normalize+=1.0;
4069 }
4070 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4071 normalize);
4072 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004073 q->red=ClampToQuantum(normalize*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004074 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004075 q->green=ClampToQuantum(normalize*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004076 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004077 q->blue=ClampToQuantum(normalize*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004078 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004079 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004080 if (((channel & IndexChannel) != 0) &&
4081 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004082 blur_indexes[x]=(IndexPacket) ClampToQuantum(normalize*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004083 }
4084 else
4085 {
4086 MagickRealType
4087 alpha,
4088 gamma;
4089
4090 alpha=1.0;
4091 gamma=0.0;
4092 for (i=0; i < (long) n; i+=step)
4093 {
4094 (void) GetOneCacheViewVirtualPixel(image_view,(long) (blur_center.x+
4095 center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),(long) (
4096 blur_center.y+center.x*sin_theta[i]+center.y*cos_theta[i]+0.5),
4097 &pixel,exception);
4098 alpha=(MagickRealType) (QuantumScale*(QuantumRange-pixel.opacity));
4099 qixel.red+=alpha*pixel.red;
4100 qixel.green+=alpha*pixel.green;
4101 qixel.blue+=alpha*pixel.blue;
4102 qixel.opacity+=pixel.opacity;
4103 if (image->colorspace == CMYKColorspace)
4104 {
4105 indexes=GetCacheViewVirtualIndexQueue(image_view);
4106 qixel.index+=alpha*(*indexes);
4107 }
4108 gamma+=alpha;
4109 normalize+=1.0;
4110 }
4111 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4112 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4113 normalize);
4114 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004115 q->red=ClampToQuantum(gamma*qixel.red);
cristy3ed852e2009-09-05 21:47:34 +00004116 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004117 q->green=ClampToQuantum(gamma*qixel.green);
cristy3ed852e2009-09-05 21:47:34 +00004118 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004119 q->blue=ClampToQuantum(gamma*qixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00004120 if ((channel & OpacityChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004121 q->opacity=ClampToQuantum(normalize*qixel.opacity);
cristy3ed852e2009-09-05 21:47:34 +00004122 if (((channel & IndexChannel) != 0) &&
4123 (image->colorspace == CMYKColorspace))
cristyce70c172010-01-07 17:15:30 +00004124 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
cristy3ed852e2009-09-05 21:47:34 +00004125 }
4126 q++;
4127 }
4128 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
4129 status=MagickFalse;
4130 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4131 {
4132 MagickBooleanType
4133 proceed;
4134
cristyb5d5f722009-11-04 03:03:49 +00004135#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004136 #pragma omp critical (MagickCore_RadialBlurImageChannel)
4137#endif
4138 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
4139 if (proceed == MagickFalse)
4140 status=MagickFalse;
4141 }
4142 }
4143 blur_view=DestroyCacheView(blur_view);
4144 image_view=DestroyCacheView(image_view);
4145 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
4146 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
4147 if (status == MagickFalse)
4148 blur_image=DestroyImage(blur_image);
4149 return(blur_image);
4150}
4151
4152/*
4153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4154% %
4155% %
4156% %
4157% R e d u c e N o i s e I m a g e %
4158% %
4159% %
4160% %
4161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4162%
4163% ReduceNoiseImage() smooths the contours of an image while still preserving
4164% edge information. The algorithm works by replacing each pixel with its
4165% neighbor closest in value. A neighbor is defined by radius. Use a radius
4166% of 0 and ReduceNoise() selects a suitable radius for you.
4167%
4168% The format of the ReduceNoiseImage method is:
4169%
4170% Image *ReduceNoiseImage(const Image *image,const double radius,
4171% ExceptionInfo *exception)
4172%
4173% A description of each parameter follows:
4174%
4175% o image: the image.
4176%
4177% o radius: the radius of the pixel neighborhood.
4178%
4179% o exception: return any errors or warnings in this structure.
4180%
4181*/
4182
4183static MagickPixelPacket GetNonpeakMedianPixelList(MedianPixelList *pixel_list)
4184{
4185 MagickPixelPacket
4186 pixel;
4187
4188 register long
4189 channel;
4190
4191 register MedianSkipList
4192 *list;
4193
4194 unsigned long
4195 center,
4196 color,
4197 count,
4198 previous,
4199 next;
4200
4201 unsigned short
4202 channels[5];
4203
4204 /*
4205 Finds the median value for each of the color.
4206 */
4207 center=pixel_list->center;
4208 for (channel=0; channel < 5; channel++)
4209 {
4210 list=pixel_list->lists+channel;
4211 color=65536UL;
4212 next=list->nodes[color].next[0];
4213 count=0;
4214 do
4215 {
4216 previous=color;
4217 color=next;
4218 next=list->nodes[color].next[0];
4219 count+=list->nodes[color].count;
4220 }
4221 while (count <= center);
4222 if ((previous == 65536UL) && (next != 65536UL))
4223 color=next;
4224 else
4225 if ((previous != 65536UL) && (next == 65536UL))
4226 color=previous;
4227 channels[channel]=(unsigned short) color;
4228 }
4229 GetMagickPixelPacket((const Image *) NULL,&pixel);
4230 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4231 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4232 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4233 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4234 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4235 return(pixel);
4236}
4237
4238MagickExport Image *ReduceNoiseImage(const Image *image,const double radius,
4239 ExceptionInfo *exception)
4240{
4241#define ReduceNoiseImageTag "ReduceNoise/Image"
4242
cristyfa112112010-01-04 17:48:07 +00004243 CacheView
4244 *image_view,
4245 *noise_view;
4246
cristy3ed852e2009-09-05 21:47:34 +00004247 Image
4248 *noise_image;
4249
4250 long
4251 progress,
4252 y;
4253
4254 MagickBooleanType
4255 status;
4256
4257 MedianPixelList
cristyfa112112010-01-04 17:48:07 +00004258 **restrict pixel_list;
cristy3ed852e2009-09-05 21:47:34 +00004259
4260 unsigned long
4261 width;
4262
cristy3ed852e2009-09-05 21:47:34 +00004263 /*
4264 Initialize noise image attributes.
4265 */
4266 assert(image != (Image *) NULL);
4267 assert(image->signature == MagickSignature);
4268 if (image->debug != MagickFalse)
4269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4270 assert(exception != (ExceptionInfo *) NULL);
4271 assert(exception->signature == MagickSignature);
4272 width=GetOptimalKernelWidth2D(radius,0.5);
4273 if ((image->columns < width) || (image->rows < width))
4274 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
4275 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4276 exception);
4277 if (noise_image == (Image *) NULL)
4278 return((Image *) NULL);
4279 if (SetImageStorageClass(noise_image,DirectClass) == MagickFalse)
4280 {
4281 InheritException(exception,&noise_image->exception);
4282 noise_image=DestroyImage(noise_image);
4283 return((Image *) NULL);
4284 }
4285 pixel_list=AcquireMedianPixelListThreadSet(width);
4286 if (pixel_list == (MedianPixelList **) NULL)
4287 {
4288 noise_image=DestroyImage(noise_image);
4289 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4290 }
4291 /*
4292 Reduce noise image.
4293 */
4294 status=MagickTrue;
4295 progress=0;
4296 image_view=AcquireCacheView(image);
4297 noise_view=AcquireCacheView(noise_image);
cristyb5d5f722009-11-04 03:03:49 +00004298#if defined(MAGICKCORE_OPENMP_SUPPORT)
4299 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004300#endif
4301 for (y=0; y < (long) noise_image->rows; y++)
4302 {
4303 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004304 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004305
4306 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004307 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004308
4309 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004310 *restrict noise_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004311
4312 register long
4313 id,
4314 x;
4315
4316 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004317 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004318
4319 if (status == MagickFalse)
4320 continue;
4321 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
4322 2L),image->columns+width,width,exception);
4323 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
4324 exception);
4325 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4326 {
4327 status=MagickFalse;
4328 continue;
4329 }
4330 indexes=GetCacheViewVirtualIndexQueue(image_view);
4331 noise_indexes=GetCacheViewAuthenticIndexQueue(noise_view);
4332 id=GetOpenMPThreadId();
4333 for (x=0; x < (long) noise_image->columns; x++)
4334 {
4335 MagickPixelPacket
4336 pixel;
4337
4338 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004339 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004340
4341 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004342 *restrict s;
cristy3ed852e2009-09-05 21:47:34 +00004343
4344 register long
4345 u,
4346 v;
4347
4348 r=p;
4349 s=indexes+x;
4350 ResetMedianPixelList(pixel_list[id]);
4351 for (v=0; v < (long) width; v++)
4352 {
4353 for (u=0; u < (long) width; u++)
4354 InsertMedianPixelList(image,r+u,s+u,pixel_list[id]);
4355 r+=image->columns+width;
4356 s+=image->columns+width;
4357 }
4358 pixel=GetNonpeakMedianPixelList(pixel_list[id]);
4359 SetPixelPacket(noise_image,&pixel,q,noise_indexes+x);
4360 p++;
4361 q++;
4362 }
4363 if (SyncCacheViewAuthenticPixels(noise_view,exception) == MagickFalse)
4364 status=MagickFalse;
4365 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4366 {
4367 MagickBooleanType
4368 proceed;
4369
cristyb5d5f722009-11-04 03:03:49 +00004370#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004371 #pragma omp critical (MagickCore_ReduceNoiseImage)
4372#endif
4373 proceed=SetImageProgress(image,ReduceNoiseImageTag,progress++,
4374 image->rows);
4375 if (proceed == MagickFalse)
4376 status=MagickFalse;
4377 }
4378 }
4379 noise_view=DestroyCacheView(noise_view);
4380 image_view=DestroyCacheView(image_view);
4381 pixel_list=DestroyMedianPixelListThreadSet(pixel_list);
4382 return(noise_image);
4383}
4384
4385/*
4386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4387% %
4388% %
4389% %
4390% S e l e c t i v e B l u r I m a g e %
4391% %
4392% %
4393% %
4394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4395%
4396% SelectiveBlurImage() selectively blur pixels within a contrast threshold.
4397% It is similar to the unsharpen mask that sharpens everything with contrast
4398% above a certain threshold.
4399%
4400% The format of the SelectiveBlurImage method is:
4401%
4402% Image *SelectiveBlurImage(const Image *image,const double radius,
4403% const double sigma,const double threshold,ExceptionInfo *exception)
4404% Image *SelectiveBlurImageChannel(const Image *image,
4405% const ChannelType channel,const double radius,const double sigma,
4406% const double threshold,ExceptionInfo *exception)
4407%
4408% A description of each parameter follows:
4409%
4410% o image: the image.
4411%
4412% o channel: the channel type.
4413%
4414% o radius: the radius of the Gaussian, in pixels, not counting the center
4415% pixel.
4416%
4417% o sigma: the standard deviation of the Gaussian, in pixels.
4418%
4419% o threshold: only pixels within this contrast threshold are included
4420% in the blur operation.
4421%
4422% o exception: return any errors or warnings in this structure.
4423%
4424*/
4425
4426static inline MagickBooleanType SelectiveContrast(const PixelPacket *p,
4427 const PixelPacket *q,const double threshold)
4428{
4429 if (fabs(PixelIntensity(p)-PixelIntensity(q)) < threshold)
4430 return(MagickTrue);
4431 return(MagickFalse);
4432}
4433
4434MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
4435 const double sigma,const double threshold,ExceptionInfo *exception)
4436{
4437 Image
4438 *blur_image;
4439
4440 blur_image=SelectiveBlurImageChannel(image,DefaultChannels,radius,sigma,
4441 threshold,exception);
4442 return(blur_image);
4443}
4444
4445MagickExport Image *SelectiveBlurImageChannel(const Image *image,
4446 const ChannelType channel,const double radius,const double sigma,
4447 const double threshold,ExceptionInfo *exception)
4448{
4449#define SelectiveBlurImageTag "SelectiveBlur/Image"
4450
cristy47e00502009-12-17 19:19:57 +00004451 CacheView
4452 *blur_view,
4453 *image_view;
4454
cristy3ed852e2009-09-05 21:47:34 +00004455 double
cristy3ed852e2009-09-05 21:47:34 +00004456 *kernel;
4457
4458 Image
4459 *blur_image;
4460
4461 long
cristy47e00502009-12-17 19:19:57 +00004462 j,
cristy3ed852e2009-09-05 21:47:34 +00004463 progress,
cristy47e00502009-12-17 19:19:57 +00004464 u,
cristy3ed852e2009-09-05 21:47:34 +00004465 v,
4466 y;
4467
4468 MagickBooleanType
4469 status;
4470
4471 MagickPixelPacket
cristy3ed852e2009-09-05 21:47:34 +00004472 bias;
4473
4474 register long
cristy47e00502009-12-17 19:19:57 +00004475 i;
cristy3ed852e2009-09-05 21:47:34 +00004476
4477 unsigned long
4478 width;
4479
cristy3ed852e2009-09-05 21:47:34 +00004480 /*
4481 Initialize blur image attributes.
4482 */
4483 assert(image != (Image *) NULL);
4484 assert(image->signature == MagickSignature);
4485 if (image->debug != MagickFalse)
4486 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4487 assert(exception != (ExceptionInfo *) NULL);
4488 assert(exception->signature == MagickSignature);
4489 width=GetOptimalKernelWidth1D(radius,sigma);
4490 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
4491 if (kernel == (double *) NULL)
4492 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy47e00502009-12-17 19:19:57 +00004493 j=(long) width/2;
cristy3ed852e2009-09-05 21:47:34 +00004494 i=0;
cristy47e00502009-12-17 19:19:57 +00004495 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00004496 {
cristy47e00502009-12-17 19:19:57 +00004497 for (u=(-j); u <= j; u++)
4498 kernel[i++]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
4499 (2.0*MagickPI*MagickSigma*MagickSigma);
cristy3ed852e2009-09-05 21:47:34 +00004500 }
4501 if (image->debug != MagickFalse)
4502 {
4503 char
4504 format[MaxTextExtent],
4505 *message;
4506
4507 long
4508 u,
4509 v;
4510
4511 register const double
4512 *k;
4513
4514 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
4515 " SelectiveBlurImage with %ldx%ld kernel:",width,width);
4516 message=AcquireString("");
4517 k=kernel;
4518 for (v=0; v < (long) width; v++)
4519 {
4520 *message='\0';
4521 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
4522 (void) ConcatenateString(&message,format);
4523 for (u=0; u < (long) width; u++)
4524 {
4525 (void) FormatMagickString(format,MaxTextExtent,"%+f ",*k++);
4526 (void) ConcatenateString(&message,format);
4527 }
4528 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
4529 }
4530 message=DestroyString(message);
4531 }
4532 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4533 if (blur_image == (Image *) NULL)
4534 return((Image *) NULL);
4535 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4536 {
4537 InheritException(exception,&blur_image->exception);
4538 blur_image=DestroyImage(blur_image);
4539 return((Image *) NULL);
4540 }
4541 /*
4542 Threshold blur image.
4543 */
4544 status=MagickTrue;
4545 progress=0;
cristyddd82202009-11-03 20:14:50 +00004546 GetMagickPixelPacket(image,&bias);
4547 SetMagickPixelPacketBias(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00004548 image_view=AcquireCacheView(image);
4549 blur_view=AcquireCacheView(blur_image);
cristyb5d5f722009-11-04 03:03:49 +00004550#if defined(MAGICKCORE_OPENMP_SUPPORT)
4551 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004552#endif
4553 for (y=0; y < (long) image->rows; y++)
4554 {
4555 MagickBooleanType
4556 sync;
4557
4558 MagickRealType
4559 gamma;
4560
4561 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004562 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00004563
4564 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004565 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004566
4567 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004568 *restrict blur_indexes;
cristy3ed852e2009-09-05 21:47:34 +00004569
4570 register long
4571 x;
4572
4573 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004574 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004575
4576 if (status == MagickFalse)
4577 continue;
4578 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
4579 2L),image->columns+width,width,exception);
4580 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4581 exception);
4582 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4583 {
4584 status=MagickFalse;
4585 continue;
4586 }
4587 indexes=GetCacheViewVirtualIndexQueue(image_view);
4588 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
4589 for (x=0; x < (long) image->columns; x++)
4590 {
4591 long
4592 j,
4593 v;
4594
4595 MagickPixelPacket
4596 pixel;
4597
4598 register const double
cristyc47d1f82009-11-26 01:44:43 +00004599 *restrict k;
cristy3ed852e2009-09-05 21:47:34 +00004600
4601 register long
4602 u;
4603
cristyddd82202009-11-03 20:14:50 +00004604 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00004605 k=kernel;
4606 gamma=0.0;
4607 j=0;
4608 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4609 {
4610 for (v=0; v < (long) width; v++)
4611 {
4612 for (u=0; u < (long) width; u++)
4613 {
4614 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4615 {
4616 pixel.red+=(*k)*(p+u+j)->red;
4617 pixel.green+=(*k)*(p+u+j)->green;
4618 pixel.blue+=(*k)*(p+u+j)->blue;
4619 gamma+=(*k);
4620 k++;
4621 }
4622 }
4623 j+=image->columns+width;
4624 }
4625 if (gamma != 0.0)
4626 {
4627 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4628 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004629 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004630 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004631 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004632 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004633 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004634 }
4635 if ((channel & OpacityChannel) != 0)
4636 {
4637 gamma=0.0;
4638 j=0;
4639 for (v=0; v < (long) width; v++)
4640 {
4641 for (u=0; u < (long) width; u++)
4642 {
4643 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4644 {
4645 pixel.opacity+=(*k)*(p+u+j)->opacity;
4646 gamma+=(*k);
4647 k++;
4648 }
4649 }
4650 j+=image->columns+width;
4651 }
4652 if (gamma != 0.0)
4653 {
4654 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4655 gamma);
cristyce70c172010-01-07 17:15:30 +00004656 SetOpacityPixelComponent(q,ClampToQuantum(gamma*
4657 GetOpacityPixelComponent(&pixel)));
cristy3ed852e2009-09-05 21:47:34 +00004658 }
4659 }
4660 if (((channel & IndexChannel) != 0) &&
4661 (image->colorspace == CMYKColorspace))
4662 {
4663 gamma=0.0;
4664 j=0;
4665 for (v=0; v < (long) width; v++)
4666 {
4667 for (u=0; u < (long) width; u++)
4668 {
4669 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4670 {
4671 pixel.index+=(*k)*indexes[x+u+j];
4672 gamma+=(*k);
4673 k++;
4674 }
4675 }
4676 j+=image->columns+width;
4677 }
4678 if (gamma != 0.0)
4679 {
4680 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4681 gamma);
cristyce70c172010-01-07 17:15:30 +00004682 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004683 }
4684 }
4685 }
4686 else
4687 {
4688 MagickRealType
4689 alpha;
4690
4691 for (v=0; v < (long) width; v++)
4692 {
4693 for (u=0; u < (long) width; u++)
4694 {
4695 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4696 {
4697 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
4698 (p+u+j)->opacity));
4699 pixel.red+=(*k)*alpha*(p+u+j)->red;
4700 pixel.green+=(*k)*alpha*(p+u+j)->green;
4701 pixel.blue+=(*k)*alpha*(p+u+j)->blue;
4702 pixel.opacity+=(*k)*(p+u+j)->opacity;
4703 gamma+=(*k)*alpha;
4704 k++;
4705 }
4706 }
4707 j+=image->columns+width;
4708 }
4709 if (gamma != 0.0)
4710 {
4711 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4712 if ((channel & RedChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004713 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004714 if ((channel & GreenChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004715 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004716 if ((channel & BlueChannel) != 0)
cristyce70c172010-01-07 17:15:30 +00004717 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004718 }
4719 if ((channel & OpacityChannel) != 0)
4720 {
4721 gamma=0.0;
4722 j=0;
4723 for (v=0; v < (long) width; v++)
4724 {
4725 for (u=0; u < (long) width; u++)
4726 {
4727 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4728 {
4729 pixel.opacity+=(*k)*(p+u+j)->opacity;
4730 gamma+=(*k);
4731 k++;
4732 }
4733 }
4734 j+=image->columns+width;
4735 }
4736 if (gamma != 0.0)
4737 {
4738 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4739 gamma);
cristyce70c172010-01-07 17:15:30 +00004740 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004741 }
4742 }
4743 if (((channel & IndexChannel) != 0) &&
4744 (image->colorspace == CMYKColorspace))
4745 {
4746 gamma=0.0;
4747 j=0;
4748 for (v=0; v < (long) width; v++)
4749 {
4750 for (u=0; u < (long) width; u++)
4751 {
4752 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4753 {
4754 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
4755 (p+u+j)->opacity));
4756 pixel.index+=(*k)*alpha*indexes[x+u+j];
4757 gamma+=(*k);
4758 k++;
4759 }
4760 }
4761 j+=image->columns+width;
4762 }
4763 if (gamma != 0.0)
4764 {
4765 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4766 gamma);
cristyce70c172010-01-07 17:15:30 +00004767 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00004768 }
4769 }
4770 }
4771 p++;
4772 q++;
4773 }
4774 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
4775 if (sync == MagickFalse)
4776 status=MagickFalse;
4777 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4778 {
4779 MagickBooleanType
4780 proceed;
4781
cristyb5d5f722009-11-04 03:03:49 +00004782#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004783 #pragma omp critical (MagickCore_SelectiveBlurImageChannel)
4784#endif
4785 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
4786 image->rows);
4787 if (proceed == MagickFalse)
4788 status=MagickFalse;
4789 }
4790 }
4791 blur_image->type=image->type;
4792 blur_view=DestroyCacheView(blur_view);
4793 image_view=DestroyCacheView(image_view);
4794 kernel=(double *) RelinquishMagickMemory(kernel);
4795 if (status == MagickFalse)
4796 blur_image=DestroyImage(blur_image);
4797 return(blur_image);
4798}
4799
4800/*
4801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4802% %
4803% %
4804% %
4805% S h a d e I m a g e %
4806% %
4807% %
4808% %
4809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4810%
4811% ShadeImage() shines a distant light on an image to create a
4812% three-dimensional effect. You control the positioning of the light with
4813% azimuth and elevation; azimuth is measured in degrees off the x axis
4814% and elevation is measured in pixels above the Z axis.
4815%
4816% The format of the ShadeImage method is:
4817%
4818% Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4819% const double azimuth,const double elevation,ExceptionInfo *exception)
4820%
4821% A description of each parameter follows:
4822%
4823% o image: the image.
4824%
4825% o gray: A value other than zero shades the intensity of each pixel.
4826%
4827% o azimuth, elevation: Define the light source direction.
4828%
4829% o exception: return any errors or warnings in this structure.
4830%
4831*/
4832MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4833 const double azimuth,const double elevation,ExceptionInfo *exception)
4834{
4835#define ShadeImageTag "Shade/Image"
4836
cristyc4c8d132010-01-07 01:58:38 +00004837 CacheView
4838 *image_view,
4839 *shade_view;
4840
cristy3ed852e2009-09-05 21:47:34 +00004841 Image
4842 *shade_image;
4843
4844 long
4845 progress,
4846 y;
4847
4848 MagickBooleanType
4849 status;
4850
4851 PrimaryInfo
4852 light;
4853
cristy3ed852e2009-09-05 21:47:34 +00004854 /*
4855 Initialize shaded image attributes.
4856 */
4857 assert(image != (const Image *) NULL);
4858 assert(image->signature == MagickSignature);
4859 if (image->debug != MagickFalse)
4860 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4861 assert(exception != (ExceptionInfo *) NULL);
4862 assert(exception->signature == MagickSignature);
4863 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4864 if (shade_image == (Image *) NULL)
4865 return((Image *) NULL);
4866 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
4867 {
4868 InheritException(exception,&shade_image->exception);
4869 shade_image=DestroyImage(shade_image);
4870 return((Image *) NULL);
4871 }
4872 /*
4873 Compute the light vector.
4874 */
4875 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
4876 cos(DegreesToRadians(elevation));
4877 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
4878 cos(DegreesToRadians(elevation));
4879 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
4880 /*
4881 Shade image.
4882 */
4883 status=MagickTrue;
4884 progress=0;
4885 image_view=AcquireCacheView(image);
4886 shade_view=AcquireCacheView(shade_image);
cristyb5d5f722009-11-04 03:03:49 +00004887#if defined(MAGICKCORE_OPENMP_SUPPORT)
4888 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004889#endif
4890 for (y=0; y < (long) image->rows; y++)
4891 {
4892 MagickRealType
4893 distance,
4894 normal_distance,
4895 shade;
4896
4897 PrimaryInfo
4898 normal;
4899
4900 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004901 *restrict p,
4902 *restrict s0,
4903 *restrict s1,
4904 *restrict s2;
cristy3ed852e2009-09-05 21:47:34 +00004905
4906 register long
4907 x;
4908
4909 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004910 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004911
4912 if (status == MagickFalse)
4913 continue;
4914 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
4915 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
4916 exception);
4917 if ((p == (PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4918 {
4919 status=MagickFalse;
4920 continue;
4921 }
4922 /*
4923 Shade this row of pixels.
4924 */
4925 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
4926 s0=p+1;
4927 s1=s0+image->columns+2;
4928 s2=s1+image->columns+2;
4929 for (x=0; x < (long) image->columns; x++)
4930 {
4931 /*
4932 Determine the surface normal and compute shading.
4933 */
4934 normal.x=(double) (PixelIntensity(s0-1)+PixelIntensity(s1-1)+
4935 PixelIntensity(s2-1)-PixelIntensity(s0+1)-PixelIntensity(s1+1)-
4936 PixelIntensity(s2+1));
4937 normal.y=(double) (PixelIntensity(s2-1)+PixelIntensity(s2)+
4938 PixelIntensity(s2+1)-PixelIntensity(s0-1)-PixelIntensity(s0)-
4939 PixelIntensity(s0+1));
4940 if ((normal.x == 0.0) && (normal.y == 0.0))
4941 shade=light.z;
4942 else
4943 {
4944 shade=0.0;
4945 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
4946 if (distance > MagickEpsilon)
4947 {
4948 normal_distance=
4949 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
4950 if (normal_distance > (MagickEpsilon*MagickEpsilon))
4951 shade=distance/sqrt((double) normal_distance);
4952 }
4953 }
4954 if (gray != MagickFalse)
4955 {
4956 q->red=(Quantum) shade;
4957 q->green=(Quantum) shade;
4958 q->blue=(Quantum) shade;
4959 }
4960 else
4961 {
cristyce70c172010-01-07 17:15:30 +00004962 q->red=ClampToQuantum(QuantumScale*shade*s1->red);
4963 q->green=ClampToQuantum(QuantumScale*shade*s1->green);
4964 q->blue=ClampToQuantum(QuantumScale*shade*s1->blue);
cristy3ed852e2009-09-05 21:47:34 +00004965 }
4966 q->opacity=s1->opacity;
4967 s0++;
4968 s1++;
4969 s2++;
4970 q++;
4971 }
4972 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
4973 status=MagickFalse;
4974 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4975 {
4976 MagickBooleanType
4977 proceed;
4978
cristyb5d5f722009-11-04 03:03:49 +00004979#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004980 #pragma omp critical (MagickCore_ShadeImage)
4981#endif
4982 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
4983 if (proceed == MagickFalse)
4984 status=MagickFalse;
4985 }
4986 }
4987 shade_view=DestroyCacheView(shade_view);
4988 image_view=DestroyCacheView(image_view);
4989 if (status == MagickFalse)
4990 shade_image=DestroyImage(shade_image);
4991 return(shade_image);
4992}
4993
4994/*
4995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4996% %
4997% %
4998% %
4999% S h a r p e n I m a g e %
5000% %
5001% %
5002% %
5003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5004%
5005% SharpenImage() sharpens the image. We convolve the image with a Gaussian
5006% operator of the given radius and standard deviation (sigma). For
5007% reasonable results, radius should be larger than sigma. Use a radius of 0
5008% and SharpenImage() selects a suitable radius for you.
5009%
5010% Using a separable kernel would be faster, but the negative weights cancel
5011% out on the corners of the kernel producing often undesirable ringing in the
5012% filtered result; this can be avoided by using a 2D gaussian shaped image
5013% sharpening kernel instead.
5014%
5015% The format of the SharpenImage method is:
5016%
5017% Image *SharpenImage(const Image *image,const double radius,
5018% const double sigma,ExceptionInfo *exception)
5019% Image *SharpenImageChannel(const Image *image,const ChannelType channel,
5020% const double radius,const double sigma,ExceptionInfo *exception)
5021%
5022% A description of each parameter follows:
5023%
5024% o image: the image.
5025%
5026% o channel: the channel type.
5027%
5028% o radius: the radius of the Gaussian, in pixels, not counting the center
5029% pixel.
5030%
5031% o sigma: the standard deviation of the Laplacian, in pixels.
5032%
5033% o exception: return any errors or warnings in this structure.
5034%
5035*/
5036
5037MagickExport Image *SharpenImage(const Image *image,const double radius,
5038 const double sigma,ExceptionInfo *exception)
5039{
5040 Image
5041 *sharp_image;
5042
5043 sharp_image=SharpenImageChannel(image,DefaultChannels,radius,sigma,exception);
5044 return(sharp_image);
5045}
5046
5047MagickExport Image *SharpenImageChannel(const Image *image,
5048 const ChannelType channel,const double radius,const double sigma,
5049 ExceptionInfo *exception)
5050{
5051 double
cristy47e00502009-12-17 19:19:57 +00005052 *kernel,
5053 normalize;
cristy3ed852e2009-09-05 21:47:34 +00005054
5055 Image
5056 *sharp_image;
5057
cristy47e00502009-12-17 19:19:57 +00005058 long
5059 j,
cristy3ed852e2009-09-05 21:47:34 +00005060 u,
5061 v;
5062
cristy47e00502009-12-17 19:19:57 +00005063 register long
5064 i;
5065
cristy3ed852e2009-09-05 21:47:34 +00005066 unsigned long
5067 width;
5068
5069 assert(image != (const Image *) NULL);
5070 assert(image->signature == MagickSignature);
5071 if (image->debug != MagickFalse)
5072 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5073 assert(exception != (ExceptionInfo *) NULL);
5074 assert(exception->signature == MagickSignature);
5075 width=GetOptimalKernelWidth2D(radius,sigma);
5076 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
5077 if (kernel == (double *) NULL)
5078 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00005079 normalize=0.0;
cristy47e00502009-12-17 19:19:57 +00005080 j=(long) width/2;
5081 i=0;
5082 for (v=(-j); v <= j; v++)
cristy3ed852e2009-09-05 21:47:34 +00005083 {
cristy47e00502009-12-17 19:19:57 +00005084 for (u=(-j); u <= j; u++)
cristy3ed852e2009-09-05 21:47:34 +00005085 {
cristy47e00502009-12-17 19:19:57 +00005086 kernel[i]=(-exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
5087 (2.0*MagickPI*MagickSigma*MagickSigma));
cristy3ed852e2009-09-05 21:47:34 +00005088 normalize+=kernel[i];
5089 i++;
5090 }
5091 }
5092 kernel[i/2]=(double) ((-2.0)*normalize);
5093 sharp_image=ConvolveImageChannel(image,channel,width,kernel,exception);
5094 kernel=(double *) RelinquishMagickMemory(kernel);
5095 return(sharp_image);
5096}
5097
5098/*
5099%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5100% %
5101% %
5102% %
5103% S p r e a d I m a g e %
5104% %
5105% %
5106% %
5107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5108%
5109% SpreadImage() is a special effects method that randomly displaces each
5110% pixel in a block defined by the radius parameter.
5111%
5112% The format of the SpreadImage method is:
5113%
5114% Image *SpreadImage(const Image *image,const double radius,
5115% ExceptionInfo *exception)
5116%
5117% A description of each parameter follows:
5118%
5119% o image: the image.
5120%
5121% o radius: Choose a random pixel in a neighborhood of this extent.
5122%
5123% o exception: return any errors or warnings in this structure.
5124%
5125*/
5126MagickExport Image *SpreadImage(const Image *image,const double radius,
5127 ExceptionInfo *exception)
5128{
5129#define SpreadImageTag "Spread/Image"
5130
cristyfa112112010-01-04 17:48:07 +00005131 CacheView
5132 *image_view;
5133
cristy3ed852e2009-09-05 21:47:34 +00005134 Image
5135 *spread_image;
5136
5137 long
5138 progress,
5139 y;
5140
5141 MagickBooleanType
5142 status;
5143
5144 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005145 bias;
cristy3ed852e2009-09-05 21:47:34 +00005146
5147 RandomInfo
cristyfa112112010-01-04 17:48:07 +00005148 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00005149
5150 ResampleFilter
cristyfa112112010-01-04 17:48:07 +00005151 **restrict resample_filter;
cristy3ed852e2009-09-05 21:47:34 +00005152
5153 unsigned long
5154 width;
5155
cristy3ed852e2009-09-05 21:47:34 +00005156 /*
5157 Initialize spread image attributes.
5158 */
5159 assert(image != (Image *) NULL);
5160 assert(image->signature == MagickSignature);
5161 if (image->debug != MagickFalse)
5162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5163 assert(exception != (ExceptionInfo *) NULL);
5164 assert(exception->signature == MagickSignature);
5165 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5166 exception);
5167 if (spread_image == (Image *) NULL)
5168 return((Image *) NULL);
5169 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
5170 {
5171 InheritException(exception,&spread_image->exception);
5172 spread_image=DestroyImage(spread_image);
5173 return((Image *) NULL);
5174 }
5175 /*
5176 Spread image.
5177 */
5178 status=MagickTrue;
5179 progress=0;
cristyddd82202009-11-03 20:14:50 +00005180 GetMagickPixelPacket(spread_image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005181 width=GetOptimalKernelWidth1D(radius,0.5);
5182 resample_filter=AcquireResampleFilterThreadSet(image,MagickTrue,exception);
5183 random_info=AcquireRandomInfoThreadSet();
5184 image_view=AcquireCacheView(spread_image);
cristyb5d5f722009-11-04 03:03:49 +00005185#if defined(MAGICKCORE_OPENMP_SUPPORT)
5186 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005187#endif
5188 for (y=0; y < (long) spread_image->rows; y++)
5189 {
5190 MagickPixelPacket
5191 pixel;
5192
5193 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005194 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005195
5196 register long
5197 id,
5198 x;
5199
5200 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005201 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005202
5203 if (status == MagickFalse)
5204 continue;
5205 q=QueueCacheViewAuthenticPixels(image_view,0,y,spread_image->columns,1,
5206 exception);
5207 if (q == (PixelPacket *) NULL)
5208 {
5209 status=MagickFalse;
5210 continue;
5211 }
5212 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristyddd82202009-11-03 20:14:50 +00005213 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00005214 id=GetOpenMPThreadId();
5215 for (x=0; x < (long) spread_image->columns; x++)
5216 {
5217 (void) ResamplePixelColor(resample_filter[id],(double) x+width*
5218 (GetPseudoRandomValue(random_info[id])-0.5),(double) y+width*
5219 (GetPseudoRandomValue(random_info[id])-0.5),&pixel);
5220 SetPixelPacket(spread_image,&pixel,q,indexes+x);
5221 q++;
5222 }
5223 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5224 status=MagickFalse;
5225 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5226 {
5227 MagickBooleanType
5228 proceed;
5229
cristyb5d5f722009-11-04 03:03:49 +00005230#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005231 #pragma omp critical (MagickCore_SpreadImage)
5232#endif
5233 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
5234 if (proceed == MagickFalse)
5235 status=MagickFalse;
5236 }
5237 }
5238 image_view=DestroyCacheView(image_view);
5239 random_info=DestroyRandomInfoThreadSet(random_info);
5240 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
5241 return(spread_image);
5242}
5243
5244/*
5245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5246% %
5247% %
5248% %
5249% U n s h a r p M a s k I m a g e %
5250% %
5251% %
5252% %
5253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5254%
5255% UnsharpMaskImage() sharpens one or more image channels. We convolve the
5256% image with a Gaussian operator of the given radius and standard deviation
5257% (sigma). For reasonable results, radius should be larger than sigma. Use a
5258% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5259%
5260% The format of the UnsharpMaskImage method is:
5261%
5262% Image *UnsharpMaskImage(const Image *image,const double radius,
5263% const double sigma,const double amount,const double threshold,
5264% ExceptionInfo *exception)
5265% Image *UnsharpMaskImageChannel(const Image *image,
5266% const ChannelType channel,const double radius,const double sigma,
5267% const double amount,const double threshold,ExceptionInfo *exception)
5268%
5269% A description of each parameter follows:
5270%
5271% o image: the image.
5272%
5273% o channel: the channel type.
5274%
5275% o radius: the radius of the Gaussian, in pixels, not counting the center
5276% pixel.
5277%
5278% o sigma: the standard deviation of the Gaussian, in pixels.
5279%
5280% o amount: the percentage of the difference between the original and the
5281% blur image that is added back into the original.
5282%
5283% o threshold: the threshold in pixels needed to apply the diffence amount.
5284%
5285% o exception: return any errors or warnings in this structure.
5286%
5287*/
5288
5289MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
5290 const double sigma,const double amount,const double threshold,
5291 ExceptionInfo *exception)
5292{
5293 Image
5294 *sharp_image;
5295
5296 sharp_image=UnsharpMaskImageChannel(image,DefaultChannels,radius,sigma,amount,
5297 threshold,exception);
5298 return(sharp_image);
5299}
5300
5301MagickExport Image *UnsharpMaskImageChannel(const Image *image,
5302 const ChannelType channel,const double radius,const double sigma,
5303 const double amount,const double threshold,ExceptionInfo *exception)
5304{
5305#define SharpenImageTag "Sharpen/Image"
5306
cristyc4c8d132010-01-07 01:58:38 +00005307 CacheView
5308 *image_view,
5309 *unsharp_view;
5310
cristy3ed852e2009-09-05 21:47:34 +00005311 Image
5312 *unsharp_image;
5313
5314 long
5315 progress,
5316 y;
5317
5318 MagickBooleanType
5319 status;
5320
5321 MagickPixelPacket
cristyddd82202009-11-03 20:14:50 +00005322 bias;
cristy3ed852e2009-09-05 21:47:34 +00005323
5324 MagickRealType
5325 quantum_threshold;
5326
cristy3ed852e2009-09-05 21:47:34 +00005327 assert(image != (const Image *) NULL);
5328 assert(image->signature == MagickSignature);
5329 if (image->debug != MagickFalse)
5330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5331 assert(exception != (ExceptionInfo *) NULL);
5332 unsharp_image=BlurImageChannel(image,channel,radius,sigma,exception);
5333 if (unsharp_image == (Image *) NULL)
5334 return((Image *) NULL);
5335 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5336 /*
5337 Unsharp-mask image.
5338 */
5339 status=MagickTrue;
5340 progress=0;
cristyddd82202009-11-03 20:14:50 +00005341 GetMagickPixelPacket(image,&bias);
cristy3ed852e2009-09-05 21:47:34 +00005342 image_view=AcquireCacheView(image);
5343 unsharp_view=AcquireCacheView(unsharp_image);
cristyb5d5f722009-11-04 03:03:49 +00005344#if defined(MAGICKCORE_OPENMP_SUPPORT)
5345 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005346#endif
5347 for (y=0; y < (long) image->rows; y++)
5348 {
5349 MagickPixelPacket
5350 pixel;
5351
5352 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005353 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00005354
5355 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005356 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005357
5358 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005359 *restrict unsharp_indexes;
cristy3ed852e2009-09-05 21:47:34 +00005360
5361 register long
5362 x;
5363
5364 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005365 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005366
5367 if (status == MagickFalse)
5368 continue;
5369 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5370 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5371 exception);
5372 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5373 {
5374 status=MagickFalse;
5375 continue;
5376 }
5377 indexes=GetCacheViewVirtualIndexQueue(image_view);
5378 unsharp_indexes=GetCacheViewAuthenticIndexQueue(unsharp_view);
cristyddd82202009-11-03 20:14:50 +00005379 pixel=bias;
cristy3ed852e2009-09-05 21:47:34 +00005380 for (x=0; x < (long) image->columns; x++)
5381 {
5382 if ((channel & RedChannel) != 0)
5383 {
5384 pixel.red=p->red-(MagickRealType) q->red;
5385 if (fabs(2.0*pixel.red) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005386 pixel.red=(MagickRealType) GetRedPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005387 else
5388 pixel.red=(MagickRealType) p->red+(pixel.red*amount);
cristyce70c172010-01-07 17:15:30 +00005389 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005390 }
5391 if ((channel & GreenChannel) != 0)
5392 {
5393 pixel.green=p->green-(MagickRealType) q->green;
5394 if (fabs(2.0*pixel.green) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005395 pixel.green=(MagickRealType) GetGreenPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005396 else
5397 pixel.green=(MagickRealType) p->green+(pixel.green*amount);
cristyce70c172010-01-07 17:15:30 +00005398 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005399 }
5400 if ((channel & BlueChannel) != 0)
5401 {
5402 pixel.blue=p->blue-(MagickRealType) q->blue;
5403 if (fabs(2.0*pixel.blue) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005404 pixel.blue=(MagickRealType) GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005405 else
5406 pixel.blue=(MagickRealType) p->blue+(pixel.blue*amount);
cristyce70c172010-01-07 17:15:30 +00005407 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005408 }
5409 if ((channel & OpacityChannel) != 0)
5410 {
5411 pixel.opacity=p->opacity-(MagickRealType) q->opacity;
5412 if (fabs(2.0*pixel.opacity) < quantum_threshold)
cristyce70c172010-01-07 17:15:30 +00005413 pixel.opacity=(MagickRealType) GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +00005414 else
5415 pixel.opacity=p->opacity+(pixel.opacity*amount);
cristyce70c172010-01-07 17:15:30 +00005416 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00005417 }
5418 if (((channel & IndexChannel) != 0) &&
5419 (image->colorspace == CMYKColorspace))
5420 {
5421 pixel.index=unsharp_indexes[x]-(MagickRealType) indexes[x];
5422 if (fabs(2.0*pixel.index) < quantum_threshold)
5423 pixel.index=(MagickRealType) unsharp_indexes[x];
5424 else
5425 pixel.index=(MagickRealType) unsharp_indexes[x]+(pixel.index*
5426 amount);
cristyce70c172010-01-07 17:15:30 +00005427 unsharp_indexes[x]=ClampToQuantum(pixel.index);
cristy3ed852e2009-09-05 21:47:34 +00005428 }
5429 p++;
5430 q++;
5431 }
5432 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5433 status=MagickFalse;
5434 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5435 {
5436 MagickBooleanType
5437 proceed;
5438
cristyb5d5f722009-11-04 03:03:49 +00005439#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005440 #pragma omp critical (MagickCore_UnsharpMaskImageChannel)
5441#endif
5442 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5443 if (proceed == MagickFalse)
5444 status=MagickFalse;
5445 }
5446 }
5447 unsharp_image->type=image->type;
5448 unsharp_view=DestroyCacheView(unsharp_view);
5449 image_view=DestroyCacheView(image_view);
5450 if (status == MagickFalse)
5451 unsharp_image=DestroyImage(unsharp_image);
5452 return(unsharp_image);
5453}